MediaWiki REL1_34
Common.php
Go to the documentation of this file.
1<?php
2
6class 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
141
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}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
const NS_MODULE
MediaWiki exception.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:74
setTitle(Title $t=null)
Set the context title.
Definition Parser.php:915
getTitle()
Accessor for the Title object.
Definition Parser.php:935
An exception class which represents an error in the script.
Definition Common.php:136
__construct( $messageName, $params=[])
Definition Common.php:156
getScriptTraceHtml( $options=[])
Get the backtrace as HTML, or false if there is none available.
Definition Common.php:206
string $messageName
Definition Common.php:140
Static function collection for general extension support.
Definition Common.php:6
static newDefaultEngine( $extraOptions=[])
Create a new engine object with default parameters.
Definition Common.php:32
static isParserEnginePresent(Parser $parser)
Check if an engine instance is present in the given parser.
Definition Common.php:70
static newEngine( $options)
Create a new engine object with specified parameters.
Definition Common.php:15
static isDocPage(Title $title, Title &$forModule=null)
Test whether the page should be considered a documentation page.
Definition Common.php:92
const LOCAL
Definition Common.php:7
static getDocPage(Title $title)
Return the Title for the documentation page.
Definition Common.php:122
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
static resetParserEngine(Parser $parser)
Remove the current engine instance from the parser.
Definition Common.php:78
Represents a title within MediaWiki.
Definition Title.php:42