r47687 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r47686‎ | r47687 | r47688 >
Date:21:54, 22 February 2009
Author:minuteelectron
Status:deferred
Tags:
Comment:
Babel:
* Rewrite parameter parser, now languages such as "be-tarask" will function (therefore, now all defualt MediaWiki languages).
* Remove option parser, they are no longer used as gender support has been made available in core.
Modified paths:
  • /trunk/extensions/Babel/Babel.class.php (modified) (history)
  • /trunk/extensions/Babel/Babel.php (modified) (history)
  • /trunk/extensions/Babel/BabelLanguageCodes.class.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Babel/Babel.class.php
@@ -51,9 +51,6 @@
5252 // Load various often used messages into the message member variables.
5353 $this->_getMessages();
5454
55 - // Parse the options and provide an array.
56 - $options = $this->_parseOptions( $parameters );
57 -
5855 // Do a link batch on all the parameters so that their information is
5956 // cached for use later on.
6057 $this->_doTemplateLinkBatch( $parameters );
@@ -70,7 +67,7 @@
7168
7269 $contents .= $parser->replaceVariables( "{{{$this->_addFixes( $name,'template' )}}}" );
7370
74 - } elseif( $chunks = $this->_parseParameter( $name ) ) {
 71+ } elseif( $chunks = $this->mParseParameter( $name ) ) {
7572
7673 $contents .= $this->_generateBox( $chunks[ 'code' ], $chunks[ 'level' ] );
7774 $contents .= $this->_generateCategories( $chunks[ 'code' ], $chunks[ 'level' ] );
@@ -199,43 +196,6 @@
200197 }
201198
202199 /**
203 - * Run through the array of parameters and generate an array of options
204 - * (all parameters starting with a '#') and unset them from the parameters
205 - * array.
206 - *
207 - * @param $parameters Array: Parameters passed to the parser function.
208 - * @return Array: All options with the options as keys.
209 - */
210 - private function _parseOptions( array &$parameters ) {
211 -
212 - // Open empty options array.
213 - $options = array();
214 -
215 - // Get list of all options.
216 - foreach( $parameters as $index => $value ) {
217 -
218 - // Classed as option if the parameter begins with a # and has at
219 - // least one other character.
220 - if( strpos( $value, '#' ) === 0 && strlen( $value ) > 1 ) {
221 -
222 - // Add to options array as a key, this is so that each option
223 - // only gets registered once.
224 - $options[ substr( $value, 1 ) ] = true;
225 -
226 - // Unset it from the parameters array so it does not get
227 - // processed as a box.
228 - unset( $parameters[ $index ] );
229 -
230 - }
231 -
232 - }
233 -
234 - // Return the array of options.
235 - return $options;
236 -
237 - }
238 -
239 - /**
240200 * Identify whether or not the template exists or not.
241201 *
242202 * @param $title String: Name of the template to check.
@@ -274,81 +234,30 @@
275235 * @param $parameter String: Parameter.
276236 * @return Array: { 'code' => xx, 'level' => xx }
277237 */
278 - private function _parseParameter( $parameter ) {
279 -
280 - // Break up the parameter on - (which separates it's two parts).
281 - $chunks = explode( '-', $parameter );
282 -
283 - // Initialise the return array.
 238+ private function mParseParameter( $parameter ) {
284239 $return = array();
285240
286 - // Actually parse the parameter.
287 - if( count( $chunks ) == 1 ) {
288 -
289 - // The parameter is in the form 'xx'.
290 -
291 - // Check whether the language code is valid.
292 - if( $this->mCodes->getCode( $chunks[ 0 ] ) !== false ) {
293 -
294 - // Set the code for returning.
295 - $return[ 'code' ] = $this->mCodes->getCode( $chunks[ 0 ] );
296 -
297 - // This form defaults to level 'N'.
298 - $return[ 'level' ] = 'N';
299 -
300 - // Everything needed has been gathered, return.
301 - return $return;
302 -
303 - } else {
304 -
305 - // Invalid language code, return false.
306 - return false;
307 -
308 - }
309 -
310 - } elseif( count( $chunks ) == 2 ) {
311 -
312 - // The parameter is in the form 'xx-x'.
313 -
314 - // Check whether the language code is valid.
315 - if( $this->mCodes->getCode( $chunks[ 0 ] ) !== false ) {
316 -
317 - // Set the code for returning.
318 - $return[ 'code' ] = $this->mCodes->getCode( $chunks[ 0 ] );
319 -
320 - } else {
321 -
322 - // Invalid language code, return false.
323 - return false;
324 -
325 - }
326 -
327 - // Check whether the level is valid.
328 - if( strtoupper( $chunks[ 1 ] ) == 'N' ) {
329 -
330 - $return[ 'level' ] = 'N';
331 -
332 - } elseif( $chunks[ 1 ] >= 0 && $chunks[ 1 ] <= 5 ) {
333 -
334 - $return[ 'level' ] = $chunks[ 1 ];
335 -
336 - } else {
337 -
338 - // Invalid language code.
339 - return false;
340 -
341 - }
342 -
343 - // Parameters decided, return parameters.
 241+ // Try treating the paramter as a language code (for native).
 242+ if( $this->mCodes->getCode( $parameter ) !== false && $this->mCodes->getCode( $parameter ) !== null ) {
 243+ $return[ 'code' ] = $this->mCodes->getCode( $parameter );
 244+ $return[ 'level' ] = 'N';
344245 return $return;
345 -
346 - } else {
347 -
348 - // Invalid parameters.
349 - return false;
350 -
351246 }
 247+ // Try splitting the paramter in to language and level, split on last hyphen.
 248+ $lastSplit = strrpos( $parameter, '-' );
 249+ if( $lastSplit === false ) return false;
 250+ $code = substr( $parameter, 0, $lastSplit );
 251+ $level = substr( $parameter, $lastSplit + 1 );
352252
 253+ // Validate code.
 254+ $return[ 'code' ] = $this->mCodes->getCode( $code );
 255+ if( $return[ 'code' ] === false ) return false;
 256+ // Validate level.
 257+ $intLevel = (int) $level;
 258+ if( ( $intLevel < 0 || $intLevel > 5 ) && $level !== 'N' ) return false;
 259+ $return[ 'level' ] = $level;
 260+
 261+ return $return;
353262 }
354263
355264 /**
Index: trunk/extensions/Babel/BabelLanguageCodes.class.php
@@ -90,7 +90,7 @@
9191 $cacheType = 'name';
9292 // Get correct code, even though it should already be correct.
9393 $code = $this->getCode( $code, $file, $cachePrefix );
94 - if( $code === false ) return false;
 94+ if( $code === false || $code === null ) return false;
9595 // Try cache.
9696 $fromCache = $this->mGetFromCache( $cacheType, $code, false, $cachePrefix );
9797 if( $fromCache !== false ) return $fromCache;
Index: trunk/extensions/Babel/Babel.php
@@ -21,7 +21,7 @@
2222 // Register extension credits.
2323 $wgExtensionCredits[ 'parserhook' ][] = array(
2424 'name' => 'Babel',
25 - 'version' => '1.1.0',
 25+ 'version' => '1.2.0',
2626 'author' => 'Robert Leverington',
2727 'url' => 'http://www.mediawiki.org/wiki/Extension:Babel',
2828 'description' => 'Adds a parser function to allow automated generation of a babel userbox column with the ability to include custom templates.',

Status & tagging log