r70206 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70205‎ | r70206 | r70207 >
Date:20:14, 30 July 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Documentation and style improvements
Modified paths:
  • /trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php
@@ -1,45 +1,71 @@
22 <?php
33 class LocalisationUpdate {
 4+
45 private static $newHashes = null;
56 private static $filecache = array();
67
7 - public static function onRecache( $lc, $langcode, &$cache ) {
8 - $cache['messages'] = array_merge( $cache['messages'],
9 - self::readFile( $langcode ) );
 8+ /**
 9+ * LocalisationCacheRecache hook handler.
 10+ *
 11+ * @param $lc LocalisationCache
 12+ * @param $langcode String
 13+ * @param $cache Array
 14+ *
 15+ * @return true
 16+ */
 17+ public static function onRecache( LocalisationCache $lc, $langcode, array &$cache ) {
 18+ $cache['messages'] = array_merge(
 19+ $cache['messages'],
 20+ self::readFile( $langcode )
 21+ );
 22+
1023 $cache['deps'][] = new FileDependency(
11 - self::filename( $langcode ) );
 24+ self::filename( $langcode )
 25+ );
 26+
1227 return true;
1328 }
1429
15 - // Called from the cronjob to fetch new messages from SVN
16 - public static function updateMessages( $options ) {
 30+ /**
 31+ * Called from the cronjob to fetch new messages from SVN.
 32+ *
 33+ * @param $options Array
 34+ *
 35+ * @return true
 36+ */
 37+ public static function updateMessages( array $options ) {
1738 global $wgLocalisationUpdateDirectory;
1839
1940 $verbose = !isset( $options['quiet'] );
2041 $all = isset( $options['all'] );
2142 $skipCore = isset( $options['skip-core'] );
2243 $skipExtensions = isset( $options['skip-extensions'] );
 44+
2345 if( isset( $options['outdir'] ) ) {
2446 $wgLocalisationUpdateDirectory = $options['outdir'];
2547 }
2648
27 - // Update all MW core messages
 49+ $result = 0;
 50+
 51+ // Update all MW core messages.
2852 if( !$skipCore ) {
2953 $result = self::updateMediawikiMessages( $verbose );
3054 }
3155
32 - // Update all Extension messages
 56+ // Update all Extension messages.
3357 if( !$skipExtensions ) {
3458 if( $all ) {
3559 global $IP;
3660 $extFiles = array();
3761
3862 // Look in extensions/ for all available items...
 63+ // TODO: add support for $wgExtensionAssetsPath
3964 $dirs = new RecursiveDirectoryIterator( "$IP/extensions/" );
4065
4166 // I ain't kidding... RecursiveIteratorIterator.
4267 foreach( new RecursiveIteratorIterator( $dirs ) as $pathname => $item ) {
4368 $filename = basename( $pathname );
 69+
4470 if( preg_match( '/^(.*)\.i18n\.php$/', $filename, $matches ) ) {
4571 $group = $matches[1];
4672 $extFiles[$group] = $pathname;
@@ -59,129 +85,167 @@
6086 // And output the result!
6187 self::myLog( "Updated {$result} messages in total" );
6288 self::myLog( "Done" );
 89+
6390 return true;
6491 }
6592
66 - // Update Extension Messages
 93+ /**
 94+ * Update Extension Messages.
 95+ *
 96+ * @param $file String
 97+ * @param $extension String
 98+ * @param $verbose Boolean
 99+ *
 100+ * @return Integer: the amount of updated messages
 101+ */
67102 public static function updateExtensionMessages( $file, $extension, $verbose ) {
68103 global $IP, $wgLocalisationUpdateSVNURL;
69104
70105 $relfile = wfRelativePath( $file, "$IP/extensions" );
71 - // Create a full path
 106+
 107+ // Create a full path.
 108+ // TODO: add support for $wgExtensionAssetsPath
72109 $localfile = "$IP/extensions/$relfile";
73110
74 - // Get the full SVN directory path
 111+ // Get the full SVN directory path.
 112+ // TODO: add support for $wgExtensionAssetsPath
75113 $svnfile = "$wgLocalisationUpdateSVNURL/extensions/$relfile";
76114
77 - // Compare the 2 files
 115+ // Compare the 2 files.
78116 $result = self::compareExtensionFiles( $extension, $svnfile, $file, $verbose, false, true );
 117+
79118 return $result;
80119 }
81120
82 - // Update the Mediawiki Core Messages
 121+ /**
 122+ * Update the Mediawiki Core Messages.
 123+ *
 124+ * @param $verbose Boolean
 125+ *
 126+ * @return Integer: the amount of updated messages
 127+ */
83128 public static function updateMediawikiMessages( $verbose ) {
84129 global $IP, $wgLocalisationUpdateSVNURL;
85130
86 - // Create an array which will later contain all the files that we want to try to update
 131+ // Create an array which will later contain all the files that we want to try to update.
87132 $files = array();
88133
89 - // The directory which contains the files
 134+ // The directory which contains the files.
90135 $dirname = "languages/messages";
91136
92 - // Get the full path to the directory
 137+ // Get the full path to the directory.
93138 $localdir = $IP . "/" . $dirname;
94139
95 - // Get the full SVN Path
 140+ // Get the full SVN Path.
96141 $svndir = "$wgLocalisationUpdateSVNURL/phase3/$dirname";
97142
98 - // Open the directory
 143+ // Open the directory.
99144 $dir = opendir( $localdir );
100145 while ( false !== ( $file = readdir( $dir ) ) ) {
101146 $m = array();
102147
103148 // And save all the filenames of files containing messages
104149 if ( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
105 - if ( $m[1] != 'En' ) { // Except for the English one
 150+ if ( $m[1] != 'En' ) { // Except for the English one.
106151 $files[] = $file;
107152 }
108153 }
109154 }
110155 closedir( $dir );
111156
112 - // Find the changed English strings (as these messages won't be updated in ANY language)
113 - $changedEnglishStrings = self::compareFiles( $localdir . "/MessagesEn.php", $svndir . "/MessagesEn.php", $verbose, true );
 157+ // Find the changed English strings (as these messages won't be updated in ANY language).
 158+ $changedEnglishStrings = self::compareFiles( $localdir . '/MessagesEn.php', $svndir . '/MessagesEn.php', $verbose, true );
114159
115 - // Count the changes
 160+ // Count the changes.
116161 $changedCount = 0;
117162
118 - // For each language
119 - sort($files);
 163+ // For each language.
 164+ sort( $files );
120165 foreach ( $files as $file ) {
121 - $svnfile = $svndir . "/" . $file;
122 - $localfile = $localdir . "/" . $file;
 166+ $svnfile = $svndir . '/' . $file;
 167+ $localfile = $localdir . '/' . $file;
123168
124 - // Compare the files
 169+ // Compare the files.
125170 $result = self::compareFiles( $svnfile, $localfile, $verbose, $changedEnglishStrings, false, true );
126171
127 - // And update the change counter
 172+ // And update the change counter.
128173 $changedCount += count( $result );
129174 }
130175
131 - // Log some nice info
 176+ // Log some nice info.
132177 self::myLog( "{$changedCount} Mediawiki messages are updated" );
 178+
133179 return $changedCount;
134180 }
135181
136 - // Remove all unneeded content
 182+ /**
 183+ * Removes all unneeded content from a file and returns it.
 184+ *
 185+ * @param $contents String
 186+ *
 187+ * @return String
 188+ */
137189 public static function cleanupFile( $contents ) {
138 - // We don't need any PHP tags
 190+ // We don't need any PHP tags.
139191 $contents = strtr( $contents,
140 - array( "<?php" => "",
141 - "?" . ">" => ""
142 - ) );
 192+ array(
 193+ '<?php' => '',
 194+ '?' . '>' => ''
 195+ )
 196+ );
 197+
143198 $results = array();
144 - // And we only want the messages array
 199+
 200+ // And we only want the messages array.
145201 preg_match( "/\\\$messages(.*\s)*?\);/", $contents, $results );
146202
147203 // If there is any!
148204 if ( !empty( $results[0] ) ) {
149205 $contents = $results[0];
150206 } else {
151 - $contents = "";
 207+ $contents = '';
152208 }
153209
154 - // Windows vs Unix always stinks when comparing files
 210+ // Windows vs Unix always stinks when comparing files.
155211 $contents = preg_replace( "/\\r\\n?/", "\n", $contents );
156212
157 - // return the cleaned up file
 213+ // Return the cleaned up file.
158214 return $contents;
159215 }
160216
 217+ /**
 218+ *
 219+ * @param $basefile String
 220+ */
161221 public static function getFileContents( $basefile ) {
162222 global $wgLocalisationUpdateRetryAttempts;
 223+
163224 $attempts = 0;
164 - $basefilecontents = "";
165 - // use cURL to get the SVN contents
 225+ $basefilecontents = '';
 226+
 227+ // Use cURL to get the SVN contents.
166228 if ( preg_match( "/^http/", $basefile ) ) {
167 - while( !$basefilecontents && $attempts <= $wgLocalisationUpdateRetryAttempts) {
168 - if($attempts > 0) {
 229+ while( !$basefilecontents && $attempts <= $wgLocalisationUpdateRetryAttempts ) {
 230+ if( $attempts > 0 ) {
169231 $delay = 1;
170 - self::myLog( "Failed to download " . $basefile . "; retrying in ${delay}s..." );
 232+ self::myLog( 'Failed to download ' . $basefile . "; retrying in ${delay}s..." );
171233 sleep( $delay );
172234 }
 235+
173236 $basefilecontents = Http::get( $basefile );
174237 $attempts++;
175238 }
176239 if ( !$basefilecontents ) {
177 - self::myLog( "Cannot get the contents of " . $basefile . " (curl)" );
 240+ self::myLog( 'Cannot get the contents of ' . $basefile . ' (curl)' );
178241 return false;
179242 }
180243 } else {// otherwise try file_get_contents
181244 if ( !( $basefilecontents = file_get_contents( $basefile ) ) ) {
182 - self::myLog( "Cannot get the contents of " . $basefile );
 245+ self::myLog( 'Cannot get the contents of ' . $basefile );
183246 return false;
184247 }
185248 }
 249+
186250 return $basefilecontents;
187251 }
188252
@@ -466,11 +530,15 @@
467531
468532 public static function filename( $lang ) {
469533 global $wgLocalisationUpdateDirectory, $wgCacheDirectory;
 534+
470535 $dir = $wgLocalisationUpdateDirectory ?
471536 $wgLocalisationUpdateDirectory :
472537 $wgCacheDirectory;
473 - if ( !$dir )
 538+
 539+ if ( !$dir ) {
474540 throw new MWException( 'No cache directory configured' );
 541+ }
 542+
475543 return "$dir/l10nupdate-$lang.cache";
476544 }
477545
@@ -478,11 +546,13 @@
479547 if ( !isset( self::$filecache[$lang] ) ) {
480548 $file = self::filename( $lang );
481549 $contents = @file_get_contents( $file );
 550+
482551 if ( $contents === false ) {
483552 wfDebug( "Failed to read file '$file'\n" );
484553 $retval = array();
485554 } else {
486555 $retval = unserialize( $contents );
 556+
487557 if ( $retval === false ) {
488558 wfDebug( "Corrupted data in file '$file'\n" );
489559 $retval = array();
@@ -490,13 +560,18 @@
491561 }
492562 self::$filecache[$lang] = $retval;
493563 }
 564+
494565 return self::$filecache[$lang];
495566 }
496567
497568 public static function writeFile( $lang, $var ) {
498569 $file = self::filename( $lang );
499 - if ( !@file_put_contents( $file, serialize( $var ) ) )
 570+
 571+ if ( !@file_put_contents( $file, serialize( $var ) ) ) {
500572 throw new MWException( "Failed to write to file '$file'" );
 573+ }
 574+
501575 self::$filecache[$lang] = $var;
502576 }
503 -}
 577+
 578+}
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r70210Follow up to r70206jeroendedauw22:03, 30 July 2010

Status & tagging log