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

Change subject: mw.ViewPageTarget: Fix incorrect retention of the wrong oldid
......................................................................


mw.ViewPageTarget: Fix incorrect retention of the wrong oldid

* Only pass the oldid to the API from #load if we restoring from
  oldid in the url. Otherwise load the latest version.
* Setting 'restoring' from mw.Target instead of mw.ViewPageTarget
  so that we don't rely on mw.ViewPageTarget in mw.Target#load.
* Fix the API to not require 'oldid' to be passed.
* Fix the API to actually return the 'newrevid' property. It
  was doing a no-op on a $result that is never used due to the
  same variable being overwritten with the result of parseWikitext.
* Moved updating of wgCurRevisionId to mw.ViewPageTarget as it
  belongs there (possible future inline editors probably act
  on a different page than the main one). Also made it only
  update if it isn't undefined, so that a null edit doesn't
  result in wgCurRevisionId being unset.

Bug: 49943
Bug: 50441
Change-Id: I221e5038f95eadf6d87013e80f12394f0376a293
---
M ApiVisualEditor.php
M modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js
M modules/ve-mw/init/ve.init.mw.Target.js
3 files changed, 72 insertions(+), 40 deletions(-)

Approvals:
  Krinkle: Looks good to me, but someone else must approve
  Catrope: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/ApiVisualEditor.php b/ApiVisualEditor.php
