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-05-25 22:23:40 +00:00
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
public function execute() {
|
2012-08-01 06:50:19 +00:00
|
|
|
global $wgVisualEditorParsoidURL, $wgVisualEditorParsoidPrefix;
|
2012-07-21 17:37:20 +00:00
|
|
|
$user = $this->getUser();
|
|
|
|
|
2012-06-06 23:08:41 +00:00
|
|
|
$parsoid = $wgVisualEditorParsoidURL;
|
2012-05-25 19:50:48 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$page = Title::newFromText( $params['page'] );
|
|
|
|
|
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' ) {
|
|
|
|
if ( $page->exists() ) {
|
|
|
|
$parsed = Http::get(
|
2012-08-14 00:56:30 +00:00
|
|
|
// Insert slash since wgVisualEditorParsoidURL does not
|
|
|
|
// end in a slash
|
2012-08-23 19:01:07 +00:00
|
|
|
wfAppendQuery(
|
|
|
|
$parsoid . '/' . $wgVisualEditorParsoidPrefix . '/' .
|
|
|
|
urlencode( $page->getPrefixedDBkey() ),
|
|
|
|
$parserParams
|
|
|
|
)
|
2012-05-25 19:50:48 +00:00
|
|
|
);
|
2012-06-19 00:39:20 +00:00
|
|
|
|
|
|
|
if ( $parsed ) {
|
|
|
|
$result = array(
|
|
|
|
'result' => 'success',
|
|
|
|
'parsed' => $parsed
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$result = array(
|
|
|
|
'result' => 'error'
|
|
|
|
);
|
|
|
|
}
|
2012-05-25 19:50:48 +00:00
|
|
|
} else {
|
2012-08-01 00:49:04 +00:00
|
|
|
$result = array(
|
|
|
|
'result' => 'success',
|
|
|
|
'parsed' => ''
|
|
|
|
);
|
2012-05-25 19:50:48 +00:00
|
|
|
}
|
2012-07-21 17:37:20 +00:00
|
|
|
} elseif ( $params['paction'] === 'save' && $user->isBlocked() ) {
|
2012-06-21 20:55:33 +00:00
|
|
|
$result = array( 'result' => 'error' );
|
|
|
|
} elseif ( $params['paction'] === 'save' /* means user is not blocked */ ) {
|
2012-05-25 19:50:48 +00:00
|
|
|
// API Posts HTML to Parsoid Service, receives Wikitext,
|
|
|
|
// API Saves Wikitext to page.
|
2012-08-23 19:01:07 +00:00
|
|
|
$wikitext = Http::post(
|
|
|
|
$parsoid . '/' . $page->getPrefixedDBkey(),
|
2012-06-19 00:39:20 +00:00
|
|
|
array( 'postData' => array( 'content' => $params['html'] ) )
|
|
|
|
);
|
2012-05-25 22:23:40 +00:00
|
|
|
|
|
|
|
if ( $wikitext ) {
|
|
|
|
|
2012-05-31 00:09:06 +00:00
|
|
|
/* Save Page */
|
2012-06-19 00:39:20 +00:00
|
|
|
$flags = $params['minor'] === 'true' ? EDIT_MINOR : 0;
|
2012-05-31 00:09:06 +00:00
|
|
|
|
|
|
|
$wikiPage = WikiPage::factory( $page );
|
|
|
|
$status = $wikiPage->doEdit(
|
|
|
|
$wikitext,
|
|
|
|
$params['summary'],
|
|
|
|
$flags
|
2012-05-25 22:23:40 +00:00
|
|
|
);
|
2012-06-01 23:26:03 +00:00
|
|
|
|
|
|
|
// Check status ?
|
|
|
|
// $status->ok === true ?
|
|
|
|
|
|
|
|
// Add / Remove from watch list.
|
|
|
|
if( $params['watch'] === 'true' ) {
|
2012-07-21 17:37:20 +00:00
|
|
|
if ( $user->isWatched( $page ) === false ) {
|
|
|
|
$user->addWatch( $page );
|
2012-06-01 23:26:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Remove from watchlist?
|
2012-07-21 17:37:20 +00:00
|
|
|
if ( $user->isWatched( $page ) === true ) {
|
|
|
|
$user->removeWatch( $page );
|
2012-06-01 23:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-25 22:23:40 +00:00
|
|
|
|
2012-05-31 00:09:06 +00:00
|
|
|
/* Get page content */
|
|
|
|
// NOTE: possibly return content from revision object vs current rev ?
|
|
|
|
// $revisionObj = $status->value['revision'];
|
|
|
|
|
2012-05-25 22:23:40 +00:00
|
|
|
$apiParams = array(
|
|
|
|
'action' => 'parse',
|
|
|
|
'page' => $page
|
|
|
|
);
|
|
|
|
$api = new ApiMain(
|
|
|
|
new DerivativeRequest(
|
2012-07-21 17:37:20 +00:00
|
|
|
$this->getRequest(),
|
2012-05-25 22:23:40 +00:00
|
|
|
$apiParams,
|
|
|
|
false // was posted?
|
|
|
|
),
|
|
|
|
true // enable write?
|
|
|
|
);
|
2012-05-31 00:09:06 +00:00
|
|
|
|
2012-05-25 22:23:40 +00:00
|
|
|
$api->execute();
|
|
|
|
$result = $api->getResultData();
|
|
|
|
|
|
|
|
$result = array(
|
|
|
|
'result' => 'success',
|
|
|
|
'content' => $result['parse']['text']['*']
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$result = array(
|
|
|
|
'result' => 'error'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-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-05-25 23:19:46 +00:00
|
|
|
'summary' => null
|
2012-05-25 19:50:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needsToken() {
|
2012-05-25 23:19:46 +00:00
|
|
|
return false;
|
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.',
|
|
|
|
'paction' => 'Which action? parse or save.',
|
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-05-25 19:50:48 +00:00
|
|
|
'html' => 'HTML to send to parsoid in exchange for wikitext'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDescription() {
|
|
|
|
return 'Returns HTML5 for a page from the parsoid service.';
|
|
|
|
}
|
|
|
|
}
|