CoreParserFunctions.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <?php
  2. /**
  3. * Various core parser functions, registered in Parser::firstCallInit()
  4. * @ingroup Parser
  5. */
  6. class CoreParserFunctions {
  7. static function register( $parser ) {
  8. global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
  9. # Syntax for arguments (see self::setFunctionHook):
  10. # "name for lookup in localized magic words array",
  11. # function callback,
  12. # optional SFH_NO_HASH to omit the hash from calls (e.g. {{int:...}
  13. # instead of {{#int:...}})
  14. $parser->setFunctionHook( 'int', array( __CLASS__, 'intFunction' ), SFH_NO_HASH );
  15. $parser->setFunctionHook( 'ns', array( __CLASS__, 'ns' ), SFH_NO_HASH );
  16. $parser->setFunctionHook( 'urlencode', array( __CLASS__, 'urlencode' ), SFH_NO_HASH );
  17. $parser->setFunctionHook( 'lcfirst', array( __CLASS__, 'lcfirst' ), SFH_NO_HASH );
  18. $parser->setFunctionHook( 'ucfirst', array( __CLASS__, 'ucfirst' ), SFH_NO_HASH );
  19. $parser->setFunctionHook( 'lc', array( __CLASS__, 'lc' ), SFH_NO_HASH );
  20. $parser->setFunctionHook( 'uc', array( __CLASS__, 'uc' ), SFH_NO_HASH );
  21. $parser->setFunctionHook( 'localurl', array( __CLASS__, 'localurl' ), SFH_NO_HASH );
  22. $parser->setFunctionHook( 'localurle', array( __CLASS__, 'localurle' ), SFH_NO_HASH );
  23. $parser->setFunctionHook( 'fullurl', array( __CLASS__, 'fullurl' ), SFH_NO_HASH );
  24. $parser->setFunctionHook( 'fullurle', array( __CLASS__, 'fullurle' ), SFH_NO_HASH );
  25. $parser->setFunctionHook( 'formatnum', array( __CLASS__, 'formatnum' ), SFH_NO_HASH );
  26. $parser->setFunctionHook( 'grammar', array( __CLASS__, 'grammar' ), SFH_NO_HASH );
  27. $parser->setFunctionHook( 'gender', array( __CLASS__, 'gender' ), SFH_NO_HASH );
  28. $parser->setFunctionHook( 'plural', array( __CLASS__, 'plural' ), SFH_NO_HASH );
  29. $parser->setFunctionHook( 'numberofpages', array( __CLASS__, 'numberofpages' ), SFH_NO_HASH );
  30. $parser->setFunctionHook( 'numberofusers', array( __CLASS__, 'numberofusers' ), SFH_NO_HASH );
  31. $parser->setFunctionHook( 'numberofactiveusers', array( __CLASS__, 'numberofactiveusers' ), SFH_NO_HASH );
  32. $parser->setFunctionHook( 'numberofarticles', array( __CLASS__, 'numberofarticles' ), SFH_NO_HASH );
  33. $parser->setFunctionHook( 'numberoffiles', array( __CLASS__, 'numberoffiles' ), SFH_NO_HASH );
  34. $parser->setFunctionHook( 'numberofadmins', array( __CLASS__, 'numberofadmins' ), SFH_NO_HASH );
  35. $parser->setFunctionHook( 'numberingroup', array( __CLASS__, 'numberingroup' ), SFH_NO_HASH );
  36. $parser->setFunctionHook( 'numberofedits', array( __CLASS__, 'numberofedits' ), SFH_NO_HASH );
  37. $parser->setFunctionHook( 'numberofviews', array( __CLASS__, 'numberofviews' ), SFH_NO_HASH );
  38. $parser->setFunctionHook( 'language', array( __CLASS__, 'language' ), SFH_NO_HASH );
  39. $parser->setFunctionHook( 'padleft', array( __CLASS__, 'padleft' ), SFH_NO_HASH );
  40. $parser->setFunctionHook( 'padright', array( __CLASS__, 'padright' ), SFH_NO_HASH );
  41. $parser->setFunctionHook( 'anchorencode', array( __CLASS__, 'anchorencode' ), SFH_NO_HASH );
  42. $parser->setFunctionHook( 'special', array( __CLASS__, 'special' ) );
  43. $parser->setFunctionHook( 'defaultsort', array( __CLASS__, 'defaultsort' ), SFH_NO_HASH );
  44. $parser->setFunctionHook( 'filepath', array( __CLASS__, 'filepath' ), SFH_NO_HASH );
  45. $parser->setFunctionHook( 'pagesincategory', array( __CLASS__, 'pagesincategory' ), SFH_NO_HASH );
  46. $parser->setFunctionHook( 'pagesize', array( __CLASS__, 'pagesize' ), SFH_NO_HASH );
  47. $parser->setFunctionHook( 'protectionlevel', array( __CLASS__, 'protectionlevel' ), SFH_NO_HASH );
  48. $parser->setFunctionHook( 'namespace', array( __CLASS__, 'mwnamespace' ), SFH_NO_HASH );
  49. $parser->setFunctionHook( 'namespacee', array( __CLASS__, 'namespacee' ), SFH_NO_HASH );
  50. $parser->setFunctionHook( 'talkspace', array( __CLASS__, 'talkspace' ), SFH_NO_HASH );
  51. $parser->setFunctionHook( 'talkspacee', array( __CLASS__, 'talkspacee' ), SFH_NO_HASH );
  52. $parser->setFunctionHook( 'subjectspace', array( __CLASS__, 'subjectspace' ), SFH_NO_HASH );
  53. $parser->setFunctionHook( 'subjectspacee', array( __CLASS__, 'subjectspacee' ), SFH_NO_HASH );
  54. $parser->setFunctionHook( 'pagename', array( __CLASS__, 'pagename' ), SFH_NO_HASH );
  55. $parser->setFunctionHook( 'pagenamee', array( __CLASS__, 'pagenamee' ), SFH_NO_HASH );
  56. $parser->setFunctionHook( 'fullpagename', array( __CLASS__, 'fullpagename' ), SFH_NO_HASH );
  57. $parser->setFunctionHook( 'fullpagenamee', array( __CLASS__, 'fullpagenamee' ), SFH_NO_HASH );
  58. $parser->setFunctionHook( 'basepagename', array( __CLASS__, 'basepagename' ), SFH_NO_HASH );
  59. $parser->setFunctionHook( 'basepagenamee', array( __CLASS__, 'basepagenamee' ), SFH_NO_HASH );
  60. $parser->setFunctionHook( 'subpagename', array( __CLASS__, 'subpagename' ), SFH_NO_HASH );
  61. $parser->setFunctionHook( 'subpagenamee', array( __CLASS__, 'subpagenamee' ), SFH_NO_HASH );
  62. $parser->setFunctionHook( 'talkpagename', array( __CLASS__, 'talkpagename' ), SFH_NO_HASH );
  63. $parser->setFunctionHook( 'talkpagenamee', array( __CLASS__, 'talkpagenamee' ), SFH_NO_HASH );
  64. $parser->setFunctionHook( 'subjectpagename', array( __CLASS__, 'subjectpagename' ), SFH_NO_HASH );
  65. $parser->setFunctionHook( 'subjectpagenamee', array( __CLASS__, 'subjectpagenamee' ), SFH_NO_HASH );
  66. $parser->setFunctionHook( 'tag', array( __CLASS__, 'tagObj' ), SFH_OBJECT_ARGS );
  67. $parser->setFunctionHook( 'formatdate', array( __CLASS__, 'formatDate' ) );
  68. if ( $wgAllowDisplayTitle ) {
  69. $parser->setFunctionHook( 'displaytitle', array( __CLASS__, 'displaytitle' ), SFH_NO_HASH );
  70. }
  71. if ( $wgAllowSlowParserFunctions ) {
  72. $parser->setFunctionHook( 'pagesinnamespace', array( __CLASS__, 'pagesinnamespace' ), SFH_NO_HASH );
  73. }
  74. }
  75. static function intFunction( $parser, $part1 = '' /*, ... */ ) {
  76. if ( strval( $part1 ) !== '' ) {
  77. $args = array_slice( func_get_args(), 2 );
  78. $message = wfMsgGetKey( $part1, true, false, false );
  79. $message = wfMsgReplaceArgs( $message, $args );
  80. $message = $parser->replaceVariables( $message ); // like $wgMessageCache->transform()
  81. return $message;
  82. } else {
  83. return array( 'found' => false );
  84. }
  85. }
  86. static function formatDate( $parser, $date, $defaultPref = null ) {
  87. $df = DateFormatter::getInstance();
  88. $date = trim($date);
  89. $pref = $parser->mOptions->getDateFormat();
  90. // Specify a different default date format other than the the normal default
  91. // iff the user has 'default' for their setting
  92. if ($pref == 'default' && $defaultPref)
  93. $pref = $defaultPref;
  94. $date = $df->reformat( $pref, $date, array('match-whole') );
  95. return $date;
  96. }
  97. static function ns( $parser, $part1 = '' ) {
  98. global $wgContLang;
  99. if ( intval( $part1 ) || $part1 == "0" ) {
  100. $index = intval( $part1 );
  101. } else {
  102. $index = $wgContLang->getNsIndex( str_replace( ' ', '_', $part1 ) );
  103. }
  104. if ( $index !== false ) {
  105. return $wgContLang->getFormattedNsText( $index );
  106. } else {
  107. return array( 'found' => false );
  108. }
  109. }
  110. static function urlencode( $parser, $s = '' ) {
  111. return urlencode( $s );
  112. }
  113. static function lcfirst( $parser, $s = '' ) {
  114. global $wgContLang;
  115. return $wgContLang->lcfirst( $s );
  116. }
  117. static function ucfirst( $parser, $s = '' ) {
  118. global $wgContLang;
  119. return $wgContLang->ucfirst( $s );
  120. }
  121. static function lc( $parser, $s = '' ) {
  122. global $wgContLang;
  123. if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
  124. return $parser->markerSkipCallback( $s, array( $wgContLang, 'lc' ) );
  125. } else {
  126. return $wgContLang->lc( $s );
  127. }
  128. }
  129. static function uc( $parser, $s = '' ) {
  130. global $wgContLang;
  131. if ( is_callable( array( $parser, 'markerSkipCallback' ) ) ) {
  132. return $parser->markerSkipCallback( $s, array( $wgContLang, 'uc' ) );
  133. } else {
  134. return $wgContLang->uc( $s );
  135. }
  136. }
  137. static function localurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getLocalURL', $s, $arg ); }
  138. static function localurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeLocalURL', $s, $arg ); }
  139. static function fullurl( $parser, $s = '', $arg = null ) { return self::urlFunction( 'getFullURL', $s, $arg ); }
  140. static function fullurle( $parser, $s = '', $arg = null ) { return self::urlFunction( 'escapeFullURL', $s, $arg ); }
  141. static function urlFunction( $func, $s = '', $arg = null ) {
  142. $title = Title::newFromText( $s );
  143. # Due to order of execution of a lot of bits, the values might be encoded
  144. # before arriving here; if that's true, then the title can't be created
  145. # and the variable will fail. If we can't get a decent title from the first
  146. # attempt, url-decode and try for a second.
  147. if( is_null( $title ) )
  148. $title = Title::newFromUrl( urldecode( $s ) );
  149. if( !is_null( $title ) ) {
  150. # Convert NS_MEDIA -> NS_FILE
  151. if( $title->getNamespace() == NS_MEDIA ) {
  152. $title = Title::makeTitle( NS_FILE, $title->getDBKey() );
  153. }
  154. if( !is_null( $arg ) ) {
  155. $text = $title->$func( $arg );
  156. } else {
  157. $text = $title->$func();
  158. }
  159. return $text;
  160. } else {
  161. return array( 'found' => false );
  162. }
  163. }
  164. static function formatNum( $parser, $num = '', $raw = null) {
  165. if ( self::israw( $raw ) ) {
  166. return $parser->getFunctionLang()->parseFormattedNumber( $num );
  167. } else {
  168. return $parser->getFunctionLang()->formatNum( $num );
  169. }
  170. }
  171. static function grammar( $parser, $case = '', $word = '' ) {
  172. return $parser->getFunctionLang()->convertGrammar( $word, $case );
  173. }
  174. static function gender( $parser, $user ) {
  175. $forms = array_slice( func_get_args(), 2);
  176. // default
  177. $gender = User::getDefaultOption( 'gender' );
  178. // allow prefix.
  179. $title = Title::newFromText( $user );
  180. if (is_object( $title ) && $title->getNamespace() == NS_USER)
  181. $user = $title->getText();
  182. // check parameter, or use $wgUser if in interface message
  183. $user = User::newFromName( $user );
  184. if ( $user ) {
  185. $gender = $user->getOption( 'gender' );
  186. } elseif ( $parser->mOptions->getInterfaceMessage() ) {
  187. global $wgUser;
  188. $gender = $wgUser->getOption( 'gender' );
  189. }
  190. return $parser->getFunctionLang()->gender( $gender, $forms );
  191. }
  192. static function plural( $parser, $text = '') {
  193. $forms = array_slice( func_get_args(), 2);
  194. $text = $parser->getFunctionLang()->parseFormattedNumber( $text );
  195. return $parser->getFunctionLang()->convertPlural( $text, $forms );
  196. }
  197. /**
  198. * Override the title of the page when viewed, provided we've been given a
  199. * title which will normalise to the canonical title
  200. *
  201. * @param Parser $parser Parent parser
  202. * @param string $text Desired title text
  203. * @return string
  204. */
  205. static function displaytitle( $parser, $text = '' ) {
  206. global $wgRestrictDisplayTitle;
  207. $text = trim( Sanitizer::decodeCharReferences( $text ) );
  208. if ( !$wgRestrictDisplayTitle ) {
  209. $parser->mOutput->setDisplayTitle( $text );
  210. } else {
  211. $title = Title::newFromText( $text );
  212. if( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) )
  213. $parser->mOutput->setDisplayTitle( $text );
  214. }
  215. return '';
  216. }
  217. static function isRaw( $param ) {
  218. static $mwRaw;
  219. if ( !$mwRaw ) {
  220. $mwRaw =& MagicWord::get( 'rawsuffix' );
  221. }
  222. if ( is_null( $param ) ) {
  223. return false;
  224. } else {
  225. return $mwRaw->match( $param );
  226. }
  227. }
  228. static function formatRaw( $num, $raw ) {
  229. if( self::isRaw( $raw ) ) {
  230. return $num;
  231. } else {
  232. global $wgContLang;
  233. return $wgContLang->formatNum( $num );
  234. }
  235. }
  236. static function numberofpages( $parser, $raw = null ) {
  237. return self::formatRaw( SiteStats::pages(), $raw );
  238. }
  239. static function numberofusers( $parser, $raw = null ) {
  240. return self::formatRaw( SiteStats::users(), $raw );
  241. }
  242. static function numberofactiveusers( $parser, $raw = null ) {
  243. return self::formatRaw( SiteStats::activeUsers(), $raw );
  244. }
  245. static function numberofarticles( $parser, $raw = null ) {
  246. return self::formatRaw( SiteStats::articles(), $raw );
  247. }
  248. static function numberoffiles( $parser, $raw = null ) {
  249. return self::formatRaw( SiteStats::images(), $raw );
  250. }
  251. static function numberofadmins( $parser, $raw = null ) {
  252. return self::formatRaw( SiteStats::numberingroup('sysop'), $raw );
  253. }
  254. static function numberofedits( $parser, $raw = null ) {
  255. return self::formatRaw( SiteStats::edits(), $raw );
  256. }
  257. static function numberofviews( $parser, $raw = null ) {
  258. return self::formatRaw( SiteStats::views(), $raw );
  259. }
  260. static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) {
  261. return self::formatRaw( SiteStats::pagesInNs( intval( $namespace ) ), $raw );
  262. }
  263. static function numberingroup( $parser, $name = '', $raw = null) {
  264. return self::formatRaw( SiteStats::numberingroup( strtolower( $name ) ), $raw );
  265. }
  266. /**
  267. * Given a title, return the namespace name that would be given by the
  268. * corresponding magic word
  269. * Note: function name changed to "mwnamespace" rather than "namespace"
  270. * to not break PHP 5.3
  271. */
  272. static function mwnamespace( $parser, $title = null ) {
  273. $t = Title::newFromText( $title );
  274. if ( is_null($t) )
  275. return '';
  276. return str_replace( '_', ' ', $t->getNsText() );
  277. }
  278. static function namespacee( $parser, $title = null ) {
  279. $t = Title::newFromText( $title );
  280. if ( is_null($t) )
  281. return '';
  282. return wfUrlencode( $t->getNsText() );
  283. }
  284. static function talkspace( $parser, $title = null ) {
  285. $t = Title::newFromText( $title );
  286. if ( is_null($t) || !$t->canTalk() )
  287. return '';
  288. return str_replace( '_', ' ', $t->getTalkNsText() );
  289. }
  290. static function talkspacee( $parser, $title = null ) {
  291. $t = Title::newFromText( $title );
  292. if ( is_null($t) || !$t->canTalk() )
  293. return '';
  294. return wfUrlencode( $t->getTalkNsText() );
  295. }
  296. static function subjectspace( $parser, $title = null ) {
  297. $t = Title::newFromText( $title );
  298. if ( is_null($t) )
  299. return '';
  300. return str_replace( '_', ' ', $t->getSubjectNsText() );
  301. }
  302. static function subjectspacee( $parser, $title = null ) {
  303. $t = Title::newFromText( $title );
  304. if ( is_null($t) )
  305. return '';
  306. return wfUrlencode( $t->getSubjectNsText() );
  307. }
  308. /*
  309. * Functions to get and normalize pagenames, corresponding to the magic words
  310. * of the same names
  311. */
  312. static function pagename( $parser, $title = null ) {
  313. $t = Title::newFromText( $title );
  314. if ( is_null($t) )
  315. return '';
  316. return wfEscapeWikiText( $t->getText() );
  317. }
  318. static function pagenamee( $parser, $title = null ) {
  319. $t = Title::newFromText( $title );
  320. if ( is_null($t) )
  321. return '';
  322. return $t->getPartialURL();
  323. }
  324. static function fullpagename( $parser, $title = null ) {
  325. $t = Title::newFromText( $title );
  326. if ( is_null($t) || !$t->canTalk() )
  327. return '';
  328. return wfEscapeWikiText( $t->getPrefixedText() );
  329. }
  330. static function fullpagenamee( $parser, $title = null ) {
  331. $t = Title::newFromText( $title );
  332. if ( is_null($t) || !$t->canTalk() )
  333. return '';
  334. return $t->getPrefixedURL();
  335. }
  336. static function subpagename( $parser, $title = null ) {
  337. $t = Title::newFromText( $title );
  338. if ( is_null($t) )
  339. return '';
  340. return $t->getSubpageText();
  341. }
  342. static function subpagenamee( $parser, $title = null ) {
  343. $t = Title::newFromText( $title );
  344. if ( is_null($t) )
  345. return '';
  346. return $t->getSubpageUrlForm();
  347. }
  348. static function basepagename( $parser, $title = null ) {
  349. $t = Title::newFromText( $title );
  350. if ( is_null($t) )
  351. return '';
  352. return $t->getBaseText();
  353. }
  354. static function basepagenamee( $parser, $title = null ) {
  355. $t = Title::newFromText( $title );
  356. if ( is_null($t) )
  357. return '';
  358. return wfUrlEncode( str_replace( ' ', '_', $t->getBaseText() ) );
  359. }
  360. static function talkpagename( $parser, $title = null ) {
  361. $t = Title::newFromText( $title );
  362. if ( is_null($t) || !$t->canTalk() )
  363. return '';
  364. return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
  365. }
  366. static function talkpagenamee( $parser, $title = null ) {
  367. $t = Title::newFromText( $title );
  368. if ( is_null($t) || !$t->canTalk() )
  369. return '';
  370. return $t->getTalkPage()->getPrefixedUrl();
  371. }
  372. static function subjectpagename( $parser, $title = null ) {
  373. $t = Title::newFromText( $title );
  374. if ( is_null($t) )
  375. return '';
  376. return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
  377. }
  378. static function subjectpagenamee( $parser, $title = null ) {
  379. $t = Title::newFromText( $title );
  380. if ( is_null($t) )
  381. return '';
  382. return $t->getSubjectPage()->getPrefixedUrl();
  383. }
  384. /**
  385. * Return the number of pages in the given category, or 0 if it's nonexis-
  386. * tent. This is an expensive parser function and can't be called too many
  387. * times per page.
  388. */
  389. static function pagesincategory( $parser, $name = '', $raw = null ) {
  390. static $cache = array();
  391. $category = Category::newFromName( $name );
  392. if( !is_object( $category ) ) {
  393. $cache[$name] = 0;
  394. return self::formatRaw( 0, $raw );
  395. }
  396. # Normalize name for cache
  397. $name = $category->getName();
  398. $count = 0;
  399. if( isset( $cache[$name] ) ) {
  400. $count = $cache[$name];
  401. } elseif( $parser->incrementExpensiveFunctionCount() ) {
  402. $count = $cache[$name] = (int)$category->getPageCount();
  403. }
  404. return self::formatRaw( $count, $raw );
  405. }
  406. /**
  407. * Return the size of the given page, or 0 if it's nonexistent. This is an
  408. * expensive parser function and can't be called too many times per page.
  409. *
  410. * @FIXME This doesn't work correctly on preview for getting the size of
  411. * the current page.
  412. * @FIXME Title::getLength() documentation claims that it adds things to
  413. * the link cache, so the local cache here should be unnecessary, but in
  414. * fact calling getLength() repeatedly for the same $page does seem to
  415. * run one query for each call?
  416. */
  417. static function pagesize( $parser, $page = '', $raw = null ) {
  418. static $cache = array();
  419. $title = Title::newFromText($page);
  420. if( !is_object( $title ) ) {
  421. $cache[$page] = 0;
  422. return self::formatRaw( 0, $raw );
  423. }
  424. # Normalize name for cache
  425. $page = $title->getPrefixedText();
  426. $length = 0;
  427. if( isset( $cache[$page] ) ) {
  428. $length = $cache[$page];
  429. } elseif( $parser->incrementExpensiveFunctionCount() ) {
  430. $rev = Revision::newFromTitle($title);
  431. $id = $rev ? $rev->getPage() : 0;
  432. $length = $cache[$page] = $rev ? $rev->getSize() : 0;
  433. // Register dependency in templatelinks
  434. $parser->mOutput->addTemplate( $title, $id, $rev ? $rev->getId() : 0 );
  435. }
  436. return self::formatRaw( $length, $raw );
  437. }
  438. /**
  439. * Returns the requested protection level for the current page
  440. */
  441. static function protectionlevel( $parser, $type = '' ) {
  442. $restrictions = $parser->mTitle->getRestrictions( strtolower( $type ) );
  443. # Title::getRestrictions returns an array, its possible it may have
  444. # multiple values in the future
  445. return implode( $restrictions, ',' );
  446. }
  447. static function language( $parser, $arg = '' ) {
  448. global $wgContLang;
  449. $lang = $wgContLang->getLanguageName( strtolower( $arg ) );
  450. return $lang != '' ? $lang : $arg;
  451. }
  452. /**
  453. * Unicode-safe str_pad with the restriction that $length is forced to be <= 500
  454. */
  455. static function pad( $string, $length, $padding = '0', $direction = STR_PAD_RIGHT ) {
  456. $lengthOfPadding = mb_strlen( $padding );
  457. if ( $lengthOfPadding == 0 ) return $string;
  458. # The remaining length to add counts down to 0 as padding is added
  459. $length = min( $length, 500 ) - mb_strlen( $string );
  460. # $finalPadding is just $padding repeated enough times so that
  461. # mb_strlen( $string ) + mb_strlen( $finalPadding ) == $length
  462. $finalPadding = '';
  463. while ( $length > 0 ) {
  464. # If $length < $lengthofPadding, truncate $padding so we get the
  465. # exact length desired.
  466. $finalPadding .= mb_substr( $padding, 0, $length );
  467. $length -= $lengthOfPadding;
  468. }
  469. if ( $direction == STR_PAD_LEFT ) {
  470. return $finalPadding . $string;
  471. } else {
  472. return $string . $finalPadding;
  473. }
  474. }
  475. static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) {
  476. return self::pad( $string, $length, $padding, STR_PAD_LEFT );
  477. }
  478. static function padright( $parser, $string = '', $length = 0, $padding = '0' ) {
  479. return self::pad( $string, $length, $padding );
  480. }
  481. static function anchorencode( $parser, $text ) {
  482. $a = urlencode( $text );
  483. $a = strtr( $a, array( '%' => '.', '+' => '_' ) );
  484. # leave colons alone, however
  485. $a = str_replace( '.3A', ':', $a );
  486. return $a;
  487. }
  488. static function special( $parser, $text ) {
  489. $title = SpecialPage::getTitleForAlias( $text );
  490. if ( $title ) {
  491. return $title->getPrefixedText();
  492. } else {
  493. return wfMsgForContent( 'nosuchspecialpage' );
  494. }
  495. }
  496. public static function defaultsort( $parser, $text ) {
  497. $text = trim( $text );
  498. if( strlen( $text ) == 0 )
  499. return '';
  500. $old = $parser->getCustomDefaultSort();
  501. $parser->setDefaultSort( $text );
  502. if( $old === false || $old == $text )
  503. return '';
  504. else
  505. return( '<span class="error">' .
  506. wfMsg( 'duplicate-defaultsort',
  507. htmlspecialchars( $old ),
  508. htmlspecialchars( $text ) ) .
  509. '</span>' );
  510. }
  511. public static function filepath( $parser, $name='', $option='' ) {
  512. $file = wfFindFile( $name );
  513. if( $file ) {
  514. $url = $file->getFullUrl();
  515. if( $option == 'nowiki' ) {
  516. return array( $url, 'nowiki' => true );
  517. }
  518. return $url;
  519. } else {
  520. return '';
  521. }
  522. }
  523. /**
  524. * Parser function to extension tag adaptor
  525. */
  526. public static function tagObj( $parser, $frame, $args ) {
  527. $xpath = false;
  528. if ( !count( $args ) ) {
  529. return '';
  530. }
  531. $tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) );
  532. if ( count( $args ) ) {
  533. $inner = $frame->expand( array_shift( $args ) );
  534. } else {
  535. $inner = null;
  536. }
  537. $stripList = $parser->getStripList();
  538. if ( !in_array( $tagName, $stripList ) ) {
  539. return '<span class="error">' .
  540. wfMsg( 'unknown_extension_tag', $tagName ) .
  541. '</span>';
  542. }
  543. $attributes = array();
  544. foreach ( $args as $arg ) {
  545. $bits = $arg->splitArg();
  546. if ( strval( $bits['index'] ) === '' ) {
  547. $name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
  548. $value = trim( $frame->expand( $bits['value'] ) );
  549. if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) {
  550. $value = isset( $m[1] ) ? $m[1] : '';
  551. }
  552. $attributes[$name] = $value;
  553. }
  554. }
  555. $params = array(
  556. 'name' => $tagName,
  557. 'inner' => $inner,
  558. 'attributes' => $attributes,
  559. 'close' => "</$tagName>",
  560. );
  561. return $parser->extensionSubstitution( $params, $frame );
  562. }
  563. }