SpecialTranslationStash.php

Go to the documentation of this file.
00001 <?php
00015 class SpecialTranslationStash extends SpecialPage {
00017     protected $stash;
00018 
00019     function __construct() {
00020         parent::__construct( 'TranslationStash' );
00021     }
00022 
00023     public function execute( $params ) {
00024         global $wgTranslateSandboxLimit;
00025 
00026         $this->setHeaders();
00027         $out = $this->getOutput();
00028 
00029         $this->stash = new TranslationStashStorage( wfGetDB( DB_MASTER ) );
00030 
00031         if ( !$this->hasPermissionToUse() ) {
00032             $out->redirect( Title::newMainPage()->getLocalUrl() );
00033 
00034             return;
00035         }
00036 
00037         $out->addJsConfigVars( 'wgTranslateSandboxLimit', $wgTranslateSandboxLimit );
00038         $out->addModules( 'ext.translate.special.translationstash' );
00039         $this->showPage();
00040     }
00041 
00048     protected function hasPermissionToUse() {
00049         global $wgTranslateTestUsers;
00050 
00051         $request = $this->getRequest();
00052         $user = $this->getUser();
00053 
00054         if ( in_array( $user->getName(), $wgTranslateTestUsers, true ) ) {
00055             if ( $request->getVal( 'integrationtesting' ) === 'activatestash' ) {
00056                 $user->addGroup( 'translate-sandboxed' );
00057 
00058                 return true;
00059             } elseif ( $request->getVal( 'integrationtesting' ) === 'deactivatestash' ) {
00060                 $user->removeGroup( 'translate-sandboxed' );
00061                 $this->stash->deleteTranslations( $user );
00062 
00063                 return false;
00064             }
00065         }
00066 
00067         if ( !TranslateSandbox::isSandboxed( $user ) ) {
00068             return false;
00069         }
00070 
00071         return true;
00072     }
00073 
00077     protected function showPage() {
00078         $out = $this->getOutput();
00079         $user = $this->getUser();
00080 
00081         $count = count( $this->stash->getTranslations( $user ) );
00082         if ( $count === 0 ) {
00083             $progress = $this->msg( 'translate-translationstash-initialtranslation' )->parse();
00084         } else {
00085             $progress = $this->msg( 'translate-translationstash-translations' )
00086                 ->numParams( $count )->parse();
00087         }
00088 
00089         $out->addHtml( <<<HTML
00090 <div class="grid">
00091     <div class="row translate-welcome-header">
00092         <h1>
00093             {$this->msg( 'translate-translationstash-welcome', $user->getName() )->parse()}
00094         </h1>
00095         <p>
00096             {$this->msg( 'translate-translationstash-welcome-note' )->parse()}
00097         </p>
00098     </div>
00099     <div class="row translate-stash-control">
00100         <div class="six columns stash-stats">
00101             {$progress}
00102         </div>
00103         <div class="six columns ext-translate-language-selector right">
00104             {$this->tuxLanguageSelector()}
00105         </div>
00106     </div>
00107     {$this->getMessageTable()}
00108     <div class="row limit-reached hide"></div>
00109 </div>
00110 HTML
00111         );
00112     }
00113 
00114     protected function getMessageTable() {
00115         $sourceLang = $this->getSourceLanguage();
00116         $targetLang = $this->getTargetLanguage();
00117 
00118         $list = Html::element( 'div', array(
00119             'class' => 'row tux-messagelist',
00120             'data-sourcelangcode' => $sourceLang->getCode(),
00121             'data-sourcelangdir' => $sourceLang->getDir(),
00122             'data-targetlangcode' => $targetLang->getCode(),
00123             'data-targetlangdir' => $targetLang->getDir(),
00124         ) );
00125 
00126         return $list;
00127     }
00128 
00129     protected function tuxLanguageSelector() {
00130         // The name will be displayed in the UI language,
00131         // so use for lang and dir
00132         $language = $this->getTargetLanguage();
00133         $targetLangName = Language::fetchLanguageName( $language->getCode() );
00134 
00135         $label = Html::element(
00136             'span',
00137             array( 'class' => 'ext-translate-language-selector-label' ),
00138             $this->msg( 'tux-languageselector' )->text()
00139         );
00140 
00141         $trigger = Html::element(
00142             'span',
00143             array(
00144                 'class' => 'uls',
00145                 'lang' => $language->getCode(),
00146                 'dir' => $language->getDir(),
00147             ),
00148             $targetLangName
00149         );
00150 
00151         // No-break space is added for spacing after the label
00152         // and to ensure separation of words (in Arabic, for example)
00153         return "$label&#160;$trigger";
00154     }
00155 
00160     protected function getSourceLanguage() {
00161         // Bad
00162         return Language::factory( 'en' );
00163     }
00164 
00169     protected function getTargetLanguage() {
00170         $ui = $this->getLanguage();
00171         $source = $this->getSourceLanguage();
00172         if ( $ui->getCode() !== $source->getCode() ) {
00173             return $ui;
00174         }
00175 
00176         $options = FormatJson::decode( $this->getUser()->getOption( 'translate-sandbox' ), true );
00177         $supported = TranslateUtils::getLanguageNames( 'en' );
00178 
00179         if ( isset( $options['languages' ] ) ) {
00180             foreach ( $options['languages'] as $code ) {
00181                 if ( !isset( $supported[$code] ) ) {
00182                     continue;
00183                 }
00184 
00185                 if ( $code !== $source->getCode() ) {
00186                     return Language::factory( $code );
00187                 }
00188             }
00189         }
00190 
00191         // User has not chosen any valid language. Pick one at random.
00192         $codes = array_keys( $supported );
00193         return Language::factory( $codes[rand( 0, count( $codes ) - 1 )] );
00194     }
00195 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3