MediaWiki  1.34.0
Base.php
Go to the documentation of this file.
1 <?php
2 
29 abstract class ScribuntoEngineBase {
30 
31  // Flags for ScribuntoEngineBase::getResourceUsage()
32  const CPU_SECONDS = 1;
33  const MEM_PEAK_BYTES = 2;
34 
38  protected $title;
39 
43  protected $options;
44 
48  protected $modules = [];
49 
53  protected $parser;
54 
62  abstract protected function newModule( $text, $chunkName );
63 
77  abstract public function runConsole( array $params );
78 
83  abstract public function getSoftwareInfo( array &$software );
84 
89  public function __construct( array $options ) {
90  $this->options = $options;
91  if ( isset( $options['parser'] ) ) {
92  $this->parser = $options['parser'];
93  }
94  if ( isset( $options['title'] ) ) {
95  $this->title = $options['title'];
96  }
97  }
98 
99  public function __destruct() {
100  $this->destroy();
101  }
102 
103  public function destroy() {
104  // Break reference cycles
105  $this->parser = null;
106  $this->title = null;
107  $this->modules = null;
108  }
109 
113  public function setTitle( $title ) {
114  $this->title = $title;
115  }
116 
120  public function getTitle() {
121  return $this->title;
122  }
123 
130  public function getOption( $optionName ) {
131  return $this->options[$optionName] ?? null;
132  }
133 
139  public function newException( $message, array $params = [] ) {
140  return new ScribuntoException( $message, $this->getDefaultExceptionParams() + $params );
141  }
142 
146  public function getDefaultExceptionParams() {
147  $params = [];
148  if ( $this->title ) {
149  $params['title'] = $this->title;
150  }
151  return $params;
152  }
153 
164  public function fetchModuleFromParser( Title $title ) {
165  $key = $title->getPrefixedDBkey();
166  if ( !array_key_exists( $key, $this->modules ) ) {
167  list( $text, $finalTitle ) = $this->parser->fetchTemplateAndTitle( $title );
168  if ( $text === false ) {
169  $this->modules[$key] = null;
170  return null;
171  }
172 
173  $finalKey = $finalTitle->getPrefixedDBkey();
174  if ( !isset( $this->modules[$finalKey] ) ) {
175  $this->modules[$finalKey] = $this->newModule( $text, $finalKey );
176  }
177  // Almost certainly $key === $finalKey, but just in case...
178  $this->modules[$key] = $this->modules[$finalKey];
179  }
180  return $this->modules[$key];
181  }
182 
191  public function validate( $text, $chunkName = false ) {
192  $module = $this->newModule( $text, $chunkName );
193  return $module->validate();
194  }
195 
208  public function getResourceUsage( $resource ) {
209  return false;
210  }
211 
216  public function getGeSHiLanguage() {
217  return false;
218  }
219 
224  public function getCodeEditorLanguage() {
225  return false;
226  }
227 
231  public function getParser() {
232  return $this->parser;
233  }
234 
249  protected function getLibraries( $engine, array $coreLibraries = [] ) {
250  $extraLibraries = [];
251  Hooks::run( 'ScribuntoExternalLibraries', [ $engine, &$extraLibraries ] );
252  return $coreLibraries + $extraLibraries;
253  }
254 
262  protected function getLibraryPaths( $engine, array $coreLibraryPaths = [] ) {
263  $extraLibraryPaths = [];
264  Hooks::run( 'ScribuntoExternalLibraryPaths', [ $engine, &$extraLibraryPaths ] );
265  return array_merge( $coreLibraryPaths, $extraLibraryPaths );
266  }
267 
274  public function reportLimitData( ParserOutput $output ) {
275  }
276 
287  public function formatLimitData( $key, &$value, &$report, $isHTML, $localize ) {
288  return true;
289  }
290 }
291 
296 abstract class ScribuntoModuleBase {
300  protected $engine;
301 
305  protected $code;
306 
310  protected $chunkName;
311 
318  $this->engine = $engine;
319  $this->code = $code;
320  $this->chunkName = $chunkName;
321  }
322 
326  public function getEngine() {
327  return $this->engine;
328  }
329 
333  public function getCode() {
334  return $this->code;
335  }
336 
340  public function getChunkName() {
341  return $this->chunkName;
342  }
343 
350  abstract public function validate();
351 
359  abstract public function invoke( $name, $frame );
360 }
ScribuntoEngineBase\MEM_PEAK_BYTES
const MEM_PEAK_BYTES
Definition: Base.php:33
ParserOutput
Definition: ParserOutput.php:25
ScribuntoEngineBase\destroy
destroy()
Definition: Base.php:103
ScribuntoEngineBase\runConsole
runConsole(array $params)
Run an interactive console request.
ScribuntoEngineBase\CPU_SECONDS
const CPU_SECONDS
Definition: Base.php:32
ScribuntoEngineBase\getCodeEditorLanguage
getCodeEditorLanguage()
Get the language for Ace code editor.
Definition: Base.php:224
ScribuntoModuleBase\getEngine
getEngine()
Definition: Base.php:326
Title\getPrefixedDBkey
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1806
ScribuntoModuleBase\__construct
__construct(ScribuntoEngineBase $engine, $code, $chunkName)
Definition: Base.php:317
ScribuntoEngineBase\getOption
getOption( $optionName)
Get an element from the configuration array.
Definition: Base.php:130
ScribuntoEngineBase\reportLimitData
reportLimitData(ParserOutput $output)
Add limit report data to a ParserOutput object.
Definition: Base.php:274
ScribuntoEngineBase\formatLimitData
formatLimitData( $key, &$value, &$report, $isHTML, $localize)
Format limit report data.
Definition: Base.php:287
ScribuntoEngineBase\$options
array $options
Definition: Base.php:43
ScribuntoEngineBase\getSoftwareInfo
getSoftwareInfo(array &$software)
Get software information for Special:Version.
ScribuntoEngineBase\getGeSHiLanguage
getGeSHiLanguage()
Get the language for GeSHi syntax highlighter.
Definition: Base.php:216
ScribuntoModuleBase\getCode
getCode()
Definition: Base.php:333
ScribuntoEngineBase\$modules
ScribuntoModuleBase[] $modules
Definition: Base.php:48
ScribuntoEngineBase\__construct
__construct(array $options)
Definition: Base.php:89
ScribuntoModuleBase
Class that represents a module.
Definition: Base.php:296
ScribuntoEngineBase\getLibraryPaths
getLibraryPaths( $engine, array $coreLibraryPaths=[])
Load a list of all paths libraries can be in for this engine.
Definition: Base.php:262
ScribuntoEngineBase\fetchModuleFromParser
fetchModuleFromParser(Title $title)
Load a module from some parser-defined template loading mechanism and register a parser output depend...
Definition: Base.php:164
ScribuntoEngineBase\validate
validate( $text, $chunkName=false)
Validates the script and returns a Status object containing the syntax errors for the given code.
Definition: Base.php:191
ScribuntoModuleBase\$chunkName
string bool $chunkName
Definition: Base.php:310
ScribuntoModuleBase\getChunkName
getChunkName()
Definition: Base.php:340
ScribuntoEngineBase\__destruct
__destruct()
Definition: Base.php:99
ScribuntoEngineBase\getTitle
getTitle()
Definition: Base.php:120
ScribuntoEngineBase\getParser
getParser()
Definition: Base.php:231
ScribuntoEngineBase\getLibraries
getLibraries( $engine, array $coreLibraries=[])
Load a list of all libraries supported by this engine.
Definition: Base.php:249
$output
$output
Definition: SyntaxHighlight.php:335
ScribuntoEngineBase\setTitle
setTitle( $title)
Definition: Base.php:113
ScribuntoEngineBase\getDefaultExceptionParams
getDefaultExceptionParams()
Definition: Base.php:146
ScribuntoModuleBase\validate
validate()
Validates the script and returns a Status object containing the syntax errors for the given code.
ScribuntoEngineBase\$parser
Parser $parser
Definition: Base.php:53
ScribuntoModuleBase\$engine
ScribuntoEngineBase $engine
Definition: Base.php:300
ScribuntoEngineBase
Wikitext scripting infrastructure for MediaWiki: base classes.
Definition: Base.php:29
ScribuntoException
An exception class which represents an error in the script.
Definition: Common.php:136
ScribuntoModuleBase\invoke
invoke( $name, $frame)
Invoke the function with the specified name.
ScribuntoEngineBase\newModule
newModule( $text, $chunkName)
Creates a new module object within this engine.
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ScribuntoEngineBase\newException
newException( $message, array $params=[])
Definition: Base.php:139
ScribuntoModuleBase\$code
string $code
Definition: Base.php:305
ScribuntoEngineBase\getResourceUsage
getResourceUsage( $resource)
Get CPU and memory usage information, if the script engine provides it.
Definition: Base.php:208
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
ScribuntoEngineBase\$title
Title $title
Definition: Base.php:38