00001 <?php
00002
00007 class CoreParserFunctions {
00008 static function register( $parser ) {
00009 global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
00010
00011 # Syntax for arguments (see self::setFunctionHook):
00012 # "name for lookup in localized magic words array",
00013 # function callback,
00014 # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}
00015 # instead of {{#int:...}})
00016
00017 $parser->setFunctionHook( 'int', array( __CLASS__, 'intFunction' ), SFH_NO_HASH );
00018 $parser->setFunctionHook( 'ns', array( __CLASS__, 'ns' ), SFH_NO_HASH );
00019 $parser->setFunctionHook( 'nse', array( __CLASS__, 'nse' ), SFH_NO_HASH );
00020 $parser->setFunctionHook( 'urlencode', array( __CLASS__, 'urlencode' ), SFH_NO_HASH );
00021 $parser->setFunctionHook( 'lcfirst', array( __CLASS__, 'lcfirst' ), SFH_NO_HASH );
00022 $parser->setFunctionHook( 'ucfirst', array( __CLASS__, 'ucfirst' ), SFH_NO_HASH );
00023 $parser->setFunctionHook( 'lc', array( __CLASS__, 'lc' ), SFH_NO_HASH );
00024 $parser->setFunctionHook( 'uc', array( __CLASS__, 'uc' ), SFH_NO_HASH );
00025 $parser->setFunctionHook( 'localurl', array( __CLASS__, 'localurl' ), SFH_NO_HASH );
00026 $parser->setFunctionHook( 'localurle', array( __CLASS__, 'localurle' ), SFH_NO_HASH );
00027 $parser->setFunctionHook( 'fullurl', array( __CLASS__, 'fullurl' ), SFH_NO_HASH );
00028 $parser->setFunctionHook( 'fullurle', array( __CLASS__, 'fullurle' ), SFH_NO_HASH );
00029 $parser->setFunctionHook( 'formatnum', array( __CLASS__, 'formatnum' ), SFH_NO_HASH );
00030 $parser->setFunctionHook( 'grammar', array( __CLASS__, 'grammar' ), SFH_NO_HASH );
00031 $parser->setFunctionHook( 'gender', array( __CLASS__, 'gender' ), SFH_NO_HASH );
00032 $parser->setFunctionHook( 'plural', array( __CLASS__, 'plural' ), SFH_NO_HASH );
00033 $parser->setFunctionHook( 'numberofpages', array( __CLASS__, 'numberofpages' ), SFH_NO_HASH );
00034 $parser->setFunctionHook( 'numberofusers', array( __CLASS__, 'numberofusers' ), SFH_NO_HASH );
00035 $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__, 'numberofactiveusers' ), SFH_NO_HASH );
00036 $parser->setFunctionHook( 'numberofarticles', array( __CLASS__, 'numberofarticles' ), SFH_NO_HASH );
00037 $parser->setFunctionHook( 'numberoffiles', array( __CLASS__, 'numberoffiles' ), SFH_NO_HASH );
00038 $parser->setFunctionHook( 'numberofadmins', array( __CLASS__, 'numberofadmins' ), SFH_NO_HASH );
00039 $parser->setFunctionHook( 'numberingroup', array( __CLASS__, 'numberingroup' ), SFH_NO_HASH );
00040 $parser->setFunctionHook( 'numberofedits', array( __CLASS__, 'numberofedits' ), SFH_NO_HASH );
00041 $parser->setFunctionHook( 'numberofviews', array( __CLASS__, 'numberofviews' ), SFH_NO_HASH );
00042 $parser->setFunctionHook( 'language', array( __CLASS__, 'language' ), SFH_NO_HASH );
00043 $parser->setFunctionHook( 'padleft', array( __CLASS__, 'padleft' ), SFH_NO_HASH );
00044 $parser->setFunctionHook( 'padright', array( __CLASS__, 'padright' ), SFH_NO_HASH );
00045 $parser->setFunctionHook( 'anchorencode', array( __CLASS__, 'anchorencode' ), SFH_NO_HASH );
00046 $parser->setFunctionHook( 'special', array( __CLASS__, 'special' ) );
00047 $parser->setFunctionHook( 'defaultsort', array( __CLASS__, 'defaultsort' ), SFH_NO_HASH );
00048 $parser->setFunctionHook( 'filepath', array( __CLASS__, 'filepath' ), SFH_NO_HASH );
00049 $parser->setFunctionHook( 'pagesincategory', array( __CLASS__, 'pagesincategory' ), SFH_NO_HASH );
00050 $parser->setFunctionHook( 'pagesize', array( __CLASS__, 'pagesize' ), SFH_NO_HASH );
00051 $parser->setFunctionHook( 'protectionlevel', array( __CLASS__, 'protectionlevel' ), SFH_NO_HASH );
00052 $parser->setFunctionHook( 'namespace', array( __CLASS__, 'mwnamespace' ), SFH_NO_HASH );
00053 $parser->setFunctionHook( 'namespacee', array( __CLASS__, 'namespacee' ), SFH_NO_HASH );
00054 $parser->setFunctionHook( 'talkspace', array( __CLASS__, 'talkspace' ), SFH_NO_HASH );
00055 $parser->setFunctionHook( 'talkspacee', array( __CLASS__, 'talkspacee' ), SFH_NO_HASH );
00056 $parser->setFunctionHook( 'subjectspace', array( __CLASS__, 'subjectspace' ), SFH_NO_HASH );
00057 $parser->setFunctionHook( 'subjectspacee', array( __CLASS__, 'subjectspacee' ), SFH_NO_HASH );
00058 $parser->setFunctionHook( 'pagename', array( __CLASS__, 'pagename' ), SFH_NO_HASH );
00059 $parser->setFunctionHook( 'pagenamee', array( __CLASS__, 'pagenamee' ), SFH_NO_HASH );
00060 $parser->setFunctionHook( 'fullpagename', array( __CLASS__, 'fullpagename' ), SFH_NO_HASH );
00061 $parser->setFunctionHook( 'fullpagenamee', array( __CLASS__, 'fullpagenamee' ), SFH_NO_HASH );
00062 $parser->setFunctionHook( 'basepagename', array( __CLASS__, 'basepagename' ), SFH_NO_HASH );
00063 $parser->setFunctionHook( 'basepagenamee', array( __CLASS__, 'basepagenamee' ), SFH_NO_HASH );
00064 $parser->setFunctionHook( 'subpagename', array( __CLASS__, 'subpagename' ), SFH_NO_HASH );
00065 $parser->setFunctionHook( 'subpagenamee', array( __CLASS__, 'subpagenamee' ), SFH_NO_HASH );
00066 $parser->setFunctionHook( 'talkpagename', array( __CLASS__, 'talkpagename' ), SFH_NO_HASH );
00067 $parser->setFunctionHook( 'talkpagenamee', array( __CLASS__, 'talkpagenamee' ), SFH_NO_HASH );
00068 $parser->setFunctionHook( 'subjectpagename', array( __CLASS__, 'subjectpagename' ), SFH_NO_HASH );
00069 $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__, 'subjectpagenamee' ), SFH_NO_HASH );
00070 $parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS );
00071 $parser->setFunctionHook( 'formatdate', array( __CLASS__, 'formatDate' ) );
00072
00073 if ( $wgAllowDisplayTitle ) {
00074 $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH );
00075 }
00076 if ( $wgAllowSlowParserFunctions ) {
00077 $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__, 'pagesinnamespace' ), SFH_NO_HASH );
00078 }
00079 }
00080
00081 static function intFunction( $parser, $part1 = '' ) {
00082 if ( strval( $part1 ) !== '' ) {
00083 $args = array_slice( func_get_args(), 2 );
00084 $message = wfMsgGetKey( $part1, true, false, false );
00085 $message = wfMsgReplaceArgs( $message, $args );
00086 $message = $parser->replaceVariables( $message );
00087 return $message;
00088 } else {
00089 return array( 'found' => false );
00090 }
00091 }
00092
00093 static function formatDate( $parser, $date, $defaultPref = null ) {
00094 $df = DateFormatter::getInstance();
00095
00096 $date = trim( $date );
00097
00098 $pref = $parser->mOptions->getDateFormat();
00099
00100
00101
00102 if ( $pref == 'default' && $defaultPref )
00103 $pref = $defaultPref;
00104
00105 $date = $df->reformat( $pref, $date, array( 'match-whole' ) );
00106 return $date;
00107 }
00108
00109 static function ns( $parser, $part1 = '' ) {
00110 global $wgContLang;
00111 if ( intval( $part1 ) || $part1 == "0" ) {
00112 $index = intval( $part1 );
00113 } else {
00114 $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
00115 }
00116 if ( $index !== false ) {
00117 return $wgContLang->getFormattedNsText( $index );
00118 } else {
00119 return array( 'found' => false );
00120 }
00121 }
00122
00123 static function nse( $parser, $part1 = '' ) {
00124 return wfUrlencode( str_replace( ' ', '_', self::ns( $parser, $part1 ) ) );
00125 }
00126
00127 static function urlencode( $parser, $s = '' ) {
00128 return urlencode( $s );
00129 }
00130
00131 static function lcfirst( $parser, $s = '' ) {
00132 global $wgContLang;
00133 return $wgContLang->lcfirst( $s );
00134 }
00135
00136 static function ucfirst( $parser, $s = '' ) {
00137 global $wgContLang;
00138 return $wgContLang->ucfirst( $s );
00139 }
00140
00141 static function lc( $parser, $s = '' ) {
00142 global $wgContLang;
00143 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
00144 return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
00145 } else {
00146 return $wgContLang->lc( $s );
00147 }
00148 }
00149
00150 static function uc( $parser, $s = '' ) {
00151 global $wgContLang;
00152 if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
00153 return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
00154 } else {
00155 return $wgContLang->uc( $s );
00156 }
00157 }
00158
00159 static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
00160 static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
00161 static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
00162 static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
00163
00164 static function urlFunction( $func, $s = '', $arg = null ) {
00165 $title = Title::newFromText( $s );
00166 # Due to order of execution of a lot of bits, the values might be encoded
00167 # before arriving here; if that's true, then the title can't be created
00168 # and the variable will fail. If we can't get a decent title from the first
00169 # attempt, url-decode and try for a second.
00170 if( is_null( $title ) )
00171 $title = Title::newFromURL( urldecode( $s ) );
00172 if( !is_null( $title ) ) {
00173 # Convert NS_MEDIA -> NS_FILE
00174 if( $title->getNamespace() == NS_MEDIA ) {
00175 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
00176 }
00177 if( !is_null( $arg ) ) {
00178 $text = $title->$func( $arg );
00179 } else {
00180 $text = $title->$func();
00181 }
00182 return $text;
00183 } else {
00184 return array( 'found' => false );
00185 }
00186 }
00187
00188 static function formatNum( $parser, $num = '', $raw = null) {
00189 if ( self::israw( $raw ) ) {
00190 return $parser->getFunctionLang()->parseFormattedNumber( $num );
00191 } else {
00192 return $parser->getFunctionLang()->formatNum( $num );
00193 }
00194 }
00195
00196 static function grammar( $parser, $case = '', $word = '' ) {
00197 return $parser->getFunctionLang()->convertGrammar( $word, $case );
00198 }
00199
00200 static function gender( $parser, $user ) {
00201 wfProfileIn( __METHOD__ );
00202 $forms = array_slice( func_get_args(), 2);
00203
00204
00205 $gender = User::getDefaultOption( 'gender' );
00206
00207
00208 $title = Title::newFromText( $user );
00209
00210 if ( is_object( $title ) && $title->getNamespace() == NS_USER )
00211 $user = $title->getText();
00212
00213
00214 $user = User::newFromName( $user );
00215 if ( $user ) {
00216 $gender = $user->getOption( 'gender' );
00217 } elseif ( $parser->mOptions->getInterfaceMessage() ) {
00218 global $wgUser;
00219 $gender = $wgUser->getOption( 'gender' );
00220 }
00221 $ret = $parser->getFunctionLang()->gender( $gender, $forms );
00222 wfProfileOut( __METHOD__ );
00223 return $ret;
00224 }
00225 static function plural( $parser, $text = '' ) {
00226 $forms = array_slice( func_get_args(), 2 );
00227 $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
00228 return $parser->getFunctionLang()->convertPlural( $text, $forms );
00229 }
00230
00239 static function displaytitle( $parser, $text = '' ) {
00240 global $wgRestrictDisplayTitle;
00241
00242 #parse a limited subset of wiki markup (just the single quote items)
00243 $text = $parser->doQuotes( $text );
00244
00245 #remove stripped text (e.g. the UNIQ-QINU stuff) that was generated by tag extensions/whatever
00246 $text = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
00247 . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '/', '', $text );
00248
00249 #list of disallowed tags for DISPLAYTITLE
00250 #these will be escaped even though they are allowed in normal wiki text
00251 $bad = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr',
00252 'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rp', 'br' );
00253
00254 #only requested titles that normalize to the actual title are allowed through
00255 #if $wgRestrictDisplayTitle is true (it is by default)
00256 #mimic the escaping process that occurs in OutputPage::setPageTitle
00257 $text = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $text, null, array(), array(), $bad ) );
00258 $title = Title::newFromText( Sanitizer::stripAllTags( $text ) );
00259
00260 if( !$wgRestrictDisplayTitle ) {
00261 $parser->mOutput->setDisplayTitle( $text );
00262 } else {
00263 if ( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) ) {
00264 $parser->mOutput->setDisplayTitle( $text );
00265 }
00266 }
00267
00268 return '';
00269 }
00270
00271 static function isRaw( $param ) {
00272 static $mwRaw;
00273 if ( !$mwRaw ) {
00274 $mwRaw =& MagicWord::get( 'rawsuffix' );
00275 }
00276 if ( is_null( $param ) ) {
00277 return false;
00278 } else {
00279 return $mwRaw->match( $param );
00280 }
00281 }
00282
00283 static function formatRaw( $num, $raw ) {
00284 if( self::isRaw( $raw ) ) {
00285 return $num;
00286 } else {
00287 global $wgContLang;
00288 return $wgContLang->formatNum( $num );
00289 }
00290 }
00291 static function numberofpages( $parser, $raw = null ) {
00292 return self::formatRaw( SiteStats::pages(), $raw );
00293 }
00294 static function numberofusers( $parser, $raw = null ) {
00295 return self::formatRaw( SiteStats::users(), $raw );
00296 }
00297 static function numberofactiveusers( $parser, $raw = null ) {
00298 return self::formatRaw( SiteStats::activeUsers(), $raw );
00299 }
00300 static function numberofarticles( $parser, $raw = null ) {
00301 return self::formatRaw( SiteStats::articles(), $raw );
00302 }
00303 static function numberoffiles( $parser, $raw = null ) {
00304 return self::formatRaw( SiteStats::images(), $raw );
00305 }
00306 static function numberofadmins( $parser, $raw = null ) {
00307 return self::formatRaw( SiteStats::numberingroup('sysop'), $raw );
00308 }
00309 static function numberofedits( $parser, $raw = null ) {
00310 return self::formatRaw( SiteStats::edits(), $raw );
00311 }
00312 static function numberofviews( $parser, $raw = null ) {
00313 return self::formatRaw( SiteStats::views(), $raw );
00314 }
00315 static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
00316 return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
00317 }
00318 static function numberingroup( $parser, $name = '', $raw = null) {
00319 return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
00320 }
00321
00322
00329 static function mwnamespace( $parser, $title = null ) {
00330 $t = Title::newFromText( $title );
00331 if ( is_null( $t ) )
00332 return '';
00333 return str_replace( '_', ' ', $t->getNsText() );
00334 }
00335 static function namespacee( $parser, $title = null ) {
00336 $t = Title::newFromText( $title );
00337 if ( is_null( $t ) )
00338 return '';
00339 return wfUrlencode( $t->getNsText() );
00340 }
00341 static function talkspace( $parser, $title = null ) {
00342 $t = Title::newFromText( $title );
00343 if ( is_null( $t ) || !$t->canTalk() )
00344 return '';
00345 return str_replace( '_', ' ', $t->getTalkNsText() );
00346 }
00347 static function talkspacee( $parser, $title = null ) {
00348 $t = Title::newFromText( $title );
00349 if ( is_null( $t ) || !$t->canTalk() )
00350 return '';
00351 return wfUrlencode( $t->getTalkNsText() );
00352 }
00353 static function subjectspace( $parser, $title = null ) {
00354 $t = Title::newFromText( $title );
00355 if ( is_null( $t ) )
00356 return '';
00357 return str_replace( '_', ' ', $t->getSubjectNsText() );
00358 }
00359 static function subjectspacee( $parser, $title = null ) {
00360 $t = Title::newFromText( $title );
00361 if ( is_null( $t ) )
00362 return '';
00363 return wfUrlencode( $t->getSubjectNsText() );
00364 }
00365
00366
00367
00368
00369 static function pagename( $parser, $title = null ) {
00370 $t = Title::newFromText( $title );
00371 if ( is_null( $t ) )
00372 return '';
00373 return wfEscapeWikiText( $t->getText() );
00374 }
00375 static function pagenamee( $parser, $title = null ) {
00376 $t = Title::newFromText( $title );
00377 if ( is_null( $t ) )
00378 return '';
00379 return $t->getPartialURL();
00380 }
00381 static function fullpagename( $parser, $title = null ) {
00382 $t = Title::newFromText( $title );
00383 if ( is_null( $t ) || !$t->canTalk() )
00384 return '';
00385 return wfEscapeWikiText( $t->getPrefixedText() );
00386 }
00387 static function fullpagenamee( $parser, $title = null ) {
00388 $t = Title::newFromText( $title );
00389 if ( is_null( $t ) || !$t->canTalk() )
00390 return '';
00391 return $t->getPrefixedURL();
00392 }
00393 static function subpagename( $parser, $title = null ) {
00394 $t = Title::newFromText( $title );
00395 if ( is_null( $t ) )
00396 return '';
00397 return $t->getSubpageText();
00398 }
00399 static function subpagenamee( $parser, $title = null ) {
00400 $t = Title::newFromText( $title );
00401 if ( is_null( $t ) )
00402 return '';
00403 return $t->getSubpageUrlForm();
00404 }
00405 static function basepagename( $parser, $title = null ) {
00406 $t = Title::newFromText( $title );
00407 if ( is_null( $t ) )
00408 return '';
00409 return $t->getBaseText();
00410 }
00411 static function basepagenamee( $parser, $title = null ) {
00412 $t = Title::newFromText( $title );
00413 if ( is_null( $t ) )
00414 return '';
00415 return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
00416 }
00417 static function talkpagename( $parser, $title = null ) {
00418 $t = Title::newFromText( $title );
00419 if ( is_null( $t ) || !$t->canTalk() )
00420 return '';
00421 return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
00422 }
00423 static function talkpagenamee( $parser, $title = null ) {
00424 $t = Title::newFromText( $title );
00425 if ( is_null( $t ) || !$t->canTalk() )
00426 return '';
00427 return $t->getTalkPage()->getPrefixedUrl();
00428 }
00429 static function subjectpagename( $parser, $title = null ) {
00430 $t = Title::newFromText( $title );
00431 if ( is_null( $t ) )
00432 return '';
00433 return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
00434 }
00435 static function subjectpagenamee( $parser, $title = null ) {
00436 $t = Title::newFromText( $title );
00437 if ( is_null( $t ) )
00438 return '';
00439 return $t->getSubjectPage()->getPrefixedUrl();
00440 }
00441
00447 static function pagesincategory( $parser, $name = '', $raw = null ) {
00448 static $cache = array();
00449 $category = Category::newFromName( $name );
00450
00451 if( !is_object( $category ) ) {
00452 $cache[$name] = 0;
00453 return self::formatRaw( 0, $raw );
00454 }
00455
00456 # Normalize name for cache
00457 $name = $category->getName();
00458
00459 $count = 0;
00460 if( isset( $cache[$name] ) ) {
00461 $count = $cache[$name];
00462 } elseif( $parser->incrementExpensiveFunctionCount() ) {
00463 $count = $cache[$name] = (int)$category->getPageCount();
00464 }
00465 return self::formatRaw( $count, $raw );
00466 }
00467
00479 static function pagesize( $parser, $page = '', $raw = null ) {
00480 static $cache = array();
00481 $title = Title::newFromText( $page );
00482
00483 if( !is_object( $title ) ) {
00484 $cache[$page] = 0;
00485 return self::formatRaw( 0, $raw );
00486 }
00487
00488 # Normalize name for cache
00489 $page = $title->getPrefixedText();
00490
00491 $length = 0;
00492 if( isset( $cache[$page] ) ) {
00493 $length = $cache[$page];
00494 } elseif( $parser->incrementExpensiveFunctionCount() ) {
00495 $rev = Revision::newFromTitle( $title );
00496 $id = $rev ? $rev->getPage() : 0;
00497 $length = $cache[$page] = $rev ? $rev->getSize() : 0;
00498
00499
00500 $parser->mOutput->addTemplate( $title, $id, $rev ? $rev->getId() : 0 );
00501 }
00502 return self::formatRaw( $length, $raw );
00503 }
00504
00508 static function protectionlevel( $parser, $type = '' ) {
00509 $restrictions = $parser->mTitle->getRestrictions( strtolower( $type ) );
00510 # Title::getRestrictions returns an array, its possible it may have
00511 # multiple values in the future
00512 return implode( $restrictions, ',' );
00513 }
00514
00515 static function language( $parser, $arg = '' ) {
00516 global $wgContLang;
00517 $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
00518 return $lang != '' ? $lang : $arg;
00519 }
00520
00524 static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) {
00525 $lengthOfPadding = mb_strlen( $padding );
00526 if ( $lengthOfPadding == 0 ) return $string;
00527
00528 # The remaining length to add counts down to 0 as padding is added
00529 $length = min( $length, 500 ) - mb_strlen( $string );
00530 # $finalPadding is just $padding repeated enough times so that
00531 # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
00532 $finalPadding = '';
00533 while ( $length > 0 ) {
00534 # If $length < $lengthofPadding, truncate $padding so we get the
00535 # exact length desired.
00536 $finalPadding .= mb_substr( $padding, 0, $length );
00537 $length -= $lengthOfPadding;
00538 }
00539
00540 if ( $direction == STR_PAD_LEFT ) {
00541 return $finalPadding . $string;
00542 } else {
00543 return $string . $finalPadding;
00544 }
00545 }
00546
00547 static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
00548 return self::pad( $string, $length, $padding, STR_PAD_LEFT );
00549 }
00550
00551 static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
00552 return self::pad( $string, $length, $padding );
00553 }
00554
00555 static function anchorencode( $parser, $text ) {
00556 $a = urlencode( $text );
00557 $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
00558 # leave colons alone, however
00559 $a = str_replace( '.3A', ':', $a );
00560 return $a;
00561 }
00562
00563 static function special( $parser, $text ) {
00564 $title = SpecialPage::getTitleForAlias( $text );
00565 if ( $title ) {
00566 return $title->getPrefixedText();
00567 } else {
00568 return wfMsgForContent( 'nosuchspecialpage' );
00569 }
00570 }
00571
00572 public static function defaultsort( $parser, $text ) {
00573 $text = trim( $text );
00574 if( strlen( $text ) == 0 )
00575 return '';
00576 $old = $parser->getCustomDefaultSort();
00577 $parser->setDefaultSort( $text );
00578 if( $old === false || $old == $text )
00579 return '';
00580 else
00581 return( '<span class="error">' .
00582 wfMsg( 'duplicate-defaultsort',
00583 htmlspecialchars( $old ),
00584 htmlspecialchars( $text ) ) .
00585 '</span>' );
00586 }
00587
00588 public static function filepath( $parser, $name='', $option='' ) {
00589 $file = wfFindFile( $name );
00590 if( $file ) {
00591 $url = $file->getFullUrl();
00592 if( $option == 'nowiki' ) {
00593 return array( $url, 'nowiki' => true );
00594 }
00595 return $url;
00596 } else {
00597 return '';
00598 }
00599 }
00600
00604 public static function tagObj( $parser, $frame, $args ) {
00605 $xpath = false;
00606 if ( !count( $args ) ) {
00607 return '';
00608 }
00609 $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
00610
00611 if ( count( $args ) ) {
00612 $inner = $frame->expand( array_shift( $args ) );
00613 } else {
00614 $inner = null;
00615 }
00616
00617 $stripList = $parser->getStripList();
00618 if ( !in_array( $tagName, $stripList ) ) {
00619 return '<span class="error">' .
00620 wfMsg( 'unknown_extension_tag', $tagName ) .
00621 '</span>';
00622 }
00623
00624 $attributes = array();
00625 foreach ( $args as $arg ) {
00626 $bits = $arg->splitArg();
00627 if ( strval( $bits['index'] ) === '' ) {
00628 $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
00629 $value = trim( $frame->expand( $bits['value'] ) );
00630 if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
00631 $value = isset( $m[1] ) ? $m[1] : '';
00632 }
00633 $attributes[$name] = $value;
00634 }
00635 }
00636
00637 $params = array(
00638 'name' => $tagName,
00639 'inner' => $inner,
00640 'attributes' => $attributes,
00641 'close' => "</$tagName>",
00642 );
00643 return $parser->extensionSubstitution( $params, $frame );
00644 }
00645 }