jenkins-bot has submitted this change and it was merged. Change subject: Factor out API deflation and change storeInSerializationCache API ......................................................................
Factor out API deflation and change storeInSerializationCache API Change-Id: I7e2e15a8ba5088e6c370cb035c71f7af18c13b61 --- M ApiVisualEditor.php M ApiVisualEditorEdit.php 2 files changed, 39 insertions(+), 30 deletions(-) Approvals: Alex Monk: Looks good to me, approved jenkins-bot: Verified diff --git a/ApiVisualEditor.php b/ApiVisualEditor.php index 896b87b..97a8d6f 100644 --- a/ApiVisualEditor.php +++ b/ApiVisualEditor.php @@ -123,24 +123,22 @@ return $response['body']; } - protected function storeInSerializationCache( $title, $oldid, $html, $etag ) { + protected function storeInSerializationCache( $title, $wikitext ) { global $wgMemc; - // Convert the VE HTML to wikitext - $text = $this->postHTML( $title, $html, [ 'oldid' => $oldid ], $etag ); - if ( $text === false ) { + if ( $wikitext === false ) { return false; } // Store the corresponding wikitext, referenceable by a new key - $hash = md5( $text ); + $hash = md5( $wikitext ); $key = wfMemcKey( 'visualeditor', 'serialization', $hash ); - $wgMemc->set( $key, $text, + $wgMemc->set( $key, $wikitext, $this->veConfig->get( 'VisualEditorSerializationCacheTimeout' ) ); // Also parse and prepare the edit in case it might be saved later $page = WikiPage::factory( $title ); - $content = ContentHandler::makeContent( $text, $title, CONTENT_MODEL_WIKITEXT ); + $content = ContentHandler::makeContent( $wikitext, $title, CONTENT_MODEL_WIKITEXT ); $status = ApiStashEdit::parseAndStash( $page, $content, $this->getUser(), '' ); if ( $status === ApiStashEdit::ERROR_NONE ) { @@ -167,7 +165,7 @@ $path .= '/' . $parserParams['oldid']; } if ( !is_string( $etag ) || $etag === '' ) { - wfDebugLog( 'AdHocDebug', 'VisualEditr T135171 - bad etag: ' . var_export( $etag, true ) ); + wfDebugLog( 'AdHocDebug', 'VisualEditor T135171 - bad etag: ' . var_export( $etag, true ) ); } return $this->requestRestbase( 'POST', @@ -285,6 +283,20 @@ return $langlinks; } + protected function tryDeflate( $content ) { + if ( substr( $content, 0, 11 ) === 'rawdeflate,' ) { + $deflated = base64_decode( substr( $content, 11 ) ); + wfSuppressWarnings(); + $inflated = gzinflate( $deflated ); + wfRestoreWarnings(); + if ( $deflated === $inflated || $inflated === false ) { + $this->dieUsage( "Content provided is not properly deflated", 'invaliddeflate' ); + } + return $inflated; + } + return $content; + } + public function execute() { $user = $this->getUser(); $params = $this->extractRequestParams(); @@ -303,17 +315,6 @@ $parserParams = []; if ( isset( $params['oldid'] ) ) { $parserParams['oldid'] = $params['oldid']; - } - - $html = $params['html']; - if ( substr( $html, 0, 11 ) === 'rawdeflate,' ) { - $deflated = base64_decode( substr( $html, 11 ) ); - wfSuppressWarnings(); - $html = gzinflate( $deflated ); - wfRestoreWarnings(); - if ( $deflated === $html || $html === false ) { - $this->dieUsage( "HTML provided is not properly deflated", 'invaliddeflate' ); - } } wfDebugLog( 'visualeditor', "called on '$title' with paction: '{$params['paction']}'" ); @@ -582,7 +583,9 @@ if ( $params['html'] === null ) { $this->dieUsageMsg( 'missingparam', 'html' ); } - $content = $this->postHTML( $title, $html, $parserParams, $params['etag'] ); + $content = $this->postHTML( + $title, $this->tryDeflate( $params['html'] ), $parserParams, $params['etag'] + ); if ( $content === false ) { $this->dieUsage( 'Error contacting the document server', 'docserver' ); } @@ -597,7 +600,12 @@ $this->dieUsage( 'No cached serialization found with that key', 'badcachekey' ); } } else { - $wikitext = $this->postHTML( $title, $html, $parserParams, $params['etag'] ); + if ( $params['html'] === null ) { + $this->dieUsageMsg( 'missingparam', 'html' ); + } + $wikitext = $this->postHTML( + $title, $this->tryDeflate( $params['html'] ), $parserParams, $params['etag'] + ); if ( $wikitext === false ) { $this->dieUsage( 'Error contacting the document server', 'docserver' ); } @@ -615,11 +623,15 @@ if ( !isset( $parserParams['oldid'] ) ) { $parserParams['oldid'] = Revision::newFromTitle( $title )->getId(); } + if ( $params['html'] === null ) { + $this->dieUsageMsg( 'missingparam', 'html' ); + } + $wikitext = $this->postHTML( + $title, $this->tryDeflate( $params['html'] ), $parserParams, $params['etag'] + ); $key = $this->storeInSerializationCache( $title, - $parserParams['oldid'], - $html, - $params['etag'] + $wikitext ); $result = [ 'result' => 'success', 'cachekey' => $key ]; break; diff --git a/ApiVisualEditorEdit.php b/ApiVisualEditorEdit.php index 32e2b1f..720fc44 100644 --- a/ApiVisualEditorEdit.php +++ b/ApiVisualEditorEdit.php @@ -134,18 +134,15 @@ $parserParams['oldid'] = $params['oldid']; } - $html = $params['html']; - if ( substr( $html, 0, 11 ) === 'rawdeflate,' ) { - $html = gzinflate( base64_decode( substr( $html, 11 ) ) ); - } - if ( $params['cachekey'] !== null ) { $wikitext = $this->trySerializationCache( $params['cachekey'] ); if ( !is_string( $wikitext ) ) { $this->dieUsage( 'No cached serialization found with that key', 'badcachekey' ); } } else { - $wikitext = $this->postHTML( $title, $html, $parserParams, $params['etag'] ); + $wikitext = $this->postHTML( + $title, $this->tryDeflate( $params['html'] ), $parserParams, $params['etag'] + ); if ( $wikitext === false ) { $this->dieUsage( 'Error contacting the Parsoid/RESTbase server', 'docserver' ); } -- To view, visit https://gerrit.wikimedia.org/r/303932 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I7e2e15a8ba5088e6c370cb035c71f7af18c13b61 Gerrit-PatchSet: 4 Gerrit-Project: mediawiki/extensions/VisualEditor Gerrit-Branch: master Gerrit-Owner: Esanders <esand...@wikimedia.org> Gerrit-Reviewer: Alex Monk <a...@wikimedia.org> Gerrit-Reviewer: Catrope <roan.katt...@gmail.com> Gerrit-Reviewer: Esanders <esand...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits