MediaWiki  REL1_20
CoreParserFunctions.php
Go to the documentation of this file.
00001 <?php
00028 class CoreParserFunctions {
00033         static function register( $parser ) {
00034                 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
00035 
00036                 # Syntax for arguments (see self::setFunctionHook):
00037                 #  "name for lookup in localized magic words array",
00038                 #  function callback,
00039                 #  optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
00040                 #    instead of {{#int:...}})
00041 
00042                 $parser->setFunctionHook( 'int',              array( __CLASS__, 'intFunction'      ), SFH_NO_HASH );
00043                 $parser->setFunctionHook( 'ns',               array( __CLASS__, 'ns'               ), SFH_NO_HASH );
00044                 $parser->setFunctionHook( 'nse',              array( __CLASS__, 'nse'              ), SFH_NO_HASH );
00045                 $parser->setFunctionHook( 'urlencode',        array( __CLASS__, 'urlencode'        ), SFH_NO_HASH );
00046                 $parser->setFunctionHook( 'lcfirst',          array( __CLASS__, 'lcfirst'          ), SFH_NO_HASH );
00047                 $parser->setFunctionHook( 'ucfirst',          array( __CLASS__, 'ucfirst'          ), SFH_NO_HASH );
00048                 $parser->setFunctionHook( 'lc',               array( __CLASS__, 'lc'               ), SFH_NO_HASH );
00049                 $parser->setFunctionHook( 'uc',               array( __CLASS__, 'uc'               ), SFH_NO_HASH );
00050                 $parser->setFunctionHook( 'localurl',         array( __CLASS__, 'localurl'         ), SFH_NO_HASH );
00051                 $parser->setFunctionHook( 'localurle',        array( __CLASS__, 'localurle'        ), SFH_NO_HASH );
00052                 $parser->setFunctionHook( 'fullurl',          array( __CLASS__, 'fullurl'          ), SFH_NO_HASH );
00053                 $parser->setFunctionHook( 'fullurle',         array( __CLASS__, 'fullurle'         ), SFH_NO_HASH );
00054                 $parser->setFunctionHook( 'canonicalurl',     array( __CLASS__, 'canonicalurl'     ), SFH_NO_HASH );
00055                 $parser->setFunctionHook( 'canonicalurle',    array( __CLASS__, 'canonicalurle'    ), SFH_NO_HASH );
00056                 $parser->setFunctionHook( 'formatnum',        array( __CLASS__, 'formatnum'        ), SFH_NO_HASH );
00057                 $parser->setFunctionHook( 'grammar',          array( __CLASS__, 'grammar'          ), SFH_NO_HASH );
00058                 $parser->setFunctionHook( 'gender',           array( __CLASS__, 'gender'           ), SFH_NO_HASH );
00059                 $parser->setFunctionHook( 'plural',           array( __CLASS__, 'plural'           ), SFH_NO_HASH );
00060                 $parser->setFunctionHook( 'numberofpages',    array( __CLASS__, 'numberofpages'    ), SFH_NO_HASH );
00061                 $parser->setFunctionHook( 'numberofusers',    array( __CLASS__, 'numberofusers'    ), SFH_NO_HASH );
00062                 $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__, 'numberofactiveusers' ), SFH_NO_HASH );
00063                 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__, 'numberofarticles' ), SFH_NO_HASH );
00064                 $parser->setFunctionHook( 'numberoffiles',    array( __CLASS__, 'numberoffiles'    ), SFH_NO_HASH );
00065                 $parser->setFunctionHook( 'numberofadmins',   array( __CLASS__, 'numberofadmins'   ), SFH_NO_HASH );
00066                 $parser->setFunctionHook( 'numberingroup',    array( __CLASS__, 'numberingroup'    ), SFH_NO_HASH );
00067                 $parser->setFunctionHook( 'numberofedits',    array( __CLASS__, 'numberofedits'    ), SFH_NO_HASH );
00068                 $parser->setFunctionHook( 'numberofviews',    array( __CLASS__, 'numberofviews'    ), SFH_NO_HASH );
00069                 $parser->setFunctionHook( 'language',         array( __CLASS__, 'language'         ), SFH_NO_HASH );
00070                 $parser->setFunctionHook( 'padleft',          array( __CLASS__, 'padleft'          ), SFH_NO_HASH );
00071                 $parser->setFunctionHook( 'padright',         array( __CLASS__, 'padright'         ), SFH_NO_HASH );
00072                 $parser->setFunctionHook( 'anchorencode',     array( __CLASS__, 'anchorencode'     ), SFH_NO_HASH );
00073                 $parser->setFunctionHook( 'special',          array( __CLASS__, 'special'          ) );
00074                 $parser->setFunctionHook( 'speciale',         array( __CLASS__, 'speciale'         ) );
00075                 $parser->setFunctionHook( 'defaultsort',      array( __CLASS__, 'defaultsort'      ), SFH_NO_HASH );
00076                 $parser->setFunctionHook( 'filepath',         array( __CLASS__, 'filepath'         ), SFH_NO_HASH );
00077                 $parser->setFunctionHook( 'pagesincategory',  array( __CLASS__, 'pagesincategory'  ), SFH_NO_HASH );
00078                 $parser->setFunctionHook( 'pagesize',         array( __CLASS__, 'pagesize'         ), SFH_NO_HASH );
00079                 $parser->setFunctionHook( 'protectionlevel',  array( __CLASS__, 'protectionlevel'  ), SFH_NO_HASH );
00080                 $parser->setFunctionHook( 'namespace',        array( __CLASS__, 'mwnamespace'      ), SFH_NO_HASH );
00081                 $parser->setFunctionHook( 'namespacee',       array( __CLASS__, 'namespacee'       ), SFH_NO_HASH );
00082                 $parser->setFunctionHook( 'namespacenumber',  array( __CLASS__, 'namespacenumber'  ), SFH_NO_HASH );
00083                 $parser->setFunctionHook( 'talkspace',        array( __CLASS__, 'talkspace'        ), SFH_NO_HASH );
00084                 $parser->setFunctionHook( 'talkspacee',       array( __CLASS__, 'talkspacee'       ), SFH_NO_HASH );
00085                 $parser->setFunctionHook( 'subjectspace',     array( __CLASS__, 'subjectspace'     ), SFH_NO_HASH );
00086                 $parser->setFunctionHook( 'subjectspacee',    array( __CLASS__, 'subjectspacee'    ), SFH_NO_HASH );
00087                 $parser->setFunctionHook( 'pagename',         array( __CLASS__, 'pagename'         ), SFH_NO_HASH );
00088                 $parser->setFunctionHook( 'pagenamee',        array( __CLASS__, 'pagenamee'        ), SFH_NO_HASH );
00089                 $parser->setFunctionHook( 'fullpagename',     array( __CLASS__, 'fullpagename'     ), SFH_NO_HASH );
00090                 $parser->setFunctionHook( 'fullpagenamee',    array( __CLASS__, 'fullpagenamee'    ), SFH_NO_HASH );
00091                 $parser->setFunctionHook( 'basepagename',     array( __CLASS__, 'basepagename'     ), SFH_NO_HASH );
00092                 $parser->setFunctionHook( 'basepagenamee',    array( __CLASS__, 'basepagenamee'    ), SFH_NO_HASH );
00093                 $parser->setFunctionHook( 'subpagename',      array( __CLASS__, 'subpagename'      ), SFH_NO_HASH );
00094                 $parser->setFunctionHook( 'subpagenamee',     array( __CLASS__, 'subpagenamee'     ), SFH_NO_HASH );
00095                 $parser->setFunctionHook( 'talkpagename',     array( __CLASS__, 'talkpagename'     ), SFH_NO_HASH );
00096                 $parser->setFunctionHook( 'talkpagenamee',    array( __CLASS__, 'talkpagenamee'    ), SFH_NO_HASH );
00097                 $parser->setFunctionHook( 'subjectpagename',  array( __CLASS__, 'subjectpagename'  ), SFH_NO_HASH );
00098                 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__, 'subjectpagenamee' ), SFH_NO_HASH );
00099                 $parser->setFunctionHook( 'tag',              array( __CLASS__, 'tagObj'           ), SFH_OBJECT_ARGS );
00100                 $parser->setFunctionHook( 'formatdate',       array( __CLASS__, 'formatDate'       ) );
00101 
00102                 if ( $wgAllowDisplayTitle ) {
00103                         $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH );
00104                 }
00105                 if ( $wgAllowSlowParserFunctions ) {
00106                         $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__, 'pagesinnamespace' ), SFH_NO_HASH );
00107                 }
00108         }
00109 
00115         static function intFunction( $parser, $part1 = '' /*, ... */ ) {
00116                 if ( strval( $part1 ) !== '' ) {
00117                         $args = array_slice( func_get_args(), 2 );
00118                         $message = wfMessage( $part1, $args )->inLanguage( $parser->getOptions()->getUserLangObj() )->plain();
00119                         return array( $message, 'noparse' => false );
00120                 } else {
00121                         return array( 'found' => false );
00122                 }
00123         }
00124 
00131         static function formatDate( $parser, $date, $defaultPref = null ) {
00132                 $lang = $parser->getFunctionLang();
00133                 $df = DateFormatter::getInstance( $lang );
00134 
00135                 $date = trim( $date );
00136 
00137                 $pref = $parser->getOptions()->getDateFormat();
00138 
00139                 // Specify a different default date format other than the the normal default
00140                 // iff the user has 'default' for their setting
00141                 if ( $pref == 'default' && $defaultPref )
00142                         $pref = $defaultPref;
00143 
00144                 $date = $df->reformat( $pref, $date, array( 'match-whole' ) );
00145                 return $date;
00146         }
00147 
00148         static function ns( $parser, $part1 = '' ) {
00149                 global $wgContLang;
00150                 if ( intval( $part1 ) || $part1 == "0" ) {
00151                         $index = intval( $part1 );
00152                 } else {
00153                         $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
00154                 }
00155                 if ( $index !== false ) {
00156                         return $wgContLang->getFormattedNsText( $index );
00157                 } else {
00158                         return array( 'found' => false );
00159                 }
00160         }
00161 
00162         static function nse( $parser, $part1 = '' ) {
00163                 $ret = self::ns( $parser, $part1 );
00164                 if ( is_string( $ret ) ) {
00165                         $ret = wfUrlencode( str_replace( ' ', '_', $ret ) );
00166                 }
00167                 return $ret;
00168         }
00169 
00182         static function urlencode( $parser, $s = '', $arg = null ) {
00183                 static $magicWords = null;
00184                 if ( is_null( $magicWords ) ) {
00185                         $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) );
00186                 }
00187                 switch( $magicWords->matchStartToEnd( $arg ) ) {
00188 
00189                         // Encode as though it's a wiki page, '_' for ' '.
00190                         case 'url_wiki':
00191                                 $func = 'wfUrlencode';
00192                                 $s = str_replace( ' ', '_', $s );
00193                                 break;
00194 
00195                         // Encode for an HTTP Path, '%20' for ' '.
00196                         case 'url_path':
00197                                 $func = 'rawurlencode';
00198                                 break;
00199 
00200                         // Encode for HTTP query, '+' for ' '.
00201                         case 'url_query':
00202                         default:
00203                                 $func = 'urlencode';
00204                 }
00205                 return $parser->markerSkipCallback( $s, $func );
00206         }
00207 
00208         static function lcfirst( $parser, $s = '' ) {
00209                 global $wgContLang;
00210                 return $wgContLang->lcfirst( $s );
00211         }
00212 
00213         static function ucfirst( $parser, $s = '' ) {
00214                 global $wgContLang;
00215                 return $wgContLang->ucfirst( $s );
00216         }
00217 
00223         static function lc( $parser, $s = '' ) {
00224                 global $wgContLang;
00225                 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
00226         }
00227 
00233         static function uc( $parser, $s = '' ) {
00234                 global $wgContLang;
00235                 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
00236         }
00237 
00238         static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
00239         static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
00240         static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
00241         static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
00242         static function canonicalurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getCanonicalURL', $s, $arg ); }
00243         static function canonicalurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeCanonicalURL', $s, $arg ); }
00244 
00245         static function urlFunction( $func, $s = '', $arg = null ) {
00246                 $title = Title::newFromText( $s );
00247                 # Due to order of execution of a lot of bits, the values might be encoded
00248                 # before arriving here; if that's true, then the title can't be created
00249                 # and the variable will fail. If we can't get a decent title from the first
00250                 # attempt, url-decode and try for a second.
00251                 if( is_null( $title ) )
00252                         $title = Title::newFromURL( urldecode( $s ) );
00253                 if( !is_null( $title ) ) {
00254                         # Convert NS_MEDIA -> NS_FILE
00255                         if( $title->getNamespace() == NS_MEDIA ) {
00256                                 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
00257                         }
00258                         if( !is_null( $arg ) ) {
00259                                 $text = $title->$func( $arg );
00260                         } else {
00261                                 $text = $title->$func();
00262                         }
00263                         return $text;
00264                 } else {
00265                         return array( 'found' => false );
00266                 }
00267         }
00268 
00275         static function formatnum( $parser, $num = '', $raw = null) {
00276                 if ( self::isRaw( $raw ) ) {
00277                         $func = array( $parser->getFunctionLang(), 'parseFormattedNumber' );
00278                 } else {
00279                         $func = array( $parser->getFunctionLang(), 'formatNum' );
00280                 }
00281                 return $parser->markerSkipCallback( $num, $func );
00282         }
00283 
00290         static function grammar( $parser, $case = '', $word = '' ) {
00291                 $word = $parser->killMarkers( $word );
00292                 return $parser->getFunctionLang()->convertGrammar( $word, $case );
00293         }
00294 
00300         static function gender( $parser, $username ) {
00301                 wfProfileIn( __METHOD__ );
00302                 $forms = array_slice( func_get_args(), 2 );
00303 
00304                 // Some shortcuts to avoid loading user data unnecessarily
00305                 if ( count( $forms ) === 0 ) {
00306                         wfProfileOut( __METHOD__ );
00307                         return '';
00308                 } elseif ( count( $forms ) === 1 ) {
00309                         wfProfileOut( __METHOD__ );
00310                         return $forms[0];
00311                 }
00312 
00313                 $username = trim( $username );
00314 
00315                 // default
00316                 $gender = User::getDefaultOption( 'gender' );
00317 
00318                 // allow prefix.
00319                 $title = Title::newFromText( $username );
00320 
00321                 if ( $title && $title->getNamespace() == NS_USER ) {
00322                         $username = $title->getText();
00323                 }
00324 
00325                 // check parameter, or use the ParserOptions if in interface message
00326                 $user = User::newFromName( $username );
00327                 if ( $user ) {
00328                         $gender = GenderCache::singleton()->getGenderOf( $user, __METHOD__ );
00329                 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
00330                         $gender = GenderCache::singleton()->getGenderOf( $parser->getOptions()->getUser(), __METHOD__ );
00331                 }
00332                 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
00333                 wfProfileOut( __METHOD__ );
00334                 return $ret;
00335         }
00336 
00342         static function plural( $parser, $text = '' ) {
00343                 $forms = array_slice( func_get_args(), 2 );
00344                 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
00345                 settype( $text, ctype_digit( $text ) ? 'int' : 'float' );
00346                 return $parser->getFunctionLang()->convertPlural( $text, $forms );
00347         }
00348 
00357         static function displaytitle( $parser, $text = '' ) {
00358                 global $wgRestrictDisplayTitle;
00359 
00360                 #parse a limited subset of wiki markup (just the single quote items)
00361                 $text = $parser->doQuotes( $text );
00362 
00363                 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
00364                 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
00365                         . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '/', '', $text );
00366 
00367                 #list of disallowed tags for DISPLAYTITLE
00368                 #these will be escaped even though they are allowed in normal wiki text
00369                 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
00370                         'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
00371 
00372                 #only requested titles that normalize to the actual title are allowed through
00373                 #if $wgRestrictDisplayTitle is true (it is by default)
00374                 #mimic the escaping process that occurs in OutputPage::setPageTitle
00375                 $text = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $text, null, array(), array(), $bad ) );
00376                 $title = Title::newFromText( Sanitizer::stripAllTags( $text ) );
00377 
00378                 if( !$wgRestrictDisplayTitle ) {
00379                         $parser->mOutput->setDisplayTitle( $text );
00380                 } else {
00381                         if ( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) ) {
00382                                 $parser->mOutput->setDisplayTitle( $text );
00383                         }
00384                 }
00385 
00386                 return '';
00387         }
00388 
00389         static function isRaw( $param ) {
00390                 static $mwRaw;
00391                 if ( !$mwRaw ) {
00392                         $mwRaw =& MagicWord::get( 'rawsuffix' );
00393                 }
00394                 if ( is_null( $param ) ) {
00395                         return false;
00396                 } else {
00397                         return $mwRaw->match( $param );
00398                 }
00399         }
00400 
00401         static function formatRaw( $num, $raw ) {
00402                 if( self::isRaw( $raw ) ) {
00403                         return $num;
00404                 } else {
00405                         global $wgContLang;
00406                         return $wgContLang->formatNum( $num );
00407                 }
00408         }
00409         static function numberofpages( $parser, $raw = null ) {
00410                 return self::formatRaw( SiteStats::pages(), $raw );
00411         }
00412         static function numberofusers( $parser, $raw = null ) {
00413                 return self::formatRaw( SiteStats::users(), $raw );
00414         }
00415         static function numberofactiveusers( $parser, $raw = null ) {
00416                 return self::formatRaw( SiteStats::activeUsers(), $raw );
00417         }
00418         static function numberofarticles( $parser, $raw = null ) {
00419                 return self::formatRaw( SiteStats::articles(), $raw );
00420         }
00421         static function numberoffiles( $parser, $raw = null ) {
00422                 return self::formatRaw( SiteStats::images(), $raw );
00423         }
00424         static function numberofadmins( $parser, $raw = null ) {
00425                 return self::formatRaw( SiteStats::numberingroup('sysop'), $raw );
00426         }
00427         static function numberofedits( $parser, $raw = null ) {
00428                 return self::formatRaw( SiteStats::edits(), $raw );
00429         }
00430         static function numberofviews( $parser, $raw = null ) {
00431                 return self::formatRaw( SiteStats::views(), $raw );
00432         }
00433         static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
00434                 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
00435         }
00436         static function numberingroup( $parser, $name = '', $raw = null) {
00437                 return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
00438         }
00439 
00440 
00448         static function mwnamespace( $parser, $title = null ) {
00449                 $t = Title::newFromText( $title );
00450                 if ( is_null( $t ) )
00451                         return '';
00452                 return str_replace( '_', ' ', $t->getNsText() );
00453         }
00454         static function namespacee( $parser, $title = null ) {
00455                 $t = Title::newFromText( $title );
00456                 if ( is_null( $t ) )
00457                         return '';
00458                 return wfUrlencode( $t->getNsText() );
00459         }
00460         static function namespacenumber( $parser, $title = null ) {
00461                 $t = Title::newFromText( $title );
00462                 if ( is_null( $t ) )
00463                         return '';
00464                 return $t->getNamespace();
00465         }
00466         static function talkspace( $parser, $title = null ) {
00467                 $t = Title::newFromText( $title );
00468                 if ( is_null( $t ) || !$t->canTalk() )
00469                         return '';
00470                 return str_replace( '_', ' ', $t->getTalkNsText() );
00471         }
00472         static function talkspacee( $parser, $title = null ) {
00473                 $t = Title::newFromText( $title );
00474                 if ( is_null( $t ) || !$t->canTalk() )
00475                         return '';
00476                 return wfUrlencode( $t->getTalkNsText() );
00477         }
00478         static function subjectspace( $parser, $title = null ) {
00479                 $t = Title::newFromText( $title );
00480                 if ( is_null( $t ) )
00481                         return '';
00482                 return str_replace( '_', ' ', $t->getSubjectNsText() );
00483         }
00484         static function subjectspacee( $parser, $title = null ) {
00485                 $t = Title::newFromText( $title );
00486                 if ( is_null( $t ) )
00487                         return '';
00488                 return wfUrlencode( $t->getSubjectNsText() );
00489         }
00490 
00496         static function pagename( $parser, $title = null ) {
00497                 $t = Title::newFromText( $title );
00498                 if ( is_null( $t ) )
00499                         return '';
00500                 return wfEscapeWikiText( $t->getText() );
00501         }
00502         static function pagenamee( $parser, $title = null ) {
00503                 $t = Title::newFromText( $title );
00504                 if ( is_null( $t ) )
00505                         return '';
00506                 return wfEscapeWikiText( $t->getPartialURL() );
00507         }
00508         static function fullpagename( $parser, $title = null ) {
00509                 $t = Title::newFromText( $title );
00510                 if ( is_null( $t ) || !$t->canTalk() )
00511                         return '';
00512                 return wfEscapeWikiText( $t->getPrefixedText() );
00513         }
00514         static function fullpagenamee( $parser, $title = null ) {
00515                 $t = Title::newFromText( $title );
00516                 if ( is_null( $t ) || !$t->canTalk() )
00517                         return '';
00518                 return wfEscapeWikiText( $t->getPrefixedURL() );
00519         }
00520         static function subpagename( $parser, $title = null ) {
00521                 $t = Title::newFromText( $title );
00522                 if ( is_null( $t ) )
00523                         return '';
00524                 return wfEscapeWikiText( $t->getSubpageText() );
00525         }
00526         static function subpagenamee( $parser, $title = null ) {
00527                 $t = Title::newFromText( $title );
00528                 if ( is_null( $t ) )
00529                         return '';
00530                 return wfEscapeWikiText( $t->getSubpageUrlForm() );
00531         }
00532         static function basepagename( $parser, $title = null ) {
00533                 $t = Title::newFromText( $title );
00534                 if ( is_null( $t ) )
00535                         return '';
00536                 return wfEscapeWikiText( $t->getBaseText() );
00537         }
00538         static function basepagenamee( $parser, $title = null ) {
00539                 $t = Title::newFromText( $title );
00540                 if ( is_null( $t ) )
00541                         return '';
00542                 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
00543         }
00544         static function talkpagename( $parser, $title = null ) {
00545                 $t = Title::newFromText( $title );
00546                 if ( is_null( $t ) || !$t->canTalk() )
00547                         return '';
00548                 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
00549         }
00550         static function talkpagenamee( $parser, $title = null ) {
00551                 $t = Title::newFromText( $title );
00552                 if ( is_null( $t ) || !$t->canTalk() )
00553                         return '';
00554                 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
00555         }
00556         static function subjectpagename( $parser, $title = null ) {
00557                 $t = Title::newFromText( $title );
00558                 if ( is_null( $t ) )
00559                         return '';
00560                 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
00561         }
00562         static function subjectpagenamee( $parser, $title = null ) {
00563                 $t = Title::newFromText( $title );
00564                 if ( is_null( $t ) )
00565                         return '';
00566                 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
00567         }
00568 
00575         static function pagesincategory( $parser, $name = '', $arg1 = null, $arg2 = null ) {
00576                 static $magicWords = null;
00577                 if ( is_null( $magicWords ) ) {
00578                         $magicWords = new MagicWordArray( array(
00579                                 'pagesincategory_all',
00580                                 'pagesincategory_pages',
00581                                 'pagesincategory_subcats',
00582                                 'pagesincategory_files'
00583                         ) );
00584                 }
00585                 static $cache = array();
00586 
00587                 // split the given option to its variable
00588                 if( self::isRaw( $arg1 ) ) {
00589                         //{{pagesincategory:|raw[|type]}}
00590                         $raw = $arg1;
00591                         $type = $magicWords->matchStartToEnd( $arg2 );
00592                 } else {
00593                         //{{pagesincategory:[|type[|raw]]}}
00594                         $type = $magicWords->matchStartToEnd( $arg1 );
00595                         $raw = $arg2;
00596                 }
00597                 if( !$type ) { //backward compatibility
00598                         $type = 'pagesincategory_all';
00599                 }
00600 
00601                 $title = Title::makeTitleSafe( NS_CATEGORY, $name );
00602                 if( !$title ) { # invalid title
00603                         return self::formatRaw( 0, $raw );
00604                 }
00605 
00606                 // Normalize name for cache
00607                 $name = $title->getDBkey();
00608 
00609                 if( !isset( $cache[$name] ) ) {
00610                         $category = Category::newFromTitle( $title );
00611 
00612                         $allCount = $subcatCount = $fileCount = $pagesCount = 0;
00613                         if( $parser->incrementExpensiveFunctionCount() ) {
00614                                 // $allCount is the total number of cat members,
00615                                 // not the count of how many members are normal pages.
00616                                 $allCount = (int)$category->getPageCount();
00617                                 $subcatCount = (int)$category->getSubcatCount();
00618                                 $fileCount = (int)$category->getFileCount();
00619                                 $pagesCount = $allCount - $subcatCount - $fileCount;
00620                         }
00621                         $cache[$name]['pagesincategory_all'] = $allCount;
00622                         $cache[$name]['pagesincategory_pages'] = $pagesCount;
00623                         $cache[$name]['pagesincategory_subcats'] = $subcatCount;
00624                         $cache[$name]['pagesincategory_files'] = $fileCount;
00625                 }
00626 
00627                 $count = $cache[$name][$type];
00628                 return self::formatRaw( $count, $raw );
00629         }
00630 
00648         static function pagesize( $parser, $page = '', $raw = null ) {
00649                 static $cache = array();
00650                 $title = Title::newFromText( $page );
00651 
00652                 if( !is_object( $title ) ) {
00653                         $cache[$page] = 0;
00654                         return self::formatRaw( 0, $raw );
00655                 }
00656 
00657                 # Normalize name for cache
00658                 $page = $title->getPrefixedText();
00659 
00660                 $length = 0;
00661                 if( isset( $cache[$page] ) ) {
00662                         $length = $cache[$page];
00663                 } elseif( $parser->incrementExpensiveFunctionCount() ) {
00664                         $rev = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
00665                         $id = $rev ? $rev->getPage() : 0;
00666                         $length = $cache[$page] = $rev ? $rev->getSize() : 0;
00667 
00668                         // Register dependency in templatelinks
00669                         $parser->mOutput->addTemplate( $title, $id, $rev ? $rev->getId() : 0 );
00670                 }
00671                 return self::formatRaw( $length, $raw );
00672         }
00673 
00678         static function protectionlevel( $parser, $type = '' ) {
00679                 $restrictions = $parser->mTitle->getRestrictions( strtolower( $type ) );
00680                 # Title::getRestrictions returns an array, its possible it may have
00681                 # multiple values in the future
00682                 return implode( $restrictions, ',' );
00683         }
00684 
00692         static function language( $parser, $code = '', $inLanguage = '' ) {
00693                 $code = strtolower( $code );
00694                 $inLanguage = strtolower( $inLanguage );
00695                 $lang = Language::fetchLanguageName( $code, $inLanguage );
00696                 return $lang !== '' ? $lang : wfBCP47( $code );
00697         }
00698 
00703         static function pad( $parser, $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) {
00704                 $padding = $parser->killMarkers( $padding );
00705                 $lengthOfPadding = mb_strlen( $padding );
00706                 if ( $lengthOfPadding == 0 ) return $string;
00707 
00708                 # The remaining length to add counts down to 0 as padding is added
00709                 $length = min( $length, 500 ) - mb_strlen( $string );
00710                 # $finalPadding is just $padding repeated enough times so that
00711                 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
00712                 $finalPadding = '';
00713                 while ( $length > 0 ) {
00714                         # If $length < $lengthofPadding, truncate $padding so we get the
00715                         # exact length desired.
00716                         $finalPadding .= mb_substr( $padding, 0, $length );
00717                         $length -= $lengthOfPadding;
00718                 }
00719 
00720                 if ( $direction == STR_PAD_LEFT ) {
00721                         return $finalPadding . $string;
00722                 } else {
00723                         return $string . $finalPadding;
00724                 }
00725         }
00726 
00727         static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
00728                 return self::pad( $parser, $string, $length, $padding, STR_PAD_LEFT );
00729         }
00730 
00731         static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
00732                 return self::pad( $parser, $string, $length, $padding );
00733         }
00734 
00740         static function anchorencode( $parser, $text ) {
00741                 $text = $parser->killMarkers( $text );
00742                 return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
00743         }
00744 
00745         static function special( $parser, $text ) {
00746                 list( $page, $subpage ) = SpecialPageFactory::resolveAlias( $text );
00747                 if ( $page ) {
00748                         $title = SpecialPage::getTitleFor( $page, $subpage );
00749                         return $title->getPrefixedText();
00750                 } else {
00751                         return wfMessage( 'nosuchspecialpage' )->inContentLanguage()->text();
00752                 }
00753         }
00754 
00755         static function speciale( $parser, $text ) {
00756                 return wfUrlencode( str_replace( ' ', '_', self::special( $parser, $text ) ) );
00757         }
00758 
00767         public static function defaultsort( $parser, $text, $uarg = '' ) {
00768                 static $magicWords = null;
00769                 if ( is_null( $magicWords ) ) {
00770                         $magicWords = new MagicWordArray( array( 'defaultsort_noerror', 'defaultsort_noreplace' ) );
00771                 }
00772                 $arg = $magicWords->matchStartToEnd( $uarg );
00773 
00774                 $text = trim( $text );
00775                 if( strlen( $text ) == 0 )
00776                         return '';
00777                 $old = $parser->getCustomDefaultSort();
00778                 if ( $old === false || $arg !== 'defaultsort_noreplace' ) {
00779                         $parser->setDefaultSort( $text );
00780                 }
00781 
00782                 if( $old === false || $old == $text || $arg ) {
00783                         return '';
00784                 } else {
00785                         return( '<span class="error">' .
00786                                 wfMessage( 'duplicate-defaultsort', $old, $text )->inContentLanguage()->escaped() .
00787                                 '</span>' );
00788                 }
00789         }
00790 
00791         // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
00792         // or {{filepath|300px}}, {{filepath|200x300px}}, {{filepath|nowiki|200x300px}}, {{filepath|200x300px|nowiki}}
00793         public static function filepath( $parser, $name='', $argA='', $argB='' ) {
00794                 $file = wfFindFile( $name );
00795 
00796                 if( $argA == 'nowiki' ) {
00797                         // {{filepath: | option [| size] }}
00798                         $isNowiki = true;
00799                         $parsedWidthParam = $parser->parseWidthParam( $argB );
00800                 } else {
00801                         // {{filepath: [| size [|option]] }}
00802                         $parsedWidthParam = $parser->parseWidthParam( $argA );
00803                         $isNowiki = ($argB == 'nowiki');
00804                 }
00805 
00806                 if ( $file ) {
00807                         $url = $file->getFullUrl();
00808 
00809                         // If a size is requested...
00810                         if ( count( $parsedWidthParam ) ) {
00811                                 $mto = $file->transform( $parsedWidthParam );
00812                                 // ... and we can
00813                                 if ( $mto && !$mto->isError() ) {
00814                                         // ... change the URL to point to a thumbnail.
00815                                         $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE );
00816                                 }
00817                         }
00818                         if ( $isNowiki ) {
00819                                 return array( $url, 'nowiki' => true );
00820                         }
00821                         return $url;
00822                 } else {
00823                         return '';
00824                 }
00825         }
00826 
00831         public static function tagObj( $parser, $frame, $args ) {
00832                 if ( !count( $args ) ) {
00833                         return '';
00834                 }
00835                 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
00836 
00837                 if ( count( $args ) ) {
00838                         $inner = $frame->expand( array_shift( $args ) );
00839                 } else {
00840                         $inner = null;
00841                 }
00842 
00843                 $stripList = $parser->getStripList();
00844                 if ( !in_array( $tagName, $stripList ) ) {
00845                         return '<span class="error">' .
00846                                 wfMessage( 'unknown_extension_tag', $tagName )->inContentLanguage()->text() .
00847                                 '</span>';
00848                 }
00849 
00850                 $attributes = array();
00851                 foreach ( $args as $arg ) {
00852                         $bits = $arg->splitArg();
00853                         if ( strval( $bits['index'] ) === '' ) {
00854                                 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
00855                                 $value = trim( $frame->expand( $bits['value'] ) );
00856                                 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
00857                                         $value = isset( $m[1] ) ? $m[1] : '';
00858                                 }
00859                                 $attributes[$name] = $value;
00860                         }
00861                 }
00862 
00863                 $params = array(
00864                         'name' => $tagName,
00865                         'inner' => $inner,
00866                         'attributes' => $attributes,
00867                         'close' => "</$tagName>",
00868                 );
00869                 return $parser->extensionSubstitution( $params, $frame );
00870         }
00871 }