r98324 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98323‎ | r98324 | r98325 >
Date:16:23, 28 September 2011
Author:siebrand
Status:ok
Tags:
Comment:
Partial revert of r98323: no need to move Checker.php for MediaWiki.
Modified paths:
  • /trunk/extensions/Translate/ffs/MediaWikiChecker.php (deleted) (history)
  • /trunk/extensions/Translate/groups/MediaWiki/Checker.php (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/groups/MediaWiki/Checker.php
@@ -0,0 +1,250 @@
 2+<?php
 3+/**
 4+ * Implements MessageChecker for %MediaWiki.
 5+ *
 6+ * @file
 7+ * @author Niklas Laxström
 8+ * @copyright Copyright © 2008-2010, Niklas Laxström
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ */
 11+
 12+/**
 13+ * %MediaWiki specific message checks.
 14+ *
 15+ * @ingroup MessageCheckers
 16+ */
 17+class MediaWikiMessageChecker extends MessageChecker {
 18+ /**
 19+ * Checks if the translation uses all variables $[1-9] that the definition
 20+ * uses and vice versa.
 21+ *
 22+ * @param $messages \array Iterable list of TMessage objects.
 23+ * @param $code \string Language code of the translations.
 24+ * @param $warnings \array Array where warnings are appended to.
 25+ */
 26+ protected function wikiParameterCheck( $messages, $code, &$warnings ) {
 27+ return parent::parameterCheck( $messages, $code, $warnings, '/\$[1-9]/' );
 28+ }
 29+
 30+ /**
 31+ * Checks if the translation uses links that are discouraged. Valid links are
 32+ * those that link to Special: or {{ns:special}}: or project pages trough
 33+ * MediaWiki messages like {{MediaWiki:helppage-url}}:. Also links in the
 34+ * definition are allowed.
 35+ *
 36+ * @param $messages \array Iterable list of TMessage objects.
 37+ * @param $code \string Language code of the translations.
 38+ * @param $warnings \array Array where warnings are appended to.
 39+ */
 40+ protected function wikiLinksCheck( $messages, $code, &$warnings ) {
 41+ $tc = Title::legalChars() . '#%{}';
 42+
 43+ foreach ( $messages as $message ) {
 44+ $key = $message->key();
 45+ $definition = $message->definition();
 46+ $translation = $message->translation();
 47+
 48+ $subcheck = 'extra';
 49+ $matches = $links = array();
 50+ preg_match_all( "/\[\[([{$tc}]+)(\\|(.+?))?]]/sDu", $translation, $matches );
 51+ for ( $i = 0; $i < count( $matches[0] ); $i++ ) {
 52+ $backMatch = preg_quote( $matches[1][$i], '/' );
 53+
 54+ if ( preg_match( "/\[\[$backMatch/", $definition ) ) {
 55+ continue;
 56+ }
 57+
 58+ $links[] = "[[{$matches[1][$i]}{$matches[2][$i]}]]";
 59+ }
 60+
 61+ if ( count( $links ) ) {
 62+ $warnings[$key][] = array(
 63+ array( 'links', $subcheck, $key, $code ),
 64+ 'translate-checks-links',
 65+ array( 'PARAMS', $links ),
 66+ array( 'COUNT', count( $links ) ),
 67+ );
 68+ }
 69+
 70+ $subcheck = 'missing';
 71+ $matches = $links = array();
 72+ preg_match_all( "/\[\[([{$tc}]+)(\\|(.+?))?]]/sDu", $definition, $matches );
 73+ for ( $i = 0; $i < count( $matches[0] ); $i++ ) {
 74+ $backMatch = preg_quote( $matches[1][$i], '/' );
 75+
 76+ if ( preg_match( "/\[\[$backMatch/", $translation ) ) {
 77+ continue;
 78+ }
 79+
 80+ $links[] = "[[{$matches[1][$i]}{$matches[2][$i]}]]";
 81+ }
 82+
 83+ if ( count( $links ) ) {
 84+ $warnings[$key][] = array(
 85+ array( 'links', $subcheck, $key, $code ),
 86+ 'translate-checks-links-missing',
 87+ array( 'PARAMS', $links ),
 88+ array( 'COUNT', count( $links ) ),
 89+ );
 90+ }
 91+ }
 92+ }
 93+
 94+ /**
 95+ * Checks if the \<br /> and \<hr /> tags are using the correct syntax.
 96+ *
 97+ * @param $messages \array Iterable list of TMessage objects.
 98+ * @param $code \string Language code of the translations.
 99+ * @param $warnings \array Array where warnings are appended to.
 100+ */
 101+ protected function XhtmlCheck( $messages, $code, &$warnings ) {
 102+ foreach ( $messages as $message ) {
 103+ $key = $message->key();
 104+ $translation = $message->translation();
 105+ if ( strpos( $translation, '<' ) === false ) {
 106+ continue;
 107+ }
 108+
 109+ $subcheck = 'invalid';
 110+ $tags = array(
 111+ '~<hr *(\\\\)?>~suDi' => '<hr />', // Wrong syntax
 112+ '~<br *(\\\\)?>~suDi' => '<br />',
 113+ '~<hr/>~suDi' => '<hr />', // Wrong syntax
 114+ '~<br/>~suDi' => '<br />',
 115+ '~<(HR|Hr|hR) />~su' => '<hr />', // Case
 116+ '~<(BR|Br|bR) />~su' => '<br />',
 117+ );
 118+
 119+ $wrongTags = array();
 120+ foreach ( $tags as $wrong => $correct ) {
 121+ $matches = array();
 122+ preg_match_all( $wrong, $translation, $matches, PREG_PATTERN_ORDER );
 123+ foreach ( $matches[0] as $wrongMatch ) {
 124+ $wrongTags[$wrongMatch] = "$wrongMatch → $correct";
 125+ }
 126+ }
 127+
 128+ if ( count( $wrongTags ) ) {
 129+ $warnings[$key][] = array(
 130+ array( 'xhtml', $subcheck, $key, $code ),
 131+ 'translate-checks-xhtml',
 132+ array( 'PARAMS', $wrongTags ),
 133+ array( 'COUNT', count( $wrongTags ) ),
 134+ );
 135+ }
 136+ }
 137+ }
 138+
 139+ /**
 140+ * Checks if the translation doesn't use plural while the definition has one.
 141+ *
 142+ * @param $messages \array Iterable list of TMessage objects.
 143+ * @param $code \string Language code of the translations.
 144+ * @param $warnings \array Array where warnings are appended to.
 145+ */
 146+ protected function pluralCheck( $messages, $code, &$warnings ) {
 147+ foreach ( $messages as $message ) {
 148+ $key = $message->key();
 149+ $definition = $message->definition();
 150+ $translation = $message->translation();
 151+
 152+ $subcheck = 'missing';
 153+ if (
 154+ stripos( $definition, '{{plural:' ) !== false &&
 155+ stripos( $translation, '{{plural:' ) === false
 156+ ) {
 157+ $warnings[$key][] = array(
 158+ array( 'plural', $subcheck, $key, $code ),
 159+ 'translate-checks-plural',
 160+ );
 161+ }
 162+ }
 163+ }
 164+
 165+ /**
 166+ * Checks for page names that they have an untranslated namespace.
 167+ *
 168+ * @param $messages \array Iterable list of TMessage objects.
 169+ * @param $code \string Language code of the translations.
 170+ * @param $warnings \array Array where warnings are appended to.
 171+ */
 172+ protected function pagenameMessagesCheck( $messages, $code, &$warnings ) {
 173+ foreach ( $messages as $message ) {
 174+ $key = $message->key();
 175+ $definition = $message->definition();
 176+ $translation = $message->translation();
 177+
 178+ $subcheck = 'namespace';
 179+ $namespaces = 'help|project|\{\{ns:project}}|mediawiki';
 180+ $matches = array();
 181+ if ( preg_match( "/^($namespaces):[\w\s]+$/ui", $definition, $matches ) ) {
 182+ if ( !preg_match( "/^{$matches[1]}:.+$/u", $translation ) ) {
 183+ $warnings[$key][] = array(
 184+ array( 'pagename', $subcheck, $key, $code ),
 185+ 'translate-checks-pagename',
 186+ );
 187+ }
 188+ }
 189+ }
 190+ }
 191+
 192+ /**
 193+ * Checks for some miscellaneous messages with special syntax.
 194+ *
 195+ * @param $messages \array Iterable list of TMessage objects.
 196+ * @param $code \string Language code of the translations.
 197+ * @param $warnings \array Array where warnings are appended to.
 198+ */
 199+ protected function miscMWChecks( $messages, $code, &$warnings ) {
 200+ $timeList = array( 'protect-expiry-options', 'ipboptions' );
 201+
 202+ foreach ( $messages as $message ) {
 203+ $key = $message->key();
 204+ $definition = $message->definition();
 205+ $translation = $message->translation();
 206+
 207+
 208+ if ( in_array( strtolower( $key ), $timeList, true ) ) {
 209+ $defArray = explode( ',', $definition );
 210+ $traArray = explode( ',', $translation );
 211+
 212+ $subcheck = 'timelist-count';
 213+ $defCount = count( $defArray );
 214+ $traCount = count( $traArray );
 215+ if ( $defCount !== $traCount ) {
 216+ $warnings[$key][] = array(
 217+ array( 'miscmw', $subcheck, $key, $code ),
 218+ 'translate-checks-format',
 219+ "Parameter count is $traCount; should be $defCount", // @todo Missing i18n.
 220+ );
 221+ continue;
 222+ }
 223+
 224+ for ( $i = 0; $i < count( $defArray ); $i++ ) {
 225+ $defItems = array_map( 'trim', explode( ':', $defArray[$i] ) );
 226+ $traItems = array_map( 'trim', explode( ':', $traArray[$i] ) );
 227+
 228+ $subcheck = 'timelist-format';
 229+ if ( count( $traItems ) !== 2 ) {
 230+ $warnings[$key][] = array(
 231+ array( 'miscmw', $subcheck, $key, $code ),
 232+ 'translate-checks-format',
 233+ "<nowiki>$traArray[$i]</nowiki> is malformed", // @todo Missing i18n.
 234+ );
 235+ continue;
 236+ }
 237+
 238+ $subcheck = 'timelist-format-value';
 239+ if ( $traItems[1] !== $defItems[1] ) {
 240+ $warnings[$key][] = array(
 241+ array( 'miscmw', $subcheck, $key, $code ),
 242+ 'translate-checks-format',
 243+ "<tt><nowiki>$traItems[1] !== $defItems[1]</nowiki></tt>",
 244+ );
 245+ continue;
 246+ }
 247+ }
 248+ }
 249+ }
 250+ }
 251+}
Property changes on: trunk/extensions/Translate/groups/MediaWiki/Checker.php
___________________________________________________________________
Added: svn:eol-style
1252 + native
Index: trunk/extensions/Translate/ffs/MediaWikiChecker.php
@@ -1,250 +0,0 @@
2 -<?php
3 -/**
4 - * Implements MessageChecker for %MediaWiki.
5 - *
6 - * @file
7 - * @author Niklas Laxström
8 - * @copyright Copyright © 2008-2010, Niklas Laxström
9 - * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
10 - */
11 -
12 -/**
13 - * %MediaWiki specific message checks.
14 - *
15 - * @ingroup MessageCheckers
16 - */
17 -class MediaWikiMessageChecker extends MessageChecker {
18 - /**
19 - * Checks if the translation uses all variables $[1-9] that the definition
20 - * uses and vice versa.
21 - *
22 - * @param $messages \array Iterable list of TMessage objects.
23 - * @param $code \string Language code of the translations.
24 - * @param $warnings \array Array where warnings are appended to.
25 - */
26 - protected function wikiParameterCheck( $messages, $code, &$warnings ) {
27 - return parent::parameterCheck( $messages, $code, $warnings, '/\$[1-9]/' );
28 - }
29 -
30 - /**
31 - * Checks if the translation uses links that are discouraged. Valid links are
32 - * those that link to Special: or {{ns:special}}: or project pages trough
33 - * MediaWiki messages like {{MediaWiki:helppage-url}}:. Also links in the
34 - * definition are allowed.
35 - *
36 - * @param $messages \array Iterable list of TMessage objects.
37 - * @param $code \string Language code of the translations.
38 - * @param $warnings \array Array where warnings are appended to.
39 - */
40 - protected function wikiLinksCheck( $messages, $code, &$warnings ) {
41 - $tc = Title::legalChars() . '#%{}';
42 -
43 - foreach ( $messages as $message ) {
44 - $key = $message->key();
45 - $definition = $message->definition();
46 - $translation = $message->translation();
47 -
48 - $subcheck = 'extra';
49 - $matches = $links = array();
50 - preg_match_all( "/\[\[([{$tc}]+)(\\|(.+?))?]]/sDu", $translation, $matches );
51 - for ( $i = 0; $i < count( $matches[0] ); $i++ ) {
52 - $backMatch = preg_quote( $matches[1][$i], '/' );
53 -
54 - if ( preg_match( "/\[\[$backMatch/", $definition ) ) {
55 - continue;
56 - }
57 -
58 - $links[] = "[[{$matches[1][$i]}{$matches[2][$i]}]]";
59 - }
60 -
61 - if ( count( $links ) ) {
62 - $warnings[$key][] = array(
63 - array( 'links', $subcheck, $key, $code ),
64 - 'translate-checks-links',
65 - array( 'PARAMS', $links ),
66 - array( 'COUNT', count( $links ) ),
67 - );
68 - }
69 -
70 - $subcheck = 'missing';
71 - $matches = $links = array();
72 - preg_match_all( "/\[\[([{$tc}]+)(\\|(.+?))?]]/sDu", $definition, $matches );
73 - for ( $i = 0; $i < count( $matches[0] ); $i++ ) {
74 - $backMatch = preg_quote( $matches[1][$i], '/' );
75 -
76 - if ( preg_match( "/\[\[$backMatch/", $translation ) ) {
77 - continue;
78 - }
79 -
80 - $links[] = "[[{$matches[1][$i]}{$matches[2][$i]}]]";
81 - }
82 -
83 - if ( count( $links ) ) {
84 - $warnings[$key][] = array(
85 - array( 'links', $subcheck, $key, $code ),
86 - 'translate-checks-links-missing',
87 - array( 'PARAMS', $links ),
88 - array( 'COUNT', count( $links ) ),
89 - );
90 - }
91 - }
92 - }
93 -
94 - /**
95 - * Checks if the \<br /> and \<hr /> tags are using the correct syntax.
96 - *
97 - * @param $messages \array Iterable list of TMessage objects.
98 - * @param $code \string Language code of the translations.
99 - * @param $warnings \array Array where warnings are appended to.
100 - */
101 - protected function XhtmlCheck( $messages, $code, &$warnings ) {
102 - foreach ( $messages as $message ) {
103 - $key = $message->key();
104 - $translation = $message->translation();
105 - if ( strpos( $translation, '<' ) === false ) {
106 - continue;
107 - }
108 -
109 - $subcheck = 'invalid';
110 - $tags = array(
111 - '~<hr *(\\\\)?>~suDi' => '<hr />', // Wrong syntax
112 - '~<br *(\\\\)?>~suDi' => '<br />',
113 - '~<hr/>~suDi' => '<hr />', // Wrong syntax
114 - '~<br/>~suDi' => '<br />',
115 - '~<(HR|Hr|hR) />~su' => '<hr />', // Case
116 - '~<(BR|Br|bR) />~su' => '<br />',
117 - );
118 -
119 - $wrongTags = array();
120 - foreach ( $tags as $wrong => $correct ) {
121 - $matches = array();
122 - preg_match_all( $wrong, $translation, $matches, PREG_PATTERN_ORDER );
123 - foreach ( $matches[0] as $wrongMatch ) {
124 - $wrongTags[$wrongMatch] = "$wrongMatch → $correct";
125 - }
126 - }
127 -
128 - if ( count( $wrongTags ) ) {
129 - $warnings[$key][] = array(
130 - array( 'xhtml', $subcheck, $key, $code ),
131 - 'translate-checks-xhtml',
132 - array( 'PARAMS', $wrongTags ),
133 - array( 'COUNT', count( $wrongTags ) ),
134 - );
135 - }
136 - }
137 - }
138 -
139 - /**
140 - * Checks if the translation doesn't use plural while the definition has one.
141 - *
142 - * @param $messages \array Iterable list of TMessage objects.
143 - * @param $code \string Language code of the translations.
144 - * @param $warnings \array Array where warnings are appended to.
145 - */
146 - protected function pluralCheck( $messages, $code, &$warnings ) {
147 - foreach ( $messages as $message ) {
148 - $key = $message->key();
149 - $definition = $message->definition();
150 - $translation = $message->translation();
151 -
152 - $subcheck = 'missing';
153 - if (
154 - stripos( $definition, '{{plural:' ) !== false &&
155 - stripos( $translation, '{{plural:' ) === false
156 - ) {
157 - $warnings[$key][] = array(
158 - array( 'plural', $subcheck, $key, $code ),
159 - 'translate-checks-plural',
160 - );
161 - }
162 - }
163 - }
164 -
165 - /**
166 - * Checks for page names that they have an untranslated namespace.
167 - *
168 - * @param $messages \array Iterable list of TMessage objects.
169 - * @param $code \string Language code of the translations.
170 - * @param $warnings \array Array where warnings are appended to.
171 - */
172 - protected function pagenameMessagesCheck( $messages, $code, &$warnings ) {
173 - foreach ( $messages as $message ) {
174 - $key = $message->key();
175 - $definition = $message->definition();
176 - $translation = $message->translation();
177 -
178 - $subcheck = 'namespace';
179 - $namespaces = 'help|project|\{\{ns:project}}|mediawiki';
180 - $matches = array();
181 - if ( preg_match( "/^($namespaces):[\w\s]+$/ui", $definition, $matches ) ) {
182 - if ( !preg_match( "/^{$matches[1]}:.+$/u", $translation ) ) {
183 - $warnings[$key][] = array(
184 - array( 'pagename', $subcheck, $key, $code ),
185 - 'translate-checks-pagename',
186 - );
187 - }
188 - }
189 - }
190 - }
191 -
192 - /**
193 - * Checks for some miscellaneous messages with special syntax.
194 - *
195 - * @param $messages \array Iterable list of TMessage objects.
196 - * @param $code \string Language code of the translations.
197 - * @param $warnings \array Array where warnings are appended to.
198 - */
199 - protected function miscMWChecks( $messages, $code, &$warnings ) {
200 - $timeList = array( 'protect-expiry-options', 'ipboptions' );
201 -
202 - foreach ( $messages as $message ) {
203 - $key = $message->key();
204 - $definition = $message->definition();
205 - $translation = $message->translation();
206 -
207 -
208 - if ( in_array( strtolower( $key ), $timeList, true ) ) {
209 - $defArray = explode( ',', $definition );
210 - $traArray = explode( ',', $translation );
211 -
212 - $subcheck = 'timelist-count';
213 - $defCount = count( $defArray );
214 - $traCount = count( $traArray );
215 - if ( $defCount !== $traCount ) {
216 - $warnings[$key][] = array(
217 - array( 'miscmw', $subcheck, $key, $code ),
218 - 'translate-checks-format',
219 - "Parameter count is $traCount; should be $defCount", // @todo Missing i18n.
220 - );
221 - continue;
222 - }
223 -
224 - for ( $i = 0; $i < count( $defArray ); $i++ ) {
225 - $defItems = array_map( 'trim', explode( ':', $defArray[$i] ) );
226 - $traItems = array_map( 'trim', explode( ':', $traArray[$i] ) );
227 -
228 - $subcheck = 'timelist-format';
229 - if ( count( $traItems ) !== 2 ) {
230 - $warnings[$key][] = array(
231 - array( 'miscmw', $subcheck, $key, $code ),
232 - 'translate-checks-format',
233 - "<nowiki>$traArray[$i]</nowiki> is malformed", // @todo Missing i18n.
234 - );
235 - continue;
236 - }
237 -
238 - $subcheck = 'timelist-format-value';
239 - if ( $traItems[1] !== $defItems[1] ) {
240 - $warnings[$key][] = array(
241 - array( 'miscmw', $subcheck, $key, $code ),
242 - 'translate-checks-format',
243 - "<tt><nowiki>$traItems[1] !== $defItems[1]</nowiki></tt>",
244 - );
245 - continue;
246 - }
247 - }
248 - }
249 - }
250 - }
251 -}

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r98323Started taking PHP code out of the groups/ folder anticipating Wikimedia revi...siebrand16:19, 28 September 2011

Status & tagging log