MediaWiki  1.34.0
ApiScribuntoConsole.php
Go to the documentation of this file.
1 <?php
2 
7 class ApiScribuntoConsole extends ApiBase {
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 
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',
143  ApiBase::PARAM_REQUIRED => true,
144  ],
145  'clear' => [
146  ApiBase::PARAM_TYPE => 'boolean',
147  ],
148  ];
149  }
150 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:42
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
$wgParser
$wgParser
Definition: Setup.php:892
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiScribuntoConsole\newSession
newSession()
Definition: ApiScribuntoConsole.php:117
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiScribuntoConsole
API module for serving debug console requests on the edit page.
Definition: ApiScribuntoConsole.php:7
Scribunto\getParserEngine
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
ScribuntoException\getMessageName
getMessageName()
Definition: Common.php:191
ApiScribuntoConsole\isInternal
isInternal()
Indicates whether this module is "internal" Internal API modules are not (yet) intended for 3rd party...
Definition: ApiScribuntoConsole.php:126
ObjectCache\getInstance
static getInstance( $id)
Get a cached instance of the specified type of cache object.
Definition: ObjectCache.php:80
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
$title
$title
Definition: testCompression.php:34
ScribuntoException\getScriptTraceHtml
getScriptTraceHtml( $options=[])
Get the backtrace as HTML, or false if there is none available.
Definition: Common.php:206
ApiScribuntoConsole\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiScribuntoConsole.php:11
ApiScribuntoConsole\runConsole
runConsole(array $params)
Definition: ApiScribuntoConsole.php:83
ContextSource\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
ApiScribuntoConsole\SC_MAX_SIZE
const SC_MAX_SIZE
Definition: ApiScribuntoConsole.php:8
CACHE_ANYTHING
const CACHE_ANYTHING
Definition: Defines.php:81
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1551
ScribuntoException
An exception class which represents an error in the script.
Definition: Common.php:136
ApiScribuntoConsole\SC_SESSION_EXPIRY
const SC_SESSION_EXPIRY
Definition: ApiScribuntoConsole.php:9
OT_HTML
const OT_HTML
Definition: Defines.php:164
$cache
$cache
Definition: mcc.php:33
ApiScribuntoConsole\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiScribuntoConsole.php:130