r37637 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r37636‎ | r37637 | r37638 >
Date:10:13, 14 July 2008
Author:nikerabbit
Status:old
Tags:
Comment:
* Fixed a huge memleak. Thanks to milian who helped to debug it.
Modified paths:
  • /trunk/extensions/Translate/Message.php (modified) (history)
  • /trunk/extensions/Translate/MessageChecks.php (modified) (history)
  • /trunk/extensions/Translate/TranslateEditAddons.php (modified) (history)
  • /trunk/extensions/Translate/TranslateUtils.php (modified) (history)
  • /trunk/extensions/Translate/scripts/cli.inc (modified) (history)
  • /trunk/extensions/Translate/scripts/createCheckIndex.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/TranslateEditAddons.php
@@ -259,7 +259,8 @@
260260 $message = new TMessage( $key, $en );
261261 // Take the contents from edit field as a translation
262262 $message->database = $translation;
263 - $checks = MessageChecks::doChecks( $message, $group->getType(), $code );
 263+ $checker = MessageChecks::getInstance();
 264+ $checks = $checker->doChecks( $message, $group->getType(), $code );
264265 if ( count($checks) ) {
265266 $checkMessages = array();
266267 foreach ( $checks as $checkParams ) {
Index: trunk/extensions/Translate/scripts/cli.inc
@@ -14,26 +14,26 @@
1515 @include("$dir/../../CorePath.php"); // Allow override
1616 require_once( "$IP/maintenance/commandLine.inc" );
1717
18 -function STDOUT( $str, $singeLineItem = null ) {
 18+function STDOUT( $str, $channel = null ) {
1919 global $options;
2020 if ( isset($options['quiet']) ) return;
2121
22 - static $atStart = true;
23 - static $lastSingle = false;
 22+ static $lastChannel = null;
 23+ static $lineStart = true;
2424
25 - if ( !$atStart && $singeLineItem === null || !$lastSingle && !$atStart && $singeLineItem ) {
26 - fwrite( STDOUT, "\n" );
 25+ if ( $channel !== null && ($lineStart || $channel === $lastChannel) ) {
 26+ fwrite( STDOUT, $str );
 27+ } else {
 28+ if ( !$lineStart ) fwrite( STDOUT, "\n" );
 29+ fwrite( STDOUT, $str );
2730 }
28 - fwrite( STDOUT, $str );
2931
30 - if ( $singeLineItem !== true ) {
 32+ $lineStart = false;
 33+ if ( $channel === null ) {
3134 fwrite( STDOUT, "\n" );
32 - $atStart = true;
33 - $lastSingle = false;
34 - } else {
35 - $lastSingle = true;
36 - $atStart = false;
 35+ $lineStart = true;
3736 }
 37+ $lastChannel = $channel;
3838 }
3939
4040 function STDERR( $message ) {
Index: trunk/extensions/Translate/scripts/createCheckIndex.php
@@ -18,6 +18,9 @@
1919 if ( $wgTranslateDocumentationLanguageCode )
2020 unset( $codes[$wgTranslateDocumentationLanguageCode] );
2121
 22+// Skip source
 23+unset($codes['en']);
 24+
2225 $codes = array_keys( $codes );
2326 sort( $codes );
2427
@@ -27,21 +30,31 @@
2831 $reqGroups = false;
2932 }
3033
 34+$verbose = isset($options['verbose']);
 35+
3136 $groups = MessageGroups::singleton()->getGroups();
 37+$checker = MessageChecks::getInstance();
3238
3339 foreach ( $groups as $g ) {
3440 $id = $g->getId();
3541
3642 // Skip groups that are not requested
37 - if ( $reqGroups && !in_array($id, $reqGroups) ) continue;
 43+ if ( $reqGroups && !in_array($id, $reqGroups) ) {
 44+ unset($g);
 45+ continue;
 46+ }
3847
3948 $problematic = array();
4049 $type = $g->getType();
 50+ if ( !$checker->hasChecks($type) ) {
 51+ unset($g);
 52+ continue;
 53+ }
4154
42 - STDOUT( "Working with $id: ", true );
 55+ STDOUT( "Working with $id: ", $id );
4356
4457 foreach ( $codes as $code ) {
45 - STDOUT( "$code ", true );
 58+ STDOUT( "$code ", $id );
4659
4760 // Initialise messages, using unique definitions if appropriate
4861 $collection = $g->initCollection( $code, $g->isMeta() );
@@ -49,22 +62,27 @@
5063 $namespace = $g->namespaces[0];
5164
5265 foreach ( $collection->keys() as $key ) {
53 - $prob = MessageChecks::doFastChecks( $collection[$key], $type, $code );
 66+ $prob = $checker->doFastChecks( $collection[$key], $type, $code );
5467 if ( $prob ) {
55 - // Print it
56 - $nsText = $wgContLang->getNsText( $namespace );
57 - STDOUT( "# [[$nsText:$key/$code]]" );
5868
 69+ if ( $verbose ) {
 70+ // Print it
 71+ $nsText = $wgContLang->getNsText( $namespace );
 72+ STDOUT( "# [[$nsText:$key/$code]]" );
 73+ }
 74+
5975 // Add it to the array
60 - $key = strtolower( "$namespace:$key" );
61 - $problematic[$code][] = $key;
 76+ $problematic[$code][] = strtolower( "$namespace:$key" );
6277 }
6378 }
64 -
6579 }
6680
 81+ break;
 82+
6783 // Store the results
6884 $file = TRANSLATE_CHECKFILE . "-$id";
6985 wfMkdirParents( dirname($file) );
7086 file_put_contents( $file, serialize( $problematic ) );
7187 }
 88+
 89+unset( $checker );
\ No newline at end of file
Index: trunk/extensions/Translate/MessageChecks.php
@@ -10,26 +10,26 @@
1111 */
1212 class MessageChecks {
1313
 14+ // Fastest first
 15+ var $checksForType = array(
 16+ 'mediawiki' => array(
 17+ 'checkPlural',
 18+ 'checkParameters',
 19+ 'checkUnknownParameters',
 20+ 'checkBalance',
 21+ 'checkLinks',
 22+ 'checkXHTML',
 23+ ),
 24+ 'freecol' => array(
 25+ 'checkFreeColMissingVars',
 26+ 'checkFreeColExtraVars',
 27+ ),
 28+ );
 29+
1430 private function __construct() {
1531 $file = dirname(__FILE__) . '/check-blacklist.php';
1632 $this->blacklist =
1733 ResourceLoader::loadVariableFromPHPFile( $file, 'checkBlacklist' );
18 -
19 - // Fastest first
20 - $this->checksForType = array(
21 - 'mediawiki' => array(
22 - array( $this, 'checkPlural' ),
23 - array( $this, 'checkParameters' ),
24 - array( $this, 'checkUnknownParameters' ),
25 - array( $this, 'checkBalance' ),
26 - array( $this, 'checkLinks' ),
27 - array( $this, 'checkXHTML' ),
28 - ),
29 - 'freecol' => array(
30 - array( $this, 'checkFreeColMissingVars' ),
31 - array( $this, 'checkFreeColExtraVars' ),
32 - ),
33 - );
3434 }
3535
3636 public static function getInstance() {
@@ -40,9 +40,8 @@
4141 return $obj;
4242 }
4343
44 - public function getChecks( $type ) {
45 - if ( !isset($this->checksForType[$type]) ) return array();
46 - return $this->checksForType[$type];
 44+ public function hasChecks( $type ) {
 45+ return isset($this->checksForType[$type]);
4746 }
4847
4948 /**
@@ -51,14 +50,13 @@
5251 * @param $message Instance of TMessage.
5352 * @return Array of warning messages, html-format.
5453 */
55 - public static function doChecks( TMessage $message, $type, $code ) {
 54+ public function doChecks( TMessage $message, $type, $code ) {
5655 if ( $message->translation === null) return false;
57 - $obj = new MessageChecks;
5856 $warnings = array();
5957
60 - foreach ( $obj->getChecks( $type ) as $check ) {
 58+ foreach ( $this->checksForType[$type] as $check ) {
6159 $warning = '';
62 - if ( call_user_func( $check, $message, $code, &$warning ) ) {
 60+ if ( call_user_func( array($this, $check), $message, $code, &$warning ) ) {
6361 $warnings[] = $warning;
6462 }
6563 }
@@ -66,12 +64,11 @@
6765 return $warnings;
6866 }
6967
70 - public static function doFastChecks( TMessage $message, $type, $code ) {
 68+ public function doFastChecks( TMessage $message, $type, $code ) {
7169 if ( $message->translation === null) return false;
7270
73 - $obj = new MessageChecks;
74 - foreach ( $obj->getChecks( $type ) as $check ) {
75 - if ( call_user_func( $check, $message, $code ) ) return true;
 71+ foreach ( $this->checksForType[$type] as $check ) {
 72+ if ( call_user_func( array($this, $check), $message, $code ) ) return true;
7673 }
7774
7875 return false;
@@ -182,19 +179,21 @@
183180 * @return Array of problematic links.
184181 */
185182 protected function checkLinks( TMessage $message, $code, &$desc = null ) {
186 - $translation = $message->translation;
187 - if ( strpos( $translation, '[[' ) === false ) return false;
 183+ if ( strpos( $message->translation, '[[' ) === false ) return false;
188184
189185 $matches = array();
190186 $links = array();
191187 $tc = Title::legalChars() . '#%{}';
192 - preg_match_all( "/\[\[([{$tc}]+)(?:\\|(.+?))?]]/sDu", $translation, $matches);
 188+ preg_match_all( "/\[\[([{$tc}]+)(?:\\|(.+?))?]]/sDu", $message->translation, $matches);
193189 for ($i = 0; $i < count($matches[0]); $i++ ) {
194 - if ( preg_match( '/({{ns:)?special(}})?:.*/sDui', $matches[1][$i] ) ) continue;
195 - if ( preg_match( '/{{mediawiki:.*}}/sDui', $matches[1][$i] ) ) continue;
196 - if ( preg_match( '/user([ _]talk)?:.*/sDui', $matches[1][$i] ) ) continue;
197 - if ( preg_match( '/:?\$[1-9]/sDu', $matches[1][$i] ) ) continue;
 190+ //if ( preg_match( '/({{ns:)?special(}})?:.*/sDui', $matches[1][$i] ) ) continue;
 191+ //if ( preg_match( '/{{mediawiki:.*}}/sDui', $matches[1][$i] ) ) continue;
 192+ //if ( preg_match( '/user([ _]talk)?:.*/sDui', $matches[1][$i] ) ) continue;
 193+ //if ( preg_match( '/:?\$[1-9]/sDu', $matches[1][$i] ) ) continue;
198194
 195+ $backMatch = preg_quote( $matches[1][$i], '/' );
 196+ if ( preg_match( "/$backMatch/", $message->definition ) ) continue;
 197+
199198 $links[] = "[[{$matches[1][$i]}|{$matches[2][$i]}]]";
200199 }
201200
Index: trunk/extensions/Translate/TranslateUtils.php
@@ -20,7 +20,14 @@
2121 */
2222 public static function title( $message, $code ) {
2323 global $wgContLang;
24 - return $wgContLang->ucfirst( $message . '/' . strtolower( $code ) );
 24+
 25+ // Cache some amount of titles for speed
 26+ static $cache = array();
 27+ if ( count($cache)>500 ) $cache = array();
 28+ if ( !isset($cache[$message]) ) {
 29+ $cache[$message] = $wgContLang->ucfirst($message);
 30+ }
 31+ return $cache[$message] . '/' . $code;
2532 }
2633
2734 /**
Index: trunk/extensions/Translate/Message.php
@@ -204,12 +204,12 @@
205205 /**
206206 * String that uniquely identifies this message.
207207 */
208 - private $key = null;
 208+ public $key = null;
209209
210210 /**
211211 * The definition of this message - usually in English.
212212 */
213 - private $definition = null;
 213+ public $definition = null;
214214
215215 // Following properties are lazy declared to save memory
216216
@@ -235,8 +235,6 @@
236236
237237 // Values that can be accessed with $message->value syntax
238238 protected static $callable = array(
239 - // Obligatory basic values
240 - 'key', 'definition',
241239 // Basic values
242240 'infile', 'database', 'optional', 'pageExists', 'talkExists',
243241 // Derived values
@@ -279,14 +277,14 @@
280278
281279 /** Determines if this message has uncommitted changes. */
282280 public function changed() {
283 - return $this->pageExists() && ( $this->infile() !== $this->database() );
 281+ return !!@$this->pageExists && ( @$this->infile !== @$this->database );
284282 }
285283
286284 /** Determies if this message has a proper translation. */
287285 public function translated() {
288 - if ( $this->fuzzy() ) return false;
289 - $optionalSame = $this->optional() && ($this->translation() === $this->definition);
290 - return ($this->translation() !== null) && !$optionalSame;
 286+ if ( @$this->translation === null || $this->fuzzy() ) return false;
 287+ $optionalSame = !!@$this->optional && (@$this->translation === @$this->definition);
 288+ return !$optionalSame;
291289 }
292290
293291 /**
@@ -296,7 +294,7 @@
297295 * @return Translated string or null if there isn't translation.
298296 */
299297 public function translation() {
300 - return ($this->database() !== null) ? $this->database() : $this->infile();
 298+ return (@$this->database !== null) ? @$this->database : @$this->infile;
301299 }
302300
303301 /**
@@ -306,8 +304,8 @@
307305 * @return true or false
308306 */
309307 public function fuzzy() {
310 - if ( $this->translation() !== null ) {
311 - return strpos($this->translation(), TRANSLATE_FUZZY) !== false;
 308+ if ( @$this->translation !== null ) {
 309+ return strpos($this->translation, TRANSLATE_FUZZY) !== false;
312310 } else {
313311 return false;
314312 }
@@ -342,11 +340,7 @@
343341 }
344342
345343 public function __isset( $name ) {
346 - if ( property_exists( $this, $name ) ) {
347 - return $this->$name !== null;
348 - } else {
349 - return false;
350 - }
 344+ return @$this->$name !== null;
351345 }
352346
353347 }

Status & tagging log