summaryrefslogtreecommitdiff
path: root/includes/parser/CoreParserFunctions.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/parser/CoreParserFunctions.php')
-rw-r--r--includes/parser/CoreParserFunctions.php173
1 files changed, 141 insertions, 32 deletions
diff --git a/includes/parser/CoreParserFunctions.php b/includes/parser/CoreParserFunctions.php
index 94949221..eebed44c 100644
--- a/includes/parser/CoreParserFunctions.php
+++ b/includes/parser/CoreParserFunctions.php
@@ -10,6 +10,10 @@
* @ingroup Parser
*/
class CoreParserFunctions {
+ /**
+ * @param $parser Parser
+ * @return void
+ */
static function register( $parser ) {
global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
@@ -31,6 +35,8 @@ class CoreParserFunctions {
$parser->setFunctionHook( 'localurle', array( __CLASS__, 'localurle' ), SFH_NO_HASH );
$parser->setFunctionHook( 'fullurl', array( __CLASS__, 'fullurl' ), SFH_NO_HASH );
$parser->setFunctionHook( 'fullurle', array( __CLASS__, 'fullurle' ), SFH_NO_HASH );
+ $parser->setFunctionHook( 'canonicalurl', array( __CLASS__, 'canonicalurl' ), SFH_NO_HASH );
+ $parser->setFunctionHook( 'canonicalurle', array( __CLASS__, 'canonicalurle' ), SFH_NO_HASH );
$parser->setFunctionHook( 'formatnum', array( __CLASS__, 'formatnum' ), SFH_NO_HASH );
$parser->setFunctionHook( 'grammar', array( __CLASS__, 'grammar' ), SFH_NO_HASH );
$parser->setFunctionHook( 'gender', array( __CLASS__, 'gender' ), SFH_NO_HASH );
@@ -83,18 +89,27 @@ class CoreParserFunctions {
}
}
+ /**
+ * @param $parser Parser
+ * @param string $part1
+ * @return array
+ */
static function intFunction( $parser, $part1 = '' /*, ... */ ) {
if ( strval( $part1 ) !== '' ) {
$args = array_slice( func_get_args(), 2 );
- $message = wfMsgGetKey( $part1, true, $parser->getOptions()->getUserLang(), false );
- $message = wfMsgReplaceArgs( $message, $args );
- $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
- return $message;
+ $message = wfMessage( $part1, $args )->inLanguage( $parser->getOptions()->getUserLang() )->plain();
+ return array( $message, 'noparse' => false );
} else {
return array( 'found' => false );
}
}
+ /**
+ * @param $parser Parser
+ * @param $date
+ * @param null $defaultPref
+ * @return mixed|string
+ */
static function formatDate( $parser, $date, $defaultPref = null ) {
$df = DateFormatter::getInstance();
@@ -172,6 +187,11 @@ class CoreParserFunctions {
return $wgContLang->ucfirst( $s );
}
+ /**
+ * @param $parser Parser
+ * @param string $s
+ * @return
+ */
static function lc( $parser, $s = '' ) {
global $wgContLang;
if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
@@ -181,6 +201,11 @@ class CoreParserFunctions {
}
}
+ /**
+ * @param $parser Parser
+ * @param string $s
+ * @return
+ */
static function uc( $parser, $s = '' ) {
global $wgContLang;
if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
@@ -194,6 +219,8 @@ class CoreParserFunctions {
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 canonicalurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getCanonicalURL', $s, $arg ); }
+ static function canonicalurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeCanonicalURL', $s, $arg ); }
static function urlFunction( $func, $s = '', $arg = null ) {
$title = Title::newFromText( $s );
@@ -219,6 +246,12 @@ class CoreParserFunctions {
}
}
+ /**
+ * @param $parser Parser
+ * @param string $num
+ * @param null $raw
+ * @return
+ */
static function formatNum( $parser, $num = '', $raw = null) {
if ( self::israw( $raw ) ) {
return $parser->getFunctionLang()->parseFormattedNumber( $num );
@@ -227,35 +260,54 @@ class CoreParserFunctions {
}
}
+ /**
+ * @param $parser Parser
+ * @param string $case
+ * @param string $word
+ * @return
+ */
static function grammar( $parser, $case = '', $word = '' ) {
return $parser->getFunctionLang()->convertGrammar( $word, $case );
}
- static function gender( $parser, $user ) {
+ /**
+ * @param $parser Parser
+ * @param $username string
+ * @return
+ */
+ static function gender( $parser, $username ) {
wfProfileIn( __METHOD__ );
$forms = array_slice( func_get_args(), 2);
+ $username = trim( $username );
+
// default
$gender = User::getDefaultOption( 'gender' );
// allow prefix.
- $title = Title::newFromText( $user );
+ $title = Title::newFromText( $username );
- if ( is_object( $title ) && $title->getNamespace() == NS_USER )
- $user = $title->getText();
+ if ( $title && $title->getNamespace() == NS_USER ) {
+ $username = $title->getText();
+ }
- // check parameter, or use $wgUser if in interface message
- $user = User::newFromName( $user );
+ // check parameter, or use the ParserOptions if in interface message
+ $user = User::newFromName( $username );
if ( $user ) {
$gender = $user->getOption( 'gender' );
- } elseif ( $parser->getOptions()->getInterfaceMessage() ) {
- global $wgUser;
- $gender = $wgUser->getOption( 'gender' );
+ } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
+ $gender = $parser->getOptions()->getUser()->getOption( 'gender' );
}
$ret = $parser->getFunctionLang()->gender( $gender, $forms );
wfProfileOut( __METHOD__ );
return $ret;
}
+
+ /**
+ * @param $parser Parser
+ * @param string $text
+ * @return
+ */
static function plural( $parser, $text = '' ) {
$forms = array_slice( func_get_args(), 2 );
$text = $parser->getFunctionLang()->parseFormattedNumber( $text );
@@ -396,10 +448,11 @@ class CoreParserFunctions {
return '';
return wfUrlencode( $t->getSubjectNsText() );
}
- /*
+
+ /**
* Functions to get and normalize pagenames, corresponding to the magic words
* of the same names
- */
+ */
static function pagename( $parser, $title = null ) {
$t = Title::newFromText( $title );
if ( is_null( $t ) )
@@ -410,7 +463,7 @@ class CoreParserFunctions {
$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 );
@@ -422,31 +475,31 @@ class CoreParserFunctions {
$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 );
@@ -458,7 +511,7 @@ class CoreParserFunctions {
$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 );
@@ -470,7 +523,7 @@ class CoreParserFunctions {
$t = Title::newFromText( $title );
if ( is_null( $t ) )
return '';
- return $t->getSubjectPage()->getPrefixedUrl();
+ return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
}
/**
@@ -503,12 +556,13 @@ class CoreParserFunctions {
* 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.
*
- * @todo Fixme: This doesn't work correctly on preview for getting the size
+ * @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
+ * @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?
+ * @param $parser Parser
*/
static function pagesize( $parser, $page = '', $raw = null ) {
static $cache = array();
@@ -546,10 +600,25 @@ class CoreParserFunctions {
return implode( $restrictions, ',' );
}
- static function language( $parser, $arg = '' ) {
+ /**
+ * Gives language names.
+ * @param $parser Parser
+ * @param $code String Language code
+ * @param $language String Language code
+ * @return String
+ */
+ static function language( $parser, $code = '', $language = '' ) {
global $wgContLang;
- $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
- return $lang != '' ? $lang : $arg;
+ $code = strtolower( $code );
+ $language = strtolower( $language );
+
+ if ( $language !== '' ) {
+ $names = Language::getTranslatedLanguageNames( $language );
+ return isset( $names[$code] ) ? $names[$code] : wfBCP47( $code );
+ }
+
+ $lang = $wgContLang->getLanguageName( $code );
+ return $lang !== '' ? $lang : wfBCP47( $code );
}
/**
@@ -586,12 +655,17 @@ class CoreParserFunctions {
return self::pad( $string, $length, $padding );
}
+ /**
+ * @param $parser Parser
+ * @param $text
+ * @return string
+ */
static function anchorencode( $parser, $text ) {
return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
}
static function special( $parser, $text ) {
- list( $page, $subpage ) = SpecialPage::resolveAliasWithSubpage( $text );
+ list( $page, $subpage ) = SpecialPageFactory::resolveAlias( $text );
if ( $page ) {
$title = SpecialPage::getTitleFor( $page, $subpage );
return $title;
@@ -600,6 +674,11 @@ class CoreParserFunctions {
}
}
+ /**
+ * @param $parser Parser
+ * @param $text
+ * @return string
+ */
public static function defaultsort( $parser, $text ) {
$text = trim( $text );
if( strlen( $text ) == 0 )
@@ -616,11 +695,41 @@ class CoreParserFunctions {
'</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 ) {
+ $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->getFullUrl();
- if( $option == 'nowiki' ) {
+
+ // 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(), PROTO_RELATIVE );
+ }
+ }
+ if ( $option == 'nowiki' ) {
return array( $url, 'nowiki' => true );
}
return $url;