2012-05-25 19:50:48 +00:00
|
|
|
<?php
|
2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* Parsoid API wrapper.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
2014-01-05 12:05:05 +00:00
|
|
|
* @copyright 2011-2014 VisualEditor Team and others; see AUTHORS.txt
|
2012-07-19 00:11:26 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
class ApiVisualEditor extends ApiBase {
|
2013-07-05 07:56:28 +00:00
|
|
|
|
2014-08-13 08:15:42 +00:00
|
|
|
/**
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
protected $veConfig;
|
|
|
|
|
|
|
|
public function __construct( ApiMain $main, $name, Config $config ) {
|
|
|
|
parent::__construct( $main, $name );
|
|
|
|
$this->veConfig = $config;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:10:59 +00:00
|
|
|
/**
|
|
|
|
* Parsoid HTTP proxy configuration for MWHttpRequest
|
|
|
|
*/
|
|
|
|
protected function getProxyConf() {
|
2014-08-13 08:15:42 +00:00
|
|
|
$parsoidHTTPProxy = $this->veConfig->get( 'VisualEditorParsoidHTTPProxy' );
|
|
|
|
if ( $parsoidHTTPProxy ) {
|
|
|
|
return array( 'proxy' => $parsoidHTTPProxy );
|
2014-02-07 16:10:59 +00:00
|
|
|
} else {
|
|
|
|
return array( 'noProxy' => true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-18 20:17:02 +00:00
|
|
|
protected function requestParsoid( $method, $title, $params ) {
|
2014-08-13 08:15:42 +00:00
|
|
|
$url = $this->veConfig->get( 'VisualEditorParsoidURL' ) . '/' .
|
|
|
|
$this->veConfig->get( 'VisualEditorParsoidPrefix' ) . '/' .
|
2014-04-18 20:17:02 +00:00
|
|
|
urlencode( $title->getPrefixedDBkey() );
|
|
|
|
|
|
|
|
$data = array_merge(
|
|
|
|
$this->getProxyConf(),
|
|
|
|
array(
|
|
|
|
'method' => $method,
|
2014-08-13 08:15:42 +00:00
|
|
|
'timeout' => $this->veConfig->get( 'VisualEditorParsoidTimeout' ),
|
2014-04-18 20:17:02 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $method === 'POST' ) {
|
|
|
|
$data['postData'] = $params;
|
|
|
|
} else {
|
|
|
|
$url = wfAppendQuery( $url, $params );
|
|
|
|
}
|
|
|
|
|
|
|
|
$req = MWHttpRequest::factory( $url, $data );
|
|
|
|
// Forward cookies, but only if configured to do so and if there are read restrictions
|
2014-08-13 08:15:42 +00:00
|
|
|
if ( $this->veConfig->get( 'VisualEditorParsoidForwardCookies' )
|
|
|
|
&& !User::isEveryoneAllowed( 'read' )
|
|
|
|
) {
|
2014-04-18 20:17:02 +00:00
|
|
|
$req->setHeader( 'Cookie', $this->getRequest()->getHeader( 'Cookie' ) );
|
|
|
|
}
|
|
|
|
$status = $req->execute();
|
|
|
|
if ( $status->isOK() ) {
|
|
|
|
// Pass thru performance data from Parsoid to the client, unless the response was
|
|
|
|
// served directly from Varnish, in which case discard the value of the XPP header
|
|
|
|
// and use it to declare the cache hit instead.
|
|
|
|
$xCache = $req->getResponseHeader( 'X-Cache' );
|
|
|
|
if ( is_string( $xCache ) && strpos( $xCache, 'hit' ) !== false ) {
|
|
|
|
$xpp = 'cached-response=true';
|
|
|
|
} else {
|
|
|
|
$xpp = $req->getResponseHeader( 'X-Parsoid-Performance' );
|
|
|
|
}
|
|
|
|
if ( $xpp !== null ) {
|
|
|
|
$resp = $this->getRequest()->response();
|
|
|
|
$resp->header( 'X-Parsoid-Performance: ' . $xpp );
|
|
|
|
}
|
|
|
|
} elseif ( $status->isGood() ) {
|
|
|
|
$this->dieUsage( $req->getContent(), 'parsoidserver-http-' . $req->getStatus() );
|
|
|
|
} elseif ( $errors = $status->getErrorsByType( 'error' ) ) {
|
|
|
|
$error = $errors[0];
|
|
|
|
$code = $error['message'];
|
|
|
|
if ( count( $error['params'] ) ) {
|
|
|
|
$message = $error['params'][0];
|
|
|
|
} else {
|
|
|
|
$message = 'MWHttpRequest error';
|
|
|
|
}
|
|
|
|
$this->dieUsage( $message, 'parsoidserver-' . $code );
|
|
|
|
}
|
|
|
|
// TODO pass through X-Parsoid-Performance header, merge with getHTML above
|
|
|
|
return $req->getContent();
|
|
|
|
}
|
|
|
|
|
2012-11-14 18:33:57 +00:00
|
|
|
protected function getHTML( $title, $parserParams ) {
|
2013-05-24 14:48:27 +00:00
|
|
|
$restoring = false;
|
|
|
|
|
2012-11-28 23:57:00 +00:00
|
|
|
if ( $title->exists() ) {
|
2013-06-18 19:17:44 +00:00
|
|
|
$latestRevision = Revision::newFromTitle( $title );
|
2013-07-05 07:56:28 +00:00
|
|
|
if ( $latestRevision === null ) {
|
2012-11-28 23:57:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-07-05 07:56:28 +00:00
|
|
|
$revision = null;
|
|
|
|
if ( !isset( $parserParams['oldid'] ) || $parserParams['oldid'] === 0 ) {
|
|
|
|
$parserParams['oldid'] = $latestRevision->getId();
|
|
|
|
$revision = $latestRevision;
|
|
|
|
} else {
|
|
|
|
$revision = Revision::newFromId( $parserParams['oldid'] );
|
|
|
|
if ( $revision === null ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-05-24 14:48:27 +00:00
|
|
|
|
2013-07-05 07:56:28 +00:00
|
|
|
$restoring = $revision && !$revision->isCurrent();
|
|
|
|
$oldid = $parserParams['oldid'];
|
2013-03-05 17:24:51 +00:00
|
|
|
|
2014-04-18 20:17:02 +00:00
|
|
|
$content = $this->requestParsoid( 'GET', $title, $parserParams );
|
2013-03-05 17:24:51 +00:00
|
|
|
|
2012-12-04 21:04:19 +00:00
|
|
|
if ( $content === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-18 19:17:44 +00:00
|
|
|
$timestamp = $latestRevision->getTimestamp();
|
2012-11-28 23:57:00 +00:00
|
|
|
} else {
|
|
|
|
$content = '';
|
|
|
|
$timestamp = wfTimestampNow();
|
2013-07-05 07:56:28 +00:00
|
|
|
$oldid = 0;
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
2012-11-28 23:57:00 +00:00
|
|
|
return array(
|
2013-05-24 14:48:27 +00:00
|
|
|
'result' => array(
|
|
|
|
'content' => $content,
|
|
|
|
'basetimestamp' => $timestamp,
|
|
|
|
'starttimestamp' => wfTimestampNow(),
|
2013-07-05 07:56:28 +00:00
|
|
|
'oldid' => $oldid,
|
2013-05-24 14:48:27 +00:00
|
|
|
),
|
|
|
|
'restoring' => $restoring,
|
2012-11-14 18:33:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-11-01 21:30:22 +00:00
|
|
|
protected function storeInSerializationCache( $title, $oldid, $html ) {
|
2014-08-13 08:15:42 +00:00
|
|
|
global $wgMemc;
|
2013-11-01 21:30:22 +00:00
|
|
|
$content = $this->postHTML( $title, $html, array( 'oldid' => $oldid ) );
|
|
|
|
if ( $content === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$hash = md5( $content );
|
|
|
|
$key = wfMemcKey( 'visualeditor', 'serialization', $hash );
|
2014-08-13 08:15:42 +00:00
|
|
|
$wgMemc->set( $key, $content, $this->veConfig->get( 'VisualEditorSerializationCacheTimeout' ) );
|
2013-11-01 21:30:22 +00:00
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function trySerializationCache( $hash ) {
|
|
|
|
global $wgMemc;
|
|
|
|
$key = wfMemcKey( 'visualeditor', 'serialization', $hash );
|
|
|
|
return $wgMemc->get( $key );
|
|
|
|
}
|
|
|
|
|
2013-04-15 23:22:51 +00:00
|
|
|
protected function postHTML( $title, $html, $parserParams ) {
|
|
|
|
if ( $parserParams['oldid'] === 0 ) {
|
|
|
|
$parserParams['oldid'] = '';
|
|
|
|
}
|
2014-04-18 20:17:02 +00:00
|
|
|
return $this->requestParsoid( 'POST', $title,
|
|
|
|
array(
|
|
|
|
'content' => $html,
|
|
|
|
'oldid' => $parserParams['oldid'],
|
2012-11-15 01:16:13 +00:00
|
|
|
)
|
2012-11-14 18:33:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-04-18 20:19:54 +00:00
|
|
|
protected function parseWikitextFragment( $title, $wikitext ) {
|
|
|
|
return $this->requestParsoid( 'POST', $title,
|
|
|
|
array(
|
|
|
|
'wt' => $wikitext,
|
|
|
|
'body' => 1,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-11-14 18:33:57 +00:00
|
|
|
protected function parseWikitext( $title ) {
|
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'parse',
|
2014-01-12 14:23:49 +00:00
|
|
|
'page' => $title->getPrefixedDBkey(),
|
|
|
|
'prop' => 'text|revid|categorieshtml',
|
2012-11-14 18:33:57 +00:00
|
|
|
);
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
|
|
|
false // was posted?
|
|
|
|
),
|
|
|
|
true // enable write?
|
|
|
|
);
|
|
|
|
|
|
|
|
$api->execute();
|
|
|
|
$result = $api->getResultData();
|
2012-11-28 23:57:00 +00:00
|
|
|
$content = isset( $result['parse']['text']['*'] ) ? $result['parse']['text']['*'] : false;
|
2014-01-12 14:23:49 +00:00
|
|
|
$categorieshtml = isset( $result['parse']['categorieshtml']['*'] ) ?
|
|
|
|
$result['parse']['categorieshtml']['*'] : false;
|
2014-03-11 00:46:26 +00:00
|
|
|
$links = isset( $result['parse']['links'] ) ? $result['parse']['links'] : array();
|
2012-11-28 23:57:00 +00:00
|
|
|
$revision = Revision::newFromId( $result['parse']['revid'] );
|
|
|
|
$timestamp = $revision ? $revision->getTimestamp() : wfTimestampNow();
|
|
|
|
|
|
|
|
if ( $content === false || ( strlen( $content ) && $revision === null ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
'content' => $content,
|
2014-01-12 14:23:49 +00:00
|
|
|
'categorieshtml' => $categorieshtml,
|
2012-11-28 23:57:00 +00:00
|
|
|
'basetimestamp' => $timestamp,
|
|
|
|
'starttimestamp' => wfTimestampNow()
|
|
|
|
);
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function diffWikitext( $title, $wikitext ) {
|
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'query',
|
|
|
|
'prop' => 'revisions',
|
|
|
|
'titles' => $title->getPrefixedDBkey(),
|
2014-04-23 19:48:08 +00:00
|
|
|
'rvdifftotext' => ContentHandler::makeContent( $wikitext, $title )
|
|
|
|
->preSaveTransform(
|
|
|
|
$title,
|
|
|
|
$this->getUser(),
|
|
|
|
WikiPage::factory( $title )->makeParserOptions( $this->getContext() )
|
|
|
|
)
|
|
|
|
->serialize( 'text/x-wiki' )
|
2012-11-14 18:33:57 +00:00
|
|
|
);
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
|
|
|
false // was posted?
|
|
|
|
),
|
|
|
|
false // enable write?
|
|
|
|
);
|
|
|
|
$api->execute();
|
|
|
|
$result = $api->getResultData();
|
2012-12-07 16:23:23 +00:00
|
|
|
if ( !isset( $result['query']['pages'][$title->getArticleID()]['revisions'][0]['diff']['*'] ) ) {
|
2013-05-14 17:40:00 +00:00
|
|
|
return array( 'result' => 'fail' );
|
2012-12-07 16:23:23 +00:00
|
|
|
}
|
|
|
|
$diffRows = $result['query']['pages'][$title->getArticleID()]['revisions'][0]['diff']['*'];
|
|
|
|
|
2013-05-10 22:32:23 +00:00
|
|
|
if ( $diffRows !== '' ) {
|
|
|
|
$context = new DerivativeContext( $this->getContext() );
|
|
|
|
$context->setTitle( $title );
|
|
|
|
$engine = new DifferenceEngine( $context );
|
2013-05-14 17:40:00 +00:00
|
|
|
return array(
|
|
|
|
'result' => 'success',
|
|
|
|
'diff' => $engine->addHeader(
|
|
|
|
$diffRows,
|
|
|
|
wfMessage( 'currentrev' )->parse(),
|
|
|
|
wfMessage( 'yourtext' )->parse()
|
|
|
|
)
|
2013-05-10 22:32:23 +00:00
|
|
|
);
|
|
|
|
} else {
|
2013-05-14 17:40:00 +00:00
|
|
|
return array( 'result' => 'nochanges' );
|
2013-05-10 22:32:23 +00:00
|
|
|
}
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
2012-05-25 22:23:40 +00:00
|
|
|
|
2013-12-04 15:30:21 +00:00
|
|
|
protected function getLangLinks( $title ) {
|
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'query',
|
|
|
|
'prop' => 'langlinks',
|
|
|
|
'lllimit' => 500,
|
|
|
|
'titles' => $title->getPrefixedDBkey(),
|
|
|
|
'indexpageids' => 1,
|
|
|
|
);
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
|
|
|
false // was posted?
|
|
|
|
),
|
|
|
|
true // enable write?
|
|
|
|
);
|
|
|
|
|
|
|
|
$api->execute();
|
|
|
|
$result = $api->getResultData();
|
|
|
|
if ( !isset( $result['query']['pages'][$title->getArticleID()]['langlinks'] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$langlinks = $result['query']['pages'][$title->getArticleID()]['langlinks'];
|
|
|
|
$langnames = Language::fetchLanguageNames();
|
|
|
|
foreach ( $langlinks as $i => $lang ) {
|
|
|
|
$langlinks[$i]['langname'] = $langnames[$langlinks[$i]['lang']];
|
|
|
|
}
|
|
|
|
return $langlinks;
|
|
|
|
}
|
|
|
|
|
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();
|
2014-03-21 00:44:30 +00:00
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
$page = Title::newFromText( $params['page'] );
|
2012-11-14 18:33:57 +00:00
|
|
|
if ( !$page ) {
|
|
|
|
$this->dieUsageMsg( 'invalidtitle', $params['page'] );
|
|
|
|
}
|
2014-08-13 08:15:42 +00:00
|
|
|
if ( !in_array( $page->getNamespace(), $this->veConfig->get( 'VisualEditorNamespaces' ) ) ) {
|
2012-11-14 18:33:57 +00:00
|
|
|
$this->dieUsage( "VisualEditor is not enabled in namespace " .
|
|
|
|
$page->getNamespace(), 'novenamespace' );
|
|
|
|
}
|
2012-05-25 19:50:48 +00:00
|
|
|
|
2013-05-15 23:51:11 +00:00
|
|
|
$parserParams = array();
|
|
|
|
if ( isset( $params['oldid'] ) ) {
|
|
|
|
$parserParams['oldid'] = $params['oldid'];
|
|
|
|
}
|
2012-08-23 19:01:07 +00:00
|
|
|
|
2014-06-23 17:55:59 +00:00
|
|
|
$html = $params['html'];
|
|
|
|
if ( substr( $html, 0, 11 ) === 'rawdeflate,' ) {
|
|
|
|
$html = gzinflate( base64_decode( substr( $html, 11 ) ) );
|
|
|
|
}
|
|
|
|
|
2013-05-15 21:17:06 +00:00
|
|
|
switch ( $params['paction'] ) {
|
|
|
|
case 'parse':
|
|
|
|
$parsed = $this->getHTML( $page, $parserParams );
|
|
|
|
// Dirty hack to provide the correct context for edit notices
|
|
|
|
global $wgTitle; // FIXME NOOOOOOOOES
|
|
|
|
$wgTitle = $page;
|
2014-04-19 00:13:17 +00:00
|
|
|
RequestContext::getMain()->setTitle( $page );
|
2013-05-15 21:17:06 +00:00
|
|
|
$notices = $page->getEditNotices();
|
|
|
|
if ( $user->isAnon() ) {
|
2014-03-26 22:20:08 +00:00
|
|
|
$notices[] = $this->msg( 'anoneditwarning' )->parseAsBlock();
|
2012-12-11 03:40:09 +00:00
|
|
|
}
|
2013-05-24 14:48:27 +00:00
|
|
|
if ( $parsed && $parsed['restoring'] ) {
|
2014-03-26 22:20:08 +00:00
|
|
|
$notices[] = $this->msg( 'editingold' )->parseAsBlock();
|
2014-03-26 21:33:19 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 09:43:21 +00:00
|
|
|
// Creating new page
|
|
|
|
if ( !$page->exists() ) {
|
2014-03-26 21:33:19 +00:00
|
|
|
$notices[] = $this->msg(
|
|
|
|
$user->isLoggedIn() ? 'newarticletext' : 'newarticletextanon',
|
|
|
|
Skin::makeInternalOrExternalUrl(
|
|
|
|
$this->msg( 'helppage' )->inContentLanguage()->text()
|
|
|
|
)
|
|
|
|
)->parseAsBlock();
|
2013-07-24 09:43:21 +00:00
|
|
|
// Page protected from creation
|
|
|
|
if ( $page->getRestrictions( 'create' ) ) {
|
2014-03-26 21:33:19 +00:00
|
|
|
$notices[] = $this->msg( 'titleprotectedwarning' )->parseAsBlock();
|
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)
|
|
|
|
$protectedClasses = array();
|
|
|
|
if ( MWNamespace::getRestrictionLevels( $page->getNamespace() ) !== array( '' ) ) {
|
2014-04-16 23:16:06 +00:00
|
|
|
// Page protected from editing
|
|
|
|
if ( $page->isProtected( 'edit' ) ) {
|
|
|
|
# Is the title semi-protected?
|
|
|
|
if ( $page->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';
|
|
|
|
|
2014-04-16 23:16:06 +00:00
|
|
|
# Then it must be protected based on static groups (regular)
|
|
|
|
$noticeMsg = 'protectedpagewarning';
|
|
|
|
}
|
|
|
|
$notices[] = $this->msg( $noticeMsg )->parseAsBlock() .
|
|
|
|
$this->getLastLogEntry( $page, 'protect' );
|
2014-03-17 22:11:01 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 23:16:06 +00:00
|
|
|
// Deal with cascading edit protection
|
|
|
|
list( $sources, $restrictions ) = $page->getCascadeProtectionSources();
|
|
|
|
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 ) {
|
|
|
|
$notice .= "<li>" . Linker::link( $source ) . "</li>";
|
|
|
|
}
|
|
|
|
$notice .= '</ul>';
|
|
|
|
$notices[] = $notice;
|
|
|
|
}
|
2014-03-26 20:39:45 +00:00
|
|
|
}
|
|
|
|
|
2014-07-05 19:09:23 +00:00
|
|
|
if ( !$page->userCan( 'create' ) && !$page->exists() ) {
|
|
|
|
$notices[] = $this->msg(
|
|
|
|
'permissionserrorstext-withaction', 1, $this->msg( 'action-createpage' )
|
|
|
|
) . "<br>" . $this->msg( 'nocreatetext' )->parse();
|
|
|
|
}
|
|
|
|
|
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 :(
|
|
|
|
if ( $page->getNamespace() == NS_USER || $page->getNamespace() == NS_USER_TALK ) {
|
|
|
|
$parts = explode( '/', $page->getText(), 2 );
|
|
|
|
$targetUsername = $parts[0];
|
|
|
|
$targetUser = User::newFromName( $targetUsername, false /* allow IP users*/ );
|
|
|
|
|
|
|
|
if (
|
|
|
|
!( $targetUser && $targetUser->isLoggedIn() ) &&
|
|
|
|
!User::isIP( $targetUsername )
|
|
|
|
) { // User does not exist
|
|
|
|
$notices[] = "<div class=\"mw-userpage-userdoesnotexist error\">\n" .
|
|
|
|
$this->msg( 'userpage-userdoesnotexist', wfEscapeWikiText( $targetUsername ) ) .
|
|
|
|
"\n</div>";
|
|
|
|
} elseif ( $targetUser->isBlocked() ) { // Show log extract if the user is currently blocked
|
|
|
|
$notices[] = $this->msg(
|
|
|
|
'blocked-notice-logextract',
|
|
|
|
$targetUser->getName() // Support GENDER in notice
|
|
|
|
)->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
|
|
|
|
2014-03-17 21:22:41 +00:00
|
|
|
if ( $user->isBlockedFrom( $page ) && $user->getBlock()->prevents( 'edit' ) !== false ) {
|
|
|
|
$notices[] = call_user_func_array(
|
|
|
|
array( $this, 'msg' ),
|
|
|
|
$user->getBlock()->getPermissionsError( $this->getContext() )
|
|
|
|
)->parseAsBlock();
|
|
|
|
}
|
|
|
|
|
2014-04-22 00:22:48 +00:00
|
|
|
if ( class_exists( 'GlobalBlocking' ) ) {
|
|
|
|
$error = GlobalBlocking::getUserBlockErrors(
|
|
|
|
$user,
|
|
|
|
$this->getRequest()->getIP()
|
|
|
|
);
|
|
|
|
if ( count( $error ) ) {
|
|
|
|
$notices[] = call_user_func_array(
|
|
|
|
array( $this, 'msg' ),
|
|
|
|
$error
|
|
|
|
)->parseAsBlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Render check boxes from EditPage
EditPage has a lovely getCheckboxes() function which includes the
minor and watch checkboxes as rendered by MW core, as well as any
checkboxes extensions like FlaggedRevs might have added. Output
these in the API, render them, and send their values back.
ApiVisualEditor.php:
* Build a fake EditPage, get its checkboxes, and return them
ApiVisualEditorEdit.php:
* Pass through posted request data to ApiEdit, which passes it
through to EditPage thanks to Idab5b524b0e3 in core
ve.init.mw.ViewPageTarget.js:
* Remove minor and watch checkboxes from the save dialog template
and replace them with a generic checkbox container
* Have getSaveOptions() pull the state of all checkboxes in
** Special-case minor and watch, and pass the rest straight through
** Move normalization from true/false to presence/absence here, from
ve.init.mw.Target.prototype.save(), because here we know which ones
are checkboxes and we don't know that in save() without
special-casing
* Remove getSaveDialogHtml(), we don't need to hide checkboxes based on
rights anymore because in that case the API just won't send them to us.
** Moved logic for checking the watch checkbox down to where the same
logic for the minor checkbox already is
* Unwrap getSaveDialogHtml() in setupSaveDialog()
* Access minor and watch by their new IDs throughout
ve.init.mw.Target.js:
* Get and store checkboxes from the API
* Pass all keys straight through to the API
Bug: 49699
Change-Id: I09d02a42b05146bc9b7080ab38338ae869bf15e3
2013-07-24 06:39:03 +00:00
|
|
|
// HACK: Build a fake EditPage so we can get checkboxes from it
|
|
|
|
$article = new Article( $page ); // Deliberately omitting ,0 so oldid comes from request
|
|
|
|
$ep = new EditPage( $article );
|
2013-08-02 18:37:30 +00:00
|
|
|
$req = $this->getRequest();
|
2014-01-09 21:16:55 +00:00
|
|
|
$req->setVal( 'format', 'text/x-wiki' );
|
2013-08-02 18:37:30 +00:00
|
|
|
$ep->importFormData( $req ); // By reference for some reason (bug 52466)
|
Render check boxes from EditPage
EditPage has a lovely getCheckboxes() function which includes the
minor and watch checkboxes as rendered by MW core, as well as any
checkboxes extensions like FlaggedRevs might have added. Output
these in the API, render them, and send their values back.
ApiVisualEditor.php:
* Build a fake EditPage, get its checkboxes, and return them
ApiVisualEditorEdit.php:
* Pass through posted request data to ApiEdit, which passes it
through to EditPage thanks to Idab5b524b0e3 in core
ve.init.mw.ViewPageTarget.js:
* Remove minor and watch checkboxes from the save dialog template
and replace them with a generic checkbox container
* Have getSaveOptions() pull the state of all checkboxes in
** Special-case minor and watch, and pass the rest straight through
** Move normalization from true/false to presence/absence here, from
ve.init.mw.Target.prototype.save(), because here we know which ones
are checkboxes and we don't know that in save() without
special-casing
* Remove getSaveDialogHtml(), we don't need to hide checkboxes based on
rights anymore because in that case the API just won't send them to us.
** Moved logic for checking the watch checkbox down to where the same
logic for the minor checkbox already is
* Unwrap getSaveDialogHtml() in setupSaveDialog()
* Access minor and watch by their new IDs throughout
ve.init.mw.Target.js:
* Get and store checkboxes from the API
* Pass all keys straight through to the API
Bug: 49699
Change-Id: I09d02a42b05146bc9b7080ab38338ae869bf15e3
2013-07-24 06:39:03 +00:00
|
|
|
$tabindex = 0;
|
|
|
|
$states = array( 'minor' => false, 'watch' => false );
|
|
|
|
$checkboxes = $ep->getCheckboxes( $tabindex, $states );
|
|
|
|
|
2014-03-11 00:46:26 +00:00
|
|
|
// HACK: Find out which red links are on the page
|
|
|
|
// We do the lookup for the current version. This might not be entirely complete
|
|
|
|
// if we're loading an oldid, but it'll probably be close enough, and LinkCache
|
|
|
|
// will automatically request any additional data it needs.
|
|
|
|
$links = array();
|
|
|
|
$wikipage = WikiPage::factory( $page );
|
|
|
|
$popts = $wikipage->makeParserOptions( 'canonical' );
|
|
|
|
$cached = ParserCache::singleton()->get( $article, $popts, true );
|
|
|
|
if ( $cached ) {
|
|
|
|
foreach ( $cached->getLinks() as $ns => $dbks ) {
|
|
|
|
foreach ( $dbks as $dbk => $id ) {
|
|
|
|
$links[ Title::makeTitle( $ns, $dbk )->getPrefixedText() ] = array(
|
|
|
|
'missing' => $id == 0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// On parser cache miss, just don't bother populating red link data
|
|
|
|
|
2013-05-15 21:17:06 +00:00
|
|
|
if ( $parsed === false ) {
|
|
|
|
$this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' );
|
|
|
|
} else {
|
|
|
|
$result = array_merge(
|
2013-06-29 01:52:29 +00:00
|
|
|
array(
|
|
|
|
'result' => 'success',
|
Render check boxes from EditPage
EditPage has a lovely getCheckboxes() function which includes the
minor and watch checkboxes as rendered by MW core, as well as any
checkboxes extensions like FlaggedRevs might have added. Output
these in the API, render them, and send their values back.
ApiVisualEditor.php:
* Build a fake EditPage, get its checkboxes, and return them
ApiVisualEditorEdit.php:
* Pass through posted request data to ApiEdit, which passes it
through to EditPage thanks to Idab5b524b0e3 in core
ve.init.mw.ViewPageTarget.js:
* Remove minor and watch checkboxes from the save dialog template
and replace them with a generic checkbox container
* Have getSaveOptions() pull the state of all checkboxes in
** Special-case minor and watch, and pass the rest straight through
** Move normalization from true/false to presence/absence here, from
ve.init.mw.Target.prototype.save(), because here we know which ones
are checkboxes and we don't know that in save() without
special-casing
* Remove getSaveDialogHtml(), we don't need to hide checkboxes based on
rights anymore because in that case the API just won't send them to us.
** Moved logic for checking the watch checkbox down to where the same
logic for the minor checkbox already is
* Unwrap getSaveDialogHtml() in setupSaveDialog()
* Access minor and watch by their new IDs throughout
ve.init.mw.Target.js:
* Get and store checkboxes from the API
* Pass all keys straight through to the API
Bug: 49699
Change-Id: I09d02a42b05146bc9b7080ab38338ae869bf15e3
2013-07-24 06:39:03 +00:00
|
|
|
'notices' => $notices,
|
|
|
|
'checkboxes' => $checkboxes,
|
2014-03-11 00:46:26 +00:00
|
|
|
'links' => $links,
|
2014-04-30 19:28:29 +00:00
|
|
|
'protectedClasses' => implode( ' ', $protectedClasses )
|
2013-06-29 01:52:29 +00:00
|
|
|
),
|
|
|
|
$parsed['result']
|
2012-11-14 18:33:57 +00:00
|
|
|
);
|
2013-05-15 21:17:06 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-12-04 15:30:21 +00:00
|
|
|
|
2013-05-15 23:51:11 +00:00
|
|
|
case 'parsefragment':
|
2014-04-18 20:19:54 +00:00
|
|
|
$content = $this->parseWikitextFragment( $page, $params['wikitext'] );
|
2013-05-15 23:51:11 +00:00
|
|
|
if ( $content === false ) {
|
2014-04-18 20:19:54 +00:00
|
|
|
$this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' );
|
2013-05-15 23:51:11 +00:00
|
|
|
} else {
|
|
|
|
$result = array(
|
|
|
|
'result' => 'success',
|
|
|
|
'content' => $content
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
2013-12-04 15:30:21 +00:00
|
|
|
|
2013-05-15 21:17:06 +00:00
|
|
|
case 'serialize':
|
2013-11-01 21:30:22 +00:00
|
|
|
if ( $params['cachekey'] !== null ) {
|
|
|
|
$content = $this->trySerializationCache( $params['cachekey'] );
|
|
|
|
if ( !is_string( $content ) ) {
|
|
|
|
$this->dieUsage( 'No cached serialization found with that key', 'badcachekey' );
|
|
|
|
}
|
2012-06-01 23:26:03 +00:00
|
|
|
} else {
|
2013-11-01 21:30:22 +00:00
|
|
|
if ( $params['html'] === null ) {
|
|
|
|
$this->dieUsageMsg( 'missingparam', 'html' );
|
|
|
|
}
|
|
|
|
$content = $this->postHTML( $page, $html, $parserParams );
|
|
|
|
if ( $content === false ) {
|
|
|
|
$this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' );
|
|
|
|
}
|
2013-05-15 21:17:06 +00:00
|
|
|
}
|
2013-11-01 21:30:22 +00:00
|
|
|
$result = array( 'result' => 'success', 'content' => $content );
|
2013-05-15 21:17:06 +00:00
|
|
|
break;
|
2013-12-04 15:30:21 +00:00
|
|
|
|
2013-06-28 22:46:15 +00:00
|
|
|
case 'diff':
|
2013-11-01 21:30:22 +00:00
|
|
|
if ( $params['cachekey'] !== null ) {
|
|
|
|
$wikitext = $this->trySerializationCache( $params['cachekey'] );
|
|
|
|
if ( !is_string( $wikitext ) ) {
|
|
|
|
$this->dieUsage( 'No cached serialization found with that key', 'badcachekey' );
|
|
|
|
}
|
|
|
|
} else {
|
2014-06-23 17:55:59 +00:00
|
|
|
$wikitext = $this->postHTML( $page, $html, $parserParams );
|
2013-11-01 21:30:22 +00:00
|
|
|
if ( $wikitext === false ) {
|
|
|
|
$this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' );
|
|
|
|
}
|
2013-06-28 22:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$diff = $this->diffWikitext( $page, $wikitext );
|
|
|
|
if ( $diff['result'] === 'fail' ) {
|
|
|
|
$this->dieUsage( 'Diff failed', 'difffailed' );
|
|
|
|
}
|
|
|
|
$result = $diff;
|
|
|
|
|
2013-11-01 21:30:22 +00:00
|
|
|
break;
|
2013-12-04 15:30:21 +00:00
|
|
|
|
2013-11-01 21:30:22 +00:00
|
|
|
case 'serializeforcache':
|
2014-06-23 17:55:59 +00:00
|
|
|
$key = $this->storeInSerializationCache( $page, $parserParams['oldid'], $html );
|
2013-11-01 21:30:22 +00:00
|
|
|
$result = array( 'result' => 'success', 'cachekey' => $key );
|
2013-05-15 21:17:06 +00:00
|
|
|
break;
|
2013-12-04 15:30:21 +00:00
|
|
|
|
|
|
|
case 'getlanglinks':
|
|
|
|
$langlinks = $this->getLangLinks( $page );
|
|
|
|
if ( $langlinks === false ) {
|
|
|
|
$this->dieUsage( 'Error querying MediaWiki API', 'parsoidserver' );
|
|
|
|
} else {
|
|
|
|
$result = array( 'result' => 'success', 'langlinks' => $langlinks );
|
|
|
|
}
|
|
|
|
break;
|
2012-05-25 19:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param $title Title
|
|
|
|
* @param $types array|string
|
2014-08-17 19:43:56 +00:00
|
|
|
* @return string
|
2014-03-26 20:39:45 +00:00
|
|
|
*/
|
|
|
|
private function getLastLogEntry( $title, $types = '' ) {
|
|
|
|
$lp = new LogPager(
|
|
|
|
new LogEventsList( $this->getContext() ),
|
|
|
|
$types,
|
|
|
|
'',
|
|
|
|
$title->getPrefixedDbKey()
|
|
|
|
);
|
|
|
|
$lp->mLimit = 1;
|
|
|
|
|
|
|
|
return $lp->getBody() . Linker::link(
|
|
|
|
SpecialPage::getTitleFor( 'Log' ),
|
|
|
|
$this->msg( 'log-fulllog' )->escaped(),
|
|
|
|
array(),
|
|
|
|
array(
|
|
|
|
'page' => $title->getPrefixedDBkey(),
|
|
|
|
'type' => is_string( $types ) ? $types : null
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
public function getAllowedParams() {
|
|
|
|
return array(
|
|
|
|
'page' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
|
|
),
|
2014-03-21 00:44:30 +00:00
|
|
|
'format' => array(
|
|
|
|
ApiBase::PARAM_DFLT => 'json',
|
|
|
|
ApiBase::PARAM_TYPE => array( 'json', 'jsonfm' ),
|
|
|
|
),
|
2012-05-25 19:50:48 +00:00
|
|
|
'paction' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2013-11-01 21:30:22 +00:00
|
|
|
ApiBase::PARAM_TYPE => array(
|
2014-03-21 00:44:30 +00:00
|
|
|
'parse',
|
|
|
|
'parsefragment',
|
|
|
|
'serialize',
|
|
|
|
'serializeforcache',
|
|
|
|
'diff',
|
|
|
|
'getlanglinks',
|
2013-11-01 21:30:22 +00:00
|
|
|
),
|
2012-05-25 22:23:40 +00:00
|
|
|
),
|
2013-05-15 23:51:11 +00:00
|
|
|
'wikitext' => null,
|
2012-11-14 18:33:57 +00:00
|
|
|
'basetimestamp' => null,
|
2012-11-28 23:57:00 +00:00
|
|
|
'starttimestamp' => null,
|
2013-05-15 23:51:11 +00:00
|
|
|
'oldid' => null,
|
2012-11-28 23:57:00 +00:00
|
|
|
'html' => null,
|
2013-11-01 21:30:22 +00:00
|
|
|
'cachekey' => null,
|
2012-05-25 19:50:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needsToken() {
|
2013-07-11 17:09:28 +00:00
|
|
|
return false;
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function mustBePosted() {
|
2013-12-04 15:30:21 +00:00
|
|
|
return false;
|
2012-11-14 18:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isWriteMode() {
|
|
|
|
return true;
|
2012-05-25 19:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
|
|
|
return array(
|
2012-05-31 00:09:06 +00:00
|
|
|
'page' => 'The page to perform actions on.',
|
2012-11-14 18:33:57 +00:00
|
|
|
'paction' => 'Action to perform',
|
2013-07-11 17:09:28 +00:00
|
|
|
'oldid' => 'The revision number to use (defaults to latest version).',
|
2012-11-14 18:33:57 +00:00
|
|
|
'html' => 'HTML to send to parsoid in exchange for wikitext',
|
2013-05-29 14:49:16 +00:00
|
|
|
'basetimestamp' => 'When saving, set this to the timestamp of the revision that was'
|
2013-11-01 21:30:22 +00:00
|
|
|
. ' edited. Used to detect edit conflicts.',
|
2013-05-29 14:49:16 +00:00
|
|
|
'starttimestamp' => 'When saving, set this to the timestamp of when the page was loaded.'
|
2013-11-01 21:30:22 +00:00
|
|
|
. ' Used to detect edit conflicts.',
|
|
|
|
'cachekey' => 'For serialize or diff, use the result of a previous serializeforcache'
|
|
|
|
. ' request with this key. Overrides html.',
|
2012-05-25 19:50:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription() {
|
|
|
|
return 'Returns HTML5 for a page from the parsoid service.';
|
|
|
|
}
|
|
|
|
}
|