MediaWiki  REL1_19
CoreParserFunctions.php
Go to the documentation of this file.
00001 <?php
00012 class CoreParserFunctions {
00017         static function register( $parser ) {
00018                 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
00019 
00020                 # Syntax for arguments (see self::setFunctionHook):
00021                 #  "name for lookup in localized magic words array",
00022                 #  function callback,
00023                 #  optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}}
00024                 #    instead of {{#int:...}})
00025 
00026                 $parser->setFunctionHook( 'int',              array( __CLASS__, 'intFunction'      ), SFH_NO_HASH );
00027                 $parser->setFunctionHook( 'ns',               array( __CLASS__, 'ns'               ), SFH_NO_HASH );
00028                 $parser->setFunctionHook( 'nse',              array( __CLASS__, 'nse'              ), SFH_NO_HASH );
00029                 $parser->setFunctionHook( 'urlencode',        array( __CLASS__, 'urlencode'        ), SFH_NO_HASH );
00030                 $parser->setFunctionHook( 'lcfirst',          array( __CLASS__, 'lcfirst'          ), SFH_NO_HASH );
00031                 $parser->setFunctionHook( 'ucfirst',          array( __CLASS__, 'ucfirst'          ), SFH_NO_HASH );
00032                 $parser->setFunctionHook( 'lc',               array( __CLASS__, 'lc'               ), SFH_NO_HASH );
00033                 $parser->setFunctionHook( 'uc',               array( __CLASS__, 'uc'               ), SFH_NO_HASH );
00034                 $parser->setFunctionHook( 'localurl',         array( __CLASS__, 'localurl'         ), SFH_NO_HASH );
00035                 $parser->setFunctionHook( 'localurle',        array( __CLASS__, 'localurle'        ), SFH_NO_HASH );
00036                 $parser->setFunctionHook( 'fullurl',          array( __CLASS__, 'fullurl'          ), SFH_NO_HASH );
00037                 $parser->setFunctionHook( 'fullurle',         array( __CLASS__, 'fullurle'         ), SFH_NO_HASH );
00038                 $parser->setFunctionHook( 'canonicalurl',     array( __CLASS__, 'canonicalurl'     ), SFH_NO_HASH );
00039                 $parser->setFunctionHook( 'canonicalurle',    array( __CLASS__, 'canonicalurle'    ), SFH_NO_HASH );
00040                 $parser->setFunctionHook( 'formatnum',        array( __CLASS__, 'formatnum'        ), SFH_NO_HASH );
00041                 $parser->setFunctionHook( 'grammar',          array( __CLASS__, 'grammar'          ), SFH_NO_HASH );
00042                 $parser->setFunctionHook( 'gender',           array( __CLASS__, 'gender'           ), SFH_NO_HASH );
00043                 $parser->setFunctionHook( 'plural',           array( __CLASS__, 'plural'           ), SFH_NO_HASH );
00044                 $parser->setFunctionHook( 'numberofpages',    array( __CLASS__, 'numberofpages'    ), SFH_NO_HASH );
00045                 $parser->setFunctionHook( 'numberofusers',    array( __CLASS__, 'numberofusers'    ), SFH_NO_HASH );
00046                 $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__, 'numberofactiveusers' ), SFH_NO_HASH );
00047                 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__, 'numberofarticles' ), SFH_NO_HASH );
00048                 $parser->setFunctionHook( 'numberoffiles',    array( __CLASS__, 'numberoffiles'    ), SFH_NO_HASH );
00049                 $parser->setFunctionHook( 'numberofadmins',   array( __CLASS__, 'numberofadmins'   ), SFH_NO_HASH );
00050                 $parser->setFunctionHook( 'numberingroup',    array( __CLASS__, 'numberingroup'    ), SFH_NO_HASH );
00051                 $parser->setFunctionHook( 'numberofedits',    array( __CLASS__, 'numberofedits'    ), SFH_NO_HASH );
00052                 $parser->setFunctionHook( 'numberofviews',    array( __CLASS__, 'numberofviews'    ), SFH_NO_HASH );
00053                 $parser->setFunctionHook( 'language',         array( __CLASS__, 'language'         ), SFH_NO_HASH );
00054                 $parser->setFunctionHook( 'padleft',          array( __CLASS__, 'padleft'          ), SFH_NO_HASH );
00055                 $parser->setFunctionHook( 'padright',         array( __CLASS__, 'padright'         ), SFH_NO_HASH );
00056                 $parser->setFunctionHook( 'anchorencode',     array( __CLASS__, 'anchorencode'     ), SFH_NO_HASH );
00057                 $parser->setFunctionHook( 'special',          array( __CLASS__, 'special'          ) );
00058                 $parser->setFunctionHook( 'defaultsort',      array( __CLASS__, 'defaultsort'      ), SFH_NO_HASH );
00059                 $parser->setFunctionHook( 'filepath',         array( __CLASS__, 'filepath'         ), SFH_NO_HASH );
00060                 $parser->setFunctionHook( 'pagesincategory',  array( __CLASS__, 'pagesincategory'  ), SFH_NO_HASH );
00061                 $parser->setFunctionHook( 'pagesize',         array( __CLASS__, 'pagesize'         ), SFH_NO_HASH );
00062                 $parser->setFunctionHook( 'protectionlevel',  array( __CLASS__, 'protectionlevel'  ), SFH_NO_HASH );
00063                 $parser->setFunctionHook( 'namespace',        array( __CLASS__, 'mwnamespace'      ), SFH_NO_HASH );
00064                 $parser->setFunctionHook( 'namespacee',       array( __CLASS__, 'namespacee'       ), SFH_NO_HASH );
00065                 $parser->setFunctionHook( 'talkspace',        array( __CLASS__, 'talkspace'        ), SFH_NO_HASH );
00066                 $parser->setFunctionHook( 'talkspacee',       array( __CLASS__, 'talkspacee'       ), SFH_NO_HASH );
00067                 $parser->setFunctionHook( 'subjectspace',     array( __CLASS__, 'subjectspace'     ), SFH_NO_HASH );
00068                 $parser->setFunctionHook( 'subjectspacee',    array( __CLASS__, 'subjectspacee'    ), SFH_NO_HASH );
00069                 $parser->setFunctionHook( 'pagename',         array( __CLASS__, 'pagename'         ), SFH_NO_HASH );
00070                 $parser->setFunctionHook( 'pagenamee',        array( __CLASS__, 'pagenamee'        ), SFH_NO_HASH );
00071                 $parser->setFunctionHook( 'fullpagename',     array( __CLASS__, 'fullpagename'     ), SFH_NO_HASH );
00072                 $parser->setFunctionHook( 'fullpagenamee',    array( __CLASS__, 'fullpagenamee'    ), SFH_NO_HASH );
00073                 $parser->setFunctionHook( 'basepagename',     array( __CLASS__, 'basepagename'     ), SFH_NO_HASH );
00074                 $parser->setFunctionHook( 'basepagenamee',    array( __CLASS__, 'basepagenamee'    ), SFH_NO_HASH );
00075                 $parser->setFunctionHook( 'subpagename',      array( __CLASS__, 'subpagename'      ), SFH_NO_HASH );
00076                 $parser->setFunctionHook( 'subpagenamee',     array( __CLASS__, 'subpagenamee'     ), SFH_NO_HASH );
00077                 $parser->setFunctionHook( 'talkpagename',     array( __CLASS__, 'talkpagename'     ), SFH_NO_HASH );
00078                 $parser->setFunctionHook( 'talkpagenamee',    array( __CLASS__, 'talkpagenamee'    ), SFH_NO_HASH );
00079                 $parser->setFunctionHook( 'subjectpagename',  array( __CLASS__, 'subjectpagename'  ), SFH_NO_HASH );
00080                 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__, 'subjectpagenamee' ), SFH_NO_HASH );
00081                 $parser->setFunctionHook( 'tag',              array( __CLASS__, 'tagObj'           ), SFH_OBJECT_ARGS );
00082                 $parser->setFunctionHook( 'formatdate',       array( __CLASS__, 'formatDate'       ) );
00083 
00084                 if ( $wgAllowDisplayTitle ) {
00085                         $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH );
00086                 }
00087                 if ( $wgAllowSlowParserFunctions ) {
00088                         $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__, 'pagesinnamespace' ), SFH_NO_HASH );
00089                 }
00090         }
00091 
00097         static function intFunction( $parser, $part1 = '' /*, ... */ ) {
00098                 if ( strval( $part1 ) !== '' ) {
00099                         $args = array_slice( func_get_args(), 2 );
00100                         $message = wfMessage( $part1, $args )->inLanguage( $parser->getOptions()->getUserLangObj() )->plain();
00101                         return array( $message, 'noparse' => false );
00102                 } else {
00103                         return array( 'found' => false );
00104                 }
00105         }
00106 
00113         static function formatDate( $parser, $date, $defaultPref = null ) {
00114                 $df = DateFormatter::getInstance();
00115 
00116                 $date = trim( $date );
00117 
00118                 $pref = $parser->getOptions()->getDateFormat();
00119 
00120                 // Specify a different default date format other than the the normal default
00121                 // iff the user has 'default' for their setting
00122                 if ( $pref == 'default' && $defaultPref )
00123                         $pref = $defaultPref;
00124 
00125                 $date = $df->reformat( $pref, $date, array( 'match-whole' ) );
00126                 return $date;
00127         }
00128 
00129         static function ns( $parser, $part1 = '' ) {
00130                 global $wgContLang;
00131                 if ( intval( $part1 ) || $part1 == "0" ) {
00132                         $index = intval( $part1 );
00133                 } else {
00134                         $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
00135                 }
00136                 if ( $index !== false ) {
00137                         return $wgContLang->getFormattedNsText( $index );
00138                 } else {
00139                         return array( 'found' => false );
00140                 }
00141         }
00142 
00143         static function nse( $parser, $part1 = '' ) {
00144                 $ret = self::ns( $parser, $part1 );
00145                 if ( is_string( $ret ) ) {
00146                         $ret = wfUrlencode( str_replace( ' ', '_', $ret ) );
00147                 }
00148                 return $ret;
00149         }
00150 
00162         static function urlencode( $parser, $s = '', $arg = null ) {
00163                 static $magicWords = null;
00164                 if ( is_null( $magicWords ) ) {
00165                         $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) );
00166                 }
00167                 switch( $magicWords->matchStartToEnd( $arg ) ) {
00168 
00169                         // Encode as though it's a wiki page, '_' for ' '.
00170                         case 'url_wiki':
00171                                 $func = 'wfUrlencode';
00172                                 $s = str_replace( ' ', '_', $s );
00173                                 break;
00174 
00175                         // Encode for an HTTP Path, '%20' for ' '.
00176                         case 'url_path':
00177                                 $func = 'rawurlencode';
00178                                 break;
00179 
00180                         // Encode for HTTP query, '+' for ' '.
00181                         case 'url_query':
00182                         default:
00183                                 $func = 'urlencode';
00184                 }
00185                 return $parser->markerSkipCallback( $s, $func );
00186         }
00187 
00188         static function lcfirst( $parser, $s = '' ) {
00189                 global $wgContLang;
00190                 return $wgContLang->lcfirst( $s );
00191         }
00192 
00193         static function ucfirst( $parser, $s = '' ) {
00194                 global $wgContLang;
00195                 return $wgContLang->ucfirst( $s );
00196         }
00197 
00203         static function lc( $parser, $s = '' ) {
00204                 global $wgContLang;
00205                 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
00206         }
00207 
00213         static function uc( $parser, $s = '' ) {
00214                 global $wgContLang;
00215                 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
00216         }
00217 
00218         static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
00219         static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
00220         static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
00221         static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
00222         static function canonicalurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getCanonicalURL', $s, $arg ); }
00223         static function canonicalurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeCanonicalURL', $s, $arg ); }
00224 
00225         static function urlFunction( $func, $s = '', $arg = null ) {
00226                 $title = Title::newFromText( $s );
00227                 # Due to order of execution of a lot of bits, the values might be encoded
00228                 # before arriving here; if that's true, then the title can't be created
00229                 # and the variable will fail. If we can't get a decent title from the first
00230                 # attempt, url-decode and try for a second.
00231                 if( is_null( $title ) )
00232                         $title = Title::newFromURL( urldecode( $s ) );
00233                 if( !is_null( $title ) ) {
00234                         # Convert NS_MEDIA -> NS_FILE
00235                         if( $title->getNamespace() == NS_MEDIA ) {
00236                                 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
00237                         }
00238                         if( !is_null( $arg ) ) {
00239                                 $text = $title->$func( $arg );
00240                         } else {
00241                                 $text = $title->$func();
00242                         }
00243                         return $text;
00244                 } else {
00245                         return array( 'found' => false );
00246                 }
00247         }
00248 
00255         static function formatnum( $parser, $num = '', $raw = null) {
00256                 if ( self::isRaw( $raw ) ) {
00257                         $func = array( $parser->getFunctionLang(), 'parseFormattedNumber' );
00258                 } else {
00259                         $func = array( $parser->getFunctionLang(), 'formatNum' );
00260                 }
00261                 return $parser->markerSkipCallback( $num, $func );
00262         }
00263 
00270         static function grammar( $parser, $case = '', $word = '' ) {
00271                 $word = $parser->killMarkers( $word );
00272                 return $parser->getFunctionLang()->convertGrammar( $word, $case );
00273         }
00274 
00280         static function gender( $parser, $username ) {
00281                 wfProfileIn( __METHOD__ );
00282                 $forms = array_slice( func_get_args(), 2 );
00283 
00284                 // Some shortcuts to avoid loading user data unnecessarily
00285                 if ( count( $forms ) === 0 ) {
00286                         return '';
00287                 } elseif ( count( $forms ) === 1 ) {
00288                         return $forms[0];
00289                 }
00290 
00291                 $username = trim( $username );
00292 
00293                 // default
00294                 $gender = User::getDefaultOption( 'gender' );
00295 
00296                 // allow prefix.
00297                 $title = Title::newFromText( $username );
00298 
00299                 if ( $title && $title->getNamespace() == NS_USER ) {
00300                         $username = $title->getText();
00301                 }
00302 
00303                 // check parameter, or use the ParserOptions if in interface message
00304                 $user = User::newFromName( $username );
00305                 if ( $user ) {
00306                         $gender = $user->getOption( 'gender' );
00307                 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) {
00308                         $gender = $parser->getOptions()->getUser()->getOption( 'gender' );
00309                 }
00310                 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
00311                 wfProfileOut( __METHOD__ );
00312                 return $ret;
00313         }
00314 
00320         static function plural( $parser, $text = '' ) {
00321                 $forms = array_slice( func_get_args(), 2 );
00322                 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
00323                 return $parser->getFunctionLang()->convertPlural( $text, $forms );
00324         }
00325 
00334         static function displaytitle( $parser, $text = '' ) {
00335                 global $wgRestrictDisplayTitle;
00336 
00337                 #parse a limited subset of wiki markup (just the single quote items)
00338                 $text = $parser->doQuotes( $text );
00339 
00340                 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
00341                 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
00342                         . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '/', '', $text );
00343 
00344                 #list of disallowed tags for DISPLAYTITLE
00345                 #these will be escaped even though they are allowed in normal wiki text
00346                 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
00347                         'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
00348 
00349                 #only requested titles that normalize to the actual title are allowed through
00350                 #if $wgRestrictDisplayTitle is true (it is by default)
00351                 #mimic the escaping process that occurs in OutputPage::setPageTitle
00352                 $text = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $text, null, array(), array(), $bad ) );
00353                 $title = Title::newFromText( Sanitizer::stripAllTags( $text ) );
00354 
00355                 if( !$wgRestrictDisplayTitle ) {
00356                         $parser->mOutput->setDisplayTitle( $text );
00357                 } else {
00358                         if ( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) ) {
00359                                 $parser->mOutput->setDisplayTitle( $text );
00360                         }
00361                 }
00362 
00363                 return '';
00364         }
00365 
00366         static function isRaw( $param ) {
00367                 static $mwRaw;
00368                 if ( !$mwRaw ) {
00369                         $mwRaw =& MagicWord::get( 'rawsuffix' );
00370                 }
00371                 if ( is_null( $param ) ) {
00372                         return false;
00373                 } else {
00374                         return $mwRaw->match( $param );
00375                 }
00376         }
00377 
00378         static function formatRaw( $num, $raw ) {
00379                 if( self::isRaw( $raw ) ) {
00380                         return $num;
00381                 } else {
00382                         global $wgContLang;
00383                         return $wgContLang->formatNum( $num );
00384                 }
00385         }
00386         static function numberofpages( $parser, $raw = null ) {
00387                 return self::formatRaw( SiteStats::pages(), $raw );
00388         }
00389         static function numberofusers( $parser, $raw = null ) {
00390                 return self::formatRaw( SiteStats::users(), $raw );
00391         }
00392         static function numberofactiveusers( $parser, $raw = null ) {
00393                 return self::formatRaw( SiteStats::activeUsers(), $raw );
00394         }
00395         static function numberofarticles( $parser, $raw = null ) {
00396                 return self::formatRaw( SiteStats::articles(), $raw );
00397         }
00398         static function numberoffiles( $parser, $raw = null ) {
00399                 return self::formatRaw( SiteStats::images(), $raw );
00400         }
00401         static function numberofadmins( $parser, $raw = null ) {
00402                 return self::formatRaw( SiteStats::numberingroup('sysop'), $raw );
00403         }
00404         static function numberofedits( $parser, $raw = null ) {
00405                 return self::formatRaw( SiteStats::edits(), $raw );
00406         }
00407         static function numberofviews( $parser, $raw = null ) {
00408                 return self::formatRaw( SiteStats::views(), $raw );
00409         }
00410         static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
00411                 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
00412         }
00413         static function numberingroup( $parser, $name = '', $raw = null) {
00414                 return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
00415         }
00416 
00417 
00424         static function mwnamespace( $parser, $title = null ) {
00425                 $t = Title::newFromText( $title );
00426                 if ( is_null( $t ) )
00427                         return '';
00428                 return str_replace( '_', ' ', $t->getNsText() );
00429         }
00430         static function namespacee( $parser, $title = null ) {
00431                 $t = Title::newFromText( $title );
00432                 if ( is_null( $t ) )
00433                         return '';
00434                 return wfUrlencode( $t->getNsText() );
00435         }
00436         static function talkspace( $parser, $title = null ) {
00437                 $t = Title::newFromText( $title );
00438                 if ( is_null( $t ) || !$t->canTalk() )
00439                         return '';
00440                 return str_replace( '_', ' ', $t->getTalkNsText() );
00441         }
00442         static function talkspacee( $parser, $title = null ) {
00443                 $t = Title::newFromText( $title );
00444                 if ( is_null( $t ) || !$t->canTalk() )
00445                         return '';
00446                 return wfUrlencode( $t->getTalkNsText() );
00447         }
00448         static function subjectspace( $parser, $title = null ) {
00449                 $t = Title::newFromText( $title );
00450                 if ( is_null( $t ) )
00451                         return '';
00452                 return str_replace( '_', ' ', $t->getSubjectNsText() );
00453         }
00454         static function subjectspacee( $parser, $title = null ) {
00455                 $t = Title::newFromText( $title );
00456                 if ( is_null( $t ) )
00457                         return '';
00458                 return wfUrlencode( $t->getSubjectNsText() );
00459         }
00460 
00465         static function pagename( $parser, $title = null ) {
00466                 $t = Title::newFromText( $title );
00467                 if ( is_null( $t ) )
00468                         return '';
00469                 return wfEscapeWikiText( $t->getText() );
00470         }
00471         static function pagenamee( $parser, $title = null ) {
00472                 $t = Title::newFromText( $title );
00473                 if ( is_null( $t ) )
00474                         return '';
00475                 return wfEscapeWikiText( $t->getPartialURL() );
00476         }
00477         static function fullpagename( $parser, $title = null ) {
00478                 $t = Title::newFromText( $title );
00479                 if ( is_null( $t ) || !$t->canTalk() )
00480                         return '';
00481                 return wfEscapeWikiText( $t->getPrefixedText() );
00482         }
00483         static function fullpagenamee( $parser, $title = null ) {
00484                 $t = Title::newFromText( $title );
00485                 if ( is_null( $t ) || !$t->canTalk() )
00486                         return '';
00487                 return wfEscapeWikiText( $t->getPrefixedURL() );
00488         }
00489         static function subpagename( $parser, $title = null ) {
00490                 $t = Title::newFromText( $title );
00491                 if ( is_null( $t ) )
00492                         return '';
00493                 return wfEscapeWikiText( $t->getSubpageText() );
00494         }
00495         static function subpagenamee( $parser, $title = null ) {
00496                 $t = Title::newFromText( $title );
00497                 if ( is_null( $t ) )
00498                         return '';
00499                 return wfEscapeWikiText( $t->getSubpageUrlForm() );
00500         }
00501         static function basepagename( $parser, $title = null ) {
00502                 $t = Title::newFromText( $title );
00503                 if ( is_null( $t ) )
00504                         return '';
00505                 return wfEscapeWikiText( $t->getBaseText() );
00506         }
00507         static function basepagenamee( $parser, $title = null ) {
00508                 $t = Title::newFromText( $title );
00509                 if ( is_null( $t ) )
00510                         return '';
00511                 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) );
00512         }
00513         static function talkpagename( $parser, $title = null ) {
00514                 $t = Title::newFromText( $title );
00515                 if ( is_null( $t ) || !$t->canTalk() )
00516                         return '';
00517                 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
00518         }
00519         static function talkpagenamee( $parser, $title = null ) {
00520                 $t = Title::newFromText( $title );
00521                 if ( is_null( $t ) || !$t->canTalk() )
00522                         return '';
00523                 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedUrl() );
00524         }
00525         static function subjectpagename( $parser, $title = null ) {
00526                 $t = Title::newFromText( $title );
00527                 if ( is_null( $t ) )
00528                         return '';
00529                 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
00530         }
00531         static function subjectpagenamee( $parser, $title = null ) {
00532                 $t = Title::newFromText( $title );
00533                 if ( is_null( $t ) )
00534                         return '';
00535                 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedUrl() );
00536         }
00537 
00543         static function pagesincategory( $parser, $name = '', $raw = null ) {
00544                 static $cache = array();
00545                 $category = Category::newFromName( $name );
00546 
00547                 if( !is_object( $category ) ) {
00548                         $cache[$name] = 0;
00549                         return self::formatRaw( 0, $raw );
00550                 }
00551 
00552                 # Normalize name for cache
00553                 $name = $category->getName();
00554 
00555                 $count = 0;
00556                 if( isset( $cache[$name] ) ) {
00557                         $count = $cache[$name];
00558                 } elseif( $parser->incrementExpensiveFunctionCount() ) {
00559                         $count = $cache[$name] = (int)$category->getPageCount();
00560                 }
00561                 return self::formatRaw( $count, $raw );
00562         }
00563 
00580         static function pagesize( $parser, $page = '', $raw = null ) {
00581                 static $cache = array();
00582                 $title = Title::newFromText( $page );
00583 
00584                 if( !is_object( $title ) ) {
00585                         $cache[$page] = 0;
00586                         return self::formatRaw( 0, $raw );
00587                 }
00588 
00589                 # Normalize name for cache
00590                 $page = $title->getPrefixedText();
00591 
00592                 $length = 0;
00593                 if( isset( $cache[$page] ) ) {
00594                         $length = $cache[$page];
00595                 } elseif( $parser->incrementExpensiveFunctionCount() ) {
00596                         $rev = Revision::newFromTitle( $title );
00597                         $id = $rev ? $rev->getPage() : 0;
00598                         $length = $cache[$page] = $rev ? $rev->getSize() : 0;
00599 
00600                         // Register dependency in templatelinks
00601                         $parser->mOutput->addTemplate( $title, $id, $rev ? $rev->getId() : 0 );
00602                 }
00603                 return self::formatRaw( $length, $raw );
00604         }
00605 
00609         static function protectionlevel( $parser, $type = '' ) {
00610                 $restrictions = $parser->mTitle->getRestrictions( strtolower( $type ) );
00611                 # Title::getRestrictions returns an array, its possible it may have
00612                 # multiple values in the future
00613                 return implode( $restrictions, ',' );
00614         }
00615 
00623         static function language( $parser, $code = '', $language = '' ) {
00624                 global $wgContLang;
00625                 $code = strtolower( $code );
00626                 $language = strtolower( $language );
00627 
00628                 if ( $language !== '' ) {
00629                         $names = Language::getTranslatedLanguageNames( $language );
00630                         return isset( $names[$code] ) ? $names[$code] : wfBCP47( $code );
00631                 }
00632 
00633                 $lang = $wgContLang->getLanguageName( $code );
00634                 return $lang !== '' ? $lang : wfBCP47( $code );
00635         }
00636 
00640         static function pad( $parser, $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) {
00641                 $padding = $parser->killMarkers( $padding );
00642                 $lengthOfPadding = mb_strlen( $padding );
00643                 if ( $lengthOfPadding == 0 ) return $string;
00644 
00645                 # The remaining length to add counts down to 0 as padding is added
00646                 $length = min( $length, 500 ) - mb_strlen( $string );
00647                 # $finalPadding is just $padding repeated enough times so that
00648                 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
00649                 $finalPadding = '';
00650                 while ( $length > 0 ) {
00651                         # If $length < $lengthofPadding, truncate $padding so we get the
00652                         # exact length desired.
00653                         $finalPadding .= mb_substr( $padding, 0, $length );
00654                         $length -= $lengthOfPadding;
00655                 }
00656 
00657                 if ( $direction == STR_PAD_LEFT ) {
00658                         return $finalPadding . $string;
00659                 } else {
00660                         return $string . $finalPadding;
00661                 }
00662         }
00663 
00664         static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
00665                 return self::pad( $parser, $string, $length, $padding, STR_PAD_LEFT );
00666         }
00667 
00668         static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
00669                 return self::pad( $parser, $string, $length, $padding );
00670         }
00671 
00677         static function anchorencode( $parser, $text ) {
00678                 $text = $parser->killMarkers( $text );
00679                 return substr( $parser->guessSectionNameFromWikiText( $text ), 1);
00680         }
00681 
00682         static function special( $parser, $text ) {
00683                 list( $page, $subpage ) = SpecialPageFactory::resolveAlias( $text );
00684                 if ( $page ) {
00685                         $title = SpecialPage::getTitleFor( $page, $subpage );
00686                         return $title;
00687                 } else {
00688                         return wfMsgForContent( 'nosuchspecialpage' );
00689                 }
00690         }
00691 
00700         public static function defaultsort( $parser, $text, $uarg = '' ) {
00701                 static $magicWords = null;
00702                 if ( is_null( $magicWords ) ) {
00703                         $magicWords = new MagicWordArray( array( 'defaultsort_noerror', 'defaultsort_noreplace' ) );
00704                 }
00705                 $arg = $magicWords->matchStartToEnd( $uarg );
00706 
00707                 $text = trim( $text );
00708                 if( strlen( $text ) == 0 )
00709                         return '';
00710                 $old = $parser->getCustomDefaultSort();
00711                 if ( $old === false || $arg !== 'defaultsort_noreplace' ) {
00712                         $parser->setDefaultSort( $text );
00713                 }
00714 
00715                 if( $old === false || $old == $text || $arg ) {
00716                         return '';
00717                 } else {
00718                         return( '<span class="error">' .
00719                                 wfMsgForContent( 'duplicate-defaultsort',
00720                                                  htmlspecialchars( $old ),
00721                                                  htmlspecialchars( $text ) ) .
00722                                 '</span>' );
00723                 }
00724         }
00725 
00726         // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}}
00727         public static function filepath( $parser, $name='', $argA='', $argB='' ) {
00728                 $file = wfFindFile( $name );
00729                 $size = '';
00730                 $argA_int = intval( $argA );
00731                 $argB_int = intval( $argB );
00732 
00733                 if ( $argB_int > 0 ) {
00734                         // {{filepath: | option | size }}
00735                         $size = $argB_int;
00736                         $option = $argA;
00737 
00738                 } elseif ( $argA_int > 0 ) {
00739                         // {{filepath: | size [|option] }}
00740                         $size = $argA_int;
00741                         $option = $argB;
00742 
00743                 } else {
00744                         // {{filepath: [|option] }}
00745                         $option = $argA;
00746                 }
00747 
00748                 if ( $file ) {
00749                         $url = $file->getFullUrl();
00750 
00751                         // If a size is requested...
00752                         if ( is_integer( $size ) ) {
00753                                 $mto = $file->transform( array( 'width' => $size ) );
00754                                 // ... and we can
00755                                 if ( $mto && !$mto->isError() ) {
00756                                         // ... change the URL to point to a thumbnail.
00757                                         $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE );
00758                                 }
00759                         }
00760                         if ( $option == 'nowiki' ) {
00761                                 return array( $url, 'nowiki' => true );
00762                         }
00763                         return $url;
00764                 } else {
00765                         return '';
00766                 }
00767         }
00768 
00772         public static function tagObj( $parser, $frame, $args ) {
00773                 if ( !count( $args ) ) {
00774                         return '';
00775                 }
00776                 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
00777 
00778                 if ( count( $args ) ) {
00779                         $inner = $frame->expand( array_shift( $args ) );
00780                 } else {
00781                         $inner = null;
00782                 }
00783 
00784                 $stripList = $parser->getStripList();
00785                 if ( !in_array( $tagName, $stripList ) ) {
00786                         return '<span class="error">' .
00787                                 wfMsgForContent( 'unknown_extension_tag', $tagName ) .
00788                                 '</span>';
00789                 }
00790 
00791                 $attributes = array();
00792                 foreach ( $args as $arg ) {
00793                         $bits = $arg->splitArg();
00794                         if ( strval( $bits['index'] ) === '' ) {
00795                                 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
00796                                 $value = trim( $frame->expand( $bits['value'] ) );
00797                                 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
00798                                         $value = isset( $m[1] ) ? $m[1] : '';
00799                                 }
00800                                 $attributes[$name] = $value;
00801                         }
00802                 }
00803 
00804                 $params = array(
00805                         'name' => $tagName,
00806                         'inner' => $inner,
00807                         'attributes' => $attributes,
00808                         'close' => "</$tagName>",
00809                 );
00810                 return $parser->extensionSubstitution( $params, $frame );
00811         }
00812 }