2012-01-28 16:22:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2012-04-06 05:04:30 +00:00
|
|
|
* Static function collection for general extension support.
|
2012-01-28 16:22:18 +00:00
|
|
|
*/
|
2012-04-06 05:04:30 +00:00
|
|
|
class Scribunto {
|
2012-04-05 07:58:02 +00:00
|
|
|
/**
|
|
|
|
* Create a new engine object with specified parameters.
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
|
|
|
* @param array $options
|
2012-06-15 23:34:35 +00:00
|
|
|
* @return ScribuntoEngineBase
|
2012-04-05 07:58:02 +00:00
|
|
|
*/
|
|
|
|
public static function newEngine( $options ) {
|
2017-06-24 21:58:12 +00:00
|
|
|
if ( isset( $options['factory'] ) ) {
|
|
|
|
return call_user_func( $options['factory'], $options );
|
|
|
|
} else {
|
|
|
|
$class = $options['class'];
|
|
|
|
return new $class( $options );
|
|
|
|
}
|
2012-04-05 07:58:02 +00:00
|
|
|
}
|
2012-01-28 16:46:15 +00:00
|
|
|
|
2012-04-05 07:58:02 +00:00
|
|
|
/**
|
|
|
|
* Create a new engine object with default parameters
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param array $extraOptions Extra options to pass to the constructor,
|
2015-06-26 16:37:34 +00:00
|
|
|
* in addition to the configured options
|
2012-06-15 23:34:35 +00:00
|
|
|
* @throws MWException
|
|
|
|
* @return ScribuntoEngineBase
|
2012-04-05 07:58:02 +00:00
|
|
|
*/
|
2017-06-15 17:19:00 +00:00
|
|
|
public static function newDefaultEngine( $extraOptions = [] ) {
|
2012-04-06 05:04:30 +00:00
|
|
|
global $wgScribuntoDefaultEngine, $wgScribuntoEngineConf;
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( !$wgScribuntoDefaultEngine ) {
|
2016-05-17 14:52:05 +00:00
|
|
|
throw new MWException(
|
|
|
|
'Scribunto extension is enabled but $wgScribuntoDefaultEngine is not set'
|
|
|
|
);
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-01-28 16:46:15 +00:00
|
|
|
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( !isset( $wgScribuntoEngineConf[$wgScribuntoDefaultEngine] ) ) {
|
2012-04-06 05:04:30 +00:00
|
|
|
throw new MWException( 'Invalid scripting engine is specified in $wgScribuntoDefaultEngine' );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-06 05:04:30 +00:00
|
|
|
$options = $extraOptions + $wgScribuntoEngineConf[$wgScribuntoDefaultEngine];
|
2019-03-21 04:19:37 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgument false positive
|
2012-04-05 07:58:02 +00:00
|
|
|
return self::newEngine( $options );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-01-28 16:46:15 +00:00
|
|
|
|
2012-04-05 07:58:02 +00:00
|
|
|
/**
|
|
|
|
* Get an engine instance for the given parser, and cache it in the parser
|
|
|
|
* so that subsequent calls to this function for the same parser will return
|
|
|
|
* the same engine.
|
|
|
|
*
|
|
|
|
* @param Parser $parser
|
2014-10-10 09:02:03 +00:00
|
|
|
* @return ScribuntoEngineBase
|
2012-04-05 07:58:02 +00:00
|
|
|
*/
|
2014-10-10 09:02:03 +00:00
|
|
|
public static function getParserEngine( Parser $parser ) {
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( empty( $parser->scribunto_engine ) ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
$parser->scribunto_engine = self::newDefaultEngine( [ 'parser' => $parser ] );
|
2012-04-23 11:36:13 +00:00
|
|
|
$parser->scribunto_engine->setTitle( $parser->getTitle() );
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-04-06 05:04:30 +00:00
|
|
|
return $parser->scribunto_engine;
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2012-01-28 16:46:15 +00:00
|
|
|
|
2012-04-13 10:38:12 +00:00
|
|
|
/**
|
|
|
|
* Check if an engine instance is present in the given parser
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
|
|
|
* @param Parser $parser
|
|
|
|
* @return bool
|
2012-04-13 10:38:12 +00:00
|
|
|
*/
|
2014-10-10 09:02:03 +00:00
|
|
|
public static function isParserEnginePresent( Parser $parser ) {
|
2012-04-13 10:38:12 +00:00
|
|
|
return !empty( $parser->scribunto_engine );
|
|
|
|
}
|
|
|
|
|
2012-04-05 07:58:02 +00:00
|
|
|
/**
|
|
|
|
* Remove the current engine instance from the parser
|
2017-09-24 17:38:14 +00:00
|
|
|
* @param Parser $parser
|
2012-04-05 07:58:02 +00:00
|
|
|
*/
|
2014-10-10 09:02:03 +00:00
|
|
|
public static function resetParserEngine( Parser $parser ) {
|
2012-09-05 09:39:58 +00:00
|
|
|
if ( !empty( $parser->scribunto_engine ) ) {
|
|
|
|
$parser->scribunto_engine->destroy();
|
|
|
|
$parser->scribunto_engine = null;
|
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|
2013-02-20 22:00:42 +00:00
|
|
|
|
|
|
|
/**
|
2013-03-08 17:01:50 +00:00
|
|
|
* Test whether the page should be considered a documentation page
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
|
|
|
* @param Title $title
|
2018-04-05 11:09:10 +00:00
|
|
|
* @param Title|null &$forModule Module for which this is a doc page
|
2017-08-11 04:28:16 +00:00
|
|
|
* @return bool
|
2013-02-20 22:00:42 +00:00
|
|
|
*/
|
2014-10-10 09:02:03 +00:00
|
|
|
public static function isDocPage( Title $title, Title &$forModule = null ) {
|
2013-03-08 17:01:50 +00:00
|
|
|
$docPage = wfMessage( 'scribunto-doc-page-name' )->inContentLanguage();
|
|
|
|
if ( $docPage->isDisabled() ) {
|
2013-02-20 22:00:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-08 17:01:50 +00:00
|
|
|
// Canonicalize the input pseudo-title. The unreplaced "$1" shouldn't
|
|
|
|
// cause a problem.
|
2013-08-01 15:55:37 +00:00
|
|
|
$docTitle = Title::newFromText( $docPage->plain() );
|
|
|
|
if ( !$docTitle ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$docPage = $docTitle->getPrefixedText();
|
2013-03-08 17:01:50 +00:00
|
|
|
|
|
|
|
// Make it into a regex, and match it against the input title
|
|
|
|
$docPage = str_replace( '\\$1', '(.+)', preg_quote( $docPage, '/' ) );
|
|
|
|
if ( preg_match( "/^$docPage$/", $title->getPrefixedText(), $m ) ) {
|
|
|
|
$forModule = Title::makeTitleSafe( NS_MODULE, $m[1] );
|
2013-06-07 22:21:31 +00:00
|
|
|
return $forModule !== null;
|
2013-03-08 17:01:50 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-02-20 22:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-08 17:01:50 +00:00
|
|
|
* Return the Title for the documentation page
|
2014-10-10 09:02:03 +00:00
|
|
|
*
|
|
|
|
* @param Title $title
|
2013-02-20 22:00:42 +00:00
|
|
|
* @return Title|null
|
|
|
|
*/
|
2014-10-10 09:02:03 +00:00
|
|
|
public static function getDocPage( Title $title ) {
|
2013-03-08 17:01:50 +00:00
|
|
|
$docPage = wfMessage( 'scribunto-doc-page-name', $title->getText() )->inContentLanguage();
|
|
|
|
if ( $docPage->isDisabled() ) {
|
2013-02-20 22:00:42 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-03-08 17:01:50 +00:00
|
|
|
return Title::newFromText( $docPage->plain() );
|
2013-02-20 22:00:42 +00:00
|
|
|
}
|
2012-01-28 16:22:18 +00:00
|
|
|
}
|