jenkins-bot has submitted this change and it was merged.

Change subject: Do not use GET requests for write actions in 
Special:PageTranslation
......................................................................


Do not use GET requests for write actions in Special:PageTranslation

Uses some ugly JavaScript to keep user experience mostly the same
(loading indicators are missing). Without JavaScript users have
to click extra button.

Change-Id: Ie270a6038bb1ad15a68956f5d356d47c6a9f6274
---
M Resources.php
M i18n/pagetranslation/en.json
M i18n/pagetranslation/qqq.json
M resources/js/ext.translate.special.pagetranslation.js
M tag/SpecialPageTranslation.php
5 files changed, 124 insertions(+), 57 deletions(-)

Approvals:
  Aaron Schulz: Looks good to me, approved
  Siebrand: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/Resources.php b/Resources.php
index 6d83fd7..d84367f 100644
--- a/Resources.php
+++ b/Resources.php
@@ -400,6 +400,8 @@
        'dependencies' => array(
                'ext.translate.multiselectautocomplete',
                'mediawiki.ui.button',
+               'mediawiki.Uri',
+               'user.tokens',
        ),
        'position' => 'top',
 ) + $resourcePaths;
diff --git a/i18n/pagetranslation/en.json b/i18n/pagetranslation/en.json
index c3bd245..4538e28 100644
--- a/i18n/pagetranslation/en.json
+++ b/i18n/pagetranslation/en.json
@@ -193,5 +193,7 @@
        "pp-diff-new-header": "Prepared text",
        "tpt-unlink-confirm": "Please confirm that you really want to remove 
this page from the translation system.\nThe language selector and translated 
page names will stop working.\nThe translation pages will become editable.",
        "tpt-unlink-button": "Remove from translation",
-       "tpt-unlink-summary": "Removed page from translation"
+       "tpt-unlink-summary": "Removed page from translation",
+       "tpt-generic-confirm": "Please confirm the action.",
+       "tpt-generic-button": "Confirm"
 }
diff --git a/i18n/pagetranslation/qqq.json b/i18n/pagetranslation/qqq.json
index 7ea2a77..47aae05 100644
--- a/i18n/pagetranslation/qqq.json
+++ b/i18n/pagetranslation/qqq.json
@@ -207,5 +207,7 @@
        "pp-diff-new-header": "Header text for new revision for the diff shown 
at Special:PagePreparation",
        "tpt-unlink-confirm": "Confirmation message shown to user when 
unmarking page in active translation",
        "tpt-unlink-button": "Button text",
-       "tpt-unlink-summary": "Edit summary"
+       "tpt-unlink-summary": "Edit summary",
+       "tpt-generic-confirm": "Confirmation message shown to user on 
Special:PageTranslation. Mostly shown to users without JavaScript.",
+       "tpt-generic-button": "Butten text message shown to user on 
Special:PageTranslation. Mostly shown to users without JavaScript."
 }
diff --git a/resources/js/ext.translate.special.pagetranslation.js 
b/resources/js/ext.translate.special.pagetranslation.js
index f47de95..007d2da 100644
--- a/resources/js/ext.translate.special.pagetranslation.js
+++ b/resources/js/ext.translate.special.pagetranslation.js
@@ -1,8 +1,26 @@
-/*
+/*!
  * @author Santhosh Thottingal
+ * @author Niklas Laxström
+ * @license GPL-2.0+
  */
-jQuery( function ( $ ) {
+
+( function ( $, mw ) {
        'use strict';
 
-       $( '#wpUserLanguage' ).multiselectautocomplete( { inputbox: 
'#tpt-prioritylangs' } );
-} );
+       $( document ).ready( function () {
+               $( '#wpUserLanguage' ).multiselectautocomplete( { inputbox: 
'#tpt-prioritylangs' } );
+
+               $( '#mw-content-text' ).on( 'click', '.mw-translate-jspost', 
function ( e ) {
+                       var params,
+                               uri = new mw.Uri( e.target.href );
+
+                       params = uri.query;
+                       params.token = mw.user.tokens.get( 'csrfToken' );
+                       $.post( uri.path, params ).done( function () {
+                               location.reload();
+                       } );
+
+                       e.preventDefault();
+               } );
+       } );
+}( jQuery, mediaWiki ) );
diff --git a/tag/SpecialPageTranslation.php b/tag/SpecialPageTranslation.php
index 43a3e82..e471369 100644
--- a/tag/SpecialPageTranslation.php
+++ b/tag/SpecialPageTranslation.php
@@ -40,37 +40,59 @@
                $revision = $request->getInt( 'revision', 0 );
                $action = $request->getVal( 'do' );
                $out = $this->getOutput();
