| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\ParserFunctions; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use DateTimeZone; |
| 7 |
use Exception; |
| 8 |
use MediaWiki\Cache\LinkCache; |
| 9 |
use MediaWiki\Config\Config; |
| 10 |
use MediaWiki\HookContainer\HookContainer; |
| 11 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 12 |
use MediaWiki\Languages\LanguageFactory; |
| 13 |
use MediaWiki\Languages\LanguageNameUtils; |
| 14 |
use MediaWiki\Parser\Parser; |
| 15 |
use MediaWiki\Parser\PPFrame; |
| 16 |
use MediaWiki\Parser\PPNode; |
| 17 |
use MediaWiki\Parser\Sanitizer; |
| 18 |
use MediaWiki\SpecialPage\SpecialPageFactory; |
| 19 |
use MediaWiki\Title\Title; |
| 20 |
use MediaWiki\Utils\MWTimestamp; |
| 21 |
use RepoGroup; |
| 22 |
use StringUtils; |
| 23 |
use Wikimedia\RequestTimeout\TimeoutException; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class ParserFunctions { |
| 31 |
|
| 32 |
private static $mExprParser = null; |
| 33 |
|
| 34 |
private static $mTimeCache = []; |
| 35 |
|
| 36 |
private static $mTimeChars = 0; |
| 37 |
|
| 38 |
|
| 39 |
private const MAX_TIME_CHARS = 6000; |
| 40 |
|
| 41 |
|
| 42 |
private $config; |
| 43 |
|
| 44 |
|
| 45 |
private $hookContainer; |
| 46 |
|
| 47 |
|
| 48 |
private $languageConverterFactory; |
| 49 |
|
| 50 |
|
| 51 |
private $languageFactory; |
| 52 |
|
| 53 |
|
| 54 |
private $languageNameUtils; |
| 55 |
|
| 56 |
|
| 57 |
private $linkCache; |
| 58 |
|
| 59 |
|
| 60 |
private $repoGroup; |
| 61 |
|
| 62 |
|
| 63 |
private $specialPageFactory; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function __construct( |
| 76 |
Config $config, |
| 77 |
HookContainer $hookContainer, |
| 78 |
LanguageConverterFactory $languageConverterFactory, |
| 79 |
LanguageFactory $languageFactory, |
| 80 |
LanguageNameUtils $languageNameUtils, |
| 81 |
LinkCache $linkCache, |
| 82 |
RepoGroup $repoGroup, |
| 83 |
SpecialPageFactory $specialPageFactory |
| 84 |
) { |
| 85 |
$this->config = $config; |
| 86 |
$this->hookContainer = $hookContainer; |
| 87 |
$this->languageConverterFactory = $languageConverterFactory; |
| 88 |
$this->languageFactory = $languageFactory; |
| 89 |
$this->languageNameUtils = $languageNameUtils; |
| 90 |
$this->linkCache = $linkCache; |
| 91 |
$this->repoGroup = $repoGroup; |
| 92 |
$this->specialPageFactory = $specialPageFactory; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
private static function &getExprParser() { |
| 99 |
if ( self::$mExprParser === null ) { |
| 100 |
self::$mExprParser = new ExprParser; |
| 101 |
} |
| 102 |
return self::$mExprParser; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function expr( Parser $parser, $expr = '' ) { |
| 115 |
try { |
| 116 |
return self::getExprParser()->doExpression( $expr ); |
| 117 |
} catch ( ExprError $e ) { |
| 118 |
return '<strong class="error">' . htmlspecialchars( $e->getUserFriendlyMessage() ) . '</strong>'; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function ifexpr( Parser $parser, PPFrame $frame, array $args ) { |
| 133 |
$expr = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 134 |
$then = $args[1] ?? ''; |
| 135 |
$else = $args[2] ?? ''; |
| 136 |
|
| 137 |
try { |
| 138 |
$result = self::getExprParser()->doExpression( $expr ); |
| 139 |
if ( is_numeric( $result ) ) { |
| 140 |
$result = (float)$result; |
| 141 |
} |
| 142 |
$result = $result ? $then : $else; |
| 143 |
} catch ( ExprError $e ) { |
| 144 |
return '<strong class="error">' . htmlspecialchars( $e->getUserFriendlyMessage() ) . '</strong>'; |
| 145 |
} |
| 146 |
|
| 147 |
if ( is_object( $result ) ) { |
| 148 |
$result = trim( $frame->expand( $result ) ); |
| 149 |
} |
| 150 |
|
| 151 |
return $result; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function if( Parser $parser, PPFrame $frame, array $args ) { |
| 165 |
$test = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 166 |
if ( $test !== '' ) { |
| 167 |
return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; |
| 168 |
} else { |
| 169 |
return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : ''; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function ifeq( Parser $parser, PPFrame $frame, array $args ) { |
| 184 |
$left = isset( $args[0] ) ? self::decodeTrimExpand( $args[0], $frame ) : ''; |
| 185 |
$right = isset( $args[1] ) ? self::decodeTrimExpand( $args[1], $frame ) : ''; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
if ( $left == $right ) { |
| 190 |
return isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : ''; |
| 191 |
} else { |
| 192 |
return isset( $args[3] ) ? trim( $frame->expand( $args[3] ) ) : ''; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public function iferror( Parser $parser, PPFrame $frame, array $args ) { |
| 210 |
$test = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 211 |
$then = $args[1] ?? false; |
| 212 |
$else = $args[2] ?? false; |
| 213 |
|
| 214 |
if ( preg_match( |
| 215 |
'/<(?:strong|span|p|div)\s(?:[^\s>]*\s+)*?class="(?:[^"\s>]*\s+)*?error(?:\s[^">]*)?"/', |
| 216 |
$test ) |
| 217 |
) { |
| 218 |
$result = $then; |
| 219 |
} elseif ( $else === false ) { |
| 220 |
$result = $test; |
| 221 |
} else { |
| 222 |
$result = $else; |
| 223 |
} |
| 224 |
if ( $result === false ) { |
| 225 |
return ''; |
| 226 |
} |
| 227 |
|
| 228 |
return trim( $frame->expand( $result ) ); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public function switch( Parser $parser, PPFrame $frame, array $args ) { |
| 247 |
if ( count( $args ) === 0 ) { |
| 248 |
return ''; |
| 249 |
} |
| 250 |
$primary = self::decodeTrimExpand( array_shift( $args ), $frame ); |
| 251 |
$found = $defaultFound = false; |
| 252 |
$default = null; |
| 253 |
$lastItemHadNoEquals = false; |
| 254 |
$lastItem = ''; |
| 255 |
$mwDefault = $parser->getMagicWordFactory()->get( 'default' ); |
| 256 |
foreach ( $args as $arg ) { |
| 257 |
$bits = $arg->splitArg(); |
| 258 |
$nameNode = $bits['name']; |
| 259 |
$index = $bits['index']; |
| 260 |
$valueNode = $bits['value']; |
| 261 |
|
| 262 |
if ( $index === '' ) { |
| 263 |
|
| 264 |
$lastItemHadNoEquals = false; |
| 265 |
if ( $found ) { |
| 266 |
|
| 267 |
return trim( $frame->expand( $valueNode ) ); |
| 268 |
} |
| 269 |
$test = self::decodeTrimExpand( $nameNode, $frame ); |
| 270 |
|
| 271 |
if ( $test == $primary ) { |
| 272 |
|
| 273 |
return trim( $frame->expand( $valueNode ) ); |
| 274 |
} |
| 275 |
if ( $defaultFound || $mwDefault->matchStartToEnd( $test ) ) { |
| 276 |
$default = $valueNode; |
| 277 |
$defaultFound = false; |
| 278 |
} |
| 279 |
} else { |
| 280 |
|
| 281 |
|
| 282 |
$lastItemHadNoEquals = true; |
| 283 |
|
| 284 |
$decodedTest = self::decodeTrimExpand( $valueNode, $frame, $lastItem ); |
| 285 |
|
| 286 |
if ( $decodedTest == $primary ) { |
| 287 |
$found = true; |
| 288 |
} elseif ( $mwDefault->matchStartToEnd( $decodedTest ) ) { |
| 289 |
$defaultFound = true; |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
if ( $lastItemHadNoEquals ) { |
| 296 |
return $lastItem; |
| 297 |
} |
| 298 |
if ( $default === null ) { |
| 299 |
return ''; |
| 300 |
} |
| 301 |
return trim( $frame->expand( $default ) ); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public function rel2abs( Parser $parser, $to = '', $from = '' ) { |
| 322 |
$from = trim( $from ); |
| 323 |
if ( $from === '' ) { |
| 324 |
$from = $parser->getTitle()->getPrefixedText(); |
| 325 |
} |
| 326 |
|
| 327 |
$to = rtrim( $to, ' /' ); |
| 328 |
|
| 329 |
|
| 330 |
if ( $to === '' || $to === '.' ) { |
| 331 |
return $from; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
if ( substr( $to, 0, 1 ) !== '/' && |
| 336 |
substr( $to, 0, 2 ) !== './' && |
| 337 |
substr( $to, 0, 3 ) !== '../' && |
| 338 |
$to !== '..' |
| 339 |
) { |
| 340 |
$from = ''; |
| 341 |
} |
| 342 |
|
| 343 |
$fullPath = '/' . $from . '/' . $to . '/'; |
| 344 |
|
| 345 |
|
| 346 |
$fullPath = preg_replace( '!/(\./)+!', '/', $fullPath ); |
| 347 |
|
| 348 |
|
| 349 |
$fullPath = preg_replace( '!/{2,}!', '/', $fullPath ); |
| 350 |
|
| 351 |
|
| 352 |
$fullPath = trim( $fullPath, '/' ); |
| 353 |
$exploded = explode( '/', $fullPath ); |
| 354 |
$newExploded = []; |
| 355 |
|
| 356 |
foreach ( $exploded as $current ) { |
| 357 |
if ( $current === '..' ) { |
| 358 |
if ( !count( $newExploded ) ) { |
| 359 |
|
| 360 |
$msg = wfMessage( 'pfunc_rel2abs_invalid_depth', $fullPath ) |
| 361 |
->inContentLanguage()->escaped(); |
| 362 |
return '<strong class="error">' . $msg . '</strong>'; |
| 363 |
} |
| 364 |
|
| 365 |
array_pop( $newExploded ); |
| 366 |
} else { |
| 367 |
|
| 368 |
$newExploded[] = $current; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
return implode( '/', $newExploded ); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
private function ifexistInternal( Parser $parser, $titletext ): bool { |
| 383 |
$title = Title::newFromText( $titletext ); |
| 384 |
$this->languageConverterFactory->getLanguageConverter( $parser->getContentLanguage() ) |
| 385 |
->findVariantLink( $titletext, $title, true ); |
| 386 |
if ( !$title ) { |
| 387 |
return false; |
| 388 |
} |
| 389 |
|
| 390 |
if ( $title->getNamespace() === NS_MEDIA ) { |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
if ( !$parser->incrementExpensiveFunctionCount() ) { |
| 395 |
return false; |
| 396 |
} |
| 397 |
$file = $this->repoGroup->findFile( $title ); |
| 398 |
if ( !$file ) { |
| 399 |
$parser->getOutput()->addImage( |
| 400 |
$title->getDBKey(), false, false ); |
| 401 |
return false; |
| 402 |
} |
| 403 |
$parser->getOutput()->addImage( |
| 404 |
$file->getName(), $file->getTimestamp(), $file->getSha1() ); |
| 405 |
return $file->exists(); |
| 406 |
} |
| 407 |
if ( $title->isSpecialPage() ) { |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
return $this->specialPageFactory->exists( $title->getDBkey() ); |
| 413 |
} |
| 414 |
if ( $title->isExternal() ) { |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
return false; |
| 420 |
} |
| 421 |
$pdbk = $title->getPrefixedDBkey(); |
| 422 |
$id = $this->linkCache->getGoodLinkID( $pdbk ); |
| 423 |
if ( $id !== 0 ) { |
| 424 |
$parser->getOutput()->addLink( $title, $id ); |
| 425 |
return true; |
| 426 |
} |
| 427 |
if ( $this->linkCache->isBadLink( $pdbk ) ) { |
| 428 |
$parser->getOutput()->addLink( $title, 0 ); |
| 429 |
return false; |
| 430 |
} |
| 431 |
if ( !$parser->incrementExpensiveFunctionCount() ) { |
| 432 |
return false; |
| 433 |
} |
| 434 |
$id = $title->getArticleID(); |
| 435 |
$parser->getOutput()->addLink( $title, $id ); |
| 436 |
|
| 437 |
|
| 438 |
return $title->exists(); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public function ifexist( Parser $parser, PPFrame $frame, array $args ) { |
| 452 |
$title = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 453 |
$then = $args[1] ?? null; |
| 454 |
$else = $args[2] ?? null; |
| 455 |
|
| 456 |
$result = $this->ifexistInternal( $parser, $title ) ? $then : $else; |
| 457 |
if ( $result === null ) { |
| 458 |
return ''; |
| 459 |
} |
| 460 |
return trim( $frame->expand( $result ) ); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
private function timeCommon( |
| 475 |
Parser $parser, PPFrame $frame, $format, $date, $language, $local |
| 476 |
) { |
| 477 |
$this->hookContainer->register( |
| 478 |
'ParserClearState', |
| 479 |
static function () { |
| 480 |
self::$mTimeChars = 0; |
| 481 |
} |
| 482 |
); |
| 483 |
|
| 484 |
if ( $date === '' ) { |
| 485 |
$cacheKey = $parser->getOptions()->getTimestamp(); |
| 486 |
$timestamp = new MWTimestamp( $cacheKey ); |
| 487 |
$date = $timestamp->getTimestamp( TS_ISO_8601 ); |
| 488 |
$useTTL = true; |
| 489 |
} else { |
| 490 |
$cacheKey = $date; |
| 491 |
$useTTL = false; |
| 492 |
} |
| 493 |
if ( isset( self::$mTimeCache[$format][$cacheKey][$language][$local] ) ) { |
| 494 |
$cachedVal = self::$mTimeCache[$format][$cacheKey][$language][$local]; |
| 495 |
if ( $useTTL && $cachedVal[1] !== null ) { |
| 496 |
$frame->setTTL( $cachedVal[1] ); |
| 497 |
} |
| 498 |
return $cachedVal[0]; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
$invalidTime = false; |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
try { |
| 510 |
|
| 511 |
|
| 512 |
$utc = new DateTimeZone( 'UTC' ); |
| 513 |
|
| 514 |
|
| 515 |
if ( preg_match( '/^[0-9]{4}$/', $date ) ) { |
| 516 |
$date = '00:00 ' . $date; |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
$dateObject = new DateTime( $date, $utc ); |
| 522 |
|
| 523 |
|
| 524 |
if ( $local ) { |
| 525 |
$tz = new DateTimeZone( |
| 526 |
$this->config->get( 'Localtimezone' ) ?? |
| 527 |
date_default_timezone_get() |
| 528 |
); |
| 529 |
} else { |
| 530 |
$tz = $utc; |
| 531 |
} |
| 532 |
$dateObject->setTimezone( $tz ); |
| 533 |
|
| 534 |
$ts = $dateObject->format( 'YmdHis' ); |
| 535 |
|
| 536 |
} catch ( TimeoutException $ex ) { |
| 537 |
|
| 538 |
|
| 539 |
throw $ex; |
| 540 |
} catch ( Exception $ex ) { |
| 541 |
$invalidTime = true; |
| 542 |
} |
| 543 |
|
| 544 |
$ttl = null; |
| 545 |
|
| 546 |
if ( $invalidTime ) { |
| 547 |
$result = '<strong class="error">' . |
| 548 |
wfMessage( 'pfunc_time_error' )->inContentLanguage()->escaped() . |
| 549 |
'</strong>'; |
| 550 |
} else { |
| 551 |
self::$mTimeChars += strlen( $format ); |
| 552 |
if ( self::$mTimeChars > self::MAX_TIME_CHARS ) { |
| 553 |
return '<strong class="error">' . |
| 554 |
wfMessage( 'pfunc_time_too_long' )->inContentLanguage()->escaped() . |
| 555 |
'</strong>'; |
| 556 |
} |
| 557 |
|
| 558 |
if ( $ts < 0 ) { |
| 559 |
return '<strong class="error">' . |
| 560 |
wfMessage( 'pfunc_time_too_small' )->inContentLanguage()->escaped() . |
| 561 |
'</strong>'; |
| 562 |
} |
| 563 |
if ( $ts >= 100000000000000 ) { |
| 564 |
return '<strong class="error">' . |
| 565 |
wfMessage( 'pfunc_time_too_big' )->inContentLanguage()->escaped() . |
| 566 |
'</strong>'; |
| 567 |
} |
| 568 |
|
| 569 |
$langObject = $this->languageFactory->getLanguage( |
| 570 |
$this->normalizeLangCode( $parser, $language ) ); |
| 571 |
$result = $langObject->sprintfDate( $format, $ts, $tz, $ttl ); |
| 572 |
} |
| 573 |
self::$mTimeCache[$format][$cacheKey][$language][$local] = [ $result, $ttl ]; |
| 574 |
if ( $useTTL && $ttl !== null ) { |
| 575 |
$frame->setTTL( $ttl ); |
| 576 |
} |
| 577 |
return $result; |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
private function normalizeLangCode( Parser $parser, string $langCode ) { |
| 588 |
if ( $langCode !== '' && $this->languageNameUtils->isKnownLanguageTag( $langCode ) ) { |
| 589 |
return $langCode; |
| 590 |
} else { |
| 591 |
return $parser->getTargetLanguage()->getCode(); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
public function time( Parser $parser, PPFrame $frame, array $args ) { |
| 608 |
$format = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 609 |
$date = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; |
| 610 |
$language = isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : ''; |
| 611 |
$local = isset( $args[3] ) && trim( $frame->expand( $args[3] ) ); |
| 612 |
return $this->timeCommon( $parser, $frame, $format, $date, $language, $local ); |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
public function localTime( Parser $parser, PPFrame $frame, array $args ) { |
| 629 |
$format = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 630 |
$date = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; |
| 631 |
$language = isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : ''; |
| 632 |
return $this->timeCommon( $parser, $frame, $format, $date, $language, true ); |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
public function timef( Parser $parser, PPFrame $frame, array $args ) { |
| 644 |
return $this->timefCommon( $parser, $frame, $args, false ); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
public function timefl( Parser $parser, PPFrame $frame, array $args ) { |
| 657 |
return $this->timefCommon( $parser, $frame, $args, true ); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
private function timefCommon( Parser $parser, PPFrame $frame, array $args, $local ) { |
| 670 |
$date = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 671 |
$inputType = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; |
| 672 |
|
| 673 |
if ( $inputType !== '' ) { |
| 674 |
$types = $parser->getMagicWordFactory()->newArray( [ |
| 675 |
'timef-time', |
| 676 |
'timef-date', |
| 677 |
'timef-both', |
| 678 |
'timef-pretty' |
| 679 |
] ); |
| 680 |
$id = $types->matchStartToEnd( $inputType ); |
| 681 |
if ( $id === false ) { |
| 682 |
return '<strong class="error">' . |
| 683 |
wfMessage( 'pfunc_timef_bad_format' ) . |
| 684 |
'</strong>'; |
| 685 |
} |
| 686 |
$type = str_replace( 'timef-', '', $id ); |
| 687 |
} else { |
| 688 |
$type = 'both'; |
| 689 |
} |
| 690 |
|
| 691 |
$langCode = isset( $args[2] ) ? trim( $frame->expand( $args[2] ) ) : ''; |
| 692 |
$langCode = $this->normalizeLangCode( $parser, $langCode ); |
| 693 |
$lang = $this->languageFactory->getLanguage( $langCode ); |
| 694 |
$format = $lang->getDateFormatString( $type, 'default' ); |
| 695 |
return $this->timeCommon( $parser, $frame, $format, $date, $langCode, $local ); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
public function titleparts( Parser $parser, $title = '', $parts = 0, $offset = 0 ) { |
| 711 |
$parts = (int)$parts; |
| 712 |
$offset = (int)$offset; |
| 713 |
$ntitle = Title::newFromText( $title ); |
| 714 |
if ( !$ntitle ) { |
| 715 |
return $title; |
| 716 |
} |
| 717 |
|
| 718 |
$bits = explode( '/', $ntitle->getPrefixedText(), 25 ); |
| 719 |
if ( $offset > 0 ) { |
| 720 |
--$offset; |
| 721 |
} |
| 722 |
return implode( '/', array_slice( $bits, $offset, $parts ?: null ) ); |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
private function checkLength( $text ) { |
| 732 |
return ( mb_strlen( $text ) < $this->config->get( 'PFStringLengthLimit' ) ); |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
private function tooLongError() { |
| 740 |
$msg = wfMessage( 'pfunc_string_too_long' ) |
| 741 |
->numParams( $this->config->get( 'PFStringLengthLimit' ) ); |
| 742 |
return '<strong class="error">' . $msg->inContentLanguage()->escaped() . '</strong>'; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
public function runLen( Parser $parser, $inStr = '' ) { |
| 755 |
$inStr = $parser->killMarkers( (string)$inStr ); |
| 756 |
return mb_strlen( $inStr ); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
public function runPos( Parser $parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) { |
| 773 |
$inStr = $parser->killMarkers( (string)$inStr ); |
| 774 |
$inNeedle = $parser->killMarkers( (string)$inNeedle ); |
| 775 |
|
| 776 |
if ( !$this->checkLength( $inStr ) || |
| 777 |
!$this->checkLength( $inNeedle ) ) { |
| 778 |
return $this->tooLongError(); |
| 779 |
} |
| 780 |
|
| 781 |
if ( $inNeedle === '' ) { |
| 782 |
$inNeedle = ' '; |
| 783 |
} |
| 784 |
|
| 785 |
$pos = mb_strpos( $inStr, $inNeedle, min( (int)$inOffset, mb_strlen( $inStr ) ) ); |
| 786 |
if ( $pos === false ) { |
| 787 |
$pos = ''; |
| 788 |
} |
| 789 |
|
| 790 |
return $pos; |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
public function runRPos( Parser $parser, $inStr = '', $inNeedle = '' ) { |
| 806 |
$inStr = $parser->killMarkers( (string)$inStr ); |
| 807 |
$inNeedle = $parser->killMarkers( (string)$inNeedle ); |
| 808 |
|
| 809 |
if ( !$this->checkLength( $inStr ) || |
| 810 |
!$this->checkLength( $inNeedle ) ) { |
| 811 |
return $this->tooLongError(); |
| 812 |
} |
| 813 |
|
| 814 |
if ( $inNeedle === '' ) { |
| 815 |
$inNeedle = ' '; |
| 816 |
} |
| 817 |
|
| 818 |
$pos = mb_strrpos( $inStr, $inNeedle ); |
| 819 |
if ( $pos === false ) { |
| 820 |
$pos = -1; |
| 821 |
} |
| 822 |
|
| 823 |
return $pos; |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
public function runSub( Parser $parser, $inStr = '', $inStart = 0, $inLength = 0 ) { |
| 845 |
$inStr = $parser->killMarkers( (string)$inStr ); |
| 846 |
|
| 847 |
if ( !$this->checkLength( $inStr ) ) { |
| 848 |
return $this->tooLongError(); |
| 849 |
} |
| 850 |
|
| 851 |
if ( (int)$inLength === 0 ) { |
| 852 |
$result = mb_substr( $inStr, (int)$inStart ); |
| 853 |
} else { |
| 854 |
$result = mb_substr( $inStr, (int)$inStart, (int)$inLength ); |
| 855 |
} |
| 856 |
|
| 857 |
return $result; |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
public function runCount( Parser $parser, $inStr = '', $inSubStr = '' ) { |
| 873 |
$inStr = $parser->killMarkers( (string)$inStr ); |
| 874 |
$inSubStr = $parser->killMarkers( (string)$inSubStr ); |
| 875 |
|
| 876 |
if ( !$this->checkLength( $inStr ) || |
| 877 |
!$this->checkLength( $inSubStr ) ) { |
| 878 |
return $this->tooLongError(); |
| 879 |
} |
| 880 |
|
| 881 |
if ( $inSubStr === '' ) { |
| 882 |
$inSubStr = ' '; |
| 883 |
} |
| 884 |
|
| 885 |
$result = mb_substr_count( $inStr, $inSubStr ); |
| 886 |
|
| 887 |
return $result; |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
public function runReplace( Parser $parser, $inStr = '', |
| 907 |
$inReplaceFrom = '', $inReplaceTo = '', $inLimit = -1 ) { |
| 908 |
$inStr = $parser->killMarkers( (string)$inStr ); |
| 909 |
$inReplaceFrom = $parser->killMarkers( (string)$inReplaceFrom ); |
| 910 |
$inReplaceTo = $parser->killMarkers( (string)$inReplaceTo ); |
| 911 |
|
| 912 |
if ( !$this->checkLength( $inStr ) || |
| 913 |
!$this->checkLength( $inReplaceFrom ) || |
| 914 |
!$this->checkLength( $inReplaceTo ) ) { |
| 915 |
return $this->tooLongError(); |
| 916 |
} |
| 917 |
|
| 918 |
if ( $inReplaceFrom === '' ) { |
| 919 |
$inReplaceFrom = ' '; |
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
$diff = mb_strlen( $inReplaceTo ) - mb_strlen( $inReplaceFrom ); |
| 924 |
if ( $diff > 0 ) { |
| 925 |
$limit = (int)( ( $this->config->get( 'PFStringLengthLimit' ) - mb_strlen( $inStr ) ) / $diff ) + 1; |
| 926 |
} else { |
| 927 |
$limit = -1; |
| 928 |
} |
| 929 |
|
| 930 |
$inLimit = (int)$inLimit; |
| 931 |
if ( $inLimit >= 0 ) { |
| 932 |
if ( $limit > $inLimit || $limit == -1 ) { |
| 933 |
$limit = $inLimit; |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
|
| 938 |
$inReplaceFrom = preg_quote( $inReplaceFrom, '/' ); |
| 939 |
$inReplaceTo = StringUtils::escapeRegexReplacement( $inReplaceTo ); |
| 940 |
|
| 941 |
$result = preg_replace( '/' . $inReplaceFrom . '/u', |
| 942 |
$inReplaceTo, $inStr, $limit ); |
| 943 |
|
| 944 |
if ( !$this->checkLength( $result ) ) { |
| 945 |
return $this->tooLongError(); |
| 946 |
} |
| 947 |
|
| 948 |
return $result; |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
public function runExplode( |
| 969 |
Parser $parser, $inStr = '', $inDiv = '', $inPos = 0, $inLim = null |
| 970 |
) { |
| 971 |
$inStr = $parser->killMarkers( (string)$inStr ); |
| 972 |
$inDiv = $parser->killMarkers( (string)$inDiv ); |
| 973 |
|
| 974 |
if ( $inDiv === '' ) { |
| 975 |
$inDiv = ' '; |
| 976 |
} |
| 977 |
|
| 978 |
if ( !$this->checkLength( $inStr ) || |
| 979 |
!$this->checkLength( $inDiv ) ) { |
| 980 |
return $this->tooLongError(); |
| 981 |
} |
| 982 |
|
| 983 |
$inDiv = preg_quote( $inDiv, '/' ); |
| 984 |
|
| 985 |
$matches = preg_split( '/' . $inDiv . '/u', $inStr, (int)$inLim ); |
| 986 |
|
| 987 |
if ( $inPos >= 0 && isset( $matches[$inPos] ) ) { |
| 988 |
$result = $matches[$inPos]; |
| 989 |
} elseif ( $inPos < 0 && isset( $matches[count( $matches ) + (int)$inPos] ) ) { |
| 990 |
$result = $matches[count( $matches ) + (int)$inPos]; |
| 991 |
} else { |
| 992 |
$result = ''; |
| 993 |
} |
| 994 |
|
| 995 |
return $result; |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
public function runUrlDecode( Parser $parser, $inStr = '' ) { |
| 1008 |
$inStr = $parser->killMarkers( (string)$inStr ); |
| 1009 |
if ( !$this->checkLength( $inStr ) ) { |
| 1010 |
return $this->tooLongError(); |
| 1011 |
} |
| 1012 |
|
| 1013 |
return urldecode( $inStr ); |
| 1014 |
} |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
private static function decodeTrimExpand( $obj, PPFrame $frame, &$trimExpanded = '' ) { |
| 1029 |
$expanded = $frame->expand( $obj ); |
| 1030 |
$trimExpanded = trim( $expanded ); |
| 1031 |
return trim( Sanitizer::decodeCharReferences( $expanded ) ); |
| 1032 |
} |
| 1033 |
} |