Factor out getWikitext in ApiVisualEditorEdit

Change-Id: I0f6709f170034263acc94daa41603bcc77897d31
This commit is contained in:
Ed Sanders 2016-10-31 17:45:49 +00:00
parent 76b3f63ead
commit dcbbaa79db

View file

@ -127,6 +127,29 @@ class ApiVisualEditorEdit extends ApiVisualEditor {
return $content; return $content;
} }
protected function getWikitext( $title, $params, $parserParams ) {
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->getWikitextNoCache( $title, $params, $parserParams );
}
return $wikitext;
}
protected function getWikitextNoCache( $title, $params, $parserParams ) {
$this->requireOnlyOneParameter( $params, 'html' );
$wikitext = $this->postHTML(
$title, $this->tryDeflate( $params['html'] ), $parserParams, $params['etag']
);
if ( $wikitext === false ) {
$this->dieUsage( 'Error contacting the document server', 'docserver' );
}
return $wikitext;
}
protected function storeInSerializationCache( $title, $wikitext ) { protected function storeInSerializationCache( $title, $wikitext ) {
global $wgMemc; global $wgMemc;
@ -160,25 +183,27 @@ class ApiVisualEditorEdit extends ApiVisualEditor {
return $wgMemc->get( $key ); return $wgMemc->get( $key );
} }
protected function postHTML( $title, $html, $parserParams, $etag ) { protected function postData( $path, $title, $data, $parserParams, $etag ) {
if ( $parserParams['oldid'] === 0 ) { if ( $parserParams['oldid'] === 0 ) {
$parserParams['oldid'] = ''; $parserParams['oldid'] = '';
} }
$path = 'transform/html/to/wikitext/' . urlencode( $title->getPrefixedDBkey() ); $path .= urlencode( $title->getPrefixedDBkey() );
if ( $parserParams['oldid'] ) { if ( $parserParams['oldid'] ) {
$path .= '/' . $parserParams['oldid']; $path .= '/' . $parserParams['oldid'];
} }
return $this->requestRestbase( return $this->requestRestbase(
'POST', 'POST', $path, $data,
$path,
[
'html' => $html,
'scrub_wikitext' => 1,
],
[ 'If-Match' => $etag ] [ 'If-Match' => $etag ]
); );
} }
protected function postHTML( $title, $html, $parserParams, $etag ) {
return $this->postData(
'transform/html/to/wikitext/', $title,
[ 'html' => $html, 'scrub_wikitext' => 1 ], $parserParams, $etag
);
}
protected function diffWikitext( $title, $wikitext, $section = null ) { protected function diffWikitext( $title, $wikitext, $section = null ) {
$apiParams = [ $apiParams = [
'action' => 'query', 'action' => 'query',
@ -236,30 +261,9 @@ class ApiVisualEditorEdit extends ApiVisualEditor {
$parserParams['oldid'] = $params['oldid']; $parserParams['oldid'] = $params['oldid'];
} }
if ( $wikitext = $params['wikitext'];
$params['paction'] !== 'serialize' && if ( !$wikitext ) {
$params['paction'] !== 'serializeforcache' && $wikitext = $this->getWikitext( $title, $params, $parserParams );
$params['wikitext'] !== null
) {
// Get wikitext from param
$wikitext = $params['wikitext'];
} elseif ( $params['paction'] !== 'serializeforcache' && $params['cachekey'] !== null ) {
// Get wikitext from cachekey
$wikitext = $this->trySerializationCache( $params['cachekey'] );
if ( !is_string( $wikitext ) ) {
$this->dieUsage( 'No cached serialization found with that key', 'badcachekey' );
}
} else {
// Get wikitext by parsing HTML
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 Parsoid/RESTbase server', 'docserver' );
}
} }
if ( $params['paction'] === 'serialize' ) { if ( $params['paction'] === 'serialize' ) {