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-02-07 16:10:59 +00:00
|
|
|
/**
|
|
|
|
* Parsoid HTTP proxy configuration for MWHttpRequest
|
|
|
|
*/
|
|
|
|
protected function getProxyConf() {
|
|
|
|
global $wgVisualEditorParsoidHTTPProxy;
|
|
|
|
if ( $wgVisualEditorParsoidHTTPProxy ) {
|
|
|
|
return array( 'proxy' => $wgVisualEditorParsoidHTTPProxy );
|
|
|
|
} else {
|
|
|
|
return array( 'noProxy' => true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 18:33:57 +00:00
|
|
|
protected function getHTML( $title, $parserParams ) {
|
2014-02-07 16:10:59 +00:00
|
|
|
global $wgVisualEditorParsoidURL,
|
|
|
|
$wgVisualEditorParsoidPrefix,
|
|
|
|
$wgVisualEditorParsoidTimeout,
|
|
|
|
$wgVisualEditorParsoidForwardCookies;
|
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
|
|
|
|
|
|
|
$req = MWHttpRequest::factory( wfAppendQuery(
|
2012-11-28 23:57:00 +00:00
|
|
|
$wgVisualEditorParsoidURL . '/' . $wgVisualEditorParsoidPrefix .
|
|
|
|
'/' . urlencode( $title->getPrefixedDBkey() ),
|
|
|
|
$parserParams
|
|
|
|
),
|
2014-02-07 16:10:59 +00:00
|
|
|
array_merge(
|
|
|
|
$this->getProxyConf(),
|
|
|
|
array(
|
|
|
|
'method' => 'GET',
|
|
|
|
'timeout' => $wgVisualEditorParsoidTimeout,
|
|
|
|
)
|
2013-03-05 17:24:51 +00:00
|
|
|
)
|
2012-11-28 23:57:00 +00:00
|
|
|
);
|
2013-10-22 00:24:27 +00:00
|
|
|
// Forward cookies, but only if configured to do so and if there are read restrictions
|
|
|
|
if ( $wgVisualEditorParsoidForwardCookies && !User::isEveryoneAllowed( 'read' ) ) {
|
|
|
|
$req->setHeader( 'Cookie', $this->getRequest()->getHeader( 'Cookie' ) );
|
|
|
|
}
|
2013-03-05 17:24:51 +00:00
|
|
|
$status = $req->execute();
|
|
|
|
|
|
|
|
if ( $status->isOK() ) {
|
|
|
|
$content = $req->getContent();
|
2013-10-04 10:13:58 +00:00
|
|
|
// 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 ) {
|
2013-10-06 23:26:19 +00:00
|
|
|
$xpp = 'cached-response=true';
|
2013-10-04 10:13:58 +00:00
|
|
|
} else {
|
|
|
|
$xpp = $req->getResponseHeader( 'X-Parsoid-Performance' );
|
|
|
|
}
|
|
|
|
if ( $xpp !== null ) {
|
|
|
|
$resp = $this->getRequest()->response();
|
|
|
|
$resp->header( 'X-Parsoid-Performance: ' . $xpp );
|
|
|
|
}
|
2013-05-29 14:49:16 +00:00
|
|
|
} elseif ( $status->isGood() ) {
|
2013-03-05 17:24:51 +00:00
|
|
|
$this->dieUsage( $req->getContent(), 'parsoidserver-http-'.$req->getStatus() );
|
2013-05-29 14:49:16 +00:00
|
|
|
} elseif ( $errors = $status->getErrorsByType( 'error' ) ) {
|
2013-03-09 18:28:23 +00:00
|
|
|
$error = $errors[0];
|
|
|
|
$code = $error['message'];
|
2013-05-29 14:49:16 +00:00
|
|
|
if ( count( $error['params'] ) ) {
|
2013-03-09 18:28:23 +00:00
|
|
|
$message = $error['params'][0];
|
|
|
|
} else {
|
|
|
|
$message = 'MWHttpRequest error';
|
|
|
|
}
|
|
|
|
$this->dieUsage( $message, 'parsoidserver-'.$code );
|
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 ) {
|
|
|
|
global $wgMemc, $wgVisualEditorSerializationCacheTimeout;
|
|
|
|
$content = $this->postHTML( $title, $html, array( 'oldid' => $oldid ) );
|
|
|
|
if ( $content === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$hash = md5( $content );
|
|
|
|
$key = wfMemcKey( 'visualeditor', 'serialization', $hash );
|
|
|
|
$wgMemc->set( $key, $content, $wgVisualEditorSerializationCacheTimeout );
|
|
|
|
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 ) {
|
2014-02-07 16:10:59 +00:00
|
|
|
global $wgVisualEditorParsoidURL,
|
|
|
|
$wgVisualEditorParsoidPrefix,
|
|
|
|
$wgVisualEditorParsoidTimeout,
|
|
|
|
$wgVisualEditorParsoidForwardCookies;
|
|
|
|
|
2013-04-15 23:22:51 +00:00
|
|
|
if ( $parserParams['oldid'] === 0 ) {
|
|
|
|
$parserParams['oldid'] = '';
|
|
|
|
}
|
2013-10-22 00:24:27 +00:00
|
|
|
$req = MWHttpRequest::factory(
|
2012-11-27 01:39:44 +00:00
|
|
|
$wgVisualEditorParsoidURL . '/' . $wgVisualEditorParsoidPrefix .
|
|
|
|
'/' . urlencode( $title->getPrefixedDBkey() ),
|
2014-02-07 16:10:59 +00:00
|
|
|
array_merge(
|
|
|
|
$this->getProxyConf(),
|
|
|
|
array(
|
|
|
|
'method' => 'POST',
|
|
|
|
'postData' => array(
|
|
|
|
'content' => $html,
|
|
|
|
'oldid' => $parserParams['oldid']
|
|
|
|
),
|
|
|
|
'timeout' => $wgVisualEditorParsoidTimeout,
|
|
|
|
)
|
2012-11-15 01:16:13 +00:00
|
|
|
)
|
2012-11-14 18:33:57 +00:00
|
|
|
);
|
2013-10-22 00:24:27 +00:00
|
|
|
// Forward cookies, but only if configured to do so and if there are read restrictions
|
|
|
|
if ( $wgVisualEditorParsoidForwardCookies && !User::isEveryoneAllowed( 'read' ) ) {
|
|
|
|
$req->setHeader( 'Cookie', $this->getRequest()->getHeader( 'Cookie' ) );
|
|
|
|
}
|
|
|
|
$status = $req->execute();
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
// TODO proper error handling, merge with getHTML above
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// TODO pass through X-Parsoid-Performance header, merge with getHTML above
|
|
|
|
return $req->getContent();
|
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
|
|
|
}
|
|
|
|
|
2013-05-15 23:51:11 +00:00
|
|
|
protected function parseWikitextFragment( $wikitext, $title = null ) {
|
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'parse',
|
|
|
|
'title' => $title,
|
|
|
|
'prop' => 'text',
|
|
|
|
'disablepp' => true,
|
2014-02-12 01:06:26 +00:00
|
|
|
'pst' => true,
|
2013-05-15 23:51:11 +00:00
|
|
|
'text' => $wikitext
|
|
|
|
);
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
|
|
|
false // was posted?
|
|
|
|
),
|
|
|
|
true // enable write?
|
|
|
|
);
|
|
|
|
|
|
|
|
$api->execute();
|
|
|
|
$result = $api->getResultData();
|
|
|
|
return isset( $result['parse']['text']['*'] ) ? $result['parse']['text']['*'] : false;
|
|
|
|
}
|
|
|
|
|
2012-11-14 18:33:57 +00:00
|
|
|
protected function diffWikitext( $title, $wikitext ) {
|
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'query',
|
|
|
|
'prop' => 'revisions',
|
|
|
|
'titles' => $title->getPrefixedDBkey(),
|
|
|
|
'rvdifftotext' => $wikitext
|
|
|
|
);
|
|
|
|
$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() {
|
2013-07-12 16:37:22 +00:00
|
|
|
global $wgVisualEditorNamespaces, $wgVisualEditorEditNotices;
|
|
|
|
|
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'] );
|
|
|
|
}
|
|
|
|
if ( !in_array( $page->getNamespace(), $wgVisualEditorNamespaces ) ) {
|
|
|
|
$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
|
|
|
|
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;
|
|
|
|
$notices = $page->getEditNotices();
|
|
|
|
if ( $user->isAnon() ) {
|
|
|
|
$wgVisualEditorEditNotices[] = 'anoneditwarning';
|
2012-12-11 03:40:09 +00:00
|
|
|
}
|
2013-05-24 14:48:27 +00:00
|
|
|
if ( $parsed && $parsed['restoring'] ) {
|
|
|
|
$wgVisualEditorEditNotices[] = 'editingold';
|
|
|
|
}
|
2014-03-26 21:33:19 +00:00
|
|
|
if ( count( $wgVisualEditorEditNotices ) ) {
|
|
|
|
foreach ( $wgVisualEditorEditNotices as $key ) {
|
|
|
|
$notices[] = wfMessage( $key )->parseAsBlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-17 22:11:01 +00:00
|
|
|
// Page protected from editing
|
|
|
|
if ( $page->getNamespace() != NS_MEDIAWIKI && $page->isProtected( 'edit' ) ) {
|
|
|
|
# Is the title semi-protected?
|
|
|
|
if ( $page->isSemiProtected() ) {
|
|
|
|
$noticeMsg = 'semiprotectedpagewarning';
|
|
|
|
} else {
|
|
|
|
# Then it must be protected based on static groups (regular)
|
|
|
|
$noticeMsg = 'protectedpagewarning';
|
|
|
|
}
|
|
|
|
|
2014-03-26 20:39:45 +00:00
|
|
|
$notices[] = $this->msg( $noticeMsg )->parseAsBlock() .
|
|
|
|
$this->getLastLogEntry( $page, 'protect' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// 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,
|
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':
|
|
|
|
$content = $this->parseWikitextFragment( $params['wikitext'], $page->getText() );
|
|
|
|
if ( $content === false ) {
|
2013-08-02 22:35:42 +00:00
|
|
|
$this->dieUsage( 'Error querying MediaWiki API', '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' );
|
|
|
|
}
|
|
|
|
$html = $params['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 {
|
|
|
|
$wikitext = $this->postHTML( $page, $params['html'], $parserParams );
|
|
|
|
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':
|
|
|
|
$key = $this->storeInSerializationCache( $page, $parserParams['oldid'], $params['html'] );
|
|
|
|
$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
|
|
|
|
* @returns string
|
|
|
|
*/
|
|
|
|
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 getVersion() {
|
|
|
|
return __CLASS__ . ': $Id$';
|
|
|
|
}
|
|
|
|
|
|
|
|
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.';
|
|
|
|
}
|
|
|
|
}
|