r99596 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99595‎ | r99596 | r99597 >
Date:23:30, 11 October 2011
Author:nikerabbit
Status:ok
Tags:
Comment:
Moved this checker back to the extension, needed for page translation.
Modified paths:
  • /trunk/extensions/Translate/MediaWikiMessageChecker.php (added) (history)
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/translatewiki/MediaWiki/Checker.php (deleted) (history)
  • /trunk/translatewiki/MediaWiki/setup.php (deleted) (history)

Diff [purge]

Index: trunk/translatewiki/MediaWiki/setup.php
@@ -1,15 +0,0 @@
2 -<?php
3 -/**
4 - * Support %MediaWiki: http://www.mediawiki.org/.
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 - * @todo Remove once %MediaWiki core group uses yaml configuration.
11 - */
12 -
13 -/// @cond
14 -$dir = dirname( __FILE__ );
15 -$wgAutoloadClasses['MediaWikiMessageChecker'] = dirname( __FILE__ ) . '/Checker.php';
16 -/// @endcond
\ No newline at end of file
Index: trunk/translatewiki/MediaWiki/Checker.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 -}
Index: trunk/extensions/Translate/MediaWikiMessageChecker.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/MediaWikiMessageChecker.php
___________________________________________________________________
Added: svn:eol-style
1252 + native
Index: trunk/extensions/Translate/_autoload.php
@@ -24,6 +24,7 @@
2525 $wgAutoloadClasses['TranslateHooks'] = $dir . 'TranslateHooks.php';
2626
2727 $wgAutoloadClasses['MessageChecker'] = $dir . 'MessageChecks.php';
 28+$wgAutoloadClasses['MediaWikiMessageChecker'] = $dir . 'MediaWikiMessageChecker.php';
2829
2930 $wgAutoloadClasses['MessageGroup'] = $dir . 'Groups.php';
3031 $wgAutoloadClasses['MessageGroupBase'] = $dir . 'Groups.php';

Follow-up revisions

RevisionCommit summaryAuthorDate
r995981.18wmf1: MFT r99548, r99564, r99596reedy23:41, 11 October 2011

Status & tagging log