| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Parser; |
| 25 |
|
| 26 |
use InvalidArgumentException; |
| 27 |
use MediaWiki\Category\Category; |
| 28 |
use MediaWiki\Config\ServiceOptions; |
| 29 |
use MediaWiki\Language\Language; |
| 30 |
use MediaWiki\Language\LanguageCode; |
| 31 |
use MediaWiki\Languages\LanguageNameUtils; |
| 32 |
use MediaWiki\MainConfigNames; |
| 33 |
use MediaWiki\MediaWikiServices; |
| 34 |
use MediaWiki\Message\Message; |
| 35 |
use MediaWiki\Revision\RevisionAccessException; |
| 36 |
use MediaWiki\Revision\RevisionRecord; |
| 37 |
use MediaWiki\SiteStats\SiteStats; |
| 38 |
use MediaWiki\SpecialPage\SpecialPage; |
| 39 |
use MediaWiki\Title\Title; |
| 40 |
use MediaWiki\User\User; |
| 41 |
use Wikimedia\Bcp47Code\Bcp47CodeValue; |
| 42 |
use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 43 |
use Wikimedia\RemexHtml\Tokenizer\PlainAttributes; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class CoreParserFunctions { |
| 50 |
|
| 51 |
private const MAX_TTS = 900; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public const REGISTER_OPTIONS = [ |
| 57 |
|
| 58 |
MainConfigNames::AllowDisplayTitle, |
| 59 |
MainConfigNames::AllowSlowParserFunctions, |
| 60 |
]; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static function register( Parser $parser, ServiceOptions $options ) { |
| 70 |
$options->assertRequiredOptions( self::REGISTER_OPTIONS ); |
| 71 |
$allowDisplayTitle = $options->get( MainConfigNames::AllowDisplayTitle ); |
| 72 |
$allowSlowParserFunctions = $options->get( MainConfigNames::AllowSlowParserFunctions ); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
$noHashFunctions = [ |
| 80 |
'ns', 'nse', 'urlencode', 'lcfirst', 'ucfirst', 'lc', 'uc', |
| 81 |
'localurl', 'localurle', 'fullurl', 'fullurle', 'canonicalurl', |
| 82 |
'canonicalurle', 'formatnum', 'grammar', 'gender', 'plural', 'formal', |
| 83 |
'bidi', 'numberingroup', 'language', |
| 84 |
'padleft', 'padright', 'anchorencode', 'defaultsort', 'filepath', |
| 85 |
'pagesincategory', 'pagesize', 'protectionlevel', 'protectionexpiry', |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
'pagename', 'pagenamee', |
| 98 |
'fullpagename', 'fullpagenamee', |
| 99 |
'subpagename', 'subpagenamee', |
| 100 |
'rootpagename', 'rootpagenamee', |
| 101 |
'basepagename', 'basepagenamee', |
| 102 |
'talkpagename', 'talkpagenamee', |
| 103 |
'subjectpagename', 'subjectpagenamee', |
| 104 |
'pageid', 'revisionid', 'revisionday', |
| 105 |
'revisionday2', 'revisionmonth', 'revisionmonth1', 'revisionyear', |
| 106 |
'revisiontimestamp', |
| 107 |
'revisionuser', |
| 108 |
'cascadingsources', |
| 109 |
'namespace', 'namespacee', 'namespacenumber', 'talkspace', 'talkspacee', |
| 110 |
'subjectspace', 'subjectspacee', |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
'numberofarticles', 'numberoffiles', |
| 117 |
'numberofusers', |
| 118 |
'numberofactiveusers', |
| 119 |
'numberofpages', |
| 120 |
'numberofadmins', |
| 121 |
'numberofedits', |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
'bcp47', |
| 126 |
'dir', |
| 127 |
]; |
| 128 |
foreach ( $noHashFunctions as $func ) { |
| 129 |
$parser->setFunctionHook( $func, [ __CLASS__, $func ], Parser::SFH_NO_HASH ); |
| 130 |
} |
| 131 |
|
| 132 |
$parser->setFunctionHook( 'int', [ __CLASS__, 'intFunction' ], Parser::SFH_NO_HASH ); |
| 133 |
$parser->setFunctionHook( 'special', [ __CLASS__, 'special' ] ); |
| 134 |
$parser->setFunctionHook( 'speciale', [ __CLASS__, 'speciale' ] ); |
| 135 |
$parser->setFunctionHook( 'tag', [ __CLASS__, 'tagObj' ], Parser::SFH_OBJECT_ARGS ); |
| 136 |
$parser->setFunctionHook( 'formatdate', [ __CLASS__, 'formatDate' ] ); |
| 137 |
|
| 138 |
if ( $allowDisplayTitle ) { |
| 139 |
$parser->setFunctionHook( |
| 140 |
'displaytitle', |
| 141 |
[ __CLASS__, 'displaytitle' ], |
| 142 |
Parser::SFH_NO_HASH |
| 143 |
); |
| 144 |
} |
| 145 |
if ( $allowSlowParserFunctions ) { |
| 146 |
$parser->setFunctionHook( |
| 147 |
'pagesinnamespace', |
| 148 |
[ __CLASS__, 'pagesinnamespace' ], |
| 149 |
Parser::SFH_NO_HASH |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public static function intFunction( $parser, $part1 = '', ...$params ) { |
| 161 |
if ( strval( $part1 ) !== '' ) { |
| 162 |
$message = wfMessage( $part1, $params ) |
| 163 |
->inLanguage( $parser->getOptions()->getUserLangObj() ); |
| 164 |
return [ $message->plain(), 'noparse' => false ]; |
| 165 |
} else { |
| 166 |
return [ 'found' => false ]; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public static function formatDate( $parser, $date, $defaultPref = null ) { |
| 178 |
$lang = $parser->getTargetLanguage(); |
| 179 |
$df = MediaWikiServices::getInstance()->getDateFormatterFactory()->get( $lang ); |
| 180 |
|
| 181 |
$date = trim( $date ); |
| 182 |
|
| 183 |
$pref = $parser->getOptions()->getDateFormat(); |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if ( $pref == 'default' && $defaultPref ) { |
| 188 |
$pref = $defaultPref; |
| 189 |
} |
| 190 |
|
| 191 |
$date = $df->reformat( $pref, $date, [ 'match-whole' ] ); |
| 192 |
return $date; |
| 193 |
} |
| 194 |
|
| 195 |
public static function ns( $parser, $part1 = '' ) { |
| 196 |
if ( intval( $part1 ) || $part1 == "0" ) { |
| 197 |
$index = intval( $part1 ); |
| 198 |
} else { |
| 199 |
$index = $parser->getContentLanguage()->getNsIndex( str_replace( ' ', '_', $part1 ) ); |
| 200 |
} |
| 201 |
if ( $index !== false ) { |
| 202 |
return $parser->getContentLanguage()->getFormattedNsText( $index ); |
| 203 |
} else { |
| 204 |
return [ 'found' => false ]; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
public static function nse( $parser, $part1 = '' ) { |
| 209 |
$ret = self::ns( $parser, $part1 ); |
| 210 |
if ( is_string( $ret ) ) { |
| 211 |
$ret = wfUrlencode( str_replace( ' ', '_', $ret ) ); |
| 212 |
} |
| 213 |
return $ret; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public static function urlencode( $parser, $s = '', $arg = null ) { |
| 229 |
static $magicWords = null; |
| 230 |
if ( $magicWords === null ) { |
| 231 |
$magicWords = |
| 232 |
$parser->getMagicWordFactory()->newArray( [ 'url_path', 'url_query', 'url_wiki' ] ); |
| 233 |
} |
| 234 |
switch ( $magicWords->matchStartToEnd( $arg ?? '' ) ) { |
| 235 |
|
| 236 |
case 'url_wiki': |
| 237 |
$func = 'wfUrlencode'; |
| 238 |
$s = str_replace( ' ', '_', $s ); |
| 239 |
break; |
| 240 |
|
| 241 |
|
| 242 |
case 'url_path': |
| 243 |
$func = 'rawurlencode'; |
| 244 |
break; |
| 245 |
|
| 246 |
|
| 247 |
case 'url_query': |
| 248 |
default: |
| 249 |
$func = 'urlencode'; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
return $func( $parser->killMarkers( $s ) ); |
| 254 |
} |
| 255 |
|
| 256 |
public static function lcfirst( $parser, $s = '' ) { |
| 257 |
return $parser->getContentLanguage()->lcfirst( $s ); |
| 258 |
} |
| 259 |
|
| 260 |
public static function ucfirst( $parser, $s = '' ) { |
| 261 |
return $parser->getContentLanguage()->ucfirst( $s ); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public static function lc( $parser, $s = '' ) { |
| 270 |
return $parser->markerSkipCallback( $s, [ $parser->getContentLanguage(), 'lc' ] ); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public static function uc( $parser, $s = '' ) { |
| 279 |
return $parser->markerSkipCallback( $s, [ $parser->getContentLanguage(), 'uc' ] ); |
| 280 |
} |
| 281 |
|
| 282 |
public static function localurl( $parser, $s = '', $arg = null ) { |
| 283 |
return self::urlFunction( 'getLocalURL', $s, $arg ); |
| 284 |
} |
| 285 |
|
| 286 |
public static function localurle( $parser, $s = '', $arg = null ) { |
| 287 |
$temp = self::urlFunction( 'getLocalURL', $s, $arg ); |
| 288 |
if ( !is_string( $temp ) ) { |
| 289 |
return $temp; |
| 290 |
} else { |
| 291 |
return htmlspecialchars( $temp, ENT_COMPAT ); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
public static function fullurl( $parser, $s = '', $arg = null ) { |
| 296 |
return self::urlFunction( 'getFullURL', $s, $arg ); |
| 297 |
} |
| 298 |
|
| 299 |
public static function fullurle( $parser, $s = '', $arg = null ) { |
| 300 |
$temp = self::urlFunction( 'getFullURL', $s, $arg ); |
| 301 |
if ( !is_string( $temp ) ) { |
| 302 |
return $temp; |
| 303 |
} else { |
| 304 |
return htmlspecialchars( $temp, ENT_COMPAT ); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
public static function canonicalurl( $parser, $s = '', $arg = null ) { |
| 309 |
return self::urlFunction( 'getCanonicalURL', $s, $arg ); |
| 310 |
} |
| 311 |
|
| 312 |
public static function canonicalurle( $parser, $s = '', $arg = null ) { |
| 313 |
$temp = self::urlFunction( 'getCanonicalURL', $s, $arg ); |
| 314 |
if ( !is_string( $temp ) ) { |
| 315 |
return $temp; |
| 316 |
} else { |
| 317 |
return htmlspecialchars( $temp, ENT_COMPAT ); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
public static function urlFunction( $func, $s = '', $arg = null ) { |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
$title = Title::newFromText( $s ) ?? Title::newFromURL( urldecode( $s ) ); |
| 327 |
if ( $title !== null ) { |
| 328 |
|
| 329 |
if ( $title->inNamespace( NS_MEDIA ) ) { |
| 330 |
$title = Title::makeTitle( NS_FILE, $title->getDBkey() ); |
| 331 |
} |
| 332 |
if ( $arg !== null ) { |
| 333 |
$text = $title->$func( $arg ); |
| 334 |
} else { |
| 335 |
$text = $title->$func(); |
| 336 |
} |
| 337 |
return $text; |
| 338 |
} else { |
| 339 |
return [ 'found' => false ]; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public static function formatnum( $parser, $num = '', $arg = null ) { |
| 350 |
if ( self::matchAgainstMagicword( $parser->getMagicWordFactory(), 'rawsuffix', $arg ) ) { |
| 351 |
$func = [ $parser->getTargetLanguage(), 'parseFormattedNumber' ]; |
| 352 |
} elseif ( |
| 353 |
self::matchAgainstMagicword( $parser->getMagicWordFactory(), 'nocommafysuffix', $arg ) |
| 354 |
) { |
| 355 |
$func = [ $parser->getTargetLanguage(), 'formatNumNoSeparators' ]; |
| 356 |
$func = self::getLegacyFormatNum( $parser, $func ); |
| 357 |
} else { |
| 358 |
$func = [ $parser->getTargetLanguage(), 'formatNum' ]; |
| 359 |
$func = self::getLegacyFormatNum( $parser, $func ); |
| 360 |
} |
| 361 |
return $parser->markerSkipCallback( $num, $func ); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
private static function getLegacyFormatNum( $parser, $callback ) { |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
return static function ( $number ) use ( $parser, $callback ) { |
| 376 |
$validNumberRe = '(-(?=[\d\.]))?(\d+|(?=\.\d))(\.\d*)?([Ee][-+]?\d+)?'; |
| 377 |
if ( |
| 378 |
!is_numeric( $number ) && |
| 379 |
$number !== 'NAN' && |
| 380 |
$number !== 'INF' && |
| 381 |
$number !== '-INF' |
| 382 |
) { |
| 383 |
$parser->addTrackingCategory( 'nonnumeric-formatnum' ); |
| 384 |
|
| 385 |
|
| 386 |
return preg_replace_callback( "/{$validNumberRe}/", static function ( $m ) use ( $callback ) { |
| 387 |
return call_user_func( $callback, $m[0] ); |
| 388 |
}, $number ); |
| 389 |
} |
| 390 |
return call_user_func( $callback, $number ); |
| 391 |
}; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public static function grammar( $parser, $case = '', $word = '' ) { |
| 401 |
$word = $parser->killMarkers( $word ); |
| 402 |
return $parser->getTargetLanguage()->convertGrammar( $word, $case ); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public static function gender( $parser, $username, ...$forms ) { |
| 412 |
|
| 413 |
if ( count( $forms ) === 0 ) { |
| 414 |
return ''; |
| 415 |
} elseif ( count( $forms ) === 1 ) { |
| 416 |
return $forms[0]; |
| 417 |
} |
| 418 |
|
| 419 |
$username = trim( $username ); |
| 420 |
|
| 421 |
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup(); |
| 422 |
$gender = $userOptionsLookup->getDefaultOption( 'gender' ); |
| 423 |
|
| 424 |
|
| 425 |
$title = Title::newFromText( $username, NS_USER ); |
| 426 |
|
| 427 |
if ( $title && $title->inNamespace( NS_USER ) ) { |
| 428 |
$username = $title->getText(); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
$user = User::newFromName( $username ); |
| 433 |
$genderCache = MediaWikiServices::getInstance()->getGenderCache(); |
| 434 |
if ( $user ) { |
| 435 |
$gender = $genderCache->getGenderOf( $user, __METHOD__ ); |
| 436 |
} elseif ( $username === '' && $parser->getOptions()->getInterfaceMessage() ) { |
| 437 |
$gender = $genderCache->getGenderOf( $parser->getOptions()->getUserIdentity(), __METHOD__ ); |
| 438 |
} |
| 439 |
$ret = $parser->getTargetLanguage()->gender( $gender, $forms ); |
| 440 |
return $ret; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
public static function plural( $parser, $text = '', ...$forms ) { |
| 450 |
$text = $parser->getTargetLanguage()->parseFormattedNumber( $text ); |
| 451 |
settype( $text, ctype_digit( $text ) ? 'int' : 'float' ); |
| 452 |
|
| 453 |
return $parser->getTargetLanguage()->convertPlural( $text, $forms ); |
| 454 |
} |
| 455 |
|
| 456 |
public static function formal( Parser $parser, string ...$forms ): string { |
| 457 |
$index = $parser->getTargetLanguage()->getFormalityIndex(); |
| 458 |
return $forms[$index] ?? $forms[0]; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
public static function bidi( $parser, $text = '' ) { |
| 467 |
return $parser->getTargetLanguage()->embedBidi( $text ); |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
public static function displaytitle( $parser, $text = '', $uarg = '' ) { |
| 480 |
$restrictDisplayTitle = MediaWikiServices::getInstance()->getMainConfig() |
| 481 |
->get( MainConfigNames::RestrictDisplayTitle ); |
| 482 |
|
| 483 |
static $magicWords = null; |
| 484 |
if ( $magicWords === null ) { |
| 485 |
$magicWords = $parser->getMagicWordFactory()->newArray( |
| 486 |
[ 'displaytitle_noerror', 'displaytitle_noreplace' ] ); |
| 487 |
} |
| 488 |
$arg = $magicWords->matchStartToEnd( $uarg ); |
| 489 |
|
| 490 |
|
| 491 |
$text = $parser->doQuotes( $text ); |
| 492 |
|
| 493 |
|
| 494 |
$text = $parser->killMarkers( $text ); |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
$bad = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'blockquote', 'ol', 'ul', 'li', 'hr', |
| 500 |
'table', 'tr', 'th', 'td', 'dl', 'dd', 'caption', 'p', 'ruby', 'rb', 'rt', 'rtc', 'rp', 'br' ]; |
| 501 |
|
| 502 |
|
| 503 |
if ( $restrictDisplayTitle ) { |
| 504 |
|
| 505 |
|
| 506 |
$htmlTagsCallback = static function ( Attributes $attr ): Attributes { |
| 507 |
$decoded = $attr->getValues(); |
| 508 |
|
| 509 |
if ( isset( $decoded['style'] ) ) { |
| 510 |
|
| 511 |
|
| 512 |
$decoded['style'] = Sanitizer::checkCss( $decoded['style'] ); |
| 513 |
|
| 514 |
if ( preg_match( '/(display|user-select|visibility)\s*:/i', $decoded['style'] ) ) { |
| 515 |
$decoded['style'] = '/* attempt to bypass $wgRestrictDisplayTitle */'; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
return new PlainAttributes( $decoded ); |
| 520 |
}; |
| 521 |
} else { |
| 522 |
$htmlTagsCallback = null; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
$text = Sanitizer::removeSomeTags( $text, [ |
| 529 |
'attrCallback' => $htmlTagsCallback, |
| 530 |
'removeTags' => $bad, |
| 531 |
] ); |
| 532 |
$title = Title::newFromText( Sanitizer::stripAllTags( $text ) ); |
| 533 |
|
| 534 |
$filteredText = Sanitizer::decodeCharReferencesAndNormalize( $text ); |
| 535 |
|
| 536 |
if ( !$restrictDisplayTitle || |
| 537 |
( $title instanceof Title |
| 538 |
&& !$title->hasFragment() |
| 539 |
&& $title->equals( $parser->getTitle() ) ) |
| 540 |
) { |
| 541 |
$old = $parser->getOutput()->getPageProperty( 'displaytitle' ); |
| 542 |
if ( $old === null || $arg !== 'displaytitle_noreplace' ) { |
| 543 |
$parser->getOutput()->setDisplayTitle( $text ); |
| 544 |
} |
| 545 |
if ( $old !== null && $old !== $text && !$arg ) { |
| 546 |
|
| 547 |
$converter = $parser->getTargetLanguageConverter(); |
| 548 |
return '<span class="error">' . |
| 549 |
$parser->msg( 'duplicate-displaytitle', |
| 550 |
|
| 551 |
$converter->markNoConversion( wfEscapeWikiText( $old ) ), |
| 552 |
$converter->markNoConversion( wfEscapeWikiText( $filteredText ) ) |
| 553 |
)->text() . |
| 554 |
'</span>'; |
| 555 |
} else { |
| 556 |
return ''; |
| 557 |
} |
| 558 |
} else { |
| 559 |
$parser->getOutput()->addWarningMsg( |
| 560 |
'restricted-displaytitle', |
| 561 |
|
| 562 |
Message::plaintextParam( $filteredText ) |
| 563 |
); |
| 564 |
$parser->addTrackingCategory( 'restricted-displaytitle-ignored' ); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
private static function matchAgainstMagicword( |
| 578 |
MagicWordFactory $magicWordFactory, $magicword, $value |
| 579 |
) { |
| 580 |
$value = trim( strval( $value ) ); |
| 581 |
if ( $value === '' ) { |
| 582 |
return false; |
| 583 |
} |
| 584 |
$mwObject = $magicWordFactory->get( $magicword ); |
| 585 |
return $mwObject->matchStartToEnd( $value ); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
public static function formatRaw( |
| 598 |
$num, $raw, $language, ?MagicWordFactory $magicWordFactory = null |
| 599 |
) { |
| 600 |
if ( $raw !== null && $raw !== '' ) { |
| 601 |
if ( !$magicWordFactory ) { |
| 602 |
$magicWordFactory = MediaWikiServices::getInstance()->getMagicWordFactory(); |
| 603 |
} |
| 604 |
if ( self::matchAgainstMagicword( $magicWordFactory, 'rawsuffix', $raw ) ) { |
| 605 |
return (string)$num; |
| 606 |
} |
| 607 |
} |
| 608 |
return $language->formatNum( $num ); |
| 609 |
} |
| 610 |
|
| 611 |
public static function numberofpages( $parser, $raw = null ) { |
| 612 |
return self::formatRaw( SiteStats::pages(), $raw, $parser->getTargetLanguage() ); |
| 613 |
} |
| 614 |
|
| 615 |
public static function numberofusers( $parser, $raw = null ) { |
| 616 |
return self::formatRaw( SiteStats::users(), $raw, $parser->getTargetLanguage() ); |
| 617 |
} |
| 618 |
|
| 619 |
public static function numberofactiveusers( $parser, $raw = null ) { |
| 620 |
return self::formatRaw( SiteStats::activeUsers(), $raw, $parser->getTargetLanguage() ); |
| 621 |
} |
| 622 |
|
| 623 |
public static function numberofarticles( $parser, $raw = null ) { |
| 624 |
return self::formatRaw( SiteStats::articles(), $raw, $parser->getTargetLanguage() ); |
| 625 |
} |
| 626 |
|
| 627 |
public static function numberoffiles( $parser, $raw = null ) { |
| 628 |
return self::formatRaw( SiteStats::images(), $raw, $parser->getTargetLanguage() ); |
| 629 |
} |
| 630 |
|
| 631 |
public static function numberofadmins( $parser, $raw = null ) { |
| 632 |
return self::formatRaw( |
| 633 |
SiteStats::numberingroup( 'sysop' ), |
| 634 |
$raw, |
| 635 |
$parser->getTargetLanguage() |
| 636 |
); |
| 637 |
} |
| 638 |
|
| 639 |
public static function numberofedits( $parser, $raw = null ) { |
| 640 |
return self::formatRaw( SiteStats::edits(), $raw, $parser->getTargetLanguage() ); |
| 641 |
} |
| 642 |
|
| 643 |
public static function pagesinnamespace( $parser, $namespace = 0, $raw = null ) { |
| 644 |
return self::formatRaw( |
| 645 |
SiteStats::pagesInNs( intval( $namespace ) ), |
| 646 |
$raw, |
| 647 |
$parser->getTargetLanguage() |
| 648 |
); |
| 649 |
} |
| 650 |
|
| 651 |
public static function numberingroup( $parser, $name = '', $raw = null ) { |
| 652 |
return self::formatRaw( |
| 653 |
SiteStats::numberingroup( strtolower( $name ) ), |
| 654 |
$raw, |
| 655 |
$parser->getTargetLanguage() |
| 656 |
); |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
private static function makeTitle( Parser $parser, ?string $t ) { |
| 668 |
if ( $t === null ) { |
| 669 |
|
| 670 |
$title = $parser->getTitle(); |
| 671 |
} else { |
| 672 |
$title = Title::newFromText( $t ); |
| 673 |
} |
| 674 |
return $title; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
public static function namespace( $parser, $title = null ) { |
| 686 |
$t = self::makeTitle( $parser, $title ); |
| 687 |
if ( $t === null ) { |
| 688 |
return ''; |
| 689 |
} |
| 690 |
return str_replace( '_', ' ', $t->getNsText() ); |
| 691 |
} |
| 692 |
|
| 693 |
public static function namespacee( $parser, $title = null ) { |
| 694 |
$t = self::makeTitle( $parser, $title ); |
| 695 |
if ( $t === null ) { |
| 696 |
return ''; |
| 697 |
} |
| 698 |
return wfUrlencode( $t->getNsText() ); |
| 699 |
} |
| 700 |
|
| 701 |
public static function namespacenumber( $parser, $title = null ) { |
| 702 |
$t = self::makeTitle( $parser, $title ); |
| 703 |
if ( $t === null ) { |
| 704 |
return ''; |
| 705 |
} |
| 706 |
return (string)$t->getNamespace(); |
| 707 |
} |
| 708 |
|
| 709 |
public static function talkspace( $parser, $title = null ) { |
| 710 |
$t = self::makeTitle( $parser, $title ); |
| 711 |
if ( $t === null || !$t->canHaveTalkPage() ) { |
| 712 |
return ''; |
| 713 |
} |
| 714 |
return str_replace( '_', ' ', $t->getTalkNsText() ); |
| 715 |
} |
| 716 |
|
| 717 |
public static function talkspacee( $parser, $title = null ) { |
| 718 |
$t = self::makeTitle( $parser, $title ); |
| 719 |
if ( $t === null || !$t->canHaveTalkPage() ) { |
| 720 |
return ''; |
| 721 |
} |
| 722 |
return wfUrlencode( $t->getTalkNsText() ); |
| 723 |
} |
| 724 |
|
| 725 |
public static function subjectspace( $parser, $title = null ) { |
| 726 |
$t = self::makeTitle( $parser, $title ); |
| 727 |
if ( $t === null ) { |
| 728 |
return ''; |
| 729 |
} |
| 730 |
return str_replace( '_', ' ', $t->getSubjectNsText() ); |
| 731 |
} |
| 732 |
|
| 733 |
public static function subjectspacee( $parser, $title = null ) { |
| 734 |
$t = self::makeTitle( $parser, $title ); |
| 735 |
if ( $t === null ) { |
| 736 |
return ''; |
| 737 |
} |
| 738 |
return wfUrlencode( $t->getSubjectNsText() ); |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
public static function pagename( $parser, $title = null ) { |
| 749 |
$t = self::makeTitle( $parser, $title ); |
| 750 |
if ( $t === null ) { |
| 751 |
return ''; |
| 752 |
} |
| 753 |
return wfEscapeWikiText( $t->getText() ); |
| 754 |
} |
| 755 |
|
| 756 |
public static function pagenamee( $parser, $title = null ) { |
| 757 |
$t = self::makeTitle( $parser, $title ); |
| 758 |
if ( $t === null ) { |
| 759 |
return ''; |
| 760 |
} |
| 761 |
return wfEscapeWikiText( $t->getPartialURL() ); |
| 762 |
} |
| 763 |
|
| 764 |
public static function fullpagename( $parser, $title = null ) { |
| 765 |
$t = self::makeTitle( $parser, $title ); |
| 766 |
if ( $t === null ) { |
| 767 |
return ''; |
| 768 |
} |
| 769 |
return wfEscapeWikiText( $t->getPrefixedText() ); |
| 770 |
} |
| 771 |
|
| 772 |
public static function fullpagenamee( $parser, $title = null ) { |
| 773 |
$t = self::makeTitle( $parser, $title ); |
| 774 |
if ( $t === null ) { |
| 775 |
return ''; |
| 776 |
} |
| 777 |
return wfEscapeWikiText( $t->getPrefixedURL() ); |
| 778 |
} |
| 779 |
|
| 780 |
public static function subpagename( $parser, $title = null ) { |
| 781 |
$t = self::makeTitle( $parser, $title ); |
| 782 |
if ( $t === null ) { |
| 783 |
return ''; |
| 784 |
} |
| 785 |
return wfEscapeWikiText( $t->getSubpageText() ); |
| 786 |
} |
| 787 |
|
| 788 |
public static function subpagenamee( $parser, $title = null ) { |
| 789 |
$t = self::makeTitle( $parser, $title ); |
| 790 |
if ( $t === null ) { |
| 791 |
return ''; |
| 792 |
} |
| 793 |
return wfEscapeWikiText( $t->getSubpageUrlForm() ); |
| 794 |
} |
| 795 |
|
| 796 |
public static function rootpagename( $parser, $title = null ) { |
| 797 |
$t = self::makeTitle( $parser, $title ); |
| 798 |
if ( $t === null ) { |
| 799 |
return ''; |
| 800 |
} |
| 801 |
return wfEscapeWikiText( $t->getRootText() ); |
| 802 |
} |
| 803 |
|
| 804 |
public static function rootpagenamee( $parser, $title = null ) { |
| 805 |
$t = self::makeTitle( $parser, $title ); |
| 806 |
if ( $t === null ) { |
| 807 |
return ''; |
| 808 |
} |
| 809 |
return wfEscapeWikiText( wfUrlencode( str_replace( ' ', '_', $t->getRootText() ) ) ); |
| 810 |
} |
| 811 |
|
| 812 |
public static function basepagename( $parser, $title = null ) { |
| 813 |
$t = self::makeTitle( $parser, $title ); |
| 814 |
if ( $t === null ) { |
| 815 |
return ''; |
| 816 |
} |
| 817 |
return wfEscapeWikiText( $t->getBaseText() ); |
| 818 |
} |
| 819 |
|
| 820 |
public static function basepagenamee( $parser, $title = null ) { |
| 821 |
$t = self::makeTitle( $parser, $title ); |
| 822 |
if ( $t === null ) { |
| 823 |
return ''; |
| 824 |
} |
| 825 |
return wfEscapeWikiText( wfUrlencode( str_replace( ' ', '_', $t->getBaseText() ) ) ); |
| 826 |
} |
| 827 |
|
| 828 |
public static function talkpagename( $parser, $title = null ) { |
| 829 |
$t = self::makeTitle( $parser, $title ); |
| 830 |
if ( $t === null || !$t->canHaveTalkPage() ) { |
| 831 |
return ''; |
| 832 |
} |
| 833 |
return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() ); |
| 834 |
} |
| 835 |
|
| 836 |
public static function talkpagenamee( $parser, $title = null ) { |
| 837 |
$t = self::makeTitle( $parser, $title ); |
| 838 |
if ( $t === null || !$t->canHaveTalkPage() ) { |
| 839 |
return ''; |
| 840 |
} |
| 841 |
return wfEscapeWikiText( $t->getTalkPage()->getPrefixedURL() ); |
| 842 |
} |
| 843 |
|
| 844 |
public static function subjectpagename( $parser, $title = null ) { |
| 845 |
$t = self::makeTitle( $parser, $title ); |
| 846 |
if ( $t === null ) { |
| 847 |
return ''; |
| 848 |
} |
| 849 |
return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() ); |
| 850 |
} |
| 851 |
|
| 852 |
public static function subjectpagenamee( $parser, $title = null ) { |
| 853 |
$t = self::makeTitle( $parser, $title ); |
| 854 |
if ( $t === null ) { |
| 855 |
return ''; |
| 856 |
} |
| 857 |
return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedURL() ); |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
public static function pagesincategory( $parser, $name = '', $arg1 = '', $arg2 = '' ) { |
| 871 |
static $magicWords = null; |
| 872 |
if ( $magicWords === null ) { |
| 873 |
$magicWords = $parser->getMagicWordFactory()->newArray( [ |
| 874 |
'pagesincategory_all', |
| 875 |
'pagesincategory_pages', |
| 876 |
'pagesincategory_subcats', |
| 877 |
'pagesincategory_files' |
| 878 |
] ); |
| 879 |
} |
| 880 |
static $cache = []; |
| 881 |
|
| 882 |
|
| 883 |
if ( self::matchAgainstMagicword( $parser->getMagicWordFactory(), 'rawsuffix', $arg1 ) ) { |
| 884 |
|
| 885 |
$raw = $arg1; |
| 886 |
$type = $magicWords->matchStartToEnd( $arg2 ); |
| 887 |
} else { |
| 888 |
|
| 889 |
$type = $magicWords->matchStartToEnd( $arg1 ); |
| 890 |
$raw = $arg2; |
| 891 |
} |
| 892 |
if ( !$type ) { |
| 893 |
$type = 'pagesincategory_all'; |
| 894 |
} |
| 895 |
|
| 896 |
$title = Title::makeTitleSafe( NS_CATEGORY, $name ); |
| 897 |
if ( !$title ) { |
| 898 |
return self::formatRaw( 0, $raw, $parser->getTargetLanguage() ); |
| 899 |
} |
| 900 |
$languageConverter = MediaWikiServices::getInstance() |
| 901 |
->getLanguageConverterFactory() |
| 902 |
->getLanguageConverter( $parser->getContentLanguage() ); |
| 903 |
$languageConverter->findVariantLink( $name, $title, true ); |
| 904 |
|
| 905 |
|
| 906 |
$name = $title->getDBkey(); |
| 907 |
|
| 908 |
if ( !isset( $cache[$name] ) ) { |
| 909 |
$category = Category::newFromTitle( $title ); |
| 910 |
|
| 911 |
$allCount = $subcatCount = $fileCount = $pageCount = 0; |
| 912 |
if ( $parser->incrementExpensiveFunctionCount() ) { |
| 913 |
$allCount = $category->getMemberCount(); |
| 914 |
$subcatCount = $category->getSubcatCount(); |
| 915 |
$fileCount = $category->getFileCount(); |
| 916 |
$pageCount = $category->getPageCount( Category::COUNT_CONTENT_PAGES ); |
| 917 |
} |
| 918 |
$cache[$name]['pagesincategory_all'] = $allCount; |
| 919 |
$cache[$name]['pagesincategory_pages'] = $pageCount; |
| 920 |
$cache[$name]['pagesincategory_subcats'] = $subcatCount; |
| 921 |
$cache[$name]['pagesincategory_files'] = $fileCount; |
| 922 |
} |
| 923 |
|
| 924 |
$count = $cache[$name][$type]; |
| 925 |
return self::formatRaw( $count, $raw, $parser->getTargetLanguage() ); |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
public static function pagesize( $parser, $page = '', $raw = null ) { |
| 938 |
$title = Title::newFromText( $page ); |
| 939 |
|
| 940 |
if ( !is_object( $title ) || $title->isExternal() ) { |
| 941 |
return self::formatRaw( 0, $raw, $parser->getTargetLanguage() ); |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
$rev = self::getCachedRevisionObject( $parser, $title, ParserOutputFlags::VARY_REVISION_SHA1 ); |
| 946 |
$length = $rev ? $rev->getSize() : 0; |
| 947 |
if ( $length === null ) { |
| 948 |
|
| 949 |
$length = 0; |
| 950 |
} |
| 951 |
return self::formatRaw( $length, $raw, $parser->getTargetLanguage() ); |
| 952 |
} |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
public static function protectionlevel( $parser, $type = '', $title = '' ) { |
| 967 |
$titleObject = Title::newFromText( $title ) ?? $parser->getTitle(); |
| 968 |
$restrictionStore = MediaWikiServices::getInstance()->getRestrictionStore(); |
| 969 |
if ( $restrictionStore->areRestrictionsLoaded( $titleObject ) || $parser->incrementExpensiveFunctionCount() ) { |
| 970 |
$restrictions = $restrictionStore->getRestrictions( $titleObject, strtolower( $type ) ); |
| 971 |
|
| 972 |
|
| 973 |
return implode( ',', $restrictions ); |
| 974 |
} |
| 975 |
return ''; |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
public static function protectionexpiry( $parser, $type = '', $title = '' ) { |
| 991 |
$titleObject = Title::newFromText( $title ) ?? $parser->getTitle(); |
| 992 |
$restrictionStore = MediaWikiServices::getInstance()->getRestrictionStore(); |
| 993 |
if ( $restrictionStore->areRestrictionsLoaded( $titleObject ) || $parser->incrementExpensiveFunctionCount() ) { |
| 994 |
|
| 995 |
|
| 996 |
return $restrictionStore->getRestrictionExpiry( $titleObject, strtolower( $type ) ) ?? ''; |
| 997 |
} |
| 998 |
return ''; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
public static function language( $parser, $code = '', $inLanguage = '' ) { |
| 1010 |
if ( $code === '' ) { |
| 1011 |
$code = $parser->getTargetLanguage()->getCode(); |
| 1012 |
} |
| 1013 |
if ( $inLanguage === '' ) { |
| 1014 |
$inLanguage = LanguageNameUtils::AUTONYMS; |
| 1015 |
} |
| 1016 |
$lang = MediaWikiServices::getInstance() |
| 1017 |
->getLanguageNameUtils() |
| 1018 |
->getLanguageName( $code, $inLanguage ); |
| 1019 |
return $lang !== '' ? $lang : LanguageCode::bcp47( $code ); |
| 1020 |
} |
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
public static function dir( Parser $parser, string $code = '', string $arg = '' ): string { |
| 1034 |
static $magicWords = null; |
| 1035 |
$languageFactory = MediaWikiServices::getInstance()->getLanguageFactory(); |
| 1036 |
|
| 1037 |
if ( $code === '' ) { |
| 1038 |
$lang = $parser->getTargetLanguage(); |
| 1039 |
} else { |
| 1040 |
if ( $arg !== '' ) { |
| 1041 |
if ( $magicWords === null ) { |
| 1042 |
$magicWords = $parser->getMagicWordFactory()->newArray( [ 'language_option_bcp47' ] ); |
| 1043 |
} |
| 1044 |
if ( $magicWords->matchStartToEnd( $arg ) === 'language_option_bcp47' ) { |
| 1045 |
|
| 1046 |
$code = new Bcp47CodeValue( $code ); |
| 1047 |
} |
| 1048 |
} |
| 1049 |
try { |
| 1050 |
$lang = $languageFactory->getLanguage( $code ); |
| 1051 |
} catch ( InvalidArgumentException $ex ) { |
| 1052 |
$parser->addTrackingCategory( 'bad-language-code-category' ); |
| 1053 |
return 'ltr'; |
| 1054 |
} |
| 1055 |
} |
| 1056 |
return $lang->getDir(); |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
public static function bcp47( Parser $parser, string $code = '' ): string { |
| 1068 |
if ( $code === '' ) { |
| 1069 |
return $parser->getTargetLanguage()->toBcp47Code(); |
| 1070 |
} else { |
| 1071 |
return LanguageCode::bcp47( $code ); |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
public static function pad( |
| 1085 |
$parser, $string, $length, $padding = '0', $direction = STR_PAD_RIGHT |
| 1086 |
) { |
| 1087 |
$padding = $parser->killMarkers( $padding ); |
| 1088 |
$lengthOfPadding = mb_strlen( $padding ); |
| 1089 |
if ( $lengthOfPadding == 0 ) { |
| 1090 |
return $string; |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
$length = min( (int)$length, 500 ) - mb_strlen( $string ); |
| 1095 |
if ( $length <= 0 ) { |
| 1096 |
|
| 1097 |
return $string; |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
$finalPadding = ''; |
| 1103 |
while ( $length > 0 ) { |
| 1104 |
|
| 1105 |
|
| 1106 |
$finalPadding .= mb_substr( $padding, 0, $length ); |
| 1107 |
$length -= $lengthOfPadding; |
| 1108 |
} |
| 1109 |
|
| 1110 |
if ( $direction == STR_PAD_LEFT ) { |
| 1111 |
return $finalPadding . $string; |
| 1112 |
} else { |
| 1113 |
return $string . $finalPadding; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
public static function padleft( $parser, $string = '', $length = 0, $padding = '0' ) { |
| 1118 |
return self::pad( $parser, $string, $length, $padding, STR_PAD_LEFT ); |
| 1119 |
} |
| 1120 |
|
| 1121 |
public static function padright( $parser, $string = '', $length = 0, $padding = '0' ) { |
| 1122 |
return self::pad( $parser, $string, $length, $padding ); |
| 1123 |
} |
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
public static function anchorencode( $parser, $text ) { |
| 1131 |
$text = $parser->killMarkers( $text ); |
| 1132 |
$section = substr( $parser->guessSectionNameFromWikiText( $text ), 1 ); |
| 1133 |
$encodedSection = Sanitizer::safeEncodeAttribute( $section ); |
| 1134 |
|
| 1135 |
return str_replace( '_', '_', $encodedSection ); |
| 1136 |
} |
| 1137 |
|
| 1138 |
public static function special( $parser, $text ) { |
| 1139 |
[ $page, $subpage ] = MediaWikiServices::getInstance()->getSpecialPageFactory()-> |
| 1140 |
resolveAlias( $text ); |
| 1141 |
if ( $page ) { |
| 1142 |
$title = SpecialPage::getTitleFor( $page, $subpage ); |
| 1143 |
return $title->getPrefixedText(); |
| 1144 |
} else { |
| 1145 |
|
| 1146 |
$title = Title::makeTitleSafe( NS_SPECIAL, $text ); |
| 1147 |
return $title ? $title->getPrefixedText() : self::special( $parser, 'Badtitle' ); |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
public static function speciale( $parser, $text ) { |
| 1152 |
return wfUrlencode( str_replace( ' ', '_', self::special( $parser, $text ) ) ); |
| 1153 |
} |
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
public static function defaultsort( $parser, $text, $uarg = '' ) { |
| 1164 |
static $magicWords = null; |
| 1165 |
if ( $magicWords === null ) { |
| 1166 |
$magicWords = $parser->getMagicWordFactory()->newArray( |
| 1167 |
[ 'defaultsort_noerror', 'defaultsort_noreplace' ] ); |
| 1168 |
} |
| 1169 |
$arg = $magicWords->matchStartToEnd( $uarg ); |
| 1170 |
|
| 1171 |
$text = trim( $text ); |
| 1172 |
if ( strlen( $text ) == 0 ) { |
| 1173 |
return ''; |
| 1174 |
} |
| 1175 |
$old = $parser->getOutput()->getPageProperty( 'defaultsort' ); |
| 1176 |
if ( $old === null || $arg !== 'defaultsort_noreplace' ) { |
| 1177 |
$parser->getOutput()->setPageProperty( 'defaultsort', $text ); |
| 1178 |
} |
| 1179 |
|
| 1180 |
if ( $old === null || $old == $text || $arg ) { |
| 1181 |
return ''; |
| 1182 |
} else { |
| 1183 |
$converter = $parser->getTargetLanguageConverter(); |
| 1184 |
return '<span class="error">' . |
| 1185 |
$parser->msg( 'duplicate-defaultsort', |
| 1186 |
|
| 1187 |
$converter->markNoConversion( wfEscapeWikiText( $old ) ), |
| 1188 |
$converter->markNoConversion( wfEscapeWikiText( $text ) ) |
| 1189 |
)->text() . |
| 1190 |
'</span>'; |
| 1191 |
} |
| 1192 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
public static function filepath( $parser, $name = '', $argA = '', $argB = '' ) { |
| 1206 |
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $name ); |
| 1207 |
|
| 1208 |
if ( $argA == 'nowiki' ) { |
| 1209 |
|
| 1210 |
$isNowiki = true; |
| 1211 |
$parsedWidthParam = $parser->parseWidthParam( $argB ); |
| 1212 |
} else { |
| 1213 |
|
| 1214 |
$parsedWidthParam = $parser->parseWidthParam( $argA ); |
| 1215 |
$isNowiki = ( $argB == 'nowiki' ); |
| 1216 |
} |
| 1217 |
|
| 1218 |
if ( $file ) { |
| 1219 |
$url = $file->getFullUrl(); |
| 1220 |
|
| 1221 |
|
| 1222 |
if ( count( $parsedWidthParam ) ) { |
| 1223 |
$mto = $file->transform( $parsedWidthParam ); |
| 1224 |
|
| 1225 |
if ( $mto && !$mto->isError() ) { |
| 1226 |
|
| 1227 |
$urlUtils = MediaWikiServices::getInstance()->getUrlUtils(); |
| 1228 |
$url = $urlUtils->expand( $mto->getUrl(), PROTO_RELATIVE ) ?? false; |
| 1229 |
} |
| 1230 |
} |
| 1231 |
if ( $isNowiki ) { |
| 1232 |
return [ $url, 'nowiki' => true ]; |
| 1233 |
} |
| 1234 |
return $url; |
| 1235 |
} else { |
| 1236 |
return ''; |
| 1237 |
} |
| 1238 |
} |
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
public static function tagObj( $parser, $frame, $args ) { |
| 1248 |
if ( !count( $args ) ) { |
| 1249 |
return ''; |
| 1250 |
} |
| 1251 |
$tagName = strtolower( trim( $frame->expand( array_shift( $args ) ) ) ); |
| 1252 |
$processNowiki = $parser->tagNeedsNowikiStrippedInTagPF( $tagName ) ? PPFrame::PROCESS_NOWIKI : 0; |
| 1253 |
|
| 1254 |
if ( count( $args ) ) { |
| 1255 |
$inner = $frame->expand( array_shift( $args ), $processNowiki ); |
| 1256 |
} else { |
| 1257 |
$inner = null; |
| 1258 |
} |
| 1259 |
|
| 1260 |
$attributes = []; |
| 1261 |
foreach ( $args as $arg ) { |
| 1262 |
$bits = $arg->splitArg(); |
| 1263 |
if ( strval( $bits['index'] ) === '' ) { |
| 1264 |
$name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) ); |
| 1265 |
$value = trim( $frame->expand( $bits['value'] ) ); |
| 1266 |
if ( preg_match( '/^(?:["\'](.+)["\']|""|\'\')$/s', $value, $m ) ) { |
| 1267 |
$value = $m[1] ?? ''; |
| 1268 |
} |
| 1269 |
$attributes[$name] = $value; |
| 1270 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
$stripList = $parser->getStripList(); |
| 1274 |
if ( !in_array( $tagName, $stripList ) ) { |
| 1275 |
|
| 1276 |
$attrText = ''; |
| 1277 |
foreach ( $attributes as $name => $value ) { |
| 1278 |
$attrText .= ' ' . htmlspecialchars( $name ) . |
| 1279 |
'="' . htmlspecialchars( $value, ENT_COMPAT ) . '"'; |
| 1280 |
} |
| 1281 |
if ( $inner === null ) { |
| 1282 |
return "<$tagName$attrText/>"; |
| 1283 |
} |
| 1284 |
return "<$tagName$attrText>$inner</$tagName>"; |
| 1285 |
} |
| 1286 |
|
| 1287 |
$params = [ |
| 1288 |
'name' => $tagName, |
| 1289 |
'inner' => $inner, |
| 1290 |
'attributes' => $attributes, |
| 1291 |
'close' => "</$tagName>", |
| 1292 |
]; |
| 1293 |
return $parser->extensionSubstitution( $params, $frame ); |
| 1294 |
} |
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
private static function getCachedRevisionObject( $parser, $title, $vary ) { |
| 1310 |
if ( !$title ) { |
| 1311 |
return null; |
| 1312 |
} |
| 1313 |
|
| 1314 |
$revisionRecord = null; |
| 1315 |
|
| 1316 |
$isSelfReferential = $title->equals( $parser->getTitle() ); |
| 1317 |
if ( $isSelfReferential ) { |
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
$parserRevisionRecord = $parser->getRevisionRecordObject(); |
| 1328 |
if ( $parserRevisionRecord && $parserRevisionRecord->isCurrent() ) { |
| 1329 |
$revisionRecord = $parserRevisionRecord; |
| 1330 |
} |
| 1331 |
} |
| 1332 |
|
| 1333 |
$parserOutput = $parser->getOutput(); |
| 1334 |
if ( !$revisionRecord ) { |
| 1335 |
if ( |
| 1336 |
!$parser->isCurrentRevisionOfTitleCached( $title ) && |
| 1337 |
!$parser->incrementExpensiveFunctionCount() |
| 1338 |
) { |
| 1339 |
return null; |
| 1340 |
} |
| 1341 |
|
| 1342 |
$revisionRecord = $parser->fetchCurrentRevisionRecordOfTitle( $title ); |
| 1343 |
if ( !$revisionRecord ) { |
| 1344 |
|
| 1345 |
$revisionRecord = null; |
| 1346 |
} |
| 1347 |
|
| 1348 |
$parserOutput->addTemplate( |
| 1349 |
$title, |
| 1350 |
$revisionRecord ? $revisionRecord->getPageId() : 0, |
| 1351 |
$revisionRecord ? $revisionRecord->getId() : 0 |
| 1352 |
); |
| 1353 |
} |
| 1354 |
|
| 1355 |
if ( $isSelfReferential ) { |
| 1356 |
wfDebug( __METHOD__ . ": used current revision, setting $vary" ); |
| 1357 |
|
| 1358 |
$parserOutput->setOutputFlag( $vary ); |
| 1359 |
if ( $vary === ParserOutputFlags::VARY_REVISION_SHA1 && $revisionRecord ) { |
| 1360 |
try { |
| 1361 |
$sha1 = $revisionRecord->getSha1(); |
| 1362 |
} catch ( RevisionAccessException $e ) { |
| 1363 |
$sha1 = null; |
| 1364 |
} |
| 1365 |
$parserOutput->setRevisionUsedSha1Base36( $sha1 ); |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
return $revisionRecord; |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
public static function pageid( $parser, $title = null ) { |
| 1380 |
$t = self::makeTitle( $parser, $title ); |
| 1381 |
if ( !$t ) { |
| 1382 |
return ''; |
| 1383 |
} elseif ( !$t->canExist() || $t->isExternal() ) { |
| 1384 |
return 0; |
| 1385 |
} |
| 1386 |
|
| 1387 |
$parserOutput = $parser->getOutput(); |
| 1388 |
|
| 1389 |
if ( $t->equals( $parser->getTitle() ) ) { |
| 1390 |
|
| 1391 |
|
| 1392 |
$parserOutput->setOutputFlag( ParserOutputFlags::VARY_PAGE_ID ); |
| 1393 |
$id = $parser->getTitle()->getArticleID(); |
| 1394 |
if ( $id ) { |
| 1395 |
$parserOutput->setSpeculativePageIdUsed( $id ); |
| 1396 |
} |
| 1397 |
|
| 1398 |
return $id; |
| 1399 |
} |
| 1400 |
|
| 1401 |
|
| 1402 |
$linkCache = MediaWikiServices::getInstance()->getLinkCache(); |
| 1403 |
$pdbk = $t->getPrefixedDBkey(); |
| 1404 |
$id = $linkCache->getGoodLinkID( $pdbk ); |
| 1405 |
if ( $id != 0 || $linkCache->isBadLink( $pdbk ) ) { |
| 1406 |
$parserOutput->addLink( $t, $id ); |
| 1407 |
|
| 1408 |
return $id; |
| 1409 |
} |
| 1410 |
|
| 1411 |
|
| 1412 |
if ( $parser->incrementExpensiveFunctionCount() ) { |
| 1413 |
$id = $t->getArticleID(); |
| 1414 |
$parserOutput->addLink( $t, $id ); |
| 1415 |
|
| 1416 |
return $id; |
| 1417 |
} |
| 1418 |
|
| 1419 |
return null; |
| 1420 |
} |
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
public static function revisionid( $parser, $title = null ) { |
| 1430 |
$t = self::makeTitle( $parser, $title ); |
| 1431 |
if ( $t === null || $t->isExternal() ) { |
| 1432 |
return ''; |
| 1433 |
} |
| 1434 |
|
| 1435 |
$services = MediaWikiServices::getInstance(); |
| 1436 |
if ( |
| 1437 |
$t->equals( $parser->getTitle() ) && |
| 1438 |
$services->getMainConfig()->get( MainConfigNames::MiserMode ) && |
| 1439 |
!$parser->getOptions()->getInterfaceMessage() && |
| 1440 |
|
| 1441 |
$services->getNamespaceInfo()->isSubject( $t->getNamespace() ) |
| 1442 |
) { |
| 1443 |
|
| 1444 |
|
| 1445 |
if ( $parser->getRevisionId() || $parser->getOptions()->getSpeculativeRevId() ) { |
| 1446 |
return '-'; |
| 1447 |
} else { |
| 1448 |
$parser->getOutput()->setOutputFlag( ParserOutputFlags::VARY_REVISION_EXISTS ); |
| 1449 |
return ''; |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
if ( $t->equals( $parser->getTitle() ) && $title === null ) { |
| 1457 |
|
| 1458 |
|
| 1459 |
$parser->getOutput()->setOutputFlag( ParserOutputFlags::VARY_REVISION_ID ); |
| 1460 |
$id = $parser->getRevisionId(); |
| 1461 |
if ( $id === 0 ) { |
| 1462 |
$rev = $parser->getRevisionRecordObject(); |
| 1463 |
if ( $rev ) { |
| 1464 |
$id = $rev->getId(); |
| 1465 |
} |
| 1466 |
} |
| 1467 |
if ( !$id ) { |
| 1468 |
$id = $parser->getOptions()->getSpeculativeRevId(); |
| 1469 |
if ( $id ) { |
| 1470 |
$parser->getOutput()->setSpeculativeRevIdUsed( $id ); |
| 1471 |
} |
| 1472 |
} |
| 1473 |
return (string)$id; |
| 1474 |
} |
| 1475 |
$rev = self::getCachedRevisionObject( $parser, $t, ParserOutputFlags::VARY_REVISION_ID ); |
| 1476 |
return $rev ? $rev->getId() : ''; |
| 1477 |
} |
| 1478 |
|
| 1479 |
private static function getRevisionTimestampSubstring( |
| 1480 |
Parser $parser, |
| 1481 |
Title $title, |
| 1482 |
int $start, |
| 1483 |
int $len, |
| 1484 |
int $mtts |
| 1485 |
): string { |
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
if ( $title->equals( $parser->getTitle() ) && !$parser->getOptions()->getInterfaceMessage() ) { |
| 1493 |
|
| 1494 |
$resNow = substr( $parser->getRevisionTimestamp(), $start, $len ); |
| 1495 |
|
| 1496 |
if ( !$parser->getRevisionRecordObject() ) { |
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
$resThen = substr( |
| 1502 |
$parser->getContentLanguage()->userAdjust( wfTimestamp( TS_MW, time() + $mtts ), '' ), |
| 1503 |
$start, |
| 1504 |
$len |
| 1505 |
); |
| 1506 |
|
| 1507 |
if ( $resNow !== $resThen ) { |
| 1508 |
|
| 1509 |
|
| 1510 |
$parser->getOutput()->setOutputFlag( ParserOutputFlags::VARY_REVISION_TIMESTAMP ); |
| 1511 |
} |
| 1512 |
} |
| 1513 |
|
| 1514 |
return $resNow; |
| 1515 |
} else { |
| 1516 |
$rev = self::getCachedRevisionObject( $parser, $title, ParserOutputFlags::VARY_REVISION_TIMESTAMP ); |
| 1517 |
if ( !$rev ) { |
| 1518 |
return ''; |
| 1519 |
} |
| 1520 |
$resNow = substr( |
| 1521 |
$parser->getContentLanguage()->userAdjust( $rev->getTimestamp(), '' ), $start, $len |
| 1522 |
); |
| 1523 |
return $resNow; |
| 1524 |
} |
| 1525 |
} |
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
public static function revisionday( $parser, $title = null ) { |
| 1535 |
$t = self::makeTitle( $parser, $title ); |
| 1536 |
if ( $t === null || $t->isExternal() ) { |
| 1537 |
return ''; |
| 1538 |
} |
| 1539 |
return strval( (int)self::getRevisionTimestampSubstring( |
| 1540 |
$parser, $t, 6, 2, self::MAX_TTS |
| 1541 |
) ); |
| 1542 |
} |
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
public static function revisionday2( $parser, $title = null ) { |
| 1552 |
$t = self::makeTitle( $parser, $title ); |
| 1553 |
if ( $t === null || $t->isExternal() ) { |
| 1554 |
return ''; |
| 1555 |
} |
| 1556 |
return self::getRevisionTimestampSubstring( |
| 1557 |
$parser, $t, 6, 2, self::MAX_TTS |
| 1558 |
); |
| 1559 |
} |
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
public static function revisionmonth( $parser, $title = null ) { |
| 1569 |
$t = self::makeTitle( $parser, $title ); |
| 1570 |
if ( $t === null || $t->isExternal() ) { |
| 1571 |
return ''; |
| 1572 |
} |
| 1573 |
return self::getRevisionTimestampSubstring( |
| 1574 |
$parser, $t, 4, 2, self::MAX_TTS |
| 1575 |
); |
| 1576 |
} |
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
public static function revisionmonth1( $parser, $title = null ) { |
| 1586 |
$t = self::makeTitle( $parser, $title ); |
| 1587 |
if ( $t === null || $t->isExternal() ) { |
| 1588 |
return ''; |
| 1589 |
} |
| 1590 |
return strval( (int)self::getRevisionTimestampSubstring( |
| 1591 |
$parser, $t, 4, 2, self::MAX_TTS |
| 1592 |
) ); |
| 1593 |
} |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
public static function revisionyear( $parser, $title = null ) { |
| 1603 |
$t = self::makeTitle( $parser, $title ); |
| 1604 |
if ( $t === null || $t->isExternal() ) { |
| 1605 |
return ''; |
| 1606 |
} |
| 1607 |
return self::getRevisionTimestampSubstring( |
| 1608 |
$parser, $t, 0, 4, self::MAX_TTS |
| 1609 |
); |
| 1610 |
} |
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
public static function revisiontimestamp( $parser, $title = null ) { |
| 1620 |
$t = self::makeTitle( $parser, $title ); |
| 1621 |
if ( $t === null || $t->isExternal() ) { |
| 1622 |
return ''; |
| 1623 |
} |
| 1624 |
return self::getRevisionTimestampSubstring( |
| 1625 |
$parser, $t, 0, 14, self::MAX_TTS |
| 1626 |
); |
| 1627 |
} |
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
public static function revisionuser( $parser, $title = null ) { |
| 1637 |
$t = self::makeTitle( $parser, $title ); |
| 1638 |
if ( $t === null || $t->isExternal() ) { |
| 1639 |
return ''; |
| 1640 |
} |
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
if ( $t->equals( $parser->getTitle() ) ) { |
| 1645 |
|
| 1646 |
$parser->getOutput()->setOutputFlag( ParserOutputFlags::VARY_USER ); |
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
return (string)$parser->getRevisionUser(); |
| 1651 |
} |
| 1652 |
|
| 1653 |
$rev = self::getCachedRevisionObject( $parser, $t, ParserOutputFlags::VARY_USER ); |
| 1654 |
$user = ( $rev !== null ) ? $rev->getUser() : null; |
| 1655 |
return $user ? $user->getName() : ''; |
| 1656 |
} |
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
public static function cascadingsources( $parser, $title = '' ) { |
| 1671 |
$titleObject = Title::newFromText( $title ) ?? $parser->getTitle(); |
| 1672 |
$restrictionStore = MediaWikiServices::getInstance()->getRestrictionStore(); |
| 1673 |
if ( $restrictionStore->areCascadeProtectionSourcesLoaded( $titleObject ) |
| 1674 |
|| $parser->incrementExpensiveFunctionCount() |
| 1675 |
) { |
| 1676 |
$names = []; |
| 1677 |
$sources = $restrictionStore->getCascadeProtectionSources( $titleObject ); |
| 1678 |
$titleFormatter = MediaWikiServices::getInstance()->getTitleFormatter(); |
| 1679 |
foreach ( $sources[0] as $sourcePageIdentity ) { |
| 1680 |
$names[] = $titleFormatter->getPrefixedText( $sourcePageIdentity ); |
| 1681 |
} |
| 1682 |
return implode( '|', $names ); |
| 1683 |
} |
| 1684 |
return ''; |
| 1685 |
} |
| 1686 |
} |
| 1687 |
|
| 1688 |
|
| 1689 |
class_alias( CoreParserFunctions::class, 'CoreParserFunctions' ); |