mediawiki-extensions-Parser.../ParserFunctions.php

142 lines
5.1 KiB
PHP
Raw Normal View History

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' );
}
/**
* 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';
$wgExtensionCredits['parserhook'][] = array(
'path' => __FILE__,
'name' => 'ParserFunctions',
'version' => '1.2.0',
2008-12-30 14:55:45 +00:00
'url' => 'http://www.mediawiki.org/wiki/Extension:ParserFunctions',
'author' => array('Tim Starling', 'Robert Rohde', 'Ross McClure', 'Juraj Simlovic'),
'description' => 'Enhance parser with logical functions',
'descriptionmsg' => 'pfunc_desc',
);
2007-01-05 02:13:39 +00:00
$wgAutoloadClasses['ExtParserFunctions'] = dirname(__FILE__).'/ParserFunctions_body.php';
$wgExtensionMessagesFiles['ParserFunctions'] = dirname(__FILE__) . '/ParserFunctions.i18n.php';
2007-01-05 02:13:39 +00:00
$wgHooks['LanguageGetMagic'][] = 'wfParserFunctionsLanguageGetMagic';
$wgParserTestFiles[] = dirname( __FILE__ ) . "/funcsParserTests.txt";
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' );
}
function wfParserFunctionsLanguageGetMagic( &$magicWords, $langCode ) {
require_once( dirname( __FILE__ ) . '/ParserFunctions.i18n.magic.php' );
foreach( efParserFunctionsWords( $langCode ) as $word => $trans )
$magicWords[$word] = $trans;
return true;
}
2007-01-05 02:13:39 +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;
function registerParser( &$parser ) {
global $wgPFEnableStringFunctions;
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' ) );
//String Functions
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' ));
}
return true;
}
/** Defer ParserClearState */
function clearState( &$parser ) {
if ( !is_null( $this->realObj ) ) {
$this->realObj->clearState( $parser );
}
return true;
}
2007-01-05 02:13:39 +00:00
/** Pass through function call */
function __call( $name, $args ) {
if ( is_null( $this->realObj ) ) {
$this->realObj = new ExtParserFunctions;
$this->realObj->clearState( $args[0] );
}
return call_user_func_array( array( $this->realObj, $name ), $args );
}
2007-01-05 02:13:39 +00:00
}