2007-01-05 02:13:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
|
|
|
die( 'This file is a MediaWiki extension, it is not a valid entry point' );
|
|
|
|
}
|
|
|
|
|
2009-06-05 09:53:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* CONFIGURATION
|
|
|
|
* These variables may be overridden in LocalSettings.php after you include the
|
|
|
|
* extension file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the maximum length of a string that string functions are allowed to operate on
|
|
|
|
* Prevention against denial of service by string function abuses.
|
|
|
|
*/
|
|
|
|
$wgPFStringLengthLimit = 1000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable string functions.
|
|
|
|
*
|
|
|
|
* Set this to true if you want your users to be able to implement their own
|
|
|
|
* parsers in the ugliest, most inefficient programming language known to man:
|
|
|
|
* MediaWiki wikitext with ParserFunctions.
|
|
|
|
*
|
|
|
|
* WARNING: enabling this may have an adverse impact on the sanity of your users.
|
|
|
|
* An alternative, saner solution for embedding complex text processing in
|
|
|
|
* MediaWiki templates can be found at: http://www.mediawiki.org/wiki/Extension:Lua
|
|
|
|
*/
|
|
|
|
$wgPFEnableStringFunctions = false;
|
|
|
|
|
|
|
|
/** REGISTRATION */
|
2007-01-05 02:13:39 +00:00
|
|
|
$wgExtensionFunctions[] = 'wfSetupParserFunctions';
|
2007-01-07 13:57:35 +00:00
|
|
|
$wgExtensionCredits['parserhook'][] = array(
|
2009-04-26 05:22:33 +00:00
|
|
|
'path' => __FILE__,
|
2008-07-09 17:46:09 +00:00
|
|
|
'name' => 'ParserFunctions',
|
2009-08-12 13:08:25 +00:00
|
|
|
'version' => '1.3.0',
|
2008-12-30 14:55:45 +00:00
|
|
|
'url' => 'http://www.mediawiki.org/wiki/Extension:ParserFunctions',
|
2009-05-26 00:43:34 +00:00
|
|
|
'author' => array('Tim Starling', 'Robert Rohde', 'Ross McClure', 'Juraj Simlovic'),
|
2008-07-09 17:46:09 +00:00
|
|
|
'description' => 'Enhance parser with logical functions',
|
2008-02-05 18:52:55 +00:00
|
|
|
'descriptionmsg' => 'pfunc_desc',
|
2007-01-07 13:57:35 +00:00
|
|
|
);
|
2007-01-05 02:13:39 +00:00
|
|
|
|
2009-06-05 09:53:05 +00:00
|
|
|
$wgAutoloadClasses['ExtParserFunctions'] = dirname(__FILE__).'/ParserFunctions_body.php';
|
2008-11-30 03:15:22 +00:00
|
|
|
$wgExtensionMessagesFiles['ParserFunctions'] = dirname(__FILE__) . '/ParserFunctions.i18n.php';
|
2007-11-29 10:42:48 +00:00
|
|
|
|
2008-11-30 03:15:22 +00:00
|
|
|
$wgParserTestFiles[] = dirname( __FILE__ ) . "/funcsParserTests.txt";
|
2008-10-27 17:56:58 +00:00
|
|
|
|
2009-06-05 09:53:05 +00:00
|
|
|
function wfSetupParserFunctions() {
|
|
|
|
global $wgParser, $wgPFHookStub, $wgHooks;
|
|
|
|
|
|
|
|
$wgPFHookStub = new ParserFunctions_HookStub;
|
|
|
|
|
|
|
|
// Check for SFH_OBJECT_ARGS capability
|
|
|
|
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
|
|
|
|
$wgHooks['ParserFirstCallInit'][] = array( &$wgPFHookStub, 'registerParser' );
|
|
|
|
} else {
|
|
|
|
if ( class_exists( 'StubObject' ) && !StubObject::isRealObject( $wgParser ) ) {
|
|
|
|
$wgParser->_unstub();
|
|
|
|
}
|
|
|
|
$wgPFHookStub->registerParser( $wgParser );
|
|
|
|
}
|
|
|
|
|
|
|
|
$wgHooks['ParserClearState'][] = array( &$wgPFHookStub, 'clearState' );
|
2009-05-26 00:43:34 +00:00
|
|
|
}
|
|
|
|
|
2009-06-05 09:53:05 +00:00
|
|
|
/**
|
|
|
|
* Stub class to defer loading of the bulk of the code until a parser function is
|
|
|
|
* actually used.
|
|
|
|
*/
|
|
|
|
class ParserFunctions_HookStub {
|
|
|
|
var $realObj;
|
2009-05-26 00:43:34 +00:00
|
|
|
|
2008-01-17 08:58:24 +00:00
|
|
|
function registerParser( &$parser ) {
|
2009-06-05 09:53:05 +00:00
|
|
|
global $wgPFEnableStringFunctions;
|
|
|
|
|
2008-01-17 08:58:24 +00:00
|
|
|
if ( defined( get_class( $parser ) . '::SFH_OBJECT_ARGS' ) ) {
|
|
|
|
// These functions accept DOM-style arguments
|
|
|
|
$parser->setFunctionHook( 'if', array( &$this, 'ifObj' ), SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifeq', array( &$this, 'ifeqObj' ), SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'switch', array( &$this, 'switchObj' ), SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifexist', array( &$this, 'ifexistObj' ), SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'ifexpr', array( &$this, 'ifexprObj' ), SFH_OBJECT_ARGS );
|
|
|
|
$parser->setFunctionHook( 'iferror', array( &$this, 'iferrorObj' ), SFH_OBJECT_ARGS );
|
|
|
|
} else {
|
|
|
|
$parser->setFunctionHook( 'if', array( &$this, 'ifHook' ) );
|
|
|
|
$parser->setFunctionHook( 'ifeq', array( &$this, 'ifeq' ) );
|
|
|
|
$parser->setFunctionHook( 'switch', array( &$this, 'switchHook' ) );
|
|
|
|
$parser->setFunctionHook( 'ifexist', array( &$this, 'ifexist' ) );
|
|
|
|
$parser->setFunctionHook( 'ifexpr', array( &$this, 'ifexpr' ) );
|
|
|
|
$parser->setFunctionHook( 'iferror', array( &$this, 'iferror' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$parser->setFunctionHook( 'expr', array( &$this, 'expr' ) );
|
|
|
|
$parser->setFunctionHook( 'time', array( &$this, 'time' ) );
|
|
|
|
$parser->setFunctionHook( 'timel', array( &$this, 'localTime' ) );
|
|
|
|
$parser->setFunctionHook( 'rel2abs', array( &$this, 'rel2abs' ) );
|
|
|
|
$parser->setFunctionHook( 'titleparts', array( &$this, 'titleparts' ) );
|
|
|
|
|
2009-05-26 00:43:34 +00:00
|
|
|
//String Functions
|
2009-06-05 09:53:05 +00:00
|
|
|
if ( $wgPFEnableStringFunctions ) {
|
|
|
|
$parser->setFunctionHook( 'len', array(&$this, 'runLen' ));
|
|
|
|
$parser->setFunctionHook( 'pos', array(&$this, 'runPos' ));
|
|
|
|
$parser->setFunctionHook( 'rpos', array(&$this, 'runRPos' ));
|
|
|
|
$parser->setFunctionHook( 'sub', array(&$this, 'runSub' ));
|
|
|
|
$parser->setFunctionHook( 'count', array(&$this, 'runCount' ));
|
|
|
|
$parser->setFunctionHook( 'replace', array(&$this, 'runReplace' ));
|
|
|
|
$parser->setFunctionHook( 'explode', array(&$this, 'runExplode' ));
|
2007-12-09 08:11:38 +00:00
|
|
|
}
|
2007-12-10 06:04:34 +00:00
|
|
|
|
2009-06-05 09:53:05 +00:00
|
|
|
return true;
|
2009-05-26 00:43:34 +00:00
|
|
|
}
|
|
|
|
|
2009-06-05 09:53:05 +00:00
|
|
|
/** Defer ParserClearState */
|
|
|
|
function clearState( &$parser ) {
|
|
|
|
if ( !is_null( $this->realObj ) ) {
|
|
|
|
$this->realObj->clearState( $parser );
|
2009-05-26 00:43:34 +00:00
|
|
|
}
|
2009-06-05 09:53:05 +00:00
|
|
|
return true;
|
2009-05-26 00:43:34 +00:00
|
|
|
}
|
2007-01-05 02:13:39 +00:00
|
|
|
|
2009-06-05 09:53:05 +00:00
|
|
|
/** Pass through function call */
|
|
|
|
function __call( $name, $args ) {
|
|
|
|
if ( is_null( $this->realObj ) ) {
|
|
|
|
$this->realObj = new ExtParserFunctions;
|
|
|
|
$this->realObj->clearState( $args[0] );
|
2008-01-17 08:58:24 +00:00
|
|
|
}
|
2009-06-05 09:53:05 +00:00
|
|
|
return call_user_func_array( array( $this->realObj, $name ), $args );
|
2007-11-20 14:16:10 +00:00
|
|
|
}
|
2007-01-05 02:13:39 +00:00
|
|
|
}
|