2012-05-25 19:50:48 +00:00
|
|
|
<?php
|
|
|
|
/* Base parsoid API wrapper. */
|
|
|
|
class ApiVisualEditor extends ApiBase {
|
2012-05-25 22:23:40 +00:00
|
|
|
|
2012-05-25 19:50:48 +00:00
|
|
|
public function execute() {
|
2012-06-06 23:08:41 +00:00
|
|
|
global $wgRequest, $wgUser, $wgVisualEditorParsoidURL;
|
2012-05-25 23:19:46 +00:00
|
|
|
|
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-06-19 00:39:20 +00:00
|
|
|
if ( $params['paction'] === 'parse' ) {
|
|
|
|
if ( $page->exists() ) {
|
|
|
|
$parsed = Http::get(
|
|
|
|
$parsoid . $page->getPrefixedDBkey()
|
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-06-19 00:39:20 +00:00
|
|
|
$result = array( 'result' => 'success', 'parsed' => '' );
|
2012-05-25 19:50:48 +00:00
|
|
|
}
|
2012-06-19 00:39:20 +00:00
|
|
|
} elseif ( $params['paction'] === 'save' ) {
|
2012-05-25 19:50:48 +00:00
|
|
|
// API Posts HTML to Parsoid Service, receives Wikitext,
|
|
|
|
// API Saves Wikitext to page.
|
2012-06-19 00:39:20 +00:00
|
|
|
$wikitext = Http::post( $parsoid . $page->getPrefixedDBkey(),
|
|
|
|
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' ) {
|
|
|
|
if ( $wgUser->isWatched( $page ) === false ) {
|
|
|
|
$wgUser->addWatch( $page );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Remove from watchlist?
|
|
|
|
if ( $wgUser->isWatched( $page ) === true ) {
|
|
|
|
$wgUser->removeWatch( $page );
|
|
|
|
}
|
|
|
|
}
|
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(
|
|
|
|
$wgRequest,
|
|
|
|
$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-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.',
|
|
|
|
'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.';
|
|
|
|
}
|
|
|
|
}
|