msg( 'translate-magic-pagename' )->text(); } /** * Returns HTML5 output of the form * GLOBALS: $wgScript * @return string */ protected function getForm() { global $wgScript; $form = Xml::tags( 'form', [ 'action' => $wgScript, 'method' => 'get' ], '
' . $this->msg( 'translate-page-language' )->escaped() . '' . TranslateUtils::languageSelector( $this->getLanguage()->getCode(), $this->options['language'] ) . '
' . $this->msg( 'translate-magic-module' )->escaped() . '' . $this->moduleSelector( $this->options['module'] ) . '
' . Xml::submitButton( $this->msg( 'translate-magic-submit' )->text() ) . ' ' . Xml::submitButton( $this->msg( 'translate-magic-cm-export' )->text(), [ 'name' => 'export' ] ) . '
' . Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) ); return $form; } /** * Helper function get module selector. * * @param string $selectedId Which value should be selected by default * @return string HTML5-compatible select-element. */ protected function moduleSelector( $selectedId ) { // Give grep a chance to find the usages: // translate-magic-words, translate-magic-special, translate-magic-namespace $selector = new XmlSelect( 'module', 'module', $selectedId ); foreach ( $this->aModules as $code ) { $selector->addOption( $this->msg( 'translate-magic-' . $code )->text(), $code ); } return $selector->getHTML(); } protected function setup( $parameters ) { $defaults = [ /* str */'module' => '', /* str */'language' => $this->getUser()->getOption( 'language' ), /* bool */'export' => false, /* bool */'savetodb' => false ]; /** * Place where all non default variables will end. */ $nondefaults = []; /** * Temporary store possible values parsed from parameters. */ $options = $defaults; $request = $this->getRequest(); foreach ( $options as $v => $t ) { if ( is_bool( $t ) ) { $r = $request->getBool( $v, $options[$v] ); } elseif ( is_int( $t ) ) { $r = $request->getInt( $v, $options[$v] ); } elseif ( is_string( $t ) ) { $r = $request->getText( $v, $options[$v] ); } if ( !isset( $r ) ) { throw new MWException( '$r was not set' ); } wfAppendToArrayIfNotDefault( $v, $r, $defaults, $nondefaults ); } $this->defaults = $defaults; $this->nondefaults = $nondefaults; $this->options = $nondefaults + $defaults; } /** * The special page running code * * @param null|string $parameters * @throws MWException|PermissionsError */ public function execute( $parameters ) { $this->setup( $parameters ); $this->setHeaders(); $out = $this->getOutput(); $out->addHelpLink( '//translatewiki.net/wiki/FAQ#Special:AdvancedTranslate', true ); $out->addHTML( $this->getForm() ); if ( !$this->options['module'] ) { return; } switch ( $this->options['module'] ) { case 'alias': case self::MODULE_SPECIAL: $o = new SpecialPageAliasesCM( $this->options['language'] ); break; case self::MODULE_MAGIC: $o = new MagicWordsCM( $this->options['language'] ); break; case self::MODULE_NAMESPACE: $o = new NamespaceCM( $this->options['language'] ); break; default: throw new MWException( "Unknown module {$this->options['module']}" ); } $request = $this->getRequest(); if ( $this->options['savetodb'] && $request->wasPosted() ) { if ( !$this->getUser()->isAllowed( 'translate' ) ) { throw new PermissionsError( 'translate' ); } $errors = []; $o->loadFromRequest( $request ); $o->validate( $errors ); if ( $errors ) { $out->wrapWikiMsg( '
$1
', 'translate-magic-notsaved' ); $this->outputErrors( $errors ); $out->addHTML( $o->output() ); return; } else { $o->save( $request ); $out->wrapWikiMsg( '$1', 'translate-magic-saved' ); $out->addHTML( $o->output() ); return; } } if ( $this->options['export'] ) { $output = $o->export(); if ( $output === '' ) { $out->addWikiMsg( 'translate-magic-nothing-to-export' ); return; } $result = Xml::element( 'textarea', [ 'rows' => '30' ], $output ); $out->addHTML( $result ); return; } $out->addWikiMsg( 'translate-magic-help' ); $errors = []; $o->validate( $errors ); if ( $errors ) { $this->outputErrors( $errors ); } $out->addHTML( $o->output() ); } protected function outputErrors( $errors ) { $count = $this->getLanguage()->formatNum( count( $errors ) ); $out = $this->getOutput(); $out->addWikiMsg( 'translate-magic-errors', $count ); $out->addHTML( '
    ' ); foreach ( $errors as $error ) { $out->addHTML( "
  1. $error
  2. " ); } $out->addHTML( '
' ); } }