r72415 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72414‎ | r72415 | r72416 >
Date:12:52, 5 September 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Documentation updates

Also removed TestMessageCollection, replaced with another constructor
Modified paths:
  • /trunk/extensions/Translate/MessageCollection.php (modified) (history)
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/extensions/Translate/scripts/magic-export.php (modified) (history)
  • /trunk/extensions/Translate/scripts/pagetranslation-test-parser.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/MessageCollection.php
@@ -24,23 +24,23 @@
2525 /// \type{MessageDefinitions}
2626 protected $definitions = null;
2727
28 - /// \arrayof{String,String} Message key => translation.
 28+ /// \arrayof{String,String} %Message key => translation.
2929 protected $infile = array();
3030
3131 // Keys and messages.
3232
33 - /// \arrayof{String,String} Message key => database key.
 33+ /// \arrayof{String,String} %Message display key => database key.
3434 protected $keys = null;
3535
36 - /// \arrayof{String,TMessage} Message key => messages.
 36+ /// \arrayof{String,TMessage} %Message key => messages.
3737 protected $messages = null;
3838
3939 // Database resources
4040
41 - // existence, fuzzy
 41+ /// \type{Database Result Resource} Stored message existence and fuzzy state.
4242 protected $dbInfo = null;
4343
44 - // all translations
 44+ /// \type{Database Result Resource} Stored translations in database.
4545 protected $dbData = null;
4646
4747 /**
@@ -73,27 +73,60 @@
7474 return $collection;
7575 }
7676
 77+ /**
 78+ * Constructs a new empty message collection. Suitable for example for testing.
 79+ * @param $code \string Language Code.
 80+ * @return \type{MessageCollection}
 81+ */
 82+ public static function newEmpty( $code ) {
 83+
 84+ }
 85+
7786 // Data setters
7887
 88+ /**
 89+ * Set translation from file, as opposed to translation which only exists
 90+ * in the wiki because they are not exported and committed yet.
 91+ * @param $messages \arrayof{String,String} Array of translations indexed
 92+ * by display key.
 93+ */
7994 public function setInfile( array $messages ) {
8095 $this->infile = $messages;
8196 }
8297
 98+ /**
 99+ * Set message tags.
 100+ * @param $type \string Tag type, usually ignored or optional.
 101+ * @param $keys \list{String} List of display keys.
 102+ */
83103 public function setTags( $type, array $keys ) {
84104 $this->tags[$type] = $keys;
85105 }
86106
87107 /**
88 - * Data getters
 108+ * Returns list of available message keys. This is affected by filtering.
 109+ * @return \arrayof{String,String} List of database keys indexed by display keys.
89110 */
90111 public function keys() {
91112 return $this->keys;
92113 }
93114
 115+ /**
 116+ * Returns stored message tags.
 117+ * @param $type \string Tag type, usually optional or ignored.
 118+ * @return \types{\list{String},\null} List of keys or null if no tags.
 119+ * @todo Return empty array instead?
 120+ */
94121 public function getTags( $type ) {
95122 return isset( $this->tags[$type] ) ? $this->tags[$type] : null;
96123 }
97124
 125+ /**
 126+ * Lists all translators that have contributed to the latest revisions of
 127+ * each translation. Causes translations to be loaded from the database.
 128+ * Is not affected by filters.
 129+ * @return \list{String} List of usernames.
 130+ */
