2013-07-11 17:09:28 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-09-14 16:13:31 +00:00
|
|
|
* Parsoid/RESTBase+MediaWiki API wrapper.
|
2013-07-11 17:09:28 +00:00
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2018-03-28 19:47:04 +00:00
|
|
|
* @license MIT
|
2013-07-11 17:09:28 +00:00
|
|
|
*/
|
|
|
|
|
2019-10-11 19:42:23 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2017-03-17 11:19:32 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2016-09-08 04:28:59 +00:00
|
|
|
|
2020-03-16 23:30:26 +00:00
|
|
|
class ApiVisualEditorEdit extends ApiBase {
|
|
|
|
|
|
|
|
use ApiParsoidTrait;
|
|
|
|
|
2019-03-28 17:42:45 +00:00
|
|
|
const MAX_CACHE_RECENT = 2;
|
|
|
|
const MAX_CACHE_TTL = 900;
|
2013-07-11 17:09:28 +00:00
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-05-12 16:53:31 +00:00
|
|
|
public function __construct( ApiMain $main, $name ) {
|
|
|
|
parent::__construct( $main, $name );
|
2020-03-16 23:30:26 +00:00
|
|
|
$this->setLogger( LoggerFactory::getInstance( 'VisualEditor' ) );
|
2014-08-13 08:15:42 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* Attempt to save a given page's wikitext to MediaWiki's storage layer via its API
|
|
|
|
*
|
2018-06-26 15:35:09 +00:00
|
|
|
* @param Title $title The title of the page to write
|
2018-03-28 19:47:04 +00:00
|
|
|
* @param string $wikitext The wikitext to write
|
|
|
|
* @param array $params The edit parameters
|
2019-03-01 22:49:26 +00:00
|
|
|
* @return mixed The result of the save attempt
|
2018-03-28 19:47:04 +00:00
|
|
|
*/
|
2018-06-26 15:35:09 +00:00
|
|
|
protected function saveWikitext( Title $title, $wikitext, $params ) {
|
2016-02-17 16:18:02 +00:00
|
|
|
$apiParams = [
|
2013-07-11 17:09:28 +00:00
|
|
|
'action' => 'edit',
|
|
|
|
'title' => $title->getPrefixedDBkey(),
|
|
|
|
'text' => $wikitext,
|
|
|
|
'summary' => $params['summary'],
|
|
|
|
'basetimestamp' => $params['basetimestamp'],
|
|
|
|
'starttimestamp' => $params['starttimestamp'],
|
|
|
|
'token' => $params['token'],
|
2020-02-29 17:35:29 +00:00
|
|
|
'watchlist' => $params['watchlist'],
|
2020-02-29 17:38:12 +00:00
|
|
|
'tags' => $params['tags'],
|
|
|
|
'section' => $params['section'],
|
|
|
|
'sectiontitle' => $params['sectiontitle'],
|
|
|
|
'captchaid' => $params['captchaid'],
|
|
|
|
'captchaword' => $params['captchaword'],
|
2017-01-12 00:30:01 +00:00
|
|
|
'errorformat' => 'html',
|
2016-02-17 16:18:02 +00:00
|
|
|
];
|
2013-07-11 17:09:28 +00:00
|
|
|
|
2020-03-23 16:36:22 +00:00
|
|
|
if ( $params['minor'] !== null ) {
|
2013-07-11 17:09:28 +00:00
|
|
|
$apiParams['minor'] = true;
|
2013-07-24 14:07:05 +00:00
|
|
|
} else {
|
|
|
|
$apiParams['notminor'] = true;
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 17:38:12 +00:00
|
|
|
// Pass any unrecognized query parameters to the internal action=edit API request. This is
|
|
|
|
// necessary to support extensions that add extra stuff to the edit form (e.g. FlaggedRevs)
|
|
|
|
// and allows passing any other query parameters to be used for edit tagging (e.g. T209132).
|
|
|
|
// Exclude other known params from here and ApiMain.
|
|
|
|
// TODO: This doesn't exclude params from the formatter
|
|
|
|
$allParams = $this->getRequest()->getValues();
|
|
|
|
$knownParams = array_keys( $this->getAllowedParams() + $this->getMain()->getAllowedParams() );
|
|
|
|
foreach ( $knownParams as $knownParam ) {
|
|
|
|
unset( $allParams[ $knownParam ] );
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
2020-02-29 17:38:12 +00:00
|
|
|
$apiParams + $allParams,
|
2017-05-04 22:27:27 +00:00
|
|
|
/* was posted? */ true
|
2013-07-11 17:09:28 +00:00
|
|
|
),
|
2017-05-04 22:27:27 +00:00
|
|
|
/* enable write? */ true
|
2013-07-11 17:09:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$api->execute();
|
|
|
|
|
2016-09-20 19:28:15 +00:00
|
|
|
return $api->getResult()->getResultData();
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* Load into an array the output of MediaWiki's parser for a given revision
|
|
|
|
*
|
|
|
|
* @param int $newRevId The revision to load
|
2019-01-26 13:50:22 +00:00
|
|
|
* @return array|false The parsed of the save attempt
|
2018-03-28 19:47:04 +00:00
|
|
|
*/
|
2016-12-06 21:35:36 +00:00
|
|
|
protected function parseWikitext( $newRevId ) {
|
2016-02-17 16:18:02 +00:00
|
|
|
$apiParams = [
|
2015-03-13 20:10:49 +00:00
|
|
|
'action' => 'parse',
|
2015-04-08 20:22:48 +00:00
|
|
|
'oldid' => $newRevId,
|
2015-07-27 19:27:03 +00:00
|
|
|
'prop' => 'text|revid|categorieshtml|displaytitle|modules|jsconfigvars',
|
2016-02-17 16:18:02 +00:00
|
|
|
];
|
2015-03-13 20:10:49 +00:00
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
2017-05-04 22:27:27 +00:00
|
|
|
/* was posted? */ false
|
2015-03-13 20:10:49 +00:00
|
|
|
),
|
2017-05-04 22:27:27 +00:00
|
|
|
/* enable write? */ true
|
2015-03-13 20:10:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$api->execute();
|
2016-09-20 19:28:15 +00:00
|
|
|
$result = $api->getResult()->getResultData( null, [
|
2017-05-04 22:27:27 +00:00
|
|
|
/* Transform content nodes to '*' */ 'BC' => [],
|
|
|
|
/* Add back-compat subelements */ 'Types' => [],
|
|
|
|
/* Remove any metadata keys from the links array */ 'Strip' => 'all',
|
2016-09-20 19:28:15 +00:00
|
|
|
] );
|
2019-03-12 20:27:47 +00:00
|
|
|
$content = $result['parse']['text']['*'] ?? false;
|
|
|
|
$categorieshtml = $result['parse']['categorieshtml']['*'] ?? false;
|
|
|
|
$displaytitle = $result['parse']['displaytitle'] ?? false;
|
2017-02-14 20:39:34 +00:00
|
|
|
$modules = array_merge(
|
2019-03-12 20:27:47 +00:00
|
|
|
$result['parse']['modules'] ?? [],
|
|
|
|
$result['parse']['modulestyles'] ?? []
|
2017-02-14 20:39:34 +00:00
|
|
|
);
|
2019-03-12 20:27:47 +00:00
|
|
|
$jsconfigvars = $result['parse']['jsconfigvars'] ?? [];
|
2015-03-13 20:10:49 +00:00
|
|
|
|
2020-03-17 20:04:51 +00:00
|
|
|
if (
|
|
|
|
$content === false ||
|
|
|
|
// TODO: Is this check still needed?
|
2020-03-30 04:44:18 +00:00
|
|
|
( strlen( $content ) && MediaWikiServices::getInstance()
|
|
|
|
->getRevisionLookup()
|
|
|
|
->getRevisionById( $result['parse']['revid'] ) === null
|
|
|
|
)
|
2020-03-17 20:04:51 +00:00
|
|
|
) {
|
2015-03-13 20:10:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $displaytitle !== false ) {
|
|
|
|
// Escape entities as in OutputPage::setPageTitle()
|
|
|
|
$displaytitle = Sanitizer::normalizeCharReferences(
|
|
|
|
Sanitizer::removeHTMLtags( $displaytitle ) );
|
|
|
|
}
|
|
|
|
|
2016-02-17 16:18:02 +00:00
|
|
|
return [
|
2015-03-13 20:10:49 +00:00
|
|
|
'content' => $content,
|
|
|
|
'categorieshtml' => $categorieshtml,
|
2015-07-27 19:27:03 +00:00
|
|
|
'displayTitleHtml' => $displaytitle,
|
|
|
|
'modules' => $modules,
|
|
|
|
'jsconfigvars' => $jsconfigvars
|
2016-02-17 16:18:02 +00:00
|
|
|
];
|
2015-03-13 20:10:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* Create and load the parsed wikitext of an edit, or from the serialisation cache if available.
|
|
|
|
*
|
2018-06-26 15:35:09 +00:00
|
|
|
* @param Title $title The title of the page
|
2018-03-28 19:47:04 +00:00
|
|
|
* @param array $params The edit parameters
|
|
|
|
* @param array $parserParams The parser parameters
|
|
|
|
* @return string The wikitext of the edit
|
|
|
|
*/
|
2018-06-26 15:35:09 +00:00
|
|
|
protected function getWikitext( Title $title, $params, $parserParams ) {
|
2016-10-31 17:45:49 +00:00
|
|
|
if ( $params['cachekey'] !== null ) {
|
|
|
|
$wikitext = $this->trySerializationCache( $params['cachekey'] );
|
|
|
|
if ( !is_string( $wikitext ) ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'apierror-visualeditor-badcachekey', 'badcachekey' );
|
2016-10-31 17:45:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$wikitext = $this->getWikitextNoCache( $title, $params, $parserParams );
|
|
|
|
}
|
2019-12-15 04:30:43 +00:00
|
|
|
'@phan-var string $wikitext';
|
2016-10-31 17:45:49 +00:00
|
|
|
return $wikitext;
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* Create and load the parsed wikitext of an edit, ignoring the serialisation cache.
|
|
|
|
*
|
2018-06-26 15:35:09 +00:00
|
|
|
* @param Title $title The title of the page
|
2018-03-28 19:47:04 +00:00
|
|
|
* @param array $params The edit parameters
|
|
|
|
* @param array $parserParams The parser parameters
|
|
|
|
* @return string The wikitext of the edit
|
|
|
|
*/
|
2018-06-26 15:35:09 +00:00
|
|
|
protected function getWikitextNoCache( Title $title, $params, $parserParams ) {
|
2016-10-31 17:45:49 +00:00
|
|
|
$this->requireOnlyOneParameter( $params, 'html' );
|
2019-10-30 19:13:47 +00:00
|
|
|
if ( Deflate::isDeflated( $params['html'] ) ) {
|
|
|
|
$status = Deflate::inflate( $params['html'] );
|
2018-08-02 19:38:55 +00:00
|
|
|
if ( !$status->isGood() ) {
|
2019-10-30 19:13:47 +00:00
|
|
|
$this->dieWithError( 'deflate-invaliddeflate', 'invaliddeflate' );
|
2018-08-02 19:38:55 +00:00
|
|
|
}
|
|
|
|
$html = $status->getValue();
|
|
|
|
} else {
|
|
|
|
$html = $params['html'];
|
2018-06-10 14:57:42 +00:00
|
|
|
}
|
2016-10-31 17:45:49 +00:00
|
|
|
$wikitext = $this->postHTML(
|
2018-08-02 19:38:55 +00:00
|
|
|
$title, $html, $parserParams, $params['etag']
|
2016-10-31 17:45:49 +00:00
|
|
|
);
|
|
|
|
return $wikitext;
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* Load the parsed wikitext of an edit into the serialisation cache.
|
|
|
|
*
|
2018-06-26 15:35:09 +00:00
|
|
|
* @param Title $title The title of the page
|
2018-03-28 19:47:04 +00:00
|
|
|
* @param string $wikitext The wikitext of the edit
|
2019-12-15 04:30:43 +00:00
|
|
|
* @return string|false The key of the wikitext in the serialisation cache
|
2018-03-28 19:47:04 +00:00
|
|
|
*/
|
2018-06-26 15:35:09 +00:00
|
|
|
protected function storeInSerializationCache( Title $title, $wikitext ) {
|
2016-09-08 04:28:59 +00:00
|
|
|
if ( $wikitext === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-27 19:04:57 +00:00
|
|
|
$cache = ObjectCache::getLocalClusterInstance();
|
2019-04-23 19:45:17 +00:00
|
|
|
|
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
$statsd = $services->getStatsdDataFactory();
|
|
|
|
$editStash = $services->getPageEditStash();
|
2019-03-27 19:04:57 +00:00
|
|
|
|
2016-09-08 04:28:59 +00:00
|
|
|
// Store the corresponding wikitext, referenceable by a new key
|
|
|
|
$hash = md5( $wikitext );
|
2019-03-27 19:04:57 +00:00
|
|
|
$key = $cache->makeKey( 'visualeditor', 'serialization', $hash );
|
2019-03-28 17:42:45 +00:00
|
|
|
$ok = $cache->set( $key, $wikitext, self::MAX_CACHE_TTL );
|
|
|
|
if ( $ok ) {
|
|
|
|
$this->pruneExcessStashedEntries( $cache, $this->getUser(), $key );
|
|
|
|
}
|
|
|
|
|
2019-03-27 19:04:57 +00:00
|
|
|
$status = $ok ? 'ok' : 'failed';
|
|
|
|
$statsd->increment( "editstash.ve_serialization_cache.set_" . $status );
|
2016-09-08 04:28:59 +00:00
|
|
|
|
|
|
|
// Also parse and prepare the edit in case it might be saved later
|
|
|
|
$page = WikiPage::factory( $title );
|
|
|
|
$content = ContentHandler::makeContent( $wikitext, $title, CONTENT_MODEL_WIKITEXT );
|
|
|
|
|
2019-04-23 19:45:17 +00:00
|
|
|
$status = $editStash->parseAndCache( $page, $content, $this->getUser(), '' );
|
|
|
|
if ( $status === $editStash::ERROR_NONE ) {
|
2016-09-08 04:28:59 +00:00
|
|
|
$logger = LoggerFactory::getInstance( 'StashEdit' );
|
|
|
|
$logger->debug( "Cached parser output for VE content key '$key'." );
|
|
|
|
}
|
2019-03-27 19:04:57 +00:00
|
|
|
$statsd->increment( "editstash.ve_cache_stores.$status" );
|
2016-09-08 04:28:59 +00:00
|
|
|
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:42:45 +00:00
|
|
|
/**
|
|
|
|
* @param BagOStuff $cache
|
|
|
|
* @param User $user
|
|
|
|
* @param string $newKey
|
|
|
|
*/
|
|
|
|
private function pruneExcessStashedEntries( BagOStuff $cache, User $user, $newKey ) {
|
|
|
|
$key = $cache->makeKey( 'visualeditor-serialization-recent', $user->getName() );
|
|
|
|
|
|
|
|
$keyList = $cache->get( $key ) ?: [];
|
|
|
|
if ( count( $keyList ) >= self::MAX_CACHE_RECENT ) {
|
|
|
|
$oldestKey = array_shift( $keyList );
|
|
|
|
$cache->delete( $oldestKey );
|
|
|
|
}
|
|
|
|
|
|
|
|
$keyList[] = $newKey;
|
|
|
|
$cache->set( $key, $keyList, 2 * self::MAX_CACHE_TTL );
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* Load some parsed wikitext of an edit from the serialisation cache.
|
|
|
|
*
|
|
|
|
* @param string $hash The key of the wikitext in the serialisation cache
|
|
|
|
* @return string|null The wikitext
|
|
|
|
*/
|
2016-09-08 04:28:59 +00:00
|
|
|
protected function trySerializationCache( $hash ) {
|
2019-03-27 19:04:57 +00:00
|
|
|
$cache = ObjectCache::getLocalClusterInstance();
|
|
|
|
$key = $cache->makeKey( 'visualeditor', 'serialization', $hash );
|
|
|
|
$value = $cache->get( $key );
|
|
|
|
|
|
|
|
$status = ( $value !== false ) ? 'hit' : 'miss';
|
|
|
|
$statsd = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
$statsd->increment( "editstash.ve_serialization_cache.get_$status" );
|
|
|
|
|
|
|
|
return $value;
|
2016-09-08 04:28:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* Calculate the different between the wikitext of an edit and an existing revision.
|
|
|
|
*
|
2018-06-26 15:35:09 +00:00
|
|
|
* @param Title $title The title of the page
|
2018-03-28 19:47:04 +00:00
|
|
|
* @param int $fromId The existing revision of the page to compare with
|
|
|
|
* @param string $wikitext The wikitext to compare against
|
|
|
|
* @param int|null $section Whether the wikitext refers to a given section or the whole page
|
|
|
|
* @return array The comparison, or `[ 'result' => 'nochanges' ]` if there are none
|
|
|
|
*/
|
2018-06-26 15:35:09 +00:00
|
|
|
protected function diffWikitext( Title $title, $fromId, $wikitext, $section = null ) {
|
2016-09-08 04:28:59 +00:00
|
|
|
$apiParams = [
|
2018-03-10 22:32:02 +00:00
|
|
|
'action' => 'compare',
|
|
|
|
'prop' => 'diff',
|
|
|
|
'fromtitle' => $title->getPrefixedDBkey(),
|
|
|
|
'fromrev' => $fromId,
|
|
|
|
'fromsection' => $section,
|
2020-05-12 18:55:57 +00:00
|
|
|
'totext' => $wikitext,
|
|
|
|
'topst' => true,
|
2016-09-08 04:28:59 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
2017-05-04 22:27:27 +00:00
|
|
|
/* was posted? */ false
|
2016-09-08 04:28:59 +00:00
|
|
|
),
|
2017-05-04 22:27:27 +00:00
|
|
|
/* enable write? */ false
|
2016-09-08 04:28:59 +00:00
|
|
|
);
|
|
|
|
$api->execute();
|
|
|
|
$result = $api->getResult()->getResultData( null, [
|
2017-05-04 22:27:27 +00:00
|
|
|
/* Transform content nodes to '*' */ 'BC' => [],
|
|
|
|
/* Add back-compat subelements */ 'Types' => [],
|
2016-09-08 04:28:59 +00:00
|
|
|
] );
|
2018-03-10 22:32:02 +00:00
|
|
|
|
|
|
|
if ( !isset( $result['compare']['*'] ) ) {
|
2019-11-18 20:57:36 +00:00
|
|
|
$this->dieWithError( 'apierror-visualeditor-difffailed', 'difffailed' );
|
2016-09-08 04:28:59 +00:00
|
|
|
}
|
2018-03-10 22:32:02 +00:00
|
|
|
$diffRows = $result['compare']['*'];
|
2016-09-08 04:28:59 +00:00
|
|
|
|
2019-11-19 19:38:15 +00:00
|
|
|
$context = new DerivativeContext( $this->getContext() );
|
|
|
|
$context->setTitle( $title );
|
|
|
|
$engine = new DifferenceEngine( $context );
|
|
|
|
return [
|
|
|
|
'result' => 'success',
|
|
|
|
'diff' => $diffRows ? $engine->addHeader(
|
|
|
|
$diffRows,
|
|
|
|
$context->msg( 'currentrev' )->parse(),
|
|
|
|
$context->msg( 'yourtext' )->parse()
|
|
|
|
) : ''
|
|
|
|
];
|
2016-09-08 04:28:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2013-07-11 17:09:28 +00:00
|
|
|
public function execute() {
|
|
|
|
$user = $this->getUser();
|
|
|
|
$params = $this->extractRequestParams();
|
2016-04-29 16:00:57 +00:00
|
|
|
$title = Title::newFromText( $params['page'] );
|
2019-12-20 15:50:48 +00:00
|
|
|
if ( $title && $title->isSpecial( 'CollabPad' ) ) {
|
|
|
|
// Convert Special:CollabPad/MyPage to MyPage so we can serialize properly
|
|
|
|
$title = SpecialCollabPad::getSubPage( $title );
|
|
|
|
}
|
2016-04-29 16:00:57 +00:00
|
|
|
if ( !$title ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] );
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
2019-12-15 04:30:43 +00:00
|
|
|
'@phan-var Title $title';
|
2016-04-29 16:00:57 +00:00
|
|
|
|
2016-02-17 16:18:02 +00:00
|
|
|
$parserParams = [];
|
2013-07-11 17:09:28 +00:00
|
|
|
if ( isset( $params['oldid'] ) ) {
|
|
|
|
$parserParams['oldid'] = $params['oldid'];
|
|
|
|
}
|
|
|
|
|
2016-11-19 14:09:16 +00:00
|
|
|
if ( isset( $params['wikitext'] ) ) {
|
2019-11-13 22:24:06 +00:00
|
|
|
$wikitext = str_replace( "\r\n", "\n", $params['wikitext'] );
|
2016-11-19 14:09:16 +00:00
|
|
|
} else {
|
2016-10-31 17:45:49 +00:00
|
|
|
$wikitext = $this->getWikitext( $title, $params, $parserParams );
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 04:28:59 +00:00
|
|
|
if ( $params['paction'] === 'serialize' ) {
|
|
|
|
$result = [ 'result' => 'success', 'content' => $wikitext ];
|
|
|
|
} elseif ( $params['paction'] === 'serializeforcache' ) {
|
|
|
|
$key = $this->storeInSerializationCache(
|
|
|
|
$title,
|
|
|
|
$wikitext
|
|
|
|
);
|
|
|
|
$result = [ 'result' => 'success', 'cachekey' => $key ];
|
|
|
|
} elseif ( $params['paction'] === 'diff' ) {
|
2019-03-12 20:27:47 +00:00
|
|
|
$section = $params['section'] ?? null;
|
2019-11-18 20:57:36 +00:00
|
|
|
$result = $this->diffWikitext( $title, $params['oldid'], $wikitext, $section );
|
2016-09-08 04:28:59 +00:00
|
|
|
} elseif ( $params['paction'] === 'save' ) {
|
|
|
|
$saveresult = $this->saveWikitext( $title, $wikitext, $params );
|
|
|
|
$editStatus = $saveresult['edit']['result'];
|
|
|
|
|
|
|
|
// Error
|
|
|
|
if ( $editStatus !== 'Success' ) {
|
|
|
|
$result = [
|
|
|
|
'result' => 'error',
|
|
|
|
'edit' => $saveresult['edit']
|
|
|
|
];
|
2015-06-30 15:28:35 +00:00
|
|
|
|
2016-09-08 04:28:59 +00:00
|
|
|
// Success
|
2015-04-08 20:22:48 +00:00
|
|
|
} else {
|
2016-09-08 04:28:59 +00:00
|
|
|
if ( isset( $saveresult['edit']['newrevid'] ) ) {
|
|
|
|
$newRevId = intval( $saveresult['edit']['newrevid'] );
|
|
|
|
} else {
|
|
|
|
$newRevId = $title->getLatestRevId();
|
|
|
|
}
|
2013-07-11 17:09:28 +00:00
|
|
|
|
2016-09-08 04:28:59 +00:00
|
|
|
// Return result of parseWikitext instead of saveWikitext so that the
|
|
|
|
// frontend can update the page rendering without a refresh.
|
2016-12-06 21:35:36 +00:00
|
|
|
$result = $this->parseWikitext( $newRevId );
|
2016-09-08 04:28:59 +00:00
|
|
|
if ( $result === false ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'apierror-visualeditor-docserver', 'docserver' );
|
2016-09-08 04:28:59 +00:00
|
|
|
}
|
2014-11-07 00:31:34 +00:00
|
|
|
|
2017-05-04 22:07:10 +00:00
|
|
|
$result['isRedirect'] = (string)$title->isRedirect();
|
2016-09-08 04:28:59 +00:00
|
|
|
|
2020-04-20 22:07:01 +00:00
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'FlaggedRevs' ) ) {
|
2016-09-08 04:28:59 +00:00
|
|
|
$view = FlaggablePageView::singleton();
|
|
|
|
|
2019-01-30 21:03:09 +00:00
|
|
|
$originalContext = $view->getContext();
|
2016-09-08 04:28:59 +00:00
|
|
|
$originalTitle = RequestContext::getMain()->getTitle();
|
2019-01-30 21:03:09 +00:00
|
|
|
|
|
|
|
$newContext = new DerivativeContext( $originalContext );
|
2016-09-08 04:28:59 +00:00
|
|
|
// Defeat !$this->isPageView( $request ) || $request->getVal( 'oldid' ) check in setPageContent
|
|
|
|
$newRequest = new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
[
|
|
|
|
'diff' => null,
|
|
|
|
'oldid' => '',
|
|
|
|
'title' => $title->getPrefixedText(),
|
|
|
|
'action' => 'view'
|
|
|
|
] + $this->getRequest()->getValues()
|
|
|
|
);
|
2019-01-30 21:03:09 +00:00
|
|
|
$newContext->setRequest( $newRequest );
|
|
|
|
$newContext->setTitle( $title );
|
|
|
|
$view->setContext( $newContext );
|
2016-09-08 04:28:59 +00:00
|
|
|
RequestContext::getMain()->setTitle( $title );
|
|
|
|
|
|
|
|
// The two parameters here are references but we don't care
|
|
|
|
// about what FlaggedRevs does with them.
|
|
|
|
$outputDone = null;
|
|
|
|
$useParserCache = null;
|
2019-12-15 04:30:43 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgument
|
2016-09-08 04:28:59 +00:00
|
|
|
$view->setPageContent( $outputDone, $useParserCache );
|
|
|
|
$view->displayTag();
|
2019-01-30 21:03:09 +00:00
|
|
|
$view->setContext( $originalContext );
|
2016-09-08 04:28:59 +00:00
|
|
|
RequestContext::getMain()->setTitle( $originalTitle );
|
|
|
|
}
|
2016-04-04 16:27:46 +00:00
|
|
|
|
2016-09-08 04:28:59 +00:00
|
|
|
$context = new RequestContext;
|
|
|
|
$context->setTitle( $title );
|
|
|
|
$tempOut = new OutputPage( $context );
|
|
|
|
$tempOut->setArticleFlag( true );
|
2016-04-04 16:27:46 +00:00
|
|
|
|
2016-09-08 04:28:59 +00:00
|
|
|
$subpagestr = $this->getSkin()->subPageSubtitle( $tempOut );
|
|
|
|
if ( $subpagestr !== '' ) {
|
|
|
|
$subpagestr = '<span class="subpages">' . $subpagestr . '</span>';
|
|
|
|
}
|
|
|
|
$result['contentSub'] = $subpagestr . $this->getOutput()->getSubtitle();
|
2016-04-04 16:27:46 +00:00
|
|
|
|
2016-09-08 04:28:59 +00:00
|
|
|
$lang = $this->getLanguage();
|
2014-10-31 00:26:32 +00:00
|
|
|
|
2016-09-08 04:28:59 +00:00
|
|
|
if ( isset( $saveresult['edit']['newtimestamp'] ) ) {
|
|
|
|
$ts = $saveresult['edit']['newtimestamp'];
|
2014-11-17 01:00:06 +00:00
|
|
|
|
2016-09-08 04:28:59 +00:00
|
|
|
$result['lastModified'] = [
|
|
|
|
'date' => $lang->userDate( $ts, $user ),
|
|
|
|
'time' => $lang->userTime( $ts, $user )
|
|
|
|
];
|
|
|
|
}
|
2014-10-31 00:26:32 +00:00
|
|
|
|
2016-09-08 04:28:59 +00:00
|
|
|
if ( isset( $saveresult['edit']['newrevid'] ) ) {
|
|
|
|
$result['newrevid'] = intval( $saveresult['edit']['newrevid'] );
|
|
|
|
}
|
2013-07-11 17:09:28 +00:00
|
|
|
|
2016-09-08 04:28:59 +00:00
|
|
|
$result['result'] = 'success';
|
|
|
|
}
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 10:36:43 +00:00
|
|
|
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable False positive
|
2013-07-11 17:09:28 +00:00
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2013-07-11 17:09:28 +00:00
|
|
|
public function getAllowedParams() {
|
2016-02-17 16:18:02 +00:00
|
|
|
return [
|
2016-09-08 04:28:59 +00:00
|
|
|
'paction' => [
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
|
|
ApiBase::PARAM_TYPE => [
|
|
|
|
'serialize',
|
|
|
|
'serializeforcache',
|
|
|
|
'diff',
|
|
|
|
'save',
|
|
|
|
],
|
|
|
|
],
|
2016-02-17 16:18:02 +00:00
|
|
|
'page' => [
|
2013-07-11 17:09:28 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2016-02-17 16:18:02 +00:00
|
|
|
],
|
|
|
|
'token' => [
|
2013-07-11 17:09:28 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2016-02-17 16:18:02 +00:00
|
|
|
],
|
2020-06-10 16:08:23 +00:00
|
|
|
'wikitext' => [
|
|
|
|
ApiBase::PARAM_TYPE => 'text',
|
|
|
|
ApiBase::PARAM_DFLT => null,
|
|
|
|
],
|
2016-09-06 19:16:55 +00:00
|
|
|
'section' => null,
|
2016-12-08 19:24:10 +00:00
|
|
|
'sectiontitle' => null,
|
2013-07-11 17:09:28 +00:00
|
|
|
'basetimestamp' => null,
|
|
|
|
'starttimestamp' => null,
|
|
|
|
'oldid' => null,
|
|
|
|
'minor' => null,
|
2020-02-29 17:35:29 +00:00
|
|
|
'watchlist' => null,
|
2020-06-10 16:08:23 +00:00
|
|
|
'html' => [
|
|
|
|
ApiBase::PARAM_TYPE => 'text',
|
|
|
|
ApiBase::PARAM_DFLT => null,
|
|
|
|
],
|
2015-10-08 22:16:56 +00:00
|
|
|
'etag' => null,
|
2013-07-11 17:09:28 +00:00
|
|
|
'summary' => null,
|
|
|
|
'captchaid' => null,
|
|
|
|
'captchaword' => null,
|
2013-11-01 21:30:22 +00:00
|
|
|
'cachekey' => null,
|
2020-01-24 19:14:32 +00:00
|
|
|
'tags' => [
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
],
|
2016-02-17 16:18:02 +00:00
|
|
|
];
|
2013-07-11 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2013-07-11 17:09:28 +00:00
|
|
|
public function mustBePosted() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-16 23:30:26 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function isInternal() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2013-07-11 17:09:28 +00:00
|
|
|
public function isWriteMode() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|