diff options
Diffstat (limited to 'MLEB/Translate/TranslateEditAddons.php')
-rw-r--r-- | MLEB/Translate/TranslateEditAddons.php | 225 |
1 files changed, 150 insertions, 75 deletions
diff --git a/MLEB/Translate/TranslateEditAddons.php b/MLEB/Translate/TranslateEditAddons.php index e26a306b..d128ee6d 100644 --- a/MLEB/Translate/TranslateEditAddons.php +++ b/MLEB/Translate/TranslateEditAddons.php @@ -6,7 +6,7 @@ * @file * @author Niklas Laxström * @author Siebrand Mazeland - * @license GPL-2.0+ + * @license GPL-2.0-or-later */ /** @@ -16,32 +16,66 @@ */ class TranslateEditAddons { /** - * Keep the usual diiba daaba hidden from translators. + * Do not show the usual introductory messages on edit page for messages. * Hook: AlternateEdit + * @param EditPage $editPage */ - public static function intro( EditPage $editpage ) { - $handle = new MessageHandle( $editpage->getTitle() ); + public static function suppressIntro( EditPage $editPage ) { + $handle = new MessageHandle( $editPage->getTitle() ); if ( $handle->isValid() ) { - $editpage->suppressIntro = true; - $group = $handle->getGroup(); - $languages = $group->getTranslatableLanguages(); - if ( $handle->getCode() && $languages !== null && !isset( $languages[$handle->getCode()] ) ) { - $editpage->getArticle()->getContext()->getOutput()->wrapWikiMsg( - "<div class='error'>$1</div>", 'translate-language-disabled' - ); + $editPage->suppressIntro = true; + } + } - return false; - } + /** + * Prevent translations to non-translatable languages for the group + * Hook: getUserPermissionsErrorsExpensive + * + * @param Title $title + * @param User $user + * @param string $action + * @param mixed &$result + * @return bool + */ + public static function disallowLangTranslations( Title $title, User $user, + $action, &$result + ) { + global $wgTranslateBlacklist; + if ( $action !== 'edit' ) { return true; } - $msg = wfMessage( 'translate-edit-tag-warning' )->inContentLanguage(); - if ( !$msg->isDisabled() && - TranslatablePage::isSourcePage( $editpage->getTitle() ) - ) { - $editpage->editFormTextTop .= $editpage->getArticle()->getContext() - ->getOutput()->parse( $msg->plain() ); + $handle = new MessageHandle( $title ); + if ( !$handle->isValid() ) { + return true; + } + + if ( $user->isAllowed( 'translate-manage' ) ) { + return true; + } + + $group = $handle->getGroup(); + $languages = $group->getTranslatableLanguages(); + $langCode = $handle->getCode(); + if ( $languages !== null && $langCode && !isset( $languages[$langCode] ) ) { + $result = [ 'translate-language-disabled' ]; + return false; + } + + $groupId = $group->getId(); + $checks = [ + $groupId, + strtok( $groupId, '-' ), + '*' + ]; + + foreach ( $checks as $check ) { + if ( isset( $wgTranslateBlacklist[$check][$langCode] ) ) { + $reason = $wgTranslateBlacklist[$check][$langCode]; + $result = [ 'translate-page-disabled', $reason ]; + return false; + } } return true; @@ -50,8 +84,10 @@ class TranslateEditAddons { /** * Adds the translation aids and navigation to the normal edit page. * Hook: EditPage::showEditForm:initial + * @param EditPage $object + * @return true */ - static function addTools( EditPage $object ) { + public static function addTools( EditPage $object ) { $handle = new MessageHandle( $object->getTitle() ); if ( !$handle->isValid() ) { return true; @@ -66,11 +102,15 @@ class TranslateEditAddons { * Replace the normal save button with one that says if you are editing * message documentation to try to avoid accidents. * Hook: EditPageBeforeEditButtons + * + * @param EditPage $editpage + * @param array &$buttons + * @param int $tabindex */ - static function buttonHack( EditPage $editpage, &$buttons, $tabindex ) { + public static function buttonHack( EditPage $editpage, &$buttons, $tabindex ) { $handle = new MessageHandle( $editpage->getTitle() ); if ( !$handle->isValid() ) { - return true; + return; } $context = $editpage->getArticle()->getContext(); @@ -78,47 +118,43 @@ class TranslateEditAddons { if ( $handle->isDoc() ) { $langCode = $context->getLanguage()->getCode(); $name = TranslateUtils::getLanguageName( $handle->getCode(), $langCode ); - $accessKey = $context->msg( 'accesskey-save' )->plain(); - $temp = array( + $attribs = [ 'id' => 'wpSave', 'name' => 'wpSave', - 'type' => 'submit', 'tabindex' => ++$tabindex, - 'value' => $context->msg( 'translate-save', $name )->text(), - 'accesskey' => $accessKey, - 'title' => $context->msg( 'tooltip-save' )->text() . ' [' . $accessKey . ']', - ); - $buttons['save'] = Xml::element( 'input', $temp, '' ); - } - - global $wgTranslateSupportUrl; - if ( !$wgTranslateSupportUrl ) { - return true; - } - - $supportTitle = Title::newFromText( $wgTranslateSupportUrl['page'] ); - if ( !$supportTitle ) { - return true; + ] + Linker::tooltipAndAccesskeyAttribs( 'save' ); + + $saveConfig = OOUI\Element::configFromHtmlAttributes( $attribs ); + $buttons['save'] = new OOUI\ButtonInputWidget( [ + // Support: IE 6 – Use <input>, otherwise it can't distinguish which button was clicked + 'useInputTag' => true, + 'flags' => [ 'progressive', 'primary' ], + 'label' => $context->msg( 'translate-save', $name )->text(), + 'type' => 'submit', + ] + $saveConfig ); } - $supportParams = $wgTranslateSupportUrl['params']; - foreach ( $supportParams as &$value ) { - $value = str_replace( '%MESSAGE%', $handle->getTitle()->getPrefixedText(), $value ); + try { + $supportUrl = SupportAid::getSupportUrl( $handle->getTitle() ); + } catch ( TranslationHelperException $e ) { + return; } - $temp = array( + $attribs = [ 'id' => 'wpSupport', 'name' => 'wpSupport', 'type' => 'button', 'tabindex' => ++$tabindex, - 'value' => $context->msg( 'translate-js-support' )->text(), 'title' => $context->msg( 'translate-js-support-title' )->text(), - 'data-load-url' => $supportTitle->getLocalUrl( $supportParams ), - 'onclick' => "window.open( jQuery(this).attr('data-load-url') );", - ); - $buttons['ask'] = Html::element( 'input', $temp, '' ); - - return true; + ]; + + $attribs += [ + 'label' => $context->msg( 'translate-js-support' )->text(), + 'href' => $supportUrl, + 'target' => '_blank', + ]; + $saveConfig = OOUI\Element::configFromHtmlAttributes( $attribs ); + $buttons['ask'] = new OOUI\ButtonWidget( $saveConfig ); } /** @@ -149,18 +185,28 @@ class TranslateEditAddons { /** * Runs message checks, adds tp:transver tags and updates statistics. * Hook: PageContentSaveComplete + * @param WikiPage $wikiPage + * @param User $user + * @param Content $content + * @param string $summary + * @param bool $minor + * @param string $_1 + * @param bool $_2 + * @param int $flags + * @param Revision $revision + * @return true */ public static function onSave( WikiPage $wikiPage, $user, $content, $summary, - $minor, $_, $_, $flags, $revision + $minor, $_1, $_2, $flags, $revision ) { + global $wgEnablePageTranslation; - if ( $content instanceof TextContent ) { - $text = $content->getNativeData(); - } else { + if ( !$content instanceof TextContent ) { // Screw it, not interested return true; } + $text = $content->getNativeData(); $title = $wikiPage->getTitle(); $handle = new MessageHandle( $title ); @@ -170,21 +216,38 @@ class TranslateEditAddons { // Update it. if ( $revision === null ) { - $rev = $wikiPage->getTitle()->getLatestRevId(); + $rev = $wikiPage->getTitle()->getLatestRevID(); } else { $rev = $revision->getID(); } $fuzzy = self::checkNeedsFuzzy( $handle, $text ); self::updateFuzzyTag( $title, $rev, $fuzzy ); - wfRunHooks( 'TranslateEventTranslationEdit', array( $handle ) ); + + $group = $handle->getGroup(); + // Update translation stats - source language should always be up to date + if ( $handle->getCode() !== $group->getSourceLanguage() ) { + MessageGroupStats::forItem( + $group->getId(), + $handle->getCode(), + MessageGroupStats::FLAG_NO_CACHE + ); + } + + MessageGroupStatesUpdaterJob::onChange( $handle ); if ( $fuzzy === false ) { - wfRunHooks( 'Translate:newTranslation', array( $handle, $rev, $text, $user ) ); + Hooks::run( 'Translate:newTranslation', [ $handle, $rev, $text, $user ] ); } TTMServer::onChange( $handle, $text, $fuzzy ); + if ( $wgEnablePageTranslation && $handle->isPageTranslation() ) { + // Updates for translatable pages only + PageTranslationHooks::onSectionSave( $wikiPage, $user, $content, + $summary, $minor, $flags, $revision, $handle ); + } + return true; } @@ -227,24 +290,27 @@ class TranslateEditAddons { /** * @param Title $title * @param int $revision - * @param bool $fuzzy + * @param bool $fuzzy Whether to fuzzy or not + * @return bool Whether status changed */ protected static function updateFuzzyTag( Title $title, $revision, $fuzzy ) { $dbw = wfGetDB( DB_MASTER ); - $conds = array( + $conds = [ 'rt_page' => $title->getArticleID(), 'rt_type' => RevTag::getType( 'fuzzy' ), 'rt_revision' => $revision - ); + ]; // Replace the existing fuzzy tag, if any if ( $fuzzy !== false ) { $index = array_keys( $conds ); - $dbw->replace( 'revtag', array( $index ), $conds, __METHOD__ ); + $dbw->replace( 'revtag', [ $index ], $conds, __METHOD__ ); } else { $dbw->delete( 'revtag', $conds, __METHOD__ ); } + + return (bool)$dbw->affectedRows(); } /** @@ -278,14 +344,14 @@ class TranslateEditAddons { $dbw = wfGetDB( DB_MASTER ); - $conds = array( + $conds = [ 'rt_page' => $title->getArticleID(), 'rt_type' => RevTag::getType( 'tp:transver' ), 'rt_revision' => $revision, 'rt_value' => $definitionRevision, - ); - $index = array( 'rt_type', 'rt_page', 'rt_revision' ); - $dbw->replace( 'revtag', array( $index ), $conds, __METHOD__ ); + ]; + $index = [ 'rt_type', 'rt_page', 'rt_revision' ]; + $dbw->replace( 'revtag', [ $index ], $conds, __METHOD__ ); return true; } @@ -296,7 +362,7 @@ class TranslateEditAddons { * @param ParserOptions $popts * @return bool */ - public static function disablePreSaveTransform( $wikiPage, ParserOptions $popts ) { + public static function disablePreSaveTransform( WikiPage $wikiPage, ParserOptions $popts ) { global $wgTranslateUsePreSaveTransform; if ( !$wgTranslateUsePreSaveTransform ) { @@ -311,6 +377,9 @@ class TranslateEditAddons { /** * Hook: ArticleContentOnDiff + * @param DifferenceEngine $de + * @param OutputPage $out + * @return true */ public static function displayOnDiff( DifferenceEngine $de, OutputPage $out ) { $title = $de->getTitle(); @@ -324,26 +393,32 @@ class TranslateEditAddons { $th->setEditMode( false ); $de->loadNewText(); - if ( $de->mNewContent instanceof TextContent ) { - $th->setTranslation( $de->mNewContent->getNativeData() ); + if ( method_exists( $de, 'getNewRevision' ) ) { + $newRevision = $de->getNewRevision(); + $newContent = $newRevision ? $newRevision->getContent( 'main' ) : null; + } else { + $newContent = $de->mNewRev ? $de->mNewRev->getContent() : null; + } + if ( $newContent instanceof TextContent ) { + $th->setTranslation( $newContent->getNativeData() ); } else { // Screw you, not interested. return true; } TranslationHelpers::addModules( $out ); - $boxes = array(); - $boxes[] = $th->callBox( 'documentation', array( $th, 'getDocumentationBox' ) ); - $boxes[] = $th->callBox( 'definition', array( $th, 'getDefinitionBox' ) ); - $boxes[] = $th->callBox( 'translation', array( $th, 'getTranslationDisplayBox' ) ); + $boxes = []; + $boxes[] = $th->callBox( 'documentation', [ $th, 'getDocumentationBox' ] ); + $boxes[] = $th->callBox( 'definition', [ $th, 'getDefinitionBox' ] ); + $boxes[] = $th->callBox( 'translation', [ $th, 'getTranslationDisplayBox' ] ); $output = implode( "\n", $boxes ); $output = Html::rawElement( 'div', - array( 'class' => 'mw-sp-translate-edit-fields' ), + [ 'class' => 'mw-sp-translate-edit-fields' ], $output ); - $out->addHtml( $output ); + $out->addHTML( $output ); return true; } |