+               $out->addModules( 'ext.translate.special.pagetranslation' );
 
                TranslateUtils::addSpecialHelpLink(
                        $out,
                        'Help:Extension:Translate/Page_translation_example'
                );
 
-               // No specific page or invalid input
-               $title = Title::newFromText( $target );
-               if ( !$title ) {
-                       if ( $target !== '' ) {
-                               $out->addWikiMsg( 'tpt-badtitle' );
-                       } else {
-                               $this->listPages();
-                       }
+               if ( $target === '' ) {
+                       $this->listPages();
 
                        return;
                }
 
-               // Check permissions
+               // Anything else than listing the pages need permissions
                if ( !$user->isAllowed( 'pagetranslation' ) ) {
                        throw new PermissionsError( 'pagetranslation' );
                }
 
-               // Check permissions
+               $title = Title::newFromText( $target );
+               if ( !$title ) {
+                       $out->addWikiMsg( 'tpt-badtitle' );
+
+                       return;
+               } elseif ( !$title->exists() ) {
+                       $out->addWikiMsg( 'tpt-nosuchpage', 
$title->getPrefixedText() );
+
+                       return;
+               }
+
+               // Check token for all POST actions here
                if ( $request->wasPosted() && !$user->matchEditToken( 
$request->getText( 'token' ) ) ) {
                        throw new PermissionsError( 'pagetranslation' );
                }
 
-               // We are processing some specific page
-               if ( !$title->exists() ) {
-                       $out->addWikiMsg( 'tpt-nosuchpage', 
$title->getPrefixedText() );
+               if ( $action === 'mark' ) {
+                       // Has separate form
+                       $this->onActionMark( $title, $revision );
+
+                       return;
+               }
+
+               // On GET requests, show form which has token
+               if ( !$request->wasPosted() ) {
+                       if ( $action === 'unlink' ) {
+                               $this->showUnlinkConfirmation( $title, $target 
);
+                       } else {
+                               $params = array(
+                                       'do' => $action,
+                                       'target' => $title->getPrefixedText(),
+                                       'revision' => $revision
+                               );
+                               $this->showGenericConfirmation( $params );
+                       }
 
                        return;
                }
@@ -104,42 +126,36 @@
                }
 
                if ( $action === 'unlink' ) {
-                       if ( !$request->wasPosted() ) {
-                               $this->showUnlinkConfirmation( $title );
+                       $page = TranslatablePage::newFromTitle( $title );
+                       $content = ContentHandler::makeContent(
+                               self::getStrippedSourcePageText( 
$page->getParse() ),
+                               $title
+                       );
 
-                               return;
-                       } else {
-                               $page = TranslatablePage::newFromTitle( $title 
);
-                               $content = ContentHandler::makeContent(
-                                       self::getStrippedSourcePageText( 
$page->getParse() ),
-                                       $title
-                               );
+                       $status = WikiPage::factory( $title )->doEditContent(
+                               $content,
+                               $this->msg( 'tpt-unlink-summary' 
)->inContentLanguage()->text(),
+                               EDIT_FORCE_BOT | EDIT_UPDATE
+                       );
 
-                               $status = WikiPage::factory( $title 
)->doEditContent(
-                                       $content,
-                                       $this->msg( 'tpt-unlink-summary' 
)->inContentLanguage()->text(),
-                                       EDIT_FORCE_BOT | EDIT_UPDATE
-                               );
-
-                               if ( !$status->isOK() ) {
-                                       $out->wrapWikiMsg(
-                                               '<div 
class="errorbox">$1</div>',
-                                               array( 'tpt-edit-failed', 
$status->getWikiText() )
-                                       );
-
-                                       return;
-                               }
-
-                               $page = TranslatablePage::newFromTitle( $title 
);
-                               $this->unmarkPage( $page, $user );
+                       if ( !$status->isOK() ) {
                                $out->wrapWikiMsg(
-                                       '<div class="successbox">$1</div>',
-                                       array( 'tpt-unmarked', 
$title->getPrefixedText() )
+                                       '<div class="errorbox">$1</div>',
+                                       array( 'tpt-edit-failed', 
$status->getWikiText() )
                                );
-                               $this->listPages();
 
                                return;
                        }
+
+                       $page = TranslatablePage::newFromTitle( $title );
+                       $this->unmarkPage( $page, $user );
+                       $out->wrapWikiMsg(
+                               '<div class="successbox">$1</div>',
+                               array( 'tpt-unmarked', 
$title->getPrefixedText() )
+                       );
+                       $this->listPages();
+
+                       return;
                }
 
                if ( $action === 'unmark' ) {
@@ -153,6 +169,10 @@
 
                        return;
                }
+       }
+
+       protected function onActionMark( Title $title, $revision ) {
+               $request = $this->getRequest();
 
                if ( $revision === 0 ) {
                        // Get the latest revision
@@ -248,6 +268,32 @@
                                array( 'tpage' => 
$page->getTitle()->getArticleID() ) );
                        $this->getOutput()->addWikiMsg( 'tpt-offer-notify', 
$link );
                }
+       }
+
+       protected function showGenericConfirmation( array $params ) {
+               $formParams = array(
+                       'method' => 'post',
+                       'action' => $this->getPageTitle()->getFullURL(),
+               );
+
+               $params['title'] = $this->getPageTitle()->getPrefixedText();
+               $params['token'] = $this->getUser()->getEditToken();
+
+               $hidden = '';
+               foreach ( $params as $key => $value ) {
+                       $hidden .= Html::hidden( $key, $value );
+               }
+
+               $this->getOutput()->addHtml(
+                       Html::openElement( 'form', $formParams ) .
+                       $hidden .
+                       $this->msg( 'tpt-generic-confirm' )->parseAsBlock() .
+                       Xml::submitButton(
+                               $this->msg( 'tpt-generic-button' )->text(),
+                               array( 'class' => 'mw-ui-button mw-ui-primary' )
+                       ) .
+                       Html::closeElement( 'form' )
+               );
        }
 
        protected function showUnlinkConfirmation( Title $target ) {
@@ -450,9 +496,10 @@
                $title = $page['title'];
                $user = $this->getUser();
 
-               if ( $user->isAllowed( 'pagetranslation' ) ) {
-                       $token = $user->getEditToken();
+               // Class to allow one-click POSTs
+               $js = array( 'class' => 'mw-translate-jspost' );
 
+               if ( $user->isAllowed( 'pagetranslation' ) ) {
                        $pending = $type === 'active' && $page['latest'] !== 
$page['tp:mark'];
                        if ( $type === 'proposed' || $pending ) {
                                $actions[] = Linker::link(
@@ -463,7 +510,6 @@
                                                'do' => 'mark',
                                                'target' => 
$title->getPrefixedText(),
                                                'revision' => 
$title->getLatestRevId(),
-                                               'token' => $token,
                                        )
                                );
                        }
@@ -472,24 +518,22 @@
                                $actions[] = Linker::link(
                                        $this->getPageTitle(),
                                        $this->msg( 'tpt-rev-discourage' 
)->escaped(),
-                                       array( 'title' => $this->msg( 
'tpt-rev-discourage-tooltip' )->text() ),
+                                       array( 'title' => $this->msg( 
'tpt-rev-discourage-tooltip' )->text() ) + $js,
                                        array(
                                                'do' => 'discourage',
                                                'target' => 
$title->getPrefixedText(),
                                                'revision' => -1,
-                                               'token' => $token,
                                        )
                                );
                        } elseif ( $type === 'discouraged' ) {
                                $actions[] = Linker::link(
                                        $this->getPageTitle(),
                                        $this->msg( 'tpt-rev-encourage' 
)->escaped(),
-                                       array( 'title' => $this->msg( 
'tpt-rev-encourage-tooltip' )->text() ),
+                                       array( 'title' => $this->msg( 
'tpt-rev-encourage-tooltip' )->text() ) + $js,
                                        array(
                                                'do' => 'encourage',
                                                'target' => 
$title->getPrefixedText(),
                                                'revision' => -1,
-                                               'token' => $token,
                                        )
                                );
                        }
@@ -503,7 +547,6 @@
                                                'do' => $type === 'broken' ? 
'unmark' : 'unlink',
                                                'target' => 
$title->getPrefixedText(),
                                                'revision' => -1,
-                                               'token' => $token,
                                        )
                                );
                        }
@@ -559,7 +602,6 @@
                $out = $this->getOutput();
 
                $out->setSubtitle( Linker::link( $page->getTitle() ) );
-               $out->addModules( 'ext.translate.special.pagetranslation' );
 
                $out->addWikiMsg( 'tpt-showpage-intro' );
 
@@ -571,6 +613,7 @@
 
                $out->addHTML(
                        Xml::openElement( 'form', $formParams ) .
+                       Html::hidden( 'do', 'mark' ) .
                        Html::hidden( 'title', 
$this->getPageTitle()->getPrefixedText() ) .
                        Html::hidden( 'revision', $page->getRevision() ) .
                        Html::hidden( 'target', 
$page->getTitle()->getPrefixedtext() ) .

-- 
To view, visit https://gerrit.wikimedia.org/r/272481
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie270a6038bb1ad15a68956f5d356d47c6a9f6274
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>
Gerrit-Reviewer: Aaron Schulz <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: Nemo bis <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to