2013-07-11 17:09:28 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Parsoid API wrapper.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
2015-01-08 23:54:03 +00:00
|
|
|
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
|
2013-07-11 17:09:28 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ApiVisualEditorEdit extends ApiVisualEditor {
|
|
|
|
|
2014-08-13 08:15:42 +00:00
|
|
|
public function __construct( ApiMain $main, $name, Config $config ) {
|
|
|
|
parent::__construct( $main, $name, $config );
|
|
|
|
}
|
|
|
|
|
2013-07-11 17:09:28 +00:00
|
|
|
protected function saveWikitext( $title, $wikitext, $params ) {
|
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'edit',
|
|
|
|
'title' => $title->getPrefixedDBkey(),
|
|
|
|
'text' => $wikitext,
|
|
|
|
'summary' => $params['summary'],
|
|
|
|
'basetimestamp' => $params['basetimestamp'],
|
|
|
|
'starttimestamp' => $params['starttimestamp'],
|
|
|
|
'token' => $params['token'],
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $params['minor'] ) {
|
|
|
|
$apiParams['minor'] = true;
|
2013-07-24 14:07:05 +00:00
|
|
|
} else {
|
|
|
|
$apiParams['notminor'] = true;
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME add some way that the user's preferences can be respected
|
|
|
|
$apiParams['watchlist'] = $params['watch'] ? 'watch' : 'unwatch';
|
|
|
|
|
|
|
|
if ( $params['captchaid'] ) {
|
|
|
|
$apiParams['captchaid'] = $params['captchaid'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $params['captchaword'] ) {
|
|
|
|
$apiParams['captchaword'] = $params['captchaword'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
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
|
|
|
$apiParams + $this->getRequest()->getValues(),
|
2013-07-11 17:09:28 +00:00
|
|
|
true // was posted
|
|
|
|
),
|
|
|
|
true // enable write
|
|
|
|
);
|
|
|
|
|
|
|
|
$api->execute();
|
|
|
|
|
2014-12-30 22:20:55 +00:00
|
|
|
if ( defined( 'ApiResult::META_CONTENT' ) ) {
|
|
|
|
return $api->getResult()->getResultData();
|
|
|
|
} else {
|
|
|
|
return $api->getResultData();
|
|
|
|
}
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 20:22:48 +00:00
|
|
|
protected function parseWikitext( $title, $newRevId ) {
|
2015-03-13 20:10:49 +00:00
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'parse',
|
|
|
|
'page' => $title->getPrefixedDBkey(),
|
2015-04-08 20:22:48 +00:00
|
|
|
'oldid' => $newRevId,
|
2015-03-13 20:10:49 +00:00
|
|
|
'prop' => 'text|revid|categorieshtml|displaytitle',
|
|
|
|
);
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
|
|
|
false // was posted?
|
|
|
|
),
|
|
|
|
true // enable write?
|
|
|
|
);
|
|
|
|
|
|
|
|
$api->execute();
|
|
|
|
if ( defined( 'ApiResult::META_CONTENT' ) ) {
|
2015-04-20 18:41:29 +00:00
|
|
|
$result = $api->getResult()->getResultData( null, array(
|
|
|
|
'BC' => array(), // Transform content nodes to '*'
|
|
|
|
'Types' => array(), // Add back-compat subelements
|
|
|
|
'Strip' => 'all', // Remove any metadata keys from the links array
|
|
|
|
) );
|
2015-03-13 20:10:49 +00:00
|
|
|
} else {
|
|
|
|
$result = $api->getResultData();
|
|
|
|
}
|
|
|
|
$content = isset( $result['parse']['text']['*'] ) ? $result['parse']['text']['*'] : false;
|
|
|
|
$categorieshtml = isset( $result['parse']['categorieshtml']['*'] ) ?
|
|
|
|
$result['parse']['categorieshtml']['*'] : false;
|
|
|
|
$links = isset( $result['parse']['links'] ) ? $result['parse']['links'] : array();
|
|
|
|
$revision = Revision::newFromId( $result['parse']['revid'] );
|
|
|
|
$timestamp = $revision ? $revision->getTimestamp() : wfTimestampNow();
|
|
|
|
$displaytitle = isset( $result['parse']['displaytitle'] ) ?
|
|
|
|
$result['parse']['displaytitle'] : false;
|
|
|
|
|
|
|
|
if ( $content === false || ( strlen( $content ) && $revision === null ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $displaytitle !== false ) {
|
|
|
|
// Escape entities as in OutputPage::setPageTitle()
|
|
|
|
$displaytitle = Sanitizer::normalizeCharReferences(
|
|
|
|
Sanitizer::removeHTMLtags( $displaytitle ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'content' => $content,
|
|
|
|
'categorieshtml' => $categorieshtml,
|
|
|
|
'basetimestamp' => $timestamp,
|
|
|
|
'starttimestamp' => wfTimestampNow(),
|
|
|
|
'displayTitleHtml' => $displaytitle
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-07-11 17:09:28 +00:00
|
|
|
public function execute() {
|
|
|
|
$user = $this->getUser();
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$page = Title::newFromText( $params['page'] );
|
|
|
|
if ( !$page ) {
|
|
|
|
$this->dieUsageMsg( 'invalidtitle', $params['page'] );
|
|
|
|
}
|
2015-07-31 03:39:21 +00:00
|
|
|
if ( !in_array( $page->getNamespace(), $this->veConfig->get( 'VisualEditorNamespaces' ) ) ) {
|
2013-07-11 17:09:28 +00:00
|
|
|
$this->dieUsage( "VisualEditor is not enabled in namespace " .
|
|
|
|
$page->getNamespace(), 'novenamespace' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$parserParams = array();
|
|
|
|
if ( isset( $params['oldid'] ) ) {
|
|
|
|
$parserParams['oldid'] = $params['oldid'];
|
|
|
|
}
|
|
|
|
|
2014-06-23 17:55:59 +00:00
|
|
|
$html = $params['html'];
|
|
|
|
if ( substr( $html, 0, 11 ) === 'rawdeflate,' ) {
|
|
|
|
$html = gzinflate( base64_decode( substr( $html, 11 ) ) );
|
|
|
|
}
|
|
|
|
|
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 {
|
2014-06-23 17:55:59 +00:00
|
|
|
$wikitext = $this->postHTML( $page, $html, $parserParams );
|
2013-11-01 21:30:22 +00:00
|
|
|
if ( $wikitext === false ) {
|
|
|
|
$this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' );
|
|
|
|
}
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$saveresult = $this->saveWikitext( $page, $wikitext, $params );
|
|
|
|
$editStatus = $saveresult['edit']['result'];
|
|
|
|
|
|
|
|
// Error
|
2014-04-17 08:16:34 +00:00
|
|
|
if ( $editStatus !== 'Success' ) {
|
2013-07-11 17:09:28 +00:00
|
|
|
$result = array(
|
|
|
|
'result' => 'error',
|
|
|
|
'edit' => $saveresult['edit']
|
|
|
|
);
|
|
|
|
|
2015-06-30 15:28:35 +00:00
|
|
|
if ( isset( $saveresult['edit']['spamblacklist'] ) ) {
|
|
|
|
$matches = explode( '|', $saveresult['edit']['spamblacklist'] );
|
|
|
|
$matcheslist = $this->getLanguage()->listToText( $matches );
|
|
|
|
$result['edit']['sberrorparsed'] = $this->msg( 'spamprotectiontext' )->parse() . ' ' .
|
|
|
|
$this->msg( 'spamprotectionmatch', $matcheslist )->parse();
|
|
|
|
}
|
|
|
|
|
2013-07-11 17:09:28 +00:00
|
|
|
// Success
|
|
|
|
} else {
|
2015-04-08 20:22:48 +00:00
|
|
|
if ( isset( $saveresult['edit']['newrevid'] ) ) {
|
|
|
|
$newRevId = intval( $saveresult['edit']['newrevid'] );
|
|
|
|
if ( $this->veConfig->get( 'VisualEditorUseChangeTagging' ) ) {
|
2015-05-27 18:19:39 +00:00
|
|
|
// Defer till after the RC row is inserted
|
|
|
|
// @TODO: doEditContent should let callers specify desired tags
|
|
|
|
DeferredUpdates::addCallableUpdate( function() use ( $newRevId ) {
|
|
|
|
ChangeTags::addTags( 'visualeditor', null, $newRevId, null );
|
|
|
|
} );
|
2015-04-08 20:22:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$newRevId = $page->getLatestRevId();
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return result of parseWikitext instead of saveWikitext so that the
|
|
|
|
// frontend can update the page rendering without a refresh.
|
2015-04-08 20:22:48 +00:00
|
|
|
$result = $this->parseWikitext( $page, $newRevId );
|
2013-07-11 17:09:28 +00:00
|
|
|
if ( $result === false ) {
|
|
|
|
$this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' );
|
|
|
|
}
|
2014-11-07 00:31:34 +00:00
|
|
|
|
2014-06-10 14:35:29 +00:00
|
|
|
$result['isRedirect'] = $page->isRedirect();
|
2013-07-11 17:09:28 +00:00
|
|
|
|
2014-11-07 00:31:34 +00:00
|
|
|
if ( class_exists( 'FlaggablePageView' ) ) {
|
|
|
|
$view = FlaggablePageView::singleton();
|
|
|
|
|
|
|
|
// Defeat !$this->isPageView( $request ) || $request->getVal( 'oldid' ) check in setPageContent
|
|
|
|
$view->getContext()->setRequest( new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
array(
|
|
|
|
'diff' => null,
|
|
|
|
'oldid' => '',
|
|
|
|
'action' => 'view'
|
|
|
|
) + $this->getRequest()->getValues()
|
|
|
|
) );
|
|
|
|
|
2014-11-17 01:24:54 +00:00
|
|
|
// The two parameters here are references but we don't care
|
|
|
|
// about what FlaggedRevs does with them.
|
|
|
|
$outputDone = null;
|
|
|
|
$useParserCache = null;
|
|
|
|
$view->setPageContent( $outputDone, $useParserCache );
|
2014-11-07 00:31:34 +00:00
|
|
|
$view->displayTag();
|
|
|
|
}
|
|
|
|
$result['contentSub'] = $this->getOutput()->getSubtitle();
|
2014-10-31 00:26:32 +00:00
|
|
|
$lang = $this->getLanguage();
|
|
|
|
|
2014-11-17 01:00:06 +00:00
|
|
|
if ( isset( $saveresult['edit']['newtimestamp'] ) ) {
|
|
|
|
$ts = $saveresult['edit']['newtimestamp'];
|
|
|
|
|
|
|
|
$result['lastModified'] = array(
|
|
|
|
'date' => $lang->userDate( $ts, $user ),
|
|
|
|
'time' => $lang->userTime( $ts, $user )
|
|
|
|
);
|
|
|
|
}
|
2014-10-31 00:26:32 +00:00
|
|
|
|
2013-07-11 17:09:28 +00:00
|
|
|
if ( isset( $saveresult['edit']['newrevid'] ) ) {
|
|
|
|
$result['newrevid'] = intval( $saveresult['edit']['newrevid'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
$result['result'] = 'success';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return array(
|
|
|
|
'page' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
|
|
),
|
|
|
|
'token' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
|
|
),
|
|
|
|
'wikitext' => null,
|
|
|
|
'basetimestamp' => null,
|
|
|
|
'starttimestamp' => null,
|
|
|
|
'oldid' => null,
|
|
|
|
'minor' => null,
|
|
|
|
'watch' => null,
|
|
|
|
'html' => null,
|
|
|
|
'summary' => null,
|
|
|
|
'captchaid' => null,
|
|
|
|
'captchaword' => null,
|
2013-11-01 21:30:22 +00:00
|
|
|
'cachekey' => null,
|
2013-07-11 17:09:28 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needsToken() {
|
2014-08-09 13:08:14 +00:00
|
|
|
return 'csrf';
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getTokenSalt() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function mustBePosted() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isWriteMode() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-29 20:02:11 +00:00
|
|
|
/**
|
|
|
|
* @deprecated since MediaWiki core 1.25
|
|
|
|
*/
|
2013-07-11 17:09:28 +00:00
|
|
|
public function getParamDescription() {
|
|
|
|
return array(
|
|
|
|
'page' => 'The page to perform actions on.',
|
|
|
|
'oldid' => 'The revision number to use. Defaults to latest revision. Use 0 for new page.',
|
|
|
|
'minor' => 'Flag for minor edit.',
|
|
|
|
'html' => 'HTML to send to Parsoid in exchange for wikitext',
|
|
|
|
'summary' => 'Edit summary',
|
|
|
|
'basetimestamp' => 'When saving, set this to the timestamp of the revision that was'
|
|
|
|
. ' edited. Used to detect edit conflicts.',
|
|
|
|
'starttimestamp' => 'When saving, set this to the timestamp of when the page was loaded.'
|
|
|
|
. ' Used to detect edit conflicts.',
|
|
|
|
'token' => 'Edit token',
|
|
|
|
'captchaid' => 'Captcha ID (when saving with a captcha response).',
|
|
|
|
'captchaword' => 'Answer to the captcha (when saving with a captcha response).',
|
2013-11-01 21:30:22 +00:00
|
|
|
'cachekey' => 'Use the result of a previous serializeforcache request with this key.'
|
|
|
|
. 'Overrides html.',
|
2013-07-11 17:09:28 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-29 20:02:11 +00:00
|
|
|
/**
|
|
|
|
* @deprecated since MediaWiki core 1.25
|
|
|
|
*/
|
2013-07-11 17:09:28 +00:00
|
|
|
public function getDescription() {
|
|
|
|
return 'Save an HTML5 page to MediaWiki (converted to wikitext via the Parsoid service).';
|
|
|
|
}
|
|
|
|
}
|