<?php
+/**
+ * Parser functions provided by MediaWiki core
+ *
+ * @file
+ */
/**
* Various core parser functions, registered in Parser::firstCallInit()
# Syntax for arguments (see self::setFunctionHook):
# "name for lookup in localized magic words array",
# function callback,
- # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}
+ # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
# instead of {{#int:...}})
$parser->setFunctionHook( 'int', array( __CLASS__, 'intFunction' ), SFH_NO_HASH );
$parser->setFunctionHook( 'talkpagenamee', array( __CLASS__, 'talkpagenamee' ), SFH_NO_HASH );
$parser->setFunctionHook( 'subjectpagename', array( __CLASS__, 'subjectpagename' ), SFH_NO_HASH );
$parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__, 'subjectpagenamee' ), SFH_NO_HASH );
- $parser->setFunctionHook( 'pipetrick', array( __CLASS__, 'pipetrick' ), SFH_NO_HASH );
- $parser->setFunctionHook( 'pipetricke', array( __CLASS__, 'pipetricke' ), SFH_NO_HASH );
$parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS );
$parser->setFunctionHook( 'formatdate', array( __CLASS__, 'formatDate' ) );
static function intFunction( $parser, $part1 = '' /*, ... */ ) {
if ( strval( $part1 ) !== '' ) {
$args = array_slice( func_get_args(), 2 );
- $message = wfMsgGetKey( $part1, true, false, false );
+ $message = wfMsgGetKey( $part1, true, $parser->getOptions()->getUserLang(), false );
$message = wfMsgReplaceArgs( $message, $args );
- $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
+ $message = $parser->replaceVariables( $message ); // like MessageCache::transform()
return $message;
} else {
return array( 'found' => false );
$date = trim( $date );
- $pref = $parser->mOptions->getDateFormat();
+ $pref = $parser->getOptions()->getDateFormat();
// Specify a different default date format other than the the normal default
// iff the user has 'default' for their setting
return wfUrlencode( str_replace( ' ', '_', self::ns( $parser, $part1 ) ) );
}
- static function urlencode( $parser, $s = '' ) {
- return urlencode( $s );
+ /**
+ * urlencodes a string according to one of three patterns: (bug 22474)
+ *
+ * By default (for HTTP "query" strings), spaces are encoded as '+'.
+ * Or to encode a value for the HTTP "path", spaces are encoded as '%20'.
+ * For links to "wiki"s, or similar software, spaces are encoded as '_',
+ *
+ * @param $parser Parser object
+ * @param $s String: The text to encode.
+ * @param $arg String (optional): The type of encoding.
+ */
+ static function urlencode( $parser, $s = '', $arg = null ) {
+ static $magicWords = null;
+ if ( is_null( $magicWords ) ) {
+ $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) );
+ }
+ switch( $magicWords->matchStartToEnd( $arg ) ) {
+
+ // Encode as though it's a wiki page, '_' for ' '.
+ case 'url_wiki':
+ return wfUrlencode( str_replace( ' ', '_', $s ) );
+
+ // Encode for an HTTP Path, '%20' for ' '.
+ case 'url_path':
+ return rawurlencode( $s );
+
+ // Encode for HTTP query, '+' for ' '.
+ case 'url_query':
+ default:
+ return urlencode( $s );
+ }
}
static function lcfirst( $parser, $s = '' ) {
if ( is_object( $title ) && $title->getNamespace() == NS_USER )
$user = $title->getText();
- // check parameter, or use $wgUser if in interface message
+ // check parameter, or use the ParserOptions if in interface message
$user = User::newFromName( $user );
if ( $user ) {
$gender = $user->getOption( 'gender' );
- } elseif ( $parser->mOptions->getInterfaceMessage() ) {
- global $wgUser;
- $gender = $wgUser->getOption( 'gender' );
+ } elseif ( $parser->getOptions()->getInterfaceMessage() ) {
+ $gender = $parser->getOptions()->getUser()->getOption( 'gender' );
}
$ret = $parser->getFunctionLang()->gender( $gender, $forms );
wfProfileOut( __METHOD__ );
* 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
+ * @param $parser Parser: parent parser
+ * @param $text String: desired title text
+ * @return String
*/
static function displaytitle( $parser, $text = '' ) {
global $wgRestrictDisplayTitle;
$t = Title::newFromText( $title );
if ( is_null( $t ) )
return '';
- return $t->getPartialURL();
+ return wfEscapeWikiText( $t->getPartialURL() );
}
static function fullpagename( $parser, $title = null ) {
$t = Title::newFromText( $title );
$t = Title::newFromText( $title );
if ( is_null( $t ) || !$t->canTalk() )
return '';
- return $t->getPrefixedURL();
+ return wfEscapeWikiText( $t->getPrefixedURL() );
}
static function subpagename( $parser, $title = null ) {
$t = Title::newFromText( $title );
if ( is_null( $t ) )
return '';
- return $t->getSubpageText();
+ return wfEscapeWikiText( $t->getSubpageText() );
}
static function subpagenamee( $parser, $title = null ) {
$t = Title::newFromText( $title );
if ( is_null( $t ) )
return '';
- return $t->getSubpageUrlForm();
+ return wfEscapeWikiText( $t->getSubpageUrlForm() );
}
static function basepagename( $parser, $title = null ) {
$t = Title::newFromText( $title );
if ( is_null( $t ) )
return '';
- return $t->getBaseText();
+ return wfEscapeWikiText( $t->getBaseText() );
}
static function basepagenamee( $parser, $title = null ) {
$t = Title::newFromText( $title );
if ( is_null( $t ) )
return '';
- return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
+ return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
}
static function talkpagename( $parser, $title = null ) {
$t = Title::newFromText( $title );
$t = Title::newFromText( $title );
if ( is_null( $t ) || !$t->canTalk() )
return '';
- return $t->getTalkPage()->getPrefixedUrl();
+ return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
}
static function subjectpagename( $parser, $title = null ) {
$t = Title::newFromText( $title );
$t = Title::newFromText( $title );
if ( is_null( $t ) )
return '';
- return $t->getSubjectPage()->getPrefixedUrl();
- }
-
- /**
- * Performs the pipe trick in the same manner as [[title|]] or [[|title]].
- * {{#pipetrick:title}} == {{#pipetrick:title|}} -> Parser::getPipeTrickText
- * {{#pipetrick:|title}} -> Parser::getPipeTrickLink (rarer)
- * See http://en.wikipedia.org/wiki/Help:Pipe_trick and the Parser documentation
- * for more information.
- */
- static function pipetrick( $parser, $link = '', $text = '' ) {
- if ( $link )
- return $parser->getPipeTrickText( $link );
- else
- return $parser->getPipeTrickLink( $text );
- }
-
- /**
- * Performs the pipetrick and then url encodes the result
- */
- static function pipetricke( $parser, $link = '', $text = '' ) {
- return wfUrlEncode( str_replace( ' ', '_', self::pipetrick( $parser, $link, $text ) ) );
+ return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
}
/**
* Return the size of the given page, or 0 if it's nonexistent. This is an
* expensive parser function and can't be called too many times per page.
*
- * @FIXME This doesn't work correctly on preview for getting the size of
- * the current page.
- * @FIXME Title::getLength() documentation claims that it adds things to
- * the link cache, so the local cache here should be unnecessary, but in
- * fact calling getLength() repeatedly for the same $page does seem to
+ * @todo Fixme: This doesn't work correctly on preview for getting the size
+ * of the current page.
+ * @todo Fixme: Title::getLength() documentation claims that it adds things
+ * to the link cache, so the local cache here should be unnecessary, but
+ * in fact calling getLength() repeatedly for the same $page does seem to
* run one query for each call?
*/
static function pagesize( $parser, $page = '', $raw = null ) {
}
static function anchorencode( $parser, $text ) {
- $a = urlencode( $text );
- $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
- # leave colons alone, however
- $a = str_replace( '.3A', ':', $a );
- return $a;
+ return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
}
static function special( $parser, $text ) {
- $title = SpecialPage::getTitleForAlias( $text );
- if ( $title ) {
- return $title->getPrefixedText();
+ list( $page, $subpage ) = SpecialPage::resolveAliasWithSubpage( $text );
+ if ( $page ) {
+ $title = SpecialPage::getTitleFor( $page, $subpage );
+ return $title;
} else {
return wfMsgForContent( 'nosuchspecialpage' );
}
return '';
else
return( '<span class="error">' .
- wfMsg( 'duplicate-defaultsort',
+ wfMsgForContent( 'duplicate-defaultsort',
htmlspecialchars( $old ),
htmlspecialchars( $text ) ) .
'</span>' );
}
- public static function filepath( $parser, $name='', $option='' ) {
+ // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
+ public static function filepath( $parser, $name='', $argA='', $argB='' ) {
$file = wfFindFile( $name );
- if( $file ) {
- $url = $file->getFullUrl();
- if( $option == 'nowiki' ) {
+ $size = '';
+ $argA_int = intval( $argA );
+ $argB_int = intval( $argB );
+
+ if ( $argB_int > 0 ) {
+ // {{filepath: | option | size }}
+ $size = $argB_int;
+ $option = $argA;
+
+ } elseif ( $argA_int > 0 ) {
+ // {{filepath: | size [|option] }}
+ $size = $argA_int;
+ $option = $argB;
+
+ } else {
+ // {{filepath: [|option] }}
+ $option = $argA;
+ }
+
+ if ( $file ) {
+ $url = $file->getUrl();
+
+ // If a size is requested...
+ if ( is_integer( $size ) ) {
+ $mto = $file->transform( array( 'width' => $size ) );
+ // ... and we can
+ if ( $mto && !$mto->isError() ) {
+ // ... change the URL to point to a thumbnail.
+ $url = wfExpandUrl( $mto->getUrl() );
+ }
+ }
+ if ( $option == 'nowiki' ) {
return array( $url, 'nowiki' => true );
}
return $url;
* Parser function to extension tag adaptor
*/
public static function tagObj( $parser, $frame, $args ) {
- $xpath = false;
if ( !count( $args ) ) {
return '';
}
$stripList = $parser->getStripList();
if ( !in_array( $tagName, $stripList ) ) {
return '<span class="error">' .
- wfMsg( 'unknown_extension_tag', $tagName ) .
+ wfMsgForContent( 'unknown_extension_tag', $tagName ) .
'</span>';
}