Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Scribunto
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 7
272
0.00% covered (danger)
0.00%
0 / 1
 newEngine
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 newDefaultEngine
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 getParserEngine
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 isParserEnginePresent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 resetParserEngine
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 isDocPage
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 getDocPage
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\Scribunto;
4
5use MediaWiki\Config\ConfigException;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Title\Title;
8use Parser;
9
10/**
11 * Static function collection for general extension support.
12 */
13class 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}