MediaWiki  1.34.0
Common.php
Go to the documentation of this file.
1 <?php
2 
6 class Scribunto {
7  const LOCAL = 'local';
8 
15  public static function newEngine( $options ) {
16  if ( isset( $options['factory'] ) ) {
17  return call_user_func( $options['factory'], $options );
18  } else {
19  $class = $options['class'];
20  return new $class( $options );
21  }
22  }
23 
32  public static function newDefaultEngine( $extraOptions = [] ) {
33  global $wgScribuntoDefaultEngine, $wgScribuntoEngineConf;
34  if ( !$wgScribuntoDefaultEngine ) {
35  throw new MWException(
36  'Scribunto extension is enabled but $wgScribuntoDefaultEngine is not set'
37  );
38  }
39 
40  if ( !isset( $wgScribuntoEngineConf[$wgScribuntoDefaultEngine] ) ) {
41  throw new MWException( 'Invalid scripting engine is specified in $wgScribuntoDefaultEngine' );
42  }
43  $options = $extraOptions + $wgScribuntoEngineConf[$wgScribuntoDefaultEngine];
44  // @phan-suppress-next-line PhanTypeMismatchArgument false positive
45  return self::newEngine( $options );
46  }
47 
56  public static function getParserEngine( Parser $parser ) {
57  if ( empty( $parser->scribunto_engine ) ) {
58  $parser->scribunto_engine = self::newDefaultEngine( [ 'parser' => $parser ] );
59  $parser->scribunto_engine->setTitle( $parser->getTitle() );
60  }
61  return $parser->scribunto_engine;
62  }
63 
70  public static function isParserEnginePresent( Parser $parser ) {
71  return !empty( $parser->scribunto_engine );
72  }
73 
78  public static function resetParserEngine( Parser $parser ) {
79  if ( !empty( $parser->scribunto_engine ) ) {
80  $parser->scribunto_engine->destroy();
81  $parser->scribunto_engine = null;
82  }
83  }
84 
92  public static function isDocPage( Title $title, Title &$forModule = null ) {
93  $docPage = wfMessage( 'scribunto-doc-page-name' )->inContentLanguage();
94  if ( $docPage->isDisabled() ) {
95  return false;
96  }
97 
98  // Canonicalize the input pseudo-title. The unreplaced "$1" shouldn't
99  // cause a problem.
100  $docTitle = Title::newFromText( $docPage->plain() );
101  if ( !$docTitle ) {
102  return false;
103  }
104  $docPage = $docTitle->getPrefixedText();
105 
106  // Make it into a regex, and match it against the input title
107  $docPage = str_replace( '\\$1', '(.+)', preg_quote( $docPage, '/' ) );
108  if ( preg_match( "/^$docPage$/", $title->getPrefixedText(), $m ) ) {
109  $forModule = Title::makeTitleSafe( NS_MODULE, $m[1] );
110  return $forModule !== null;
111  } else {
112  return false;
113  }
114  }
115 
122  public static function getDocPage( Title $title ) {
123  $docPage = wfMessage( 'scribunto-doc-page-name', $title->getText() )->inContentLanguage();
124  if ( $docPage->isDisabled() ) {
125  return null;
126  }
127 
128  return Title::newFromText( $docPage->plain() );
129  }
130 }
131 
140  public $messageName;
141 
145  public $messageArgs;
146 
150  public $params;
151 
156  public function __construct( $messageName, $params = [] ) {
157  if ( isset( $params['args'] ) ) {
158  $this->messageArgs = $params['args'];
159  } else {
160  $this->messageArgs = [];
161  }
162  if ( isset( $params['module'] ) && isset( $params['line'] ) ) {
163  $codeLocation = false;
164  if ( isset( $params['title'] ) ) {
165  $moduleTitle = Title::newFromText( $params['module'] );
166  if ( $moduleTitle && $moduleTitle->equals( $params['title'] ) ) {
167  $codeLocation = wfMessage( 'scribunto-line', $params['line'] )->inContentLanguage()->text();
168  }
169  }
170  if ( $codeLocation === false ) {
171  $codeLocation = wfMessage(
172  'scribunto-module-line',
173  $params['module'],
174  $params['line']
175  )->inContentLanguage()->text();
176  }
177  } else {
178  $codeLocation = '[UNKNOWN]';
179  }
180  array_unshift( $this->messageArgs, $codeLocation );
181  $msg = wfMessage( $messageName )->params( $this->messageArgs )->inContentLanguage()->text();
182  parent::__construct( $msg );
183 
184  $this->messageName = $messageName;
185  $this->params = $params;
186  }
187 
191  public function getMessageName() {
192  return $this->messageName;
193  }
194 
195  public function toStatus() {
196  $status = Status::newFatal( $this->messageName, ...$this->messageArgs );
197  $status->scribunto_error = $this;
198  return $status;
199  }
200 
206  public function getScriptTraceHtml( $options = [] ) {
207  return false;
208  }
209 }
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
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
ScribuntoException\$messageArgs
array $messageArgs
Definition: Common.php:145
Scribunto
Static function collection for general extension support.
Definition: Common.php:6
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
Scribunto\isParserEnginePresent
static isParserEnginePresent(Parser $parser)
Check if an engine instance is present in the given parser.
Definition: Common.php:70
ScribuntoException\$messageName
string $messageName
Definition: Common.php:140
ScribuntoException\__construct
__construct( $messageName, $params=[])
Definition: Common.php:156
Scribunto\newDefaultEngine
static newDefaultEngine( $extraOptions=[])
Create a new engine object with default parameters.
Definition: Common.php:32
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
MWException
MediaWiki exception.
Definition: MWException.php:26
ScribuntoException\getMessageName
getMessageName()
Definition: Common.php:191
NS_MODULE
const NS_MODULE
Definition: Scribunto.constants.php:5
Scribunto\LOCAL
const LOCAL
Definition: Common.php:7
$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
Scribunto\resetParserEngine
static resetParserEngine(Parser $parser)
Remove the current engine instance from the parser.
Definition: Common.php:78
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
Scribunto\getDocPage
static getDocPage(Title $title)
Return the Title for the documentation page.
Definition: Common.php:122
ScribuntoException\toStatus
toStatus()
Definition: Common.php:195
ScribuntoException\$params
array $params
Definition: Common.php:150
ScribuntoException
An exception class which represents an error in the script.
Definition: Common.php:136
Title
Represents a title within MediaWiki.
Definition: Title.php:42
$status
return $status
Definition: SyntaxHighlight.php:347
Scribunto\isDocPage
static isDocPage(Title $title, Title &$forModule=null)
Test whether the page should be considered a documentation page.
Definition: Common.php:92
Scribunto\newEngine
static newEngine( $options)
Create a new engine object with specified parameters.
Definition: Common.php:15