2012-07-14 04:23:42 +00:00
|
|
|
<?php
|
|
|
|
|
2022-04-07 23:12:32 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use Html;
|
2019-10-28 20:17:06 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-04-07 23:12:32 +00:00
|
|
|
use ObjectCache;
|
|
|
|
use Parser;
|
|
|
|
use ParserOptions;
|
|
|
|
use Title;
|
2022-04-03 23:29:23 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
2019-10-28 20:17:06 +00:00
|
|
|
|
2012-07-14 04:23:42 +00:00
|
|
|
/**
|
|
|
|
* API module for serving debug console requests on the edit page
|
|
|
|
*/
|
|
|
|
class ApiScribuntoConsole extends ApiBase {
|
2020-05-30 00:32:16 +00:00
|
|
|
private const SC_MAX_SIZE = 500000;
|
|
|
|
private const SC_SESSION_EXPIRY = 3600;
|
2012-07-14 04:23:42 +00:00
|
|
|
|
2020-01-30 03:53:43 +00:00
|
|
|
/**
|
|
|
|
* @suppress PhanTypePossiblyInvalidDimOffset
|
|
|
|
*/
|
2012-07-14 04:23:42 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/**
|
|
|
|
* Execute the console
|
|
|
|
* @param array $params
|
|
|
|
* - 'title': (Title) Module being processed
|
|
|
|
* - 'content': (string) New module text
|
|
|
|
* - 'prevQuestions': (string[]) Previous values for 'question' in this session.
|
|
|
|
* - 'question': (string) Lua code to run.
|
|
|
|
* @return array Result data
|
|
|
|
*/
|
2014-10-10 09:02:03 +00:00
|
|
|
protected function runConsole( array $params ) {
|
2019-10-28 20:17:06 +00:00
|
|
|
$parser = MediaWikiServices::getInstance()->getParser();
|
2020-03-07 02:31:53 +00:00
|
|
|
$options = new ParserOptions( $this->getUser() );
|
2019-10-28 20:17:06 +00:00
|
|
|
$parser->startExternalParse( $params['title'], $options, Parser::OT_HTML, true );
|
|
|
|
$engine = Scribunto::getParserEngine( $parser );
|
2012-07-14 04:23:42 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-09-30 00:55:40 +00:00
|
|
|
public function needsToken() {
|
|
|
|
return 'csrf';
|
|
|
|
}
|
|
|
|
|
2014-10-29 18:33:40 +00:00
|
|
|
public function isInternal() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/** @inheritDoc */
|
2012-07-14 04:23:42 +00:00
|
|
|
public function getAllowedParams() {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [
|
|
|
|
'title' => [
|
2022-04-03 23:29:23 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
|
|
|
'content' => [
|
2022-04-03 23:29:23 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'text'
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
|
|
|
'session' => [
|
2022-04-03 23:29:23 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'integer',
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
|
|
|
'question' => [
|
2022-04-03 23:29:23 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'text',
|
|
|
|
ParamValidator::PARAM_REQUIRED => true,
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
|
|
|
'clear' => [
|
2022-04-03 23:29:23 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'boolean',
|
2017-06-15 17:19:00 +00:00
|
|
|
],
|
|
|
|
];
|
2012-07-14 04:23:42 +00:00
|
|
|
}
|
|
|
|
}
|