98131 public function getAuthors() {
99132 global $wgTranslateFuzzyBotName;
100133
@@ -102,9 +135,7 @@
103136 $authors = array_flip( $this->authors );
104137
105138 foreach ( $this->messages as $m ) {
106 - /**
107 - * Check if there are authors
108 - */
 139+ // Check if there are authors
109140 $author = $m->author();
110141
111142 if ( $author === null ) {
@@ -129,7 +160,13 @@
130161 return isset( $filteredAuthors ) ? $filteredAuthors : array();
131162 }
132163
133 - public function addCollectionAuthors( /*list*/$authors, $mode = 'append' ) {
 164+ /**
 165+ * Add external authors (usually from the file).
 166+ * @param $authors \list{String} List of authors.
 167+ * @param $mode \string Either append or set authors.
 168+ * @throws MWException If invalid $mode given.
 169+ */
 170+ public function addCollectionAuthors( $authors, $mode = 'append' ) {
134171 switch( $mode ) {
135172 case 'append':
136173 $authors = array_merge( $this->authors, $authors );
@@ -143,10 +180,12 @@
144181 $this->authors = array_unique( $authors );
145182 }
146183
 184+ // Data modifiers
 185+
147186 /**
148 - * Data modifiers
 187+ * Loads all message data. Must be called before accessing the messages
 188+ * with ArrayAccess or iteration.
149189 */
150 -
151190 public function loadTranslations() {
152191 $this->loadData( $this->keys );
153192 $this->loadInfo( $this->keys );
@@ -154,6 +193,29 @@
155194 }
156195
157196 /**
 197+ * Some statistics scripts for example loop the same collection over every
 198+ * language. This is a shortcut which keeps tags and definitions.
 199+ */
 200+ public function resetForNewLanguage( $code ) {
 201+ $this->code = $code;
 202+ $this->keys = $this->fixKeys( array_keys( $this->definitions->messages ) );
 203+ $this->dbInfo = null;
 204+ $this->dbData = null;
 205+ $this->messages = null;
 206+ $this->infile = array();
 207+ $this->authors = array();
 208+
 209+ unset( $this->tags['fuzzy'] );
 210+ }
 211+
 212+ /**
 213+ * For paging messages. One can count messages before and after slice.
 214+ */
 215+ public function slice( $offset, $limit ) {
 216+ $this->keys = array_slice( $this->keys, $offset, $limit, true );
 217+ }
 218+
 219+ /**
158220 * Filters messages based on some condition. Some filters cause data to be
159221 * loaded from the database. PAGEINFO: existence and fuzzy tags.
160222 * TRANSLATIONS: translations for every message. It is recommended to first
@@ -161,14 +223,19 @@
162224 * translations from file with addInfile, and it is needed for changed
163225 * filter to work.
164226 *
165 - * @param $type
166 - * fuzzy: messages with fuzzy tag (PAGEINFO)
167 - * optional: messages marked for optional.
168 - * ignored: messages which are not for translation.
169 - * hastranslation: messages which have translation (be if fuzzy or not) (PAGEINFO, *INFILE).
170 - * translated: messages which have translation which is not fuzzy (PAGEINFO, *INFILE).
171 - * changed: translation in database differs from infile. (INFILE, TRANSLATIONS)
172 - * @param $condition True or false.
 227+ * @param $type \string
 228+ * - fuzzy: messages with fuzzy tag (PAGEINFO)
 229+ * - optional: messages marked for optional.
 230+ * - ignored: messages which are not for translation.
 231+ * - hastranslation: messages which have translation (be if fuzzy or not)
 232+ * (PAGEINFO, *INFILE).
 233+ * - translated: messages which have translation which is not fuzzy
 234+ * (PAGEINFO, *INFILE).
 235+ * - changed: translation in database differs from infile.
 236+ * (INFILE, TRANSLATIONS)
 237+ * @param $condition \bool Whether to return messages which do not satisfy
 238+ * the given filter condition (true), or only which do (false).
 239+ * @throws \type{MWException} If given invalid filter name.
173240 */
174241 public function filter( $type, $condition = true ) {
175242 switch( $type ) {
@@ -186,31 +253,11 @@
187254 }
188255
189256 /**
190 - * Some statistics scripts for example loop the same collection over every
191 - * language. This is a shortcut which keeps tags and definitions.
 257+ * Really apply a filter. Some filters need multiple conditions.
 258+ * @param $filter \string Filter name.
 259+ * @param $condition \bool Whether to return messages which do not satisfy
 260+ * the given filter condition (true), or only which do (false).
192261 */
193 - public function resetForNewLanguage( $code ) {
194 - $this->code = $code;
195 - $this->keys = $this->fixKeys( array_keys( $this->definitions->messages ) );
196 - $this->dbInfo = null;
197 - $this->dbData = null;
198 - $this->messages = null;
199 - $this->infile = array();
200 - $this->authors = array();
201 -
202 - unset( $this->tags['fuzzy'] );
203 - }
204 -
205 - /**
206 - * For paging messages. One can count messages before and after slice.
207 - */
208 - public function slice( $offset, $limit ) {
209 - $this->keys = array_slice( $this->keys, $offset, $limit, true );
210 - }
211 -
212 - /**
213 - * Protected functions
214 - */
215262 protected function applyFilter( $filter, $condition ) {
216263 $keys = $this->keys;
217264 if ( $filter === 'fuzzy' ) {
@@ -220,17 +267,13 @@
221268 } elseif ( $filter === 'translated' ) {
222269 $fuzzy = $this->filterFuzzy( $keys, false );
223270 $hastranslation = $this->filterHastranslation( $keys, false );
224 - /**
225 - * Fuzzy messages are not counted as translated messages
226 - */
 271+ // Fuzzy messages are not counted as translated messages
227272 $translated = $this->filterOnCondition( $hastranslation, $fuzzy );
228273 $keys = $this->filterOnCondition( $keys, $translated, $condition );
229274 } elseif ( $filter === 'changed' ) {
230275 $keys = $this->filterChanged( $keys, $condition );
231276 } else {
232 - /**
233 - * Filter based on tags.
234 - */
 277+ // Filter based on tags.
235278 if ( !isset( $this->tags[$filter] ) ) {
236279 if ( $filter !== 'optional' && $filter !== 'ignored' ) {
237280 throw new MWException( "No tagged messages for custom filter $filter" );
@@ -245,6 +288,21 @@
246289 $this->keys = $keys;
247290 }
248291
 292+ /**
 293+ * Filters list of keys with other list of keys according to the condition.
 294+ * In other words, you have a list of keys, and you have determined list of
 295+ * keys that have some feature. Now you can either take messages that are
 296+ * both in the first list and the second list OR are in the first list but
 297+ * are not in the second list (conditition = true and false respectively).
 298+ * What makes this more complex is that second list of keys might not be a
 299+ * subset of the first list of keys.
 300+ * @param $keys \list{String} List of keys to filter.
 301+ * @param $condKeys \list{String} Second list of keys for filtering.
 302+ * @param $condition \bool True (default) to return keys which are on first
 303+ * and second list, false to return keys which are on the first but not on
 304+ * second.
 305+ * @return \list{String} Filtered keys.
 306+ */
249307 protected function filterOnCondition( array $keys, array $condKeys, $condition = true ) {
250308 if ( $condition === true ) {
251309 /**
@@ -267,6 +325,13 @@
268326 return $keys;
269327 }
270328
 329+ /**
 330+ * Filters list of keys according to whether the translation is fuzzy.
 331+ * @param $keys \list{String} List of keys to filter.
 332+ * @param $condition \bool True to filter away fuzzy translations, false
 333+ * to filter non-fuzzy translations.
 334+ * @return \list{String} Filtered keys.
 335+ */
271336 protected function filterFuzzy( array $keys, $condition ) {
272337 $this->loadInfo( $keys );
273338
@@ -293,6 +358,13 @@
294359 return $keys;
295360 }
296361
 362+ /**
 363+ * Filters list of keys according to whether they have a translation.
 364+ * @param $keys \list{String} List of keys to filter.
 365+ * @param $condition \bool True to filter away translated, false
 366+ * to filter untranslated.
 367+ * @return \list{String} Filtered keys.
 368+ */
297369 protected function filterHastranslation( array $keys, $condition ) {
298370 $this->loadInfo( $keys );
299371
@@ -303,9 +375,7 @@
304376 $flipKeys = array_flip( $keys );
305377
306378 foreach ( $this->dbInfo as $row ) {
307 - /**
308 - * Remove messages which have a translation from keys
309 - */
 379+ // Remove messages which have a translation from keys
310380 if ( !isset( $flipKeys[$row->page_title] ) ) {
311381 continue;
312382 }
@@ -313,16 +383,12 @@
314384 unset( $keys[$flipKeys[$row->page_title]] );
315385 }
316386
317 - /**
318 - * Check also if there is something in the file that is not yet in the db
319 - */
 387+ // Check also if there is something in the file that is not yet in the database
320388 foreach ( array_keys( $this->infile ) as $inf ) {
321389 unset( $keys[$inf] );
322390 }
323391
324 - /**
325 - * Remove the messages which do not have a translation from the list
326 - */
 392+ // Remove the messages which do not have a translation from the list
327393 if ( $condition === false ) {
328394 $keys = array_diff( $origKeys, $keys );
329395 }
@@ -330,6 +396,14 @@
331397 return $keys;
332398 }
333399
 400+ /**
 401+ * Filters list of keys according to whether the current translation
 402+ * differs from the commited translation.
 403+ * @param $keys \list{String} List of keys to filter.
 404+ * @param $condition \bool True to filter changed translations, false
 405+ * to filter unchanged translations.
 406+ * @return \list{String} Filtered keys.
 407+ */
334408 protected function filterChanged( array $keys, $condition ) {
335409 $this->loadData( $keys );
336410
@@ -347,23 +421,25 @@
348422
349423 $text = Revision::getRevisionText( $row );
350424 if ( $this->infile[$realKey] === $text ) {
351 - /**
352 - * Remove changed messages from the list
353 - */
 425+ // Remove unchanged messages from the list
354426 unset( $keys[$realKey] );
355427 }
356428 }
357429
358 - /**
359 - * Remove the messages which have not changed from the list
360 - */
 430+ // Remove the messages which have not changed from the list
361431 if ( $condition === false ) {
362432 $keys = $this->filterOnCondition( $keys, $origKeys, false );
363433 }
364434
365435 return $keys;
366436 }
 437+ /** @} */
367438
 439+ /**
 440+ * Takes list of keys and converts them into database format.
 441+ * @param $keys \list{String} List of keys in display format.
 442+ * @return \arrayof{String,String} Array of keys in database format indexed by display format.
 443+ */
368444 protected function fixKeys( array $keys ) {
369445 $newkeys = array();
370446 $namespace = $this->definitions->namespace;
@@ -380,14 +456,15 @@
381457 return $newkeys;
382458 }
383459
 460+ /**
 461+ * Loads existence and fuzzy state for given list of keys.
 462+ * @param $keys \list{String} List of keys in database format.
 463+ */
384464 protected function loadInfo( array $keys ) {
385465 if ( $this->dbInfo !== null ) {
386466 return;
387467 }
388468
389 - /**
390 - * Something iterable
391 - */
392469 $this->dbInfo = array();
393470
394471 if ( !count( $keys ) ) {
@@ -417,14 +494,15 @@
418495 $this->dbInfo = $dbr->select( $tables, $fields, $conds, __METHOD__, array(), $joins );
419496 }
420497
 498+ /**
 499+ * Loads translation for given list of keys.
 500+ * @param $keys \list{String} List of keys in database format.
 501+ */
421502 protected function loadData( $keys ) {
422503 if ( $this->dbData !== null ) {
423504 return;
424505 }
425506
426 - /**
427 - * Something iterable
428 - */
429507 $this->dbData = array();
430508
431509 if ( !count( $keys ) ) {
@@ -447,6 +525,10 @@
448526 $this->dbData = $res;
449527 }
450528
 529+ /**
 530+ * Constructs all TMessages from the data accumulated so far.
 531+ * Usually there is no need to call this method directly.
 532+ */
451533 public function initMessages() {
452534 if ( $this->messages !== null ) {
453535 return;
@@ -460,9 +542,7 @@
461543
462544 $flipKeys = array_flip( $this->keys );
463545
464 - /**
465 - * Copy rows if any.
466 - */
 546+ // Copy rows if any.
467547 if ( $this->dbData !== null ) {
468548 foreach ( $this->dbData as $row ) {
469549 if ( !isset( $flipKeys[$row->page_title] ) ) {
@@ -489,9 +569,7 @@
490570 $this->setTags( 'fuzzy', $fuzzy );
491571 }
492572
493 - /**
494 - * Copy tags if any.
495 - */
 573+ // Copy tags if any.
496574 foreach ( $this->tags as $type => $keys ) {
497575 foreach ( $keys as $key ) {
498576 if ( isset( $messages[$key] ) ) {
@@ -500,9 +578,7 @@
501579 }
502580 }
503581
504 - /**
505 - * Copy infile if any.
506 - */
 582+ // Copy infile if any.
507583 foreach ( $this->infile as $key => $value ) {
508584 if ( isset( $messages[$key] ) ) {
509585 $messages[$key]->setInfile( $value );
@@ -513,12 +589,8 @@
514590 }
515591
516592 /**
517 - * Interfaces etc.
 593+ * ArrayAccess methods. @{
518594 */
519 -
520 - /**
521 - * ArrayAccess methods
522 - */
523595 public function offsetExists( $offset ) {
524596 return isset( $this->keys[$offset] );
525597 }
@@ -534,23 +606,22 @@
535607 public function offsetUnset( $offset ) {
536608 unset( $this->keys[$offset] );
537609 }
 610+ /** @} */
538611
539612 /**
540 - * Fail fast
 613+ * Fail fast if trying to access unknown properties. @{
541614 */
542615 public function __get( $name ) {
543616 throw new MWException( __METHOD__ . ": Trying to access unknown property $name" );
544617 }
545618
546 - /**
547 - * Fail fast
548 - */
549619 public function __set( $name, $value ) {
550620 throw new MWException( __METHOD__ . ": Trying to modify unknown property $name" );
551621 }
 622+ /** @} */
552623
553624 /**
554 - * Iterator methods
 625+ * Iterator method. @{
555626 */
556627 public function rewind() {
557628 reset( $this->keys );
@@ -579,10 +650,14 @@
580651 public function count() {
581652 return count( $this->keys() );
582653 }
 654+ /** @} */
 655+
583656 }
584657
585658 /**
586 - * @todo Documentation needed.
 659+ * Wrapper for message definitions, just to beauty the code.
 660+ * This is one reason why message collections and thus message groups are
 661+ * restricted into single namespace.
587662 */
588663 class MessageDefinitions {
589664 public $namespace;
@@ -592,14 +667,3 @@
593668 $this->messages = $messages;
594669 }
595670 }
596 -
597 -/**
598 - * @todo Documentation needed.
599 - */
600 -class TestMessageCollection extends MessageCollection {
601 - public function __construct( $code ) {
602 - $this->code = $code;
603 - $this->definitions = array();
604 - $this->keys = array();
605 - }
606 -}
Index: trunk/extensions/Translate/scripts/magic-export.php
@@ -8,6 +8,8 @@
99 * @file
1010 */
1111
 12+/// @cond
 13+
1214 $optionsWithArgs = array( 'target', 'type' );
1315 require( dirname( __FILE__ ) . '/cli.inc' );
1416
@@ -104,3 +106,5 @@
105107 wfMkdirParents( dirname( $options['target'] . "/$filename" ) );
106108 file_put_contents( $options['target'] . "/$filename", trim( $output ) . "\n" );
107109 }
 110+
 111+/// @endcond
\ No newline at end of file
Index: trunk/extensions/Translate/scripts/pagetranslation-test-parser.php
@@ -44,7 +44,7 @@
4545 try {
4646 $parse = $translatablePage->getParse();
4747 if ( $failureExpected ) {
48 - $target = $parse->getTranslationPageText( new TestMessageCollection( "foo" ) );
 48+ $target = $parse->getTranslationPageText( MessageCollection::newEmpty( "foo" ) );
4949 $this->output( "Testfile $filename should have failed... see $pattern.pttarget.fail\n" );
5050 file_put_contents( "$pattern.pttarget.fail", $target );
5151 }
@@ -65,7 +65,7 @@
6666 }
6767
6868 if ( file_exists( "$pattern.pttarget" ) ) {
69 - $target = $parse->getTranslationPageText( new TestMessageCollection( "foo" ) );
 69+ $target = $parse->getTranslationPageText( MessageCollection::newEmpty( "foo" ) );
7070 if ( $target !== file_get_contents( "$pattern.pttarget" ) ) {
7171 $this->output( "Testfile $filename failed with target page output... writing $pattern.pttarget.fail\n" );
7272 file_put_contents( "$pattern.pttarget.fail", $target );
Index: trunk/extensions/Translate/_autoload.php
@@ -35,7 +35,6 @@
3636
3737 $wgAutoloadClasses['MessageCollection'] = $dir . 'MessageCollection.php';
3838 $wgAutoloadClasses['MessageDefinitions'] = $dir . 'MessageCollection.php';
39 -$wgAutoloadClasses['TestMessageCollection'] = $dir . 'MessageCollection.php';
4039 $wgAutoloadClasses['TMessage'] = $dir . 'Message.php';
4140 $wgAutoloadClasses['ThinMessage'] = $dir . 'Message.php';
4241 $wgAutoloadClasses['FatMessage'] = $dir . 'Message.php';

Status & tagging log