2006-07-03 11:07:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Various core parser functions, registered in Parser::firstCallInit()
|
2007-04-20 08:55:14 +00:00
|
|
|
* @addtogroup Parser
|
2006-07-03 11:07:00 +00:00
|
|
|
*/
|
|
|
|
|
class CoreParserFunctions {
|
2006-09-30 04:53:36 +00:00
|
|
|
static function intFunction( $parser, $part1 = '' /*, ... */ ) {
|
|
|
|
|
if ( strval( $part1 ) !== '' ) {
|
|
|
|
|
$args = array_slice( func_get_args(), 2 );
|
|
|
|
|
return wfMsgReal( $part1, $args, true );
|
|
|
|
|
} else {
|
|
|
|
|
return array( 'found' => false );
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-18 17:30:35 +00:00
|
|
|
|
2006-07-03 11:07:00 +00:00
|
|
|
static function ns( $parser, $part1 = '' ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
$found = false;
|
|
|
|
|
if ( intval( $part1 ) || $part1 == "0" ) {
|
|
|
|
|
$text = $wgContLang->getNsText( intval( $part1 ) );
|
|
|
|
|
$found = true;
|
|
|
|
|
} else {
|
|
|
|
|
$param = str_replace( ' ', '_', strtolower( $part1 ) );
|
|
|
|
|
$index = Namespace::getCanonicalIndex( strtolower( $param ) );
|
|
|
|
|
if ( !is_null( $index ) ) {
|
|
|
|
|
$text = $wgContLang->getNsText( $index );
|
|
|
|
|
$found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $found ) {
|
|
|
|
|
return $text;
|
|
|
|
|
} else {
|
|
|
|
|
return array( 'found' => false );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function urlencode( $parser, $s = '' ) {
|
|
|
|
|
return urlencode( $s );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function lcfirst( $parser, $s = '' ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
return $wgContLang->lcfirst( $s );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function ucfirst( $parser, $s = '' ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
return $wgContLang->ucfirst( $s );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function lc( $parser, $s = '' ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
return $wgContLang->lc( $s );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function uc( $parser, $s = '' ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
return $wgContLang->uc( $s );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
|
|
|
|
|
static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
|
|
|
|
|
static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
|
|
|
|
|
static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
|
|
|
|
|
|
|
|
|
|
static function urlFunction( $func, $s = '', $arg = null ) {
|
|
|
|
|
$title = Title::newFromText( $s );
|
|
|
|
|
# Due to order of execution of a lot of bits, the values might be encoded
|
|
|
|
|
# before arriving here; if that's true, then the title can't be created
|
|
|
|
|
# and the variable will fail. If we can't get a decent title from the first
|
|
|
|
|
# attempt, url-decode and try for a second.
|
|
|
|
|
if( is_null( $title ) )
|
|
|
|
|
$title = Title::newFromUrl( urldecode( $s ) );
|
|
|
|
|
if ( !is_null( $title ) ) {
|
|
|
|
|
if ( !is_null( $arg ) ) {
|
|
|
|
|
$text = $title->$func( $arg );
|
|
|
|
|
} else {
|
|
|
|
|
$text = $title->$func();
|
|
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
} else {
|
|
|
|
|
return array( 'found' => false );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function formatNum( $parser, $num = '' ) {
|
2006-07-03 11:07:00 +00:00
|
|
|
return $parser->getFunctionLang()->formatNum( $num );
|
|
|
|
|
}
|
2007-01-13 12:58:33 +00:00
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function grammar( $parser, $case = '', $word = '' ) {
|
2006-07-03 11:07:00 +00:00
|
|
|
return $parser->getFunctionLang()->convertGrammar( $word, $case );
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-18 20:15:49 +00:00
|
|
|
static function plural( $parser, $text = '') {
|
|
|
|
|
$forms = array_slice( func_get_args(), 2);
|
2006-12-23 18:58:44 +00:00
|
|
|
$text = $parser->getFunctionLang()->parseFormattedNumber( $text );
|
2007-11-18 20:15:49 +00:00
|
|
|
return $parser->getFunctionLang()->convertPlural( $text, $forms );
|
2006-07-03 11:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
2007-06-25 15:51:09 +00:00
|
|
|
/**
|
|
|
|
|
* Override the title of the page when viewed,
|
|
|
|
|
* provided we've been given a title which
|
|
|
|
|
* will normalise to the canonical title
|
|
|
|
|
*
|
|
|
|
|
* @param Parser $parser Parent parser
|
|
|
|
|
* @param string $text Desired title text
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
static function displaytitle( $parser, $text = '' ) {
|
2007-07-10 01:32:30 +00:00
|
|
|
$text = trim( Sanitizer::decodeCharReferences( $text ) );
|
2007-06-25 15:51:09 +00:00
|
|
|
$title = Title::newFromText( $text );
|
2007-06-25 20:30:25 +00:00
|
|
|
if( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) )
|
2007-06-25 15:51:09 +00:00
|
|
|
$parser->mOutput->setDisplayTitle( $text );
|
2006-07-03 11:07:00 +00:00
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function isRaw( $param ) {
|
2006-07-03 11:07:00 +00:00
|
|
|
static $mwRaw;
|
|
|
|
|
if ( !$mwRaw ) {
|
2006-07-14 15:39:23 +00:00
|
|
|
$mwRaw =& MagicWord::get( 'rawsuffix' );
|
2006-07-03 11:07:00 +00:00
|
|
|
}
|
|
|
|
|
if ( is_null( $param ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return $mwRaw->match( $param );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function statisticsFunction( $func, $raw = null ) {
|
2006-07-03 11:07:00 +00:00
|
|
|
if ( self::isRaw( $raw ) ) {
|
2006-11-21 09:53:45 +00:00
|
|
|
return call_user_func( array( 'SiteStats', $func ) );
|
2006-07-03 13:55:52 +00:00
|
|
|
} else {
|
2006-07-03 11:07:00 +00:00
|
|
|
global $wgContLang;
|
2006-11-21 09:53:45 +00:00
|
|
|
return $wgContLang->formatNum( call_user_func( array( 'SiteStats', $func ) ) );
|
2006-07-03 11:07:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function numberofpages( $parser, $raw = null ) { return self::statisticsFunction( 'pages', $raw ); }
|
|
|
|
|
static function numberofusers( $parser, $raw = null ) { return self::statisticsFunction( 'users', $raw ); }
|
|
|
|
|
static function numberofarticles( $parser, $raw = null ) { return self::statisticsFunction( 'articles', $raw ); }
|
|
|
|
|
static function numberoffiles( $parser, $raw = null ) { return self::statisticsFunction( 'images', $raw ); }
|
|
|
|
|
static function numberofadmins( $parser, $raw = null ) { return self::statisticsFunction( 'admins', $raw ); }
|
2007-04-17 09:23:31 +00:00
|
|
|
static function numberofedits( $parser, $raw = null ) { return self::statisticsFunction( 'edits', $raw ); }
|
2006-07-03 11:07:00 +00:00
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
|
2006-11-21 09:53:45 +00:00
|
|
|
$count = SiteStats::pagesInNs( intval( $namespace ) );
|
2006-07-03 11:07:00 +00:00
|
|
|
if ( self::isRaw( $raw ) ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
return $wgContLang->formatNum( $count );
|
|
|
|
|
} else {
|
|
|
|
|
return $count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function language( $parser, $arg = '' ) {
|
2006-07-03 11:07:00 +00:00
|
|
|
global $wgContLang;
|
|
|
|
|
$lang = $wgContLang->getLanguageName( strtolower( $arg ) );
|
|
|
|
|
return $lang != '' ? $lang : $arg;
|
|
|
|
|
}
|
2007-01-13 12:58:33 +00:00
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function pad( $string = '', $length = 0, $char = 0, $direction = STR_PAD_RIGHT ) {
|
2006-08-21 11:07:58 +00:00
|
|
|
$length = min( max( $length, 0 ), 500 );
|
|
|
|
|
$char = substr( $char, 0, 1 );
|
2007-07-11 05:40:05 +00:00
|
|
|
return ( $string !== '' && (int)$length > 0 && strlen( trim( (string)$char ) ) > 0 )
|
2006-08-21 11:07:58 +00:00
|
|
|
? str_pad( $string, $length, (string)$char, $direction )
|
2006-08-29 03:24:33 +00:00
|
|
|
: $string;
|
2006-08-21 11:07:58 +00:00
|
|
|
}
|
2007-01-13 12:58:33 +00:00
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function padleft( $parser, $string = '', $length = 0, $char = 0 ) {
|
2006-08-21 11:07:58 +00:00
|
|
|
return self::pad( $string, $length, $char, STR_PAD_LEFT );
|
2006-08-18 17:30:35 +00:00
|
|
|
}
|
2007-01-13 12:58:33 +00:00
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function padright( $parser, $string = '', $length = 0, $char = 0 ) {
|
2006-08-21 11:07:58 +00:00
|
|
|
return self::pad( $string, $length, $char );
|
2006-08-18 17:30:35 +00:00
|
|
|
}
|
2007-01-13 12:58:33 +00:00
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function anchorencode( $parser, $text ) {
|
2007-03-29 18:57:54 +00:00
|
|
|
$a = urlencode( $text );
|
|
|
|
|
$a = strtr( $a, array( '%' => '.', '+' => '_' ) );
|
|
|
|
|
# leave colons alone, however
|
|
|
|
|
$a = str_replace( '.3A', ':', $a );
|
|
|
|
|
return $a;
|
2006-08-30 07:51:44 +00:00
|
|
|
}
|
2006-10-31 13:25:47 +00:00
|
|
|
|
2006-11-29 05:45:03 +00:00
|
|
|
static function special( $parser, $text ) {
|
2006-10-31 13:25:47 +00:00
|
|
|
$title = SpecialPage::getTitleForAlias( $text );
|
|
|
|
|
if ( $title ) {
|
|
|
|
|
return $title->getPrefixedText();
|
|
|
|
|
} else {
|
|
|
|
|
return wfMsgForContent( 'nosuchspecialpage' );
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-11-23 17:49:08 +00:00
|
|
|
|
2006-12-29 10:39:35 +00:00
|
|
|
public static function defaultsort( $parser, $text ) {
|
|
|
|
|
$text = trim( $text );
|
|
|
|
|
if( strlen( $text ) > 0 )
|
|
|
|
|
$parser->setDefaultSort( $text );
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2007-09-14 15:29:52 +00:00
|
|
|
|
|
|
|
|
public static function filepath( $parser, $name='', $option='' ) {
|
|
|
|
|
$file = wfFindFile( $name );
|
|
|
|
|
if( $file ) {
|
|
|
|
|
$url = $file->getFullUrl();
|
|
|
|
|
if( $option == 'nowiki' ) {
|
|
|
|
|
return "<nowiki>$url</nowiki>";
|
|
|
|
|
}
|
|
|
|
|
return $url;
|
|
|
|
|
} else {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-01-09 07:13:54 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parser function to extension tag adaptor
|
|
|
|
|
*/
|
|
|
|
|
public static function tagObj( $parser, $frame, $args ) {
|
|
|
|
|
$xpath = false;
|
|
|
|
|
if ( !count( $args ) ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
$tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
|
2008-01-16 07:46:27 +00:00
|
|
|
|
|
|
|
|
if ( count( $args ) ) {
|
|
|
|
|
$inner = $frame->expand( array_shift( $args ) );
|
|
|
|
|
} else {
|
|
|
|
|
$inner = null;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-09 07:13:54 +00:00
|
|
|
$stripList = $parser->getStripList();
|
|
|
|
|
if ( !in_array( $tagName, $stripList ) ) {
|
|
|
|
|
return '<span class="error">' .
|
|
|
|
|
wfMsg( 'unknown_extension_tag', $tagName ) .
|
|
|
|
|
'</span>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$attributes = array();
|
|
|
|
|
foreach ( $args as $arg ) {
|
|
|
|
|
if ( !$xpath ) {
|
|
|
|
|
$xpath = new DOMXPath( $arg->ownerDocument );
|
|
|
|
|
}
|
|
|
|
|
$names = $xpath->query( 'name', $arg );
|
2008-01-16 07:46:27 +00:00
|
|
|
if ( !$names->item( 0 )->hasAttributes() ) {
|
2008-01-09 07:13:54 +00:00
|
|
|
$name = $frame->expand( $names->item( 0 ), PPFrame::STRIP_COMMENTS );
|
2008-01-16 07:46:27 +00:00
|
|
|
$values = $xpath->query( 'value', $arg );
|
|
|
|
|
$value = trim( $frame->expand( $values->item( 0 ) ) );
|
|
|
|
|
if ( preg_match( '/^(?:"|\')(.*)(?:"|\')$/s', $value, $m ) ) {
|
|
|
|
|
$value = $m[1];
|
2008-01-09 07:13:54 +00:00
|
|
|
}
|
2008-01-16 07:46:27 +00:00
|
|
|
$attributes[$name] = $value;
|
|
|
|
|
}
|
2008-01-09 07:13:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$params = array(
|
|
|
|
|
'name' => $tagName,
|
|
|
|
|
'inner' => $inner,
|
|
|
|
|
'attributes' => $attributes
|
|
|
|
|
);
|
|
|
|
|
return $parser->extensionSubstitution( $params, $frame );
|
|
|
|
|
}
|
2006-07-03 11:07:00 +00:00
|
|
|
}
|
2007-06-29 01:19:14 +00:00
|
|
|
|