MediaWiki
REL1_22
|
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( 'rootpagename', array( __CLASS__, 'rootpagename' ), SFH_NO_HASH ); 00092 $parser->setFunctionHook( 'rootpagenamee', array( __CLASS__, 'rootpagenamee' ), SFH_NO_HASH ); 00093 $parser->setFunctionHook( 'basepagename', array( __CLASS__, 'basepagename' ), SFH_NO_HASH ); 00094 $parser->setFunctionHook( 'basepagenamee', array( __CLASS__, 'basepagenamee' ), SFH_NO_HASH ); 00095 $parser->setFunctionHook( 'subpagename', array( __CLASS__, 'subpagename' ), SFH_NO_HASH ); 00096 $parser->setFunctionHook( 'subpagenamee', array( __CLASS__, 'subpagenamee' ), SFH_NO_HASH ); 00097 $parser->setFunctionHook( 'talkpagename', array( __CLASS__, 'talkpagename' ), SFH_NO_HASH ); 00098 $parser->setFunctionHook( 'talkpagenamee', array( __CLASS__, 'talkpagenamee' ), SFH_NO_HASH ); 00099 $parser->setFunctionHook( 'subjectpagename', array( __CLASS__, 'subjectpagename' ), SFH_NO_HASH ); 00100 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__, 'subjectpagenamee' ), SFH_NO_HASH ); 00101 $parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS ); 00102 $parser->setFunctionHook( 'formatdate', array( __CLASS__, 'formatDate' ) ); 00103 00104 if ( $wgAllowDisplayTitle ) { 00105 $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH ); 00106 } 00107 if ( $wgAllowSlowParserFunctions ) { 00108 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__, 'pagesinnamespace' ), SFH_NO_HASH ); 00109 } 00110 } 00111 00117 static function intFunction( $parser, $part1 = '' /*, ... */ ) { 00118 if ( strval( $part1 ) !== '' ) { 00119 $args = array_slice( func_get_args(), 2 ); 00120 $message = wfMessage( $part1, $args )->inLanguage( $parser->getOptions()->getUserLangObj() )->plain(); 00121 return array( $message, 'noparse' => false ); 00122 } else { 00123 return array( 'found' => false ); 00124 } 00125 } 00126 00133 static function formatDate( $parser, $date, $defaultPref = null ) { 00134 $lang = $parser->getFunctionLang(); 00135 $df = DateFormatter::getInstance( $lang ); 00136 00137 $date = trim( $date ); 00138 00139 $pref = $parser->getOptions()->getDateFormat(); 00140 00141 // Specify a different default date format other than the the normal default 00142 // if the user has 'default' for their setting 00143 if ( $pref == 'default' && $defaultPref ) { 00144 $pref = $defaultPref; 00145 } 00146 00147 $date = $df->reformat( $pref, $date, array( 'match-whole' ) ); 00148 return $date; 00149 } 00150 00151 static function ns( $parser, $part1 = '' ) { 00152 global $wgContLang; 00153 if ( intval( $part1 ) || $part1 == "0" ) { 00154 $index = intval( $part1 ); 00155 } else { 00156 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) ); 00157 } 00158 if ( $index !== false ) { 00159 return $wgContLang->getFormattedNsText( $index ); 00160 } else { 00161 return array( 'found' => false ); 00162 } 00163 } 00164 00165 static function nse( $parser, $part1 = '' ) { 00166 $ret = self::ns( $parser, $part1 ); 00167 if ( is_string( $ret ) ) { 00168 $ret = wfUrlencode( str_replace( ' ', '_', $ret ) ); 00169 } 00170 return $ret; 00171 } 00172 00185 static function urlencode( $parser, $s = '', $arg = null ) { 00186 static $magicWords = null; 00187 if ( is_null( $magicWords ) ) { 00188 $magicWords = new MagicWordArray( array( 'url_path', 'url_query', 'url_wiki' ) ); 00189 } 00190 switch ( $magicWords->matchStartToEnd( $arg ) ) { 00191 00192 // Encode as though it's a wiki page, '_' for ' '. 00193 case 'url_wiki': 00194 $func = 'wfUrlencode'; 00195 $s = str_replace( ' ', '_', $s ); 00196 break; 00197 00198 // Encode for an HTTP Path, '%20' for ' '. 00199 case 'url_path': 00200 $func = 'rawurlencode'; 00201 break; 00202 00203 // Encode for HTTP query, '+' for ' '. 00204 case 'url_query': 00205 default: 00206 $func = 'urlencode'; 00207 } 00208 return $parser->markerSkipCallback( $s, $func ); 00209 } 00210 00211 static function lcfirst( $parser, $s = '' ) { 00212 global $wgContLang; 00213 return $wgContLang->lcfirst( $s ); 00214 } 00215 00216 static function ucfirst( $parser, $s = '' ) { 00217 global $wgContLang; 00218 return $wgContLang->ucfirst( $s ); 00219 } 00220 00226 static function lc( $parser, $s = '' ) { 00227 global $wgContLang; 00228 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) ); 00229 } 00230 00236 static function uc( $parser, $s = '' ) { 00237 global $wgContLang; 00238 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) ); 00239 } 00240 00241 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); } 00242 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); } 00243 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); } 00244 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); } 00245 static function canonicalurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getCanonicalURL', $s, $arg ); } 00246 static function canonicalurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeCanonicalURL', $s, $arg ); } 00247 00248 static function urlFunction( $func, $s = '', $arg = null ) { 00249 $title = Title::newFromText( $s ); 00250 # Due to order of execution of a lot of bits, the values might be encoded 00251 # before arriving here; if that's true, then the title can't be created 00252 # and the variable will fail. If we can't get a decent title from the first 00253 # attempt, url-decode and try for a second. 00254 if ( is_null( $title ) ) { 00255 $title = Title::newFromURL( urldecode( $s ) ); 00256 } 00257 if ( !is_null( $title ) ) { 00258 # Convert NS_MEDIA -> NS_FILE 00259 if ( $title->getNamespace() == NS_MEDIA ) { 00260 $title = Title::makeTitle( NS_FILE, $title->getDBkey() ); 00261 } 00262 if ( !is_null( $arg ) ) { 00263 $text = $title->$func( $arg ); 00264 } else { 00265 $text = $title->$func(); 00266 } 00267 return $text; 00268 } else { 00269 return array( 'found' => false ); 00270 } 00271 } 00272 00279 static function formatnum( $parser, $num = '', $arg = null ) { 00280 if ( self::matchAgainstMagicword( 'rawsuffix', $arg ) ) { 00281 $func = array( $parser->getFunctionLang(), 'parseFormattedNumber' ); 00282 } elseif ( self::matchAgainstMagicword( 'nocommafysuffix', $arg ) ) { 00283 $func = array( $parser->getFunctionLang(), 'formatNumNoSeparators' ); 00284 } else { 00285 $func = array( $parser->getFunctionLang(), 'formatNum' ); 00286 } 00287 return $parser->markerSkipCallback( $num, $func ); 00288 } 00289 00296 static function grammar( $parser, $case = '', $word = '' ) { 00297 $word = $parser->killMarkers( $word ); 00298 return $parser->getFunctionLang()->convertGrammar( $word, $case ); 00299 } 00300 00306 static function gender( $parser, $username ) { 00307 wfProfileIn( __METHOD__ ); 00308 $forms = array_slice( func_get_args(), 2 ); 00309 00310 // Some shortcuts to avoid loading user data unnecessarily 00311 if ( count( $forms ) === 0 ) { 00312 wfProfileOut( __METHOD__ ); 00313 return ''; 00314 } elseif ( count( $forms ) === 1 ) { 00315 wfProfileOut( __METHOD__ ); 00316 return $forms[0]; 00317 } 00318 00319 $username = trim( $username ); 00320 00321 // default 00322 $gender = User::getDefaultOption( 'gender' ); 00323 00324 // allow prefix. 00325 $title = Title::newFromText( $username ); 00326 00327 if ( $title && $title->getNamespace() == NS_USER ) { 00328 $username = $title->getText(); 00329 } 00330 00331 // check parameter, or use the ParserOptions if in interface message 00332 $user = User::newFromName( $username ); 00333 if ( $user ) { 00334 $gender = GenderCache::singleton()->getGenderOf( $user, __METHOD__ ); 00335 } elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) { 00336 $gender = GenderCache::singleton()->getGenderOf( $parser->getOptions()->getUser(), __METHOD__ ); 00337 } 00338 $ret = $parser->getFunctionLang()->gender( $gender, $forms ); 00339 wfProfileOut( __METHOD__ ); 00340 return $ret; 00341 } 00342 00348 static function plural( $parser, $text = '' ) { 00349 $forms = array_slice( func_get_args(), 2 ); 00350 $text = $parser->getFunctionLang()->parseFormattedNumber( $text ); 00351 settype( $text, ctype_digit( $text ) ? 'int' : 'float' ); 00352 return $parser->getFunctionLang()->convertPlural( $text, $forms ); 00353 } 00354 00363 static function displaytitle( $parser, $text = '' ) { 00364 global $wgRestrictDisplayTitle; 00365 00366 // parse a limited subset of wiki markup (just the single quote items) 00367 $text = $parser->doQuotes( $text ); 00368 00369 // remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever 00370 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?' 00371 . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '/', '', $text ); 00372 00373 // list of disallowed tags for DISPLAYTITLE 00374 // these will be escaped even though they are allowed in normal wiki text 00375 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr', 00376 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' ); 00377 00378 // disallow some styles that could be used to bypass $wgRestrictDisplayTitle 00379 if ( $wgRestrictDisplayTitle ) { 00380 $htmlTagsCallback = function ( &$params ) { 00381 $decoded = Sanitizer::decodeTagAttributes( $params ); 00382 00383 if ( isset( $decoded['style'] ) ) { 00384 // this is called later anyway, but we need it right now for the regexes below to be safe 00385 // calling it twice doesn't hurt 00386 $decoded['style'] = Sanitizer::checkCss( $decoded['style'] ); 00387 00388 if ( preg_match( '/(display|user-select|visibility)\s*:/i', $decoded['style'] ) ) { 00389 $decoded['style'] = '/* attempt to bypass $wgRestrictDisplayTitle */'; 00390 } 00391 } 00392 00393 $params = Sanitizer::safeEncodeTagAttributes( $decoded ); 00394 }; 00395 } else { 00396 $htmlTagsCallback = null; 00397 } 00398 00399 // only requested titles that normalize to the actual title are allowed through 00400 // if $wgRestrictDisplayTitle is true (it is by default) 00401 // mimic the escaping process that occurs in OutputPage::setPageTitle 00402 $text = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $text, $htmlTagsCallback, array(), array(), $bad ) ); 00403 $title = Title::newFromText( Sanitizer::stripAllTags( $text ) ); 00404 00405 if ( !$wgRestrictDisplayTitle ) { 00406 $parser->mOutput->setDisplayTitle( $text ); 00407 } elseif ( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) ) { 00408 $parser->mOutput->setDisplayTitle( $text ); 00409 } 00410 00411 return ''; 00412 } 00413 00421 static private function matchAgainstMagicword( $magicword, $value ) { 00422 if ( strval( $value ) === '' ) { 00423 return false; 00424 } 00425 $mwObject = MagicWord::get( $magicword ); 00426 return $mwObject->match( $value ); 00427 } 00428 00429 static function formatRaw( $num, $raw ) { 00430 if ( self::matchAgainstMagicword( 'rawsuffix', $raw ) ) { 00431 return $num; 00432 } else { 00433 global $wgContLang; 00434 return $wgContLang->formatNum( $num ); 00435 } 00436 } 00437 static function numberofpages( $parser, $raw = null ) { 00438 return self::formatRaw( SiteStats::pages(), $raw ); 00439 } 00440 static function numberofusers( $parser, $raw = null ) { 00441 return self::formatRaw( SiteStats::users(), $raw ); 00442 } 00443 static function numberofactiveusers( $parser, $raw = null ) { 00444 return self::formatRaw( SiteStats::activeUsers(), $raw ); 00445 } 00446 static function numberofarticles( $parser, $raw = null ) { 00447 return self::formatRaw( SiteStats::articles(), $raw ); 00448 } 00449 static function numberoffiles( $parser, $raw = null ) { 00450 return self::formatRaw( SiteStats::images(), $raw ); 00451 } 00452 static function numberofadmins( $parser, $raw = null ) { 00453 return self::formatRaw( SiteStats::numberingroup( 'sysop' ), $raw ); 00454 } 00455 static function numberofedits( $parser, $raw = null ) { 00456 return self::formatRaw( SiteStats::edits(), $raw ); 00457 } 00458 static function numberofviews( $parser, $raw = null ) { 00459 global $wgDisableCounters; 00460 return !$wgDisableCounters ? self::formatRaw( SiteStats::views(), $raw ) : ''; 00461 } 00462 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) { 00463 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw ); 00464 } 00465 static function numberingroup( $parser, $name = '', $raw = null ) { 00466 return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw ); 00467 } 00468 00476 static function mwnamespace( $parser, $title = null ) { 00477 $t = Title::newFromText( $title ); 00478 if ( is_null( $t ) ) { 00479 return ''; 00480 } 00481 return str_replace( '_', ' ', $t->getNsText() ); 00482 } 00483 static function namespacee( $parser, $title = null ) { 00484 $t = Title::newFromText( $title ); 00485 if ( is_null( $t ) ) { 00486 return ''; 00487 } 00488 return wfUrlencode( $t->getNsText() ); 00489 } 00490 static function namespacenumber( $parser, $title = null ) { 00491 $t = Title::newFromText( $title ); 00492 if ( is_null( $t ) ) { 00493 return ''; 00494 } 00495 return $t->getNamespace(); 00496 } 00497 static function talkspace( $parser, $title = null ) { 00498 $t = Title::newFromText( $title ); 00499 if ( is_null( $t ) || !$t->canTalk() ) { 00500 return ''; 00501 } 00502 return str_replace( '_', ' ', $t->getTalkNsText() ); 00503 } 00504 static function talkspacee( $parser, $title = null ) { 00505 $t = Title::newFromText( $title ); 00506 if ( is_null( $t ) || !$t->canTalk() ) { 00507 return ''; 00508 } 00509 return wfUrlencode( $t->getTalkNsText() ); 00510 } 00511 static function subjectspace( $parser, $title = null ) { 00512 $t = Title::newFromText( $title ); 00513 if ( is_null( $t ) ) { 00514 return ''; 00515 } 00516 return str_replace( '_', ' ', $t->getSubjectNsText() ); 00517 } 00518 static function subjectspacee( $parser, $title = null ) { 00519 $t = Title::newFromText( $title ); 00520 if ( is_null( $t ) ) { 00521 return ''; 00522 } 00523 return wfUrlencode( $t->getSubjectNsText() ); 00524 } 00525 00531 static function pagename( $parser, $title = null ) { 00532 $t = Title::newFromText( $title ); 00533 if ( is_null( $t ) ) { 00534 return ''; 00535 } 00536 return wfEscapeWikiText( $t->getText() ); 00537 } 00538 static function pagenamee( $parser, $title = null ) { 00539 $t = Title::newFromText( $title ); 00540 if ( is_null( $t ) ) { 00541 return ''; 00542 } 00543 return wfEscapeWikiText( $t->getPartialURL() ); 00544 } 00545 static function fullpagename( $parser, $title = null ) { 00546 $t = Title::newFromText( $title ); 00547 if ( is_null( $t ) || !$t->canTalk() ) { 00548 return ''; 00549 } 00550 return wfEscapeWikiText( $t->getPrefixedText() ); 00551 } 00552 static function fullpagenamee( $parser, $title = null ) { 00553 $t = Title::newFromText( $title ); 00554 if ( is_null( $t ) || !$t->canTalk() ) { 00555 return ''; 00556 } 00557 return wfEscapeWikiText( $t->getPrefixedURL() ); 00558 } 00559 static function subpagename( $parser, $title = null ) { 00560 $t = Title::newFromText( $title ); 00561 if ( is_null( $t ) ) { 00562 return ''; 00563 } 00564 return wfEscapeWikiText( $t->getSubpageText() ); 00565 } 00566 static function subpagenamee( $parser, $title = null ) { 00567 $t = Title::newFromText( $title ); 00568 if ( is_null( $t ) ) { 00569 return ''; 00570 } 00571 return wfEscapeWikiText( $t->getSubpageUrlForm() ); 00572 } 00573 static function rootpagename( $parser, $title = null ) { 00574 $t = Title::newFromText( $title ); 00575 if ( is_null( $t ) ) { 00576 return ''; 00577 } 00578 return wfEscapeWikiText( $t->getRootText() ); 00579 } 00580 static function rootpagenamee( $parser, $title = null ) { 00581 $t = Title::newFromText( $title ); 00582 if ( is_null( $t ) ) { 00583 return ''; 00584 } 00585 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getRootText() ) ) ); 00586 } 00587 static function basepagename( $parser, $title = null ) { 00588 $t = Title::newFromText( $title ); 00589 if ( is_null( $t ) ) { 00590 return ''; 00591 } 00592 return wfEscapeWikiText( $t->getBaseText() ); 00593 } 00594 static function basepagenamee( $parser, $title = null ) { 00595 $t = Title::newFromText( $title ); 00596 if ( is_null( $t ) ) { 00597 return ''; 00598 } 00599 return wfEscapeWikiText( wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) ) ); 00600 } 00601 static function talkpagename( $parser, $title = null ) { 00602 $t = Title::newFromText( $title ); 00603 if ( is_null( $t ) || !$t->canTalk() ) { 00604 return ''; 00605 } 00606 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() ); 00607 } 00608 static function talkpagenamee( $parser, $title = null ) { 00609 $t = Title::newFromText( $title ); 00610 if ( is_null( $t ) || !$t->canTalk() ) { 00611 return ''; 00612 } 00613 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedURL() ); 00614 } 00615 static function subjectpagename( $parser, $title = null ) { 00616 $t = Title::newFromText( $title ); 00617 if ( is_null( $t ) ) { 00618 return ''; 00619 } 00620 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() ); 00621 } 00622 static function subjectpagenamee( $parser, $title = null ) { 00623 $t = Title::newFromText( $title ); 00624 if ( is_null( $t ) ) { 00625 return ''; 00626 } 00627 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedURL() ); 00628 } 00629 00636 static function pagesincategory( $parser, $name = '', $arg1 = null, $arg2 = null ) { 00637 global $wgContLang; 00638 static $magicWords = null; 00639 if ( is_null( $magicWords ) ) { 00640 $magicWords = new MagicWordArray( array( 00641 'pagesincategory_all', 00642 'pagesincategory_pages', 00643 'pagesincategory_subcats', 00644 'pagesincategory_files' 00645 ) ); 00646 } 00647 static $cache = array(); 00648 00649 // split the given option to its variable 00650 if ( self::matchAgainstMagicword( 'rawsuffix', $arg1 ) ) { 00651 //{{pagesincategory:|raw[|type]}} 00652 $raw = $arg1; 00653 $type = $magicWords->matchStartToEnd( $arg2 ); 00654 } else { 00655 //{{pagesincategory:[|type[|raw]]}} 00656 $type = $magicWords->matchStartToEnd( $arg1 ); 00657 $raw = $arg2; 00658 } 00659 if ( !$type ) { //backward compatibility 00660 $type = 'pagesincategory_all'; 00661 } 00662 00663 $title = Title::makeTitleSafe( NS_CATEGORY, $name ); 00664 if ( !$title ) { # invalid title 00665 return self::formatRaw( 0, $raw ); 00666 } 00667 $wgContLang->findVariantLink( $name, $title, true ); 00668 00669 // Normalize name for cache 00670 $name = $title->getDBkey(); 00671 00672 if ( !isset( $cache[$name] ) ) { 00673 $category = Category::newFromTitle( $title ); 00674 00675 $allCount = $subcatCount = $fileCount = $pagesCount = 0; 00676 if ( $parser->incrementExpensiveFunctionCount() ) { 00677 // $allCount is the total number of cat members, 00678 // not the count of how many members are normal pages. 00679 $allCount = (int)$category->getPageCount(); 00680 $subcatCount = (int)$category->getSubcatCount(); 00681 $fileCount = (int)$category->getFileCount(); 00682 $pagesCount = $allCount - $subcatCount - $fileCount; 00683 } 00684 $cache[$name]['pagesincategory_all'] = $allCount; 00685 $cache[$name]['pagesincategory_pages'] = $pagesCount; 00686 $cache[$name]['pagesincategory_subcats'] = $subcatCount; 00687 $cache[$name]['pagesincategory_files'] = $fileCount; 00688 } 00689 00690 $count = $cache[$name][$type]; 00691 return self::formatRaw( $count, $raw ); 00692 } 00693 00709 static function pagesize( $parser, $page = '', $raw = null ) { 00710 static $cache = array(); 00711 $title = Title::newFromText( $page ); 00712 00713 if ( !is_object( $title ) ) { 00714 $cache[$page] = 0; 00715 return self::formatRaw( 0, $raw ); 00716 } 00717 00718 # Normalize name for cache 00719 $page = $title->getPrefixedText(); 00720 00721 $length = 0; 00722 if ( isset( $cache[$page] ) ) { 00723 $length = $cache[$page]; 00724 } elseif ( $parser->incrementExpensiveFunctionCount() ) { 00725 $rev = Revision::newFromTitle( $title, false, Revision::READ_NORMAL ); 00726 $pageID = $rev ? $rev->getPage() : 0; 00727 $revID = $rev ? $rev->getId() : 0; 00728 $length = $cache[$page] = $rev ? $rev->getSize() : 0; 00729 00730 // Register dependency in templatelinks 00731 $parser->mOutput->addTemplate( $title, $pageID, $revID ); 00732 } 00733 return self::formatRaw( $length, $raw ); 00734 } 00735 00745 static function protectionlevel( $parser, $type = '', $title = '' ) { 00746 $titleObject = Title::newFromText( $title ); 00747 if ( !( $titleObject instanceof Title ) ) { 00748 $titleObject = $parser->mTitle; 00749 } 00750 $restrictions = $titleObject->getRestrictions( strtolower( $type ) ); 00751 # Title::getRestrictions returns an array, its possible it may have 00752 # multiple values in the future 00753 return implode( $restrictions, ',' ); 00754 } 00755 00763 static function language( $parser, $code = '', $inLanguage = '' ) { 00764 $code = strtolower( $code ); 00765 $inLanguage = strtolower( $inLanguage ); 00766 $lang = Language::fetchLanguageName( $code, $inLanguage ); 00767 return $lang !== '' ? $lang : wfBCP47( $code ); 00768 } 00769 00774 static function pad( $parser, $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) { 00775 $padding = $parser->killMarkers( $padding ); 00776 $lengthOfPadding = mb_strlen( $padding ); 00777 if ( $lengthOfPadding == 0 ) { 00778 return $string; 00779 } 00780 00781 # The remaining length to add counts down to 0 as padding is added 00782 $length = min( $length, 500 ) - mb_strlen( $string ); 00783 # $finalPadding is just $padding repeated enough times so that 00784 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length 00785 $finalPadding = ''; 00786 while ( $length > 0 ) { 00787 # If $length < $lengthofPadding, truncate $padding so we get the 00788 # exact length desired. 00789 $finalPadding .= mb_substr( $padding, 0, $length ); 00790 $length -= $lengthOfPadding; 00791 } 00792 00793 if ( $direction == STR_PAD_LEFT ) { 00794 return $finalPadding . $string; 00795 } else { 00796 return $string . $finalPadding; 00797 } 00798 } 00799 00800 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) { 00801 return self::pad( $parser, $string, $length, $padding, STR_PAD_LEFT ); 00802 } 00803 00804 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) { 00805 return self::pad( $parser, $string, $length, $padding ); 00806 } 00807 00813 static function anchorencode( $parser, $text ) { 00814 $text = $parser->killMarkers( $text ); 00815 return (string)substr( $parser->guessSectionNameFromWikiText( $text ), 1 ); 00816 } 00817 00818 static function special( $parser, $text ) { 00819 list( $page, $subpage ) = SpecialPageFactory::resolveAlias( $text ); 00820 if ( $page ) { 00821 $title = SpecialPage::getTitleFor( $page, $subpage ); 00822 return $title->getPrefixedText(); 00823 } else { 00824 // unknown special page, just use the given text as its title, if at all possible 00825 $title = Title::makeTitleSafe( NS_SPECIAL, $text ); 00826 return $title ? $title->getPrefixedText() : self::special( $parser, 'Badtitle' ); 00827 } 00828 } 00829 00830 static function speciale( $parser, $text ) { 00831 return wfUrlencode( str_replace( ' ', '_', self::special( $parser, $text ) ) ); 00832 } 00833 00842 public static function defaultsort( $parser, $text, $uarg = '' ) { 00843 static $magicWords = null; 00844 if ( is_null( $magicWords ) ) { 00845 $magicWords = new MagicWordArray( array( 'defaultsort_noerror', 'defaultsort_noreplace' ) ); 00846 } 00847 $arg = $magicWords->matchStartToEnd( $uarg ); 00848 00849 $text = trim( $text ); 00850 if ( strlen( $text ) == 0 ) { 00851 return ''; 00852 } 00853 $old = $parser->getCustomDefaultSort(); 00854 if ( $old === false || $arg !== 'defaultsort_noreplace' ) { 00855 $parser->setDefaultSort( $text ); 00856 } 00857 00858 if ( $old === false || $old == $text || $arg ) { 00859 return ''; 00860 } else { 00861 $converter = $parser->getConverterLanguage()->getConverter(); 00862 return '<span class="error">' . 00863 wfMessage( 'duplicate-defaultsort', 00864 // Message should be parsed, but these params should only be escaped. 00865 $converter->markNoConversion( wfEscapeWikiText( $old ) ), 00866 $converter->markNoConversion( wfEscapeWikiText( $text ) ) 00867 )->inContentLanguage()->text() . 00868 '</span>'; 00869 } 00870 } 00871 00872 // Usage {{filepath|300}}, {{filepath|nowiki}}, {{filepath|nowiki|300}} or {{filepath|300|nowiki}} 00873 // or {{filepath|300px}}, {{filepath|200x300px}}, {{filepath|nowiki|200x300px}}, {{filepath|200x300px|nowiki}} 00874 public static function filepath( $parser, $name = '', $argA = '', $argB = '' ) { 00875 $file = wfFindFile( $name ); 00876 00877 if ( $argA == 'nowiki' ) { 00878 // {{filepath: | option [| size] }} 00879 $isNowiki = true; 00880 $parsedWidthParam = $parser->parseWidthParam( $argB ); 00881 } else { 00882 // {{filepath: [| size [|option]] }} 00883 $parsedWidthParam = $parser->parseWidthParam( $argA ); 00884 $isNowiki = ( $argB == 'nowiki' ); 00885 } 00886 00887 if ( $file ) { 00888 $url = $file->getFullUrl(); 00889 00890 // If a size is requested... 00891 if ( count( $parsedWidthParam ) ) { 00892 $mto = $file->transform( $parsedWidthParam ); 00893 // ... and we can 00894 if ( $mto && !$mto->isError() ) { 00895 // ... change the URL to point to a thumbnail. 00896 $url = wfExpandUrl( $mto->getUrl(), PROTO_RELATIVE ); 00897 } 00898 } 00899 if ( $isNowiki ) { 00900 return array( $url, 'nowiki' => true ); 00901 } 00902 return $url; 00903 } else { 00904 return ''; 00905 } 00906 } 00907 00912 public static function tagObj( $parser, $frame, $args ) { 00913 if ( !count( $args ) ) { 00914 return ''; 00915 } 00916 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) ); 00917 00918 if ( count( $args ) ) { 00919 $inner = $frame->expand( array_shift( $args ) ); 00920 } else { 00921 $inner = null; 00922 } 00923 00924 $stripList = $parser->getStripList(); 00925 if ( !in_array( $tagName, $stripList ) ) { 00926 return '<span class="error">' . 00927 wfMessage( 'unknown_extension_tag', $tagName )->inContentLanguage()->text() . 00928 '</span>'; 00929 } 00930 00931 $attributes = array(); 00932 foreach ( $args as $arg ) { 00933 $bits = $arg->splitArg(); 00934 if ( strval( $bits['index'] ) === '' ) { 00935 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) ); 00936 $value = trim( $frame->expand( $bits['value'] ) ); 00937 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) { 00938 $value = isset( $m[1] ) ? $m[1] : ''; 00939 } 00940 $attributes[$name] = $value; 00941 } 00942 } 00943 00944 $params = array( 00945 'name' => $tagName, 00946 'inner' => $inner, 00947 'attributes' => $attributes, 00948 'close' => "</$tagName>", 00949 ); 00950 return $parser->extensionSubstitution( $params, $frame ); 00951 } 00952 }