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
|
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
class ApiVisualEditor extends ApiBase {
|
2012-11-14 18:33:57 +00:00
|
|
|
protected function getHTML( $title, $parserParams ) {
|
|
|
|
global $wgVisualEditorParsoidURL, $wgVisualEditorParsoidPrefix;
|
|
|
|
if ( !$title->exists() ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return Http::get(
|
|
|
|
// Insert slash since $wgVisualEditorParsoidURL does not
|
|
|
|
// end in a slash
|
|
|
|
wfAppendQuery(
|
|
|
|
$wgVisualEditorParsoidURL . '/' . $wgVisualEditorParsoidPrefix .
|
|
|
|
'/' . urlencode( $title->getPrefixedDBkey() ),
|
|
|
|
$parserParams
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function postHTML( $title, $html ) {
|
|
|
|
global $wgVisualEditorParsoidURL;
|
|
|
|
return Http::post(
|
|
|
|
$wgVisualEditorParsoidURL . '/' . urlencode( $title->getPrefixedDBkey() ),
|
|
|
|
array( 'postData' => array( 'content' => $html ) )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function saveWikitext( $title, $wikitext, $params ) {
|
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'edit',
|
|
|
|
'title' => $title->getPrefixedDBkey(),
|
|
|
|
'text' => $wikitext,
|
|
|
|
'summary' => $params['summary'],
|
|
|
|
'basetimestamp' => $params['basetimestamp'],
|
|
|
|
'token' => $params['token'],
|
|
|
|
);
|
|
|
|
if ( $params['minor'] ) {
|
|
|
|
$apiParams['minor'] = true;
|
|
|
|
}
|
|
|
|
// FIXME add some way that the user's preferences can be respected
|
|
|
|
$apiParams['watchlist'] = $params['watch'] ? 'watch' : 'unwatch';
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
|
|
|
$this->getRequest(),
|
|
|
|
$apiParams,
|
|
|
|
true // was posted
|
|
|
|
),
|
|
|
|
true // enable write
|
|
|
|
);
|
|
|
|
$api->execute();
|
|
|
|
return $api->getResultData();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function parseWikitext( $title ) {
|
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'parse',
|
|
|
|
'page' => $title->getPrefixedDBkey()
|
|
|
|
);
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
return isset( $result['query']['pages'][$title->getArticleID()]['revisions'][0]['diff']['*'] ) ?
|
|
|
|
$result['query']['pages'][$title->getArticleID()]['revisions'][0]['diff']['*'] :
|
|
|
|
false;
|
|
|
|
}
|
2012-05-25 22:23:40 +00:00
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
public function execute() {
|
2012-11-14 18:33:57 +00:00
|
|
|
global $wgVisualEditorNamespaces;
|
2012-07-21 17:37:20 +00:00
|
|
|
$user = $this->getUser();
|
2012-05-25 19:50:48 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$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
|
|
|
|
2012-08-23 19:01:07 +00:00
|
|
|
$parserParams = array();
|
|
|
|
if ( is_numeric( $params['oldid'] ) ) {
|
|
|
|
$parserParams['oldid'] = intval( $params['oldid'] );
|
|
|
|
}
|
|
|
|
|
2012-06-19 00:39:20 +00:00
|
|
|
if ( $params['paction'] === 'parse' ) {
|
2012-11-14 18:33:57 +00:00
|
|
|
$parsed = $this->getHTML( $page, $parserParams );
|
2012-06-19 00:39:20 +00:00
|
|
|
|
2012-11-14 18:33:57 +00:00
|
|
|
if ( $parsed !== false ) {
|
2012-08-01 00:49:04 +00:00
|
|
|
$result = array(
|
|
|
|
'result' => 'success',
|
2012-11-14 18:33:57 +00:00
|
|
|
'parsed' => $parsed
|
2012-08-01 00:49:04 +00:00
|
|
|
);
|
2012-11-14 18:33:57 +00:00
|
|
|
} else {
|
|
|
|
$this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' );
|
2012-05-25 19:50:48 +00:00
|
|
|
}
|
2012-11-14 18:33:57 +00:00
|
|
|
} elseif ( $params['paction'] === 'save' || $params['paction'] === 'diff' ) {
|
|
|
|
$wikitext = $this->postHTML( $page, $params['html'] );
|
|
|
|
|
|
|
|
if ( $wikitext === false ) {
|
|
|
|
$this->dieUsage( 'Error contacting the Parsoid server', 'parsoidserver' );
|
|
|
|
} else if ( $params['paction'] === 'save' ) {
|
|
|
|
// Save page
|
|
|
|
$editResult = $this->saveWikitext( $page, $wikitext, $params );
|
|
|
|
if (
|
|
|
|
!isset( $editResult['edit']['result'] ) ||
|
|
|
|
$editResult['edit']['result'] !== 'Success'
|
|
|
|
) {
|
|
|
|
$result = array(
|
|
|
|
'result' => 'error',
|
|
|
|
'edit' => $editResult['edit']
|
|
|
|
);
|
2012-06-01 23:26:03 +00:00
|
|
|
} else {
|
2012-11-14 18:33:57 +00:00
|
|
|
$parsed = $this->parseWikitext( $page );
|
|
|
|
$result = array( 'result' => 'success' );
|
|
|
|
if ( $parsed !== false ) {
|
|
|
|
$result['content'] = $parsed;
|
2012-06-01 23:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-14 18:33:57 +00:00
|
|
|
} else if ( $params['paction'] === 'diff' ) {
|
|
|
|
$diff = $this->diffWikitext( $page, $wikitext );
|
|
|
|
if ( $diff === false ) {
|
|
|
|
$this->dieUsage( 'Diff failed', 'difffailed' );
|
|
|
|
}
|
2012-05-25 22:23:40 +00:00
|
|
|
$result = array(
|
|
|
|
'result' => 'success',
|
2012-11-14 18:33:57 +00:00
|
|
|
'diff' => $diff
|
2012-05-25 22:23:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return array(
|
|
|
|
'page' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
|
|
),
|
|
|
|
'paction' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2012-11-14 18:33:57 +00:00
|
|
|
ApiBase::PARAM_TYPE => array( 'parse', 'save', 'diff' ),
|
2012-05-25 19:50:48 +00:00
|
|
|
),
|
2012-08-23 19:01:07 +00:00
|
|
|
'oldid' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => false,
|
|
|
|
),
|
2012-05-31 00:09:06 +00:00
|
|
|
'minor' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => false,
|
|
|
|
),
|
2012-06-01 23:26:03 +00:00
|
|
|
'watch' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => false,
|
|
|
|
),
|
2012-05-25 19:50:48 +00:00
|
|
|
'html' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => false,
|
2012-05-25 22:23:40 +00:00
|
|
|
),
|
2012-11-14 18:33:57 +00:00
|
|
|
'summary' => null,
|
|
|
|
'basetimestamp' => null,
|
|
|
|
'token' => null,
|
2012-05-25 19:50:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needsToken() {
|
2012-11-14 18:33:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTokenSalt() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function mustBePosted() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
2012-08-23 19:01:07 +00:00
|
|
|
'oldid' => 'The revision number to use.',
|
2012-05-31 00:09:06 +00:00
|
|
|
'minor' => 'Flag for minor edit.',
|
2012-11-14 18:33:57 +00:00
|
|
|
'html' => 'HTML to send to parsoid in exchange for wikitext',
|
|
|
|
'summary' => 'Edit summary',
|
|
|
|
'basetimestamp' => 'When saving, set this to the timestamp of the revision that was edited. Used to detect edit conflicts.',
|
|
|
|
'token' => 'Edit token',
|
2012-05-25 19:50:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription() {
|
|
|
|
return 'Returns HTML5 for a page from the parsoid service.';
|
|
|
|
}
|
|
|
|
}
|