setHeaders(); $out = $this->getOutput(); $this->stash = new TranslationStashStorage( wfGetDB( DB_MASTER ) ); if ( !$this->hasPermissionToUse() ) { if ( $wgTranslateSecondaryPermissionUrl && $this->getUser()->isLoggedIn() ) { $out->redirect( Title::newFromText( $wgTranslateSecondaryPermissionUrl )->getLocalURL() ); return; } $out->redirect( Title::newMainPage()->getLocalURL() ); return; } $out->addJsConfigVars( 'wgTranslateSandboxLimit', $wgTranslateSandboxLimit ); $out->addModules( 'ext.translate.special.translationstash' ); $out->addModuleStyles( 'mediawiki.ui.button' ); $this->showPage(); } /** * Checks that the user is in the sandbox. Also handles special overrides * mainly used for integration testing. * * @return bool */ protected function hasPermissionToUse() { global $wgTranslateTestUsers; $request = $this->getRequest(); $user = $this->getUser(); if ( in_array( $user->getName(), $wgTranslateTestUsers, true ) ) { if ( $request->getVal( 'integrationtesting' ) === 'activatestash' ) { $user->addGroup( 'translate-sandboxed' ); return true; } elseif ( $request->getVal( 'integrationtesting' ) === 'deactivatestash' ) { $user->removeGroup( 'translate-sandboxed' ); $this->stash->deleteTranslations( $user ); return false; } } if ( !TranslateSandbox::isSandboxed( $user ) ) { return false; } return true; } /** * Generates the whole page html and appends it to output */ protected function showPage() { $out = $this->getOutput(); $user = $this->getUser(); $count = count( $this->stash->getTranslations( $user ) ); if ( $count === 0 ) { $progress = $this->msg( 'translate-translationstash-initialtranslation' )->parse(); } else { $progress = $this->msg( 'translate-translationstash-translations' ) ->numParams( $count )->parse(); } $out->addHTML( <<

{$this->msg( 'translate-translationstash-welcome', $user->getName() )->parse()}

{$this->msg( 'translate-translationstash-welcome-note' )->parse()}

{$progress}
{$this->tuxLanguageSelector()}
{$this->getMessageTable()}
HTML ); } protected function getMessageTable() { $sourceLang = $this->getSourceLanguage(); $targetLang = $this->getTargetLanguage(); $list = Html::element( 'div', [ 'class' => 'row tux-messagelist', 'data-sourcelangcode' => $sourceLang->getCode(), 'data-sourcelangdir' => $sourceLang->getDir(), 'data-targetlangcode' => $targetLang->getCode(), 'data-targetlangdir' => $targetLang->getDir(), ] ); return $list; } protected function tuxLanguageSelector() { // The name will be displayed in the UI language, // so use for lang and dir $language = $this->getTargetLanguage(); $targetLangName = Language::fetchLanguageName( $language->getCode() ); $label = Html::element( 'span', [ 'class' => 'ext-translate-language-selector-label' ], $this->msg( 'tux-languageselector' )->text() ); $trigger = Html::element( 'span', [ 'class' => 'uls', 'lang' => $language->getHtmlCode(), 'dir' => $language->getDir(), ], $targetLangName ); // No-break space is added for spacing after the label // and to ensure separation of words (in Arabic, for example) return "$label $trigger"; } /** * Returns the source language for messages. * @return Language */ protected function getSourceLanguage() { // Bad return Language::factory( 'en' ); } /** * Returns the default target language for messages. * @return Language */ protected function getTargetLanguage() { $ui = $this->getLanguage(); $source = $this->getSourceLanguage(); if ( !$ui->equals( $source ) ) { return $ui; } $options = FormatJson::decode( $this->getUser()->getOption( 'translate-sandbox' ), true ); $supported = TranslateUtils::getLanguageNames( 'en' ); if ( isset( $options['languages' ] ) ) { foreach ( $options['languages'] as $code ) { if ( !isset( $supported[$code] ) ) { continue; } if ( $code !== $source->getCode() ) { return Language::factory( $code ); } } } // User has not chosen any valid language. Pick the source. return Language::factory( $source->getCode() ); } }