Index: trunk/extensions/Babel/Babel.class.php |
— | — | @@ -0,0 +1,721 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Main class for the Babel extension. |
| 6 | + * |
| 7 | + * @addtogroup Extensions |
| 8 | + */ |
| 9 | + |
| 10 | +class Babel { |
| 11 | + |
| 12 | + /* Define the three posisble genders as constants. |
| 13 | + */ |
| 14 | + const GENDER_FEMALE = 1; |
| 15 | + const GENDER_MALE = 2; |
| 16 | + const GENDER_NEUTER = 0; |
| 17 | + |
| 18 | + /* Various values from the message cache. |
| 19 | + */ |
| 20 | + private $_prefixes, $_suffixes, $_cellspacing, $_directionality, $_url, |
| 21 | + $_title; |
| 22 | + |
| 23 | + /* Whether or not male, female or neuter messages should be prefered. |
| 24 | + */ |
| 25 | + private $_gender = self::GENDER_NEUTER; |
| 26 | + |
| 27 | + /** |
| 28 | + * Registers the parser function hook. |
| 29 | + * |
| 30 | + * @return true |
| 31 | + */ |
| 32 | + static public function Setup() { |
| 33 | + |
| 34 | + /* Initialise the languages code object. |
| 35 | + */ |
| 36 | + global $wgLanguageCodes, $wgLanguageCodesFiles; |
| 37 | + $wgLanguageCodes = new LanguageCodes( $wgLanguageCodesFiles ); |
| 38 | + |
| 39 | + /* Initialise the Babel object. |
| 40 | + */ |
| 41 | + global $wgBabel; |
| 42 | + $wgBabel = new Babel; |
| 43 | + |
| 44 | + /* Register the hook within the parser object. |
| 45 | + */ |
| 46 | + global $wgParser; |
| 47 | + $wgParser->setFunctionHook( 'babel', array( $wgBabel, 'Render' ) ); |
| 48 | + |
| 49 | + /* Return true to ensure processing is continued and an exception is not |
| 50 | + * generated. |
| 51 | + */ |
| 52 | + return true; |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Registers the parser function magic word. |
| 58 | + * |
| 59 | + * @param array $magicWords The associative array of magic words on the |
| 60 | + * wiki for adding too. |
| 61 | + * @param string $langCode Content language code of the wiki. |
| 62 | + * @return true |
| 63 | + */ |
| 64 | + static public function Magic( array $magicWords, $langCode ) { |
| 65 | + |
| 66 | + /* Register the magic word, maybe one day this could be localised by adding |
| 67 | + * synonyms into the array -- but there is currently no simple way of doing |
| 68 | + * that given the current way of localisation. The first element is set to |
| 69 | + * 0 so that it can be case insensitive. |
| 70 | + */ |
| 71 | + $magicWords[ 'babel' ] = array( 0, 'babel' ); |
| 72 | + |
| 73 | + /* Return true to ensure processing is continued and an exception is not |
| 74 | + * generated. |
| 75 | + */ |
| 76 | + return true; |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + public function Render( $parser ) { |
| 81 | + |
| 82 | + /* Store all the parameters passed to this function in an array. |
| 83 | + */ |
| 84 | + $parameters = func_get_args(); |
| 85 | + |
| 86 | + /* Remove the first parameter (the parser object), the rest correspond |
| 87 | + * to the parameters passed to the babel parser function. |
| 88 | + */ |
| 89 | + unset( $parameters[ 0 ] ); |
| 90 | + |
| 91 | + /* Load the extension messages in basic languages (en, content and |
| 92 | + * user). |
| 93 | + */ |
| 94 | + wfLoadExtensionMessages( 'Babel' ); |
| 95 | + |
| 96 | + /* Load various often used messages into the message member variables. |
| 97 | + */ |
| 98 | + $this->_getMessages(); |
| 99 | + |
| 100 | + /* Parse the options and provide an array. |
| 101 | + */ |
| 102 | + $options = $this->_parseOptions( $parameters ); |
| 103 | + |
| 104 | + /* Process gender stuff. |
| 105 | + */ |
| 106 | + $this->_setGender( $options ); |
| 107 | + |
| 108 | + /* Do a link batch on all the parameters so that their information is |
| 109 | + * cached for use later on. |
| 110 | + */ |
| 111 | + $this->_doTemplateLinkBatch( $parameters ); |
| 112 | + |
| 113 | + /* Initialise an empty string for storing the contents of the tower. |
| 114 | + */ |
| 115 | + $contents = ''; |
| 116 | + |
| 117 | + /* Loop through each of the input parameters. |
| 118 | + */ |
| 119 | + foreach( $parameters as $name ) { |
| 120 | + |
| 121 | + /* Check if the parameter is a valid template name, if it is then |
| 122 | + * include that template. |
| 123 | + */ |
| 124 | + if( $this->_templateExists( $name ) && $name !== '' ) { |
| 125 | + |
| 126 | + $contents .= $parser->replaceVariables( "{{{$this->_addFixes( $name,'template' )}}}" ); |
| 127 | + |
| 128 | + } elseif( $chunks = $this->_parseParameter( $name ) ) { |
| 129 | + |
| 130 | + $contents .= $this->_generateBox( $chunks[ 'code' ], $chunks[ 'level' ] ); |
| 131 | + $contents .= $this->_generateCategories( $chunks[ 'code' ], $chunks[ 'level' ] ); |
| 132 | + |
| 133 | + } elseif( $this->_validTitle( $name ) ) { |
| 134 | + |
| 135 | + /* Non-existant page and invalid parameter syntax, red link */ |
| 136 | + $contents .= "[[Template:{$this->_addFixes( $name,'template' )}]]"; |
| 137 | + |
| 138 | + } else { |
| 139 | + |
| 140 | + /* Invalid title, output raw. |
| 141 | + */ |
| 142 | + $contents .= "Template:{$this->_addFixes( $name,'template' )}"; |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + /* Generate tower, filled with contents from loop. |
| 149 | + */ |
| 150 | + $r = <<<HEREDOC |
| 151 | +{| cellspacing="{$this->_cellspacing}" class="mw-babel-wrapper" |
| 152 | +! [[{$this->_url}|{$this->_top}]] |
| 153 | +|- |
| 154 | +| $contents |
| 155 | +|} |
| 156 | +HEREDOC; |
| 157 | + |
| 158 | + /* Outupt tower. |
| 159 | + */ |
| 160 | + return $r; |
| 161 | + |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Get the main messages used by Babel (e.g. prefixes and suffixes etc.) |
| 167 | + * and load them into several variables. |
| 168 | + */ |
| 169 | + private function _getMessages() { |
| 170 | + |
| 171 | + /* Create an array of all prefixes. |
| 172 | + */ |
| 173 | + $this->_prefixes = array( |
| 174 | + 'category' => wfMsgForContent( 'babel-category-prefix' ), |
| 175 | + 'template' => wfMsgForContent( 'babel-template-prefix' ), |
| 176 | + 'portal' => wfMsgForContent( 'babel-portal-prefix' ), |
| 177 | + ); |
| 178 | + |
| 179 | + /* Create an array of all suffixes. |
| 180 | + */ |
| 181 | + $this->_suffixes = array( |
| 182 | + 'category' => wfMsgForContent( 'babel-category-suffix' ), |
| 183 | + 'template' => wfMsgForContent( 'babel-template-suffix' ), |
| 184 | + 'portal' => wfMsgForContent( 'babel-portal-suffix' ), |
| 185 | + ); |
| 186 | + |
| 187 | + /* Miscellaneous messages. |
| 188 | + */ |
| 189 | + $this->_url = wfMsgForContent( 'babel-url' ); |
| 190 | + $this->_top = wfMsgForContent( 'babel' ); |
| 191 | + $this->_directionality = wfMsgForContent( 'babel-directionality' ); |
| 192 | + $this->_cellspacing = wfMsgForContent( 'babel-box-cellspacing' ); |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Adds prefixes and suffixes for a particular type to the string. |
| 198 | + * |
| 199 | + * @param string $string String to add prefixes and suffixes too. |
| 200 | + * @param string $type Type of prefixes and suffixes (template/portal/category). |
| 201 | + * @return string String with prefixes and suffixes added. |
| 202 | + */ |
| 203 | + private function _addFixes( $string, $type ) { |
| 204 | + |
| 205 | + return $this->_prefixes[ $type ] . $string . $this->_suffixes[ $type ]; |
| 206 | + |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Performs a link batch on a series of templates. |
| 211 | + * |
| 212 | + * @param array $parameters Array of templates to perform the link batch on. |
| 213 | + */ |
| 214 | + private function _doTemplateLinkBatch( $parameters ) { |
| 215 | + |
| 216 | + /* Prepare an array, this will be used to store the title objects for |
| 217 | + * each of the parameters. |
| 218 | + */ |
| 219 | + $titles = array(); |
| 220 | + |
| 221 | + /* Loop through the array passed to this function and generate a title |
| 222 | + * object for each, then add that title object to the title object |
| 223 | + * array if it is an object (invalid page names generate NULL rather |
| 224 | + * than a valid title object. |
| 225 | + */ |
| 226 | + foreach( $parameters as $name ) { |
| 227 | + |
| 228 | + /* Create the title object. |
| 229 | + */ |
| 230 | + $title = Title::newFromText( $this->_addFixes( $name,'template' ), NS_TEMPLATE ); |
| 231 | + |
| 232 | + /* Check if the title object was created sucessfully. |
| 233 | + */ |
| 234 | + if( is_object( $title ) ) { |
| 235 | + |
| 236 | + /* It was, add it to the array of title objects. |
| 237 | + */ |
| 238 | + $titles[] = $title; |
| 239 | + |
| 240 | + } |
| 241 | + |
| 242 | + } |
| 243 | + |
| 244 | + /* Create the link batch object for the array of title objects that has |
| 245 | + * been generated. |
| 246 | + */ |
| 247 | + $batch = new LinkBatch( $titles ); |
| 248 | + |
| 249 | + /* Execute the link batch. |
| 250 | + */ |
| 251 | + $batch->execute(); |
| 252 | + |
| 253 | + } |
| 254 | + |
| 255 | + /** |
| 256 | + * Run through the array of parameters and generate an array of options |
| 257 | + * (all parameters starting with a '#') and unset them from the parameters |
| 258 | + * array. |
| 259 | + * |
| 260 | + * @param array $parameters Array of parameters passed to the parser |
| 261 | + * function. |
| 262 | + * @return array List of all options with the options as keys. |
| 263 | + */ |
| 264 | + private function _parseOptions( array &$parameters ) { |
| 265 | + |
| 266 | + /* Open empty options array. |
| 267 | + */ |
| 268 | + $options = array(); |
| 269 | + |
| 270 | + /* Get list of all options. |
| 271 | + */ |
| 272 | + foreach( $parameters as $index => $value ) { |
| 273 | + |
| 274 | + /* Classed as option if the parameter begins with a # and has at |
| 275 | + * least one other character. |
| 276 | + */ |
| 277 | + if( strpos( $value, '#' ) === 0 && strlen( $value ) > 1 ) { |
| 278 | + |
| 279 | + /* Add to options array as a key, this is so that each option |
| 280 | + * only gets registered once. |
| 281 | + */ |
| 282 | + $options[ substr( $value, 1 ) ] = true; |
| 283 | + |
| 284 | + /* Unset it from the parameters array so it does not get |
| 285 | + * processed as a box. |
| 286 | + */ |
| 287 | + unset( $parameters[ $index ] ); |
| 288 | + |
| 289 | + } |
| 290 | + |
| 291 | + } |
| 292 | + |
| 293 | + /* Return the array of options. |
| 294 | + */ |
| 295 | + return $options; |
| 296 | + |
| 297 | + } |
| 298 | + |
| 299 | + /** |
| 300 | + * Identify what gender has been specified within the function. |
| 301 | + * |
| 302 | + * @param array $options Options array. |
| 303 | + */ |
| 304 | + private function _setGender( array $options ) { |
| 305 | + |
| 306 | + /* Identify whether #female or #male have been specified as options and |
| 307 | + * set gender as appropriate. |
| 308 | + */ |
| 309 | + if( array_key_exists( 'female', $options ) ) { |
| 310 | + |
| 311 | + $this->_gender = self::GENDER_FEMALE; |
| 312 | + |
| 313 | + } elseif( array_key_exists( 'male', $options ) ) { |
| 314 | + |
| 315 | + $this->_gender = self::GENDER_MALE; |
| 316 | + |
| 317 | + } else { |
| 318 | + |
| 319 | + $this->_gender = self::GENDER_NEUTER; |
| 320 | + |
| 321 | + } |
| 322 | + |
| 323 | + } |
| 324 | + |
| 325 | + /** |
| 326 | + * Identify whether or not the template exists or not. |
| 327 | + * |
| 328 | + * @param string $title Name of the template to check. |
| 329 | + * @return boolean Indication of whether the template exists. |
| 330 | + */ |
| 331 | + private function _templateExists( $title ) { |
| 332 | + |
| 333 | + /* Make title object from the templates title. |
| 334 | + */ |
| 335 | + $titleObj = Title::newFromText( $this->_addFixes( $title,'template' ), NS_TEMPLATE ); |
| 336 | + |
| 337 | + /* If the title object has been created (is of a valid title) and the template |
| 338 | + * exists return true, otherwise return false. |
| 339 | + */ |
| 340 | + return ( is_object( $titleObj ) && $titleObj->exists() ); |
| 341 | + |
| 342 | + } |
| 343 | + |
| 344 | + /** |
| 345 | + * Identify whether or not the passed string would make a valid title. |
| 346 | + * |
| 347 | + * @param string $title Name of title to check. |
| 348 | + * @return boolean Indication of whether or not the title is valid. |
| 349 | + */ |
| 350 | + private function _validTitle( $title ) { |
| 351 | + |
| 352 | + /* Make title object from the templates title. |
| 353 | + */ |
| 354 | + $titleObj = Title::newFromText( $this->_addFixes( $title, 'template' ), NS_TEMPLATE ); |
| 355 | + |
| 356 | + /* If the title object has been created (is of a valid title) return true. |
| 357 | + */ |
| 358 | + return is_object( $titleObj ); |
| 359 | + |
| 360 | + } |
| 361 | + |
| 362 | + /** |
| 363 | + * Parse a parameter, getting a language code and level. |
| 364 | + * |
| 365 | + * @param string $parameter Parameter. |
| 366 | + * @return array array( 'code' => xx, 'level' => xx ) |
| 367 | + */ |
| 368 | + private function _parseParameter( $parameter ) { |
| 369 | + |
| 370 | + /* Get language codes object. |
| 371 | + */ |
| 372 | + global $wgLanguageCodes; |
| 373 | + |
| 374 | + /* Break up the parameter on - (which seperates it's two parts). |
| 375 | + */ |
| 376 | + $chunks = explode( '-', $parameter ); |
| 377 | + |
| 378 | + /* Initialise the return array. |
| 379 | + */ |
| 380 | + $return = array(); |
| 381 | + |
| 382 | + /* Actually parse the parameter. |
| 383 | + */ |
| 384 | + if( count( $chunks ) == 1 ) { |
| 385 | + |
| 386 | + /* The parameter is in the form 'xx'. |
| 387 | + */ |
| 388 | + |
| 389 | + /* Check whether the language code is valid. |
| 390 | + */ |
| 391 | + if( $wgLanguageCodes->check( $chunks[ 0 ] ) ) { |
| 392 | + |
| 393 | + /* Set the code for returning. |
| 394 | + */ |
| 395 | + $return[ 'code' ] = $wgLanguageCodes->get( $chunks[ 0 ] ); |
| 396 | + |
| 397 | + /* This form defaults to level 'N'. |
| 398 | + */ |
| 399 | + $return[ 'level' ] = 'N'; |
| 400 | + |
| 401 | + /* Everything needed has been gathered, return. |
| 402 | + */ |
| 403 | + return $return; |
| 404 | + |
| 405 | + } else { |
| 406 | + |
| 407 | + /* Invalid language code, return false. |
| 408 | + */ |
| 409 | + return false; |
| 410 | + |
| 411 | + } |
| 412 | + |
| 413 | + } elseif( count( $chunks ) == 2 ) { |
| 414 | + |
| 415 | + /* The parameter is in the form 'xx-x'. |
| 416 | + */ |
| 417 | + |
| 418 | + /* Check whether the language code is valid. |
| 419 | + */ |
| 420 | + if( $wgLanguageCodes->check( $chunks[ 0 ] ) ) { |
| 421 | + |
| 422 | + /* Set the code for returning. |
| 423 | + */ |
| 424 | + $return[ 'code' ] = $wgLanguageCodes->get( $chunks[ 0 ] ); |
| 425 | + |
| 426 | + } else { |
| 427 | + |
| 428 | + /* Invalid language code, return false. |
| 429 | + */ |
| 430 | + return false; |
| 431 | + |
| 432 | + } |
| 433 | + |
| 434 | + /* Check whether the level is valid. |
| 435 | + */ |
| 436 | + if( strtoupper( $chunks[ 1 ] ) == 'N' ) { |
| 437 | + |
| 438 | + $return[ 'level' ] = 'N'; |
| 439 | + |
| 440 | + } elseif( $chunks[ 1 ] >= 0 && $chunks[ 1 ] <= 5 ) { |
| 441 | + |
| 442 | + $return[ 'level' ] = $chunks[ 1 ]; |
| 443 | + |
| 444 | + } else { |
| 445 | + |
| 446 | + /* Invalid language code. |
| 447 | + */ |
| 448 | + return false; |
| 449 | + |
| 450 | + } |
| 451 | + |
| 452 | + /* Parameters decided, return parameters. |
| 453 | + */ |
| 454 | + return $return; |
| 455 | + |
| 456 | + } else { |
| 457 | + |
| 458 | + /* Invalid parameters. |
| 459 | + */ |
| 460 | + return false; |
| 461 | + |
| 462 | + } |
| 463 | + |
| 464 | + } |
| 465 | + |
| 466 | + /** |
| 467 | + * Generate a babel box for the given language and level. |
| 468 | + * |
| 469 | + * @param string $code Language code to use. |
| 470 | + * @param string or integer $level Level of ability to use. |
| 471 | + */ |
| 472 | + private function _generateBox( $code, $level ) { |
| 473 | + |
| 474 | + /* Get language codes class. |
| 475 | + */ |
| 476 | + global $wgLanguageCodes; |
| 477 | + |
| 478 | + /* Get code in favoured standard. |
| 479 | + */ |
| 480 | + $code = $wgLanguageCodes->get( $code ); |
| 481 | + |
| 482 | + /* Generate the text displayed on the left hand side of the |
| 483 | + * box. |
| 484 | + */ |
| 485 | + $header = "[[{$this->_addFixes( $code,'portal' )}|$code]]-$level"; |
| 486 | + |
| 487 | + /* Get MediaWiki supported language codes\names. |
| 488 | + */ |
| 489 | + $nativeNames = Language::getLanguageNames(); |
| 490 | + |
| 491 | + /* Load extension messages for box language and it's fallback if it is |
| 492 | + * a valid MediaWiki language. |
| 493 | + */ |
| 494 | + if( array_key_exists( $code, $nativeNames ) ) { |
| 495 | + wfLoadExtensionMessages( 'Babel', $code ); |
| 496 | + wfLoadExtensionMessages( 'Babel', Language::getFallbackFor( $code ) ); |
| 497 | + } |
| 498 | + |
| 499 | + /* Get the language names. |
| 500 | + */ |
| 501 | + if( class_exists( 'LanguageNames' ) ) { |
| 502 | + $names = LanguageNames::getNames( $code ); |
| 503 | + } else { |
| 504 | + $names = $nativeNames; |
| 505 | + } |
| 506 | + |
| 507 | + /* Ensure the language code has a corresponding name. |
| 508 | + */ |
| 509 | + if( array_key_exists( $code, $names ) ) { |
| 510 | + $name = $names[ $code ]; |
| 511 | + } else { |
| 512 | + $name = $wgLanguageCodes->name( $code, 'en' ); |
| 513 | + } |
| 514 | + |
| 515 | + /* Generate the text displayed on the right hand side of the |
| 516 | + * box. |
| 517 | + */ |
| 518 | + $text = $this->_getText( $name, $code, $level ); |
| 519 | + |
| 520 | + /* Get the directionality for the current language. |
| 521 | + */ |
| 522 | + $dir = wfMsgExt( "babel-directionality", |
| 523 | + array( 'language' => $code ) |
| 524 | + ); |
| 525 | + |
| 526 | + /* Generate box and add return. |
| 527 | + */ |
| 528 | + return <<<HEREDOC |
| 529 | +<div class="mw-babel-box mw-babel-box-$level" dir="{$this->_directionality}"> |
| 530 | +{| cellspacing="{$this->_cellspacing}" |
| 531 | +! dir="{$this->_directionality}" | $header |
| 532 | +| dir="$dir" | $text |
| 533 | +|} |
| 534 | +</div> |
| 535 | +HEREDOC; |
| 536 | + |
| 537 | + } |
| 538 | + |
| 539 | + /** |
| 540 | + * Get the text to display in the language box for specific language and |
| 541 | + * level. |
| 542 | + * |
| 543 | + * @param string $language Language code of language to use. |
| 544 | + * @param string $level Level to use. |
| 545 | + * @return string Text for display, in wikitext format. |
| 546 | + */ |
| 547 | + private function _getText( $name, $language, $level ) { |
| 548 | + |
| 549 | + /* If gender not neuter then try getting the gender specific message. |
| 550 | + */ |
| 551 | + if( $this->_gender === self::GENDER_FEMALE ) { |
| 552 | + |
| 553 | + /* Try the language of the box in female. |
| 554 | + */ |
| 555 | + $text = wfMsgExt( "babel-$level-n-female", |
| 556 | + array( 'language' => $language ), |
| 557 | + ":Category:{$this->_addFixes( "$language-$level",'category' )}", |
| 558 | + ":Category:{$this->_addFixes( $language,'category' )}" |
| 559 | + ); |
| 560 | + |
| 561 | + /* Get the fallback message for comparison in female. |
| 562 | + */ |
| 563 | + $fallback = wfMsgExt( "babel-$level-n-female", |
| 564 | + array( 'language' => Language::getFallbackfor( $language ) ), |
| 565 | + ":Category:{$this->_addFixes( "$language-$level",'category' )}", |
| 566 | + ":Category:{$this->_addFixes( $language,'category' )}" |
| 567 | + ); |
| 568 | + |
| 569 | + /* Translation not found, use the generic translation of the |
| 570 | + * highest level fallback possible in female. |
| 571 | + */ |
| 572 | + if( $text == $fallback ) { |
| 573 | + |
| 574 | + $text = wfMsgExt( "babel-$level-female", |
| 575 | + array( 'language' => $language ), |
| 576 | + ":Category:{$this->_addFixes( "$language-$level",'category')}", |
| 577 | + ":Category:{$this->_addFixes( $language,'category' )}", |
| 578 | + $name |
| 579 | + ); |
| 580 | + |
| 581 | + } |
| 582 | + |
| 583 | + /* Not empty, return. |
| 584 | + */ |
| 585 | + if( $text != '' ) { |
| 586 | + |
| 587 | + return $text; |
| 588 | + |
| 589 | + } |
| 590 | + |
| 591 | + } elseif( $this->_gender === self::GENDER_MALE ) { |
| 592 | + |
| 593 | + /* Try the language of the box in male. |
| 594 | + */ |
| 595 | + $text = wfMsgExt( "babel-$level-n-male", |
| 596 | + array( 'language' => $language ), |
| 597 | + ":Category:{$this->_addFixes( "$language-$level",'category' )}", |
| 598 | + ":Category:{$this->_addFixes( $language,'category' )}" |
| 599 | + ); |
| 600 | + |
| 601 | + /* Get the fallback message for comparison in male. |
| 602 | + */ |
| 603 | + $fallback = wfMsgExt( "babel-$level-n-male", |
| 604 | + array( 'language' => Language::getFallbackfor( $language ) ), |
| 605 | + ":Category:{$this->_addFixes( "$language-$level",'category' )}", |
| 606 | + ":Category:{$this->_addFixes( $language,'category' )}" |
| 607 | + ); |
| 608 | + |
| 609 | + /* Translation not found, use the generic translation of the |
| 610 | + * highest level fallback possible in male. |
| 611 | + */ |
| 612 | + if( $text == $fallback ) { |
| 613 | + |
| 614 | + $text = wfMsgExt( "babel-$level-male", |
| 615 | + array( 'language' => $language ), |
| 616 | + ":Category:{$this->_addFixes( "$language-$level",'category')}", |
| 617 | + ":Category:{$this->_addFixes( $language,'category' )}", |
| 618 | + $name |
| 619 | + ); |
| 620 | + |
| 621 | + } |
| 622 | + |
| 623 | + /* Not empty, return. |
| 624 | + */ |
| 625 | + if( $text != '' ) { |
| 626 | + |
| 627 | + return $text; |
| 628 | + |
| 629 | + } |
| 630 | + |
| 631 | + } |
| 632 | + |
| 633 | + /* Try the language of the box. |
| 634 | + */ |
| 635 | + $text = wfMsgExt( "babel-$level-n", |
| 636 | + array( 'language' => $language ), |
| 637 | + ":Category:{$this->_addFixes( "$language-$level",'category' )}", |
| 638 | + ":Category:{$this->_addFixes( $language,'category' )}" |
| 639 | + ); |
| 640 | + |
| 641 | + /* Get the fallback message for comparison. |
| 642 | + */ |
| 643 | + $fallback = wfMsgExt( "babel-$level-n", |
| 644 | + array( 'language' => Language::getFallbackfor( $language ) ), |
| 645 | + ":Category:{$this->_addFixes( "$language-$level",'category' )}", |
| 646 | + ":Category:{$this->_addFixes( $language,'category' )}" |
| 647 | + ); |
| 648 | + |
| 649 | + /* Translation not found, use the generic translation of the |
| 650 | + * highest level fallback possible. |
| 651 | + */ |
| 652 | + if( $text == $fallback ) { |
| 653 | + |
| 654 | + $text = wfMsgExt( "babel-$level", |
| 655 | + array( 'language' => $language ), |
| 656 | + ":Category:{$this->_addFixes( "$language-$level",'category')}", |
| 657 | + ":Category:{$this->_addFixes( $language,'category' )}", |
| 658 | + $name |
| 659 | + ); |
| 660 | + |
| 661 | + } |
| 662 | + |
| 663 | + return $text; |
| 664 | + |
| 665 | + } |
| 666 | + |
| 667 | + /** |
| 668 | + * Generate categories for the given language and level. |
| 669 | + * |
| 670 | + * @param string $code Language code to use. |
| 671 | + * @param string or integer $level Level of ability to use. |
| 672 | + */ |
| 673 | + private function _generateCategories( $code, $level ) { |
| 674 | + |
| 675 | + /* Get whether or not to use main categories. |
| 676 | + */ |
| 677 | + global $wgBabelUseMainCategories; |
| 678 | + |
| 679 | + /* Get whether or not to use level zero categories. |
| 680 | + */ |
| 681 | + global $wgBabelUseLevelZeroCategory; |
| 682 | + |
| 683 | + /* Get whether or not to use simple categories. |
| 684 | + */ |
| 685 | + global $wgBabelUseSimpleCategories; |
| 686 | + |
| 687 | + /* Get user object. |
| 688 | + */ |
| 689 | + global $wgUser; |
| 690 | + |
| 691 | + /* Intialise return value. |
| 692 | + */ |
| 693 | + $r = ''; |
| 694 | + |
| 695 | + /* Add to main language category if the level is not zero. |
| 696 | + */ |
| 697 | + if( $wgBabelUseMainCategories && ( $level === 'N' || ( $wgBabelUseLevelZeroCategory && $level === 0 ) || $level > 0 ) ) { |
| 698 | + |
| 699 | + /* Add category wikitext to box tower. |
| 700 | + */ |
| 701 | + $r .= "[[Category:{$this->_addFixes( $code,'category' )}|$level{$wgUser->getName()}]]"; |
| 702 | + |
| 703 | + } |
| 704 | + |
| 705 | + /* Add to level categories, only adding it to the level 0 |
| 706 | + * one if it is set to be used. |
| 707 | + */ |
| 708 | + if( !$wgBabelUseSimpleCategories && ( $level === 'N' || ( $wgBabelUseLevelZeroCategory && $level === 0 ) || $level > 0 ) ) { |
| 709 | + |
| 710 | + /* Add category wikitext to box tower. |
| 711 | + */ |
| 712 | + $r .= "[[Category:{$this->_addFixes( "$code-$level",'category' )}|{$wgUser->getName()}]]"; |
| 713 | + |
| 714 | + } |
| 715 | + |
| 716 | + /* Return categories. |
| 717 | + */ |
| 718 | + return $r; |
| 719 | + |
| 720 | + } |
| 721 | + |
| 722 | +} |
Property changes on: trunk/extensions/Babel/Babel.class.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 723 | + native |
Index: trunk/extensions/Babel/Babel.php |
— | — | @@ -42,10 +42,8 @@ |
43 | 43 | $wgExtensionMessagesFiles[ 'Babel' ] = dirname( __FILE__ ) . '/Babel.i18n.php'; |
44 | 44 | |
45 | 45 | // Register autoload classes. |
46 | | -$wgAutoloadClasses[ 'Babel' ] = dirname( __FILE__ ) . '/includes/Babel.php'; |
47 | | -$wgAutoloadClasses[ 'LanguageCodes' ] = dirname( __FILE__ ) . '/includes/LanguageCodes.php'; |
48 | | -$wgAutoloadClasses[ 'GenerateLanguageCodes' ] = dirname( __FILE__ ) . '/includes/GenerateLanguageCodes.php'; |
49 | | -$wgAutoloadClasses[ 'GenerateLanguageCodes_ISO_639_3' ] = dirname( __FILE__ ) . '/includes/GenerateLanguageCodes_ISO_639_3.php'; |
| 46 | +$wgAutoloadClasses[ 'Babel' ] = dirname( __FILE__ ) . '/Babel.class.php'; |
| 47 | +$wgAutoloadClasses[ 'LanguageCodes' ] = dirname( __FILE__ ) . '/LanguageCodes.class.php'; |
50 | 48 | |
51 | 49 | // Definitions. |
52 | 50 | define( 'ISO_639_1', 1 ); |
Index: trunk/extensions/Babel/LanguageCodes.class.php |
— | — | @@ -0,0 +1,155 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * LanguageCodes class for Babel extension, deals with ISO 639-1 and ISO 639-3 |
| 6 | + * code checking and ordering. |
| 7 | + * |
| 8 | + * @addtogroup Extensions |
| 9 | + */ |
| 10 | + |
| 11 | +class LanguageCodes { |
| 12 | + |
| 13 | + private $_codes; |
| 14 | + private $_order = array( |
| 15 | + ISO_639_1, |
| 16 | + ISO_639_3, |
| 17 | + ); |
| 18 | + |
| 19 | + /** |
| 20 | + * Class constructor. |
| 21 | + * |
| 22 | + * @param array $file Array of files to load language codes from. |
| 23 | + */ |
| 24 | + public function __construct( $files ) { |
| 25 | + |
| 26 | + /* Load the codes from the passed file array. |
| 27 | + */ |
| 28 | + $this->_loadAll( $files ); |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Load the language codes from an array of standards to files into the |
| 34 | + * language codes array. |
| 35 | + * |
| 36 | + * @param array $file Array of files to load language codes from. |
| 37 | + */ |
| 38 | + private function _loadAll( $files ) { |
| 39 | + |
| 40 | + /* Loop through all standards. |
| 41 | + */ |
| 42 | + foreach( $this->_order as $standard ) { |
| 43 | + |
| 44 | + /* Load file for the current standard. |
| 45 | + */ |
| 46 | + $this->_load( $standard, $files[ $standard ] ); |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Load the language codes from a given file into the language codes array. |
| 54 | + * |
| 55 | + * @param const $standard Standard for the codes being loaded. |
| 56 | + * @param string $file File to load language codes from. |
| 57 | + */ |
| 58 | + private function _load( $standard, $file ) { |
| 59 | + |
| 60 | + /* Include the codes file. |
| 61 | + */ |
| 62 | + include( $file ); |
| 63 | + |
| 64 | + /* Push the array of codes into the class method. |
| 65 | + */ |
| 66 | + $this->_codes[ $standard ] = $codes; |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Check if the specified code is a valid language code. |
| 72 | + * |
| 73 | + * @param string $code Code to check. |
| 74 | + * @return boolean Whether or not the code is valid. |
| 75 | + */ |
| 76 | + public function check( $code ) { |
| 77 | + |
| 78 | + /* Check if the specified code has a key in the codes array for each of the |
| 79 | + * standards and return result. |
| 80 | + */ |
| 81 | + foreach( $this->_order as $index ) { |
| 82 | + |
| 83 | + if( array_key_exists( strtolower( $code ), $this->_codes[ $index ] ) ) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get the language code to use for a specific language, in the highest |
| 93 | + * ordered standard possible. |
| 94 | + * |
| 95 | + * @param string $code Code to get language code for. |
| 96 | + * @return string Correct code. |
| 97 | + */ |
| 98 | + public function get( $code ) { |
| 99 | + |
| 100 | + /* Loop through all the standards trying to find the language code |
| 101 | + * specified. |
| 102 | + */ |
| 103 | + foreach( $this->_order as $standard1 ) { |
| 104 | + |
| 105 | + if( array_key_exists( strtolower( $code ), $this->_codes[ $standard1 ] ) ) { |
| 106 | + |
| 107 | + /* Loop through all the standards again to find the highest |
| 108 | + * level alternate code. |
| 109 | + */ |
| 110 | + foreach( $this->_order as $standard2 ) { |
| 111 | + |
| 112 | + if( $standard1 == $standard2 ) { |
| 113 | + |
| 114 | + return $code; |
| 115 | + |
| 116 | + } elseif( array_key_exists( $standard2, $this->_codes[ $standard1 ][ $code ] ) ) { |
| 117 | + |
| 118 | + return $this->_codes[ $standard1 ][ $code ][ $standard2 ]; |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + /* Nothing found, return input. |
| 129 | + */ |
| 130 | + return $code; |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Get the name of a language in a specific language (currently only eng |
| 136 | + * supported until a index of ISO 639-1 is built with language names). |
| 137 | + * |
| 138 | + * @param string $code Code to get name for. |
| 139 | + * @param string $lang Language to get name of code in. |
| 140 | + * @return string Name of language in specified language. |
| 141 | + */ |
| 142 | + public function name( $code, $lang = 'eng' ) { |
| 143 | + |
| 144 | + $code = $this->get( $code ); |
| 145 | + |
| 146 | + if( array_key_exists( $code, $this->_codes[ ISO_639_3 ] ) && array_key_exists( "name_$lang", $this->_codes[ ISO_639_3 ][ $code ] ) ) { |
| 147 | + return $this->_codes[ ISO_639_3 ][ $code ][ "name_$lang" ]; |
| 148 | + } |
| 149 | + |
| 150 | + /* Nothing found, return input. |
| 151 | + */ |
| 152 | + return $code; |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | +} |
Property changes on: trunk/extensions/Babel/LanguageCodes.class.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 157 | + native |