Catrope has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/70106


Change subject: [WIP] Do a sanity check after loading the document
......................................................................

[WIP] Do a sanity check after loading the document

The sanity check takes the linear model, converts it to DOM,
sends it to Parsoid to serialize it to wikitext, then compares it to
the original wikitext. If they differ, the sanity check fails.

TODO: Actually do something with the results of the check in the UI

Bug: 50067
Change-Id: I04f71fe8e00c6257fbc953cc9de3323e24709b0f
---
M ApiVisualEditor.php
M modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js
M modules/ve/init/mw/ve.init.mw.Target.js
3 files changed, 78 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/06/70106/1

diff --git a/ApiVisualEditor.php b/ApiVisualEditor.php
index 8f2aab4..a8407c4 100644
--- a/ApiVisualEditor.php
+++ b/ApiVisualEditor.php
@@ -214,6 +214,18 @@
                }
        }
 
+       protected function getWikitext( $oldid ) {
+               $revision = Revision::newFromId( $oldid );
+               if ( !$revision ) {
+                       return false;
+               }
+               $content = $revision->getContent();
+               if ( !$content ) {
+                       return false;
+               }
+               return $content->getNativeData();
+       }
+
        public function execute() {
                global $wgVisualEditorNamespaces, 
$wgVisualEditorUseChangeTagging,
                        $wgVisualEditorEditNotices;
@@ -283,6 +295,7 @@
                                break;
                        case 'save':
                        case 'diff':
+                       case 'sanitycheck':
                                $wikitext = $this->postHTML( $page, 
$params['html'], $parserParams );
 
                                if ( $wikitext === false ) {
@@ -320,6 +333,13 @@
                                                $this->dieUsage( 'Diff failed', 
'difffailed' );
                                        }
                                        $result = $diff;
+                               } elseif ( $params['paction'] === 'sanitycheck' 
) {
+                                       $expectedWikitext = $this->getWikitext( 
$params['oldid'] );
+                                       if ( $wikitext !== false && $wikitext 
=== $expectedWikitext ) {
+                                               $result = array( 'result' => 
'success' );
+                                       } else {
+                                               $result = array( 'result' => 
'fail', 'actual' => $wikitext, 'expected' => $expectedWikitext );
+                                       }
                                }
                                break;
                }
@@ -334,7 +354,7 @@
                        ),
                        'paction' => array(
                                ApiBase::PARAM_REQUIRED => true,
-                               ApiBase::PARAM_TYPE => array( 'parse', 
'parsefragment', 'serialize', 'save', 'diff' ),
+                               ApiBase::PARAM_TYPE => array( 'parse', 
'parsefragment', 'serialize', 'save', 'diff', 'sanitycheck' ),
                        ),
                        'token' => array(
                                ApiBase::PARAM_REQUIRED => true,
diff --git a/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js 
b/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js
index e6cf949..622b2e2 100644
--- a/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js
+++ b/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js
@@ -345,6 +345,7 @@
                this.edited = false;
                this.doc = doc;
                this.setUpSurface( doc );
+               this.startSanityCheck();
                this.setupToolbarEditNotices();
                this.setupToolbarBetaNotice();
                this.setupToolbarButtons();
@@ -855,6 +856,22 @@
 };
 
 /**
+ * Fire off the sanity check. Must be called before the surface is active.
+ *
+ * To access the result, check whether this.sanityCheckPromise has been 
resolved or rejected
+ * (it's asynchronous, so it may still be pending when you check).
+ */
+ve.init.mw.ViewPageTarget.prototype.startSanityCheck = function () {
+       var doc = this.surface.getModel().getDocument();
+       this.sanityCheckPromise = this.sanityCheck(
+               ve.dm.converter.getDomFromData( doc.getFullData(), 
doc.getStore(), doc.getInternalList() )
+       );
+       // For testing
+       this.sanityCheckPromise.done( function () { console.log( 'Sanity check 
success :)' ); } )
+               .fail( function () { console.log( 'Sanity check failed :(' ); } 
);
+};
+
+/**
  * The toolbar has updated its position.
  * @param {jQuery} $bar
  */
diff --git a/modules/ve/init/mw/ve.init.mw.Target.js 
b/modules/ve/init/mw/ve.init.mw.Target.js
index e6ad49e..5ac362d 100644
--- a/modules/ve/init/mw/ve.init.mw.Target.js
+++ b/modules/ve/init/mw/ve.init.mw.Target.js
@@ -703,3 +703,43 @@
        } );
        return true;
 };
+
+/**
+ * Run a sanity check, asserting that the current document has an empty 
wikitext diff with
+ * the revision it was based on.
+ *
+ * This should be called before the user is able to interact with the surface.
+ *
+ * @param {HTMLDocument} doc Document to sanity-check
+ * @returns {jQuery.Promise} Promise that will be resolved if the sanity check 
succeeds and rejected
+ *  if it fails.
+ */
+ve.init.mw.Target.prototype.sanityCheck = function ( doc ) {
+       var dfd = $.Deferred();
+       $.ajax( {
+               'url': this.apiUrl,
+               'data': {
+                       'action': 'visualeditor',
+                       'paction': 'sanitycheck',
+                       'html': this.getHtml( doc ),
+                       'page': this.pageName,
+                       'oldid': this.oldid,
+                       'token': this.editToken,
+                       'format': 'json'
+               },
+               'dataType': 'json',
+               'type': 'POST',
+               // Wait up to 100 seconds before giving up
+               'timeout': 100000,
+               'cache': 'false',
+               'success': function ( data ) {
+                       if ( data.visualeditor && data.visualeditor.result === 
'success' ) {
+                               dfd.resolve();
+                       } else {
+                               dfd.reject();
+                       }
+               },
+               'error': function () { dfd.reject(); }
+       } );
+       return dfd.promise();
+};
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I04f71fe8e00c6257fbc953cc9de3323e24709b0f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Catrope <roan.katt...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to