diff options
Diffstat (limited to 'www/wiki/extensions/Translate/tag/SpecialPageTranslation.php')
-rw-r--r-- | www/wiki/extensions/Translate/tag/SpecialPageTranslation.php | 205 |
1 files changed, 109 insertions, 96 deletions
diff --git a/www/wiki/extensions/Translate/tag/SpecialPageTranslation.php b/www/wiki/extensions/Translate/tag/SpecialPageTranslation.php index a0c1896d..377d6c91 100644 --- a/www/wiki/extensions/Translate/tag/SpecialPageTranslation.php +++ b/www/wiki/extensions/Translate/tag/SpecialPageTranslation.php @@ -5,7 +5,7 @@ * @file * @author Niklas Laxström * @author Siebrand Mazeland - * @license GPL-2.0+ + * @license GPL-2.0-or-later */ /** @@ -82,11 +82,11 @@ class SpecialPageTranslation extends SpecialPage { if ( $action === 'unlink' ) { $this->showUnlinkConfirmation( $title, $target ); } else { - $params = array( + $params = [ 'do' => $action, 'target' => $title->getPrefixedText(), 'revision' => $revision - ); + ]; $this->showGenericConfirmation( $params ); } @@ -137,7 +137,7 @@ class SpecialPageTranslation extends SpecialPage { if ( !$status->isOK() ) { $out->wrapWikiMsg( '<div class="errorbox">$1</div>', - array( 'tpt-edit-failed', $status->getWikiText() ) + [ 'tpt-edit-failed', $status->getWikiText() ] ); return; @@ -147,7 +147,7 @@ class SpecialPageTranslation extends SpecialPage { $this->unmarkPage( $page, $user ); $out->wrapWikiMsg( '<div class="successbox">$1</div>', - array( 'tpt-unmarked', $title->getPrefixedText() ) + [ 'tpt-unmarked', $title->getPrefixedText() ] ); $this->listPages(); @@ -159,7 +159,7 @@ class SpecialPageTranslation extends SpecialPage { $this->unmarkPage( $page, $user ); $out->wrapWikiMsg( '<div class="successbox">$1</div>', - array( 'tpt-unmarked', $title->getPrefixedText() ) + [ 'tpt-unmarked', $title->getPrefixedText() ] ); $this->listPages(); @@ -171,6 +171,8 @@ class SpecialPageTranslation extends SpecialPage { $request = $this->getRequest(); $out = $this->getOutput(); + $out->addModuleStyles( 'ext.translate.special.pagetranslation.styles' ); + if ( $revision === 0 ) { // Get the latest revision $revision = (int)$title->getLatestRevID(); @@ -180,7 +182,7 @@ class SpecialPageTranslation extends SpecialPage { if ( !$page instanceof TranslatablePage ) { $out->wrapWikiMsg( '<div class="errorbox">$1</div>', - array( 'tpt-notsuitable', $title->getPrefixedText(), $revision ) + [ 'tpt-notsuitable', $title->getPrefixedText(), $revision ] ); return; @@ -188,22 +190,23 @@ class SpecialPageTranslation extends SpecialPage { if ( $revision !== (int)$title->getLatestRevID() ) { // We do want to notify the reviewer if the underlying page changes during review - $target = $title->getFullURL( array( 'oldid' => $revision ) ); + $target = $title->getFullURL( [ 'oldid' => $revision ] ); $link = "<span class='plainlinks'>[$target $revision]</span>"; $out->wrapWikiMsg( '<div class="warningbox">$1</div>', - array( 'tpt-oldrevision', $title->getPrefixedText(), $link ) + [ 'tpt-oldrevision', $title->getPrefixedText(), $link ] ); $this->listPages(); return; } - $lastrev = $page->getMarkedTag(); - if ( $lastrev !== false && $lastrev === $revision ) { + $lastRev = $page->getMarkedTag(); + $firstMark = $lastRev === false; + if ( !$firstMark && $lastRev === $revision ) { $out->wrapWikiMsg( '<div class="warningbox">$1</div>', - array( 'tpt-already-marked' ) + [ 'tpt-already-marked' ] ); $this->listPages(); @@ -219,7 +222,7 @@ class SpecialPageTranslation extends SpecialPage { // Check if user wants to translate title // If not, remove it from the list of sections if ( !$request->getCheck( 'translatetitle' ) ) { - $sections = array_filter( $sections, function( $s ) { + $sections = array_filter( $sections, function ( $s ) { return $s->id !== 'Page display title'; } ); } @@ -227,9 +230,9 @@ class SpecialPageTranslation extends SpecialPage { $err = $this->markForTranslation( $page, $sections ); if ( $err ) { - call_user_func_array( array( $out, 'addWikiMsg' ), $err ); + call_user_func_array( [ $out, 'addWikiMsg' ], $err ); } else { - $this->showSuccess( $page ); + $this->showSuccess( $page, $firstMark ); $this->listPages(); } @@ -240,38 +243,46 @@ class SpecialPageTranslation extends SpecialPage { } /** + * Displays success message and other instructions after a page has been marked for translation. * @param TranslatablePage $page + * @param bool $firstMark true if it is the first time the page is being marked for translation. */ - public function showSuccess( TranslatablePage $page ) { + public function showSuccess( TranslatablePage $page, $firstMark = false ) { $titleText = $page->getTitle()->getPrefixedText(); $num = $this->getLanguage()->formatNum( $page->getParse()->countSections() ); - $link = SpecialPage::getTitleFor( 'Translate' )->getFullURL( array( + $link = SpecialPage::getTitleFor( 'Translate' )->getFullURL( [ 'group' => $page->getMessageGroupId(), 'action' => 'page', 'filter' => '', - ) ); + ] ); $this->getOutput()->wrapWikiMsg( '<div class="successbox">$1</div>', - array( 'tpt-saveok', $titleText, $num, $link ) + [ 'tpt-saveok', $titleText, $num, $link ] ); + // If the page is being marked for translation for the first time + // add a link to Special:PageMigration. + if ( $firstMark ) { + $this->getOutput()->addWikiMsg( 'tpt-saveok-first' ); + } + // If TranslationNotifications is installed, and the user can notify // translators, add a convenience link. if ( method_exists( 'SpecialNotifyTranslators', 'execute' ) && $this->getUser()->isAllowed( SpecialNotifyTranslators::$right ) ) { $link = SpecialPage::getTitleFor( 'NotifyTranslators' )->getFullURL( - array( 'tpage' => $page->getTitle()->getArticleID() ) ); + [ 'tpage' => $page->getTitle()->getArticleID() ] ); $this->getOutput()->addWikiMsg( 'tpt-offer-notify', $link ); } } protected function showGenericConfirmation( array $params ) { - $formParams = array( + $formParams = [ 'method' => 'post', 'action' => $this->getPageTitle()->getFullURL(), - ); + ]; $params['title'] = $this->getPageTitle()->getPrefixedText(); $params['token'] = $this->getUser()->getEditToken(); @@ -287,17 +298,17 @@ class SpecialPageTranslation extends SpecialPage { $this->msg( 'tpt-generic-confirm' )->parseAsBlock() . Xml::submitButton( $this->msg( 'tpt-generic-button' )->text(), - array( 'class' => 'mw-ui-button mw-ui-primary' ) + [ 'class' => 'mw-ui-button mw-ui-progressive' ] ) . Html::closeElement( 'form' ) ); } protected function showUnlinkConfirmation( Title $target ) { - $formParams = array( + $formParams = [ 'method' => 'post', 'action' => $this->getPageTitle()->getFullURL(), - ); + ]; $this->getOutput()->addHTML( Html::openElement( 'form', $formParams ) . @@ -308,7 +319,7 @@ class SpecialPageTranslation extends SpecialPage { $this->msg( 'tpt-unlink-confirm', $target->getPrefixedText() )->parseAsBlock() . Xml::submitButton( $this->msg( 'tpt-unlink-button' )->text(), - array( 'class' => 'mw-ui-button mw-ui-destructive' ) + [ 'class' => 'mw-ui-button mw-ui-destructive' ] ) . Html::closeElement( 'form' ) ); @@ -327,34 +338,34 @@ class SpecialPageTranslation extends SpecialPage { public function loadPagesFromDB() { $dbr = TranslateUtils::getSafeReadDB(); - $tables = array( 'page', 'revtag' ); - $vars = array( + $tables = [ 'page', 'revtag' ]; + $vars = [ 'page_id', 'page_title', 'page_namespace', 'page_latest', 'MAX(rt_revision) AS rt_revision', 'rt_type' - ); - $conds = array( + ]; + $conds = [ 'page_id=rt_page', - 'rt_type' => array( RevTag::getType( 'tp:mark' ), RevTag::getType( 'tp:tag' ) ), - ); - $options = array( + 'rt_type' => [ RevTag::getType( 'tp:mark' ), RevTag::getType( 'tp:tag' ) ], + ]; + $options = [ 'ORDER BY' => 'page_namespace, page_title', 'GROUP BY' => 'page_id, rt_type', - ); + ]; $res = $dbr->select( $tables, $vars, $conds, __METHOD__, $options ); return $res; } protected function buildPageArray( /*db result*/$res ) { - $pages = array(); + $pages = []; foreach ( $res as $r ) { // We have multiple rows for same page, because of different tags if ( !isset( $pages[$r->page_id] ) ) { - $pages[$r->page_id] = array(); + $pages[$r->page_id] = []; $title = Title::newFromRow( $r ); $pages[$r->page_id]['title'] = $title; $pages[$r->page_id]['latest'] = (int)$title->getLatestRevID(); @@ -372,12 +383,12 @@ class SpecialPageTranslation extends SpecialPage { * @return array */ protected function classifyPages( array $in ) { - $out = array( - 'proposed' => array(), - 'active' => array(), - 'broken' => array(), - 'discouraged' => array(), - ); + $out = [ + 'proposed' => [], + 'active' => [], + 'broken' => [], + 'discouraged' => [], + ]; foreach ( $in as $index => $page ) { if ( !isset( $page['tp:mark'] ) ) { @@ -411,13 +422,21 @@ class SpecialPageTranslation extends SpecialPage { $out = $this->getOutput(); $res = $this->loadPagesFromDB(); - $allpages = $this->buildPageArray( $res ); - if ( !count( $allpages ) ) { + $allPages = $this->buildPageArray( $res ); + if ( !count( $allPages ) ) { $out->addWikiMsg( 'tpt-list-nopages' ); return; } - $types = $this->classifyPages( $allpages ); + + $lb = new LinkBatch(); + $lb->setCaller( __METHOD__ ); + foreach ( $allPages as $page ) { + $lb->addObj( $page['title'] ); + } + $lb->execute(); + + $types = $this->classifyPages( $allPages ); $pages = $types['proposed']; if ( count( $pages ) ) { @@ -486,7 +505,7 @@ class SpecialPageTranslation extends SpecialPage { * @return string */ protected function actionLinks( array $page, $type ) { - $actions = array(); + $actions = []; /** * @var Title $title */ @@ -494,57 +513,57 @@ class SpecialPageTranslation extends SpecialPage { $user = $this->getUser(); // Class to allow one-click POSTs - $js = array( 'class' => 'mw-translate-jspost' ); + $js = [ 'class' => 'mw-translate-jspost' ]; if ( $user->isAllowed( 'pagetranslation' ) ) { $pending = $type === 'active' && $page['latest'] !== $page['tp:mark']; if ( $type === 'proposed' || $pending ) { - $actions[] = Linker::link( + $actions[] = Linker::linkKnown( $this->getPageTitle(), $this->msg( 'tpt-rev-mark' )->escaped(), - array( 'title' => $this->msg( 'tpt-rev-mark-tooltip' )->text() ), - array( + [ 'title' => $this->msg( 'tpt-rev-mark-tooltip' )->text() ], + [ 'do' => 'mark', 'target' => $title->getPrefixedText(), 'revision' => $title->getLatestRevID(), - ) + ] ); } if ( $type === 'active' ) { - $actions[] = Linker::link( + $actions[] = Linker::linkKnown( $this->getPageTitle(), $this->msg( 'tpt-rev-discourage' )->escaped(), - array( 'title' => $this->msg( 'tpt-rev-discourage-tooltip' )->text() ) + $js, - array( + [ 'title' => $this->msg( 'tpt-rev-discourage-tooltip' )->text() ] + $js, + [ 'do' => 'discourage', 'target' => $title->getPrefixedText(), 'revision' => -1, - ) + ] ); } elseif ( $type === 'discouraged' ) { - $actions[] = Linker::link( + $actions[] = Linker::linkKnown( $this->getPageTitle(), $this->msg( 'tpt-rev-encourage' )->escaped(), - array( 'title' => $this->msg( 'tpt-rev-encourage-tooltip' )->text() ) + $js, - array( + [ 'title' => $this->msg( 'tpt-rev-encourage-tooltip' )->text() ] + $js, + [ 'do' => 'encourage', 'target' => $title->getPrefixedText(), 'revision' => -1, - ) + ] ); } if ( $type !== 'proposed' ) { - $actions[] = Linker::link( + $actions[] = Linker::linkKnown( $this->getPageTitle(), $this->msg( 'tpt-rev-unmark' )->escaped(), - array( 'title' => $this->msg( 'tpt-rev-unmark-tooltip' )->text() ), - array( + [ 'title' => $this->msg( 'tpt-rev-unmark-tooltip' )->text() ], + [ 'do' => $type === 'broken' ? 'unmark' : 'unlink', 'target' => $title->getPrefixedText(), 'revision' => -1, - ) + ] ); } } @@ -557,18 +576,18 @@ class SpecialPageTranslation extends SpecialPage { return Html::rawElement( 'span', - array( 'class' => 'mw-tpt-actions' ), + [ 'class' => 'mw-tpt-actions' ], $this->msg( 'parentheses' )->rawParams( $flattened )->escaped() ); } /** * @param TranslatablePage $page - * @param bool $error + * @param bool &$error * @return TPSection[] The array has string keys. */ public function checkInput( TranslatablePage $page, &$error ) { - $usedNames = array(); + $usedNames = []; $highest = (int)TranslateMetadata::get( $page->getMessageGroupId(), 'maxid' ); $parse = $page->getParse(); $sections = $parse->getSectionsForSave( $highest ); @@ -594,19 +613,17 @@ class SpecialPageTranslation extends SpecialPage { * @param TPSection[] $sections */ public function showPage( TranslatablePage $page, array $sections ) { - global $wgContLang; - $out = $this->getOutput(); $out->setSubtitle( Linker::link( $page->getTitle() ) ); $out->addWikiMsg( 'tpt-showpage-intro' ); - $formParams = array( + $formParams = [ 'method' => 'post', 'action' => $this->getPageTitle()->getFullURL(), 'class' => 'mw-tpt-sp-markform', - ); + ]; $out->addHTML( Xml::openElement( 'form', $formParams ) . @@ -627,6 +644,8 @@ class SpecialPageTranslation extends SpecialPage { // If the page is marked for translation the first time, default to checked. $defaultChecked = $page->hasPageDisplayTitle(); + $sourceLanguage = Language::factory( $page->getSourceLanguageCode() ); + foreach ( $sections as $s ) { if ( $s->name === 'Page display title' ) { // Set section type as new if title previously unchecked @@ -651,9 +670,7 @@ class SpecialPageTranslation extends SpecialPage { if ( $s->type === 'changed' ) { $hasChanges = true; $diff = new DifferenceEngine; - if ( method_exists( 'DifferenceEngine', 'setTextLanguage' ) ) { - $diff->setTextLanguage( $wgContLang ); - } + $diff->setTextLanguage( $sourceLanguage ); $diff->setReducedLineNumbers(); $oldContent = ContentHandler::makeContent( $s->getOldText(), $diff->getTitle() ); @@ -678,11 +695,12 @@ class SpecialPageTranslation extends SpecialPage { } # For changed text, the language is set by $diff->setTextLanguage() - $lang = $s->type === 'changed' ? null : $wgContLang; + $lang = $s->type === 'changed' ? null : $sourceLanguage; $out->addHTML( MessageWebImporter::makeSectionElement( $name, $s->type, - $text, $lang + $text, + $lang ) ); } @@ -701,7 +719,7 @@ class SpecialPageTranslation extends SpecialPage { $name, $s->type, $text, - $wgContLang + $sourceLanguage ) ); } } @@ -720,9 +738,7 @@ class SpecialPageTranslation extends SpecialPage { $out->wrapWikiMsg( '==$1==', 'tpt-sections-template' ); $diff = new DifferenceEngine; - if ( method_exists( 'DifferenceEngine', 'setTextLanguage' ) ) { - $diff->setTextLanguage( $wgContLang ); - } + $diff->setTextLanguage( $sourceLanguage ); $oldContent = ContentHandler::makeContent( $oldTemplate, $diff->getTitle() ); $newContent = ContentHandler::makeContent( $newTemplate, $diff->getTitle() ); @@ -736,7 +752,7 @@ class SpecialPageTranslation extends SpecialPage { $diff->showDiffStyle(); $diff->setReducedLineNumbers(); - $contentParams = array( 'class' => 'mw-tpt-sp-content' ); + $contentParams = [ 'class' => 'mw-tpt-sp-content' ]; $out->addHTML( Xml::tags( 'div', $contentParams, $text ) ); } } @@ -832,7 +848,7 @@ class SpecialPageTranslation extends SpecialPage { ); if ( !$status->isOK() ) { - return array( 'tpt-edit-failed', $status->getWikiText() ); + return [ 'tpt-edit-failed', $status->getWikiText() ]; } $newrevision = $status->value['revision']; @@ -849,8 +865,8 @@ class SpecialPageTranslation extends SpecialPage { $newrevision = $page->getTitle()->getLatestRevID(); } - $inserts = array(); - $changed = array(); + $inserts = []; + $changed = []; $maxid = (int)TranslateMetadata::get( $page->getMessageGroupId(), 'maxid' ); $pageId = $page->getTitle()->getArticleID(); @@ -862,22 +878,22 @@ class SpecialPageTranslation extends SpecialPage { $changed[] = $s->name; if ( $this->getRequest()->getCheck( "tpt-sect-{$s->id}-action-nofuzzy" ) ) { - // This will be checked by TranslationsUpdateJob::getTranslationUnitJobs() + // TranslationsUpdateJob will only fuzzy when type is changed $s->type = 'old'; } - $inserts[] = array( + $inserts[] = [ 'trs_page' => $pageId, 'trs_key' => $s->name, 'trs_text' => $s->getText(), 'trs_order' => $index - ); + ]; } $dbw = wfGetDB( DB_MASTER ); $dbw->delete( 'translate_sections', - array( 'trs_page' => $page->getTitle()->getArticleID() ), + [ 'trs_page' => $page->getTitle()->getArticleID() ], __METHOD__ ); $dbw->insert( 'translate_sections', $inserts, __METHOD__ ); @@ -886,10 +902,7 @@ class SpecialPageTranslation extends SpecialPage { $page->addMarkedTag( $newrevision ); MessageGroups::singleton()->recache(); - $job = new TranslationsUpdateJob( - $page->getTitle(), - array( 'sections' => $sections ) - ); + $job = TranslationsUpdateJob::newFromPage( $page, $sections ); JobQueueGroup::singleton()->push( $job ); // Logging @@ -898,10 +911,10 @@ class SpecialPageTranslation extends SpecialPage { $entry = new ManualLogEntry( 'pagetranslation', 'mark' ); $entry->setPerformer( $this->getUser() ); $entry->setTarget( $page->getTitle() ); - $entry->setParameters( array( + $entry->setParameters( [ 'revision' => $newrevision, 'changed' => count( $changed ), - ) ); + ] ); $logid = $entry->insert(); $entry->publish( $logid ); @@ -949,11 +962,11 @@ class SpecialPageTranslation extends SpecialPage { TranslateMetadata::set( $groupId, 'priorityreason', $npReason ); if ( $opLangs !== $npLangs || $opForce !== $npForce || $opReason !== $npReason ) { - $params = array( + $params = [ 'languages' => $npLangs, 'force' => $npForce, 'reason' => $npReason, - ); + ]; $entry = new ManualLogEntry( 'pagetranslation', 'prioritylanguages' ); $entry->setPerformer( $this->getUser() ); @@ -973,7 +986,7 @@ class SpecialPageTranslation extends SpecialPage { * @since 2014.09 */ public static function getStrippedSourcePageText( TPParse $parse ) { - $text = $parse->getTranslationPageText( array() ); + $text = $parse->getTranslationPageText( [] ); $text = preg_replace( '~<languages\s*/>\n?~s', '', $text ); return $text; } |