2012-07-14 04:23:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API module for serving debug console requests on the edit page
|
|
|
|
*/
|
|
|
|
|
|
|
|
class ApiScribuntoConsole extends ApiBase {
|
|
|
|
const SC_MAX_SIZE = 500000;
|
|
|
|
const SC_SESSION_EXPIRY = 3600;
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
|
|
$title = Title::newFromText( $params['title'] );
|
|
|
|
if ( !$title ) {
|
2018-01-10 14:19:13 +00:00
|
|
|
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $params['session'] ) {
|
|
|
|
$sessionId = $params['session'];
|
|
|
|
} else {
|
|
|
|
$sessionId = mt_rand( 0, 0x7fffffff );
|
|
|
|
}
|
|
|
|
|
|
|
|
$cache = ObjectCache::getInstance( CACHE_ANYTHING );
|
2018-01-10 14:20:06 +00:00
|
|
|
$sessionKey = $cache->makeKey( 'scribunto-console', $this->getUser()->getId(), $sessionId );
|
2012-07-14 04:23:42 +00:00
|
|
|
$session = null;
|
2013-02-08 22:32:28 +00:00
|
|
|
$sessionIsNew = false;
|
2012-07-14 04:23:42 +00:00
|
|
|
if ( $params['session'] ) {
|
|
|
|
$session = $cache->get( $sessionKey );
|
|
|
|
}
|
|
|
|
if ( !isset( $session['version'] ) ) {
|
|
|
|
$session = $this->newSession();
|
2013-02-08 22:32:28 +00:00
|
|
|
$sessionIsNew = true;
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
2014-07-07 18:46:59 +00:00
|
|
|
// Create a variable holding the session which will be stored if there
|
2012-07-14 04:23:42 +00:00
|
|
|
// are no errors. If there are errors, we don't want to store the current
|
|
|
|
// question to the state builder array, since that will cause subsequent
|
|
|
|
// requests to fail.
|
|
|
|
$newSession = $session;
|
|
|
|
|
|
|
|
if ( !empty( $params['clear'] ) ) {
|
|
|
|
$newSession['size'] -= strlen( implode( '', $newSession['questions'] ) );
|
2017-06-15 17:19:00 +00:00
|
|
|
$newSession['questions'] = [];
|
|
|
|
$session['questions'] = [];
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
if ( strlen( $params['question'] ) ) {
|
|
|
|
$newSession['size'] += strlen( $params['question'] );
|
|
|
|
$newSession['questions'][] = $params['question'];
|
|
|
|
}
|
|
|
|
if ( $params['content'] ) {
|
|
|
|
$newSession['size'] += strlen( $params['content'] ) - strlen( $newSession['content'] );
|
|
|
|
$newSession['content'] = $params['content'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $newSession['size'] > self::SC_MAX_SIZE ) {
|
2018-01-10 14:19:13 +00:00
|
|
|
$this->dieWithError( 'scribunto-console-too-large' );
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
$result = $this->runConsole( [
|
2012-07-14 04:23:42 +00:00
|
|
|
'title' => $title,
|
|
|
|
'content' => $newSession['content'],
|
|
|
|
'prevQuestions' => $session['questions'],
|
2017-06-15 17:19:00 +00:00
|
|
|
'question' => $params['question'],
|
|
|
|
] );
|
2012-07-14 04:23:42 +00:00
|
|
|
|
|
|
|
if ( $result['type'] === 'error' ) {
|
|
|
|
// Restore the questions array
|
|
|
|
$newSession['questions'] = $session['questions'];
|
|
|
|
}
|
|
|
|
$cache->set( $sessionKey, $newSession, self::SC_SESSION_EXPIRY );
|
|
|
|
$result['session'] = $sessionId;
|
|
|
|
$result['sessionSize'] = $newSession['size'];
|
|
|
|
$result['sessionMaxSize'] = self::SC_MAX_SIZE;
|
2013-02-08 22:32:28 +00:00
|
|
|
if ( $sessionIsNew ) {
|
|
|
|
$result['sessionIsNew'] = '';
|
|
|
|
}
|
2012-07-14 04:23:42 +00:00
|
|
|
foreach ( $result as $key => $value ) {
|
|
|
|
$this->getResult()->addValue( null, $key, $value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 09:02:03 +00:00
|
|
|
protected function runConsole( array $params ) {
|
2012-07-14 04:23:42 +00:00
|
|
|
global $wgParser;
|
|
|
|
$options = new ParserOptions;
|
|
|
|
$wgParser->startExternalParse( $params['title'], $options, Parser::OT_HTML, true );
|
|
|
|
$engine = Scribunto::getParserEngine( $wgParser );
|
|
|
|
try {
|
|
|
|
$result = $engine->runConsole( $params );
|
|
|
|
} catch ( ScribuntoException $e ) {
|
|
|
|
$trace = $e->getScriptTraceHtml();
|
|
|
|
$message = $e->getMessage();
|
2017-06-15 17:19:00 +00:00
|
|
|
$html = Html::element( 'p', [], $message );
|
2012-07-14 04:23:42 +00:00
|
|
|
if ( $trace !== false ) {
|
2012-10-06 23:42:39 +00:00
|
|
|
$html .= Html::element( 'p',
|
2017-06-15 17:19:00 +00:00
|
|
|
[],
|
2012-10-06 23:42:39 +00:00
|
|
|
$this->msg( 'scribunto-common-backtrace' )->inContentLanguage()->text()
|
|
|
|
) . $trace;
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
2014-07-07 18:46:59 +00:00
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
return [
|
2012-07-14 04:23:42 +00:00
|
|
|
'type' => 'error',
|
|
|
|
'html' => $html,
|
|
|
|
'message' => $message,
|
2017-06-15 17:19:00 +00:00
|
|
|
'messagename' => $e->getMessageName() ];
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
return [
|
2012-07-14 04:23:42 +00:00
|
|
|
'type' => 'normal',
|
|
|
|
'print' => strval( $result['print'] ),
|
|
|
|
'return' => strval( $result['return'] )
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 09:02:03 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-07-14 04:23:42 +00:00
|
|
|
protected function newSession() {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [
|
2012-07-14 04:23:42 +00:00
|
|
|
'content' => '',
|
2017-06-15 17:19:00 +00:00
|
|
|
'questions' => [],
|
2012-07-14 04:23:42 +00:00
|
|
|
'size' => 0,
|
|
|
|
'version' => 1,
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 18:33:40 +00:00
|
|
|
public function isInternal() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-14 04:23:42 +00:00
|
|
|
public function getAllowedParams() {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [
|
|
|
|
'title' => [
|
2012-07-14 04:23:42 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
|
|
|
'content' => [
|
2012-07-14 04:23:42 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string'
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
|
|
|
'session' => [
|
2012-07-14 04:23:42 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
|
|
|
'question' => [
|
2012-07-14 04:23:42 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
|
|
|
'clear' => [
|
2012-07-14 04:23:42 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'boolean',
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
|
|
|
];
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
}
|