Debian Codegraph @

extensions/Scribunto/includes/Scribunto.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Scribunto;
4
5 use MediaWiki\Config\ConfigException;
6 use MediaWiki\MediaWikiServices;
7 use MediaWiki\Parser\Parser;
8 use MediaWiki\Title\Title;
9
10 /**
11 * Static function collection for general extension support.
12 */
13 class Scribunto {
14 /**
15 * Create a new engine object with specified parameters.
16 *
17 * @param array $options
18 * @return ScribuntoEngineBase
19 */
20 public static function newEngine( $options ) {
21 if ( isset( $options['factory'] ) ) {
22 return call_user_func( $options['factory'], $options );
23 } else {
24 $class = $options['class'];
25 return new $class( $options );
26 }
27 }
28
29 /**
30 * Create a new engine object with default parameters
31 *
32 * @param array $extraOptions Extra options to pass to the constructor,
33 * in addition to the configured options
34 * @return ScribuntoEngineBase
35 */
36 public static function newDefaultEngine( $extraOptions = [] ) {
37 $config = MediaWikiServices::getInstance()->getMainConfig();
38 $defaultEngine = $config->get( 'ScribuntoDefaultEngine' );
39 if ( !$defaultEngine ) {
40 throw new ConfigException(
41 'Scribunto extension is enabled but $wgScribuntoDefaultEngine is not set'
42 );
43 }
44
45 $engineConf = $config->get( 'ScribuntoEngineConf' );
46 if ( !isset( $engineConf[$defaultEngine] ) ) {
47 throw new ConfigException( 'Invalid scripting engine is specified in $wgScribuntoDefaultEngine' );
48 }
49 $options = $extraOptions + $engineConf[$defaultEngine];
50 // @phan-suppress-next-line PhanTypeMismatchArgument false positive
51 return self::newEngine( $options );
52 }
53
54 /**
55 * Get an engine instance for the given parser, and cache it in the parser
56 * so that subsequent calls to this function for the same parser will return
57 * the same engine.
58 *
59 * @param Parser $parser
60 * @return ScribuntoEngineBase
61 */
62 public static function getParserEngine( Parser $parser ) {
63 if ( !isset( $parser->scribunto_engine ) ) {
64 $parser->scribunto_engine = self::newDefaultEngine( [ 'parser' => $parser ] );
65 $parser->scribunto_engine->setTitle( $parser->getTitle() );
66 }
67 return $parser->scribunto_engine;
68 }
69
70 /**
71 * Check if an engine instance is present in the given parser
72 *
73 * @param Parser $parser
74 * @return bool
75 */
76 public static function isParserEnginePresent( Parser $parser ) {
77 return isset( $parser->scribunto_engine );
78 }
79
80 /**
81 * Remove the current engine instance from the parser
82 * @param Parser $parser
83 */
84 public static function resetParserEngine( Parser $parser ) {
85 if ( isset( $parser->scribunto_engine ) ) {
86 $parser->scribunto_engine->destroy();
87 $parser->scribunto_engine = null;
88 }
89 }
90
91 /**
92 * Test whether the page should be considered a documentation page
93 *
94 * @param Title $title
95 * @param Title|null &$forModule Module for which this is a doc page
96 * @return bool
97 */
98 public static function isDocPage( Title $title, ?Title &$forModule = null ) {
99 $docPage = wfMessage( 'scribunto-doc-page-name' )->inContentLanguage();
100 if ( $docPage->isDisabled() ) {
101 return false;
102 }
103
104 // Canonicalize the input pseudo-title. The unreplaced "$1" shouldn't
105 // cause a problem.
106 $docTitle = Title::newFromText( $docPage->plain() );
107 if ( !$docTitle ) {
108 return false;
109 }
110 $docPage = $docTitle->getPrefixedText();
111
112 // Make it into a regex, and match it against the input title
113 $docPage = str_replace( '\\$1', '(.+)', preg_quote( $docPage, '/' ) );
114 if ( preg_match( "/^$docPage$/", $title->getPrefixedText(), $m ) ) {
115 $forModule = Title::makeTitleSafe( NS_MODULE, $m[1] );
116 return $forModule !== null;
117 } else {
118 return false;
119 }
120 }
121
122 /**
123 * Return the Title for the documentation page
124 *
125 * @param Title $title
126 * @return Title|null
127 */
128 public static function getDocPage( Title $title ) {
129 $docPage = wfMessage( 'scribunto-doc-page-name', $title->getText() )->inContentLanguage();
130 if ( $docPage->isDisabled() ) {
131 return null;
132 }
133
134 return Title::newFromText( $docPage->plain() );
135 }
136 }