MediaWiki REL1_34
ApiScribuntoConsole.php
Go to the documentation of this file.
1<?php
2
8 const SC_MAX_SIZE = 500000;
9 const SC_SESSION_EXPIRY = 3600;
10
11 public function execute() {
12 $params = $this->extractRequestParams();
13
14 $title = Title::newFromText( $params['title'] );
15 if ( !$title ) {
16 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
17 }
18
19 if ( $params['session'] ) {
20 $sessionId = $params['session'];
21 } else {
22 $sessionId = mt_rand( 0, 0x7fffffff );
23 }
24
25 $cache = ObjectCache::getInstance( CACHE_ANYTHING );
26 $sessionKey = $cache->makeKey( 'scribunto-console', $this->getUser()->getId(), $sessionId );
27 $session = null;
28 $sessionIsNew = false;
29 if ( $params['session'] ) {
30 $session = $cache->get( $sessionKey );
31 }
32 if ( !isset( $session['version'] ) ) {
33 $session = $this->newSession();
34 $sessionIsNew = true;
35 }
36
37 // Create a variable holding the session which will be stored if there
38 // are no errors. If there are errors, we don't want to store the current
39 // question to the state builder array, since that will cause subsequent
40 // requests to fail.
41 $newSession = $session;
42
43 if ( !empty( $params['clear'] ) ) {
44 $newSession['size'] -= strlen( implode( '', $newSession['questions'] ) );
45 $newSession['questions'] = [];
46 $session['questions'] = [];
47 }
48 if ( strlen( $params['question'] ) ) {
49 $newSession['size'] += strlen( $params['question'] );
50 $newSession['questions'][] = $params['question'];
51 }
52 if ( $params['content'] ) {
53 $newSession['size'] += strlen( $params['content'] ) - strlen( $newSession['content'] );
54 $newSession['content'] = $params['content'];
55 }
56
57 if ( $newSession['size'] > self::SC_MAX_SIZE ) {
58 $this->dieWithError( 'scribunto-console-too-large' );
59 }
60 $result = $this->runConsole( [
61 'title' => $title,
62 'content' => $newSession['content'],
63 'prevQuestions' => $session['questions'],
64 'question' => $params['question'],
65 ] );
66
67 if ( $result['type'] === 'error' ) {
68 // Restore the questions array
69 $newSession['questions'] = $session['questions'];
70 }
71 $cache->set( $sessionKey, $newSession, self::SC_SESSION_EXPIRY );
72 $result['session'] = $sessionId;
73 $result['sessionSize'] = $newSession['size'];
74 $result['sessionMaxSize'] = self::SC_MAX_SIZE;
75 if ( $sessionIsNew ) {
76 $result['sessionIsNew'] = '';
77 }
78 foreach ( $result as $key => $value ) {
79 $this->getResult()->addValue( null, $key, $value );
80 }
81 }
82
83 protected function runConsole( array $params ) {
84 global $wgParser;
85 $options = new ParserOptions;
86 $wgParser->startExternalParse( $params['title'], $options, Parser::OT_HTML, true );
88 try {
89 $result = $engine->runConsole( $params );
90 } catch ( ScribuntoException $e ) {
91 $trace = $e->getScriptTraceHtml();
92 $message = $e->getMessage();
93 $html = Html::element( 'p', [], $message );
94 if ( $trace !== false ) {
95 $html .= Html::element( 'p',
96 [],
97 $this->msg( 'scribunto-common-backtrace' )->inContentLanguage()->text()
98 ) . $trace;
99 }
100
101 return [
102 'type' => 'error',
103 'html' => $html,
104 'message' => $message,
105 'messagename' => $e->getMessageName() ];
106 }
107 return [
108 'type' => 'normal',
109 'print' => strval( $result['print'] ),
110 'return' => strval( $result['return'] )
111 ];
112 }
113
117 protected function newSession() {
118 return [
119 'content' => '',
120 'questions' => [],
121 'size' => 0,
122 'version' => 1,
123 ];
124 }
125
126 public function isInternal() {
127 return true;
128 }
129
130 public function getAllowedParams() {
131 return [
132 'title' => [
133 ApiBase::PARAM_TYPE => 'string',
134 ],
135 'content' => [
136 ApiBase::PARAM_TYPE => 'text'
137 ],
138 'session' => [
139 ApiBase::PARAM_TYPE => 'integer',
140 ],
141 'question' => [
142 ApiBase::PARAM_TYPE => 'text',
144 ],
145 'clear' => [
146 ApiBase::PARAM_TYPE => 'boolean',
147 ],
148 ];
149 }
150}
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
$wgParser
Definition Setup.php:891
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:42
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition ApiBase.php:118
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:2014
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:94
getResult()
Get the result object.
Definition ApiBase.php:640
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
API module for serving debug console requests on the edit page.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
isInternal()
Indicates whether this module is "internal" Internal API modules are not (yet) intended for 3rd party...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Set options of the Parser.
An exception class which represents an error in the script.
Definition Common.php:136
getScriptTraceHtml( $options=[])
Get the backtrace as HTML, or false if there is none available.
Definition Common.php:206
static getParserEngine(Parser $parser)
Get an engine instance for the given parser, and cache it in the parser so that subsequent calls to t...
Definition Common.php:56
const CACHE_ANYTHING
Definition Defines.php:90
$cache
Definition mcc.php:33