r26250 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r26249‎ | r26250 | r26251 >
Date:11:34, 30 September 2007
Author:nikerabbit
Status:old
Tags:
Comment:
* Forgot to add this file (yes I know it's ugly)
Modified paths:
  • /trunk/extensions/Translate/SpecialMagic.php (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/SpecialMagic.php
@@ -0,0 +1,552 @@
 2+<?php
 3+
 4+class SpecialMagic extends SpecialPage {
 5+ protected $language = 'en';
 6+
 7+ public function __construct() {
 8+ SpecialPage::SpecialPage( 'Magic' );
 9+ $this->includable( true );
 10+
 11+ global $wgLang;
 12+ $this->language = $wgLang->getCode();
 13+ }
 14+
 15+ public function execute( $params ) {
 16+ $this->setHeaders();
 17+ $names = Language::getLanguageNames();
 18+ $params = explode( '/', $params );
 19+
 20+ global $wgRequest;
 21+ if ( $wgRequest->wasPosted() ) {
 22+ $posted = true;
 23+ } else {
 24+ $posted = false;
 25+ }
 26+
 27+ if ( $wgRequest->getText( 'export' ) !== '' ) {
 28+ $export = true;
 29+ } else {
 30+ $export = false;
 31+ }
 32+
 33+ if ( !isset( $params[0] ) ) { return; }
 34+ $o = null;
 35+ switch ( $params[0] ) {
 36+ case 'alias':
 37+ case 'special':
 38+ $o = new SpecialPageAliasesCM( $this->language );
 39+ break;
 40+ case 'magic':
 41+ $o = new MagicWordsCM( $this->language );
 42+ break;
 43+ case 'skin':
 44+ $o = new SkinNamesCM( $this->language );
 45+ break;
 46+ case 'namespace':
 47+ $o = new NamespaceCM( $this->language );
 48+ break;
 49+
 50+ default:
 51+ $this->showHelp();
 52+ return;
 53+ }
 54+
 55+ if ( $posted ) {
 56+ global $wgUser, $wgOut;
 57+ if ( !$wgUser->isAllowed( 'translate' ) ) {
 58+ $wgOut->permissionRequired( 'translate' );
 59+ return;
 60+ }
 61+
 62+ $o->save( $wgRequest );
 63+ }
 64+
 65+ if ( $o instanceof ComplexMessages ) {
 66+ if ( $export ) {
 67+ $result = Xml::element( 'textarea', array( 'rows' => '20' ) , $o->export() );
 68+ } else {
 69+ $result = $o->output();
 70+ }
 71+ }
 72+
 73+ global $wgOut;
 74+ $wgOut->addHTML( $result );
 75+ }
 76+
 77+ public function showHelp() {
 78+ global $wgOut;
 79+ $wgOut->addWikitext(
 80+<<<EOL
 81+Available pages are:
 82+# [[Special:Magic/special]]
 83+# [[Special:Magic/magic]]
 84+# [[Special:Magic/skin]]
 85+# [[Special:Magic/namespace]]
 86+EOL
 87+ );
 88+ }
 89+
 90+}
 91+
 92+
 93+abstract class ComplexMessages {
 94+
 95+ protected $tableAttributes = array(
 96+ 'class' => 'wikitable',
 97+ 'border' => '2',
 98+ 'cellpadding' => '4',
 99+ 'cellspacing' => '0',
 100+ 'style' => 'background-color: #F9F9F9; border: 1px #AAAAAA solid; border-collapse: collapse;',
 101+ );
 102+
 103+
 104+ protected $language = null;
 105+
 106+ public function __construct( $language ) {
 107+ $this->language = $language;
 108+ }
 109+
 110+ abstract function getArray();
 111+ abstract function getTitle();
 112+ abstract function formatElement( $element );
 113+
 114+ public function output() {
 115+ global $wgRequest;
 116+
 117+ $table['start'] = Xml::openElement( 'table', $this->tableAttributes );
 118+ $table['heading'] = Xml::element( 'th', array('colspan' => '4' ), $this->getTitle() );
 119+ //$table['subheading'][] = Xml::element( 'th', null, "Key" );
 120+ $table['subheading'][] = Xml::element( 'th', null, "Original" );
 121+ $table['subheading'][] = Xml::element( 'th', null, "Fallback" );
 122+ $table['subheading'][] = Xml::element( 'th', null, "Current" );
 123+ $table['subheading'][] = Xml::element( 'th', null, "To-be" );
 124+ $table['headings'] =
 125+ Xml::openElement( 'tr' ) .
 126+ $table['heading'] .
 127+ Xml::closeElement( 'tr' ) .
 128+ Xml::openElement( 'tr' ) .
 129+ implode( "\n", $table['subheading'] ) .
 130+ Xml::closeElement( 'tr' );
 131+
 132+
 133+ $array = $this->getArray();
 134+
 135+ foreach ( array_keys($array) as $key ) {
 136+ $table['row'][] =
 137+ Xml::openElement( 'tr' ) .
 138+ //Xml::element( 'td', null, $key ) .
 139+ Xml::element( 'td', null, $this->formatElement( $array[$key]['en'] ) ) .
 140+ Xml::element( 'td', null, $this->formatElement( $array[$key]['fb'] ) ) .
 141+ Xml::element( 'td', null, $this->formatElement( $array[$key]['xx'] ) ) .
 142+ Xml::tags( 'td', null, $this->editElement( $key,
 143+ $this->formatElement( $array[$key]['tb'] ) ) ) .
 144+ Xml::closeElement( 'tr' );
 145+ }
 146+
 147+ $table['row'][] =
 148+ Xml::tags( 'tr', null,
 149+ Xml::tags( 'td', array( 'colspan' => '4' ), $this->getButtons() )
 150+ );
 151+
 152+ $table['rows'] = implode( "\n", $table['row'] );
 153+ $table['end'] = Xml::closeElement( 'table' );
 154+
 155+ $finalTable = $table['start'] . $table['headings'] . $table['rows'] . $table['end'];
 156+ return Xml::tags( 'form',
 157+ array( 'method' => 'post', 'action' => $wgRequest->getRequestURL() ),
 158+ $finalTable );
 159+ }
 160+
 161+ public function editElement( $key, $contents ) {
 162+ return Xml::input( $this->getKeyForEdit( $key ) , 25, $contents );
 163+ }
 164+
 165+ public function getButtons() {
 166+ return Xml::submitButton( 'Save' ) . Xml::submitButton( 'export', array( 'name' => 'export') );
 167+ }
 168+
 169+ public function save( $request ) {
 170+ $title = Title::newFromText( 'MediaWiki:' . $this->getKeyForSave() );
 171+ $article = new Article( $title );
 172+
 173+ $data = "# Please do not edit this page directly\n<pre>\n" . $this->formatForSave( $request ) . "\n#</pre>";
 174+
 175+ $success = $article->doEdit( $data, 'Updated using Special:Magic', 0 );
 176+
 177+ if ( !$success ) {
 178+ throw new MWException( 'Save failed' );
 179+ }
 180+
 181+ }
 182+
 183+ function formatForSave( $request ) {
 184+ $array = $this->getArray();
 185+
 186+ $text = '';
 187+ foreach ( array_keys( $array ) as $key ) {
 188+ $text .= $key . '=' . $request->getText( $this->getKeyForEdit( $key ) ) . "\n" ;
 189+ }
 190+
 191+ return trim($text);
 192+ }
 193+
 194+ abstract function getKeyForEdit( $key );
 195+ abstract function getKeyForSave();
 196+ abstract function export();
 197+
 198+ function getSavedData() {
 199+ $data = wfMsg( $this->getKeyForSave() );
 200+
 201+ if ( wfEmptyMsg( $this->getKeyForSave(), $data ) ) {
 202+ return array();
 203+ }
 204+
 205+
 206+ $lines = explode( "\n", $data );
 207+ $array = array();
 208+ foreach ( $lines as $line ) {
 209+ if ( ltrim( $line[0] ) === '#' || ltrim( $line[0] ) === '<' ) { continue; }
 210+
 211+ $elements = explode( '=', $line, 2 );
 212+ if ( count( $elements ) !== 2 ) { continue; }
 213+ if ( trim( $elements[1] ) === '' ) { continue; }
 214+
 215+ $array[$elements[0]] = explode( ", ", $elements[1] );
 216+ }
 217+
 218+ return $array;
 219+ }
 220+
 221+ // Some helpers for subclasses
 222+ public function reduce( $all, &$small ) {
 223+ while( count( $all ) && count( $small ) &&
 224+ $all[ count($all) -1 ] === $small[ count($small) -1 ] ) {
 225+ unset( $all[ count($all) -1 ] ); // Is not reference
 226+ unset( $small[ count($small) -1 ] ); // Is reference
 227+ }
 228+ }
 229+}
 230+
 231+class SpecialPageAliasesCM extends ComplexMessages {
 232+
 233+ public function getArray() {
 234+
 235+ // Language objects
 236+ $LO['en'] = Language::factory( 'en' );
 237+ $LO['xx'] = Language::factory( $this->language );
 238+ $LO['fb'] = null; // override
 239+
 240+ $fallbackCandidate = Language::getFallbackFor( $this->language );
 241+ if( $fallbackCandidate && $fallbackCandidate !== 'en' ) {
 242+ $LO['fb'] = Language::factory( $fallbackCandidate );
 243+ }
 244+
 245+ $array['en'] = $LO['en']->getSpecialPageAliases();
 246+ $array['xx'] = $LO['xx']->getSpecialPageAliases();
 247+ $array['fb'] = array();
 248+ if ( $LO['fb'] instanceof Language ) {
 249+ $array['fb'] = $LO['fb']->getSpecialPageAliases();
 250+ }
 251+
 252+ $array['tb'] = $this->getSavedData();
 253+
 254+ $finishedArray = array();
 255+ foreach ( $array['en'] as $key => $aliases ) {
 256+ $finishedArray[$key]['en'] = isset($array['en'][$key]) ? $array['en'][$key] : array();
 257+ $finishedArray[$key]['xx'] = isset($array['xx'][$key]) ? $array['xx'][$key] : array();
 258+ $finishedArray[$key]['fb'] = isset($array['fb'][$key]) ? $array['fb'][$key] : array();
 259+ $this->reduce( $finishedArray[$key]['en'], $finishedArray[$key]['xx'] );
 260+ $this->reduce( $finishedArray[$key]['en'], $finishedArray[$key]['fb'] );
 261+ $finishedArray[$key]['tb'] = isset($array['tb'][$key]) ? $array['tb'][$key] : $finishedArray[$key]['xx'];
 262+ }
 263+ return $finishedArray;
 264+ }
 265+
 266+ public function formatElement( $element ) {
 267+ return str_replace('_', ' ', implode( ', ', $element ) );
 268+ }
 269+
 270+ public function getTitle() {
 271+ return "Special page aliases";
 272+ }
 273+
 274+ function getKeyForSave() {
 275+ return 'sp-translate-data-SpecialPageAliases' . '/' . $this->language;
 276+ }
 277+
 278+ function getKeyForEdit( $key ) {
 279+ return 'sp-translate-uga-' . $key;
 280+ }
 281+
 282+ public function export() {
 283+ $array = $this->getArray();
 284+ $text[] = '$specialPageAliases = array(';
 285+ foreach ( array_keys( $array) as $key ) {
 286+ $temp = "\t'$key'";
 287+ while ( strlen( $temp ) <= 28 ) { $temp .= ' '; }
 288+
 289+ if ( count($array[$key]['tb']) == 0 ) { continue; }
 290+ $both = array_map( array( $this, 'normalize' ), $array[$key]['tb'] );
 291+ $temp .= "=> array( " . implode( ', ', $both ) . " ),";
 292+ $text[] = $temp;
 293+ }
 294+
 295+ $text[] = ');';
 296+
 297+ return implode("\n", $text);
 298+ }
 299+
 300+ protected function normalize( $data ) {
 301+ return '"' . trim( str_replace( ' ', '_', $data ) ) . '"';
 302+ }
 303+
 304+}
 305+
 306+
 307+class MagicWordsCM extends ComplexMessages {
 308+
 309+ public function getArray() {
 310+
 311+ // Language objects
 312+ $LO['en'] = Language::factory( 'en' );
 313+ $LO['xx'] = Language::factory( $this->language );
 314+ $LO['fb'] = null; // override
 315+
 316+ $fallbackCandidate = Language::getFallbackFor( $this->language );
 317+ if( $fallbackCandidate && $fallbackCandidate !== 'en' ) {
 318+ $LO['fb'] = Language::factory( $fallbackCandidate );
 319+ }
 320+
 321+ $array['en'] = $LO['en']->getMagicWords();
 322+ $array['xx'] = $LO['xx']->getMagicWords();
 323+ $array['fb'] = array();
 324+ if ( $LO['fb'] instanceof Language ) {
 325+ $array['fb'] = $LO['fb']->getMagicWords();
 326+ }
 327+
 328+ $array['tb'] = $this->getSavedData();
 329+
 330+ $finishedArray = array();
 331+ foreach ( $array['en'] as $key => $aliases ) {
 332+ $finishedArray[$key]['en'] = isset($array['en'][$key]) ? $array['en'][$key] : array();
 333+ $finishedArray[$key]['xx'] = isset($array['xx'][$key]) ? $array['xx'][$key] : array();
 334+ $finishedArray[$key]['fb'] = isset($array['fb'][$key]) ? $array['fb'][$key] : array();
 335+ $this->reduce( $finishedArray[$key]['en'], $finishedArray[$key]['xx'] );
 336+ $this->reduce( $finishedArray[$key]['en'], $finishedArray[$key]['fb'] );
 337+ $finishedArray[$key]['tb'] = array( 'a' => 'hack ');
 338+ $finishedArray[$key]['tb'] += isset($array['tb'][$key]) ? $array['tb'][$key] : $finishedArray[$key]['xx'];
 339+ }
 340+ return $finishedArray;
 341+ }
 342+
 343+ public function formatElement( $element ) {
 344+ array_shift( $element );
 345+ return implode( ', ', $element );
 346+ }
 347+
 348+ public function getTitle() {
 349+ return "Magic words";
 350+ }
 351+
 352+ function getKeyForSave() {
 353+ return 'sp-translate-data-MagicWords' . '/' . $this->language;
 354+ }
 355+
 356+ function getKeyForEdit( $key ) {
 357+ return 'sp-translate-uga-' . $key;
 358+ }
 359+
 360+ public function export() {
 361+ $array = $this->getArray();
 362+ $en = Language::factory( 'en' );
 363+ $narray = $en->getMagicWords();
 364+
 365+ $text[] = '$magicWords = array(';
 366+ foreach ( array_keys( $array) as $key ) {
 367+ $temp = "\t'$key'";
 368+ while ( strlen( $temp ) <= 22 ) { $temp .= ' '; }
 369+
 370+ array_shift($array[$key]['tb']);
 371+ $case = $narray[$key][0];
 372+ array_shift($narray[$key]);
 373+ $original = array_map( array( $this, 'normalize' ), $narray[$key] );
 374+ if ( count($array[$key]['tb']) == 0 ) { continue; }
 375+ $both = array_map( array( $this, 'normalize' ), $array[$key]['tb'] );
 376+ $temp .= "=> array( $case, " . implode( ', ', $both ) . ", " . implode( ', ', $original ) . " ),";
 377+ $text[] = $temp;
 378+ }
 379+
 380+ $text[] = ');';
 381+
 382+ return implode("\n", $text);
 383+ }
 384+
 385+ protected function normalize( $data ) {
 386+ return '"' . trim( $data ) . '"';
 387+ }
 388+
 389+}
 390+
 391+
 392+class SkinNamesCM extends ComplexMessages {
 393+
 394+ public function getArray() {
 395+
 396+ // Language objects
 397+ $LO['en'] = Language::factory( 'en' );
 398+ $LO['xx'] = Language::factory( $this->language );
 399+ $LO['fb'] = null; // override
 400+
 401+ $fallbackCandidate = Language::getFallbackFor( $this->language );
 402+ if( $fallbackCandidate && $fallbackCandidate !== 'en' ) {
 403+ $LO['fb'] = Language::factory( $fallbackCandidate );
 404+ }
 405+
 406+ $array['en'] = $LO['en']->getSkinNames();
 407+ $array['xx'] = $LO['xx']->getSkinNames();
 408+ $array['fb'] = array();
 409+ if ( $LO['fb'] instanceof Language ) {
 410+ $array['fb'] = $LO['fb']->getSkinNames();
 411+ }
 412+
 413+ $array['tb'] = $this->getSavedData();
 414+
 415+ $finishedArray = array();
 416+ foreach ( $array['en'] as $key => $aliases ) {
 417+ $finishedArray[$key]['en'] = isset($array['en'][$key]) ? array( $array['en'][$key] ) : array();
 418+ $finishedArray[$key]['xx'] = isset($array['xx'][$key]) ? array( $array['xx'][$key] ) : array();
 419+ $finishedArray[$key]['fb'] = isset($array['fb'][$key]) ? array( $array['fb'][$key] ) : array();
 420+ $this->reduce( $finishedArray[$key]['en'], $finishedArray[$key]['xx'] );
 421+ $this->reduce( $finishedArray[$key]['en'], $finishedArray[$key]['fb'] );
 422+ $finishedArray[$key]['tb'] = isset($array['tb'][$key]) ? $array['tb'][$key] : $finishedArray[$key]['xx'];
 423+ }
 424+ return $finishedArray;
 425+ }
 426+
 427+ public function formatElement( $element ) {
 428+ return implode( ', ', $element );
 429+ }
 430+
 431+ public function getTitle() {
 432+ return "Skin Names";
 433+ }
 434+
 435+ function getKeyForSave() {
 436+ return 'sp-translate-data-SkinNames' . '/' . $this->language;
 437+ }
 438+
 439+ function getKeyForEdit( $key ) {
 440+ return 'sp-translate-uga-' . $key;
 441+ }
 442+
 443+ public function export() {
 444+ $array = $this->getArray();
 445+
 446+ $text[] = '$skinNames = array(';
 447+ foreach ( array_keys( $array) as $key ) {
 448+ $temp = "\t'$key'";
 449+ while ( strlen( $temp ) <= 14 ) { $temp .= ' '; }
 450+
 451+ if ( count($array[$key]['tb']) == 0 ) { continue; }
 452+ $both = array_map( array( $this, 'normalize' ), $array[$key]['tb'] );
 453+ $temp .= "=> array( " . implode( ', ', $both ) . " ),";
 454+ $text[] = $temp;
 455+ }
 456+
 457+ $text[] = ');';
 458+
 459+ return implode("\n", $text);
 460+ }
 461+
 462+ protected function normalize( $data ) {
 463+ return '"' . trim( $data ) . '"';
 464+ }
 465+
 466+}
 467+
 468+class NamespaceCM extends ComplexMessages {
 469+
 470+ public function getArray() {
 471+
 472+ // Language objects
 473+ $LO['en'] = Language::factory( 'en' );
 474+ $LO['xx'] = Language::factory( $this->language );
 475+ $LO['fb'] = null; // override
 476+
 477+ $fallbackCandidate = Language::getFallbackFor( $this->language );
 478+ if( $fallbackCandidate && $fallbackCandidate !== 'en' ) {
 479+ $LO['fb'] = Language::factory( $fallbackCandidate );
 480+ }
 481+
 482+ $array['en'] = $LO['en']->getNamespaces();
 483+ $array['xx'] = $LO['xx']->getNamespaces();
 484+ $array['fb'] = array();
 485+ if ( $LO['fb'] instanceof Language ) {
 486+ $array['fb'] = $LO['fb']->getNamespaces();
 487+ }
 488+
 489+ $array['tb'] = $this->getSavedData();
 490+
 491+ $finishedArray = array();
 492+ foreach ( $array['en'] as $key => $aliases ) {
 493+ if ( $key == 4 || $key > 15 ) { continue; }
 494+ $finishedArray[$key]['en'] = isset($array['en'][$key]) ? array( $array['en'][$key] ) : array();
 495+ $finishedArray[$key]['xx'] = isset($array['xx'][$key]) ? array( $array['xx'][$key] ) : array();
 496+ $finishedArray[$key]['fb'] = isset($array['fb'][$key]) ? array( $array['fb'][$key] ) : array();
 497+ $finishedArray[$key]['tb'] = isset($array['tb'][$key]) ? $array['tb'][$key] : $finishedArray[$key]['xx'];
 498+ }
 499+ return $finishedArray;
 500+ }
 501+
 502+ public function formatElement( $element ) {
 503+ return implode( ', ', $element );
 504+ }
 505+
 506+ public function getTitle() {
 507+ return "Namespace names";
 508+ }
 509+
 510+ function getKeyForSave() {
 511+ return 'sp-translate-data-Namespaces' . '/' . $this->language;
 512+ }
 513+
 514+ function getKeyForEdit( $key ) {
 515+ return 'sp-translate-uga-' . $key;
 516+ }
 517+
 518+ public function export() {
 519+ $array = $this->getArray();
 520+
 521+ $text = <<<EOL
 522+\$namespaceNames = array(
 523+ NS_MEDIA => '{$array[-2]['tb'][0]}',
 524+ NS_SPECIAL => '{$array[-1]['tb'][0]}',
 525+ NS_MAIN => '{$array[0]['tb'][0]}',
 526+ NS_TALK => '{$array[1]['tb'][0]}',
 527+ NS_USER => '{$array[2]['tb'][0]}',
 528+ NS_USER_TALK => '{$array[3]['tb'][0]}',
 529+ # NS_PROJECT set by \$wgMetaNamespace
 530+ NS_PROJECT_TALK => '{$array[5]['tb'][0]}',
 531+ NS_IMAGE => '{$array[6]['tb'][0]}',
 532+ NS_IMAGE_TALK => '{$array[7]['tb'][0]}',
 533+ NS_MEDIAWIKI => '{$array[8]['tb'][0]}',
 534+ NS_MEDIAWIKI_TALK => '{$array[9]['tb'][0]}',
 535+ NS_TEMPLATE => '{$array[10]['tb'][0]}',
 536+ NS_TEMPLATE_TALK => '{$array[11]['tb'][0]}',
 537+ NS_HELP => '{$array[12]['tb'][0]}',
 538+ NS_HELP_TALK => '{$array[13]['tb'][0]}',
 539+ NS_CATEGORY => '{$array[14]['tb'][0]}',
 540+ NS_CATEGORY_TALK => '{$array[15]['tb'][0]}',
 541+);
 542+EOL;
 543+
 544+ return $text;
 545+ }
 546+
 547+ protected function normalize( $data ) {
 548+ return '"' . trim( $data ) . '"';
 549+ }
 550+
 551+}
 552+
 553+?>
\ No newline at end of file
Property changes on: trunk/extensions/Translate/SpecialMagic.php
___________________________________________________________________
Added: svn:eol-style
1554 + native

Status & tagging log