2012-05-25 19:50:48 +00:00
|
|
|
<?php
|
2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* Parsoid API wrapper.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
2013-02-19 23:37:34 +00:00
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
2012-07-19 00:11:26 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
class ApiVisualEditor extends ApiBase {
|
2013-07-05 07:56:28 +00:00
|
|
|
|
2012-11-14 18:33:57 +00:00
|
|
|
protected function getHTML( $title, $parserParams ) {
|
2012-11-15 01:16:13 +00:00
|
|
|
global $wgVisualEditorParsoidURL, $wgVisualEditorParsoidPrefix,
|
2013-10-22 00:24:27 +00:00
|
|
|
$wgVisualEditorParsoidTimeout, $wgVisualEditorParsoidForwardCookies;
|
2013-05-24 14:48:27 +00:00
|
|
|
|
|
|
|
$restoring = false;
|
|
|
|
|
2012-11-28 23:57:00 +00:00
|
|
|
if ( $title->exists() ) {
|
2013-06-18 19:17:44 +00:00
|
|
|
$latestRevision = Revision::newFromTitle( $title );
|
2013-07-05 07:56:28 +00:00
|
|
|
if ( $latestRevision === null ) {
|
2012-11-28 23:57:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-05 07:56:28 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2013-05-24 14:48:27 +00:00
|
|
|
|
2013-07-05 07:56:28 +00:00
|
|
|
$restoring = $revision && !$revision->isCurrent();
|
|
|
|
$oldid = $parserParams['oldid'];
|
2013-03-05 17:24:51 +00:00
|
|
|
|
|
|
|
$req = MWHttpRequest::factory( wfAppendQuery(
|
2012-11-28 23:57:00 +00:00
|
|
|
$wgVisualEditorParsoidURL . '/' . $wgVisualEditorParsoidPrefix .
|
|
|
|
'/' . urlencode( $title->getPrefixedDBkey() ),
|
|
|
|
$parserParams
|
|
|
|
),
|
2013-03-05 17:24:51 +00:00
|
|
|
array(
|
|
|
|
'method' => 'GET',
|
|
|
|
'timeout' => $wgVisualEditorParsoidTimeout
|
|
|
|
)
|
2012-11-28 23:57:00 +00:00
|
|
|
);
|
2013-10-22 00:24:27 +00:00
|
|
|
// Forward cookies, but only if configured to do so and if there are read restrictions
|
|
|
|
if ( $wgVisualEditorParsoidForwardCookies && !User::isEveryoneAllowed( 'read' ) ) {
|
|
|
|
$req->setHeader( 'Cookie', $this->getRequest()->getHeader( 'Cookie' ) );
|
|
|
|
}
|
2013-03-05 17:24:51 +00:00
|
|
|
$status = $req->execute();
|
|
|
|
|
|
|
|
if ( $status->isOK() ) {
|
|
|
|
$content = $req->getContent();
|
2013-10-04 10:13:58 +00:00
|
|
|
// Pass thru performance data from Parsoid to the client, unless the response was
|
|
|
|
// served directly from Varnish, in which case discard the value of the XPP header
|
|
|
|
// and use it to declare the cache hit instead.
|
|
|
|
$xCache = $req->getResponseHeader( 'X-Cache' );
|
|
|
|
if ( is_string( $xCache ) && strpos( $xCache, 'hit' ) !== false ) {
|
2013-10-06 23:26:19 +00:00
|
|
|
$xpp = 'cached-response=true';
|
2013-10-04 10:13:58 +00:00
|
|
|
} else {
|
|
|
|
$xpp = $req->getResponseHeader( 'X-Parsoid-Performance' );
|
|
|
|
}
|
|
|
|
if ( $xpp !== null ) {
|
|
|
|
$resp = $this->getRequest()->response();
|
|
|
|
$resp->header( 'X-Parsoid-Performance: ' . $xpp );
|
|
|
|
}
|
2013-05-29 14:49:16 +00:00
|
|
|
} elseif ( $status->isGood() ) {
|
2013-03-05 17:24:51 +00:00
|
|
|
$this->dieUsage( $req->getContent(), 'parsoidserver-http-'.$req->getStatus() );
|
2013-05-29 14:49:16 +00:00
|
|
|
} elseif ( $errors = $status->getErrorsByType( 'error' ) ) {
|
2013-03-09 18:28:23 +00:00
|
|
|
$error = $errors[0];
|
|
|
|
$code = $error['message'];
|
2013-05-29 14:49:16 +00:00
|
|
|
if ( count( $error['params'] ) ) {
|
2013-03-09 18:28:23 +00:00
|
|
|
$message = $error['params'][0];
|
|
|
|
} else {
|
|
|
|
$message = 'MWHttpRequest error';
|
|
|
|
}
|
|
|
|
$this->dieUsage( $message, 'parsoidserver-'.$code );
|
2013-03-05 17:24:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 21:04:19 +00:00
|
|
|
if ( $content === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-18 19:17:44 +00:00
|
|
|
$timestamp = $latestRevision->getTimestamp();
|
2012-11-28 23:57:00 +00:00
|
|
|
} else {
|
|
|
|
$content = '';
|
|
|
|
$timestamp = wfTimestampNow();
|
2013-07-05 07:56:28 +00:00
|
|
|
$oldid = 0;
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
2012-11-28 23:57:00 +00:00
|
|
|
return array(
|
2013-05-24 14:48:27 +00:00
|
|
|
'result' => array(
|
|
|
|
'content' => $content,
|
|
|
|
'basetimestamp' => $timestamp,
|
|
|
|
'starttimestamp' => wfTimestampNow(),
|
2013-07-05 07:56:28 +00:00
|
|
|
'oldid' => $oldid,
|
2013-05-24 14:48:27 +00:00
|
|
|
),
|
|
|
|
'restoring' => $restoring,
|
2012-11-14 18:33:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-11-01 21:30:22 +00:00
|
|
|
protected function storeInSerializationCache( $title, $oldid, $html ) {
|
|
|
|
global $wgMemc, $wgVisualEditorSerializationCacheTimeout;
|
|
|
|
$content = $this->postHTML( $title, $html, array( 'oldid' => $oldid ) );
|
|
|
|
if ( $content === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$hash = md5( $content );
|
|
|
|
$key = wfMemcKey( 'visualeditor', 'serialization', $hash );
|
|
|
|
$wgMemc->set( $key, $content, $wgVisualEditorSerializationCacheTimeout );
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function trySerializationCache( $hash ) {
|
|
|
|
global $wgMemc;
|
|
|
|
$key = wfMemcKey( 'visualeditor', 'serialization', $hash );
|
|
|
|
return $wgMemc->get( $key );
|
|
|
|
}
|
|
|
|
|
2013-04-15 23:22:51 +00:00
|
|
|
protected function postHTML( $title, $html, $parserParams ) {
|
2012-11-27 01:39:44 +00:00
|
|
|
global $wgVisualEditorParsoidURL, $wgVisualEditorParsoidPrefix,
|
2013-10-22 00:24:27 +00:00
|
|
|
$wgVisualEditorParsoidTimeout, $wgVisualEditorParsoidForwardCookies;
|
2013-04-15 23:22:51 +00:00
|
|
|
if ( $parserParams['oldid'] === 0 ) {
|
|
|
|
$parserParams['oldid'] = '';
|
|
|
|
}
|
2013-10-22 00:24:27 +00:00
|
|
|
$req = MWHttpRequest::factory(
|
2012-11-27 01:39:44 +00:00
|
|
|
$wgVisualEditorParsoidURL . '/' . $wgVisualEditorParsoidPrefix .
|
|
|
|
'/' . urlencode( $title->getPrefixedDBkey() ),
|
2012-11-15 01:16:13 +00:00
|
|
|
array(
|
2013-10-22 00:24:27 +00:00
|
|
|
'method' => 'POST',
|
2013-05-25 10:00:06 +00:00
|
|
|
'postData' => array(
|
2013-04-19 22:13:57 +00:00
|
|
|
'content' => $html,
|
2013-05-25 10:00:06 +00:00
|
|
|
'oldid' => $parserParams['oldid']
|
2013-04-19 22:13:57 +00:00
|
|
|
),
|
|
|
|
'timeout' => $wgVisualEditorParsoidTimeout
|
2012-11-15 01:16:13 +00:00
|
|
|
)
|
2012-11-14 18:33:57 +00:00
|
|
|
);
|
2013-10-22 00:24:27 +00:00
|
|
|
// Forward cookies, but only if configured to do so and if there are read restrictions
|
|
|
|
if ( $wgVisualEditorParsoidForwardCookies && !User::isEveryoneAllowed( 'read' ) ) {
|
|
|
|
$req->setHeader( 'Cookie', $this->getRequest()->getHeader( 'Cookie' ) );
|
|
|
|
}
|
|
|
|
$status = $req->execute();
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
// TODO proper error handling, merge with getHTML above
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// TODO pass through X-Parsoid-Performance header, merge with getHTML above
|
|
|
|
return $req->getContent();
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseWikitext( $title ) {
|
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'parse',
|
|
|
|
'page' => $title->getPrefixedDBkey()
|
|
|
|
);
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
|
|
|
false // was posted?
|
|
|
|
),
|
|
|
|
true // enable write?
|
|
|
|
);
|
|
|
|
|
|
|
|
$api->execute();
|
|
|
|
$result = $api->getResultData();
|
2012-11-28 23:57:00 +00:00
|
|
|
$content = isset( $result['parse']['text']['*'] ) ? $result['parse']['text']['*'] : false;
|
|
|
|
$revision = Revision::newFromId( $result['parse']['revid'] );
|
|
|
|
$timestamp = $revision ? $revision->getTimestamp() : wfTimestampNow();
|
|
|
|
|
|
|
|
if ( $content === false || ( strlen( $content ) && $revision === null ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'content' => $content,
|
|
|
|
'basetimestamp' => $timestamp,
|
|
|
|
'starttimestamp' => wfTimestampNow()
|
|
|
|
);
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
|
|
|
|
2013-05-15 23:51:11 +00:00
|
|
|
protected function parseWikitextFragment( $wikitext, $title = null ) {
|
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'parse',
|
|
|
|
'title' => $title,
|
|
|
|
'prop' => 'text',
|
|
|
|
'disablepp' => true,
|
|
|
|
'text' => $wikitext
|
|
|
|
);
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
|
|
|
false // was posted?
|
|
|
|
),
|
|
|
|
true // enable write?
|
|
|
|
);
|
|
|
|
|
|
|
|
$api->execute();
|
|
|
|
$result = $api->getResultData();
|
|
|
|
return isset( $result['parse']['text']['*'] ) ? $result['parse']['text']['*'] : false;
|
|
|
|
}
|
|
|
|
|
2012-11-14 18:33:57 +00:00
|
|
|
protected function diffWikitext( $title, $wikitext ) {
|
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'query',
|
|
|
|
'prop' => 'revisions',
|
|
|
|
'titles' => $title->getPrefixedDBkey(),
|
|
|
|
'rvdifftotext' => $wikitext
|
|
|
|
);
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
|
|
|
false // was posted?
|
|
|
|
),
|
|
|
|
false // enable write?
|
|
|
|
);
|
|
|
|
$api->execute();
|
|
|
|
$result = $api->getResultData();
|
2012-12-07 16:23:23 +00:00
|
|
|
if ( !isset( $result['query']['pages'][$title->getArticleID()]['revisions'][0]['diff']['*'] ) ) {
|
2013-05-14 17:40:00 +00:00
|
|
|
return array( 'result' => 'fail' );
|
2012-12-07 16:23:23 +00:00
|
|
|
}
|
|
|
|
$diffRows = $result['query']['pages'][$title->getArticleID()]['revisions'][0]['diff']['*'];
|
|
|
|
|
2013-05-10 22:32:23 +00:00
|
|
|
if ( $diffRows !== '' ) {
|
|
|
|
$context = new DerivativeContext( $this->getContext() );
|
|
|
|
$context->setTitle( $title );
|
|
|
|
$engine = new DifferenceEngine( $context );
|
2013-05-14 17:40:00 +00:00
|
|
|
return array(
|
|
|
|
'result' => 'success',
|
|
|
|
'diff' => $engine->addHeader(
|
|
|
|
$diffRows,
|
|
|
|
wfMessage( 'currentrev' )->parse(),
|
|
|
|
wfMessage( 'yourtext' )->parse()
|
|
|
|
)
|
2013-05-10 22:32:23 +00:00
|
|
|
);
|
|
|
|
} else {
|
2013-05-14 17:40:00 +00:00
|
|
|
return array( 'result' => 'nochanges' );
|
2013-05-10 22:32:23 +00:00
|
|
|
}
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
2012-05-25 22:23:40 +00:00
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
public function execute() {
|
2013-07-12 16:37:22 +00:00
|
|
|
global $wgVisualEditorNamespaces, $wgVisualEditorEditNotices;
|
|
|
|
|
2012-07-21 17:37:20 +00:00
|
|
|
$user = $this->getUser();
|
2012-05-25 19:50:48 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$page = Title::newFromText( $params['page'] );
|
2012-11-14 18:33:57 +00:00
|
|
|
if ( !$page ) {
|
|
|
|
$this->dieUsageMsg( 'invalidtitle', $params['page'] );
|
|
|
|
}
|
|
|
|
if ( !in_array( $page->getNamespace(), $wgVisualEditorNamespaces ) ) {
|
|
|
|
$this->dieUsage( "VisualEditor is not enabled in namespace " .
|
|
|
|
$page->getNamespace(), 'novenamespace' );
|
|
|
|
}
|
2012-05-25 19:50:48 +00:00
|
|
|
|
2013-05-15 23:51:11 +00:00
|
|
|
$parserParams = array();
|
|
|
|
if ( isset( $params['oldid'] ) ) {
|
|
|
|
$parserParams['oldid'] = $params['oldid'];
|
|
|
|
}
|
2012-08-23 19:01:07 +00:00
|
|
|
|
2013-05-15 21:17:06 +00:00
|
|
|
switch ( $params['paction'] ) {
|
|
|
|
case 'parse':
|
|
|
|
$parsed = $this->getHTML( $page, $parserParams );
|
|
|
|
// Dirty hack to provide the correct context for edit notices
|
|
|
|
global $wgTitle; // FIXME NOOOOOOOOES
|
|
|
|
$wgTitle = $page;
|
|
|
|
$notices = $page->getEditNotices();
|
|
|
|
if ( $user->isAnon() ) {
|
|
|
|
$wgVisualEditorEditNotices[] = 'anoneditwarning';
|
2012-12-11 03:40:09 +00:00
|
|
|
}
|
2013-05-24 14:48:27 +00:00
|
|
|
if ( $parsed && $parsed['restoring'] ) {
|
|
|
|
$wgVisualEditorEditNotices[] = 'editingold';
|
|
|
|
}
|
2013-07-10 21:24:02 +00:00
|
|
|
// Page protected from editing
|
2013-07-10 20:23:15 +00:00
|
|
|
if ( $page->getNamespace() != NS_MEDIAWIKI && $page->isProtected( 'edit' ) ) {
|
|
|
|
# Is the title semi-protected?
|
|
|
|
if ( $page->isSemiProtected() ) {
|
|
|
|
$wgVisualEditorEditNotices[] = 'semiprotectedpagewarning';
|
|
|
|
} else {
|
|
|
|
# Then it must be protected based on static groups (regular)
|
|
|
|
$wgVisualEditorEditNotices[] = 'protectedpagewarning';
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 09:43:21 +00:00
|
|
|
// Creating new page
|
|
|
|
if ( !$page->exists() ) {
|
|
|
|
$wgVisualEditorEditNotices[] = $user->isLoggedIn() ? 'newarticletext' : 'newarticletextanon';
|
|
|
|
// Page protected from creation
|
|
|
|
if ( $page->getRestrictions( 'create' ) ) {
|
|
|
|
$wgVisualEditorEditNotices[] = 'titleprotectedwarning';
|
|
|
|
}
|
2013-07-10 21:24:02 +00:00
|
|
|
}
|
2013-05-15 21:17:06 +00:00
|
|
|
if ( count( $wgVisualEditorEditNotices ) ) {
|
|
|
|
foreach ( $wgVisualEditorEditNotices as $key ) {
|
|
|
|
$notices[] = wfMessage( $key )->parseAsBlock();
|
|
|
|
}
|
|
|
|
}
|
Render check boxes from EditPage
EditPage has a lovely getCheckboxes() function which includes the
minor and watch checkboxes as rendered by MW core, as well as any
checkboxes extensions like FlaggedRevs might have added. Output
these in the API, render them, and send their values back.
ApiVisualEditor.php:
* Build a fake EditPage, get its checkboxes, and return them
ApiVisualEditorEdit.php:
* Pass through posted request data to ApiEdit, which passes it
through to EditPage thanks to Idab5b524b0e3 in core
ve.init.mw.ViewPageTarget.js:
* Remove minor and watch checkboxes from the save dialog template
and replace them with a generic checkbox container
* Have getSaveOptions() pull the state of all checkboxes in
** Special-case minor and watch, and pass the rest straight through
** Move normalization from true/false to presence/absence here, from
ve.init.mw.Target.prototype.save(), because here we know which ones
are checkboxes and we don't know that in save() without
special-casing
* Remove getSaveDialogHtml(), we don't need to hide checkboxes based on
rights anymore because in that case the API just won't send them to us.
** Moved logic for checking the watch checkbox down to where the same
logic for the minor checkbox already is
* Unwrap getSaveDialogHtml() in setupSaveDialog()
* Access minor and watch by their new IDs throughout
ve.init.mw.Target.js:
* Get and store checkboxes from the API
* Pass all keys straight through to the API
Bug: 49699
Change-Id: I09d02a42b05146bc9b7080ab38338ae869bf15e3
2013-07-24 06:39:03 +00:00
|
|
|
|
|
|
|
// HACK: Build a fake EditPage so we can get checkboxes from it
|
|
|
|
$article = new Article( $page ); // Deliberately omitting ,0 so oldid comes from request
|
|
|
|
$ep = new EditPage( $article );
|
2013-08-02 18:37:30 +00:00
|
|
|
$req = $this->getRequest();
|
|
|
|
$ep->importFormData( $req ); // By reference for some reason (bug 52466)
|
Render check boxes from EditPage
EditPage has a lovely getCheckboxes() function which includes the
minor and watch checkboxes as rendered by MW core, as well as any
checkboxes extensions like FlaggedRevs might have added. Output
these in the API, render them, and send their values back.
ApiVisualEditor.php:
* Build a fake EditPage, get its checkboxes, and return them
ApiVisualEditorEdit.php:
* Pass through posted request data to ApiEdit, which passes it
through to EditPage thanks to Idab5b524b0e3 in core
ve.init.mw.ViewPageTarget.js:
* Remove minor and watch checkboxes from the save dialog template
and replace them with a generic checkbox container
* Have getSaveOptions() pull the state of all checkboxes in
** Special-case minor and watch, and pass the rest straight through
** Move normalization from true/false to presence/absence here, from
ve.init.mw.Target.prototype.save(), because here we know which ones
are checkboxes and we don't know that in save() without
special-casing
* Remove getSaveDialogHtml(), we don't need to hide checkboxes based on
rights anymore because in that case the API just won't send them to us.
** Moved logic for checking the watch checkbox down to where the same
logic for the minor checkbox already is
* Unwrap getSaveDialogHtml() in setupSaveDialog()
* Access minor and watch by their new IDs throughout
ve.init.mw.Target.js:
* Get and store checkboxes from the API
* Pass all keys straight through to the API
Bug: 49699
Change-Id: I09d02a42b05146bc9b7080ab38338ae869bf15e3
2013-07-24 06:39:03 +00:00
|
|
|
$tabindex = 0;
|
|
|
|
$states = array( 'minor' => false, 'watch' => false );
|
|
|
|
$checkboxes = $ep->getCheckboxes( $tabindex, $states );
|
|
|
|
|
2013-05-15 21:17:06 +00:00
|
|
|
if ( $parsed === false ) {
|
|
|
|
$this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' );
|
|
|
|
} else {
|
|
|
|
$result = array_merge(
|
2013-06-29 01:52:29 +00:00
|
|
|
array(
|
|
|
|
'result' => 'success',
|
Render check boxes from EditPage
EditPage has a lovely getCheckboxes() function which includes the
minor and watch checkboxes as rendered by MW core, as well as any
checkboxes extensions like FlaggedRevs might have added. Output
these in the API, render them, and send their values back.
ApiVisualEditor.php:
* Build a fake EditPage, get its checkboxes, and return them
ApiVisualEditorEdit.php:
* Pass through posted request data to ApiEdit, which passes it
through to EditPage thanks to Idab5b524b0e3 in core
ve.init.mw.ViewPageTarget.js:
* Remove minor and watch checkboxes from the save dialog template
and replace them with a generic checkbox container
* Have getSaveOptions() pull the state of all checkboxes in
** Special-case minor and watch, and pass the rest straight through
** Move normalization from true/false to presence/absence here, from
ve.init.mw.Target.prototype.save(), because here we know which ones
are checkboxes and we don't know that in save() without
special-casing
* Remove getSaveDialogHtml(), we don't need to hide checkboxes based on
rights anymore because in that case the API just won't send them to us.
** Moved logic for checking the watch checkbox down to where the same
logic for the minor checkbox already is
* Unwrap getSaveDialogHtml() in setupSaveDialog()
* Access minor and watch by their new IDs throughout
ve.init.mw.Target.js:
* Get and store checkboxes from the API
* Pass all keys straight through to the API
Bug: 49699
Change-Id: I09d02a42b05146bc9b7080ab38338ae869bf15e3
2013-07-24 06:39:03 +00:00
|
|
|
'notices' => $notices,
|
|
|
|
'checkboxes' => $checkboxes,
|
2013-06-29 01:52:29 +00:00
|
|
|
),
|
|
|
|
$parsed['result']
|
2012-11-14 18:33:57 +00:00
|
|
|
);
|
2013-05-15 21:17:06 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-05-15 23:51:11 +00:00
|
|
|
case 'parsefragment':
|
|
|
|
$content = $this->parseWikitextFragment( $params['wikitext'], $page->getText() );
|
|
|
|
if ( $content === false ) {
|
2013-08-02 22:35:42 +00:00
|
|
|
$this->dieUsage( 'Error querying MediaWiki API', 'parsoidserver' );
|
2013-05-15 23:51:11 +00:00
|
|
|
} else {
|
|
|
|
$result = array(
|
|
|
|
'result' => 'success',
|
|
|
|
'content' => $content
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
2013-05-15 21:17:06 +00:00
|
|
|
case 'serialize':
|
2013-11-01 21:30:22 +00:00
|
|
|
if ( $params['cachekey'] !== null ) {
|
|
|
|
$content = $this->trySerializationCache( $params['cachekey'] );
|
|
|
|
if ( !is_string( $content ) ) {
|
|
|
|
$this->dieUsage( 'No cached serialization found with that key', 'badcachekey' );
|
|
|
|
}
|
2012-06-01 23:26:03 +00:00
|
|
|
} else {
|
2013-11-01 21:30:22 +00:00
|
|
|
if ( $params['html'] === null ) {
|
|
|
|
$this->dieUsageMsg( 'missingparam', 'html' );
|
|
|
|
}
|
|
|
|
$html = $params['html'];
|
|
|
|
$content = $this->postHTML( $page, $html, $parserParams );
|
|
|
|
if ( $content === false ) {
|
|
|
|
$this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' );
|
|
|
|
}
|
2013-05-15 21:17:06 +00:00
|
|
|
}
|
2013-11-01 21:30:22 +00:00
|
|
|
$result = array( 'result' => 'success', 'content' => $content );
|
2013-05-15 21:17:06 +00:00
|
|
|
break;
|
2013-06-28 22:46:15 +00:00
|
|
|
case 'diff':
|
2013-11-01 21:30:22 +00:00
|
|
|
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( $page, $params['html'], $parserParams );
|
|
|
|
if ( $wikitext === false ) {
|
|
|
|
$this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' );
|
|
|
|
}
|
2013-06-28 22:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$diff = $this->diffWikitext( $page, $wikitext );
|
|
|
|
if ( $diff['result'] === 'fail' ) {
|
|
|
|
$this->dieUsage( 'Diff failed', 'difffailed' );
|
|
|
|
}
|
|
|
|
$result = $diff;
|
|
|
|
|
2013-11-01 21:30:22 +00:00
|
|
|
break;
|
|
|
|
case 'serializeforcache':
|
|
|
|
$key = $this->storeInSerializationCache( $page, $parserParams['oldid'], $params['html'] );
|
|
|
|
$result = array( 'result' => 'success', 'cachekey' => $key );
|
2013-05-15 21:17:06 +00:00
|
|
|
break;
|
2012-05-25 19:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return array(
|
|
|
|
'page' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
|
|
),
|
|
|
|
'paction' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2013-11-01 21:30:22 +00:00
|
|
|
ApiBase::PARAM_TYPE => array(
|
|
|
|
'parse', 'parsefragment', 'serializeforcache', 'serialize', 'diff'
|
|
|
|
),
|
2012-05-25 22:23:40 +00:00
|
|
|
),
|
2013-05-15 23:51:11 +00:00
|
|
|
'wikitext' => null,
|
2012-11-14 18:33:57 +00:00
|
|
|
'basetimestamp' => null,
|
2012-11-28 23:57:00 +00:00
|
|
|
'starttimestamp' => null,
|
2013-05-15 23:51:11 +00:00
|
|
|
'oldid' => null,
|
2012-11-28 23:57:00 +00:00
|
|
|
'html' => null,
|
2013-11-01 21:30:22 +00:00
|
|
|
'cachekey' => null,
|
2012-05-25 19:50:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needsToken() {
|
2013-07-11 17:09:28 +00:00
|
|
|
return false;
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function mustBePosted() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isWriteMode() {
|
|
|
|
return true;
|
2012-05-25 19:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getVersion() {
|
|
|
|
return __CLASS__ . ': $Id$';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
|
|
|
return array(
|
2012-05-31 00:09:06 +00:00
|
|
|
'page' => 'The page to perform actions on.',
|
2012-11-14 18:33:57 +00:00
|
|
|
'paction' => 'Action to perform',
|
2013-07-11 17:09:28 +00:00
|
|
|
'oldid' => 'The revision number to use (defaults to latest version).',
|
2012-11-14 18:33:57 +00:00
|
|
|
'html' => 'HTML to send to parsoid in exchange for wikitext',
|
2013-05-29 14:49:16 +00:00
|
|
|
'basetimestamp' => 'When saving, set this to the timestamp of the revision that was'
|
2013-11-01 21:30:22 +00:00
|
|
|
. ' edited. Used to detect edit conflicts.',
|
2013-05-29 14:49:16 +00:00
|
|
|
'starttimestamp' => 'When saving, set this to the timestamp of when the page was loaded.'
|
2013-11-01 21:30:22 +00:00
|
|
|
. ' Used to detect edit conflicts.',
|
|
|
|
'cachekey' => 'For serialize or diff, use the result of a previous serializeforcache'
|
|
|
|
. ' request with this key. Overrides html.',
|
2012-05-25 19:50:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription() {
|
|
|
|
return 'Returns HTML5 for a page from the parsoid service.';
|
|
|
|
}
|
|
|
|
}
|