mediawiki-extensions-Visual.../ApiVisualEditor.php
Catrope b05616892d (bug 37843) Trying to edit a blank page fails
Explicitly check for false, because the empty string does not signify a
failure.

Change-Id: I8857d39e1a20080c8419c6165be2b5ad0cdbacd2
2012-10-26 14:00:43 -07:00

172 lines
3.9 KiB
PHP

<?php
/**
* Parsoid API wrapper.
*
* @file
* @ingroup Extensions
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
class ApiVisualEditor extends ApiBase {
public function execute() {
global $wgVisualEditorParsoidURL, $wgVisualEditorParsoidPrefix;
$user = $this->getUser();
$parsoid = $wgVisualEditorParsoidURL;
$params = $this->extractRequestParams();
$page = Title::newFromText( $params['page'] );
$parserParams = array();
if ( is_numeric( $params['oldid'] ) ) {
$parserParams['oldid'] = intval( $params['oldid'] );
}
if ( $params['paction'] === 'parse' ) {
if ( $page->exists() ) {
$parsed = Http::get(
// Insert slash since wgVisualEditorParsoidURL does not
// end in a slash
wfAppendQuery(
$parsoid . '/' . $wgVisualEditorParsoidPrefix . '/' .
urlencode( $page->getPrefixedDBkey() ),
$parserParams
)
);
if ( $parsed !== false ) {
$result = array(
'result' => 'success',
'parsed' => $parsed
);
} else {
$result = array(
'result' => 'error'
);
}
} else {
$result = array(
'result' => 'success',
'parsed' => ''
);
}
} elseif ( $params['paction'] === 'save' && $user->isBlocked() ) {
$result = array( 'result' => 'error' );
} elseif ( $params['paction'] === 'save' /* means user is not blocked */ ) {
// API Posts HTML to Parsoid Service, receives Wikitext,
// API Saves Wikitext to page.
$wikitext = Http::post(
$parsoid . '/' . $page->getPrefixedDBkey(),
array( 'postData' => array( 'content' => $params['html'] ) )
);
if ( $wikitext ) {
/* Save Page */
$flags = $params['minor'] === 'true' ? EDIT_MINOR : 0;
$wikiPage = WikiPage::factory( $page );
$status = $wikiPage->doEdit(
$wikitext,
$params['summary'],
$flags
);
// Check status ?
// $status->ok === true ?
// Add / Remove from watch list.
if( $params['watch'] === 'true' ) {
if ( $user->isWatched( $page ) === false ) {
$user->addWatch( $page );
}
} else {
// Remove from watchlist?
if ( $user->isWatched( $page ) === true ) {
$user->removeWatch( $page );
}
}
/* Get page content */
// NOTE: possibly return content from revision object vs current rev ?
// $revisionObj = $status->value['revision'];
$apiParams = array(
'action' => 'parse',
'page' => $page
);
$api = new ApiMain(
new DerivativeRequest(
$this->getRequest(),
$apiParams,
false // was posted?
),
true // enable write?
);
$api->execute();
$result = $api->getResultData();
$result = array(
'result' => 'success',
'content' => $result['parse']['text']['*']
);
} else {
$result = array(
'result' => 'error'
);
}
}
$this->getResult()->addValue( null, $this->getModuleName(), $result );
}
public function getAllowedParams() {
return array(
'page' => array(
ApiBase::PARAM_REQUIRED => true,
),
'paction' => array(
ApiBase::PARAM_REQUIRED => true,
),
'oldid' => array(
ApiBase::PARAM_REQUIRED => false,
),
'minor' => array(
ApiBase::PARAM_REQUIRED => false,
),
'watch' => array(
ApiBase::PARAM_REQUIRED => false,
),
'html' => array(
ApiBase::PARAM_REQUIRED => false,
),
'summary' => null
);
}
public function needsToken() {
return false;
}
public function getVersion() {
return __CLASS__ . ': $Id$';
}
public function getParamDescription() {
return array(
'page' => 'The page to perform actions on.',
'paction' => 'Which action? parse or save.',
'oldid' => 'The revision number to use.',
'minor' => 'Flag for minor edit.',
'html' => 'HTML to send to parsoid in exchange for wikitext'
);
}
public function getDescription() {
return 'Returns HTML5 for a page from the parsoid service.';
}
}