MediaWiki  REL1_21
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 = '', $arg = null ) {
00276                 if ( self::matchAgainstMagicword( 'rawsuffix', $arg ) ) {
00277                         $func = array( $parser->getFunctionLang(), 'parseFormattedNumber' );
00278                 } elseif ( self::matchAgainstMagicword( 'nocommafysuffix', $arg ) ) {
00279                         $func = array( $parser->getFunctionLang(), 'formatNumNoSeparators' );
00280                 } else {
00281                         $func = array( $parser->getFunctionLang(), 'formatNum' );
00282                 }
00283                 return $parser->markerSkipCallback( $num, $func );
00284         }
00285 
00292         static function grammar( $parser, $case = '', $word = '' ) {
00293                 $word = $parser->killMarkers( $word );
00294                 return $parser->getFunctionLang()->convertGrammar( $word, $case );
00295         }
00296 
00302         static function gender( $parser, $username ) {
00303                 wfProfileIn( __METHOD__ );
00304                 $forms = array_slice( func_get_args(), 2 );
00305 
00306                 // Some shortcuts to avoid loading user data unnecessarily
00307                 if ( count( $forms ) === 0 ) {
00308                         wfProfileOut( __METHOD__ );
00309                         return '';
00310                 } elseif ( count( $forms ) === 1 ) {
00311                         wfProfileOut( __METHOD__ );
00312                         return $forms[0];
00313                 }
00314 
00315                 $username = trim( $username );
00316 
00317                 // default
00318                 $gender = User::getDefaultOption( 'gender' );
00319 
00320                 // allow prefix.
00321                 $title = Title::newFromText( $username );
00322 
00323                 if ( $title && $title->getNamespace() == NS_USER ) {
00324                         $username = $title->getText();
00325                 }
00326 
00327                 // check parameter, or use the ParserOptions if in interface message
00328                 $user = User::newFromName( $username );
00329                 if ( $user ) {
00330                         $gender = GenderCache::singleton()->getGenderOf( $user, __METHOD__ );
00331                 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
00332                         $gender = GenderCache::singleton()->getGenderOf( $parser->getOptions()->getUser(), __METHOD__ );
00333                 }
00334                 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
00335                 wfProfileOut( __METHOD__ );
00336                 return $ret;
00337         }
00338 
00344         static function plural( $parser, $text = '' ) {
00345                 $forms = array_slice( func_get_args(), 2 );
00346                 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
00347                 settype( $text, ctype_digit( $text ) ? 'int' : 'float' );
00348                 return $parser->getFunctionLang()->convertPlural( $text, $forms );
00349         }
00350 
00359         static function displaytitle( $parser, $text = '' ) {
00360                 global $wgRestrictDisplayTitle;
00361 
00362                 #parse a limited subset of wiki markup (just the single quote items)
00363                 $text = $parser->doQuotes( $text );
00364 
00365                 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
00366                 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
00367                         . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '/', '', $text );
00368 
00369                 #list of disallowed tags for DISPLAYTITLE
00370                 #these will be escaped even though they are allowed in normal wiki text
00371                 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
00372                         'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
00373 
00374                 #only requested titles that normalize to the actual title are allowed through
00375                 #if $wgRestrictDisplayTitle is true (it is by default)
00376                 #mimic the escaping process that occurs in OutputPage::setPageTitle
00377                 $text = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $text, null, array(), array(), $bad ) );
00378                 $title = Title::newFromText( Sanitizer::stripAllTags( $text ) );
00379 
00380                 if( !$wgRestrictDisplayTitle ) {
00381                         $parser->mOutput->setDisplayTitle( $text );
00382                 } else {
00383                         if ( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) ) {
00384                                 $parser->mOutput->setDisplayTitle( $text );
00385                         }
00386                 }
00387 
00388                 return '';
00389         }
00390 
00398         static private function matchAgainstMagicword( $magicword, $value ) {
00399                 if ( strval( $value ) === '' ) {
00400                         return false;
00401                 }
00402                 $mwObject = MagicWord::get( $magicword );
00403                 return $mwObject->match( $value );
00404         }
00405 
00406         static function formatRaw( $num, $raw ) {
00407                 if( self::matchAgainstMagicword( 'rawsuffix', $raw ) ) {
00408                         return $num;
00409                 } else {
00410                         global $wgContLang;
00411                         return $wgContLang->formatNum( $num );
00412                 }
00413         }
00414         static function numberofpages( $parser, $raw = null ) {
00415                 return self::formatRaw( SiteStats::pages(), $raw );
00416         }
00417         static function numberofusers( $parser, $raw = null ) {
00418                 return self::formatRaw( SiteStats::users(), $raw );
00419         }
00420         static function numberofactiveusers( $parser, $raw = null ) {
00421                 return self::formatRaw( SiteStats::activeUsers(), $raw );
00422         }
00423         static function numberofarticles( $parser, $raw = null ) {
00424                 return self::formatRaw( SiteStats::articles(), $raw );
00425         }
00426         static function numberoffiles( $parser, $raw = null ) {
00427                 return self::formatRaw( SiteStats::images(), $raw );
00428         }
00429         static function numberofadmins( $parser, $raw = null ) {
00430                 return self::formatRaw( SiteStats::numberingroup( 'sysop' ), $raw );
00431         }
00432         static function numberofedits( $parser, $raw = null ) {
00433                 return self::formatRaw( SiteStats::edits(), $raw );
00434         }
00435         static function numberofviews( $parser, $raw = null ) {
00436                 return self::formatRaw( SiteStats::views(), $raw );
00437         }
00438         static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
00439                 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
00440         }
00441         static function numberingroup( $parser, $name = '', $raw = null) {
00442                 return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
00443         }
00444 
00452         static function mwnamespace( $parser, $title = null ) {
00453                 $t = Title::newFromText( $title );
00454                 if ( is_null( $t ) )
00455                         return '';
00456                 return str_replace( '_', ' ', $t->getNsText() );
00457         }
00458         static function namespacee( $parser, $title = null ) {
00459                 $t = Title::newFromText( $title );
00460                 if ( is_null( $t ) )
00461                         return '';
00462                 return wfUrlencode( $t->getNsText() );
00463         }
00464         static function namespacenumber( $parser, $title = null ) {
00465                 $t = Title::newFromText( $title );
00466                 if ( is_null( $t ) )
00467                         return '';
00468                 return $t->getNamespace();
00469         }
00470         static function talkspace( $parser, $title = null ) {
00471                 $t = Title::newFromText( $title );
00472                 if ( is_null( $t ) || !$t->canTalk() )
00473                         return '';
00474                 return str_replace( '_', ' ', $t->getTalkNsText() );
00475         }
00476         static function talkspacee( $parser, $title = null ) {
00477                 $t = Title::newFromText( $title );
00478                 if ( is_null( $t ) || !$t->canTalk() )
00479                         return '';
00480                 return wfUrlencode( $t->getTalkNsText() );
00481         }
00482         static function subjectspace( $parser, $title = null ) {
00483                 $t = Title::newFromText( $title );
00484                 if ( is_null( $t ) )
00485                         return '';
00486                 return str_replace( '_', ' ', $t->getSubjectNsText() );
00487         }
00488         static function subjectspacee( $parser, $title = null ) {
00489                 $t = Title::newFromText( $title );
00490                 if ( is_null( $t ) )
00491                         return '';
00492                 return wfUrlencode( $t->getSubjectNsText() );
00493         }
00494 
00500         static function pagename( $parser, $title = null ) {
00501                 $t = Title::newFromText( $title );
00502                 if ( is_null( $t ) )
00503                         return '';
00504                 return wfEscapeWikiText( $t->getText() );
00505         }
00506         static function pagenamee( $parser, $title = null ) {
00507                 $t = Title::newFromText( $title );
00508                 if ( is_null( $t ) )
00509                         return '';
00510                 return wfEscapeWikiText( $t->getPartialURL() );
00511         }
00512         static function fullpagename( $parser, $title = null ) {
00513                 $t = Title::newFromText( $title );
00514                 if ( is_null( $t ) || !$t->canTalk() )
00515                         return '';
00516                 return wfEscapeWikiText( $t->getPrefixedText() );
00517         }
00518         static function fullpagenamee( $parser, $title = null ) {
00519                 $t = Title::newFromText( $title );
00520                 if ( is_null( $t ) || !$t->canTalk() )
00521                         return '';
00522                 return wfEscapeWikiText( $t->getPrefixedURL() );
00523         }
00524         static function subpagename( $parser, $title = null ) {
00525                 $t = Title::newFromText( $title );
00526                 if ( is_null( $t ) )
00527                         return '';
00528                 return wfEscapeWikiText( $t->getSubpageText() );
00529         }
00530         static function subpagenamee( $parser, $title = null ) {
00531                 $t = Title::newFromText( $title );
00532                 if ( is_null( $t ) )
00533                         return '';
00534                 return wfEscapeWikiText( $t->getSubpageUrlForm() );
00535         }
00536         static function basepagename( $parser, $title = null ) {
00537                 $t = Title::newFromText( $title );
00538                 if ( is_null( $t ) )
00539                         return '';
00540                 return wfEscapeWikiText( $t->getBaseText() );
00541         }
00542         static function basepagenamee( $parser, $title = null ) {
00543                 $t = Title::newFromText( $title );
00544                 if ( is_null( $t ) )
00545                         return '';
00546                 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
00547         }
00548         static function talkpagename( $parser, $title = null ) {
00549                 $t = Title::newFromText( $title );
00550                 if ( is_null( $t ) || !$t->canTalk() )
00551                         return '';
00552                 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
00553         }
00554         static function talkpagenamee( $parser, $title = null ) {
00555                 $t = Title::newFromText( $title );
00556                 if ( is_null( $t ) || !$t->canTalk() )
00557                         return '';
00558                 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
00559         }
00560         static function subjectpagename( $parser, $title = null ) {
00561                 $t = Title::newFromText( $title );
00562                 if ( is_null( $t ) )
00563                         return '';
00564                 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
00565         }
00566         static function subjectpagenamee( $parser, $title = null ) {
00567                 $t = Title::newFromText( $title );
00568                 if ( is_null( $t ) )
00569                         return '';
00570                 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
00571         }
00572 
00579         static function pagesincategory( $parser, $name = '', $arg1 = null, $arg2 = null ) {
00580                 static $magicWords = null;
00581                 if ( is_null( $magicWords ) ) {
00582                         $magicWords = new MagicWordArray( array(
00583                                 'pagesincategory_all',
00584                                 'pagesincategory_pages',
00585                                 'pagesincategory_subcats',
00586                                 'pagesincategory_files'
00587                         ) );
00588                 }
00589                 static $cache = array();
00590 
00591                 // split the given option to its variable
00592                 if( self::matchAgainstMagicword( 'rawsuffix', $arg1 ) ) {
00593                         //{{pagesincategory:|raw[|type]}}
00594                         $raw = $arg1;
00595                         $type = $magicWords->matchStartToEnd( $arg2 );
00596                 } else {
00597                         //{{pagesincategory:[|type[|raw]]}}
00598                         $type = $magicWords->matchStartToEnd( $arg1 );
00599                         $raw = $arg2;
00600                 }
00601                 if( !$type ) { //backward compatibility
00602                         $type = 'pagesincategory_all';
00603                 }
00604 
00605                 $title = Title::makeTitleSafe( NS_CATEGORY, $name );
00606                 if( !$title ) { # invalid title
00607                         return self::formatRaw( 0, $raw );
00608                 }
00609 
00610                 // Normalize name for cache
00611                 $name = $title->getDBkey();
00612 
00613                 if( !isset( $cache[$name] ) ) {
00614                         $category = Category::newFromTitle( $title );
00615 
00616                         $allCount = $subcatCount = $fileCount = $pagesCount = 0;
00617                         if( $parser->incrementExpensiveFunctionCount() ) {
00618                                 // $allCount is the total number of cat members,
00619                                 // not the count of how many members are normal pages.
00620                                 $allCount = (int)$category->getPageCount();
00621                                 $subcatCount = (int)$category->getSubcatCount();
00622                                 $fileCount = (int)$category->getFileCount();
00623                                 $pagesCount = $allCount - $subcatCount - $fileCount;
00624                         }
00625                         $cache[$name]['pagesincategory_all'] = $allCount;
00626                         $cache[$name]['pagesincategory_pages'] = $pagesCount;
00627                         $cache[$name]['pagesincategory_subcats'] = $subcatCount;
00628                         $cache[$name]['pagesincategory_files'] = $fileCount;
00629                 }
00630 
00631                 $count = $cache[$name][$type];
00632                 return self::formatRaw( $count, $raw );
00633         }
00634 
00652         static function pagesize( $parser, $page = '', $raw = null ) {
00653                 static $cache = array();
00654                 $title = Title::newFromText( $page );
00655 
00656                 if( !is_object( $title ) ) {
00657                         $cache[$page] = 0;
00658                         return self::formatRaw( 0, $raw );
00659                 }
00660 
00661                 # Normalize name for cache
00662                 $page = $title->getPrefixedText();
00663 
00664                 $length = 0;
00665                 if( isset( $cache[$page] ) ) {
00666                         $length = $cache[$page];
00667                 } elseif( $parser->incrementExpensiveFunctionCount() ) {
00668                         $rev = Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
00669                         $pageID = $rev ? $rev->getPage() : 0;
00670                         $revID = $rev ? $rev->getId() : 0;
00671                         $length = $cache[$page] = $rev ? $rev->getSize() : 0;
00672 
00673                         // Register dependency in templatelinks
00674                         $parser->mOutput->addTemplate( $title, $pageID, $revID );
00675                 }
00676                 return self::formatRaw( $length, $raw );
00677         }
00678 
00688         static function protectionlevel( $parser, $type = '', $title = '' ) {
00689                 $titleObject = Title::newFromText( $title );
00690                 if ( !( $titleObject instanceof Title ) ) {
00691                         $titleObject = $parser->mTitle;
00692                 }
00693                 $restrictions = $titleObject->getRestrictions( strtolower( $type ) );
00694                 # Title::getRestrictions returns an array, its possible it may have
00695                 # multiple values in the future
00696                 return implode( $restrictions, ',' );
00697         }
00698 
00706         static function language( $parser, $code = '', $inLanguage = '' ) {
00707                 $code = strtolower( $code );
00708                 $inLanguage = strtolower( $inLanguage );
00709                 $lang = Language::fetchLanguageName( $code, $inLanguage );
00710                 return $lang !== '' ? $lang : wfBCP47( $code );
00711         }
00712 
00717         static function pad( $parser, $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) {
00718                 $padding = $parser->killMarkers( $padding );
00719                 $lengthOfPadding = mb_strlen( $padding );
00720                 if ( $lengthOfPadding == 0 ) return $string;
00721 
00722                 # The remaining length to add counts down to 0 as padding is added
00723                 $length = min( $length, 500 ) - mb_strlen( $string );
00724                 # $finalPadding is just $padding repeated enough times so that
00725                 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
00726                 $finalPadding = '';
00727                 while ( $length > 0 ) {
00728                         # If $length < $lengthofPadding, truncate $padding so we get the
00729                         # exact length desired.
00730                         $finalPadding .= mb_substr( $padding, 0, $length );
00731                         $length -= $lengthOfPadding;
00732                 }
00733 
00734                 if ( $direction == STR_PAD_LEFT ) {
00735                         return $finalPadding . $string;
00736                 } else {
00737                         return $string . $finalPadding;
00738                 }
00739         }
00740 
00741         static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
00742                 return self::pad( $parser, $string, $length, $padding, STR_PAD_LEFT );
00743         }
00744 
00745         static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
00746                 return self::pad( $parser, $string, $length, $padding );
00747         }
00748 
00754         static function anchorencode( $parser, $text ) {
00755                 $text = $parser->killMarkers( $text );
00756                 return (string)substr( $parser->guessSectionNameFromWikiText( $text ), 1 );
00757         }
00758 
00759         static function special( $parser, $text ) {
00760                 list( $page, $subpage ) = SpecialPageFactory::resolveAlias( $text );
00761                 if ( $page ) {
00762                         $title = SpecialPage::getTitleFor( $page, $subpage );
00763                         return $title->getPrefixedText();
00764                 } else {
00765                         return wfMessage( 'nosuchspecialpage' )->inContentLanguage()->text();
00766                 }
00767         }
00768 
00769         static function speciale( $parser, $text ) {
00770                 return wfUrlencode( str_replace( ' ', '_', self::special( $parser, $text ) ) );
00771         }
00772 
00781         public static function defaultsort( $parser, $text, $uarg = '' ) {
00782                 static $magicWords = null;
00783                 if ( is_null( $magicWords ) ) {
00784                         $magicWords = new MagicWordArray( array( 'defaultsort_noerror', 'defaultsort_noreplace' ) );
00785                 }
00786                 $arg = $magicWords->matchStartToEnd( $uarg );
00787 
00788                 $text = trim( $text );
00789                 if( strlen( $text ) == 0 )
00790                         return '';
00791                 $old = $parser->getCustomDefaultSort();
00792                 if ( $old === false || $arg !== 'defaultsort_noreplace' ) {
00793                         $parser->setDefaultSort( $text );
00794                 }
00795 
00796                 if( $old === false || $old == $text || $arg ) {
00797                         return '';
00798                 } else {
00799                         return( '<span class="error">' .
00800                                 wfMessage( 'duplicate-defaultsort', $old, $text )->inContentLanguage()->escaped() .
00801                                 '</span>' );
00802                 }
00803         }
00804 
00805         // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
00806         // or {{filepath|300px}}, {{filepath|200x300px}}, {{filepath|nowiki|200x300px}}, {{filepath|200x300px|nowiki}}
00807         public static function filepath( $parser, $name = '', $argA = '', $argB = '' ) {
00808                 $file = wfFindFile( $name );
00809 
00810                 if( $argA == 'nowiki' ) {
00811                         // {{filepath: | option [| size] }}
00812                         $isNowiki = true;
00813                         $parsedWidthParam = $parser->parseWidthParam( $argB );
00814                 } else {
00815                         // {{filepath: [| size [|option]] }}
00816                         $parsedWidthParam = $parser->parseWidthParam( $argA );
00817                         $isNowiki = ($argB == 'nowiki');
00818                 }
00819 
00820                 if ( $file ) {
00821                         $url = $file->getFullUrl();
00822 
00823                         // If a size is requested...
00824                         if ( count( $parsedWidthParam ) ) {
00825                                 $mto = $file->transform( $parsedWidthParam );
00826                                 // ... and we can
00827                                 if ( $mto && !$mto->isError() ) {
00828                                         // ... change the URL to point to a thumbnail.
00829                                         $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE );
00830                                 }
00831                         }
00832                         if ( $isNowiki ) {
00833                                 return array( $url, 'nowiki' => true );
00834                         }
00835                         return $url;
00836                 } else {
00837                         return '';
00838                 }
00839         }
00840 
00845         public static function tagObj( $parser, $frame, $args ) {
00846                 if ( !count( $args ) ) {
00847                         return '';
00848                 }
00849                 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
00850 
00851                 if ( count( $args ) ) {
00852                         $inner = $frame->expand( array_shift( $args ) );
00853                 } else {
00854                         $inner = null;
00855                 }
00856 
00857                 $stripList = $parser->getStripList();
00858                 if ( !in_array( $tagName, $stripList ) ) {
00859                         return '<span class="error">' .
00860                                 wfMessage( 'unknown_extension_tag', $tagName )->inContentLanguage()->text() .
00861                                 '</span>';
00862                 }
00863 
00864                 $attributes = array();
00865                 foreach ( $args as $arg ) {
00866                         $bits = $arg->splitArg();
00867                         if ( strval( $bits['index'] ) === '' ) {
00868                                 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
00869                                 $value = trim( $frame->expand( $bits['value'] ) );
00870                                 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
00871                                         $value = isset( $m[1] ) ? $m[1] : '';
00872                                 }
00873                                 $attributes[$name] = $value;
00874                         }
00875                 }
00876 
00877                 $params = array(
00878                         'name' => $tagName,
00879                         'inner' => $inner,
00880                         'attributes' => $attributes,
00881                         'close' => "</$tagName>",
00882                 );
00883                 return $parser->extensionSubstitution( $params, $frame );
00884         }
00885 }