Index: trunk/extensions/Babel/Babel.class.php |
— | — | @@ -51,9 +51,6 @@ |
52 | 52 | // Load various often used messages into the message member variables. |
53 | 53 | $this->_getMessages(); |
54 | 54 | |
55 | | - // Parse the options and provide an array. |
56 | | - $options = $this->_parseOptions( $parameters ); |
57 | | - |
58 | 55 | // Do a link batch on all the parameters so that their information is |
59 | 56 | // cached for use later on. |
60 | 57 | $this->_doTemplateLinkBatch( $parameters ); |
— | — | @@ -70,7 +67,7 @@ |
71 | 68 | |
72 | 69 | $contents .= $parser->replaceVariables( "{{{$this->_addFixes( $name,'template' )}}}" ); |
73 | 70 | |
74 | | - } elseif( $chunks = $this->_parseParameter( $name ) ) { |
| 71 | + } elseif( $chunks = $this->mParseParameter( $name ) ) { |
75 | 72 | |
76 | 73 | $contents .= $this->_generateBox( $chunks[ 'code' ], $chunks[ 'level' ] ); |
77 | 74 | $contents .= $this->_generateCategories( $chunks[ 'code' ], $chunks[ 'level' ] ); |
— | — | @@ -199,43 +196,6 @@ |
200 | 197 | } |
201 | 198 | |
202 | 199 | /** |
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 | | - /** |
240 | 200 | * Identify whether or not the template exists or not. |
241 | 201 | * |
242 | 202 | * @param $title String: Name of the template to check. |
— | — | @@ -274,81 +234,30 @@ |
275 | 235 | * @param $parameter String: Parameter. |
276 | 236 | * @return Array: { 'code' => xx, 'level' => xx } |
277 | 237 | */ |
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 ) { |
284 | 239 | $return = array(); |
285 | 240 | |
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'; |
344 | 245 | return $return; |
345 | | - |
346 | | - } else { |
347 | | - |
348 | | - // Invalid parameters. |
349 | | - return false; |
350 | | - |
351 | 246 | } |
| 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 ); |
352 | 252 | |
| 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; |
353 | 262 | } |
354 | 263 | |
355 | 264 | /** |
Index: trunk/extensions/Babel/BabelLanguageCodes.class.php |
— | — | @@ -90,7 +90,7 @@ |
91 | 91 | $cacheType = 'name'; |
92 | 92 | // Get correct code, even though it should already be correct. |
93 | 93 | $code = $this->getCode( $code, $file, $cachePrefix ); |
94 | | - if( $code === false ) return false; |
| 94 | + if( $code === false || $code === null ) return false; |
95 | 95 | // Try cache. |
96 | 96 | $fromCache = $this->mGetFromCache( $cacheType, $code, false, $cachePrefix ); |
97 | 97 | if( $fromCache !== false ) return $fromCache; |
Index: trunk/extensions/Babel/Babel.php |
— | — | @@ -21,7 +21,7 @@ |
22 | 22 | // Register extension credits. |
23 | 23 | $wgExtensionCredits[ 'parserhook' ][] = array( |
24 | 24 | 'name' => 'Babel', |
25 | | - 'version' => '1.1.0', |
| 25 | + 'version' => '1.2.0', |
26 | 26 | 'author' => 'Robert Leverington', |
27 | 27 | 'url' => 'http://www.mediawiki.org/wiki/Extension:Babel', |
28 | 28 | 'description' => 'Adds a parser function to allow automated generation of a babel userbox column with the ability to include custom templates.', |