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' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$wgExtensionFunctions[] = 'wfSetupParserFunctions';
|
2007-01-07 13:57:35 +00:00
|
|
|
$wgExtensionCredits['parserhook'][] = array(
|
|
|
|
'name' => 'ParserFunctions',
|
2008-01-13 01:06:28 +00:00
|
|
|
'version' => '1.1',
|
2007-01-07 13:57:35 +00:00
|
|
|
'url' => 'http://meta.wikimedia.org/wiki/ParserFunctions',
|
|
|
|
'author' => 'Tim Starling',
|
|
|
|
'description' => 'Enhance parser with logical functions',
|
|
|
|
);
|
2007-01-05 02:13:39 +00:00
|
|
|
|
2008-01-13 01:06:28 +00:00
|
|
|
$wgExtensionMessagesFiles['ParserFunctions'] = dirname(__FILE__) . '/ParserFunctions.i18n.php';
|
2007-01-05 02:13:39 +00:00
|
|
|
$wgHooks['LanguageGetMagic'][] = 'wfParserFunctionsLanguageGetMagic';
|
2007-11-30 14:50:48 +00:00
|
|
|
$wgHooks['ParserLimitReport'][] = 'wfParserFunctionsLimitReport';
|
2007-01-05 02:13:39 +00:00
|
|
|
|
2007-11-29 10:42:48 +00:00
|
|
|
$wgMaxIfExistCount = 100;
|
|
|
|
|
2007-01-05 02:13:39 +00:00
|
|
|
class ExtParserFunctions {
|
|
|
|
var $mExprParser;
|
|
|
|
var $mTimeCache = array();
|
|
|
|
var $mTimeChars = 0;
|
|
|
|
var $mMaxTimeChars = 6000; # ~10 seconds
|
|
|
|
|
2007-11-29 10:42:48 +00:00
|
|
|
function clearState(&$parser) {
|
2007-01-05 02:13:39 +00:00
|
|
|
$this->mTimeChars = 0;
|
2007-11-29 10:42:48 +00:00
|
|
|
$parser->pf_ifexist_count = 0;
|
2007-12-10 06:04:34 +00:00
|
|
|
$parser->pf_ifexist_breakdown = array();
|
2007-01-05 02:13:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function &getExprParser() {
|
|
|
|
if ( !isset( $this->mExpr ) ) {
|
|
|
|
if ( !class_exists( 'ExprParser' ) ) {
|
|
|
|
require( dirname( __FILE__ ) . '/Expr.php' );
|
|
|
|
}
|
|
|
|
$this->mExprParser = new ExprParser;
|
|
|
|
}
|
|
|
|
return $this->mExprParser;
|
|
|
|
}
|
|
|
|
|
|
|
|
function expr( &$parser, $expr = '' ) {
|
|
|
|
try {
|
|
|
|
return $this->getExprParser()->doExpression( $expr );
|
|
|
|
} catch(ExprError $e) {
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function ifexpr( &$parser, $expr = '', $then = '', $else = '' ) {
|
|
|
|
try{
|
|
|
|
if($this->getExprParser()->doExpression( $expr )) {
|
|
|
|
return $then;
|
|
|
|
} else {
|
|
|
|
return $else;
|
|
|
|
}
|
|
|
|
} catch (ExprError $e){
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-09 08:11:38 +00:00
|
|
|
function ifexprObj( $parser, $frame, $args ) {
|
|
|
|
$expr = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
|
|
|
$then = isset( $args[1] ) ? $args[1] : '';
|
|
|
|
$else = isset( $args[2] ) ? $args[2] : '';
|
|
|
|
$result = $this->ifexpr( $parser, $expr, $then, $else );
|
|
|
|
if ( is_object( $result ) ) {
|
|
|
|
$result = trim( $frame->expand( $result ) );
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2007-01-05 02:13:39 +00:00
|
|
|
function ifHook( &$parser, $test = '', $then = '', $else = '' ) {
|
|
|
|
if ( $test !== '' ) {
|
|
|
|
return $then;
|
|
|
|
} else {
|
|
|
|
return $else;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-20 14:16:10 +00:00
|
|
|
function ifObj( &$parser, $frame, $args ) {
|
|
|
|
$test = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
|
|
|
if ( $test !== '' ) {
|
|
|
|
return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
|
|
|
|
} else {
|
|
|
|
return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-05 02:13:39 +00:00
|
|
|
function ifeq( &$parser, $left = '', $right = '', $then = '', $else = '' ) {
|
|
|
|
if ( $left == $right ) {
|
|
|
|
return $then;
|
|
|
|
} else {
|
|
|
|
return $else;
|
|
|
|
}
|
|
|
|
}
|
2007-01-06 20:56:46 +00:00
|
|
|
|
2007-11-20 14:16:10 +00:00
|
|
|
function ifeqObj( &$parser, $frame, $args ) {
|
|
|
|
$left = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
|
|
|
$right = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
|
|
|
|
if ( $left == $right ) {
|
|
|
|
return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : '';
|
|
|
|
} else {
|
|
|
|
return isset( $args[3] ) ? trim( $frame->expand( $args[3] ) ) : '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-15 04:21:17 +00:00
|
|
|
function iferror( &$parser, $test = '', $then = '', $else = '' ) {
|
|
|
|
if ( preg_match( '/<(strong|span) class="error"/', $test ) ) {
|
|
|
|
return $then;
|
|
|
|
} else {
|
|
|
|
return $else;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function iferrorObj( &$parser, $frame, $args ) {
|
|
|
|
$test = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
|
|
|
$then = isset( $args[1] ) ? $args[1] : false;
|
|
|
|
$else = isset( $args[2] ) ? $args[2] : false;
|
|
|
|
$result = $this->iferror( $parser, $test, $then, $else );
|
|
|
|
if ( $result === false ) {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return trim( $frame->expand( $result ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-05 02:13:39 +00:00
|
|
|
function switchHook( &$parser /*,...*/ ) {
|
|
|
|
$args = func_get_args();
|
|
|
|
array_shift( $args );
|
2007-12-09 08:11:38 +00:00
|
|
|
$primary = trim(array_shift($args));
|
2007-01-05 02:13:39 +00:00
|
|
|
$found = false;
|
|
|
|
$parts = null;
|
|
|
|
$default = null;
|
2007-11-20 14:16:10 +00:00
|
|
|
$mwDefault =& MagicWord::get( 'default' );
|
2007-01-05 02:13:39 +00:00
|
|
|
foreach( $args as $arg ) {
|
|
|
|
$parts = array_map( 'trim', explode( '=', $arg, 2 ) );
|
|
|
|
if ( count( $parts ) == 2 ) {
|
2007-12-09 08:11:38 +00:00
|
|
|
# Found "="
|
|
|
|
if ( $found || $parts[0] == $primary ) {
|
|
|
|
# Found a match, return now
|
2007-01-05 02:13:39 +00:00
|
|
|
return $parts[1];
|
|
|
|
} else {
|
|
|
|
if ( $mwDefault->matchStartAndRemove( $parts[0] ) ) {
|
|
|
|
$default = $parts[1];
|
|
|
|
} # else wrong case, continue
|
|
|
|
}
|
|
|
|
} elseif ( count( $parts ) == 1 ) {
|
|
|
|
# Multiple input, single output
|
|
|
|
# If the value matches, set a flag and continue
|
2007-12-09 08:11:38 +00:00
|
|
|
if ( $parts[0] == $primary ) {
|
2007-01-05 02:13:39 +00:00
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
} # else RAM corruption due to cosmic ray?
|
|
|
|
}
|
|
|
|
# Default case
|
|
|
|
# Check if the last item had no = sign, thus specifying the default case
|
|
|
|
if ( count( $parts ) == 1) {
|
|
|
|
return $parts[0];
|
|
|
|
} elseif ( !is_null( $default ) ) {
|
|
|
|
return $default;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-09 08:11:38 +00:00
|
|
|
function switchObj( $parser, $frame, $args ) {
|
|
|
|
if ( count( $args ) == 0 ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$primary = trim( $frame->expand( array_shift( $args ) ) );
|
|
|
|
$found = false;
|
|
|
|
$default = null;
|
|
|
|
$lastItemHadNoEquals = false;
|
|
|
|
$mwDefault =& MagicWord::get( 'default' );
|
|
|
|
foreach ( $args as $arg ) {
|
|
|
|
list( $nameNode, $index, $valueNode ) = $frame->splitBraceNode( $arg );
|
|
|
|
if ( $index === '' ) {
|
|
|
|
# Found "="
|
|
|
|
$lastItemHadNoEquals = false;
|
|
|
|
$test = trim( $frame->expand( $nameNode ) );
|
|
|
|
if ( $found ) {
|
|
|
|
# Multiple input match
|
|
|
|
return trim( $frame->expand( $valueNode ) );
|
|
|
|
} else {
|
|
|
|
$test = trim( $frame->expand( $nameNode ) );
|
|
|
|
if ( $test == $primary ) {
|
|
|
|
# Found a match, return now
|
|
|
|
return trim( $frame->expand( $valueNode ) );
|
|
|
|
} else {
|
|
|
|
if ( $mwDefault->matchStartAndRemove( $test ) ) {
|
|
|
|
$default = $valueNode;
|
|
|
|
} # else wrong case, continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
# Multiple input, single output
|
|
|
|
# If the value matches, set a flag and continue
|
|
|
|
$lastItemHadNoEquals = true;
|
|
|
|
$test = trim( $frame->expand( $valueNode ) );
|
|
|
|
if ( $test == $primary ) {
|
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Default case
|
|
|
|
# Check if the last item had no = sign, thus specifying the default case
|
|
|
|
if ( $lastItemHadNoEquals ) {
|
|
|
|
return $test;
|
|
|
|
} elseif ( !is_null( $default ) ) {
|
|
|
|
return trim( $frame->expand( $default ) );
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-05 02:13:39 +00:00
|
|
|
/**
|
|
|
|
* Returns the absolute path to a subpage, relative to the current article
|
|
|
|
* title. Treats titles as slash-separated paths.
|
|
|
|
*
|
2008-01-13 01:06:28 +00:00
|
|
|
* Following subpage link syntax instead of standard path syntax, an
|
2007-01-05 02:13:39 +00:00
|
|
|
* initial slash is treated as a relative path, and vice versa.
|
|
|
|
*/
|
|
|
|
public function rel2abs( &$parser , $to = '' , $from = '' ) {
|
|
|
|
|
|
|
|
$from = trim($from);
|
|
|
|
if( $from == '' ) {
|
2007-01-17 22:57:38 +00:00
|
|
|
$from = $parser->getTitle()->getPrefixedText();
|
2007-01-05 02:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$to = rtrim( $to , ' /' );
|
|
|
|
|
|
|
|
// if we have an empty path, or just one containing a dot
|
|
|
|
if( $to == '' || $to == '.' ) {
|
|
|
|
return $from;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the path isn't relative
|
|
|
|
if ( substr( $to , 0 , 1) != '/' &&
|
|
|
|
substr( $to , 0 , 2) != './' &&
|
|
|
|
substr( $to , 0 , 3) != '../' &&
|
|
|
|
$to != '..' )
|
|
|
|
{
|
|
|
|
$from = '';
|
|
|
|
}
|
|
|
|
// Make a long path, containing both, enclose it in /.../
|
2007-01-06 20:56:46 +00:00
|
|
|
$fullPath = '/' . $from . '/' . $to . '/';
|
2007-01-05 02:13:39 +00:00
|
|
|
|
|
|
|
// remove redundant current path dots
|
|
|
|
$fullPath = preg_replace( '!/(\./)+!', '/', $fullPath );
|
|
|
|
|
|
|
|
// remove double slashes
|
|
|
|
$fullPath = preg_replace( '!/{2,}!', '/', $fullPath );
|
|
|
|
|
|
|
|
// remove the enclosing slashes now
|
2007-01-06 20:56:46 +00:00
|
|
|
$fullPath = trim( $fullPath , '/' );
|
2007-01-05 02:13:39 +00:00
|
|
|
$exploded = explode ( '/' , $fullPath );
|
|
|
|
$newExploded = array();
|
|
|
|
|
|
|
|
foreach ( $exploded as $current ) {
|
|
|
|
if( $current == '..' ) { // removing one level
|
|
|
|
if( !count( $newExploded ) ){
|
|
|
|
// attempted to access a node above root node
|
2008-01-13 01:06:28 +00:00
|
|
|
wfLoadExtensionMessages( 'ParserFunctions' );
|
2007-11-19 15:37:49 +00:00
|
|
|
return '<strong class="error">' . wfMsgForContent( 'pfunc_rel2abs_invalid_depth', $fullPath ) . '</strong>';
|
2007-01-05 02:13:39 +00:00
|
|
|
}
|
|
|
|
// remove last level from the stack
|
2007-01-06 20:56:46 +00:00
|
|
|
array_pop( $newExploded );
|
2007-01-05 02:13:39 +00:00
|
|
|
} else {
|
|
|
|
// add the current level to the stack
|
2007-01-06 20:56:46 +00:00
|
|
|
$newExploded[] = $current;
|
2007-01-05 02:13:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can now join it again
|
|
|
|
return implode( '/' , $newExploded );
|
|
|
|
}
|
|
|
|
|
2007-12-10 06:04:34 +00:00
|
|
|
function incrementIfexistCount( $parser, $frame ) {
|
2007-11-29 10:42:48 +00:00
|
|
|
// Don't let this be called more than a certain number of times. It tends to make the database explode.
|
|
|
|
global $wgMaxIfExistCount;
|
2007-12-10 06:04:34 +00:00
|
|
|
$parser->pf_ifexist_count++;
|
|
|
|
if ( $frame ) {
|
|
|
|
$pdbk = $frame->getPDBK( 1 );
|
|
|
|
if ( !isset( $parser->pf_ifexist_breakdown[$pdbk] ) ) {
|
|
|
|
$parser->pf_ifexist_breakdown[$pdbk] = 0;
|
|
|
|
}
|
|
|
|
$parser->pf_ifexist_breakdown[$pdbk] ++;
|
|
|
|
}
|
|
|
|
return $parser->pf_ifexist_count <= $wgMaxIfExistCount;
|
|
|
|
}
|
2007-11-29 10:42:48 +00:00
|
|
|
|
2007-12-10 06:04:34 +00:00
|
|
|
function ifexist( &$parser, $title = '', $then = '', $else = '' ) {
|
|
|
|
return $this->ifexistCommon( $parser, false, $title, $then, $else );
|
|
|
|
}
|
|
|
|
|
|
|
|
function ifexistCommon( &$parser, $frame, $title = '', $then = '', $else = '' ) {
|
2007-01-05 02:13:39 +00:00
|
|
|
$title = Title::newFromText( $title );
|
2007-02-12 10:24:23 +00:00
|
|
|
if ( $title ) {
|
2007-11-13 15:33:05 +00:00
|
|
|
/* If namespace is specified as NS_MEDIA, then we want to check the physical file,
|
|
|
|
* not the "description" page.
|
|
|
|
*/
|
|
|
|
if( $title->getNamespace() == NS_MEDIA ) {
|
2007-12-10 06:04:34 +00:00
|
|
|
if ( !$this->incrementIfexistCount( $parser, $frame ) ) {
|
2007-12-09 08:11:38 +00:00
|
|
|
return $else;
|
|
|
|
}
|
2007-11-13 15:33:05 +00:00
|
|
|
$file = wfFindFile($title);
|
2007-12-10 06:04:34 +00:00
|
|
|
if ( !$file ) {
|
|
|
|
return $else;
|
|
|
|
}
|
2007-11-13 15:33:05 +00:00
|
|
|
$parser->mOutput->addImage($file->getName());
|
|
|
|
return $file->exists() ? $then : $else;
|
2007-12-10 06:04:34 +00:00
|
|
|
} else {
|
|
|
|
$pdbk = $title->getPrefixedDBkey();
|
|
|
|
$lc = LinkCache::singleton();
|
|
|
|
if ( $lc->getGoodLinkID( $pdbk ) ) {
|
|
|
|
return $then;
|
|
|
|
} elseif ( $lc->isBadLink( $pdbk ) ) {
|
|
|
|
return $else;
|
|
|
|
}
|
|
|
|
if ( !$this->incrementIfexistCount( $parser, $frame ) ) {
|
|
|
|
return $else;
|
|
|
|
}
|
2007-11-13 15:33:05 +00:00
|
|
|
|
2007-12-10 06:04:34 +00:00
|
|
|
$id = $title->getArticleID();
|
|
|
|
$parser->mOutput->addLink( $title, $id );
|
|
|
|
if ( $id ) {
|
|
|
|
return $then;
|
|
|
|
}
|
2007-02-12 10:24:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $else;
|
2007-01-05 02:13:39 +00:00
|
|
|
}
|
|
|
|
|
2007-12-09 08:11:38 +00:00
|
|
|
function ifexistObj( &$parser, $frame, $args ) {
|
|
|
|
$title = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
|
|
|
|
$then = isset( $args[1] ) ? $args[1] : null;
|
|
|
|
$else = isset( $args[2] ) ? $args[2] : null;
|
|
|
|
|
2007-12-10 06:04:34 +00:00
|
|
|
$result = $this->ifexistCommon( $parser, $frame, $title, $then, $else );
|
2007-12-09 08:11:38 +00:00
|
|
|
if ( $result === null ) {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return trim( $frame->expand( $result ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-01 18:00:45 +00:00
|
|
|
function time( &$parser, $format = '', $date = '', $local = false ) {
|
|
|
|
global $wgContLang, $wgLocaltimezone;
|
|
|
|
if ( isset( $this->mTimeCache[$format][$date][$local] ) ) {
|
|
|
|
return $this->mTimeCache[$format][$date][$local];
|
2007-01-05 02:13:39 +00:00
|
|
|
}
|
2007-01-06 20:56:46 +00:00
|
|
|
|
2007-01-05 02:13:39 +00:00
|
|
|
if ( $date !== '' ) {
|
|
|
|
$unix = @strtotime( $date );
|
|
|
|
} else {
|
|
|
|
$unix = time();
|
|
|
|
}
|
2007-01-06 20:56:46 +00:00
|
|
|
|
2007-01-05 02:13:39 +00:00
|
|
|
if ( $unix == -1 || $unix == false ) {
|
2008-01-13 01:06:28 +00:00
|
|
|
wfLoadExtensionMessages( 'ParserFunctions' );
|
2007-11-19 15:37:49 +00:00
|
|
|
$result = '<strong class="error">' . wfMsgForContent( 'pfunc_time_error' ) . '</strong>';
|
2007-01-05 02:13:39 +00:00
|
|
|
} else {
|
|
|
|
$this->mTimeChars += strlen( $format );
|
|
|
|
if ( $this->mTimeChars > $this->mMaxTimeChars ) {
|
2008-01-13 01:06:28 +00:00
|
|
|
wfLoadExtensionMessages( 'ParserFunctions' );
|
2007-11-19 15:37:49 +00:00
|
|
|
return '<strong class="error">' . wfMsgForContent( 'pfunc_time_too_long' ) . '</strong>';
|
2007-01-05 02:13:39 +00:00
|
|
|
} else {
|
2007-08-01 18:00:45 +00:00
|
|
|
if ( $local ) {
|
|
|
|
# Use the time zone
|
|
|
|
if ( isset( $wgLocaltimezone ) ) {
|
|
|
|
$oldtz = getenv( 'TZ' );
|
|
|
|
putenv( 'TZ='.$wgLocaltimezone );
|
|
|
|
}
|
|
|
|
wfSuppressWarnings(); // E_STRICT system time bitching
|
|
|
|
$ts = date( 'YmdHis', $unix );
|
|
|
|
wfRestoreWarnings();
|
|
|
|
if ( isset( $wgLocaltimezone ) ) {
|
|
|
|
putenv( 'TZ='.$oldtz );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$ts = wfTimestamp( TS_MW, $unix );
|
|
|
|
}
|
2007-01-05 02:13:39 +00:00
|
|
|
if ( method_exists( $wgContLang, 'sprintfDate' ) ) {
|
|
|
|
$result = $wgContLang->sprintfDate( $format, $ts );
|
|
|
|
} else {
|
|
|
|
if ( !class_exists( 'SprintfDateCompat' ) ) {
|
|
|
|
require( dirname( __FILE__ ) . '/SprintfDateCompat.php' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = SprintfDateCompat::sprintfDate( $format, $ts );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-01 18:00:45 +00:00
|
|
|
$this->mTimeCache[$format][$date][$local] = $result;
|
2007-01-05 02:13:39 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2008-01-13 01:06:28 +00:00
|
|
|
|
2007-08-01 18:00:45 +00:00
|
|
|
function localTime( &$parser, $format = '', $date = '' ) {
|
|
|
|
return $this->time( $parser, $format, $date, true );
|
|
|
|
}
|
2008-01-13 01:06:28 +00:00
|
|
|
|
2007-06-04 17:34:07 +00:00
|
|
|
/**
|
|
|
|
* Obtain a specified number of slash-separated parts of a title,
|
|
|
|
* e.g. {{#titleparts:Hello/World|1}} => "Hello"
|
|
|
|
*
|
|
|
|
* @param Parser $parser Parent parser
|
|
|
|
* @param string $title Title to split
|
|
|
|
* @param int $parts Number of parts to keep
|
2007-06-13 00:26:28 +00:00
|
|
|
* @param int $offset Offset starting at 1
|
2007-06-04 17:34:07 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2007-12-20 13:29:59 +00:00
|
|
|
public function titleparts( $parser, $title = '', $parts = 0, $offset = 0) {
|
2007-06-04 17:34:07 +00:00
|
|
|
$parts = intval( $parts );
|
2007-12-20 13:29:59 +00:00
|
|
|
$offset = intval( $offset );
|
2007-06-04 17:34:07 +00:00
|
|
|
$ntitle = Title::newFromText( $title );
|
2007-12-20 13:29:59 +00:00
|
|
|
if ( $ntitle instanceof Title ) {
|
|
|
|
$bits = explode( '/', $ntitle->getPrefixedText(), 25 );
|
|
|
|
if ( count( $bits ) <= 0 ) {
|
|
|
|
return $ntitle->getPrefixedText();
|
2007-06-04 17:34:07 +00:00
|
|
|
} else {
|
2007-12-20 13:29:59 +00:00
|
|
|
if ( $offset > 0 ) {
|
|
|
|
--$offset;
|
|
|
|
}
|
|
|
|
if ( $parts == 0 ) {
|
|
|
|
return implode( '/', array_slice( $bits, $offset ) );
|
|
|
|
} else {
|
|
|
|
return implode( '/', array_slice( $bits, $offset, $parts ) );
|
|
|
|
}
|
2007-06-04 17:34:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return $title;
|
|
|
|
}
|
2007-12-10 06:04:34 +00:00
|
|
|
}
|
2008-01-13 01:06:28 +00:00
|
|
|
|
2007-12-10 06:04:34 +00:00
|
|
|
function afterTidy( &$parser, &$text ) {
|
2007-12-11 09:04:46 +00:00
|
|
|
global $wgMaxIfExistCount;
|
|
|
|
if ( $parser->pf_ifexist_count > $wgMaxIfExistCount ) {
|
2007-12-10 06:04:34 +00:00
|
|
|
if ( is_callable( array( $parser->mOutput, 'addWarning' ) ) ) {
|
2008-01-13 01:06:28 +00:00
|
|
|
wfLoadExtensionMessages( 'ParserFunctions' );
|
2007-12-11 09:04:46 +00:00
|
|
|
$warning = wfMsg( 'pfunc_ifexist_warning', $parser->pf_ifexist_count, $wgMaxIfExistCount );
|
2007-12-10 06:04:34 +00:00
|
|
|
$parser->mOutput->addWarning( $warning );
|
|
|
|
$cat = Title::makeTitleSafe( NS_CATEGORY, wfMsg( 'pfunc_max_ifexist_category' ) );
|
2007-12-10 06:11:52 +00:00
|
|
|
if ( $cat ) {
|
|
|
|
$parser->mOutput->addCategory( $cat->getDBkey(), $parser->getDefaultSort() );
|
|
|
|
}
|
2007-12-10 06:04:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-01-05 02:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function wfSetupParserFunctions() {
|
2008-01-13 01:06:28 +00:00
|
|
|
global $wgParser, $wgExtParserFunctions, $wgHooks;
|
2007-01-05 02:13:39 +00:00
|
|
|
|
|
|
|
$wgExtParserFunctions = new ExtParserFunctions;
|
|
|
|
|
2007-11-20 14:16:10 +00:00
|
|
|
// Check for SFH_OBJECT_ARGS capability
|
2007-12-09 08:11:38 +00:00
|
|
|
if ( class_exists( 'StubObject' ) && !StubObject::isRealObject( $wgParser ) ) {
|
2007-11-20 14:16:10 +00:00
|
|
|
$wgParser->_unstub();
|
|
|
|
}
|
|
|
|
if ( defined( get_class( $wgParser ) . '::SFH_OBJECT_ARGS' ) ) {
|
|
|
|
// These functions accept DOM-style arguments
|
|
|
|
$wgParser->setFunctionHook( 'if', array( &$wgExtParserFunctions, 'ifObj' ), SFH_OBJECT_ARGS );
|
|
|
|
$wgParser->setFunctionHook( 'ifeq', array( &$wgExtParserFunctions, 'ifeqObj' ), SFH_OBJECT_ARGS );
|
2007-12-09 08:11:38 +00:00
|
|
|
$wgParser->setFunctionHook( 'switch', array( &$wgExtParserFunctions, 'switchObj' ), SFH_OBJECT_ARGS );
|
|
|
|
$wgParser->setFunctionHook( 'ifexist', array( &$wgExtParserFunctions, 'ifexistObj' ), SFH_OBJECT_ARGS );
|
|
|
|
$wgParser->setFunctionHook( 'ifexpr', array( &$wgExtParserFunctions, 'ifexprObj' ), SFH_OBJECT_ARGS );
|
2008-01-15 04:21:17 +00:00
|
|
|
$wgParser->setFunctionHook( 'iferror', array( &$wgExtParserFunctions, 'iferrorObj' ), SFH_OBJECT_ARGS );
|
2007-11-20 14:16:10 +00:00
|
|
|
} else {
|
|
|
|
$wgParser->setFunctionHook( 'if', array( &$wgExtParserFunctions, 'ifHook' ) );
|
|
|
|
$wgParser->setFunctionHook( 'ifeq', array( &$wgExtParserFunctions, 'ifeq' ) );
|
2007-12-09 08:11:38 +00:00
|
|
|
$wgParser->setFunctionHook( 'switch', array( &$wgExtParserFunctions, 'switchHook' ) );
|
|
|
|
$wgParser->setFunctionHook( 'ifexist', array( &$wgExtParserFunctions, 'ifexist' ) );
|
2008-01-15 05:29:29 +00:00
|
|
|
$wgParser->setFunctionHook( 'ifexpr', array( &$wgExtParserFunctions, 'ifexpr' ) );
|
2008-01-15 04:21:17 +00:00
|
|
|
$wgParser->setFunctionHook( 'iferror', array( &$wgExtParserFunctions, 'iferror' ) );
|
2007-11-20 14:16:10 +00:00
|
|
|
}
|
|
|
|
|
2007-01-05 02:13:39 +00:00
|
|
|
$wgParser->setFunctionHook( 'expr', array( &$wgExtParserFunctions, 'expr' ) );
|
2007-01-06 20:56:46 +00:00
|
|
|
$wgParser->setFunctionHook( 'time', array( &$wgExtParserFunctions, 'time' ) );
|
2007-08-01 18:00:45 +00:00
|
|
|
$wgParser->setFunctionHook( 'timel', array( &$wgExtParserFunctions, 'localTime' ) );
|
2007-01-06 20:56:46 +00:00
|
|
|
$wgParser->setFunctionHook( 'rel2abs', array( &$wgExtParserFunctions, 'rel2abs' ) );
|
2007-06-04 17:34:07 +00:00
|
|
|
$wgParser->setFunctionHook( 'titleparts', array( &$wgExtParserFunctions, 'titleparts' ) );
|
2007-01-05 02:13:39 +00:00
|
|
|
|
|
|
|
$wgHooks['ParserClearState'][] = array( &$wgExtParserFunctions, 'clearState' );
|
2007-12-10 06:04:34 +00:00
|
|
|
$wgHooks['ParserAfterTidy'][] = array( &$wgExtParserFunctions, 'afterTidy' );
|
2007-01-05 02:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function wfParserFunctionsLanguageGetMagic( &$magicWords, $langCode ) {
|
2008-01-13 01:06:28 +00:00
|
|
|
require_once( dirname( __FILE__ ) . '/ParserFunctions.i18n.magic.php' );
|
2007-06-07 19:02:57 +00:00
|
|
|
foreach( efParserFunctionsWords( $langCode ) as $word => $trans )
|
|
|
|
$magicWords[$word] = $trans;
|
2007-01-05 02:13:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-11-30 14:50:48 +00:00
|
|
|
|
|
|
|
function wfParserFunctionsLimitReport( $parser, &$report ) {
|
|
|
|
global $wgMaxIfExistCount;
|
|
|
|
if ( isset( $parser->pf_ifexist_count ) ) {
|
|
|
|
$report .= "#ifexist count: {$parser->pf_ifexist_count}/$wgMaxIfExistCount\n";
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|