SpecialMagic.php

Go to the documentation of this file.
00001 <?php
00018 class SpecialMagic extends SpecialPage {
00019     const MODULE_MAGIC = 'words';
00020     const MODULE_SPECIAL = 'special';
00021     const MODULE_NAMESPACE = 'namespace';
00022 
00026     private $aModules = array(
00027         self::MODULE_SPECIAL,
00028         self::MODULE_NAMESPACE,
00029         self::MODULE_MAGIC
00030     );
00031 
00035     private $options = array();
00036     private $defaults = array();
00037     private $nondefaults = array();
00038 
00039     public function __construct() {
00040         parent::__construct( 'Magic' );
00041     }
00042 
00048     function getDescription() {
00049         return $this->msg( 'translate-magic-pagename' )->text();
00050     }
00051 
00057     protected function getForm() {
00058         global $wgScript;
00059 
00060         $form = Xml::tags( 'form',
00061             array(
00062                 'action' => $wgScript,
00063                 'method' => 'get'
00064             ),
00065 
00066             '<table><tr><td>' .
00067                 $this->msg( 'translate-page-language' )->escaped() .
00068                 '</td><td>' .
00069                 TranslateUtils::languageSelector(
00070                     $this->getLanguage()->getCode(),
00071                     $this->options['language']
00072                 ) .
00073                 '</td></tr><tr><td>' .
00074                 $this->msg( 'translate-magic-module' )->escaped() .
00075                 '</td><td>' .
00076                 $this->moduleSelector( $this->options['module'] ) .
00077                 '</td></tr><tr><td colspan="2">' .
00078                 Xml::submitButton( $this->msg( 'translate-magic-submit' )->text() ) . ' ' .
00079                 Xml::submitButton(
00080                     $this->msg( 'translate-magic-cm-export' )->text(),
00081                     array( 'name' => 'export' )
00082                 ) .
00083                 '</td></tr></table>' .
00084                 Html::hidden( 'title', $this->getTitle()->getPrefixedText() )
00085         );
00086 
00087         return $form;
00088     }
00089 
00096     protected function moduleSelector( $selectedId ) {
00097         // Give grep a chance to find the usages:
00098         // translate-magic-words, translate-magic-special, translate-magic-namespace
00099         $selector = new XmlSelect( 'module', 'module', $selectedId );
00100         foreach ( $this->aModules as $code ) {
00101             $selector->addOption( $this->msg( 'translate-magic-' . $code )->text(), $code );
00102         }
00103 
00104         return $selector->getHTML();
00105     }
00106 
00107     protected function setup( $parameters ) {
00108         $defaults = array(
00109             /* str  */'module'   => '',
00110             /* str  */'language' => $this->getUser()->getOption( 'language' ),
00111             /* bool */'export'   => false,
00112             /* bool */'savetodb' => false,
00113         );
00114 
00118         $nondefaults = array();
00119 
00123         $options = $defaults;
00124         $request = $this->getRequest();
00125         foreach ( $options as $v => $t ) {
00126             if ( is_bool( $t ) ) {
00127                 $r = $request->getBool( $v, $options[$v] );
00128             } elseif ( is_int( $t ) ) {
00129                 $r = $request->getInt( $v, $options[$v] );
00130             } elseif ( is_string( $t ) ) {
00131                 $r = $request->getText( $v, $options[$v] );
00132             }
00133 
00134             if ( !isset( $r ) ) {
00135                 throw new MWException( '$r was not set' );
00136             }
00137 
00138             wfAppendToArrayIfNotDefault( $v, $r, $defaults, $nondefaults );
00139         }
00140 
00141         $this->defaults = $defaults;
00142         $this->nondefaults = $nondefaults;
00143         $this->options = $nondefaults + $defaults;
00144     }
00145 
00149     public function execute( $parameters ) {
00150         $this->setup( $parameters );
00151         $this->setHeaders();
00152 
00153         $out = $this->getOutput();
00154         TranslateUtils::addSpecialHelpLink(
00155             $out,
00156             '//translatewiki.net/wiki/FAQ#Special:AdvancedTranslate', true
00157         );
00158 
00159         $out->addHTML( $this->getForm() );
00160 
00161         if ( !$this->options['module'] ) {
00162             return;
00163         }
00164         switch ( $this->options['module'] ) {
00165             case 'alias':
00166             case self::MODULE_SPECIAL:
00167                 $o = new SpecialPageAliasesCM( $this->options['language'] );
00168                 break;
00169             case self::MODULE_MAGIC:
00170                 $o = new MagicWordsCM( $this->options['language'] );
00171                 break;
00172             case self::MODULE_NAMESPACE:
00173                 $o = new NamespaceCM( $this->options['language'] );
00174                 break;
00175             default:
00176                 throw new MWException( "Unknown module {$this->options['module']}" );
00177         }
00178 
00179         $request = $this->getRequest();
00180         if ( $request->wasPosted() && $this->options['savetodb'] ) {
00181             if ( !$this->getUser()->isAllowed( 'translate' ) ) {
00182                 throw new PermissionsError( 'translate' );
00183             }
00184 
00185             $errors = array();
00186             $o->loadFromRequest( $request );
00187             $o->validate( $errors );
00188             if ( $errors ) {
00189                 $out->wrapWikiMsg( '<div class="error">$1</div>',
00190                     'translate-magic-notsaved' );
00191                 $this->outputErrors( $errors );
00192                 $out->addHTML( $o->output() );
00193 
00194                 return;
00195             } else {
00196                 $o->save( $request );
00197                 $out->wrapWikiMsg( '<strong>$1</strong>', 'translate-magic-saved' );
00198                 $out->addHTML( $o->output() );
00199 
00200                 return;
00201             }
00202         }
00203 
00204         if ( $this->options['export'] ) {
00205             $output = $o->export();
00206             if ( $output === '' ) {
00207                 $out->addWikiMsg( 'translate-magic-nothing-to-export' );
00208 
00209                 return;
00210             }
00211             $result = Xml::element( 'textarea', array( 'rows' => '30' ), $output );
00212             $out->addHTML( $result );
00213 
00214             return;
00215         }
00216 
00217         $out->addWikiMsg( 'translate-magic-help' );
00218         $errors = array();
00219         $o->validate( $errors );
00220         if ( $errors ) {
00221             $this->outputErrors( $errors );
00222         }
00223         $out->addHTML( $o->output() );
00224     }
00225 
00226     protected function outputErrors( $errors ) {
00227         $count = $this->getLanguage()->formatNum( count( $errors ) );
00228         $out = $this->getOutput();
00229         $out->addWikiMsg( 'translate-magic-errors', $count );
00230         $out->addHTML( '<ol>' );
00231         foreach ( $errors as $error ) {
00232             $out->addHTML( "<li>$error</li>" );
00233         }
00234         $out->addHTML( '</ol>' );
00235     }
00236 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3