index 183a120..85d1ed8 100644
--- a/ApiVisualEditor.php
+++ b/ApiVisualEditor.php
@@ -9,6 +9,7 @@
  */
 
 class ApiVisualEditor extends ApiBase {
+
        protected function getHTML( $title, $parserParams ) {
                global $wgVisualEditorParsoidURL, $wgVisualEditorParsoidPrefix,
                        $wgVisualEditorParsoidTimeout;
@@ -16,20 +17,23 @@
                $restoring = false;
 
                if ( $title->exists() ) {
-                       if ( $parserParams['oldid'] === 0 ) {
-                               $parserParams['oldid'] = ''; // Parsoid wants 
empty string rather than zero
-                       }
-                       $revision = Revision::newFromId( $parserParams['oldid'] 
);
                        $latestRevision = Revision::newFromTitle( $title );
-                       if ( $revision === null || $latestRevision === null ) {
+                       if ( $latestRevision === null ) {
                                return false;
                        }
-                       $restoring = !$revision->isCurrent();
+                       $revision = null;
+                       if ( !isset( $parserParams['oldid'] ) || 
$parserParams['oldid'] === 0 ) {
+                               $parserParams['oldid'] = 
$latestRevision->getId();
+                               $revision = $latestRevision;
+                       } else {
+                               $revision = Revision::newFromId( 
$parserParams['oldid'] );
+                               if ( $revision === null ) {
+                                       return false;
+                               }
+                       }
 
-                       # Disable cache busting as the Parsoid extension keeps 
templates
-                       # up to date.
-                       #$parserParams['touched'] = $title->getTouched();
-                       #$parserParams['cache'] = 1;
+                       $restoring = $revision && !$revision->isCurrent();
+                       $oldid = $parserParams['oldid'];
 
                        $req = MWHttpRequest::factory( wfAppendQuery(
                                        $wgVisualEditorParsoidURL . '/' . 
$wgVisualEditorParsoidPrefix .
@@ -65,12 +69,14 @@
                } else {
                        $content = '';
                        $timestamp = wfTimestampNow();
+                       $oldid = 0;
                }
                return array(
                        'result' => array(
                                'content' => $content,
                                'basetimestamp' => $timestamp,
                                'starttimestamp' => wfTimestampNow(),
+                               'oldid' => $oldid,
                        ),
                        'restoring' => $restoring,
                );
@@ -305,38 +311,43 @@
                                        $this->dieUsage( 'Error contacting the 
Parsoid server', 'parsoidserver' );
                                }
 
-                               $result = $this->saveWikitext( $page, 
$wikitext, $params );
-                               $editStatus = $result['edit']['result'];
+                               $saveresult = $this->saveWikitext( $page, 
$wikitext, $params );
+                               $editStatus = $saveresult['edit']['result'];
 
                                // Error
-                               if ( !isset( $result['edit']['result'] ) || 
$editStatus !== 'Success' ) {
+                               if ( !isset( $saveresult['edit']['result'] ) || 
$editStatus !== 'Success' ) {
                                        $result = array(
                                                'result' => 'error',
-                                               'edit' => $result['edit']
+                                               'edit' => $saveresult['edit']
                                        );
 
                                // Success
                                } else {
-                                       if ( isset( $result['edit']['newrevid'] 
) && $wgVisualEditorUseChangeTagging ) {
+                                       if ( isset( 
$saveresult['edit']['newrevid'] ) && $wgVisualEditorUseChangeTagging ) {
                                                ChangeTags::addTags( 
'visualeditor', null,
-                                                       intval( 
$result['edit']['newrevid'] ),
+                                                       intval( 
$saveresult['edit']['newrevid'] ),
                                                        null
                                                );
                                                if ( $params['needcheck'] ) {
                                                        ChangeTags::addTags( 
'visualeditor-needcheck', null,
-                                                               intval( 
$result['edit']['newrevid'] ),
+                                                               intval( 
$saveresult['edit']['newrevid'] ),
                                                                null
                                                        );
                                                }
                                        }
+
+                                       // Return result of parseWikitext 
instead of saveWikitext so that the
+                                       // frontend can update the page 
rendering without a refresh.
                                        $result = $this->parseWikitext( $page );
                                        if ( $result === false ) {
                                                $this->dieUsage( 'Error 
contacting the Parsoid server', 'parsoidserver' );
                                        }
-                                       $result['result'] = 'success';
-                                       if ( isset( $result['edit']['newrevid'] 
) ) {
-                                               $result['newrevid'] = intval( 
$result['edit']['newrevid'] );
+
+                                       if ( isset( 
$saveresult['edit']['newrevid'] ) ) {
+                                               $result['newrevid'] = intval( 
$saveresult['edit']['newrevid'] );
                                        }
+
+                                       $result['result'] = 'success';
                                }
                                break;
                        case 'diff':
@@ -410,8 +421,8 @@
                return array(
                        'page' => 'The page to perform actions on.',
                        'paction' => 'Action to perform',
-                       'oldid' => 'The revision number to use. If zero, the 
empty string is passed to Parsoid'
-                               .' to indicate new page creation.',
+                       'oldid' => 'The revision number to use. For 
paction=save, defauls to latest revision.' +
+                               ' Required for other actions. Use 0 for new 
page.',
                        'minor' => 'Flag for minor edit.',
                        'html' => 'HTML to send to parsoid in exchange for 
wikitext',
                        'summary' => 'Edit summary',
diff --git a/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js 
b/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js
index 81388e1..1aed07e 100644
--- a/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js
+++ b/modules/ve-mw/init/targets/ve.init.mw.ViewPageTarget.js
@@ -69,7 +69,6 @@
        this.scrollTop = null;
        this.currentUri = currentUri;
        this.messages = {};
-       this.restoring = this.oldid !== mw.config.get( 'wgCurRevisionId' );
        this.section = currentUri.query.vesection || null;
        this.namespaceName = mw.config.get( 'wgCanonicalNamespace' );
        this.viewUri = new mw.Uri( mw.util.wikiGetlink( this.pageName ) );
@@ -396,9 +395,18 @@
                                watchChecked ? 'unwatch': 'watch'
                        );
                }
+
+               // If we were explicitly editing an older version, make sure we 
won't
+               // load the same old version again, now that we've saved the 
next edit
+               // will be against the latest version.
+               // TODO: What about oldid in the url?
+               this.restoring = false;
+
                if ( newid !== undefined ) {
-                       this.oldid = newid;
+                       mw.config.set( 'wgCurRevisionId', newid );
+                       this.revid = newid;
                }
+
                this.hideSaveDialog();
                this.resetSaveDialog();
                this.replacePageContent( html );
diff --git a/modules/ve-mw/init/ve.init.mw.Target.js 
b/modules/ve-mw/init/ve.init.mw.Target.js
index 958c28a..8cc5a22 100644
--- a/modules/ve-mw/init/ve.init.mw.Target.js
+++ b/modules/ve-mw/init/ve.init.mw.Target.js
@@ -16,16 +16,18 @@
  * @constructor
  * @param {jQuery} $container Conainter to render target into
  * @param {string} pageName Name of target page
- * @param {number} [revision] Revision ID
+ * @param {number} [revisionId] If the editor should load a revision of the 
page, pass the
+ *  revision id here. Defaults to loading the latest version (see #load).
  */
-ve.init.mw.Target = function VeInitMwTarget( $container, pageName, revision ) {
+ve.init.mw.Target = function VeInitMwTarget( $container, pageName, revisionId 
) {
        // Parent constructor
        ve.init.Target.call( this, $container );
 
        // Properties
        this.pageName = pageName;
        this.pageExists = mw.config.get( 'wgArticleId', 0 ) !== 0;
-       this.oldid = revision || mw.config.get( 'wgCurRevisionId' );
+       this.revid = revisionId || mw.config.get( 'wgCurRevisionId' );
+       this.restoring = !!revisionId;
        this.editToken = mw.user.tokens.get( 'editToken' );
        this.apiUrl = mw.util.wikiScript( 'api' );
        this.submitUrl = ( new mw.Uri( mw.util.wikiGetlink( this.pageName ) ) )
@@ -164,6 +166,7 @@
 
                this.baseTimeStamp = data.basetimestamp;
                this.startTimeStamp = data.starttimestamp;
+               this.revid = data.oldid;
                // Everything worked, the page was loaded, continue as soon as 
the module is ready
                mw.loader.using( this.modules, ve.bind( 
ve.init.mw.Target.onReady, this ) );
        }
@@ -331,7 +334,6 @@
                        response
                );
        } else {
-               mw.config.set( 'wgCurRevisionId', data.newrevid );
                this.emit( 'save', data.content, data.newrevid );
        }
 };
@@ -492,23 +494,34 @@
  * @returns {boolean} Loading has been started
 */
 ve.init.mw.Target.prototype.load = function () {
+       var data;
        // Prevent duplicate requests
        if ( this.loading ) {
                return false;
        }
        // Start loading the module immediately
        mw.loader.load( this.modules );
+
+       data = {
+               'action': 'visualeditor',
+               'paction': 'parse',
+               'page': this.pageName,
+               'token': this.editToken,
+               'format': 'json'
+       };
+
+       // Only request the API to explicitly load the currently visible 
revision if we're restoring
+       // from oldid. Otherwise we should load the latest version. This 
prevents us from editing an
+       // old version if an edit was made while the user was viewing the page 
and/or the user is
+       // seeing (slightly) stale cache.
+       if ( this.restoring ) {
+               data.oldid = this.revid;
+       }
+
        // Load DOM
        this.loading = $.ajax( {
                'url': this.apiUrl,
-               'data': {
-                       'action': 'visualeditor',
-                       'paction': 'parse',
-                       'page': this.pageName,
-                       'oldid': this.oldid,
-                       'token': this.editToken,
-                       'format': 'json'
-               },
+               'data': data,
                'dataType': 'json',
                'type': 'POST',
                // Wait up to 100 seconds before giving up
@@ -564,7 +577,7 @@
                'action': 'visualeditor',
                'paction': 'save',
                'page': this.pageName,
-               'oldid': this.oldid,
+               'oldid': this.revid,
                'basetimestamp': this.baseTimeStamp,
                'starttimestamp': this.startTimeStamp,
                'html': this.getHtml( doc ),
@@ -617,7 +630,7 @@
                        'action': 'visualeditor',
                        'paction': 'diff',
                        'page': this.pageName,
-                       'oldid': this.oldid,
+                       'oldid': this.revid,
                        'html': this.getHtml( doc ),
                        // TODO: API required editToken, though not relevant 
for diff
                        'token': this.editToken
@@ -657,7 +670,7 @@
                $form = $( '<form method="post" 
enctype="multipart/form-data"></form>' ),
                params = {
                        'format': 'text/x-wiki',
-                       'oldid': this.oldid,
+                       'oldid': this.revid,
                        'wpStarttime': this.baseTimeStamp,
                        'wpEdittime': this.startTimeStamp,
                        'wpTextbox1': wikitext,
@@ -708,7 +721,7 @@
                        'paction': 'serialize',
                        'html': this.getHtml( doc ),
                        'page': this.pageName,
-                       'oldid': this.oldid,
+                       'oldid': this.revid,
                        'token': this.editToken,
                        'format': 'json'
                },

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I221e5038f95eadf6d87013e80f12394f0376a293
Gerrit-PatchSet: 12
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Helder.wiki <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to