2012-05-25 19:50:48 +00:00
|
|
|
<?php
|
2012-07-19 00:11:26 +00:00
|
|
|
/**
|
2015-09-14 16:13:31 +00:00
|
|
|
* Parsoid/RESTBase+MediaWiki API wrapper.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
2019-01-01 13:24:23 +00:00
|
|
|
* @copyright 2011-2019 VisualEditor Team and others; see AUTHORS.txt
|
2018-03-28 19:47:04 +00:00
|
|
|
* @license MIT
|
2012-07-19 00:11:26 +00:00
|
|
|
*/
|
|
|
|
|
2016-12-31 04:37:10 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
class ApiVisualEditor extends ApiBase {
|
2014-08-13 08:15:42 +00:00
|
|
|
/**
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
protected $veConfig;
|
|
|
|
|
2014-12-18 00:49:55 +00:00
|
|
|
/**
|
|
|
|
* @var VirtualRESTServiceClient
|
|
|
|
*/
|
|
|
|
protected $serviceClient;
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2014-08-13 08:15:42 +00:00
|
|
|
public function __construct( ApiMain $main, $name, Config $config ) {
|
|
|
|
parent::__construct( $main, $name );
|
|
|
|
$this->veConfig = $config;
|
2016-02-17 16:18:02 +00:00
|
|
|
$this->serviceClient = new VirtualRESTServiceClient( new MultiHttpClient( [] ) );
|
2015-03-04 14:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the virtual REST service object to be used in VE's API calls. The
|
|
|
|
* method determines whether to instantiate a ParsoidVirtualRESTService or a
|
|
|
|
* RestbaseVirtualRESTService object based on configuration directives: if
|
|
|
|
* $wgVirtualRestConfig['modules']['restbase'] is defined, RESTBase is chosen,
|
|
|
|
* otherwise Parsoid is used (either by using the MW Core config, or the
|
|
|
|
* VE-local one).
|
|
|
|
*
|
|
|
|
* @return VirtualRESTService the VirtualRESTService object to use
|
|
|
|
*/
|
2017-05-08 23:42:20 +00:00
|
|
|
protected function getVRSObject() {
|
2015-03-04 14:52:07 +00:00
|
|
|
// the params array to create the service object with
|
2016-02-17 16:18:02 +00:00
|
|
|
$params = [];
|
2015-03-04 14:52:07 +00:00
|
|
|
// the VRS class to use, defaults to Parsoid
|
2016-10-27 19:33:17 +00:00
|
|
|
$class = ParsoidVirtualRESTService::class;
|
2016-02-05 20:37:55 +00:00
|
|
|
// The global virtual rest service config object, if any
|
2015-03-04 14:52:07 +00:00
|
|
|
$vrs = $this->getConfig()->get( 'VirtualRestConfig' );
|
|
|
|
if ( isset( $vrs['modules'] ) && isset( $vrs['modules']['restbase'] ) ) {
|
|
|
|
// if restbase is available, use it
|
|
|
|
$params = $vrs['modules']['restbase'];
|
2017-05-04 22:27:27 +00:00
|
|
|
// backward compatibility
|
|
|
|
$params['parsoidCompat'] = false;
|
2016-10-27 19:33:17 +00:00
|
|
|
$class = RestbaseVirtualRESTService::class;
|
2015-03-04 14:52:07 +00:00
|
|
|
} elseif ( isset( $vrs['modules'] ) && isset( $vrs['modules']['parsoid'] ) ) {
|
|
|
|
// there's a global parsoid config, use it next
|
|
|
|
$params = $vrs['modules']['parsoid'];
|
2015-06-13 04:54:23 +00:00
|
|
|
$params['restbaseCompat'] = true;
|
2015-03-04 14:52:07 +00:00
|
|
|
} else {
|
2016-02-05 20:37:55 +00:00
|
|
|
// No global modules defined, so no way to contact the document server.
|
2018-03-08 20:03:37 +00:00
|
|
|
$this->dieWithError( 'apierror-visualeditor-docserver-unconfigured', 'no_vrs' );
|
2015-03-04 14:52:07 +00:00
|
|
|
}
|
|
|
|
// merge the global and service-specific params
|
|
|
|
if ( isset( $vrs['global'] ) ) {
|
|
|
|
$params = array_merge( $vrs['global'], $params );
|
|
|
|
}
|
|
|
|
// set up cookie forwarding
|
2016-07-12 17:01:24 +00:00
|
|
|
if ( $params['forwardCookies'] ) {
|
2018-04-03 02:33:25 +00:00
|
|
|
$params['forwardCookies'] = $this->getRequest()->getHeader( 'Cookie' );
|
2015-03-04 14:52:07 +00:00
|
|
|
} else {
|
|
|
|
$params['forwardCookies'] = false;
|
|
|
|
}
|
|
|
|
// create the VRS object and return it
|
|
|
|
return new $class( $params );
|
2014-02-07 16:10:59 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* Accessor function for all RESTbase requests
|
|
|
|
*
|
2018-06-26 15:14:09 +00:00
|
|
|
* @param Title $title The title of the page to use as the parsing context
|
2018-03-28 19:47:04 +00:00
|
|
|
* @param string $method The HTTP method, either 'GET' or 'POST'
|
|
|
|
* @param string $path The RESTbase api path
|
2018-10-31 18:04:46 +00:00
|
|
|
* @param array $params Request parameters
|
|
|
|
* @param array $reqheaders Request headers
|
2018-03-28 19:47:04 +00:00
|
|
|
* @return string Body of the RESTbase server's response
|
|
|
|
*/
|
2018-06-26 15:14:09 +00:00
|
|
|
protected function requestRestbase( Title $title, $method, $path, $params, $reqheaders = [] ) {
|
2016-03-30 00:05:50 +00:00
|
|
|
global $wgVersion;
|
2016-02-17 16:18:02 +00:00
|
|
|
$request = [
|
2014-12-18 00:49:55 +00:00
|
|
|
'method' => $method,
|
2015-06-13 04:54:23 +00:00
|
|
|
'url' => '/restbase/local/v1/' . $path
|
2016-02-17 16:18:02 +00:00
|
|
|
];
|
2014-12-18 00:49:55 +00:00
|
|
|
if ( $method === 'GET' ) {
|
|
|
|
$request['query'] = $params;
|
2014-04-18 20:17:02 +00:00
|
|
|
} else {
|
2014-12-18 00:49:55 +00:00
|
|
|
$request['body'] = $params;
|
2014-04-18 20:17:02 +00:00
|
|
|
}
|
2016-02-27 01:58:13 +00:00
|
|
|
// Should be synchronised with modules/ve-mw/init/ve.init.mw.ArticleTargetLoader.js
|
2018-06-22 16:57:50 +00:00
|
|
|
$reqheaders['Accept'] = 'text/html; charset=utf-8;' .
|
2018-08-02 19:25:08 +00:00
|
|
|
' profile="https://www.mediawiki.org/wiki/Specs/HTML/2.0.0"';
|
2018-06-26 15:14:09 +00:00
|
|
|
$reqheaders['Accept-Language'] = self::getPageLanguage( $title )->getCode();
|
2016-03-30 00:05:50 +00:00
|
|
|
$reqheaders['User-Agent'] = 'VisualEditor-MediaWiki/' . $wgVersion;
|
2016-10-27 15:54:46 +00:00
|
|
|
$reqheaders['Api-User-Agent'] = 'VisualEditor-MediaWiki/' . $wgVersion;
|
2015-10-08 22:16:56 +00:00
|
|
|
$request['headers'] = $reqheaders;
|
2014-12-18 00:49:55 +00:00
|
|
|
$response = $this->serviceClient->run( $request );
|
|
|
|
if ( $response['code'] === 200 && $response['error'] === "" ) {
|
2015-03-25 02:36:44 +00:00
|
|
|
// If response was served directly from Varnish, use the response
|
|
|
|
// (RP) header to declare the cache hit and pass the data to the client.
|
2015-02-16 01:13:54 +00:00
|
|
|
$headers = $response['headers'];
|
2015-03-25 02:36:44 +00:00
|
|
|
$rp = null;
|
2015-02-16 01:13:54 +00:00
|
|
|
if ( isset( $headers['x-cache'] ) && strpos( $headers['x-cache'], 'hit' ) !== false ) {
|
2015-03-25 02:36:44 +00:00
|
|
|
$rp = 'cached-response=true';
|
2014-04-18 20:17:02 +00:00
|
|
|
}
|
2015-03-25 02:36:44 +00:00
|
|
|
if ( $rp !== null ) {
|
2014-04-18 20:17:02 +00:00
|
|
|
$resp = $this->getRequest()->response();
|
2015-03-25 02:36:44 +00:00
|
|
|
$resp->header( 'X-Cache: ' . $rp );
|
2014-04-18 20:17:02 +00:00
|
|
|
}
|
2014-12-18 00:49:55 +00:00
|
|
|
} elseif ( $response['error'] !== '' ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
[ 'apierror-visualeditor-docserver-http-error', wfEscapeWikiText( $response['error'] ) ],
|
2017-01-04 14:59:51 +00:00
|
|
|
'apierror-visualeditor-docserver-http-error'
|
2016-11-03 19:16:57 +00:00
|
|
|
);
|
2017-05-04 22:27:27 +00:00
|
|
|
} else {
|
|
|
|
// error null, code not 200
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError(
|
|
|
|
[ 'apierror-visualeditor-docserver-http', $response['code'] ],
|
2017-01-04 14:59:51 +00:00
|
|
|
'apierror-visualeditor-docserver-http'
|
2016-11-03 19:16:57 +00:00
|
|
|
);
|
2014-04-18 20:17:02 +00:00
|
|
|
}
|
2014-12-18 00:49:55 +00:00
|
|
|
return $response['body'];
|
2014-04-18 20:17:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* Run wikitext through the parser's Pre-Save-Transform
|
|
|
|
*
|
2018-06-26 15:35:09 +00:00
|
|
|
* @param Title $title The title of the page to use as the parsing context
|
2018-03-28 19:47:04 +00:00
|
|
|
* @param string $wikitext The wikitext to transform
|
|
|
|
* @return string The transformed wikitext
|
|
|
|
*/
|
2018-06-26 15:35:09 +00:00
|
|
|
protected function pstWikitext( Title $title, $wikitext ) {
|
2015-03-17 20:43:03 +00:00
|
|
|
return ContentHandler::makeContent( $wikitext, $title, CONTENT_MODEL_WIKITEXT )
|
2014-10-22 22:47:21 +00:00
|
|
|
->preSaveTransform(
|
|
|
|
$title,
|
|
|
|
$this->getUser(),
|
|
|
|
WikiPage::factory( $title )->makeParserOptions( $this->getContext() )
|
|
|
|
)
|
|
|
|
->serialize( 'text/x-wiki' );
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* Provide the RESTbase-parsed HTML of a given fragment of wikitext
|
|
|
|
*
|
2018-04-03 02:28:08 +00:00
|
|
|
* @param Title $title The title of the page to use as the parsing context
|
2018-03-28 19:47:04 +00:00
|
|
|
* @param string $wikitext The wikitext fragment to parse
|
|
|
|
* @param bool $bodyOnly Whether to provide only the contents of the `<body>` tag
|
|
|
|
* @return string The parsed content HTML
|
|
|
|
*/
|
2018-04-03 02:28:08 +00:00
|
|
|
protected function parseWikitextFragment( Title $title, $wikitext, $bodyOnly ) {
|
2015-06-13 04:54:23 +00:00
|
|
|
return $this->requestRestbase(
|
2018-06-26 15:14:09 +00:00
|
|
|
$title,
|
2015-01-07 21:56:19 +00:00
|
|
|
'POST',
|
|
|
|
'transform/wikitext/to/html/' . urlencode( $title->getPrefixedDBkey() ),
|
2016-02-17 16:18:02 +00:00
|
|
|
[
|
2014-12-18 00:49:55 +00:00
|
|
|
'wikitext' => $wikitext,
|
2017-08-13 19:22:04 +00:00
|
|
|
'body_only' => $bodyOnly ? 1 : 0,
|
2016-02-17 16:18:02 +00:00
|
|
|
]
|
2014-04-18 20:19:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* Provide the preload content for a page being created from another page
|
|
|
|
*
|
|
|
|
* @param string $preload The title of the page to use as the preload content
|
|
|
|
* @param string[] $params The preloadTransform parameters to pass in, if any
|
2018-06-26 15:35:09 +00:00
|
|
|
* @param Title $contextTitle The contextual page title against which to parse the preload
|
2018-03-28 19:47:04 +00:00
|
|
|
* @param bool $parse Whether to parse the preload content
|
|
|
|
* @return string The parsed content
|
|
|
|
*/
|
2018-06-26 15:35:09 +00:00
|
|
|
protected function getPreloadContent( $preload, $params, Title $contextTitle, $parse = false ) {
|
2017-09-25 15:27:33 +00:00
|
|
|
$content = '';
|
|
|
|
$preloadTitle = Title::newFromText( $preload );
|
|
|
|
// Check for existence to avoid getting MediaWiki:Noarticletext
|
|
|
|
if ( $preloadTitle instanceof Title &&
|
|
|
|
$preloadTitle->exists() &&
|
|
|
|
$preloadTitle->userCan( 'read' )
|
|
|
|
) {
|
|
|
|
$preloadPage = WikiPage::factory( $preloadTitle );
|
|
|
|
if ( $preloadPage->isRedirect() ) {
|
|
|
|
$preloadTitle = $preloadPage->getRedirectTarget();
|
|
|
|
$preloadPage = WikiPage::factory( $preloadTitle );
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $preloadPage->getContent( Revision::RAW );
|
|
|
|
$parserOptions = ParserOptions::newFromUser( $this->getUser() );
|
|
|
|
|
|
|
|
$content = $content->preloadTransform(
|
|
|
|
$preloadTitle,
|
|
|
|
$parserOptions,
|
2017-09-25 15:56:42 +00:00
|
|
|
(array)$params
|
2017-09-25 15:27:33 +00:00
|
|
|
)->serialize();
|
|
|
|
|
|
|
|
if ( $parse ) {
|
|
|
|
// We need to turn this transformed wikitext into parsoid html
|
2018-02-09 20:40:15 +00:00
|
|
|
$content = $this->parseWikitextFragment( $contextTitle, $content, true );
|
2017-09-25 15:27:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2012-05-25 19:50:48 +00:00
|
|
|
public function execute() {
|
2017-05-02 18:42:28 +00:00
|
|
|
$this->serviceClient->mount( '/restbase/', $this->getVRSObject() );
|
|
|
|
|
2012-07-21 17:37:20 +00:00
|
|
|
$user = $this->getUser();
|
2012-05-25 19:50:48 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2014-03-21 00:44:30 +00:00
|
|
|
|
2015-03-26 19:32:01 +00:00
|
|
|
$title = Title::newFromText( $params['page'] );
|
2016-10-25 20:44:26 +00:00
|
|
|
if ( $title && $title->isSpecial( 'CollabPad' ) ) {
|
|
|
|
// Convert Special:CollabPad/MyPage to MyPage so we can parsefragment properly
|
2018-09-16 18:53:24 +00:00
|
|
|
$title = SpecialCollabPad::getSubPage( $title );
|
2016-10-25 20:44:26 +00:00
|
|
|
}
|
2015-03-26 19:32:01 +00:00
|
|
|
if ( !$title ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['page'] ) ] );
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
2015-04-02 00:41:59 +00:00
|
|
|
|
2016-02-17 16:18:02 +00:00
|
|
|
$parserParams = [];
|
2013-05-15 23:51:11 +00:00
|
|
|
if ( isset( $params['oldid'] ) ) {
|
|
|
|
$parserParams['oldid'] = $params['oldid'];
|
|
|
|
}
|
2012-08-23 19:01:07 +00:00
|
|
|
|
2015-03-26 19:32:01 +00:00
|
|
|
wfDebugLog( 'visualeditor', "called on '$title' with paction: '{$params['paction']}'" );
|
2013-05-15 21:17:06 +00:00
|
|
|
switch ( $params['paction'] ) {
|
|
|
|
case 'parse':
|
2016-05-26 12:08:26 +00:00
|
|
|
case 'wikitext':
|
2015-03-13 22:15:17 +00:00
|
|
|
case 'metadata':
|
2013-05-15 21:17:06 +00:00
|
|
|
// Dirty hack to provide the correct context for edit notices
|
2017-05-04 22:27:27 +00:00
|
|
|
// FIXME Don't write to globals! Eww.
|
|
|
|
global $wgTitle;
|
2015-03-26 19:32:01 +00:00
|
|
|
$wgTitle = $title;
|
|
|
|
RequestContext::getMain()->setTitle( $title );
|
2015-03-13 22:15:17 +00:00
|
|
|
|
2017-10-26 16:32:12 +00:00
|
|
|
$preloaded = false;
|
|
|
|
|
2015-03-13 22:15:17 +00:00
|
|
|
// Get information about current revision
|
|
|
|
if ( $title->exists() ) {
|
|
|
|
$latestRevision = Revision::newFromTitle( $title );
|
|
|
|
if ( $latestRevision === null ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'apierror-visualeditor-latestnotfound', 'latestnotfound' );
|
2015-03-13 22:15:17 +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 ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( [ 'apierror-nosuchrevid', $parserParams['oldid'] ], 'oldidnotfound' );
|
2015-03-13 22:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$restoring = $revision && !$revision->isCurrent();
|
|
|
|
$baseTimestamp = $latestRevision->getTimestamp();
|
|
|
|
$oldid = intval( $parserParams['oldid'] );
|
|
|
|
|
2015-06-13 04:54:23 +00:00
|
|
|
// If requested, request HTML from Parsoid/RESTBase
|
2015-03-13 22:15:17 +00:00
|
|
|
if ( $params['paction'] === 'parse' ) {
|
2015-06-13 04:54:23 +00:00
|
|
|
$content = $this->requestRestbase(
|
2018-06-26 15:14:09 +00:00
|
|
|
$title,
|
2015-03-13 22:15:17 +00:00
|
|
|
'GET',
|
2016-04-09 02:34:34 +00:00
|
|
|
'page/html/' . urlencode( $title->getPrefixedDBkey() ) . '/' . $oldid . '?redirect=false',
|
2016-02-17 16:18:02 +00:00
|
|
|
[]
|
2015-03-13 22:15:17 +00:00
|
|
|
);
|
|
|
|
if ( $content === false ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'apierror-visualeditor-docserver', 'docserver' );
|
2015-03-13 22:15:17 +00:00
|
|
|
}
|
2016-05-26 12:08:26 +00:00
|
|
|
} elseif ( $params['paction'] === 'wikitext' ) {
|
|
|
|
$apiParams = [
|
|
|
|
'action' => 'query',
|
2016-11-23 00:47:42 +00:00
|
|
|
'revids' => $oldid,
|
2016-05-26 12:08:26 +00:00
|
|
|
'prop' => 'revisions',
|
2016-11-23 00:47:42 +00:00
|
|
|
'rvprop' => 'content|ids'
|
2016-05-26 12:08:26 +00:00
|
|
|
];
|
2016-09-06 19:16:55 +00:00
|
|
|
|
2019-03-12 20:27:47 +00:00
|
|
|
$section = $params['section'] ?? null;
|
2016-09-06 19:16:55 +00:00
|
|
|
|
2016-12-08 19:24:10 +00:00
|
|
|
if ( $section === 'new' ) {
|
|
|
|
$content = '';
|
2017-10-04 14:44:58 +00:00
|
|
|
if ( !empty( $params['preload'] ) ) {
|
2017-09-25 15:27:33 +00:00
|
|
|
$content = $this->getPreloadContent(
|
|
|
|
$params['preload'], $params['preloadparams'], $title,
|
|
|
|
$params['paction'] !== 'wikitext'
|
|
|
|
);
|
2017-10-26 16:32:12 +00:00
|
|
|
$preloaded = true;
|
2017-09-25 15:27:33 +00:00
|
|
|
}
|
2016-12-08 19:24:10 +00:00
|
|
|
} else {
|
|
|
|
$apiParams['rvsection'] = $section;
|
|
|
|
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
2017-05-04 22:27:27 +00:00
|
|
|
/* was posted? */ false
|
2016-12-08 19:24:10 +00:00
|
|
|
),
|
2017-05-04 22:27:27 +00:00
|
|
|
/* enable write? */ true
|
2016-12-08 19:24:10 +00:00
|
|
|
);
|
|
|
|
$api->execute();
|
|
|
|
$result = $api->getResult()->getResultData();
|
|
|
|
$pid = $title->getArticleID();
|
|
|
|
$content = false;
|
|
|
|
if ( isset( $result['query']['pages'][$pid]['revisions'] ) ) {
|
|
|
|
foreach ( $result['query']['pages'][$pid]['revisions'] as $revArr ) {
|
2018-05-25 18:06:02 +00:00
|
|
|
// Check 'revisions' is an array (T193718)
|
|
|
|
if ( is_array( $revArr ) && $revArr['revid'] === $oldid ) {
|
2016-12-08 19:24:10 +00:00
|
|
|
$content = $revArr['content'];
|
|
|
|
}
|
2016-11-23 00:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-08 19:24:10 +00:00
|
|
|
if ( $content === false ) {
|
|
|
|
$this->dieWithError( 'apierror-visualeditor-docserver', 'docserver' );
|
|
|
|
}
|
2016-05-26 12:08:26 +00:00
|
|
|
}
|
2015-03-13 22:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$content = '';
|
2016-06-24 12:46:52 +00:00
|
|
|
Hooks::run( 'EditFormPreloadText', [ &$content, &$title ] );
|
2018-10-11 15:32:19 +00:00
|
|
|
if ( $content !== '' && $params['paction'] !== 'wikitext' ) {
|
2017-08-13 19:22:04 +00:00
|
|
|
$content = $this->parseWikitextFragment( $title, $content, true );
|
2016-06-24 12:46:52 +00:00
|
|
|
}
|
2017-10-04 14:44:58 +00:00
|
|
|
if ( $content === '' && !empty( $params['preload'] ) ) {
|
2017-09-25 15:27:33 +00:00
|
|
|
$content = $this->getPreloadContent(
|
|
|
|
$params['preload'], $params['preloadparams'], $title,
|
|
|
|
$params['paction'] !== 'wikitext'
|
|
|
|
);
|
2017-10-26 16:32:12 +00:00
|
|
|
$preloaded = true;
|
2017-09-14 20:08:09 +00:00
|
|
|
}
|
2015-03-13 22:15:17 +00:00
|
|
|
$baseTimestamp = wfTimestampNow();
|
|
|
|
$oldid = 0;
|
|
|
|
$restoring = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get edit notices
|
2015-03-26 19:32:01 +00:00
|
|
|
$notices = $title->getEditNotices();
|
2015-03-13 22:15:17 +00:00
|
|
|
|
|
|
|
// Anonymous user notice
|
2013-05-15 21:17:06 +00:00
|
|
|
if ( $user->isAnon() ) {
|
2014-09-02 18:56:55 +00:00
|
|
|
$notices[] = $this->msg(
|
|
|
|
'anoneditwarning',
|
|
|
|
// Log-in link
|
|
|
|
'{{fullurl:Special:UserLogin|returnto={{FULLPAGENAMEE}}}}',
|
|
|
|
// Sign-up link
|
|
|
|
'{{fullurl:Special:UserLogin/signup|returnto={{FULLPAGENAMEE}}}}'
|
|
|
|
)->parseAsBlock();
|
2012-12-11 03:40:09 +00:00
|
|
|
}
|
2015-03-13 22:15:17 +00:00
|
|
|
|
2016-12-31 17:02:33 +00:00
|
|
|
// From EditPage#showCustomIntro
|
|
|
|
if ( $params['editintro'] ) {
|
|
|
|
$eiTitle = Title::newFromText( $params['editintro'] );
|
|
|
|
if ( $eiTitle instanceof Title && $eiTitle->exists() && $eiTitle->userCan( 'read' ) ) {
|
|
|
|
global $wgParser;
|
|
|
|
$notices[] = $wgParser->parse(
|
|
|
|
'<div class="mw-editintro">{{:' . $eiTitle->getFullText() . '}}</div>',
|
|
|
|
$title,
|
|
|
|
new ParserOptions()
|
|
|
|
)->getText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 22:15:17 +00:00
|
|
|
// Old revision notice
|
|
|
|
if ( $restoring ) {
|
2014-03-26 22:20:08 +00:00
|
|
|
$notices[] = $this->msg( 'editingold' )->parseAsBlock();
|
2014-03-26 21:33:19 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 18:58:41 +00:00
|
|
|
if ( wfReadOnly() ) {
|
|
|
|
$notices[] = $this->msg( 'readonlywarning', wfReadOnlyReason() );
|
|
|
|
}
|
|
|
|
|
2015-03-13 22:15:17 +00:00
|
|
|
// New page notices
|
2015-03-26 19:32:01 +00:00
|
|
|
if ( !$title->exists() ) {
|
2015-07-02 10:25:19 +00:00
|
|
|
$notices[] = $this->msg(
|
2014-03-26 21:33:19 +00:00
|
|
|
$user->isLoggedIn() ? 'newarticletext' : 'newarticletextanon',
|
2015-05-06 20:01:59 +00:00
|
|
|
wfExpandUrl( Skin::makeInternalOrExternalUrl(
|
2014-03-26 21:33:19 +00:00
|
|
|
$this->msg( 'helppage' )->inContentLanguage()->text()
|
2015-05-06 20:01:59 +00:00
|
|
|
) )
|
2014-03-26 21:33:19 +00:00
|
|
|
)->parseAsBlock();
|
2013-07-24 09:43:21 +00:00
|
|
|
// Page protected from creation
|
2015-03-26 19:32:01 +00:00
|
|
|
if ( $title->getRestrictions( 'create' ) ) {
|
2014-03-26 21:33:19 +00:00
|
|
|
$notices[] = $this->msg( 'titleprotectedwarning' )->parseAsBlock();
|
2013-05-15 21:17:06 +00:00
|
|
|
}
|
2018-07-12 16:34:22 +00:00
|
|
|
// From EditPage#showIntro, checking if the page has previously been deleted:
|
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
|
|
|
LogEventsList::showLogExtract( $out, [ 'delete', 'move' ], $title,
|
|
|
|
'',
|
|
|
|
[
|
|
|
|
'lim' => 10,
|
|
|
|
'conds' => [ 'log_action != ' . $dbr->addQuotes( 'revision' ) ],
|
|
|
|
'showIfEmpty' => false,
|
|
|
|
'msgKey' => [ 'recreate-moveddeleted-warn' ]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
if ( $out ) {
|
|
|
|
$notices[] = $out;
|
|
|
|
}
|
2013-05-15 21:17:06 +00:00
|
|
|
}
|
2014-03-26 21:33:19 +00:00
|
|
|
|
2014-04-30 19:28:29 +00:00
|
|
|
// Look at protection status to set up notices + surface class(es)
|
2016-02-17 16:18:02 +00:00
|
|
|
$protectedClasses = [];
|
|
|
|
if ( MWNamespace::getRestrictionLevels( $title->getNamespace() ) !== [ '' ] ) {
|
2014-04-16 23:16:06 +00:00
|
|
|
// Page protected from editing
|
2015-03-26 19:32:01 +00:00
|
|
|
if ( $title->isProtected( 'edit' ) ) {
|
2017-09-18 19:46:38 +00:00
|
|
|
// Is the title semi-protected?
|
2015-03-26 19:32:01 +00:00
|
|
|
if ( $title->isSemiProtected() ) {
|
2014-04-30 19:28:29 +00:00
|
|
|
$protectedClasses[] = 'mw-textarea-sprotected';
|
|
|
|
|
2014-04-16 23:16:06 +00:00
|
|
|
$noticeMsg = 'semiprotectedpagewarning';
|
|
|
|
} else {
|
2014-04-30 19:28:29 +00:00
|
|
|
$protectedClasses[] = 'mw-textarea-protected';
|
|
|
|
|
2017-09-18 19:46:38 +00:00
|
|
|
// Then it must be protected based on static groups (regular)
|
2014-04-16 23:16:06 +00:00
|
|
|
$noticeMsg = 'protectedpagewarning';
|
|
|
|
}
|
|
|
|
$notices[] = $this->msg( $noticeMsg )->parseAsBlock() .
|
2015-03-26 19:32:01 +00:00
|
|
|
$this->getLastLogEntry( $title, 'protect' );
|
2014-03-17 22:11:01 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 23:16:06 +00:00
|
|
|
// Deal with cascading edit protection
|
2015-03-26 19:32:01 +00:00
|
|
|
list( $sources, $restrictions ) = $title->getCascadeProtectionSources();
|
2014-04-16 23:16:06 +00:00
|
|
|
if ( isset( $restrictions['edit'] ) ) {
|
2014-04-30 19:28:29 +00:00
|
|
|
$protectedClasses[] = ' mw-textarea-cprotected';
|
|
|
|
|
2014-04-16 23:16:06 +00:00
|
|
|
$notice = $this->msg( 'cascadeprotectedwarning' )->parseAsBlock() . '<ul>';
|
|
|
|
// Unfortunately there's no nice way to get only the pages which cause
|
|
|
|
// editing to be restricted
|
|
|
|
foreach ( $sources as $source ) {
|
2016-12-31 04:37:10 +00:00
|
|
|
$notice .= "<li>" .
|
|
|
|
MediaWikiServices::getInstance()->getLinkRenderer()->makeLink( $source ) .
|
|
|
|
"</li>";
|
2014-04-16 23:16:06 +00:00
|
|
|
}
|
|
|
|
$notice .= '</ul>';
|
|
|
|
$notices[] = $notice;
|
|
|
|
}
|
2014-03-26 20:39:45 +00:00
|
|
|
}
|
|
|
|
|
2015-03-13 22:15:17 +00:00
|
|
|
// Permission notice
|
2016-08-28 17:16:08 +00:00
|
|
|
$permErrors = $title->getUserPermissionsErrors( 'create', $user, 'quick' );
|
2015-11-05 02:55:24 +00:00
|
|
|
if ( $permErrors && !$title->exists() ) {
|
2014-07-05 19:09:23 +00:00
|
|
|
$notices[] = $this->msg(
|
|
|
|
'permissionserrorstext-withaction', 1, $this->msg( 'action-createpage' )
|
2016-02-17 16:18:02 +00:00
|
|
|
) . "<br>" . call_user_func_array( [ $this, 'msg' ], $permErrors[0] )->parse();
|
2014-07-05 19:09:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-26 20:39:45 +00:00
|
|
|
// Show notice when editing user / user talk page of a user that doesn't exist
|
|
|
|
// or who is blocked
|
|
|
|
// HACK of course this code is partly duplicated from EditPage.php :(
|
2015-03-26 19:32:01 +00:00
|
|
|
if ( $title->getNamespace() == NS_USER || $title->getNamespace() == NS_USER_TALK ) {
|
|
|
|
$parts = explode( '/', $title->getText(), 2 );
|
2014-03-26 20:39:45 +00:00
|
|
|
$targetUsername = $parts[0];
|
2017-05-04 22:27:27 +00:00
|
|
|
$targetUser = User::newFromName(
|
|
|
|
$targetUsername,
|
|
|
|
/* allow IP users*/ false
|
|
|
|
);
|
2018-12-05 05:35:38 +00:00
|
|
|
$block = $targetUser->getBlock();
|
2014-03-26 20:39:45 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
!( $targetUser && $targetUser->isLoggedIn() ) &&
|
|
|
|
!User::isIP( $targetUsername )
|
2017-05-04 22:27:27 +00:00
|
|
|
) {
|
|
|
|
// User does not exist
|
2014-03-26 20:39:45 +00:00
|
|
|
$notices[] = "<div class=\"mw-userpage-userdoesnotexist error\">\n" .
|
|
|
|
$this->msg( 'userpage-userdoesnotexist', wfEscapeWikiText( $targetUsername ) ) .
|
|
|
|
"\n</div>";
|
2018-12-05 05:35:38 +00:00
|
|
|
} elseif (
|
|
|
|
!is_null( $block ) &&
|
|
|
|
$block->getType() != Block::TYPE_AUTO &&
|
|
|
|
( $block->isSitewide() || $targetUser->isBlockedFrom( $title ) )
|
|
|
|
) {
|
|
|
|
// Show log extract if the user is sitewide blocked or is partially
|
|
|
|
// blocked and not allowed to edit their user page or user talk page
|
2014-03-26 20:39:45 +00:00
|
|
|
$notices[] = $this->msg(
|
|
|
|
'blocked-notice-logextract',
|
2017-05-04 22:27:27 +00:00
|
|
|
// Support GENDER in notice
|
|
|
|
$targetUser->getName()
|
2014-03-26 20:39:45 +00:00
|
|
|
)->parseAsBlock() . $this->getLastLogEntry( $targetUser->getUserPage(), 'block' );
|
|
|
|
}
|
2014-03-17 22:11:01 +00:00
|
|
|
}
|
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
|
|
|
|
2015-03-13 22:15:17 +00:00
|
|
|
// Blocked user notice
|
2018-11-07 22:01:54 +00:00
|
|
|
if ( $user->isBlockedFrom( $title, true ) || $user->isBlockedGlobally() ) {
|
|
|
|
if ( $user->isBlockedFrom( $title, true ) ) {
|
2018-11-26 23:33:59 +00:00
|
|
|
$notices[] = [
|
|
|
|
'type' => 'block',
|
|
|
|
'message' => call_user_func_array(
|
|
|
|
[ $this, 'msg' ],
|
|
|
|
$user->getBlock()->getPermissionsError( $this->getContext() )
|
|
|
|
)->parseAsBlock(),
|
|
|
|
];
|
2018-11-07 22:01:54 +00:00
|
|
|
}
|
2014-03-17 21:22:41 +00:00
|
|
|
|
2018-11-07 22:01:54 +00:00
|
|
|
if ( $user->isBlockedGlobally() ) {
|
2018-11-26 23:33:59 +00:00
|
|
|
$notices[] = [
|
|
|
|
'type' => 'block',
|
|
|
|
'message' => call_user_func_array(
|
|
|
|
[ $this, 'msg' ],
|
|
|
|
$user->getGlobalBlock()->getPermissionsError( $this->getContext() )
|
|
|
|
)->parseAsBlock(),
|
|
|
|
];
|
2018-11-07 22:01:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 23:33:59 +00:00
|
|
|
if ( $this->getConfig()->get( 'EnableBlockNoticeStats' ) ) {
|
2018-11-07 22:01:54 +00:00
|
|
|
$statsd = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
2018-11-26 23:33:59 +00:00
|
|
|
$wiki = $this->getConfig()->get( 'DBname' );
|
|
|
|
$statsd->increment( 'BlockNotices.' . $wiki . '.VisualEditor.returned' );
|
2018-11-07 22:01:54 +00:00
|
|
|
}
|
2014-04-22 00:22:48 +00:00
|
|
|
}
|
|
|
|
|
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
|
2017-05-04 22:27:27 +00:00
|
|
|
// Deliberately omitting ,0 so oldid comes from request
|
|
|
|
$article = new Article( $title );
|
2016-12-18 19:04:20 +00:00
|
|
|
$editPage = new EditPage( $article );
|
2013-08-02 18:37:30 +00:00
|
|
|
$req = $this->getRequest();
|
2016-12-18 19:04:20 +00:00
|
|
|
$req->setVal( 'format', $editPage->contentFormat );
|
2017-05-04 22:27:27 +00:00
|
|
|
// By reference for some reason (T54466)
|
|
|
|
$editPage->importFormData( $req );
|
2016-04-03 12:55:20 +00:00
|
|
|
$states = [
|
|
|
|
'minor' => $user->getOption( 'minordefault' ) && $title->exists(),
|
|
|
|
'watch' => $user->getOption( 'watchdefault' ) ||
|
|
|
|
( $user->getOption( 'watchcreations' ) && !$title->exists() ) ||
|
|
|
|
$user->isWatched( $title ),
|
|
|
|
];
|
2017-08-31 15:18:45 +00:00
|
|
|
$checkboxesDef = $editPage->getCheckboxesDefinition( $states );
|
2017-09-19 19:14:25 +00:00
|
|
|
$checkboxesMessagesList = [];
|
|
|
|
foreach ( $checkboxesDef as $name => &$options ) {
|
2017-08-31 15:18:45 +00:00
|
|
|
if ( isset( $options['tooltip'] ) ) {
|
2017-09-19 19:14:25 +00:00
|
|
|
$checkboxesMessagesList[] = "accesskey-{$options['tooltip']}";
|
|
|
|
$checkboxesMessagesList[] = "tooltip-{$options['tooltip']}";
|
2017-08-31 15:18:45 +00:00
|
|
|
}
|
|
|
|
if ( isset( $options['title-message'] ) ) {
|
2017-09-19 19:14:25 +00:00
|
|
|
$checkboxesMessagesList[] = $options['title-message'];
|
|
|
|
if ( !is_string( $options['title-message'] ) ) {
|
|
|
|
// Extract only the key. Any parameters are included in the fake message definition
|
|
|
|
// passed via $checkboxesMessages. (This changes $checkboxesDef by reference.)
|
|
|
|
$options['title-message'] = $this->msg( $options['title-message'] )->getKey();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$checkboxesMessagesList[] = $options['label-message'];
|
|
|
|
if ( !is_string( $options['label-message'] ) ) {
|
|
|
|
// Extract only the key. Any parameters are included in the fake message definition
|
|
|
|
// passed via $checkboxesMessages. (This changes $checkboxesDef by reference.)
|
|
|
|
$options['label-message'] = $this->msg( $options['label-message'] )->getKey();
|
2017-08-31 15:18:45 +00:00
|
|
|
}
|
2017-09-19 19:14:25 +00:00
|
|
|
}
|
|
|
|
$checkboxesMessages = [];
|
|
|
|
foreach ( $checkboxesMessagesList as $messageSpecifier ) {
|
|
|
|
// $messageSpecifier may be a string or a Message object
|
|
|
|
$message = $this->msg( $messageSpecifier );
|
|
|
|
$checkboxesMessages[ $message->getKey() ] = $message->plain();
|
2017-08-31 15:18:45 +00:00
|
|
|
}
|
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
|
|
|
|
2017-08-31 15:18:45 +00:00
|
|
|
foreach ( $checkboxesDef as &$value ) {
|
|
|
|
// Don't convert the boolean to empty string with formatversion=1
|
|
|
|
$value[ApiResult::META_BC_BOOLS] = [ 'default' ];
|
|
|
|
}
|
2016-02-17 16:18:02 +00:00
|
|
|
$result = [
|
2015-03-13 22:15:17 +00:00
|
|
|
'result' => 'success',
|
|
|
|
'notices' => $notices,
|
2017-08-31 15:18:45 +00:00
|
|
|
'checkboxesDef' => $checkboxesDef,
|
|
|
|
'checkboxesMessages' => $checkboxesMessages,
|
2015-03-13 22:15:17 +00:00
|
|
|
'protectedClasses' => implode( ' ', $protectedClasses ),
|
|
|
|
'basetimestamp' => $baseTimestamp,
|
|
|
|
'starttimestamp' => wfTimestampNow(),
|
|
|
|
'oldid' => $oldid,
|
|
|
|
|
2016-02-17 16:18:02 +00:00
|
|
|
];
|
2017-09-14 20:08:09 +00:00
|
|
|
if ( $params['paction'] === 'parse' ||
|
|
|
|
$params['paction'] === 'wikitext' ||
|
2017-10-05 05:43:48 +00:00
|
|
|
( !empty( $params['preload'] ) && isset( $content ) )
|
2017-09-14 20:08:09 +00:00
|
|
|
) {
|
2015-03-13 22:15:17 +00:00
|
|
|
$result['content'] = $content;
|
2017-10-26 16:32:12 +00:00
|
|
|
if ( $preloaded ) {
|
|
|
|
// If the preload param was actually used, pass it
|
|
|
|
// back so the caller knows. (It's not obvious to the
|
|
|
|
// caller, because in some situations it'll depend on
|
|
|
|
// whether the page has been created. They can work it
|
|
|
|
// out from some of the other returns, but this is
|
|
|
|
// simpler.)
|
|
|
|
$result['preloaded'] = $params['preload'];
|
|
|
|
}
|
2013-05-15 21:17:06 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-12-04 15:30:21 +00:00
|
|
|
|
2019-02-22 18:50:46 +00:00
|
|
|
case 'templatesused':
|
|
|
|
// HACK: Build a fake EditPage so we can get checkboxes from it
|
|
|
|
// Deliberately omitting ,0 so oldid comes from request
|
|
|
|
$article = new Article( $title );
|
|
|
|
$editPage = new EditPage( $article );
|
|
|
|
$result = $editPage->makeTemplatesOnThisPageList( $editPage->getTemplates() );
|
|
|
|
break;
|
|
|
|
|
2017-08-13 19:22:04 +00:00
|
|
|
case 'parsedoc':
|
2013-05-15 23:51:11 +00:00
|
|
|
case 'parsefragment':
|
2014-10-22 22:47:21 +00:00
|
|
|
$wikitext = $params['wikitext'];
|
2017-08-13 19:22:04 +00:00
|
|
|
$bodyOnly = ( $params['paction'] === 'parsefragment' );
|
2014-10-22 22:47:21 +00:00
|
|
|
if ( $params['pst'] ) {
|
2015-03-26 19:32:01 +00:00
|
|
|
$wikitext = $this->pstWikitext( $title, $wikitext );
|
2014-10-22 22:47:21 +00:00
|
|
|
}
|
2017-08-13 19:22:04 +00:00
|
|
|
$content = $this->parseWikitextFragment(
|
|
|
|
$title, $wikitext, $bodyOnly
|
|
|
|
);
|
2013-05-15 23:51:11 +00:00
|
|
|
if ( $content === false ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( 'apierror-visualeditor-docserver', 'docserver' );
|
2013-05-15 23:51:11 +00:00
|
|
|
} else {
|
2016-02-17 16:18:02 +00:00
|
|
|
$result = [
|
2013-05-15 23:51:11 +00:00
|
|
|
'result' => 'success',
|
|
|
|
'content' => $content
|
2016-02-17 16:18:02 +00:00
|
|
|
];
|
2013-05-15 23:51:11 +00:00
|
|
|
}
|
|
|
|
break;
|
2012-05-25 19:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
2016-04-29 16:00:57 +00:00
|
|
|
/**
|
|
|
|
* Check if the configured allowed namespaces include the specified namespace
|
|
|
|
*
|
|
|
|
* @param Config $config Configuration object
|
|
|
|
* @param int $namespaceId Namespace ID
|
2017-07-25 14:49:56 +00:00
|
|
|
* @return bool
|
2016-04-29 16:00:57 +00:00
|
|
|
*/
|
|
|
|
public static function isAllowedNamespace( Config $config, $namespaceId ) {
|
2016-06-01 16:06:22 +00:00
|
|
|
$availableNamespaces = self::getAvailableNamespaceIds( $config );
|
|
|
|
return in_array( $namespaceId, $availableNamespaces );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of allowed namespace IDs
|
|
|
|
*
|
|
|
|
* @param Config $config Configuration object
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getAvailableNamespaceIds( Config $config ) {
|
2016-06-29 21:10:45 +00:00
|
|
|
$availableNamespaces =
|
|
|
|
// Note: existing numeric keys might exist, and so array_merge cannot be used
|
2017-05-04 22:07:10 +00:00
|
|
|
(array)$config->get( 'VisualEditorAvailableNamespaces' ) +
|
|
|
|
(array)ExtensionRegistry::getInstance()->getAttribute( 'VisualEditorAvailableNamespaces' );
|
2016-06-30 19:34:29 +00:00
|
|
|
return array_values( array_unique( array_map( function ( $namespace ) {
|
2016-06-01 16:06:22 +00:00
|
|
|
// Convert canonical namespace names to IDs
|
|
|
|
return is_numeric( $namespace ) ?
|
|
|
|
$namespace :
|
|
|
|
MWNamespace::getCanonicalIndex( strtolower( $namespace ) );
|
2016-06-30 19:34:29 +00:00
|
|
|
}, array_keys( array_filter( $availableNamespaces ) ) ) ) );
|
2016-04-21 11:40:42 +00:00
|
|
|
}
|
2016-04-29 16:00:57 +00:00
|
|
|
|
2016-04-21 11:40:42 +00:00
|
|
|
/**
|
|
|
|
* Check if the configured allowed content models include the specified content model
|
|
|
|
*
|
|
|
|
* @param Config $config Configuration object
|
|
|
|
* @param string $contentModel Content model ID
|
2017-07-25 14:49:56 +00:00
|
|
|
* @return bool
|
2016-04-21 11:40:42 +00:00
|
|
|
*/
|
|
|
|
public static function isAllowedContentType( Config $config, $contentModel ) {
|
2016-06-01 16:06:22 +00:00
|
|
|
$availableContentModels = array_merge(
|
|
|
|
ExtensionRegistry::getInstance()->getAttribute( 'VisualEditorAvailableContentModels' ),
|
|
|
|
$config->get( 'VisualEditorAvailableContentModels' )
|
|
|
|
);
|
2017-12-30 22:29:40 +00:00
|
|
|
return isset( $availableContentModels[$contentModel] ) &&
|
|
|
|
$availableContentModels[$contentModel];
|
2016-04-29 16:00:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 15:14:09 +00:00
|
|
|
/**
|
|
|
|
* Get the page language from a title, using the content language as fallback on special pages
|
|
|
|
* @param Title $title Title
|
|
|
|
* @return Language Content language
|
|
|
|
*/
|
|
|
|
public static function getPageLanguage( Title $title ) {
|
|
|
|
if ( $title->isSpecial( 'CollabPad' ) ) {
|
|
|
|
// Use the site language for CollabPad, as getPageLanguage just
|
|
|
|
// returns the interface language for special pages.
|
|
|
|
// TODO: Let the user change the document language on multi-lingual sites.
|
2018-08-13 23:11:35 +00:00
|
|
|
return MediaWikiServices::getInstance()->getContentLanguage();
|
2018-06-26 15:14:09 +00:00
|
|
|
} else {
|
|
|
|
return $title->getPageLanguage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-26 20:39:45 +00:00
|
|
|
/**
|
|
|
|
* Gets the relevant HTML for the latest log entry on a given title, including a full log link.
|
|
|
|
*
|
2018-06-26 15:35:09 +00:00
|
|
|
* @param Title $title Title
|
2014-03-26 20:39:45 +00:00
|
|
|
* @param $types array|string
|
2014-08-17 19:43:56 +00:00
|
|
|
* @return string
|
2014-03-26 20:39:45 +00:00
|
|
|
*/
|
2018-06-26 15:35:09 +00:00
|
|
|
private function getLastLogEntry( Title $title, $types = '' ) {
|
2018-11-05 21:17:14 +00:00
|
|
|
$outString = '';
|
|
|
|
LogEventsList::showLogExtract( $outString, $types, $title, '',
|
|
|
|
[ 'lim' => 1 ] );
|
|
|
|
return $outString;
|
2014-03-26 20:39:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2012-05-25 19:50:48 +00:00
|
|
|
public function getAllowedParams() {
|
2016-02-17 16:18:02 +00:00
|
|
|
return [
|
|
|
|
'page' => [
|
2012-05-25 19:50:48 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2016-02-17 16:18:02 +00:00
|
|
|
],
|
|
|
|
'format' => [
|
2015-02-03 18:24:51 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'jsonfm',
|
2016-02-17 16:18:02 +00:00
|
|
|
ApiBase::PARAM_TYPE => [ 'json', 'jsonfm' ],
|
|
|
|
],
|
|
|
|
'paction' => [
|
2012-05-25 19:50:48 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2016-02-17 16:18:02 +00:00
|
|
|
ApiBase::PARAM_TYPE => [
|
2014-03-21 00:44:30 +00:00
|
|
|
'parse',
|
2015-03-13 22:15:17 +00:00
|
|
|
'metadata',
|
2019-02-22 18:50:46 +00:00
|
|
|
'templatesused',
|
2016-05-26 12:08:26 +00:00
|
|
|
'wikitext',
|
2014-03-21 00:44:30 +00:00
|
|
|
'parsefragment',
|
2017-08-13 19:22:04 +00:00
|
|
|
'parsedoc',
|
2016-02-17 16:18:02 +00:00
|
|
|
],
|
|
|
|
],
|
2013-05-15 23:51:11 +00:00
|
|
|
'wikitext' => null,
|
2016-09-06 19:16:55 +00:00
|
|
|
'section' => null,
|
2013-05-15 23:51:11 +00:00
|
|
|
'oldid' => null,
|
2016-12-31 17:02:33 +00:00
|
|
|
'editintro' => null,
|
2014-10-22 22:47:21 +00:00
|
|
|
'pst' => false,
|
2017-09-14 20:08:09 +00:00
|
|
|
'preload' => null,
|
2017-09-25 15:56:42 +00:00
|
|
|
'preloadparams' => [
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
],
|
2016-02-17 16:18:02 +00:00
|
|
|
];
|
2012-05-25 19:50:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2012-11-14 18:33:57 +00:00
|
|
|
public function mustBePosted() {
|
2013-12-04 15:30:21 +00:00
|
|
|
return false;
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2014-10-14 00:20:50 +00:00
|
|
|
public function isInternal() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2012-11-14 18:33:57 +00:00
|
|
|
public function isWriteMode() {
|
2016-03-10 18:58:41 +00:00
|
|
|
return false;
|
2012-05-25 19:50:48 +00:00
|
|
|
}
|
|
|
|
}
|