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
|
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
|
2012-07-19 00:11:26 +00:00
|
|
|
*/
|
|
|
|
|
2019-05-31 14:34:00 +00:00
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
2019-10-01 22:17:36 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2020-01-10 17:51:36 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-07-03 00:40:27 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2016-12-31 04:37:10 +00:00
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
class ApiVisualEditor extends ApiBase {
|
2019-05-08 20:01:36 +00:00
|
|
|
|
|
|
|
use ApiBlockInfoTrait;
|
2020-05-19 17:53:29 +00:00
|
|
|
use ApiParsoidTrait;
|
2019-05-08 20:01:36 +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 ) {
|
2014-08-13 08:15:42 +00:00
|
|
|
parent::__construct( $main, $name );
|
2020-03-16 23:30:26 +00:00
|
|
|
$this->setLogger( LoggerFactory::getInstance( 'VisualEditor' ) );
|
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
|
2019-04-05 13:51:14 +00:00
|
|
|
* @param int|null $oldid What oldid revision, if any, to base the request from (default: `null`)
|
|
|
|
* @param bool $stash Whether to stash the result in the server-side cache (default: `false`)
|
|
|
|
* @return array The RESTbase server's response, 'code', 'reason', 'headers' and 'body'
|
2018-03-28 19:47:04 +00:00
|
|
|
*/
|
2019-04-05 13:51:14 +00:00
|
|
|
protected function parseWikitextFragment(
|
|
|
|
Title $title, $wikitext, $bodyOnly, $oldid = null, $stash = false
|
|
|
|
) {
|
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',
|
2019-04-05 13:51:14 +00:00
|
|
|
'transform/wikitext/to/html/' . urlencode( $title->getPrefixedDBkey() ) .
|
|
|
|
( $oldid === null ? '' : '/' . $oldid ),
|
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,
|
2019-04-05 13:51:14 +00:00
|
|
|
'stash' => $stash ? 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
|
2019-10-23 20:24:52 +00:00
|
|
|
* @return string Wikitext content
|
2018-03-28 19:47:04 +00:00
|
|
|
*/
|
2019-10-23 20:24:52 +00:00
|
|
|
protected function getPreloadContent( $preload, $params, Title $contextTitle ) {
|
2017-09-25 15:27:33 +00:00
|
|
|
$content = '';
|
|
|
|
$preloadTitle = Title::newFromText( $preload );
|
|
|
|
// Check for existence to avoid getting MediaWiki:Noarticletext
|
2020-01-30 00:54:42 +00:00
|
|
|
if (
|
|
|
|
$preloadTitle instanceof Title &&
|
|
|
|
$preloadTitle->exists() &&
|
|
|
|
$this->getPermissionManager()->userCan( 'read', $this->getUser(), $preloadTitle )
|
2017-09-25 15:27:33 +00:00
|
|
|
) {
|
|
|
|
$preloadPage = WikiPage::factory( $preloadTitle );
|
|
|
|
if ( $preloadPage->isRedirect() ) {
|
|
|
|
$preloadTitle = $preloadPage->getRedirectTarget();
|
|
|
|
$preloadPage = WikiPage::factory( $preloadTitle );
|
|
|
|
}
|
|
|
|
|
2020-07-03 00:40:27 +00:00
|
|
|
$content = $preloadPage->getContent( RevisionRecord::RAW );
|
2017-09-25 15:27:33 +00:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:47:04 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
2020-01-26 10:36:43 +00:00
|
|
|
* @suppress PhanPossiblyUndeclaredVariable False positives
|
2018-03-28 19:47:04 +00:00
|
|
|
*/
|
2012-05-25 19:50:48 +00:00
|
|
|
public function execute() {
|
2012-07-21 17:37:20 +00:00
|
|
|
$user = $this->getUser();
|
2012-05-25 19:50:48 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2020-01-30 00:54:42 +00:00
|
|
|
$permissionManager = $this->getPermissionManager();
|
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
|
|
|
}
|
2019-12-15 04:30:43 +00:00
|
|
|
'@phan-var Title $title';
|
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;
|
2019-05-14 15:07:22 +00:00
|
|
|
$restbaseHeaders = null;
|
2017-10-26 16:32:12 +00:00
|
|
|
|
2015-03-13 22:15:17 +00:00
|
|
|
// Get information about current revision
|
|
|
|
if ( $title->exists() ) {
|
2020-06-10 15:03:26 +00:00
|
|
|
$revision = $this->getValidRevision( $title, $parserParams['oldid'] ?? null );
|
|
|
|
$latestRevision = $this->getLatestRevision( $title );
|
2015-03-13 22:15:17 +00:00
|
|
|
|
2020-06-10 15:03:26 +00:00
|
|
|
$restoring = !$revision->isCurrent();
|
2015-03-13 22:15:17 +00:00
|
|
|
$baseTimestamp = $latestRevision->getTimestamp();
|
2020-06-10 15:03:26 +00:00
|
|
|
$oldid = $revision->getId();
|
2015-03-13 22:15:17 +00:00
|
|
|
|
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' ) {
|
2019-04-05 13:51:14 +00:00
|
|
|
$wikitext = $params['wikitext'] ?? null;
|
|
|
|
if ( $wikitext !== null ) {
|
|
|
|
$stash = $params['stash'];
|
2019-12-18 07:16:31 +00:00
|
|
|
$section = $params['section'] ?? null;
|
2019-04-05 13:51:14 +00:00
|
|
|
if ( $params['pst'] ) {
|
|
|
|
$wikitext = $this->pstWikitext( $title, $wikitext );
|
|
|
|
}
|
|
|
|
if ( $section !== null ) {
|
|
|
|
$sectionContent = new WikitextContent( $wikitext );
|
|
|
|
$page = WikiPage::factory( $title );
|
2019-05-31 13:46:11 +00:00
|
|
|
$newSectionContent = $page->replaceSectionAtRev(
|
2019-04-05 13:51:14 +00:00
|
|
|
$section, $sectionContent, '', $oldid
|
2019-05-31 13:46:11 +00:00
|
|
|
);
|
|
|
|
'@phan-var WikitextContent $newSectionContent';
|
|
|
|
$wikitext = $newSectionContent->getText();
|
2019-04-05 13:51:14 +00:00
|
|
|
}
|
|
|
|
$response = $this->parseWikitextFragment(
|
|
|
|
$title, $wikitext, false, $oldid, $stash
|
|
|
|
);
|
|
|
|
} else {
|
2020-06-10 15:03:26 +00:00
|
|
|
$response = $this->requestRestbasePageHtml( $revision );
|
2019-04-05 13:51:14 +00:00
|
|
|
}
|
2019-10-14 20:11:36 +00:00
|
|
|
$content = $response['body'];
|
|
|
|
$restbaseHeaders = $response['headers'];
|
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(
|
2019-10-23 20:24:52 +00:00
|
|
|
$params['preload'], $params['preloadparams'], $title
|
2017-09-25 15:27:33 +00:00
|
|
|
);
|
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 {
|
2020-06-29 15:23:09 +00:00
|
|
|
if ( isset( $params['wikitext'] ) ) {
|
|
|
|
$content = $params['wikitext'];
|
|
|
|
if ( $params['pst'] ) {
|
|
|
|
$content = $this->pstWikitext( $title, $content );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$content = '';
|
2020-07-02 20:46:52 +00:00
|
|
|
if ( $title->getNamespace() == NS_MEDIAWIKI && $params['section'] !== 'new' ) {
|
|
|
|
// If this is a system message, get the default text.
|
|
|
|
$content = $title->getDefaultMessageText();
|
|
|
|
}
|
2020-06-29 15:23:09 +00:00
|
|
|
Hooks::run( 'EditFormPreloadText', [ &$content, &$title ] );
|
|
|
|
if ( $content === '' && !empty( $params['preload'] ) ) {
|
|
|
|
$content = $this->getPreloadContent(
|
|
|
|
$params['preload'], $params['preloadparams'], $title
|
|
|
|
);
|
|
|
|
$preloaded = true;
|
|
|
|
}
|
2017-09-14 20:08:09 +00:00
|
|
|
}
|
2020-06-29 15:23:09 +00:00
|
|
|
|
2019-10-23 20:24:52 +00:00
|
|
|
if ( $content !== '' && $params['paction'] !== 'wikitext' ) {
|
2019-10-28 12:42:00 +00:00
|
|
|
$response = $this->parseWikitextFragment( $title, $content, false, null, true );
|
2019-10-23 20:24:52 +00:00
|
|
|
$content = $response['body'];
|
|
|
|
$restbaseHeaders = $response['headers'];
|
|
|
|
}
|
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'] );
|
2020-01-30 00:54:42 +00:00
|
|
|
if (
|
|
|
|
$eiTitle instanceof Title &&
|
|
|
|
$eiTitle->exists() &&
|
|
|
|
$permissionManager->userCan( 'read', $user, $eiTitle )
|
|
|
|
) {
|
2019-10-28 20:10:07 +00:00
|
|
|
$notices[] = MediaWikiServices::getInstance()->getParser()->parse(
|
2016-12-31 17:02:33 +00:00
|
|
|
'<div class="mw-editintro">{{:' . $eiTitle->getFullText() . '}}</div>',
|
|
|
|
$title,
|
2020-03-04 04:57:20 +00:00
|
|
|
new ParserOptions( $user )
|
2016-12-31 17:02:33 +00:00
|
|
|
)->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() );
|
|
|
|
}
|
|
|
|
|
2020-01-16 23:39:08 +00:00
|
|
|
// Edit notices about the page being protected (only used when we're allowed to edit it;
|
|
|
|
// otherwise, a generic permission error is displayed via #getUserPermissionsErrors)
|
|
|
|
$protectionNotices = [];
|
|
|
|
|
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();
|
2020-01-16 23:39:08 +00:00
|
|
|
|
2013-07-24 09:43:21 +00:00
|
|
|
// Page protected from creation
|
2015-03-26 19:32:01 +00:00
|
|
|
if ( $title->getRestrictions( 'create' ) ) {
|
2020-01-16 23:39:08 +00:00
|
|
|
$protectionNotices[] = $this->msg( 'titleprotectedwarning' )->parseAsBlock() .
|
2020-01-16 23:22:32 +00:00
|
|
|
$this->getLastLogEntry( $title, 'protect' );
|
2013-05-15 21:17:06 +00:00
|
|
|
}
|
2020-01-16 23:39:08 +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 = [];
|
2020-01-30 00:54:42 +00:00
|
|
|
if (
|
|
|
|
$permissionManager->getNamespaceRestrictionLevels( $title->getNamespace() ) !== [ '' ]
|
2019-08-27 00:48:12 +00:00
|
|
|
) {
|
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';
|
|
|
|
}
|
2020-01-16 23:39:08 +00:00
|
|
|
$protectionNotices[] = $this->msg( $noticeMsg )->parseAsBlock() .
|
2020-01-16 23:22:32 +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';
|
|
|
|
|
2020-01-17 00:13:24 +00:00
|
|
|
$notice = $this->msg( 'cascadeprotectedwarning', count( $sources ) )->parseAsBlock() . '<ul>';
|
2014-04-16 23:16:06 +00:00
|
|
|
// 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>';
|
2020-01-16 23:39:08 +00:00
|
|
|
$protectionNotices[] = $notice;
|
2014-04-16 23:16:06 +00:00
|
|
|
}
|
2014-03-26 20:39:45 +00:00
|
|
|
}
|
|
|
|
|
ApiVisualEditor: Refactor edit notice code
* Query permission errors for 'edit' and 'create' in the same way,
and consistently with how MediaWiki core EditPage does it.
* Prefer using Title::getUserPermissionsErrors() instead of custom
code for user blocks. The result is the same, except for an extra
line like "You do not have permission to edit this page, for the
following reason:".
Note that this loses the 'type' => 'block' property on each notice,
which was previously used to track when a block notice was shown.
This was however removed in 96de1353d3abec99a2922416856c120c8ed98912,
and could probably be re-implemented by using the root 'blockinfo'
property anyway.
* When Title::getUserPermissionsErrors() returns multiple messages
(for example, you're blocked *and* the page is protected),
display them all in a list instead of only the first one, using
OutputPage::formatPermissionsErrorMessage().
That method returns wikitext, which we have to parse ourselves. This
is a bit silly, but I found this approach in SpecialChangeContentModel
in MediaWiki core, so it should be fine.
Change-Id: Ifaf95d8aab836e45665b1fbdf98dd1980a867d8c
2020-01-17 00:15:45 +00:00
|
|
|
// Simplified EditPage::getEditPermissionErrors()
|
2020-01-30 00:54:42 +00:00
|
|
|
$permErrors = $permissionManager->getPermissionErrors( 'edit', $user, $title, 'full' );
|
ApiVisualEditor: Refactor edit notice code
* Query permission errors for 'edit' and 'create' in the same way,
and consistently with how MediaWiki core EditPage does it.
* Prefer using Title::getUserPermissionsErrors() instead of custom
code for user blocks. The result is the same, except for an extra
line like "You do not have permission to edit this page, for the
following reason:".
Note that this loses the 'type' => 'block' property on each notice,
which was previously used to track when a block notice was shown.
This was however removed in 96de1353d3abec99a2922416856c120c8ed98912,
and could probably be re-implemented by using the root 'blockinfo'
property anyway.
* When Title::getUserPermissionsErrors() returns multiple messages
(for example, you're blocked *and* the page is protected),
display them all in a list instead of only the first one, using
OutputPage::formatPermissionsErrorMessage().
That method returns wikitext, which we have to parse ourselves. This
is a bit silly, but I found this approach in SpecialChangeContentModel
in MediaWiki core, so it should be fine.
Change-Id: Ifaf95d8aab836e45665b1fbdf98dd1980a867d8c
2020-01-17 00:15:45 +00:00
|
|
|
if ( !$title->exists() ) {
|
|
|
|
$permErrors = array_merge(
|
|
|
|
$permErrors,
|
|
|
|
wfArrayDiff2(
|
2020-01-30 00:54:42 +00:00
|
|
|
$permissionManager->getPermissionErrors( 'create', $user, $title, 'full' ),
|
ApiVisualEditor: Refactor edit notice code
* Query permission errors for 'edit' and 'create' in the same way,
and consistently with how MediaWiki core EditPage does it.
* Prefer using Title::getUserPermissionsErrors() instead of custom
code for user blocks. The result is the same, except for an extra
line like "You do not have permission to edit this page, for the
following reason:".
Note that this loses the 'type' => 'block' property on each notice,
which was previously used to track when a block notice was shown.
This was however removed in 96de1353d3abec99a2922416856c120c8ed98912,
and could probably be re-implemented by using the root 'blockinfo'
property anyway.
* When Title::getUserPermissionsErrors() returns multiple messages
(for example, you're blocked *and* the page is protected),
display them all in a list instead of only the first one, using
OutputPage::formatPermissionsErrorMessage().
That method returns wikitext, which we have to parse ourselves. This
is a bit silly, but I found this approach in SpecialChangeContentModel
in MediaWiki core, so it should be fine.
Change-Id: Ifaf95d8aab836e45665b1fbdf98dd1980a867d8c
2020-01-17 00:15:45 +00:00
|
|
|
$permErrors
|
|
|
|
)
|
|
|
|
);
|
2014-07-05 19:09:23 +00:00
|
|
|
}
|
|
|
|
|
ApiVisualEditor: Refactor edit notice code
* Query permission errors for 'edit' and 'create' in the same way,
and consistently with how MediaWiki core EditPage does it.
* Prefer using Title::getUserPermissionsErrors() instead of custom
code for user blocks. The result is the same, except for an extra
line like "You do not have permission to edit this page, for the
following reason:".
Note that this loses the 'type' => 'block' property on each notice,
which was previously used to track when a block notice was shown.
This was however removed in 96de1353d3abec99a2922416856c120c8ed98912,
and could probably be re-implemented by using the root 'blockinfo'
property anyway.
* When Title::getUserPermissionsErrors() returns multiple messages
(for example, you're blocked *and* the page is protected),
display them all in a list instead of only the first one, using
OutputPage::formatPermissionsErrorMessage().
That method returns wikitext, which we have to parse ourselves. This
is a bit silly, but I found this approach in SpecialChangeContentModel
in MediaWiki core, so it should be fine.
Change-Id: Ifaf95d8aab836e45665b1fbdf98dd1980a867d8c
2020-01-17 00:15:45 +00:00
|
|
|
if ( $permErrors ) {
|
|
|
|
// Show generic permission errors, including page protection, user blocks, etc.
|
|
|
|
$notice = $this->getOutput()->formatPermissionsErrorMessage( $permErrors, 'edit' );
|
|
|
|
// That method returns wikitext (eww), hack to get it parsed:
|
|
|
|
$notice = ( new RawMessage( '$1', [ $notice ] ) )->parseAsBlock();
|
|
|
|
$notices[] = $notice;
|
2020-01-16 23:39:08 +00:00
|
|
|
} elseif ( $protectionNotices ) {
|
|
|
|
// If we can edit, and the page is protected, then show the details about the protection
|
|
|
|
$notices = array_merge( $notices, $protectionNotices );
|
ApiVisualEditor: Refactor edit notice code
* Query permission errors for 'edit' and 'create' in the same way,
and consistently with how MediaWiki core EditPage does it.
* Prefer using Title::getUserPermissionsErrors() instead of custom
code for user blocks. The result is the same, except for an extra
line like "You do not have permission to edit this page, for the
following reason:".
Note that this loses the 'type' => 'block' property on each notice,
which was previously used to track when a block notice was shown.
This was however removed in 96de1353d3abec99a2922416856c120c8ed98912,
and could probably be re-implemented by using the root 'blockinfo'
property anyway.
* When Title::getUserPermissionsErrors() returns multiple messages
(for example, you're blocked *and* the page is protected),
display them all in a list instead of only the first one, using
OutputPage::formatPermissionsErrorMessage().
That method returns wikitext, which we have to parse ourselves. This
is a bit silly, but I found this approach in SpecialChangeContentModel
in MediaWiki core, so it should be fine.
Change-Id: Ifaf95d8aab836e45665b1fbdf98dd1980a867d8c
2020-01-17 00:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Will be false e.g. if user is blocked or page is protected
|
|
|
|
$canEdit = !$permErrors;
|
|
|
|
|
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();
|
2020-12-18 15:47:05 +00:00
|
|
|
$targetUserExists = ( $targetUser && $targetUser->isLoggedIn() );
|
|
|
|
if ( $targetUserExists && $targetUser->isHidden() &&
|
|
|
|
!$permissionManager->userHasRight( $user, 'hideuser' )
|
2017-05-04 22:27:27 +00:00
|
|
|
) {
|
2020-12-18 15:47:05 +00:00
|
|
|
// If the user exists, but is hidden, and the viewer cannot see hidden
|
|
|
|
// users, pretend like they don't exist at all. See T120883/T270453
|
|
|
|
$targetUserExists = false;
|
|
|
|
}
|
|
|
|
if ( !$targetUserExists && !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" .
|
2020-05-24 12:46:40 +00:00
|
|
|
$this->msg( 'userpage-userdoesnotexist', wfEscapeWikiText( $targetUsername ) )
|
|
|
|
->parse() .
|
2014-03-26 20:39:45 +00:00
|
|
|
"\n</div>";
|
2018-12-05 05:35:38 +00:00
|
|
|
} elseif (
|
2020-01-10 17:51:36 +00:00
|
|
|
$block !== null &&
|
2019-05-31 14:34:00 +00:00
|
|
|
$block->getType() != DatabaseBlock::TYPE_AUTO &&
|
2018-12-05 05:35:38 +00:00
|
|
|
( $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
|
|
|
|
2019-04-30 17:02:58 +00:00
|
|
|
$block = null;
|
|
|
|
$blockinfo = null;
|
2015-03-13 22:15:17 +00:00
|
|
|
// Blocked user notice
|
2019-04-30 17:02:58 +00:00
|
|
|
if ( $user->isBlockedGlobally() ) {
|
|
|
|
$block = $user->getGlobalBlock();
|
|
|
|
} elseif ( $permissionManager->isBlockedFrom( $user, $title, true ) ) {
|
|
|
|
$block = $user->getBlock();
|
|
|
|
}
|
|
|
|
if ( $block ) {
|
2020-01-30 00:54:42 +00:00
|
|
|
// Already added to $notices via #getPermissionErrors above.
|
ApiVisualEditor: Refactor edit notice code
* Query permission errors for 'edit' and 'create' in the same way,
and consistently with how MediaWiki core EditPage does it.
* Prefer using Title::getUserPermissionsErrors() instead of custom
code for user blocks. The result is the same, except for an extra
line like "You do not have permission to edit this page, for the
following reason:".
Note that this loses the 'type' => 'block' property on each notice,
which was previously used to track when a block notice was shown.
This was however removed in 96de1353d3abec99a2922416856c120c8ed98912,
and could probably be re-implemented by using the root 'blockinfo'
property anyway.
* When Title::getUserPermissionsErrors() returns multiple messages
(for example, you're blocked *and* the page is protected),
display them all in a list instead of only the first one, using
OutputPage::formatPermissionsErrorMessage().
That method returns wikitext, which we have to parse ourselves. This
is a bit silly, but I found this approach in SpecialChangeContentModel
in MediaWiki core, so it should be fine.
Change-Id: Ifaf95d8aab836e45665b1fbdf98dd1980a867d8c
2020-01-17 00:15:45 +00:00
|
|
|
// Add block info for MobileFrontend:
|
2019-05-09 10:22:29 +00:00
|
|
|
$blockinfo = $this->getBlockDetails( $block );
|
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,
|
2019-04-30 17:02:58 +00:00
|
|
|
'blockinfo' => $blockinfo,
|
2019-05-18 15:09:13 +00:00
|
|
|
'canEdit' => $canEdit,
|
2016-02-17 16:18:02 +00:00
|
|
|
];
|
2019-12-18 07:22:25 +00:00
|
|
|
if ( isset( $restbaseHeaders['etag'] ) ) {
|
2019-04-05 13:51:14 +00:00
|
|
|
$result['etag'] = $restbaseHeaders['etag'];
|
|
|
|
}
|
2019-10-14 19:49:02 +00:00
|
|
|
if ( isset( $params['badetag'] ) ) {
|
|
|
|
$badetag = $params['badetag'];
|
|
|
|
$goodetag = $result['etag'] ?? '';
|
2020-03-16 23:30:26 +00:00
|
|
|
$this->getLogger()->info(
|
2019-10-21 14:38:23 +00:00
|
|
|
__METHOD__ . ": Client reported bad ETag: {badetag}, expected: {goodetag}",
|
|
|
|
[
|
|
|
|
'badetag' => $badetag,
|
|
|
|
'goodetag' => $goodetag,
|
|
|
|
]
|
2019-10-14 19:49: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
|
2019-04-05 13:51:14 +00:00
|
|
|
)['body'];
|
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
|
2019-08-27 00:48:12 +00:00
|
|
|
$nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
|
|
|
|
$idFromName = $nsInfo->getCanonicalIndex( strtolower( $namespace ) );
|
2019-03-28 21:26:22 +00:00
|
|
|
if ( $idFromName !== null ) {
|
|
|
|
return $idFromName;
|
|
|
|
}
|
|
|
|
// Allow namespaces to be specified by ID as well
|
2019-08-27 00:48:12 +00:00
|
|
|
return $nsInfo->exists( $namespace ) ? $namespace : null;
|
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
|
|
|
}
|
|
|
|
|
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
|
2020-01-10 23:00:20 +00:00
|
|
|
* @param array|string $types
|
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
|
|
|
],
|
2019-10-14 19:49:02 +00:00
|
|
|
'badetag' => null,
|
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
|
|
|
],
|
|
|
|
],
|
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,
|
2019-04-05 13:51:14 +00:00
|
|
|
'stash' => 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
|
|
|
}
|
|
|
|
}
|