| 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 BadMethodCallException; |
| 27 |
use Exception; |
| 28 |
use File; |
| 29 |
use HtmlArmor; |
| 30 |
use ImageGalleryBase; |
| 31 |
use ImageGalleryClassNotFoundException; |
| 32 |
use InvalidArgumentException; |
| 33 |
use LogicException; |
| 34 |
use MapCacheLRU; |
| 35 |
use MediaHandler; |
| 36 |
use MediaWiki\Cache\CacheKeyHelper; |
| 37 |
use MediaWiki\Category\TrackingCategories; |
| 38 |
use MediaWiki\Config\ServiceOptions; |
| 39 |
use MediaWiki\Content\TextContent; |
| 40 |
use MediaWiki\Context\RequestContext; |
| 41 |
use MediaWiki\Debug\DeprecationHelper; |
| 42 |
use MediaWiki\HookContainer\HookContainer; |
| 43 |
use MediaWiki\HookContainer\HookRunner; |
| 44 |
use MediaWiki\Html\Html; |
| 45 |
use MediaWiki\Html\HtmlHelper; |
| 46 |
use MediaWiki\Http\HttpRequestFactory; |
| 47 |
use MediaWiki\Language\ILanguageConverter; |
| 48 |
use MediaWiki\Language\Language; |
| 49 |
use MediaWiki\Language\LanguageCode; |
| 50 |
use MediaWiki\Language\RawMessage; |
| 51 |
use MediaWiki\Languages\LanguageConverterFactory; |
| 52 |
use MediaWiki\Languages\LanguageNameUtils; |
| 53 |
use MediaWiki\Linker\Linker; |
| 54 |
use MediaWiki\Linker\LinkRenderer; |
| 55 |
use MediaWiki\Linker\LinkRendererFactory; |
| 56 |
use MediaWiki\Linker\LinkTarget; |
| 57 |
use MediaWiki\MainConfigNames; |
| 58 |
use MediaWiki\MediaWikiServices; |
| 59 |
use MediaWiki\Message\Message; |
| 60 |
use MediaWiki\Output\OutputPage; |
| 61 |
use MediaWiki\Page\File\BadFileLookup; |
| 62 |
use MediaWiki\Page\PageIdentity; |
| 63 |
use MediaWiki\Page\PageReference; |
| 64 |
use MediaWiki\Preferences\SignatureValidatorFactory; |
| 65 |
use MediaWiki\Request\FauxRequest; |
| 66 |
use MediaWiki\Revision\RevisionAccessException; |
| 67 |
use MediaWiki\Revision\RevisionRecord; |
| 68 |
use MediaWiki\Revision\SlotRecord; |
| 69 |
use MediaWiki\SpecialPage\SpecialPage; |
| 70 |
use MediaWiki\SpecialPage\SpecialPageFactory; |
| 71 |
use MediaWiki\Tidy\TidyDriverBase; |
| 72 |
use MediaWiki\Title\MalformedTitleException; |
| 73 |
use MediaWiki\Title\MediaWikiTitleCodec; |
| 74 |
use MediaWiki\Title\NamespaceInfo; |
| 75 |
use MediaWiki\Title\Title; |
| 76 |
use MediaWiki\Title\TitleFormatter; |
| 77 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 78 |
use MediaWiki\User\User; |
| 79 |
use MediaWiki\User\UserFactory; |
| 80 |
use MediaWiki\User\UserIdentity; |
| 81 |
use MediaWiki\User\UserNameUtils; |
| 82 |
use MediaWiki\Utils\MWTimestamp; |
| 83 |
use MediaWiki\Utils\UrlUtils; |
| 84 |
use MediaWiki\Xml\Xml; |
| 85 |
use Psr\Log\LoggerInterface; |
| 86 |
use RuntimeException; |
| 87 |
use SectionProfiler; |
| 88 |
use StringUtils; |
| 89 |
use UnexpectedValueException; |
| 90 |
use Wikimedia\Bcp47Code\Bcp47CodeValue; |
| 91 |
use Wikimedia\IPUtils; |
| 92 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 93 |
use Wikimedia\Parsoid\Core\SectionMetadata; |
| 94 |
use Wikimedia\Parsoid\Core\TOCData; |
| 95 |
use Wikimedia\Parsoid\DOM\Comment; |
| 96 |
use Wikimedia\Parsoid\DOM\DocumentFragment; |
| 97 |
use Wikimedia\Parsoid\DOM\Element; |
| 98 |
use Wikimedia\Parsoid\DOM\Node; |
| 99 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 100 |
use Wikimedia\Parsoid\Utils\DOMUtils; |
| 101 |
use Wikimedia\RemexHtml\Serializer\SerializerNode; |
| 102 |
use Wikimedia\ScopedCallback; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
#[\AllowDynamicProperties] |
| 145 |
class Parser { |
| 146 |
use DeprecationHelper; |
| 147 |
|
| 148 |
|
| 149 |
public const SFH_NO_HASH = 1; |
| 150 |
public const SFH_OBJECT_ARGS = 2; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public const EXT_LINK_URL_CLASS = '[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}]'; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private const EXT_LINK_ADDR = '(?:[0-9.]+|\\[(?i:[0-9a-f:.]+)\\]|[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}])'; |
| 167 |
|
| 168 |
|
| 169 |
private const EXT_IMAGE_REGEX = '/^(http:\/\/|https:\/\/)((?:\\[(?i:[0-9a-f:.]+)\\])?[^][<>"\\x00-\\x20\\x7F\p{Zs}\x{FFFD}]+) |
| 170 |
\\/([A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]+)\\.((?i)avif|gif|jpg|jpeg|png|svg|webp)$/Sxu'; |
| 171 |
|
| 172 |
|
| 173 |
private const SPACE_NOT_NL = '(?:\t| |&\#0*160;|&\#[Xx]0*[Aa]0;|\p{Zs})'; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public const PTD_FOR_INCLUSION = Preprocessor::DOM_FOR_INCLUSION; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public const OT_HTML = 1; |
| 184 |
|
| 185 |
public const OT_WIKI = 2; |
| 186 |
|
| 187 |
public const OT_PREPROCESS = 3; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public const OT_PLAIN = 4; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public const MARKER_SUFFIX = "-QINU`\"'\x7f"; |
| 212 |
public const MARKER_PREFIX = "\x7f'\"`UNIQ-"; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public const TOC_PLACEHOLDER = '<meta property="mw:PageProp/toc" />'; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private array $mTagHooks = []; |
| 233 |
|
| 234 |
private array $mFunctionHooks = []; |
| 235 |
|
| 236 |
private array $mFunctionSynonyms = [ 0 => [], 1 => [] ]; |
| 237 |
|
| 238 |
private array $mStripList = []; |
| 239 |
|
| 240 |
private array $mVarCache = []; |
| 241 |
|
| 242 |
private array $mImageParams = []; |
| 243 |
|
| 244 |
private array $mImageParamsMagicArray = []; |
| 245 |
|
| 246 |
public $mMarkerIndex = 0; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
private MagicWordArray $mVariables; |
| 251 |
private MagicWordArray $mSubstWords; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
private string $mExtLinkBracketedRegex; |
| 256 |
private UrlUtils $urlUtils; |
| 257 |
private Preprocessor $mPreprocessor; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
private ParserOutput $mOutput; |
| 262 |
private int $mAutonumber = 0; |
| 263 |
private StripState $mStripState; |
| 264 |
private LinkHolderArray $mLinkHolders; |
| 265 |
private int $mLinkID = 0; |
| 266 |
private array $mIncludeSizes; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public $mPPNodeCount; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public $mHighestExpansionDepth; |
| 277 |
private array $mTplRedirCache; |
| 278 |
|
| 279 |
public array $mHeadings; |
| 280 |
|
| 281 |
private array $mDoubleUnderscores; |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public $mExpensiveFunctionCount; |
| 287 |
private bool $mShowToc; |
| 288 |
private bool $mForceTocPosition; |
| 289 |
private array $mTplDomCache; |
| 290 |
private ?UserIdentity $mUser; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
private $mOptions; |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public $scribunto_engine; |
| 306 |
|
| 307 |
public $extCite; |
| 308 |
|
| 309 |
public $extTemplateStylesCache; |
| 310 |
|
| 311 |
public $static_tag_buf; |
| 312 |
|
| 313 |
public $mExtVariables; |
| 314 |
|
| 315 |
public $mExtArrays; |
| 316 |
|
| 317 |
public $mExtHashTables; |
| 318 |
|
| 319 |
public $mExtLoopsCounter; |
| 320 |
|
| 321 |
public $proofreadRenderingPages; |
| 322 |
|
| 323 |
public $mTemplatePath; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
private Title $mTitle; |
| 331 |
|
| 332 |
private int $mOutputType; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
private array $ot; |
| 338 |
|
| 339 |
private ?int $mRevisionId = null; |
| 340 |
|
| 341 |
private ?string $mRevisionTimestamp = null; |
| 342 |
|
| 343 |
private ?string $mRevisionUser = null; |
| 344 |
|
| 345 |
private ?int $mRevisionSize = null; |
| 346 |
|
| 347 |
private $mInputSize = false; |
| 348 |
|
| 349 |
private ?RevisionRecord $mRevisionRecordObject = null; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
private ?MapCacheLRU $currentRevisionCache = null; |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
private $mInParse = false; |
| 363 |
|
| 364 |
private SectionProfiler $mProfiler; |
| 365 |
private ?LinkRenderer $mLinkRenderer = null; |
| 366 |
|
| 367 |
private MagicWordFactory $magicWordFactory; |
| 368 |
private Language $contLang; |
| 369 |
private LanguageConverterFactory $languageConverterFactory; |
| 370 |
private LanguageNameUtils $languageNameUtils; |
| 371 |
private ParserFactory $factory; |
| 372 |
private SpecialPageFactory $specialPageFactory; |
| 373 |
private TitleFormatter $titleFormatter; |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
private ServiceOptions $svcOptions; |
| 380 |
private LinkRendererFactory $linkRendererFactory; |
| 381 |
private NamespaceInfo $nsInfo; |
| 382 |
private LoggerInterface $logger; |
| 383 |
private BadFileLookup $badFileLookup; |
| 384 |
private HookContainer $hookContainer; |
| 385 |
private HookRunner $hookRunner; |
| 386 |
private TidyDriverBase $tidy; |
| 387 |
private WANObjectCache $wanCache; |
| 388 |
private UserOptionsLookup $userOptionsLookup; |
| 389 |
private UserFactory $userFactory; |
| 390 |
private HttpRequestFactory $httpRequestFactory; |
| 391 |
private TrackingCategories $trackingCategories; |
| 392 |
private SignatureValidatorFactory $signatureValidatorFactory; |
| 393 |
private UserNameUtils $userNameUtils; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public const CONSTRUCTOR_OPTIONS = [ |
| 399 |
|
| 400 |
|
| 401 |
MainConfigNames::AllowDisplayTitle, |
| 402 |
MainConfigNames::AllowSlowParserFunctions, |
| 403 |
MainConfigNames::ArticlePath, |
| 404 |
MainConfigNames::EnableScaryTranscluding, |
| 405 |
MainConfigNames::ExtraInterlanguageLinkPrefixes, |
| 406 |
MainConfigNames::FragmentMode, |
| 407 |
MainConfigNames::Localtimezone, |
| 408 |
MainConfigNames::MaxSigChars, |
| 409 |
MainConfigNames::MaxTocLevel, |
| 410 |
MainConfigNames::MiserMode, |
| 411 |
MainConfigNames::RawHtml, |
| 412 |
MainConfigNames::ScriptPath, |
| 413 |
MainConfigNames::Server, |
| 414 |
MainConfigNames::ServerName, |
| 415 |
MainConfigNames::ShowHostnames, |
| 416 |
MainConfigNames::SignatureValidation, |
| 417 |
MainConfigNames::Sitename, |
| 418 |
MainConfigNames::StylePath, |
| 419 |
MainConfigNames::TranscludeCacheExpiry, |
| 420 |
MainConfigNames::PreprocessorCacheThreshold, |
| 421 |
MainConfigNames::ParserEnableLegacyMediaDOM, |
| 422 |
MainConfigNames::EnableParserLimitReporting, |
| 423 |
MainConfigNames::ParserEnableUserLanguage, |
| 424 |
]; |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public function __construct( |
| 454 |
ServiceOptions $svcOptions, |
| 455 |
MagicWordFactory $magicWordFactory, |
| 456 |
Language $contLang, |
| 457 |
ParserFactory $factory, |
| 458 |
UrlUtils $urlUtils, |
| 459 |
SpecialPageFactory $spFactory, |
| 460 |
LinkRendererFactory $linkRendererFactory, |
| 461 |
NamespaceInfo $nsInfo, |
| 462 |
LoggerInterface $logger, |
| 463 |
BadFileLookup $badFileLookup, |
| 464 |
LanguageConverterFactory $languageConverterFactory, |
| 465 |
LanguageNameUtils $languageNameUtils, |
| 466 |
HookContainer $hookContainer, |
| 467 |
TidyDriverBase $tidy, |
| 468 |
WANObjectCache $wanCache, |
| 469 |
UserOptionsLookup $userOptionsLookup, |
| 470 |
UserFactory $userFactory, |
| 471 |
TitleFormatter $titleFormatter, |
| 472 |
HttpRequestFactory $httpRequestFactory, |
| 473 |
TrackingCategories $trackingCategories, |
| 474 |
SignatureValidatorFactory $signatureValidatorFactory, |
| 475 |
UserNameUtils $userNameUtils |
| 476 |
) { |
| 477 |
$this->deprecateDynamicPropertiesAccess( '1.42', __CLASS__ ); |
| 478 |
$this->deprecatePublicProperty( 'ot', '1.35', __CLASS__ ); |
| 479 |
$this->deprecatePublicProperty( 'mTitle', '1.35', __CLASS__ ); |
| 480 |
$this->deprecatePublicProperty( 'mOptions', '1.35', __CLASS__ ); |
| 481 |
|
| 482 |
if ( ParserFactory::$inParserFactory === 0 ) { |
| 483 |
|
| 484 |
|
| 485 |
throw new BadMethodCallException( 'Direct construction of Parser not allowed' ); |
| 486 |
} |
| 487 |
$svcOptions->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 488 |
$this->svcOptions = $svcOptions; |
| 489 |
|
| 490 |
$this->urlUtils = $urlUtils; |
| 491 |
$this->mExtLinkBracketedRegex = '/\[(((?i)' . $this->urlUtils->validProtocols() . ')' . |
| 492 |
self::EXT_LINK_ADDR . |
| 493 |
self::EXT_LINK_URL_CLASS . '*)\p{Zs}*([^\]\\x00-\\x08\\x0a-\\x1F\\x{FFFD}]*)\]/Su'; |
| 494 |
|
| 495 |
$this->magicWordFactory = $magicWordFactory; |
| 496 |
|
| 497 |
$this->contLang = $contLang; |
| 498 |
|
| 499 |
$this->factory = $factory; |
| 500 |
$this->specialPageFactory = $spFactory; |
| 501 |
$this->linkRendererFactory = $linkRendererFactory; |
| 502 |
$this->nsInfo = $nsInfo; |
| 503 |
$this->logger = $logger; |
| 504 |
$this->badFileLookup = $badFileLookup; |
| 505 |
|
| 506 |
$this->languageConverterFactory = $languageConverterFactory; |
| 507 |
$this->languageNameUtils = $languageNameUtils; |
| 508 |
|
| 509 |
$this->hookContainer = $hookContainer; |
| 510 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 511 |
|
| 512 |
$this->tidy = $tidy; |
| 513 |
|
| 514 |
$this->wanCache = $wanCache; |
| 515 |
$this->mPreprocessor = new Preprocessor_Hash( |
| 516 |
$this, |
| 517 |
$this->wanCache, |
| 518 |
[ |
| 519 |
'cacheThreshold' => $svcOptions->get( MainConfigNames::PreprocessorCacheThreshold ), |
| 520 |
'disableLangConversion' => $languageConverterFactory->isConversionDisabled(), |
| 521 |
] |
| 522 |
); |
| 523 |
|
| 524 |
$this->userOptionsLookup = $userOptionsLookup; |
| 525 |
$this->userFactory = $userFactory; |
| 526 |
$this->titleFormatter = $titleFormatter; |
| 527 |
$this->httpRequestFactory = $httpRequestFactory; |
| 528 |
$this->trackingCategories = $trackingCategories; |
| 529 |
$this->signatureValidatorFactory = $signatureValidatorFactory; |
| 530 |
$this->userNameUtils = $userNameUtils; |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
CoreParserFunctions::register( |
| 535 |
$this, |
| 536 |
new ServiceOptions( CoreParserFunctions::REGISTER_OPTIONS, $svcOptions ) |
| 537 |
); |
| 538 |
CoreTagHooks::register( |
| 539 |
$this, |
| 540 |
new ServiceOptions( CoreTagHooks::REGISTER_OPTIONS, $svcOptions ) |
| 541 |
); |
| 542 |
$this->initializeVariables(); |
| 543 |
|
| 544 |
$this->hookRunner->onParserFirstCallInit( $this ); |
| 545 |
$this->mTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/Missing' ); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
public function __destruct() { |
| 552 |
|
| 553 |
if ( isset( $this->mLinkHolders ) ) { |
| 554 |
|
| 555 |
unset( $this->mLinkHolders ); |
| 556 |
} |
| 557 |
|
| 558 |
foreach ( $this as $name => $value ) { |
| 559 |
unset( $this->$name ); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
public function __clone() { |
| 567 |
$this->mInParse = false; |
| 568 |
|
| 569 |
$this->mPreprocessor = clone $this->mPreprocessor; |
| 570 |
$this->mPreprocessor->resetParser( $this ); |
| 571 |
|
| 572 |
$this->hookRunner->onParserCloned( $this ); |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
public function firstCallInit() { |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
public function clearState() { |
| 595 |
$this->resetOutput(); |
| 596 |
$this->mAutonumber = 0; |
| 597 |
$this->mLinkHolders = new LinkHolderArray( |
| 598 |
$this, |
| 599 |
$this->getContentLanguageConverter(), |
| 600 |
$this->getHookContainer() |
| 601 |
); |
| 602 |
$this->mLinkID = 0; |
| 603 |
$this->mRevisionTimestamp = null; |
| 604 |
$this->mRevisionId = null; |
| 605 |
$this->mRevisionUser = null; |
| 606 |
$this->mRevisionSize = null; |
| 607 |
$this->mRevisionRecordObject = null; |
| 608 |
$this->mVarCache = []; |
| 609 |
$this->mUser = null; |
| 610 |
$this->currentRevisionCache = null; |
| 611 |
|
| 612 |
$this->mStripState = new StripState( $this ); |
| 613 |
|
| 614 |
|
| 615 |
$this->mTplRedirCache = []; |
| 616 |
$this->mTplDomCache = []; |
| 617 |
|
| 618 |
$this->mShowToc = true; |
| 619 |
$this->mForceTocPosition = false; |
| 620 |
$this->mIncludeSizes = [ |
| 621 |
'post-expand' => 0, |
| 622 |
'arg' => 0, |
| 623 |
]; |
| 624 |
$this->mPPNodeCount = 0; |
| 625 |
$this->mHighestExpansionDepth = 0; |
| 626 |
$this->mHeadings = []; |
| 627 |
$this->mDoubleUnderscores = []; |
| 628 |
$this->mExpensiveFunctionCount = 0; |
| 629 |
|
| 630 |
$this->mProfiler = new SectionProfiler(); |
| 631 |
|
| 632 |
$this->hookRunner->onParserClearState( $this ); |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
public function resetOutput() { |
| 640 |
$this->mOutput = new ParserOutput; |
| 641 |
$this->mOptions->registerWatcher( [ $this->mOutput, 'recordOption' ] ); |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
public function parse( |
| 663 |
$text, PageReference $page, ParserOptions $options, |
| 664 |
$linestart = true, $clearState = true, $revid = null |
| 665 |
) { |
| 666 |
if ( $clearState ) { |
| 667 |
|
| 668 |
|
| 669 |
$text = strtr( $text, "\x7f", "?" ); |
| 670 |
$magicScopeVariable = $this->lock(); |
| 671 |
} |
| 672 |
|
| 673 |
$text = str_replace( "\000", '', $text ); |
| 674 |
|
| 675 |
$this->startParse( $page, $options, self::OT_HTML, $clearState ); |
| 676 |
|
| 677 |
$this->currentRevisionCache = null; |
| 678 |
$this->mInputSize = strlen( $text ); |
| 679 |
$this->mOutput->resetParseStartTime(); |
| 680 |
|
| 681 |
$oldRevisionId = $this->mRevisionId; |
| 682 |
$oldRevisionRecordObject = $this->mRevisionRecordObject; |
| 683 |
$oldRevisionTimestamp = $this->mRevisionTimestamp; |
| 684 |
$oldRevisionUser = $this->mRevisionUser; |
| 685 |
$oldRevisionSize = $this->mRevisionSize; |
| 686 |
if ( $revid !== null ) { |
| 687 |
$this->mRevisionId = $revid; |
| 688 |
$this->mRevisionRecordObject = null; |
| 689 |
$this->mRevisionTimestamp = null; |
| 690 |
$this->mRevisionUser = null; |
| 691 |
$this->mRevisionSize = null; |
| 692 |
} |
| 693 |
|
| 694 |
$text = $this->internalParse( $text ); |
| 695 |
$this->hookRunner->onParserAfterParse( $this, $text, $this->mStripState ); |
| 696 |
|
| 697 |
$text = $this->internalParseHalfParsed( $text, true, $linestart ); |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
if ( !$options->getDisableTitleConversion() |
| 707 |
&& !isset( $this->mDoubleUnderscores['nocontentconvert'] ) |
| 708 |
&& !isset( $this->mDoubleUnderscores['notitleconvert'] ) |
| 709 |
&& $this->mOutput->getDisplayTitle() === false |
| 710 |
) { |
| 711 |
$titleText = $this->getTargetLanguageConverter()->getConvRuleTitle(); |
| 712 |
if ( $titleText !== false ) { |
| 713 |
$titleText = Sanitizer::removeSomeTags( $titleText ); |
| 714 |
} else { |
| 715 |
[ $nsText, $nsSeparator, $mainText ] = $this->getTargetLanguageConverter()->convertSplitTitle( $page ); |
| 716 |
|
| 717 |
|
| 718 |
$titleText = self::formatPageTitle( $nsText, $nsSeparator, $mainText ); |
| 719 |
} |
| 720 |
$this->mOutput->setTitleText( $titleText ); |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
$this->mOutput->recordTimeProfile(); |
| 726 |
|
| 727 |
|
| 728 |
$this->mOutput->finalizeAdaptiveCacheExpiry(); |
| 729 |
|
| 730 |
|
| 731 |
if ( $this->mExpensiveFunctionCount > $options->getExpensiveParserFunctionLimit() ) { |
| 732 |
$this->limitationWarn( 'expensive-parserfunction', |
| 733 |
$this->mExpensiveFunctionCount, |
| 734 |
$options->getExpensiveParserFunctionLimit() |
| 735 |
); |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
if ( $this->svcOptions->get( MainConfigNames::EnableParserLimitReporting ) ) { |
| 740 |
$this->makeLimitReport(); |
| 741 |
} |
| 742 |
|
| 743 |
$this->mOutput->setFromParserOptions( $options ); |
| 744 |
|
| 745 |
$this->mOutput->setRawText( $text ); |
| 746 |
|
| 747 |
$this->mRevisionId = $oldRevisionId; |
| 748 |
$this->mRevisionRecordObject = $oldRevisionRecordObject; |
| 749 |
$this->mRevisionTimestamp = $oldRevisionTimestamp; |
| 750 |
$this->mRevisionUser = $oldRevisionUser; |
| 751 |
$this->mRevisionSize = $oldRevisionSize; |
| 752 |
$this->mInputSize = false; |
| 753 |
$this->currentRevisionCache = null; |
| 754 |
|
| 755 |
return $this->mOutput; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
protected function makeLimitReport() { |
| 762 |
$maxIncludeSize = $this->mOptions->getMaxIncludeSize(); |
| 763 |
|
| 764 |
$cpuTime = $this->mOutput->getTimeProfile( 'cpu' ); |
| 765 |
if ( $cpuTime !== null ) { |
| 766 |
$this->mOutput->setLimitReportData( 'limitreport-cputime', |
| 767 |
sprintf( "%.3f", $cpuTime ) |
| 768 |
); |
| 769 |
} |
| 770 |
|
| 771 |
$wallTime = $this->mOutput->getTimeProfile( 'wall' ); |
| 772 |
$this->mOutput->setLimitReportData( 'limitreport-walltime', |
| 773 |
sprintf( "%.3f", $wallTime ) |
| 774 |
); |
| 775 |
|
| 776 |
$this->mOutput->setLimitReportData( 'limitreport-ppvisitednodes', |
| 777 |
[ $this->mPPNodeCount, $this->mOptions->getMaxPPNodeCount() ] |
| 778 |
); |
| 779 |
$this->mOutput->setLimitReportData( 'limitreport-postexpandincludesize', |
| 780 |
[ $this->mIncludeSizes['post-expand'], $maxIncludeSize ] |
| 781 |
); |
| 782 |
$this->mOutput->setLimitReportData( 'limitreport-templateargumentsize', |
| 783 |
[ $this->mIncludeSizes['arg'], $maxIncludeSize ] |
| 784 |
); |
| 785 |
$this->mOutput->setLimitReportData( 'limitreport-expansiondepth', |
| 786 |
[ $this->mHighestExpansionDepth, $this->mOptions->getMaxPPExpandDepth() ] |
| 787 |
); |
| 788 |
$this->mOutput->setLimitReportData( 'limitreport-expensivefunctioncount', |
| 789 |
[ $this->mExpensiveFunctionCount, $this->mOptions->getExpensiveParserFunctionLimit() ] |
| 790 |
); |
| 791 |
|
| 792 |
foreach ( $this->mStripState->getLimitReport() as [ $key, $value ] ) { |
| 793 |
$this->mOutput->setLimitReportData( $key, $value ); |
| 794 |
} |
| 795 |
|
| 796 |
$this->hookRunner->onParserLimitReportPrepare( $this, $this->mOutput ); |
| 797 |
|
| 798 |
|
| 799 |
$dataByFunc = $this->mProfiler->getFunctionStats(); |
| 800 |
uasort( $dataByFunc, static function ( $a, $b ) { |
| 801 |
return $b['real'] <=> $a['real']; |
| 802 |
} ); |
| 803 |
$profileReport = []; |
| 804 |
foreach ( array_slice( $dataByFunc, 0, 10 ) as $item ) { |
| 805 |
$profileReport[] = sprintf( "%6.2f%% %8.3f %6d %s", |
| 806 |
$item['%real'], $item['real'], $item['calls'], |
| 807 |
htmlspecialchars( $item['name'] ) ); |
| 808 |
} |
| 809 |
|
| 810 |
$this->mOutput->setLimitReportData( 'limitreport-timingprofile', $profileReport ); |
| 811 |
|
| 812 |
|
| 813 |
if ( $this->svcOptions->get( MainConfigNames::ShowHostnames ) ) { |
| 814 |
$this->mOutput->setLimitReportData( 'cachereport-origin', wfHostname() ); |
| 815 |
} |
| 816 |
$this->mOutput->setLimitReportData( 'cachereport-timestamp', |
| 817 |
$this->mOutput->getCacheTime() ); |
| 818 |
$this->mOutput->setLimitReportData( 'cachereport-ttl', |
| 819 |
$this->mOutput->getCacheExpiry() ); |
| 820 |
$this->mOutput->setLimitReportData( 'cachereport-transientcontent', |
| 821 |
$this->mOutput->hasReducedExpiry() ); |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
public function recursiveTagParse( $text, $frame = false ) { |
| 850 |
$text = $this->internalParse( $text, false, $frame ); |
| 851 |
return $text; |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
public function recursiveTagParseFully( $text, $frame = false ) { |
| 874 |
$text = $this->recursiveTagParse( $text, $frame ); |
| 875 |
$text = $this->internalParseHalfParsed( $text, false ); |
| 876 |
return $text; |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
public function parseExtensionTagAsTopLevelDoc( $text ) { |
| 899 |
$text = $this->recursiveTagParse( $text ); |
| 900 |
$this->hookRunner->onParserAfterParse( $this, $text, $this->mStripState ); |
| 901 |
$text = $this->internalParseHalfParsed( $text, true ); |
| 902 |
return $text; |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
public function preprocess( |
| 918 |
$text, |
| 919 |
?PageReference $page, |
| 920 |
ParserOptions $options, |
| 921 |
$revid = null, |
| 922 |
$frame = false |
| 923 |
) { |
| 924 |
$magicScopeVariable = $this->lock(); |
| 925 |
$this->startParse( $page, $options, self::OT_PREPROCESS, true ); |
| 926 |
if ( $revid !== null ) { |
| 927 |
$this->mRevisionId = $revid; |
| 928 |
} |
| 929 |
$this->hookRunner->onParserBeforePreprocess( $this, $text, $this->mStripState ); |
| 930 |
$text = $this->replaceVariables( $text, $frame ); |
| 931 |
$text = $this->mStripState->unstripBoth( $text ); |
| 932 |
return $text; |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
public function recursivePreprocess( $text, $frame = false ) { |
| 945 |
$text = $this->replaceVariables( $text, $frame ); |
| 946 |
$text = $this->mStripState->unstripBoth( $text ); |
| 947 |
return $text; |
| 948 |
} |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
public function getPreloadText( $text, PageReference $page, ParserOptions $options, $params = [] ) { |
| 965 |
$msg = new RawMessage( $text ); |
| 966 |
$text = $msg->params( $params )->plain(); |
| 967 |
|
| 968 |
|
| 969 |
$magicScopeVariable = $this->lock(); |
| 970 |
$this->startParse( $page, $options, self::OT_PLAIN, true ); |
| 971 |
|
| 972 |
$flags = PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES; |
| 973 |
$dom = $this->preprocessToDom( $text, Preprocessor::DOM_FOR_INCLUSION ); |
| 974 |
$text = $this->getPreprocessor()->newFrame()->expand( $dom, $flags ); |
| 975 |
$text = $this->mStripState->unstripBoth( $text ); |
| 976 |
return $text; |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
public function setUser( ?UserIdentity $user ) { |
| 987 |
$this->mUser = $user; |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
public function setTitle( ?Title $t = null ) { |
| 998 |
$this->setPage( $t ); |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
public function getTitle(): Title { |
| 1007 |
return $this->mTitle; |
| 1008 |
} |
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
public function setPage( ?PageReference $t = null ) { |
| 1017 |
if ( !$t ) { |
| 1018 |
$t = Title::makeTitle( NS_SPECIAL, 'Badtitle/Parser' ); |
| 1019 |
} else { |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
$t = Title::newFromPageReference( $t ); |
| 1024 |
} |
| 1025 |
|
| 1026 |
if ( $t->hasFragment() ) { |
| 1027 |
|
| 1028 |
$this->mTitle = $t->createFragmentTarget( '' ); |
| 1029 |
} else { |
| 1030 |
$this->mTitle = $t; |
| 1031 |
} |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
public function getPage(): ?PageReference { |
| 1040 |
if ( $this->mTitle->isSpecial( 'Badtitle' ) ) { |
| 1041 |
[ , $subPage ] = $this->specialPageFactory->resolveAlias( $this->mTitle->getDBkey() ); |
| 1042 |
|
| 1043 |
if ( $subPage === 'Missing' ) { |
| 1044 |
wfDeprecated( __METHOD__ . ' without a Title set', '1.34' ); |
| 1045 |
return null; |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
return $this->mTitle; |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
public function getOutputType(): int { |
| 1058 |
return $this->mOutputType; |
| 1059 |
} |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
ts |
| 1064 |
|
| 1065 |
|
| 1066 |
public function setOutputType( $ot ): void { |
| 1067 |
$this->mOutputType = $ot; |
| 1068 |
|
| 1069 |
$this->ot = [ |
| 1070 |
'html' => $ot == self::OT_HTML, |
| 1071 |
'wiki' => $ot == self::OT_WIKI, |
| 1072 |
'pre' => $ot == self::OT_PREPROCESS, |
| 1073 |
'plain' => $ot == self::OT_PLAIN, |
| 1074 |
]; |
| 1075 |
} |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
public function OutputType( $x = null ) { |
| 1085 |
wfDeprecated( __METHOD__, '1.35' ); |
| 1086 |
return wfSetVar( $this->mOutputType, $x ); |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
public function getOutput() { |
| 1094 |
|
| 1095 |
if ( !isset( $this->mOutput ) ) { |
| 1096 |
wfDeprecated( __METHOD__ . ' before initialization', '1.42' ); |
| 1097 |
this |
| 1098 |
return null; |
| 1099 |
} |
| 1100 |
return $this->mOutput; |
| 1101 |
} |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
public function getOptions() { |
| 1108 |
return $this->mOptions; |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
public function setOptions( ParserOptions $options ): void { |
| 1117 |
$this->mOptions = $options; |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
public function Options( $x = null ) { |
| 1128 |
wfDeprecated( __METHOD__, '1.35' ); |
| 1129 |
return wfSetVar( $this->mOptions, $x ); |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
public function nextLinkID() { |
| 1137 |
return $this->mLinkID++; |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
public function setLinkID( $id ) { |
| 1145 |
$this->mLinkID = $id; |
| 1146 |
} |
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
public function getFunctionLang() { |
| 1155 |
wfDeprecated( __METHOD__, '1.40' ); |
| 1156 |
return $this->getTargetLanguage(); |
| 1157 |
} |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
public function getTargetLanguage() { |
| 1168 |
$target = $this->mOptions->getTargetLanguage(); |
| 1169 |
|
| 1170 |
if ( $target !== null ) { |
| 1171 |
return $target; |
| 1172 |
} elseif ( $this->mOptions->getInterfaceMessage() ) { |
| 1173 |
return $this->mOptions->getUserLangObj(); |
| 1174 |
} |
| 1175 |
|
| 1176 |
return $this->getTitle()->getPageLanguage(); |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
public function getUserIdentity(): UserIdentity { |
| 1187 |
return $this->mUser ?? $this->getOptions()->getUserIdentity(); |
| 1188 |
} |
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
public function getPreprocessor() { |
| 1197 |
return $this->mPreprocessor; |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
public function getLinkRenderer() { |
| 1207 |
|
| 1208 |
if ( !$this->mLinkRenderer ) { |
| 1209 |
$this->mLinkRenderer = $this->linkRendererFactory->create(); |
| 1210 |
} |
| 1211 |
|
| 1212 |
return $this->mLinkRenderer; |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
public function getMagicWordFactory() { |
| 1222 |
return $this->magicWordFactory; |
| 1223 |
} |
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
public function getContentLanguage() { |
| 1232 |
return $this->contLang; |
| 1233 |
} |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
public function getBadFileLookup() { |
| 1242 |
return $this->badFileLookup; |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
public static function extractTagsAndParams( array $elements, $text, &$matches ) { |
| 1265 |
static $n = 1; |
| 1266 |
$stripped = ''; |
| 1267 |
$matches = []; |
| 1268 |
|
| 1269 |
$taglist = implode( '|', $elements ); |
| 1270 |
$start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i"; |
| 1271 |
|
| 1272 |
while ( $text != '' ) { |
| 1273 |
$p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE ); |
| 1274 |
$stripped .= $p[0]; |
| 1275 |
if ( count( $p ) < 5 ) { |
| 1276 |
break; |
| 1277 |
} |
| 1278 |
if ( count( $p ) > 5 ) { |
| 1279 |
|
| 1280 |
$element = $p[4]; |
| 1281 |
$attributes = ''; |
| 1282 |
$close = ''; |
| 1283 |
$inside = $p[5]; |
| 1284 |
} else { |
| 1285 |
|
| 1286 |
[ , $element, $attributes, $close, $inside ] = $p; |
| 1287 |
} |
| 1288 |
|
| 1289 |
$marker = self::MARKER_PREFIX . "-$element-" . sprintf( '%08X', $n++ ) . self::MARKER_SUFFIX; |
| 1290 |
$stripped .= $marker; |
| 1291 |
|
| 1292 |
if ( $close === '/>' ) { |
| 1293 |
|
| 1294 |
$content = null; |
| 1295 |
$text = $inside; |
| 1296 |
$tail = null; |
| 1297 |
} else { |
| 1298 |
if ( $element === '!--' ) { |
| 1299 |
$end = '/(-->)/'; |
| 1300 |
} else { |
| 1301 |
$end = "/(<\\/$element\\s*>)/i"; |
| 1302 |
} |
| 1303 |
$q = preg_split( $end, $inside, 2, PREG_SPLIT_DELIM_CAPTURE ); |
| 1304 |
$content = $q[0]; |
| 1305 |
if ( count( $q ) < 3 ) { |
| 1306 |
|
| 1307 |
$tail = ''; |
| 1308 |
$text = ''; |
| 1309 |
} else { |
| 1310 |
[ , $tail, $text ] = $q; |
| 1311 |
} |
| 1312 |
} |
| 1313 |
|
| 1314 |
$matches[$marker] = [ $element, |
| 1315 |
$content, |
| 1316 |
Sanitizer::decodeTagAttributes( $attributes ), |
| 1317 |
"<$element$attributes$close$content$tail" ]; |
| 1318 |
} |
| 1319 |
return $stripped; |
| 1320 |
} |
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
public function getStripList() { |
| 1328 |
return $this->mStripList; |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
public function getStripState() { |
| 1336 |
return $this->mStripState; |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
public function insertStripItem( $text ) { |
| 1349 |
$marker = self::MARKER_PREFIX . "-item-{$this->mMarkerIndex}-" . self::MARKER_SUFFIX; |
| 1350 |
$this->mMarkerIndex++; |
| 1351 |
$this->mStripState->addGeneral( $marker, $text ); |
| 1352 |
return $marker; |
| 1353 |
} |
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
private function handleTables( $text ) { |
| 1362 |
$lines = StringUtils::explode( "\n", $text ); |
| 1363 |
$out = ''; |
| 1364 |
$td_history = []; |
| 1365 |
$last_tag_history = []; |
| 1366 |
$tr_history = []; |
| 1367 |
$tr_attributes = []; |
| 1368 |
$has_opened_tr = []; |
| 1369 |
$indent_level = 0; |
| 1370 |
|
| 1371 |
foreach ( $lines as $outLine ) { |
| 1372 |
$line = trim( $outLine ); |
| 1373 |
|
| 1374 |
if ( $line === '' ) { |
| 1375 |
$out .= $outLine . "\n"; |
| 1376 |
continue; |
| 1377 |
} |
| 1378 |
|
| 1379 |
$first_character = $line[0]; |
| 1380 |
$first_two = substr( $line, 0, 2 ); |
| 1381 |
$matches = []; |
| 1382 |
|
| 1383 |
if ( preg_match( '/^(:*)\s*\{\|(.*)$/', $line, $matches ) ) { |
| 1384 |
|
| 1385 |
$indent_level = strlen( $matches[1] ); |
| 1386 |
|
| 1387 |
$attributes = $this->mStripState->unstripBoth( $matches[2] ); |
| 1388 |
$attributes = Sanitizer::fixTagAttributes( $attributes, 'table' ); |
| 1389 |
|
| 1390 |
$outLine = str_repeat( '<dl><dd>', $indent_level ) . "<table{$attributes}>"; |
| 1391 |
$td_history[] = false; |
| 1392 |
$last_tag_history[] = ''; |
| 1393 |
$tr_history[] = false; |
| 1394 |
$tr_attributes[] = ''; |
| 1395 |
$has_opened_tr[] = false; |
| 1396 |
} elseif ( count( $td_history ) == 0 ) { |
| 1397 |
|
| 1398 |
$out .= $outLine . "\n"; |
| 1399 |
continue; |
| 1400 |
} elseif ( $first_two === '|}' ) { |
| 1401 |
|
| 1402 |
$line = '</table>' . substr( $line, 2 ); |
| 1403 |
$last_tag = array_pop( $last_tag_history ); |
| 1404 |
|
| 1405 |
if ( !array_pop( $has_opened_tr ) ) { |
| 1406 |
$line = "<tr><td></td></tr>{$line}"; |
| 1407 |
} |
| 1408 |
|
| 1409 |
if ( array_pop( $tr_history ) ) { |
| 1410 |
$line = "</tr>{$line}"; |
| 1411 |
} |
| 1412 |
|
| 1413 |
if ( array_pop( $td_history ) ) { |
| 1414 |
$line = "</{$last_tag}>{$line}"; |
| 1415 |
} |
| 1416 |
array_pop( $tr_attributes ); |
| 1417 |
if ( $indent_level > 0 ) { |
| 1418 |
$outLine = rtrim( $line ) . str_repeat( '</dd></dl>', $indent_level ); |
| 1419 |
} else { |
| 1420 |
$outLine = $line; |
| 1421 |
} |
| 1422 |
} elseif ( $first_two === '|-' ) { |
| 1423 |
|
| 1424 |
$line = preg_replace( '#^\|-+#', '', $line ); |
| 1425 |
|
| 1426 |
|
| 1427 |
$attributes = $this->mStripState->unstripBoth( $line ); |
| 1428 |
$attributes = Sanitizer::fixTagAttributes( $attributes, 'tr' ); |
| 1429 |
array_pop( $tr_attributes ); |
| 1430 |
$tr_attributes[] = $attributes; |
| 1431 |
|
| 1432 |
$line = ''; |
| 1433 |
$last_tag = array_pop( $last_tag_history ); |
| 1434 |
array_pop( $has_opened_tr ); |
| 1435 |
$has_opened_tr[] = true; |
| 1436 |
|
| 1437 |
if ( array_pop( $tr_history ) ) { |
| 1438 |
$line = '</tr>'; |
| 1439 |
} |
| 1440 |
|
| 1441 |
if ( array_pop( $td_history ) ) { |
| 1442 |
$line = "</{$last_tag}>{$line}"; |
| 1443 |
} |
| 1444 |
|
| 1445 |
$outLine = $line; |
| 1446 |
$tr_history[] = false; |
| 1447 |
$td_history[] = false; |
| 1448 |
$last_tag_history[] = ''; |
| 1449 |
} elseif ( $first_character === '|' |
| 1450 |
|| $first_character === '!' |
| 1451 |
|| $first_two === '|+' |
| 1452 |
) { |
| 1453 |
|
| 1454 |
if ( $first_two === '|+' ) { |
| 1455 |
$first_character = '+'; |
| 1456 |
$line = substr( $line, 2 ); |
| 1457 |
} else { |
| 1458 |
$line = substr( $line, 1 ); |
| 1459 |
} |
| 1460 |
|
| 1461 |
|
| 1462 |
if ( $first_character === '!' ) { |
| 1463 |
$line = StringUtils::replaceMarkup( '!!', '||', $line ); |
| 1464 |
} |
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
$cells = explode( '||', $line ); |
| 1470 |
|
| 1471 |
$outLine = ''; |
| 1472 |
|
| 1473 |
|
| 1474 |
foreach ( $cells as $cell ) { |
| 1475 |
$previous = ''; |
| 1476 |
if ( $first_character !== '+' ) { |
| 1477 |
$tr_after = array_pop( $tr_attributes ); |
| 1478 |
if ( !array_pop( $tr_history ) ) { |
| 1479 |
$previous = "<tr{$tr_after}>\n"; |
| 1480 |
} |
| 1481 |
$tr_history[] = true; |
| 1482 |
$tr_attributes[] = ''; |
| 1483 |
array_pop( $has_opened_tr ); |
| 1484 |
$has_opened_tr[] = true; |
| 1485 |
} |
| 1486 |
|
| 1487 |
$last_tag = array_pop( $last_tag_history ); |
| 1488 |
|
| 1489 |
if ( array_pop( $td_history ) ) { |
| 1490 |
$previous = "</{$last_tag}>\n{$previous}"; |
| 1491 |
} |
| 1492 |
|
| 1493 |
if ( $first_character === '|' ) { |
| 1494 |
$last_tag = 'td'; |
| 1495 |
} elseif ( $first_character === '!' ) { |
| 1496 |
$last_tag = 'th'; |
| 1497 |
} elseif ( $first_character === '+' ) { |
| 1498 |
$last_tag = 'caption'; |
| 1499 |
} else { |
| 1500 |
$last_tag = ''; |
| 1501 |
} |
| 1502 |
|
| 1503 |
$last_tag_history[] = $last_tag; |
| 1504 |
|
| 1505 |
|
| 1506 |
$cell_data = explode( '|', $cell, 2 ); |
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
if ( preg_match( '/\[\[|-\{/', $cell_data[0] ) === 1 ) { |
| 1512 |
$cell = "{$previous}<{$last_tag}>" . trim( $cell ); |
| 1513 |
} elseif ( count( $cell_data ) == 1 ) { |
| 1514 |
|
| 1515 |
$cell = "{$previous}<{$last_tag}>" . trim( $cell_data[0] ); |
| 1516 |
} else { |
| 1517 |
$attributes = $this->mStripState->unstripBoth( $cell_data[0] ); |
| 1518 |
$attributes = Sanitizer::fixTagAttributes( $attributes, $last_tag ); |
| 1519 |
|
| 1520 |
$cell = "{$previous}<{$last_tag}{$attributes}>" . trim( $cell_data[1] ); |
| 1521 |
} |
| 1522 |
|
| 1523 |
$outLine .= $cell; |
| 1524 |
$td_history[] = true; |
| 1525 |
} |
| 1526 |
} |
| 1527 |
$out .= $outLine . "\n"; |
| 1528 |
} |
| 1529 |
|
| 1530 |
|
| 1531 |
while ( count( $td_history ) > 0 ) { |
| 1532 |
if ( array_pop( $td_history ) ) { |
| 1533 |
$out .= "</td>\n"; |
| 1534 |
} |
| 1535 |
if ( array_pop( $tr_history ) ) { |
| 1536 |
$out .= "</tr>\n"; |
| 1537 |
} |
| 1538 |
if ( !array_pop( $has_opened_tr ) ) { |
| 1539 |
$out .= "<tr><td></td></tr>\n"; |
| 1540 |
} |
| 1541 |
|
| 1542 |
$out .= "</table>\n"; |
| 1543 |
} |
| 1544 |
|
| 1545 |
|
| 1546 |
if ( substr( $out, -1 ) === "\n" ) { |
| 1547 |
$out = substr( $out, 0, -1 ); |
| 1548 |
} |
| 1549 |
|
| 1550 |
|
| 1551 |
if ( $out === "<table>\n<tr><td></td></tr>\n</table>" ) { |
| 1552 |
$out = ''; |
| 1553 |
} |
| 1554 |
|
| 1555 |
return $out; |
| 1556 |
} |
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
public function internalParse( $text, $isMain = true, $frame = false ) { |
| 1572 |
$origText = $text; |
| 1573 |
|
| 1574 |
|
| 1575 |
if ( !$this->hookRunner->onParserBeforeInternalParse( $this, $text, $this->mStripState ) ) { |
| 1576 |
return $text; |
| 1577 |
} |
| 1578 |
|
| 1579 |
|
| 1580 |
if ( $frame ) { |
| 1581 |
|
| 1582 |
|
| 1583 |
if ( !$frame->depth ) { |
| 1584 |
$flag = 0; |
| 1585 |
} else { |
| 1586 |
$flag = Preprocessor::DOM_FOR_INCLUSION; |
| 1587 |
} |
| 1588 |
$dom = $this->preprocessToDom( $text, $flag ); |
| 1589 |
$text = $frame->expand( $dom ); |
| 1590 |
} else { |
| 1591 |
|
| 1592 |
$text = $this->replaceVariables( $text ); |
| 1593 |
} |
| 1594 |
|
| 1595 |
$text = Sanitizer::internalRemoveHtmlTags( |
| 1596 |
$text, |
| 1597 |
|
| 1598 |
|
| 1599 |
function ( &$text, $frame = false ) { |
| 1600 |
$text = $this->replaceVariables( $text, $frame ); |
| 1601 |
$text = $this->mStripState->unstripBoth( $text ); |
| 1602 |
}, |
| 1603 |
false, |
| 1604 |
[], |
| 1605 |
[] |
| 1606 |
); |
| 1607 |
$this->hookRunner->onInternalParseBeforeLinks( $this, $text, $this->mStripState ); |
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
$text = $this->handleTables( $text ); |
| 1614 |
|
| 1615 |
$text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text ); |
| 1616 |
|
| 1617 |
$text = $this->handleDoubleUnderscore( $text ); |
| 1618 |
|
| 1619 |
$text = $this->handleHeadings( $text ); |
| 1620 |
$text = $this->handleInternalLinks( $text ); |
| 1621 |
$text = $this->handleAllQuotes( $text ); |
| 1622 |
$text = $this->handleExternalLinks( $text ); |
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
$text = str_replace( self::MARKER_PREFIX . 'NOPARSE', '', $text ); |
| 1627 |
|
| 1628 |
$text = $this->handleMagicLinks( $text ); |
| 1629 |
$text = $this->finalizeHeadings( $text, $origText, $isMain ); |
| 1630 |
|
| 1631 |
return $text; |
| 1632 |
} |
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
public function getTargetLanguageConverter(): ILanguageConverter { |
| 1641 |
return $this->languageConverterFactory->getLanguageConverter( |
| 1642 |
$this->getTargetLanguage() |
| 1643 |
); |
| 1644 |
} |
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
private function getContentLanguageConverter(): ILanguageConverter { |
| 1652 |
return $this->languageConverterFactory->getLanguageConverter( |
| 1653 |
$this->getContentLanguage() |
| 1654 |
); |
| 1655 |
} |
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
protected function getHookContainer() { |
| 1665 |
return $this->hookContainer; |
| 1666 |
} |
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
protected function getHookRunner() { |
| 1677 |
return $this->hookRunner; |
| 1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
private function internalParseHalfParsed( $text, $isMain = true, $linestart = true ) { |
| 1690 |
$text = $this->mStripState->unstripGeneral( $text ); |
| 1691 |
|
| 1692 |
$text = BlockLevelPass::doBlockLevels( $text, $linestart ); |
| 1693 |
|
| 1694 |
$this->replaceLinkHoldersPrivate( $text ); |
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
$converter = null; |
| 1704 |
if ( !( $this->mOptions->getDisableContentConversion() |
| 1705 |
|| isset( $this->mDoubleUnderscores['nocontentconvert'] ) |
| 1706 |
|| $this->mOptions->getInterfaceMessage() ) |
| 1707 |
) { |
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
$converter = $this->getTargetLanguageConverter(); |
| 1712 |
$text = $converter->convert( $text ); |
| 1713 |
|
| 1714 |
} |
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
self::localizeTOC( |
| 1719 |
$this->mOutput->getTOCData(), |
| 1720 |
$this->getTargetLanguage(), |
| 1721 |
$converter |
| 1722 |
); |
| 1723 |
if ( $converter ) { |
| 1724 |
$this->mOutput->setLanguage( new Bcp47CodeValue( |
| 1725 |
LanguageCode::bcp47( $converter->getPreferredVariant() ) |
| 1726 |
) ); |
| 1727 |
} else { |
| 1728 |
$this->mOutput->setLanguage( $this->getTargetLanguage() ); |
| 1729 |
} |
| 1730 |
|
| 1731 |
$text = $this->mStripState->unstripNoWiki( $text ); |
| 1732 |
|
| 1733 |
$text = $this->mStripState->unstripGeneral( $text ); |
| 1734 |
|
| 1735 |
$text = $this->tidy->tidy( $text, [ Sanitizer::class, 'armorFrenchSpaces' ] ); |
| 1736 |
|
| 1737 |
if ( $isMain ) { |
| 1738 |
$this->hookRunner->onParserAfterTidy( $this, $text ); |
| 1739 |
} |
| 1740 |
|
| 1741 |
return $text; |
| 1742 |
} |
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
private function handleMagicLinks( $text ) { |
| 1755 |
$prots = $this->urlUtils->validAbsoluteProtocols(); |
| 1756 |
$urlChar = self::EXT_LINK_URL_CLASS; |
| 1757 |
$addr = self::EXT_LINK_ADDR; |
| 1758 |
$space = self::SPACE_NOT_NL; |
| 1759 |
$spdash = "(?:-|$space)"; |
| 1760 |
$spaces = "$space++"; |
| 1761 |
$text = preg_replace_callback( |
| 1762 |
'!(?: # Start cases |
| 1763 |
(<a[ \t\r\n>].*?</a>) | # m[1]: Skip link text |
| 1764 |
(<.*?>) | # m[2]: Skip stuff inside HTML elements' . " |
| 1765 |
(\b # m[3]: Free external links |
| 1766 |
(?i:$prots) |
| 1767 |
($addr$urlChar*) # m[4]: Post-protocol path |
| 1768 |
) | |
| 1769 |
\b(?:RFC|PMID) $spaces # m[5]: RFC or PMID, capture number |
| 1770 |
([0-9]+)\b | |
| 1771 |
\bISBN $spaces ( # m[6]: ISBN, capture number |
| 1772 |
(?: 97[89] $spdash? )? # optional 13-digit ISBN prefix |
| 1773 |
(?: [0-9] $spdash? ){9} # 9 digits with opt. delimiters |
| 1774 |
[0-9Xx] # check digit |
| 1775 |
)\b |
| 1776 |
)!xu", |
| 1777 |
[ $this, 'magicLinkCallback' ], |
| 1778 |
$text |
| 1779 |
); |
| 1780 |
return $text; |
| 1781 |
} |
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
private function magicLinkCallback( array $m ) { |
| 1788 |
if ( isset( $m[1] ) && $m[1] !== '' ) { |
| 1789 |
|
| 1790 |
return $m[0]; |
| 1791 |
} elseif ( isset( $m[2] ) && $m[2] !== '' ) { |
| 1792 |
|
| 1793 |
return $m[0]; |
| 1794 |
} elseif ( isset( $m[3] ) && $m[3] !== '' ) { |
| 1795 |
|
| 1796 |
return $this->makeFreeExternalLink( $m[0], strlen( $m[4] ) ); |
| 1797 |
} elseif ( isset( $m[5] ) && $m[5] !== '' ) { |
| 1798 |
|
| 1799 |
if ( substr( $m[0], 0, 3 ) === 'RFC' ) { |
| 1800 |
if ( !$this->mOptions->getMagicRFCLinks() ) { |
| 1801 |
return $m[0]; |
| 1802 |
} |
| 1803 |
$keyword = 'RFC'; |
| 1804 |
$urlmsg = 'rfcurl'; |
| 1805 |
$cssClass = 'mw-magiclink-rfc'; |
| 1806 |
$trackingCat = 'magiclink-tracking-rfc'; |
| 1807 |
$id = $m[5]; |
| 1808 |
} elseif ( substr( $m[0], 0, 4 ) === 'PMID' ) { |
| 1809 |
if ( !$this->mOptions->getMagicPMIDLinks() ) { |
| 1810 |
return $m[0]; |
| 1811 |
} |
| 1812 |
$keyword = 'PMID'; |
| 1813 |
$urlmsg = 'pubmedurl'; |
| 1814 |
$cssClass = 'mw-magiclink-pmid'; |
| 1815 |
$trackingCat = 'magiclink-tracking-pmid'; |
| 1816 |
$id = $m[5]; |
| 1817 |
} else { |
| 1818 |
|
| 1819 |
throw new UnexpectedValueException( __METHOD__ . ': unrecognised match type "' . |
| 1820 |
substr( $m[0], 0, 20 ) . '"' ); |
| 1821 |
} |
| 1822 |
$url = wfMessage( $urlmsg, $id )->inContentLanguage()->text(); |
| 1823 |
$this->addTrackingCategory( $trackingCat ); |
| 1824 |
return $this->getLinkRenderer()->makeExternalLink( |
| 1825 |
$url, |
| 1826 |
"{$keyword} {$id}", |
| 1827 |
$this->getTitle(), |
| 1828 |
$cssClass, |
| 1829 |
[] |
| 1830 |
); |
| 1831 |
} elseif ( isset( $m[6] ) && $m[6] !== '' |
| 1832 |
&& $this->mOptions->getMagicISBNLinks() |
| 1833 |
) { |
| 1834 |
|
| 1835 |
$isbn = $m[6]; |
| 1836 |
$space = self::SPACE_NOT_NL; |
| 1837 |
$isbn = preg_replace( "/$space/", ' ', $isbn ); |
| 1838 |
$num = strtr( $isbn, [ |
| 1839 |
'-' => '', |
| 1840 |
' ' => '', |
| 1841 |
'x' => 'X', |
| 1842 |
] ); |
| 1843 |
$this->addTrackingCategory( 'magiclink-tracking-isbn' ); |
| 1844 |
return $this->getLinkRenderer()->makeKnownLink( |
| 1845 |
SpecialPage::getTitleFor( 'Booksources', $num ), |
| 1846 |
"ISBN $isbn", |
| 1847 |
[ |
| 1848 |
'class' => 'internal mw-magiclink-isbn', |
| 1849 |
'title' => false |
| 1850 |
] |
| 1851 |
); |
| 1852 |
} else { |
| 1853 |
return $m[0]; |
| 1854 |
} |
| 1855 |
} |
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
private function makeFreeExternalLink( $url, $numPostProto ) { |
| 1867 |
$trail = ''; |
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
$m2 = []; |
| 1874 |
if ( preg_match( |
| 1875 |
'/&(lt|gt|nbsp|#x0*(3[CcEe]|[Aa]0)|#0*(60|62|160));/', |
| 1876 |
$url, |
| 1877 |
$m2, |
| 1878 |
PREG_OFFSET_CAPTURE |
| 1879 |
) ) { |
| 1880 |
$trail = substr( $url, $m2[0][1] ) . $trail; |
| 1881 |
$url = substr( $url, 0, $m2[0][1] ); |
| 1882 |
} |
| 1883 |
|
| 1884 |
|
| 1885 |
$sep = ',;\.:!?'; |
| 1886 |
|
| 1887 |
if ( strpos( $url, '(' ) === false ) { |
| 1888 |
$sep .= ')'; |
| 1889 |
} |
| 1890 |
|
| 1891 |
$urlRev = strrev( $url ); |
| 1892 |
$numSepChars = strspn( $urlRev, $sep ); |
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
if ( $numSepChars && substr_compare( $url, ";", -$numSepChars, 1 ) === 0 ) { |
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
if ( preg_match( '/\G([a-z]+|[\da-f]+x#|\d+#)&/i', $urlRev, $m2, 0, $numSepChars ) ) { |
| 1902 |
$numSepChars--; |
| 1903 |
} |
| 1904 |
} |
| 1905 |
if ( $numSepChars ) { |
| 1906 |
$trail = substr( $url, -$numSepChars ) . $trail; |
| 1907 |
$url = substr( $url, 0, -$numSepChars ); |
| 1908 |
} |
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
if ( strlen( $trail ) >= $numPostProto ) { |
| 1913 |
return $url . $trail; |
| 1914 |
} |
| 1915 |
|
| 1916 |
$url = Sanitizer::cleanUrl( $url ); |
| 1917 |
|
| 1918 |
|
| 1919 |
$text = $this->maybeMakeExternalImage( $url ); |
| 1920 |
if ( $text === false ) { |
| 1921 |
|
| 1922 |
$text = $this->getLinkRenderer()->makeExternalLink( |
| 1923 |
$url, |
| 1924 |
$this->getTargetLanguageConverter()->markNoConversion( $url ), |
| 1925 |
$this->getTitle(), |
| 1926 |
'free', |
| 1927 |
$this->getExternalLinkAttribs( $url ) |
| 1928 |
); |
| 1929 |
|
| 1930 |
$this->mOutput->addExternalLink( $url ); |
| 1931 |
} |
| 1932 |
return $text . $trail; |
| 1933 |
} |
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
private function handleHeadings( $text ) { |
| 1942 |
for ( $i = 6; $i >= 1; --$i ) { |
| 1943 |
$h = str_repeat( '=', $i ); |
| 1944 |
|
| 1945 |
|
| 1946 |
$text = preg_replace( "/^(?:$h)[ \\t]*(.+?)[ \\t]*(?:$h)\\s*$/m", "<h$i>\\1</h$i>", $text ); |
| 1947 |
} |
| 1948 |
return $text; |
| 1949 |
} |
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
private function handleAllQuotes( $text ) { |
| 1959 |
$outtext = ''; |
| 1960 |
$lines = StringUtils::explode( "\n", $text ); |
| 1961 |
foreach ( $lines as $line ) { |
| 1962 |
$outtext .= $this->doQuotes( $line ) . "\n"; |
| 1963 |
} |
| 1964 |
$outtext = substr( $outtext, 0, -1 ); |
| 1965 |
return $outtext; |
| 1966 |
} |
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
public function doQuotes( $text ) { |
| 1977 |
$arr = preg_split( "/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE ); |
| 1978 |
$countarr = count( $arr ); |
| 1979 |
if ( $countarr == 1 ) { |
| 1980 |
return $text; |
| 1981 |
} |
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
$numbold = 0; |
| 1987 |
$numitalics = 0; |
| 1988 |
for ( $i = 1; $i < $countarr; $i += 2 ) { |
| 1989 |
$thislen = strlen( $arr[$i] ); |
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
if ( $thislen == 4 ) { |
| 1994 |
$arr[$i - 1] .= "'"; |
| 1995 |
$arr[$i] = "'''"; |
| 1996 |
$thislen = 3; |
| 1997 |
} elseif ( $thislen > 5 ) { |
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
$arr[$i - 1] .= str_repeat( "'", $thislen - 5 ); |
| 2002 |
$arr[$i] = "'''''"; |
| 2003 |
$thislen = 5; |
| 2004 |
} |
| 2005 |
|
| 2006 |
if ( $thislen == 2 ) { |
| 2007 |
$numitalics++; |
| 2008 |
} elseif ( $thislen == 3 ) { |
| 2009 |
$numbold++; |
| 2010 |
} elseif ( $thislen == 5 ) { |
| 2011 |
$numitalics++; |
| 2012 |
$numbold++; |
| 2013 |
} |
| 2014 |
} |
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
if ( ( $numbold % 2 == 1 ) && ( $numitalics % 2 == 1 ) ) { |
| 2021 |
$firstsingleletterword = -1; |
| 2022 |
$firstmultiletterword = -1; |
| 2023 |
$firstspace = -1; |
| 2024 |
for ( $i = 1; $i < $countarr; $i += 2 ) { |
| 2025 |
if ( strlen( $arr[$i] ) == 3 ) { |
| 2026 |
$x1 = substr( $arr[$i - 1], -1 ); |
| 2027 |
$x2 = substr( $arr[$i - 1], -2, 1 ); |
| 2028 |
if ( $x1 === ' ' ) { |
| 2029 |
if ( $firstspace == -1 ) { |
| 2030 |
$firstspace = $i; |
| 2031 |
} |
| 2032 |
} elseif ( $x2 === ' ' ) { |
| 2033 |
$firstsingleletterword = $i; |
| 2034 |
|
| 2035 |
|
| 2036 |
break; |
| 2037 |
} elseif ( $firstmultiletterword == -1 ) { |
| 2038 |
$firstmultiletterword = $i; |
| 2039 |
} |
| 2040 |
} |
| 2041 |
} |
| 2042 |
|
| 2043 |
|
| 2044 |
if ( $firstsingleletterword > -1 ) { |
| 2045 |
$arr[$firstsingleletterword] = "''"; |
| 2046 |
$arr[$firstsingleletterword - 1] .= "'"; |
| 2047 |
} elseif ( $firstmultiletterword > -1 ) { |
| 2048 |
|
| 2049 |
$arr[$firstmultiletterword] = "''"; |
| 2050 |
$arr[$firstmultiletterword - 1] .= "'"; |
| 2051 |
} elseif ( $firstspace > -1 ) { |
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
$arr[$firstspace] = "''"; |
| 2056 |
$arr[$firstspace - 1] .= "'"; |
| 2057 |
} |
| 2058 |
} |
| 2059 |
|
| 2060 |
|
| 2061 |
$output = ''; |
| 2062 |
$buffer = ''; |
| 2063 |
$state = ''; |
| 2064 |
$i = 0; |
| 2065 |
foreach ( $arr as $r ) { |
| 2066 |
if ( ( $i % 2 ) == 0 ) { |
| 2067 |
if ( $state === 'both' ) { |
| 2068 |
$buffer .= $r; |
| 2069 |
} else { |
| 2070 |
$output .= $r; |
| 2071 |
} |
| 2072 |
} else { |
| 2073 |
$thislen = strlen( $r ); |
| 2074 |
if ( $thislen == 2 ) { |
| 2075 |
|
| 2076 |
if ( $state === 'i' ) { |
| 2077 |
$output .= '</i>'; |
| 2078 |
$state = ''; |
| 2079 |
} elseif ( $state === 'bi' ) { |
| 2080 |
$output .= '</i>'; |
| 2081 |
$state = 'b'; |
| 2082 |
} elseif ( $state === 'ib' ) { |
| 2083 |
$output .= '</b></i><b>'; |
| 2084 |
$state = 'b'; |
| 2085 |
} elseif ( $state === 'both' ) { |
| 2086 |
$output .= '<b><i>' . $buffer . '</i>'; |
| 2087 |
$state = 'b'; |
| 2088 |
} else { |
| 2089 |
$output .= '<i>'; |
| 2090 |
$state .= 'i'; |
| 2091 |
} |
| 2092 |
} elseif ( $thislen == 3 ) { |
| 2093 |
|
| 2094 |
if ( $state === 'b' ) { |
| 2095 |
$output .= '</b>'; |
| 2096 |
$state = ''; |
| 2097 |
} elseif ( $state === 'bi' ) { |
| 2098 |
$output .= '</i></b><i>'; |
| 2099 |
$state = 'i'; |
| 2100 |
} elseif ( $state === 'ib' ) { |
| 2101 |
$output .= '</b>'; |
| 2102 |
$state = 'i'; |
| 2103 |
} elseif ( $state === 'both' ) { |
| 2104 |
$output .= '<i><b>' . $buffer . '</b>'; |
| 2105 |
$state = 'i'; |
| 2106 |
} else { |
| 2107 |
$output .= '<b>'; |
| 2108 |
$state .= 'b'; |
| 2109 |
} |
| 2110 |
} elseif ( $thislen == 5 ) { |
| 2111 |
|
| 2112 |
if ( $state === 'b' ) { |
| 2113 |
$output .= '</b><i>'; |
| 2114 |
$state = 'i'; |
| 2115 |
} elseif ( $state === 'i' ) { |
| 2116 |
$output .= '</i><b>'; |
| 2117 |
$state = 'b'; |
| 2118 |
} elseif ( $state === 'bi' ) { |
| 2119 |
$output .= '</i></b>'; |
| 2120 |
$state = ''; |
| 2121 |
} elseif ( $state === 'ib' ) { |
| 2122 |
$output .= '</b></i>'; |
| 2123 |
$state = ''; |
| 2124 |
} elseif ( $state === 'both' ) { |
| 2125 |
$output .= '<i><b>' . $buffer . '</b></i>'; |
| 2126 |
$state = ''; |
| 2127 |
} else { |
| 2128 |
$buffer = ''; |
| 2129 |
$state = 'both'; |
| 2130 |
} |
| 2131 |
} |
| 2132 |
} |
| 2133 |
$i++; |
| 2134 |
} |
| 2135 |
|
| 2136 |
if ( $state === 'b' || $state === 'ib' ) { |
| 2137 |
$output .= '</b>'; |
| 2138 |
} |
| 2139 |
if ( $state === 'i' || $state === 'bi' || $state === 'ib' ) { |
| 2140 |
$output .= '</i>'; |
| 2141 |
} |
| 2142 |
if ( $state === 'bi' ) { |
| 2143 |
$output .= '</b>'; |
| 2144 |
} |
| 2145 |
|
| 2146 |
if ( $state === 'both' && $buffer ) { |
| 2147 |
$output .= '<b><i>' . $buffer . '</i></b>'; |
| 2148 |
} |
| 2149 |
return $output; |
| 2150 |
} |
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
private function handleExternalLinks( $text ) { |
| 2162 |
$bits = preg_split( $this->mExtLinkBracketedRegex, $text, -1, PREG_SPLIT_DELIM_CAPTURE ); |
| 2163 |
|
| 2164 |
if ( $bits === false ) { |
| 2165 |
throw new RuntimeException( "PCRE failure" ); |
| 2166 |
} |
| 2167 |
$s = array_shift( $bits ); |
| 2168 |
|
| 2169 |
$i = 0; |
| 2170 |
while ( $i < count( $bits ) ) { |
| 2171 |
$url = $bits[$i++]; |
| 2172 |
$i++; |
| 2173 |
$text = $bits[$i++]; |
| 2174 |
$trail = $bits[$i++]; |
| 2175 |
|
| 2176 |
|
| 2177 |
|
| 2178 |
|
| 2179 |
$m2 = []; |
| 2180 |
if ( preg_match( '/&(lt|gt);/', $url, $m2, PREG_OFFSET_CAPTURE ) ) { |
| 2181 |
$text = substr( $url, $m2[0][1] ) . ' ' . $text; |
| 2182 |
$url = substr( $url, 0, $m2[0][1] ); |
| 2183 |
} |
| 2184 |
|
| 2185 |
|
| 2186 |
|
| 2187 |
$img = $this->maybeMakeExternalImage( $text ); |
| 2188 |
if ( $img !== false ) { |
| 2189 |
$text = $img; |
| 2190 |
} |
| 2191 |
|
| 2192 |
$dtrail = ''; |
| 2193 |
|
| 2194 |
|
| 2195 |
$linktype = 'text'; |
| 2196 |
|
| 2197 |
|
| 2198 |
if ( $text == '' ) { |
| 2199 |
|
| 2200 |
$langObj = $this->getTargetLanguage(); |
| 2201 |
$text = '[' . $langObj->formatNum( ++$this->mAutonumber ) . ']'; |
| 2202 |
$linktype = 'autonumber'; |
| 2203 |
} else { |
| 2204 |
|
| 2205 |
|
| 2206 |
[ $dtrail, $trail ] = Linker::splitTrail( $trail ); |
| 2207 |
} |
| 2208 |
|
| 2209 |
|
| 2210 |
if ( preg_match( '/^(?:' . $this->urlUtils->validAbsoluteProtocols() . ')/', $text ) ) { |
| 2211 |
$text = $this->getTargetLanguageConverter()->markNoConversion( $text ); |
| 2212 |
} |
| 2213 |
|
| 2214 |
$url = Sanitizer::cleanUrl( $url ); |
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
y |
| 2219 |
|
| 2220 |
$s .= $this->getLinkRenderer()->makeExternalLink( |
| 2221 |
$url, |
| 2222 |
|
| 2223 |
new HtmlArmor( $text ), |
| 2224 |
$this->getTitle(), |
| 2225 |
$linktype, |
| 2226 |
$this->getExternalLinkAttribs( $url ) |
| 2227 |
) . $dtrail . $trail; |
| 2228 |
|
| 2229 |
|
| 2230 |
$this->mOutput->addExternalLink( $url ); |
| 2231 |
} |
| 2232 |
|
| 2233 |
|
| 2234 |
return $s; |
| 2235 |
} |
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
public static function getExternalLinkRel( $url = false, ?LinkTarget $title = null ) { |
| 2248 |
$mainConfig = MediaWikiServices::getInstance()->getMainConfig(); |
| 2249 |
$noFollowLinks = $mainConfig->get( MainConfigNames::NoFollowLinks ); |
| 2250 |
$noFollowNsExceptions = $mainConfig->get( MainConfigNames::NoFollowNsExceptions ); |
| 2251 |
$noFollowDomainExceptions = $mainConfig->get( MainConfigNames::NoFollowDomainExceptions ); |
| 2252 |
$ns = $title ? $title->getNamespace() : false; |
| 2253 |
if ( |
| 2254 |
$noFollowLinks && !in_array( $ns, $noFollowNsExceptions ) |
| 2255 |
&& !wfGetUrlUtils()->matchesDomainList( (string)$url, $noFollowDomainExceptions ) |
| 2256 |
) { |
| 2257 |
return 'nofollow'; |
| 2258 |
} |
| 2259 |
return null; |
| 2260 |
} |
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
|
| 2268 |
|
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
|
| 2273 |
public function getExternalLinkAttribs( $url ) { |
| 2274 |
$attribs = []; |
| 2275 |
$rel = self::getExternalLinkRel( $url, $this->getTitle() ) ?? ''; |
| 2276 |
|
| 2277 |
$target = $this->mOptions->getExternalLinkTarget(); |
| 2278 |
if ( $target ) { |
| 2279 |
$attribs['target'] = $target; |
| 2280 |
if ( !in_array( $target, [ '_self', '_parent', '_top' ] ) ) { |
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
if ( $rel !== '' ) { |
| 2285 |
$rel .= ' '; |
| 2286 |
} |
| 2287 |
$rel .= 'noreferrer noopener'; |
| 2288 |
} |
| 2289 |
} |
| 2290 |
if ( $rel !== '' ) { |
| 2291 |
$attribs['rel'] = $rel; |
| 2292 |
} |
| 2293 |
return $attribs; |
| 2294 |
} |
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
public static function normalizeLinkUrl( $url ) { |
| 2307 |
|
| 2308 |
$scheme = '[a-z][a-z0-9+.-]*:'; |
| 2309 |
$userinfo = '(?:[a-z0-9\-._~!$&\'()*+,;=:]|%[0-9a-f]{2})*'; |
| 2310 |
$ipv6Host = '\\[((?:[0-9a-f:]|%3[0-A]|%[46][1-6])+)\\]'; |
| 2311 |
if ( preg_match( "<^(?:{$scheme})?//(?:{$userinfo}@)?{$ipv6Host}(?:[:/?#].*|)$>i", $url, $m ) && |
| 2312 |
IPUtils::isValid( rawurldecode( $m[1] ) ) |
| 2313 |
) { |
| 2314 |
$isIPv6 = rawurldecode( $m[1] ); |
| 2315 |
} else { |
| 2316 |
$isIPv6 = false; |
| 2317 |
} |
| 2318 |
|
| 2319 |
|
| 2320 |
$url = preg_replace_callback( |
| 2321 |
'/[\x00-\x20"<>\[\\\\\]^`{|}\x7F-\xFF]+/', |
| 2322 |
static fn ( $m ) => rawurlencode( $m[0] ), |
| 2323 |
$url |
| 2324 |
); |
| 2325 |
|
| 2326 |
$ret = ''; |
| 2327 |
$end = strlen( $url ); |
| 2328 |
|
| 2329 |
|
| 2330 |
$start = strpos( $url, '#' ); |
| 2331 |
if ( $start !== false && $start < $end ) { |
| 2332 |
$ret = self::normalizeUrlComponent( |
| 2333 |
substr( $url, $start, $end - $start ), '"#%<>[\]^`{|}' ) . $ret; |
| 2334 |
$end = $start; |
| 2335 |
} |
| 2336 |
|
| 2337 |
|
| 2338 |
$start = strpos( $url, '?' ); |
| 2339 |
if ( $start !== false && $start < $end ) { |
| 2340 |
$ret = self::normalizeUrlComponent( |
| 2341 |
substr( $url, $start, $end - $start ), '"#%<>[\]^`{|}&=+;' ) . $ret; |
| 2342 |
$end = $start; |
| 2343 |
} |
| 2344 |
|
| 2345 |
|
| 2346 |
|
| 2347 |
$start = strpos( $url, '//' ); |
| 2348 |
$start = strpos( $url, '/', $start === false ? 0 : $start + 2 ); |
| 2349 |
if ( $start !== false && $start < $end ) { |
| 2350 |
$ret = UrlUtils::removeDotSegments( self::normalizeUrlComponent( |
| 2351 |
substr( $url, $start, $end - $start ), '"#%<>[\]^`{|}/?' ) ) . $ret; |
| 2352 |
$end = $start; |
| 2353 |
} |
| 2354 |
|
| 2355 |
|
| 2356 |
|
| 2357 |
$ret = self::normalizeUrlComponent( |
| 2358 |
substr( $url, 0, $end ), '"#%<>[\]^`{|}/?' ) . $ret; |
| 2359 |
|
| 2360 |
|
| 2361 |
if ( $isIPv6 !== false ) { |
| 2362 |
$ipv6Host = "%5B({$isIPv6})%5D"; |
| 2363 |
$ret = preg_replace( |
| 2364 |
"<^((?:{$scheme})?//(?:{$userinfo}@)?){$ipv6Host}(?=[:/?#]|$)>i", |
| 2365 |
"$1[$2]", |
| 2366 |
$ret |
| 2367 |
); |
| 2368 |
} |
| 2369 |
|
| 2370 |
return $ret; |
| 2371 |
} |
| 2372 |
|
| 2373 |
private static function normalizeUrlComponent( $component, $unsafe ) { |
| 2374 |
$callback = static function ( $matches ) use ( $unsafe ) { |
| 2375 |
$char = urldecode( $matches[0] ); |
| 2376 |
$ord = ord( $char ); |
| 2377 |
if ( $ord > 32 && $ord < 127 && strpos( $unsafe, $char ) === false ) { |
| 2378 |
|
| 2379 |
return $char; |
| 2380 |
} else { |
| 2381 |
|
| 2382 |
return strtoupper( $matches[0] ); |
| 2383 |
} |
| 2384 |
}; |
| 2385 |
return preg_replace_callback( '/%[0-9A-Fa-f]{2}/', $callback, $component ); |
| 2386 |
} |
| 2387 |
|
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
private function maybeMakeExternalImage( $url ) { |
| 2397 |
$imagesfrom = $this->mOptions->getAllowExternalImagesFrom(); |
| 2398 |
$imagesexception = (bool)$imagesfrom; |
| 2399 |
$text = false; |
| 2400 |
|
| 2401 |
if ( $imagesexception && is_array( $imagesfrom ) ) { |
| 2402 |
$imagematch = false; |
| 2403 |
foreach ( $imagesfrom as $match ) { |
| 2404 |
if ( strpos( $url, $match ) === 0 ) { |
| 2405 |
$imagematch = true; |
| 2406 |
break; |
| 2407 |
} |
| 2408 |
} |
| 2409 |
} elseif ( $imagesexception ) { |
| 2410 |
$imagematch = ( strpos( $url, $imagesfrom ) === 0 ); |
| 2411 |
} else { |
| 2412 |
$imagematch = false; |
| 2413 |
} |
| 2414 |
|
| 2415 |
if ( $this->mOptions->getAllowExternalImages() |
| 2416 |
|| ( $imagesexception && $imagematch ) |
| 2417 |
) { |
| 2418 |
if ( preg_match( self::EXT_IMAGE_REGEX, $url ) ) { |
| 2419 |
|
| 2420 |
$text = Linker::makeExternalImage( $url ); |
| 2421 |
} |
| 2422 |
} |
| 2423 |
if ( !$text && $this->mOptions->getEnableImageWhitelist() |
| 2424 |
&& preg_match( self::EXT_IMAGE_REGEX, $url ) |
| 2425 |
) { |
| 2426 |
$whitelist = explode( |
| 2427 |
"\n", |
| 2428 |
wfMessage( 'external_image_whitelist' )->inContentLanguage()->text() |
| 2429 |
); |
| 2430 |
|
| 2431 |
foreach ( $whitelist as $entry ) { |
| 2432 |
|
| 2433 |
if ( strpos( $entry, '#' ) === 0 || $entry === '' ) { |
| 2434 |
continue; |
| 2435 |
} |
| 2436 |
|
| 2437 |
if ( preg_match( '/' . str_replace( '/', '\\/', $entry ) . '/i', $url ) ) { |
| 2438 |
|
| 2439 |
$text = Linker::makeExternalImage( $url ); |
| 2440 |
break; |
| 2441 |
} |
| 2442 |
} |
| 2443 |
} |
| 2444 |
return $text; |
| 2445 |
} |
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
|
| 2454 |
private function handleInternalLinks( $text ) { |
| 2455 |
$this->mLinkHolders->merge( $this->handleInternalLinks2( $text ) ); |
| 2456 |
return $text; |
| 2457 |
} |
| 2458 |
|
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
|
| 2463 |
|
| 2464 |
private function handleInternalLinks2( &$s ) { |
| 2465 |
static $tc = false, $e1, $e1_img; |
| 2466 |
|
| 2467 |
if ( !$tc ) { |
| 2468 |
$tc = Title::legalChars() . '#%'; |
| 2469 |
|
| 2470 |
$e1 = "/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD"; |
| 2471 |
|
| 2472 |
$e1_img = "/^([{$tc}]+)\\|(.*)\$/sD"; |
| 2473 |
} |
| 2474 |
|
| 2475 |
$holders = new LinkHolderArray( |
| 2476 |
$this, |
| 2477 |
$this->getContentLanguageConverter(), |
| 2478 |
$this->getHookContainer() ); |
| 2479 |
|
| 2480 |
|
| 2481 |
$a = StringUtils::explode( '[[', ' ' . $s ); |
| 2482 |
|
| 2483 |
$s = $a->current(); |
| 2484 |
$a->next(); |
| 2485 |
$line = $a->current(); |
| 2486 |
$s = substr( $s, 1 ); |
| 2487 |
|
| 2488 |
$nottalk = !$this->getTitle()->isTalkPage(); |
| 2489 |
|
| 2490 |
$useLinkPrefixExtension = $this->getTargetLanguage()->linkPrefixExtension(); |
| 2491 |
$e2 = null; |
| 2492 |
if ( $useLinkPrefixExtension ) { |
| 2493 |
|
| 2494 |
|
| 2495 |
$charset = $this->contLang->linkPrefixCharset(); |
| 2496 |
$e2 = "/^((?>.*[^$charset]|))(.+)$/sDu"; |
| 2497 |
$m = []; |
| 2498 |
if ( preg_match( $e2, $s, $m ) ) { |
| 2499 |
$first_prefix = $m[2]; |
| 2500 |
} else { |
| 2501 |
$first_prefix = false; |
| 2502 |
} |
| 2503 |
$prefix = false; |
| 2504 |
} else { |
| 2505 |
$first_prefix = false; |
| 2506 |
$prefix = ''; |
| 2507 |
} |
| 2508 |
|
| 2509 |
|
| 2510 |
$useSubpages = $this->nsInfo->hasSubpages( |
| 2511 |
$this->getTitle()->getNamespace() |
| 2512 |
); |
| 2513 |
|
| 2514 |
|
| 2515 |
for ( ; $line !== false && $line !== null; $a->next(), $line = $a->current() ) { |
| 2516 |
|
| 2517 |
if ( $holders->isBig() ) { |
| 2518 |
|
| 2519 |
|
| 2520 |
$holders->replace( $s ); |
| 2521 |
$holders->clear(); |
| 2522 |
} |
| 2523 |
|
| 2524 |
if ( $useLinkPrefixExtension ) { |
| 2525 |
|
| 2526 |
if ( preg_match( $e2, $s, $m ) ) { |
| 2527 |
[ , $s, $prefix ] = $m; |
| 2528 |
} else { |
| 2529 |
$prefix = ''; |
| 2530 |
} |
| 2531 |
|
| 2532 |
if ( $first_prefix ) { |
| 2533 |
$prefix = $first_prefix; |
| 2534 |
$first_prefix = false; |
| 2535 |
} |
| 2536 |
} |
| 2537 |
|
| 2538 |
$might_be_img = false; |
| 2539 |
|
| 2540 |
if ( preg_match( $e1, $line, $m ) ) { |
| 2541 |
$text = $m[2]; |
| 2542 |
|
| 2543 |
|
| 2544 |
|
| 2545 |
|
| 2546 |
|
| 2547 |
|
| 2548 |
if ( $text !== '' |
| 2549 |
&& substr( $m[3], 0, 1 ) === ']' |
| 2550 |
&& strpos( $text, '[' ) !== false |
| 2551 |
) { |
| 2552 |
$text .= ']'; |
| 2553 |
$m[3] = substr( $m[3], 1 ); |
| 2554 |
} |
| 2555 |
|
| 2556 |
if ( strpos( $m[1], '%' ) !== false ) { |
| 2557 |
|
| 2558 |
$m[1] = str_replace( [ '<', '>' ], [ '<', '>' ], rawurldecode( $m[1] ) ); |
| 2559 |
} |
| 2560 |
$trail = $m[3]; |
| 2561 |
} elseif ( preg_match( $e1_img, $line, $m ) ) { |
| 2562 |
|
| 2563 |
$might_be_img = true; |
| 2564 |
$text = $m[2]; |
| 2565 |
if ( strpos( $m[1], '%' ) !== false ) { |
| 2566 |
$m[1] = str_replace( [ '<', '>' ], [ '<', '>' ], rawurldecode( $m[1] ) ); |
| 2567 |
} |
| 2568 |
$trail = ""; |
| 2569 |
} else { |
| 2570 |
$s .= $prefix . '[[' . $line; |
| 2571 |
continue; |
| 2572 |
} |
| 2573 |
|
| 2574 |
|
| 2575 |
$origLink = ltrim( $m[1], ' ' ); |
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
|
| 2580 |
if ( preg_match( '/^(?i:' . $this->urlUtils->validProtocols() . ')/', $origLink ) ) { |
| 2581 |
$s .= $prefix . '[[' . $line; |
| 2582 |
continue; |
| 2583 |
} |
| 2584 |
|
| 2585 |
|
| 2586 |
if ( $useSubpages ) { |
| 2587 |
$link = Linker::normalizeSubpageLink( |
| 2588 |
$this->getTitle(), $origLink, $text |
| 2589 |
); |
| 2590 |
} else { |
| 2591 |
$link = $origLink; |
| 2592 |
} |
| 2593 |
|
| 2594 |
|
| 2595 |
|
| 2596 |
|
| 2597 |
|
| 2598 |
$unstrip = $this->mStripState->killMarkers( $link ); |
| 2599 |
$noMarkers = ( $unstrip === $link ); |
| 2600 |
|
| 2601 |
$nt = $noMarkers ? Title::newFromText( $link ) : null; |
| 2602 |
if ( $nt === null ) { |
| 2603 |
$s .= $prefix . '[[' . $line; |
| 2604 |
continue; |
| 2605 |
} |
| 2606 |
|
| 2607 |
$ns = $nt->getNamespace(); |
| 2608 |
$iw = $nt->getInterwiki(); |
| 2609 |
|
| 2610 |
$noforce = ( substr( $origLink, 0, 1 ) !== ':' ); |
| 2611 |
|
| 2612 |
if ( $might_be_img ) { |
| 2613 |
if ( $ns === NS_FILE && $noforce ) { |
| 2614 |
$found = false; |
| 2615 |
while ( true ) { |
| 2616 |
|
| 2617 |
$a->next(); |
| 2618 |
$next_line = $a->current(); |
| 2619 |
if ( $next_line === false || $next_line === null ) { |
| 2620 |
break; |
| 2621 |
} |
| 2622 |
$m = explode( ']]', $next_line, 3 ); |
| 2623 |
if ( count( $m ) == 3 ) { |
| 2624 |
|
| 2625 |
$found = true; |
| 2626 |
$text .= "[[{$m[0]}]]{$m[1]}"; |
| 2627 |
$trail = $m[2]; |
| 2628 |
break; |
| 2629 |
} elseif ( count( $m ) == 2 ) { |
| 2630 |
|
| 2631 |
$text .= "[[{$m[0]}]]{$m[1]}"; |
| 2632 |
} else { |
| 2633 |
|
| 2634 |
$text .= '[[' . $next_line; |
| 2635 |
break; |
| 2636 |
} |
| 2637 |
} |
| 2638 |
if ( !$found ) { |
| 2639 |
|
| 2640 |
|
| 2641 |
$holders->merge( $this->handleInternalLinks2( $text ) ); |
| 2642 |
$s .= "{$prefix}[[$link|$text"; |
| 2643 |
|
| 2644 |
continue; |
| 2645 |
} |
| 2646 |
} else { |
| 2647 |
$s .= "{$prefix}[[$link|$text"; |
| 2648 |
|
| 2649 |
continue; |
| 2650 |
} |
| 2651 |
} |
| 2652 |
|
| 2653 |
$wasblank = ( $text == '' ); |
| 2654 |
if ( $wasblank ) { |
| 2655 |
$text = $link; |
| 2656 |
if ( !$noforce ) { |
| 2657 |
|
| 2658 |
$text = substr( $text, 1 ); |
| 2659 |
} |
| 2660 |
} else { |
| 2661 |
|
| 2662 |
|
| 2663 |
|
| 2664 |
|
| 2665 |
$text = $this->doQuotes( $text ); |
| 2666 |
} |
| 2667 |
|
| 2668 |
|
| 2669 |
if ( $noforce && !$nt->wasLocalInterwiki() ) { |
| 2670 |
|
| 2671 |
if ( |
| 2672 |
$iw && $this->mOptions->getInterwikiMagic() && $nottalk && ( |
| 2673 |
$this->languageNameUtils->getLanguageName( |
| 2674 |
$iw, |
| 2675 |
LanguageNameUtils::AUTONYMS, |
| 2676 |
LanguageNameUtils::DEFINED |
| 2677 |
) |
| 2678 |
|| in_array( $iw, $this->svcOptions->get( MainConfigNames::ExtraInterlanguageLinkPrefixes ) ) |
| 2679 |
) |
| 2680 |
) { |
| 2681 |
|
| 2682 |
$this->mOutput->addLanguageLink( $nt ); |
| 2683 |
|
| 2684 |
|
| 2685 |
|
| 2686 |
|
| 2687 |
|
| 2688 |
$s = preg_replace( '/\n\s*$/', '', $s . $prefix ) . $trail; |
| 2689 |
continue; |
| 2690 |
} |
| 2691 |
|
| 2692 |
if ( $ns === NS_FILE ) { |
| 2693 |
if ( $wasblank ) { |
| 2694 |
|
| 2695 |
|
| 2696 |
|
| 2697 |
|
| 2698 |
$text = ''; |
| 2699 |
} else { |
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
$text = $this->handleExternalLinks( $text ); |
| 2704 |
$holders->merge( $this->handleInternalLinks2( $text ) ); |
| 2705 |
} |
| 2706 |
|
| 2707 |
$s .= $prefix . $this->armorLinks( |
| 2708 |
$this->makeImage( $nt, $text, $holders ) ) . $trail; |
| 2709 |
continue; |
| 2710 |
} elseif ( $ns === NS_CATEGORY ) { |
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
$s = preg_replace( '/\n\s*$/', '', $s . $prefix ) . $trail; |
| 2715 |
|
| 2716 |
$sortkey = ''; |
| 2717 |
if ( !$wasblank ) { |
| 2718 |
$sortkey = $text; |
| 2719 |
} |
| 2720 |
$this->mOutput->addCategory( $nt, $sortkey ); |
| 2721 |
|
| 2722 |
continue; |
| 2723 |
} |
| 2724 |
} |
| 2725 |
|
| 2726 |
|
| 2727 |
|
| 2728 |
|
| 2729 |
if ( $ns !== NS_SPECIAL && $nt->equals( $this->getTitle() ) ) { |
| 2730 |
$s .= $prefix . Linker::makeSelfLinkObj( $nt, $text, '', $trail, '', |
| 2731 |
Sanitizer::escapeIdForLink( $nt->getFragment() ) ); |
| 2732 |
continue; |
| 2733 |
} |
| 2734 |
|
| 2735 |
|
| 2736 |
|
| 2737 |
if ( $ns === NS_MEDIA ) { |
| 2738 |
|
| 2739 |
$options = []; |
| 2740 |
$descQuery = false; |
| 2741 |
$this->hookRunner->onBeforeParserFetchFileAndTitle( |
| 2742 |
|
| 2743 |
$this, $nt, $options, $descQuery |
| 2744 |
); |
| 2745 |
|
| 2746 |
[ $file, $nt ] = $this->fetchFileAndTitle( $nt, $options ); |
| 2747 |
|
| 2748 |
$s .= $prefix . $this->armorLinks( |
| 2749 |
Linker::makeMediaLinkFile( $nt, $file, $text ) ) . $trail; |
| 2750 |
continue; |
| 2751 |
} |
| 2752 |
|
| 2753 |
|
| 2754 |
|
| 2755 |
|
| 2756 |
|
| 2757 |
if ( $iw == '' && $nt->isAlwaysKnown() ) { |
| 2758 |
$this->mOutput->addLink( $nt ); |
| 2759 |
$s .= $this->makeKnownLinkHolder( $nt, $text, $trail, $prefix ); |
| 2760 |
} else { |
| 2761 |
|
| 2762 |
$s .= $holders->makeHolder( $nt, $text, $trail, $prefix ); |
| 2763 |
} |
| 2764 |
} |
| 2765 |
return $holders; |
| 2766 |
} |
| 2767 |
|
| 2768 |
|
| 2769 |
|
| 2770 |
|
| 2771 |
|
| 2772 |
|
| 2773 |
|
| 2774 |
|
| 2775 |
|
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
private function makeKnownLinkHolder( LinkTarget $nt, $text = '', $trail = '', $prefix = '' ) { |
| 2782 |
[ $inside, $trail ] = Linker::splitTrail( $trail ); |
| 2783 |
|
| 2784 |
if ( $text == '' ) { |
| 2785 |
$text = htmlspecialchars( $this->titleFormatter->getPrefixedText( $nt ) ); |
| 2786 |
} |
| 2787 |
|
| 2788 |
$link = $this->getLinkRenderer()->makeKnownLink( |
| 2789 |
$nt, new HtmlArmor( "$prefix$text$inside" ) |
| 2790 |
); |
| 2791 |
|
| 2792 |
return $this->armorLinks( $link ) . $trail; |
| 2793 |
} |
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
private function armorLinks( $text ) { |
| 2806 |
return preg_replace( '/\b((?i)' . $this->urlUtils->validProtocols() . ')/', |
| 2807 |
self::MARKER_PREFIX . "NOPARSE$1", $text ); |
| 2808 |
} |
| 2809 |
|
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
|
| 2815 |
|
| 2816 |
|
| 2817 |
|
| 2818 |
|
| 2819 |
public function doBlockLevels( $text, $linestart ) { |
| 2820 |
wfDeprecated( __METHOD__, '1.35' ); |
| 2821 |
return BlockLevelPass::doBlockLevels( $text, $linestart ); |
| 2822 |
} |
| 2823 |
|
| 2824 |
|
| 2825 |
|
| 2826 |
|
| 2827 |
|
| 2828 |
|
| 2829 |
|
| 2830 |
|
| 2831 |
|
| 2832 |
private function expandMagicVariable( $index, $frame = false ) { |
| 2833 |
|
| 2834 |
|
| 2835 |
|
| 2836 |
|
| 2837 |
if ( isset( $this->mVarCache[$index] ) ) { |
| 2838 |
return $this->mVarCache[$index]; |
| 2839 |
} |
| 2840 |
|
| 2841 |
$ts = new MWTimestamp( $this->mOptions->getTimestamp() ); |
| 2842 |
if ( $this->hookContainer->isRegistered( 'ParserGetVariableValueTs' ) ) { |
| 2843 |
$s = $ts->getTimestamp( TS_UNIX ); |
| 2844 |
$this->hookRunner->onParserGetVariableValueTs( $this, $s ); |
| 2845 |
$ts = new MWTimestamp( $s ); |
| 2846 |
} |
| 2847 |
|
| 2848 |
$value = CoreMagicVariables::expand( |
| 2849 |
$this, $index, $ts, $this->svcOptions, $this->logger |
| 2850 |
); |
| 2851 |
|
| 2852 |
if ( $value === null ) { |
| 2853 |
|
| 2854 |
|
| 2855 |
$fakeCache = []; |
| 2856 |
$this->hookRunner->onParserGetVariableValueSwitch( |
| 2857 |
|
| 2858 |
$this, $fakeCache, $index, $value, $frame |
| 2859 |
); |
| 2860 |
|
| 2861 |
|
| 2862 |
'@phan-var string $value'; |
| 2863 |
} |
| 2864 |
|
| 2865 |
$this->mVarCache[$index] = $value; |
| 2866 |
|
| 2867 |
return $value; |
| 2868 |
} |
| 2869 |
|
| 2870 |
|
| 2871 |
|
| 2872 |
|
| 2873 |
|
| 2874 |
private function initializeVariables() { |
| 2875 |
$variableIDs = $this->magicWordFactory->getVariableIDs(); |
| 2876 |
|
| 2877 |
$this->mVariables = $this->magicWordFactory->newArray( $variableIDs ); |
| 2878 |
$this->mSubstWords = $this->magicWordFactory->getSubstArray(); |
| 2879 |
} |
| 2880 |
|
| 2881 |
|
| 2882 |
|
| 2883 |
|
| 2884 |
|
| 2885 |
|
| 2886 |
|
| 2887 |
|
| 2888 |
|
| 2889 |
|
| 2890 |
|
| 2891 |
|
| 2892 |
|
| 2893 |
|
| 2894 |
|
| 2895 |
|
| 2896 |
|
| 2897 |
|
| 2898 |
|
| 2899 |
public function preprocessToDom( $text, $flags = 0 ) { |
| 2900 |
return $this->getPreprocessor()->preprocessToObj( $text, $flags ); |
| 2901 |
} |
| 2902 |
|
| 2903 |
|
| 2904 |
|
| 2905 |
|
| 2906 |
|
| 2907 |
|
| 2908 |
|
| 2909 |
|
| 2910 |
|
| 2911 |
|
| 2912 |
|
| 2913 |
|
| 2914 |
|
| 2915 |
|
| 2916 |
|
| 2917 |
|
| 2918 |
|
| 2919 |
|
| 2920 |
|
| 2921 |
|
| 2922 |
|
| 2923 |
|
| 2924 |
public function replaceVariables( $text, $frame = false, $argsOnly = false ) { |
| 2925 |
|
| 2926 |
$textSize = strlen( $text ); |
| 2927 |
if ( $textSize < 1 || $textSize > $this->mOptions->getMaxIncludeSize() ) { |
| 2928 |
return $text; |
| 2929 |
} |
| 2930 |
|
| 2931 |
if ( $frame === false ) { |
| 2932 |
$frame = $this->getPreprocessor()->newFrame(); |
| 2933 |
} elseif ( !( $frame instanceof PPFrame ) ) { |
| 2934 |
wfDeprecated( |
| 2935 |
__METHOD__ . " called using plain parameters instead of " . |
| 2936 |
"a PPFrame instance. Creating custom frame.", |
| 2937 |
'1.43' |
| 2938 |
); |
| 2939 |
$frame = $this->getPreprocessor()->newCustomFrame( $frame ); |
| 2940 |
} |
| 2941 |
|
| 2942 |
$dom = $this->preprocessToDom( $text ); |
| 2943 |
$flags = $argsOnly ? PPFrame::NO_TEMPLATES : 0; |
| 2944 |
$text = $frame->expand( $dom, $flags ); |
| 2945 |
|
| 2946 |
return $text; |
| 2947 |
} |
| 2948 |
|
| 2949 |
|
| 2950 |
|
| 2951 |
|
| 2952 |
|
| 2953 |
|
| 2954 |
|
| 2955 |
|
| 2956 |
|
| 2957 |
|
| 2958 |
|
| 2959 |
|
| 2960 |
|
| 2961 |
|
| 2962 |
|
| 2963 |
|
| 2964 |
|
| 2965 |
|
| 2966 |
|
| 2967 |
|
| 2968 |
|
| 2969 |
|
| 2970 |
|
| 2971 |
|
| 2972 |
|
| 2973 |
|
| 2974 |
|
| 2975 |
|
| 2976 |
public function limitationWarn( $limitationType, $current = '', $max = '' ) { |
| 2977 |
|
| 2978 |
|
| 2979 |
|
| 2980 |
$this->mOutput->addWarningMsg( |
| 2981 |
"$limitationType-warning", |
| 2982 |
Message::numParam( $current ), |
| 2983 |
Message::numParam( $max ) |
| 2984 |
); |
| 2985 |
$this->addTrackingCategory( "$limitationType-category" ); |
| 2986 |
} |
| 2987 |
|
| 2988 |
|
| 2989 |
|
| 2990 |
|
| 2991 |
|
| 2992 |
|
| 2993 |
|
| 2994 |
|
| 2995 |
|
| 2996 |
|
| 2997 |
|
| 2998 |
|
| 2999 |
|
| 3000 |
|
| 3001 |
public function braceSubstitution( array $piece, PPFrame $frame ) { |
| 3002 |
|
| 3003 |
|
| 3004 |
|
| 3005 |
$found = false; |
| 3006 |
$text = ''; |
| 3007 |
|
| 3008 |
$nowiki = false; |
| 3009 |
|
| 3010 |
$isHTML = false; |
| 3011 |
|
| 3012 |
$forceRawInterwiki = false; |
| 3013 |
|
| 3014 |
$isChildObj = false; |
| 3015 |
|
| 3016 |
$isLocalObj = false; |
| 3017 |
|
| 3018 |
|
| 3019 |
$title = false; |
| 3020 |
|
| 3021 |
|
| 3022 |
|
| 3023 |
$titleWithSpaces = $frame->expand( $piece['title'] ); |
| 3024 |
$part1 = trim( $titleWithSpaces ); |
| 3025 |
$titleText = false; |
| 3026 |
|
| 3027 |
|
| 3028 |
$originalTitle = $part1; |
| 3029 |
|
| 3030 |
|
| 3031 |
$args = $piece['parts']; |
| 3032 |
|
| 3033 |
$profileSection = null; |
| 3034 |
|
| 3035 |
$sawDeprecatedTemplateEquals = false; |
| 3036 |
|
| 3037 |
|
| 3038 |
|
| 3039 |
if ( !$found ) { |
| 3040 |
$substMatch = $this->mSubstWords->matchStartAndRemove( $part1 ); |
| 3041 |
$part1 = trim( $part1 ); |
| 3042 |
|
| 3043 |
|
| 3044 |
|
| 3045 |
if ( $this->ot['wiki'] ) { |
| 3046 |
if ( $substMatch === false ) { |
| 3047 |
$literal = true; |
| 3048 |
} else { |
| 3049 |
$literal = false; |
| 3050 |
} |
| 3051 |
} else { |
| 3052 |
if ( $substMatch == 'subst' ) { |
| 3053 |
$literal = true; |
| 3054 |
} else { |
| 3055 |
$literal = false; |
| 3056 |
} |
| 3057 |
} |
| 3058 |
if ( $literal ) { |
| 3059 |
$text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args ); |
| 3060 |
$isLocalObj = true; |
| 3061 |
$found = true; |
| 3062 |
} |
| 3063 |
} |
| 3064 |
|
| 3065 |
|
| 3066 |
if ( !$found && $args->getLength() == 0 ) { |
| 3067 |
$id = $this->mVariables->matchStartToEnd( $part1 ); |
| 3068 |
if ( $id !== false ) { |
| 3069 |
if ( strpos( $part1, ':' ) !== false ) { |
| 3070 |
wfDeprecatedMsg( |
| 3071 |
'Registering a magic variable with a name including a colon', |
| 3072 |
'1.39', false, false |
| 3073 |
); |
| 3074 |
} |
| 3075 |
$text = $this->expandMagicVariable( $id, $frame ); |
| 3076 |
$found = true; |
| 3077 |
} |
| 3078 |
} |
| 3079 |
|
| 3080 |
|
| 3081 |
if ( !$found ) { |
| 3082 |
|
| 3083 |
$mwMsgnw = $this->magicWordFactory->get( 'msgnw' ); |
| 3084 |
if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) { |
| 3085 |
$nowiki = true; |
| 3086 |
} else { |
| 3087 |
|
| 3088 |
$mwMsg = $this->magicWordFactory->get( 'msg' ); |
| 3089 |
$mwMsg->matchStartAndRemove( $part1 ); |
| 3090 |
} |
| 3091 |
|
| 3092 |
|
| 3093 |
$mwRaw = $this->magicWordFactory->get( 'raw' ); |
| 3094 |
if ( $mwRaw->matchStartAndRemove( $part1 ) ) { |
| 3095 |
$forceRawInterwiki = true; |
| 3096 |
} |
| 3097 |
} |
| 3098 |
|
| 3099 |
|
| 3100 |
if ( !$found ) { |
| 3101 |
$colonPos = strpos( $part1, ':' ); |
| 3102 |
if ( $colonPos !== false ) { |
| 3103 |
$func = substr( $part1, 0, $colonPos ); |
| 3104 |
$funcArgs = [ trim( substr( $part1, $colonPos + 1 ) ) ]; |
| 3105 |
$argsLength = $args->getLength(); |
| 3106 |
for ( $i = 0; $i < $argsLength; $i++ ) { |
| 3107 |
$funcArgs[] = $args->item( $i ); |
| 3108 |
} |
| 3109 |
|
| 3110 |
$result = $this->callParserFunction( $frame, $func, $funcArgs ); |
| 3111 |
|
| 3112 |
|
| 3113 |
if ( isset( $result['title'] ) ) { |
| 3114 |
$title = $result['title']; |
| 3115 |
} |
| 3116 |
if ( isset( $result['found'] ) ) { |
| 3117 |
$found = $result['found']; |
| 3118 |
} |
| 3119 |
if ( array_key_exists( 'text', $result ) ) { |
| 3120 |
|
| 3121 |
$text = $result['text']; |
| 3122 |
} |
| 3123 |
if ( isset( $result['nowiki'] ) ) { |
| 3124 |
$nowiki = $result['nowiki']; |
| 3125 |
} |
| 3126 |
if ( isset( $result['isHTML'] ) ) { |
| 3127 |
$isHTML = $result['isHTML']; |
| 3128 |
} |
| 3129 |
if ( isset( $result['forceRawInterwiki'] ) ) { |
| 3130 |
$forceRawInterwiki = $result['forceRawInterwiki']; |
| 3131 |
} |
| 3132 |
if ( isset( $result['isChildObj'] ) ) { |
| 3133 |
$isChildObj = $result['isChildObj']; |
| 3134 |
} |
| 3135 |
if ( isset( $result['isLocalObj'] ) ) { |
| 3136 |
$isLocalObj = $result['isLocalObj']; |
| 3137 |
} |
| 3138 |
} |
| 3139 |
} |
| 3140 |
|
| 3141 |
|
| 3142 |
|
| 3143 |
if ( !$found ) { |
| 3144 |
$ns = NS_TEMPLATE; |
| 3145 |
|
| 3146 |
$subpage = ''; |
| 3147 |
$relative = Linker::normalizeSubpageLink( |
| 3148 |
$this->getTitle(), $part1, $subpage |
| 3149 |
); |
| 3150 |
if ( $part1 !== $relative ) { |
| 3151 |
$part1 = $relative; |
| 3152 |
$ns = $this->getTitle()->getNamespace(); |
| 3153 |
} |
| 3154 |
$title = Title::newFromText( $part1, $ns ); |
| 3155 |
if ( $title ) { |
| 3156 |
$titleText = $title->getPrefixedText(); |
| 3157 |
|
| 3158 |
if ( $this->getTargetLanguageConverter()->hasVariants() && $title->getArticleID() == 0 ) { |
| 3159 |
$this->getTargetLanguageConverter()->findVariantLink( $part1, $title, true ); |
| 3160 |
} |
| 3161 |
|
| 3162 |
$limit = $this->mOptions->getMaxTemplateDepth(); |
| 3163 |
if ( $frame->depth >= $limit ) { |
| 3164 |
$found = true; |
| 3165 |
$text = '<span class="error">' |
| 3166 |
. wfMessage( 'parser-template-recursion-depth-warning' ) |
| 3167 |
->numParams( $limit )->inContentLanguage()->text() |
| 3168 |
. '</span>'; |
| 3169 |
} |
| 3170 |
} |
| 3171 |
} |
| 3172 |
|
| 3173 |
|
| 3174 |
if ( !$found && $title ) { |
| 3175 |
$profileSection = $this->mProfiler->scopedProfileIn( $title->getPrefixedDBkey() ); |
| 3176 |
if ( !$title->isExternal() ) { |
| 3177 |
if ( $title->isSpecialPage() |
| 3178 |
&& $this->mOptions->getAllowSpecialInclusion() |
| 3179 |
&& $this->ot['html'] |
| 3180 |
) { |
| 3181 |
$specialPage = $this->specialPageFactory->getPage( $title->getDBkey() ); |
| 3182 |
|
| 3183 |
|
| 3184 |
|
| 3185 |
$pageArgs = []; |
| 3186 |
$argsLength = $args->getLength(); |
| 3187 |
for ( $i = 0; $i < $argsLength; $i++ ) { |
| 3188 |
$bits = $args->item( $i )->splitArg(); |
| 3189 |
if ( strval( $bits['index'] ) === '' ) { |
| 3190 |
$name = trim( $frame->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) ); |
| 3191 |
$value = trim( $frame->expand( $bits['value'] ) ); |
| 3192 |
$pageArgs[$name] = $value; |
| 3193 |
} |
| 3194 |
} |
| 3195 |
|
| 3196 |
|
| 3197 |
if ( $this->incrementExpensiveFunctionCount() ) { |
| 3198 |
$context = new RequestContext; |
| 3199 |
$context->setTitle( $title ); |
| 3200 |
$context->setRequest( new FauxRequest( $pageArgs ) ); |
| 3201 |
if ( $specialPage && $specialPage->maxIncludeCacheTime() === 0 ) { |
| 3202 |
$context->setUser( $this->userFactory->newFromUserIdentity( $this->getUserIdentity() ) ); |
| 3203 |
} else { |
| 3204 |
|
| 3205 |
$context->setUser( User::newFromName( '127.0.0.1', false ) ); |
| 3206 |
} |
| 3207 |
$context->setLanguage( $this->mOptions->getUserLangObj() ); |
| 3208 |
$ret = $this->specialPageFactory->capturePath( $title, $context, $this->getLinkRenderer() ); |
| 3209 |
if ( $ret ) { |
| 3210 |
$text = $context->getOutput()->getHTML(); |
| 3211 |
$this->mOutput->addOutputPageMetadata( $context->getOutput() ); |
| 3212 |
$found = true; |
| 3213 |
$isHTML = true; |
| 3214 |
if ( $specialPage && $specialPage->maxIncludeCacheTime() !== false ) { |
| 3215 |
$this->mOutput->updateRuntimeAdaptiveExpiry( |
| 3216 |
$specialPage->maxIncludeCacheTime() |
| 3217 |
); |
| 3218 |
} |
| 3219 |
} |
| 3220 |
} |
| 3221 |
} elseif ( $this->nsInfo->isNonincludable( $title->getNamespace() ) ) { |
| 3222 |
$found = false; |
| 3223 |
$this->logger->debug( |
| 3224 |
__METHOD__ . |
| 3225 |
": template inclusion denied for " . $title->getPrefixedDBkey() |
| 3226 |
); |
| 3227 |
} else { |
| 3228 |
[ $text, $title ] = $this->getTemplateDom( $title ); |
| 3229 |
if ( $text !== false ) { |
| 3230 |
$found = true; |
| 3231 |
$isChildObj = true; |
| 3232 |
if ( |
| 3233 |
$title->getNamespace() === NS_TEMPLATE && |
| 3234 |
$title->getDBkey() === '=' && |
| 3235 |
$originalTitle === '=' |
| 3236 |
) { |
| 3237 |
|
| 3238 |
|
| 3239 |
|
| 3240 |
|
| 3241 |
$sawDeprecatedTemplateEquals = true; |
| 3242 |
} |
| 3243 |
} |
| 3244 |
} |
| 3245 |
|
| 3246 |
|
| 3247 |
if ( !$found && ( $this->ot['html'] || $this->ot['pre'] ) ) { |
| 3248 |
$text = "[[:$titleText]]"; |
| 3249 |
$found = true; |
| 3250 |
} |
| 3251 |
} elseif ( $title->isTrans() ) { |
| 3252 |
|
| 3253 |
if ( $this->ot['html'] && !$forceRawInterwiki ) { |
| 3254 |
$text = $this->interwikiTransclude( $title, 'render' ); |
| 3255 |
$isHTML = true; |
| 3256 |
} else { |
| 3257 |
$text = $this->interwikiTransclude( $title, 'raw' ); |
| 3258 |
|
| 3259 |
$text = $this->preprocessToDom( $text, Preprocessor::DOM_FOR_INCLUSION ); |
| 3260 |
$isChildObj = true; |
| 3261 |
} |
| 3262 |
$found = true; |
| 3263 |
} |
| 3264 |
|
| 3265 |
|
| 3266 |
|
| 3267 |
if ( !$frame->loopCheck( $title ) ) { |
| 3268 |
$found = true; |
| 3269 |
$text = '<span class="error">' |
| 3270 |
. wfMessage( 'parser-template-loop-warning', $titleText )->inContentLanguage()->text() |
| 3271 |
. '</span>'; |
| 3272 |
$this->addTrackingCategory( 'template-loop-category' ); |
| 3273 |
$this->mOutput->addWarningMsg( |
| 3274 |
'template-loop-warning', |
| 3275 |
Message::plaintextParam( $titleText ) |
| 3276 |
); |
| 3277 |
$this->logger->debug( __METHOD__ . ": template loop broken at '$titleText'" ); |
| 3278 |
} |
| 3279 |
} |
| 3280 |
|
| 3281 |
|
| 3282 |
|
| 3283 |
if ( !$found ) { |
| 3284 |
$text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args ); |
| 3285 |
if ( $profileSection ) { |
| 3286 |
$this->mProfiler->scopedProfileOut( $profileSection ); |
| 3287 |
} |
| 3288 |
return [ 'object' => $text ]; |
| 3289 |
} |
| 3290 |
|
| 3291 |
|
| 3292 |
if ( $isChildObj ) { |
| 3293 |
|
| 3294 |
$newFrame = $frame->newChild( $args, $title ); |
| 3295 |
|
| 3296 |
if ( $nowiki ) { |
| 3297 |
$text = $newFrame->expand( $text, PPFrame::RECOVER_ORIG ); |
| 3298 |
} elseif ( $titleText !== false && $newFrame->isEmpty() ) { |
| 3299 |
|
| 3300 |
$text = $newFrame->cachedExpand( $titleText, $text ); |
| 3301 |
} else { |
| 3302 |
|
| 3303 |
$text = $newFrame->expand( $text ); |
| 3304 |
} |
| 3305 |
} |
| 3306 |
if ( $isLocalObj && $nowiki ) { |
| 3307 |
$text = $frame->expand( $text, PPFrame::RECOVER_ORIG ); |
| 3308 |
$isLocalObj = false; |
| 3309 |
} |
| 3310 |
|
| 3311 |
if ( $profileSection ) { |
| 3312 |
$this->mProfiler->scopedProfileOut( $profileSection ); |
| 3313 |
} |
| 3314 |
if ( |
| 3315 |
$sawDeprecatedTemplateEquals && |
| 3316 |
$this->mStripState->unstripBoth( $text ) !== '=' |
| 3317 |
) { |
| 3318 |
|
| 3319 |
|
| 3320 |
$this->addTrackingCategory( 'template-equals-category' ); |
| 3321 |
$this->mOutput->addWarningMsg( 'template-equals-warning' ); |
| 3322 |
} |
| 3323 |
|
| 3324 |
|
| 3325 |
if ( $isHTML ) { |
| 3326 |
|
| 3327 |
$text = $this->insertStripItem( $text ); |
| 3328 |
} elseif ( $nowiki && ( $this->ot['html'] || $this->ot['pre'] ) ) { |
| 3329 |
|
| 3330 |
|
| 3331 |
$text = wfEscapeWikiText( $text ); |
| 3332 |
} elseif ( is_string( $text ) |
| 3333 |
&& !$piece['lineStart'] |
| 3334 |
&& preg_match( '/^(?:{\\||:|;|#|\*)/', $text ) |
| 3335 |
) { |
| 3336 |
|
| 3337 |
|
| 3338 |
|
| 3339 |
$text = "\n" . $text; |
| 3340 |
} |
| 3341 |
|
| 3342 |
if ( is_string( $text ) && !$this->incrementIncludeSize( 'post-expand', strlen( $text ) ) ) { |
| 3343 |
|
| 3344 |
if ( $titleText !== false ) { |
| 3345 |
|
| 3346 |
$text = "[[:$titleText]]"; |
| 3347 |
} else { |
| 3348 |
|
| 3349 |
|
| 3350 |
$originalTitle = preg_replace( '/^:/', '', $originalTitle ); |
| 3351 |
$text = "[[:$originalTitle]]"; |
| 3352 |
} |
| 3353 |
$text .= $this->insertStripItem( '<!-- WARNING: template omitted, ' |
| 3354 |
. 'post-expand include size too large -->' ); |
| 3355 |
$this->limitationWarn( 'post-expand-template-inclusion' ); |
| 3356 |
} |
| 3357 |
|
| 3358 |
if ( $isLocalObj ) { |
| 3359 |
$ret = [ 'object' => $text ]; |
| 3360 |
} else { |
| 3361 |
$ret = [ 'text' => $text ]; |
| 3362 |
} |
| 3363 |
|
| 3364 |
return $ret; |
| 3365 |
} |
| 3366 |
|
| 3367 |
|
| 3368 |
|
| 3369 |
|
| 3370 |
|
| 3371 |
|
| 3372 |
|
| 3373 |
|
| 3374 |
|
| 3375 |
|
| 3376 |
|
| 3377 |
|
| 3378 |
|
| 3379 |
|
| 3380 |
|
| 3381 |
|
| 3382 |
|
| 3383 |
|
| 3384 |
|
| 3385 |
public function callParserFunction( PPFrame $frame, $function, array $args = [] ) { |
| 3386 |
|
| 3387 |
if ( isset( $this->mFunctionSynonyms[1][$function] ) ) { |
| 3388 |
$function = $this->mFunctionSynonyms[1][$function]; |
| 3389 |
} else { |
| 3390 |
|
| 3391 |
$function = $this->contLang->lc( $function ); |
| 3392 |
if ( isset( $this->mFunctionSynonyms[0][$function] ) ) { |
| 3393 |
$function = $this->mFunctionSynonyms[0][$function]; |
| 3394 |
} else { |
| 3395 |
return [ 'found' => false ]; |
| 3396 |
} |
| 3397 |
} |
| 3398 |
|
| 3399 |
[ $callback, $flags ] = $this->mFunctionHooks[$function]; |
| 3400 |
|
| 3401 |
$allArgs = [ $this ]; |
| 3402 |
if ( $flags & self::SFH_OBJECT_ARGS ) { |
| 3403 |
|
| 3404 |
$funcArgs = []; |
| 3405 |
foreach ( $args as $k => $v ) { |
| 3406 |
if ( $v instanceof PPNode || $k === 0 ) { |
| 3407 |
$funcArgs[] = $v; |
| 3408 |
} else { |
| 3409 |
$funcArgs[] = $this->mPreprocessor->newPartNodeArray( [ $k => $v ] )->item( 0 ); |
| 3410 |
} |
| 3411 |
} |
| 3412 |
|
| 3413 |
|
| 3414 |
$allArgs[] = $frame; |
| 3415 |
$allArgs[] = $funcArgs; |
| 3416 |
} else { |
| 3417 |
|
| 3418 |
foreach ( $args as $k => $v ) { |
| 3419 |
if ( $v instanceof PPNode ) { |
| 3420 |
$allArgs[] = trim( $frame->expand( $v ) ); |
| 3421 |
} elseif ( is_int( $k ) && $k >= 0 ) { |
| 3422 |
$allArgs[] = trim( $v ); |
| 3423 |
} else { |
| 3424 |
$allArgs[] = trim( "$k=$v" ); |
| 3425 |
} |
| 3426 |
} |
| 3427 |
} |
| 3428 |
|
| 3429 |
$result = $callback( ...$allArgs ); |
| 3430 |
|
| 3431 |
|
| 3432 |
|
| 3433 |
|
| 3434 |
if ( !is_array( $result ) ) { |
| 3435 |
$result = [ |
| 3436 |
'found' => true, |
| 3437 |
'text' => $result, |
| 3438 |
]; |
| 3439 |
} else { |
| 3440 |
if ( isset( $result[0] ) && !isset( $result['text'] ) ) { |
| 3441 |
$result['text'] = $result[0]; |
| 3442 |
} |
| 3443 |
unset( $result[0] ); |
| 3444 |
$result += [ |
| 3445 |
'found' => true, |
| 3446 |
]; |
| 3447 |
} |
| 3448 |
|
| 3449 |
$noparse = true; |
| 3450 |
$preprocessFlags = 0; |
| 3451 |
if ( isset( $result['noparse'] ) ) { |
| 3452 |
$noparse = $result['noparse']; |
| 3453 |
} |
| 3454 |
if ( isset( $result['preprocessFlags'] ) ) { |
| 3455 |
$preprocessFlags = $result['preprocessFlags']; |
| 3456 |
} |
| 3457 |
|
| 3458 |
if ( !$noparse ) { |
| 3459 |
$result['text'] = $this->preprocessToDom( $result['text'], $preprocessFlags ); |
| 3460 |
$result['isChildObj'] = true; |
| 3461 |
} |
| 3462 |
|
| 3463 |
return $result; |
| 3464 |
} |
| 3465 |
|
| 3466 |
|
| 3467 |
|
| 3468 |
|
| 3469 |
|
| 3470 |
|
| 3471 |
|
| 3472 |
|
| 3473 |
|
| 3474 |
|
| 3475 |
public function getTemplateDom( LinkTarget $title ) { |
| 3476 |
$cacheTitle = $title; |
| 3477 |
$titleKey = CacheKeyHelper::getKeyForPage( $title ); |
| 3478 |
|
| 3479 |
if ( isset( $this->mTplRedirCache[$titleKey] ) ) { |
| 3480 |
[ $ns, $dbk ] = $this->mTplRedirCache[$titleKey]; |
| 3481 |
$title = Title::makeTitle( $ns, $dbk ); |
| 3482 |
$titleKey = CacheKeyHelper::getKeyForPage( $title ); |
| 3483 |
} |
| 3484 |
if ( isset( $this->mTplDomCache[$titleKey] ) ) { |
| 3485 |
return [ $this->mTplDomCache[$titleKey], $title ]; |
| 3486 |
} |
| 3487 |
|
| 3488 |
|
| 3489 |
[ $text, $title ] = $this->fetchTemplateAndTitle( $title ); |
| 3490 |
|
| 3491 |
|
| 3492 |
if ( $this->nsInfo->isNonincludable( $title->getNamespace() ) ) { |
| 3493 |
return [ false, $title ]; |
| 3494 |
} |
| 3495 |
|
| 3496 |
if ( $text === false ) { |
| 3497 |
$this->mTplDomCache[$titleKey] = false; |
| 3498 |
return [ false, $title ]; |
| 3499 |
} |
| 3500 |
|
| 3501 |
$dom = $this->preprocessToDom( $text, Preprocessor::DOM_FOR_INCLUSION ); |
| 3502 |
$this->mTplDomCache[$titleKey] = $dom; |
| 3503 |
|
| 3504 |
if ( !$title->isSamePageAs( $cacheTitle ) ) { |
| 3505 |
$this->mTplRedirCache[ CacheKeyHelper::getKeyForPage( $cacheTitle ) ] = |
| 3506 |
[ $title->getNamespace(), $title->getDBkey() ]; |
| 3507 |
} |
| 3508 |
|
| 3509 |
return [ $dom, $title ]; |
| 3510 |
} |
| 3511 |
|
| 3512 |
|
| 3513 |
|
| 3514 |
|
| 3515 |
|
| 3516 |
|
| 3517 |
|
| 3518 |
|
| 3519 |
|
| 3520 |
|
| 3521 |
|
| 3522 |
|
| 3523 |
|
| 3524 |
|
| 3525 |
public function fetchCurrentRevisionRecordOfTitle( LinkTarget $link ) { |
| 3526 |
$cacheKey = CacheKeyHelper::getKeyForPage( $link ); |
| 3527 |
if ( !$this->currentRevisionCache ) { |
| 3528 |
$this->currentRevisionCache = new MapCacheLRU( 100 ); |
| 3529 |
} |
| 3530 |
if ( !$this->currentRevisionCache->has( $cacheKey ) ) { |
| 3531 |
$title = Title::newFromLinkTarget( $link ); |
| 3532 |
$revisionRecord = |
| 3533 |
|
| 3534 |
call_user_func( |
| 3535 |
$this->mOptions->getCurrentRevisionRecordCallback(), |
| 3536 |
$title, |
| 3537 |
$this |
| 3538 |
); |
| 3539 |
if ( $revisionRecord === false ) { |
| 3540 |
|
| 3541 |
|
| 3542 |
$revisionRecord = null; |
| 3543 |
} |
| 3544 |
$this->currentRevisionCache->set( $cacheKey, $revisionRecord ); |
| 3545 |
} |
| 3546 |
return $this->currentRevisionCache->get( $cacheKey ); |
| 3547 |
} |
| 3548 |
|
| 3549 |
|
| 3550 |
|
| 3551 |
|
| 3552 |
|
| 3553 |
|
| 3554 |
|
| 3555 |
public function isCurrentRevisionOfTitleCached( LinkTarget $link ) { |
| 3556 |
$key = CacheKeyHelper::getKeyForPage( $link ); |
| 3557 |
return ( |
| 3558 |
$this->currentRevisionCache && |
| 3559 |
$this->currentRevisionCache->has( $key ) |
| 3560 |
); |
| 3561 |
} |
| 3562 |
|
| 3563 |
|
| 3564 |
|
| 3565 |
|
| 3566 |
|
| 3567 |
|
| 3568 |
|
| 3569 |
|
| 3570 |
|
| 3571 |
public static function statelessFetchRevisionRecord( LinkTarget $link, $parser = null ) { |
| 3572 |
if ( $link instanceof PageIdentity ) { |
| 3573 |
|
| 3574 |
$page = $link; |
| 3575 |
} else { |
| 3576 |
|
| 3577 |
|
| 3578 |
|
| 3579 |
$page = Title::newFromLinkTarget( $link ); |
| 3580 |
} |
| 3581 |
|
| 3582 |
$revRecord = MediaWikiServices::getInstance() |
| 3583 |
->getRevisionLookup() |
| 3584 |
->getKnownCurrentRevision( $page ); |
| 3585 |
return $revRecord; |
| 3586 |
} |
| 3587 |
|
| 3588 |
|
| 3589 |
|
| 3590 |
|
| 3591 |
|
| 3592 |
|
| 3593 |
|
| 3594 |
public function fetchTemplateAndTitle( LinkTarget $link ) { |
| 3595 |
|
| 3596 |
$title = Title::newFromLinkTarget( $link ); |
| 3597 |
|
| 3598 |
|
| 3599 |
$templateCb = $this->mOptions->getTemplateCallback(); |
| 3600 |
$stuff = $templateCb( $title, $this ); |
| 3601 |
$revRecord = $stuff['revision-record'] ?? null; |
| 3602 |
|
| 3603 |
$text = $stuff['text']; |
| 3604 |
if ( is_string( $stuff['text'] ) ) { |
| 3605 |
|
| 3606 |
$text = strtr( $text, "\x7f", "?" ); |
| 3607 |
} |
| 3608 |
$finalTitle = $stuff['finalTitle'] ?? $title; |
| 3609 |
foreach ( ( $stuff['deps'] ?? [] ) as $dep ) { |
| 3610 |
$this->mOutput->addTemplate( $dep['title'], $dep['page_id'], $dep['rev_id'] ); |
| 3611 |
if ( $dep['title']->equals( $this->getTitle() ) && $revRecord instanceof RevisionRecord ) { |
| 3612 |
|
| 3613 |
try { |
| 3614 |
$sha1 = $revRecord->getSha1(); |
| 3615 |
} catch ( RevisionAccessException $e ) { |
| 3616 |
$sha1 = null; |
| 3617 |
} |
| 3618 |
$this->setOutputFlag( ParserOutputFlags::VARY_REVISION_SHA1, 'Self transclusion' ); |
| 3619 |
$this->getOutput()->setRevisionUsedSha1Base36( $sha1 ); |
| 3620 |
} |
| 3621 |
} |
| 3622 |
|
| 3623 |
return [ $text, $finalTitle ]; |
| 3624 |
} |
| 3625 |
|
| 3626 |
|
| 3627 |
|
| 3628 |
|
| 3629 |
|
| 3630 |
|
| 3631 |
|
| 3632 |
|
| 3633 |
|
| 3634 |
|
| 3635 |
|
| 3636 |
public static function statelessFetchTemplate( $page, $parser = false ) { |
| 3637 |
$title = Title::castFromLinkTarget( $page ); |
| 3638 |
$text = $skip = false; |
| 3639 |
$finalTitle = $title; |
| 3640 |
$deps = []; |
| 3641 |
$revRecord = null; |
| 3642 |
$contextTitle = $parser ? $parser->getTitle() : null; |
| 3643 |
|
| 3644 |
|
| 3645 |
|
| 3646 |
|
| 3647 |
|
| 3648 |
|
| 3649 |
|
| 3650 |
|
| 3651 |
|
| 3652 |
$services = MediaWikiServices::getInstance(); |
| 3653 |
$revLookup = $services->getRevisionLookup(); |
| 3654 |
$hookRunner = new HookRunner( $services->getHookContainer() ); |
| 3655 |
for ( $i = 0; $i < 3 && is_object( $title ); $i++ ) { |
| 3656 |
|
| 3657 |
$revRecord = null; |
| 3658 |
$origTitle = $title; |
| 3659 |
$titleChanged = false; |
| 3660 |
$hookRunner->onBeforeParserFetchTemplateRevisionRecord( |
| 3661 |
|
| 3662 |
|
| 3663 |
|
| 3664 |
|
| 3665 |
|
| 3666 |
|
| 3667 |
$contextTitle, $title, |
| 3668 |
$skip, $revRecord |
| 3669 |
); |
| 3670 |
|
| 3671 |
if ( $skip ) { |
| 3672 |
$text = false; |
| 3673 |
if ( !$title->isExternal() ) { |
| 3674 |
$deps[] = [ |
| 3675 |
'title' => $title, |
| 3676 |
'page_id' => $title->getArticleID(), |
| 3677 |
'rev_id' => null |
| 3678 |
]; |
| 3679 |
} |
| 3680 |
break; |
| 3681 |
} |
| 3682 |
|
| 3683 |
if ( !$revRecord ) { |
| 3684 |
if ( $parser ) { |
| 3685 |
$revRecord = $parser->fetchCurrentRevisionRecordOfTitle( $title ); |
| 3686 |
} else { |
| 3687 |
$revRecord = $revLookup->getRevisionByTitle( $title ); |
| 3688 |
} |
| 3689 |
} |
| 3690 |
if ( $revRecord ) { |
| 3691 |
|
| 3692 |
$title = Title::newFromLinkTarget( |
| 3693 |
$revRecord->getPageAsLinkTarget() |
| 3694 |
); |
| 3695 |
|
| 3696 |
$deps[] = [ |
| 3697 |
'title' => $title, |
| 3698 |
'page_id' => $revRecord->getPageId(), |
| 3699 |
'rev_id' => $revRecord->getId(), |
| 3700 |
]; |
| 3701 |
} elseif ( !$title->isExternal() ) { |
| 3702 |
$deps[] = [ |
| 3703 |
'title' => $title, |
| 3704 |
'page_id' => $title->getArticleID(), |
| 3705 |
'rev_id' => null, |
| 3706 |
]; |
| 3707 |
} |
| 3708 |
if ( !$title->equals( $origTitle ) ) { |
| 3709 |
|
| 3710 |
|
| 3711 |
if ( !$origTitle->isExternal() ) { |
| 3712 |
$deps[] = [ |
| 3713 |
'title' => $origTitle, |
| 3714 |
'page_id' => $origTitle->getArticleID(), |
| 3715 |
'rev_id' => null, |
| 3716 |
]; |
| 3717 |
} |
| 3718 |
$titleChanged = true; |
| 3719 |
} |
| 3720 |
|
| 3721 |
if ( $revRecord === null || $revRecord->getId() === null ) { |
| 3722 |
$linkCache = $services->getLinkCache(); |
| 3723 |
$linkCache->addBadLinkObj( $title ); |
| 3724 |
} |
| 3725 |
if ( $revRecord ) { |
| 3726 |
if ( $titleChanged && !$revRecord->hasSlot( SlotRecord::MAIN ) ) { |
| 3727 |
|
| 3728 |
|
| 3729 |
|
| 3730 |
$text = false; |
| 3731 |
$finalTitle = $title; |
| 3732 |
continue; |
| 3733 |
} |
| 3734 |
if ( $revRecord->hasSlot( SlotRecord::MAIN ) ) { |
| 3735 |
$content = $revRecord->getContent( SlotRecord::MAIN ); |
| 3736 |
$text = $content ? $content->getWikitextForTransclusion() : null; |
| 3737 |
} else { |
| 3738 |
$text = false; |
| 3739 |
} |
| 3740 |
|
| 3741 |
if ( $text === false || $text === null ) { |
| 3742 |
$text = false; |
| 3743 |
break; |
| 3744 |
} |
| 3745 |
} elseif ( $title->getNamespace() === NS_MEDIAWIKI ) { |
| 3746 |
$message = wfMessage( $services->getContentLanguage()-> |
| 3747 |
lcfirst( $title->getText() ) )->inContentLanguage(); |
| 3748 |
if ( !$message->exists() ) { |
| 3749 |
$text = false; |
| 3750 |
break; |
| 3751 |
} |
| 3752 |
$text = $message->plain(); |
| 3753 |
break; |
| 3754 |
} else { |
| 3755 |
break; |
| 3756 |
} |
| 3757 |
|
| 3758 |
if ( !$content ) { |
| 3759 |
break; |
| 3760 |
} |
| 3761 |
|
| 3762 |
$finalTitle = $title; |
| 3763 |
$title = $content->getRedirectTarget(); |
| 3764 |
} |
| 3765 |
|
| 3766 |
$retValues = [ |
| 3767 |
|
| 3768 |
|
| 3769 |
|
| 3770 |
|
| 3771 |
|
| 3772 |
'revision-record' => $revRecord ?: false, |
| 3773 |
'text' => $text, |
| 3774 |
'finalTitle' => $finalTitle, |
| 3775 |
'deps' => $deps |
| 3776 |
]; |
| 3777 |
return $retValues; |
| 3778 |
} |
| 3779 |
|
| 3780 |
|
| 3781 |
|
| 3782 |
|
| 3783 |
|
| 3784 |
|
| 3785 |
|
| 3786 |
|
| 3787 |
|
| 3788 |
public function fetchFileAndTitle( LinkTarget $link, array $options = [] ) { |
| 3789 |
$file = $this->fetchFileNoRegister( $link, $options ); |
| 3790 |
|
| 3791 |
$time = $file ? $file->getTimestamp() : false; |
| 3792 |
$sha1 = $file ? $file->getSha1() : false; |
| 3793 |
|
| 3794 |
$this->mOutput->addImage( $link, $time, $sha1 ); |
| 3795 |
if ( $file && !$link->isSameLinkAs( $file->getTitle() ) ) { |
| 3796 |
|
| 3797 |
$link = $file->getTitle(); |
| 3798 |
$this->mOutput->addImage( $link, $time, $sha1 ); |
| 3799 |
} |
| 3800 |
|
| 3801 |
$title = Title::newFromLinkTarget( $link ); |
| 3802 |
return [ $file, $title ]; |
| 3803 |
} |
| 3804 |
|
| 3805 |
|
| 3806 |
|
| 3807 |
|
| 3808 |
|
| 3809 |
|
| 3810 |
|
| 3811 |
|
| 3812 |
|
| 3813 |
|
| 3814 |
|
| 3815 |
protected function fetchFileNoRegister( LinkTarget $link, array $options = [] ) { |
| 3816 |
if ( isset( $options['broken'] ) ) { |
| 3817 |
$file = false; |
| 3818 |
} else { |
| 3819 |
$repoGroup = MediaWikiServices::getInstance()->getRepoGroup(); |
| 3820 |
if ( isset( $options['sha1'] ) ) { |
| 3821 |
$file = $repoGroup->findFileFromKey( $options['sha1'], $options ); |
| 3822 |
} else { |
| 3823 |
$file = $repoGroup->findFile( $link, $options ); |
| 3824 |
} |
| 3825 |
} |
| 3826 |
return $file; |
| 3827 |
} |
| 3828 |
|
| 3829 |
|
| 3830 |
|
| 3831 |
|
| 3832 |
|
| 3833 |
|
| 3834 |
|
| 3835 |
|
| 3836 |
|
| 3837 |
|
| 3838 |
public function interwikiTransclude( LinkTarget $link, $action ) { |
| 3839 |
if ( !$this->svcOptions->get( MainConfigNames::EnableScaryTranscluding ) ) { |
| 3840 |
return wfMessage( 'scarytranscludedisabled' )->inContentLanguage()->text(); |
| 3841 |
} |
| 3842 |
|
| 3843 |
|
| 3844 |
$title = Title::newFromLinkTarget( $link ); |
| 3845 |
|
| 3846 |
$url = $title->getFullURL( [ 'action' => $action ] ); |
| 3847 |
if ( strlen( $url ) > 1024 ) { |
| 3848 |
return wfMessage( 'scarytranscludetoolong' )->inContentLanguage()->text(); |
| 3849 |
} |
| 3850 |
|
| 3851 |
$wikiId = $title->getTransWikiID(); |
| 3852 |
|
| 3853 |
$fname = __METHOD__; |
| 3854 |
|
| 3855 |
$cache = $this->wanCache; |
| 3856 |
$data = $cache->getWithSetCallback( |
| 3857 |
$cache->makeGlobalKey( |
| 3858 |
'interwiki-transclude', |
| 3859 |
( $wikiId !== false ) ? $wikiId : 'external', |
| 3860 |
sha1( $url ) |
| 3861 |
), |
| 3862 |
$this->svcOptions->get( MainConfigNames::TranscludeCacheExpiry ), |
| 3863 |
function ( $oldValue, &$ttl ) use ( $url, $fname, $cache ) { |
| 3864 |
$req = $this->httpRequestFactory->create( $url, [], $fname ); |
| 3865 |
|
| 3866 |
$status = $req->execute(); |
| 3867 |
if ( !$status->isOK() ) { |
| 3868 |
$ttl = $cache::TTL_UNCACHEABLE; |
| 3869 |
} elseif ( $req->getResponseHeader( 'X-Database-Lagged' ) !== null ) { |
| 3870 |
$ttl = min( $cache::TTL_LAGGED, $ttl ); |
| 3871 |
} |
| 3872 |
|
| 3873 |
return [ |
| 3874 |
'text' => $status->isOK() ? $req->getContent() : null, |
| 3875 |
'code' => $req->getStatus() |
| 3876 |
]; |
| 3877 |
}, |
| 3878 |
[ |
| 3879 |
'checkKeys' => ( $wikiId !== false ) |
| 3880 |
? [ $cache->makeGlobalKey( 'interwiki-page', $wikiId, $title->getDBkey() ) ] |
| 3881 |
: [], |
| 3882 |
'pcGroup' => 'interwiki-transclude:5', |
| 3883 |
'pcTTL' => $cache::TTL_PROC_LONG |
| 3884 |
] |
| 3885 |
); |
| 3886 |
|
| 3887 |
if ( is_string( $data['text'] ) ) { |
| 3888 |
$text = $data['text']; |
| 3889 |
} elseif ( $data['code'] != 200 ) { |
| 3890 |
|
| 3891 |
$text = wfMessage( 'scarytranscludefailed-httpstatus' ) |
| 3892 |
->params( $url, $data['code'] )->inContentLanguage()->text(); |
| 3893 |
} else { |
| 3894 |
$text = wfMessage( 'scarytranscludefailed', $url )->inContentLanguage()->text(); |
| 3895 |
} |
| 3896 |
|
| 3897 |
return $text; |
| 3898 |
} |
| 3899 |
|
| 3900 |
|
| 3901 |
|
| 3902 |
|
| 3903 |
|
| 3904 |
|
| 3905 |
|
| 3906 |
|
| 3907 |
|
| 3908 |
|
| 3909 |
public function argSubstitution( array $piece, PPFrame $frame ) { |
| 3910 |
$error = false; |
| 3911 |
$parts = $piece['parts']; |
| 3912 |
$nameWithSpaces = $frame->expand( $piece['title'] ); |
| 3913 |
$argName = trim( $nameWithSpaces ); |
| 3914 |
$object = false; |
| 3915 |
$text = $frame->getArgument( $argName ); |
| 3916 |
if ( $text === false && $parts->getLength() > 0 |
| 3917 |
&& ( $this->ot['html'] |
| 3918 |
|| $this->ot['pre'] |
| 3919 |
|| ( $this->ot['wiki'] && $frame->isTemplate() ) |
| 3920 |
) |
| 3921 |
) { |
| 3922 |
|
| 3923 |
$object = $parts->item( 0 )->getChildren(); |
| 3924 |
} |
| 3925 |
if ( !$this->incrementIncludeSize( 'arg', strlen( $text ) ) ) { |
| 3926 |
$error = '<!-- WARNING: argument omitted, expansion size too large -->'; |
| 3927 |
$this->limitationWarn( 'post-expand-template-argument' ); |
| 3928 |
} |
| 3929 |
|
| 3930 |
if ( $text === false && $object === false ) { |
| 3931 |
|
| 3932 |
$object = $frame->virtualBracketedImplode( '{{{', '|', '}}}', $nameWithSpaces, $parts ); |
| 3933 |
} |
| 3934 |
if ( $error !== false ) { |
| 3935 |
$text .= $error; |
| 3936 |
} |
| 3937 |
if ( $object !== false ) { |
| 3938 |
$ret = [ 'object' => $object ]; |
| 3939 |
} else { |
| 3940 |
$ret = [ 'text' => $text ]; |
| 3941 |
} |
| 3942 |
|
| 3943 |
return $ret; |
| 3944 |
} |
| 3945 |
|
| 3946 |
|
| 3947 |
|
| 3948 |
|
| 3949 |
|
| 3950 |
public function tagNeedsNowikiStrippedInTagPF( string $lowerTagName ): bool { |
| 3951 |
$parsoidSiteConfig = MediaWikiServices::getInstance()->getParsoidSiteConfig(); |
| 3952 |
return $parsoidSiteConfig->tagNeedsNowikiStrippedInTagPF( $lowerTagName ); |
| 3953 |
} |
| 3954 |
|
| 3955 |
|
| 3956 |
|
| 3957 |
|
| 3958 |
|
| 3959 |
|
| 3960 |
|
| 3961 |
|
| 3962 |
|
| 3963 |
|
| 3964 |
|
| 3965 |
|
| 3966 |
|
| 3967 |
|
| 3968 |
|
| 3969 |
|
| 3970 |
|
| 3971 |
|
| 3972 |
|
| 3973 |
|
| 3974 |
public function extensionSubstitution( array $params, PPFrame $frame, bool $processNowiki = false ) { |
| 3975 |
static $errorStr = '<span class="error">'; |
| 3976 |
|
| 3977 |
$name = $frame->expand( $params['name'] ); |
| 3978 |
if ( str_starts_with( $name, $errorStr ) ) { |
| 3979 |
|
| 3980 |
|
| 3981 |
return $name; |
| 3982 |
} |
| 3983 |
|
| 3984 |
|
| 3985 |
$attrText = !isset( $params['attr'] ) ? '' : $frame->expand( $params['attr'] ); |
| 3986 |
if ( str_starts_with( $attrText, $errorStr ) ) { |
| 3987 |
|
| 3988 |
return $attrText; |
| 3989 |
} |
| 3990 |
|
| 3991 |
|
| 3992 |
|
| 3993 |
|
| 3994 |
$content = !isset( $params['inner'] ) ? null : $frame->expand( $params['inner'] ); |
| 3995 |
|
| 3996 |
$marker = self::MARKER_PREFIX . "-$name-" |
| 3997 |
. sprintf( '%08X', $this->mMarkerIndex++ ) . self::MARKER_SUFFIX; |
| 3998 |
|
| 3999 |
$normalizedName = strtolower( $name ); |
| 4000 |
$isNowiki = $normalizedName === 'nowiki'; |
| 4001 |
$markerType = $isNowiki ? 'nowiki' : 'general'; |
| 4002 |
if ( $this->ot['html'] || ( $processNowiki && $isNowiki ) ) { |
| 4003 |
$attributes = Sanitizer::decodeTagAttributes( $attrText ); |
| 4004 |
|
| 4005 |
if ( isset( $params['attributes'] ) ) { |
| 4006 |
$attributes += $params['attributes']; |
| 4007 |
} |
| 4008 |
|
| 4009 |
if ( isset( $this->mTagHooks[$normalizedName] ) ) { |
| 4010 |
|
| 4011 |
|
| 4012 |
$output = call_user_func_array( $this->mTagHooks[$normalizedName], |
| 4013 |
[ $content, $attributes, $this, $frame ] ); |
| 4014 |
} else { |
| 4015 |
$output = '<span class="error">Invalid tag extension name: ' . |
| 4016 |
htmlspecialchars( $normalizedName ) . '</span>'; |
| 4017 |
} |
| 4018 |
|
| 4019 |
if ( is_array( $output ) ) { |
| 4020 |
|
| 4021 |
$flags = $output; |
| 4022 |
$output = $flags[0]; |
| 4023 |
if ( isset( $flags['markerType'] ) ) { |
| 4024 |
$markerType = $flags['markerType']; |
| 4025 |
} |
| 4026 |
} |
| 4027 |
} else { |
| 4028 |
|
| 4029 |
|
| 4030 |
if ( isset( $params['attributes'] ) ) { |
| 4031 |
foreach ( $params['attributes'] as $attrName => $attrValue ) { |
| 4032 |
$attrText .= ' ' . htmlspecialchars( $attrName ) . '="' . |
| 4033 |
htmlspecialchars( $this->getStripState()->unstripBoth( $attrValue ), ENT_COMPAT ) . '"'; |
| 4034 |
} |
| 4035 |
} |
| 4036 |
if ( $content === null ) { |
| 4037 |
$output = "<$name$attrText/>"; |
| 4038 |
} else { |
| 4039 |
$close = $params['close'] === null ? '' : $frame->expand( $params['close'] ); |
| 4040 |
if ( str_starts_with( $close, $errorStr ) ) { |
| 4041 |
|
| 4042 |
return $close; |
| 4043 |
} |
| 4044 |
$output = "<$name$attrText>$content$close"; |
| 4045 |
} |
| 4046 |
} |
| 4047 |
|
| 4048 |
if ( $markerType === 'none' ) { |
| 4049 |
return $output; |
| 4050 |
} elseif ( $markerType === 'nowiki' ) { |
| 4051 |
$this->mStripState->addNoWiki( $marker, $output ); |
| 4052 |
} elseif ( $markerType === 'general' ) { |
| 4053 |
$this->mStripState->addGeneral( $marker, $output ); |
| 4054 |
} else { |
| 4055 |
throw new UnexpectedValueException( __METHOD__ . ': invalid marker type' ); |
| 4056 |
} |
| 4057 |
return $marker; |
| 4058 |
} |
| 4059 |
|
| 4060 |
|
| 4061 |
|
| 4062 |
|
| 4063 |
|
| 4064 |
|
| 4065 |
|
| 4066 |
|
| 4067 |
private function incrementIncludeSize( $type, $size ) { |
| 4068 |
if ( $this->mIncludeSizes[$type] + $size > $this->mOptions->getMaxIncludeSize() ) { |
| 4069 |
return false; |
| 4070 |
} else { |
| 4071 |
$this->mIncludeSizes[$type] += $size; |
| 4072 |
return true; |
| 4073 |
} |
| 4074 |
} |
| 4075 |
|
| 4076 |
|
| 4077 |
|
| 4078 |
|
| 4079 |
|
| 4080 |
public function incrementExpensiveFunctionCount() { |
| 4081 |
$this->mExpensiveFunctionCount++; |
| 4082 |
return $this->mExpensiveFunctionCount <= $this->mOptions->getExpensiveParserFunctionLimit(); |
| 4083 |
} |
| 4084 |
|
| 4085 |
|
| 4086 |
|
| 4087 |
|
| 4088 |
|
| 4089 |
|
| 4090 |
|
| 4091 |
|
| 4092 |
private function handleDoubleUnderscore( $text ) { |
| 4093 |
|
| 4094 |
$mw = $this->magicWordFactory->get( 'toc' ); |
| 4095 |
$tocAlias = null; |
| 4096 |
if ( $mw->match( $text ) ) { |
| 4097 |
$this->mShowToc = true; |
| 4098 |
$this->mForceTocPosition = true; |
| 4099 |
|
| 4100 |
preg_match( $mw->getRegex(), $text, $tocAlias ); |
| 4101 |
|
| 4102 |
|
| 4103 |
$text = $mw->replace( self::TOC_PLACEHOLDER, $text, 1 ); |
| 4104 |
|
| 4105 |
|
| 4106 |
$text = $mw->replace( '', $text ); |
| 4107 |
} |
| 4108 |
|
| 4109 |
|
| 4110 |
$mwa = $this->magicWordFactory->getDoubleUnderscoreArray(); |
| 4111 |
$this->mDoubleUnderscores = $mwa->matchAndRemove( $text, returnAlias: true ); |
| 4112 |
if ( $tocAlias ) { |
| 4113 |
|
| 4114 |
$this->mDoubleUnderscores['toc'] = $tocAlias[0]; |
| 4115 |
} |
| 4116 |
|
| 4117 |
if ( isset( $this->mDoubleUnderscores['nogallery'] ) ) { |
| 4118 |
$this->mOutput->setNoGallery( true ); |
| 4119 |
} |
| 4120 |
if ( isset( $this->mDoubleUnderscores['notoc'] ) && !$this->mForceTocPosition ) { |
| 4121 |
$this->mShowToc = false; |
| 4122 |
} |
| 4123 |
if ( isset( $this->mDoubleUnderscores['hiddencat'] ) |
| 4124 |
&& $this->getTitle()->getNamespace() === NS_CATEGORY |
| 4125 |
) { |
| 4126 |
$this->addTrackingCategory( 'hidden-category-category' ); |
| 4127 |
} |
| 4128 |
|
| 4129 |
|
| 4130 |
if ( isset( $this->mDoubleUnderscores['noindex'] ) && $this->getTitle()->canUseNoindex() ) { |
| 4131 |
$this->mOutput->setIndexPolicy( 'noindex' ); |
| 4132 |
$this->addTrackingCategory( 'noindex-category' ); |
| 4133 |
} |
| 4134 |
if ( isset( $this->mDoubleUnderscores['index'] ) && $this->getTitle()->canUseNoindex() ) { |
| 4135 |
$this->mOutput->setIndexPolicy( 'index' ); |
| 4136 |
$this->addTrackingCategory( 'index-category' ); |
| 4137 |
} |
| 4138 |
|
| 4139 |
foreach ( $this->mDoubleUnderscores as $key => $alias ) { |
| 4140 |
|
| 4141 |
$this->mOutput->setUnsortedPageProperty( $key ); |
| 4142 |
|
| 4143 |
$ascii = str_starts_with( $alias, '__' ) && str_ends_with( $alias, '__' ); |
| 4144 |
$wide = str_starts_with( $alias, '__' ) && str_ends_with( $alias, '__' ); |
| 4145 |
if ( !( $ascii || $wide ) ) { |
| 4146 |
$this->addTrackingCategory( 'bad-double-underscore-category' ); |
| 4147 |
} |
| 4148 |
} |
| 4149 |
|
| 4150 |
return $text; |
| 4151 |
} |
| 4152 |
|
| 4153 |
|
| 4154 |
|
| 4155 |
|
| 4156 |
|
| 4157 |
|
| 4158 |
|
| 4159 |
public function addTrackingCategory( $msg ) { |
| 4160 |
return $this->trackingCategories->addTrackingCategory( |
| 4161 |
$this->mOutput, $msg, $this->getPage() |
| 4162 |
); |
| 4163 |
} |
| 4164 |
|
| 4165 |
|
| 4166 |
|
| 4167 |
|
| 4168 |
|
| 4169 |
|
| 4170 |
|
| 4171 |
|
| 4172 |
|
| 4173 |
|
| 4174 |
|
| 4175 |
|
| 4176 |
|
| 4177 |
|
| 4178 |
public function msg( string $msg, ...$args ): Message { |
| 4179 |
return wfMessage( $msg, ...$args ) |
| 4180 |
->inLanguage( $this->getTargetLanguage() ) |
| 4181 |
->page( $this->getPage() ); |
| 4182 |
} |
| 4183 |
|
| 4184 |
private function cleanUpTocLine( Node $container ) { |
| 4185 |
'@phan-var Element|DocumentFragment $container'; |
| 4186 |
|
| 4187 |
|
| 4188 |
|
| 4189 |
|
| 4190 |
|
| 4191 |
|
| 4192 |
|
| 4193 |
|
| 4194 |
|
| 4195 |
|
| 4196 |
|
| 4197 |
$allowedTags = [ 'span', 'sup', 'sub', 'bdi', 'i', 'b', 's', 'strike', 'q' ]; |
| 4198 |
$node = $container->firstChild; |
| 4199 |
while ( $node !== null ) { |
| 4200 |
$next = $node->nextSibling; |
| 4201 |
if ( $node instanceof Element ) { |
| 4202 |
$nodeName = DOMCompat::nodeName( $node ); |
| 4203 |
if ( in_array( $nodeName, [ 'style', 'script' ], true ) ) { |
| 4204 |
|
| 4205 |
DOMCompat::remove( $node ); |
| 4206 |
} elseif ( in_array( $nodeName, $allowedTags, true ) ) { |
| 4207 |
|
| 4208 |
$removeAttrs = []; |
| 4209 |
foreach ( $node->attributes as $attr ) { |
| 4210 |
if ( |
| 4211 |
$nodeName === 'span' && $attr->name === 'dir' |
| 4212 |
&& ( $attr->value === 'rtl' || $attr->value === 'ltr' ) |
| 4213 |
) { |
| 4214 |
|
| 4215 |
continue; |
| 4216 |
} |
| 4217 |
$removeAttrs[] = $attr; |
| 4218 |
} |
| 4219 |
foreach ( $removeAttrs as $attr ) { |
| 4220 |
$node->removeAttributeNode( $attr ); |
| 4221 |
} |
| 4222 |
$this->cleanUpTocLine( $node ); |
| 4223 |
|
| 4224 |
|
| 4225 |
|
| 4226 |
if ( $nodeName === 'span' && !$node->hasChildNodes() ) { |
| 4227 |
DOMCompat::remove( $node ); |
| 4228 |
} |
| 4229 |
} else { |
| 4230 |
|
| 4231 |
if ( $node->firstChild !== null ) { |
| 4232 |
$next = $node->firstChild; |
| 4233 |
|
| 4234 |
while ( $childNode = $node->firstChild ) { |
| 4235 |
$node->parentNode->insertBefore( $childNode, $node ); |
| 4236 |
} |
| 4237 |
} |
| 4238 |
DOMCompat::remove( $node ); |
| 4239 |
} |
| 4240 |
} elseif ( $node instanceof Comment ) { |
| 4241 |
|
| 4242 |
|
| 4243 |
DOMCompat::remove( $node ); |
| 4244 |
} |
| 4245 |
$node = $next; |
| 4246 |
} |
| 4247 |
} |
| 4248 |
|
| 4249 |
|
| 4250 |
|
| 4251 |
|
| 4252 |
|
| 4253 |
|
| 4254 |
|
| 4255 |
|
| 4256 |
|
| 4257 |
|
| 4258 |
|
| 4259 |
|
| 4260 |
|
| 4261 |
|
| 4262 |
|
| 4263 |
|
| 4264 |
private function finalizeHeadings( $text, $origText, $isMain = true ) { |
| 4265 |
|
| 4266 |
if ( isset( $this->mDoubleUnderscores['noeditsection'] ) ) { |
| 4267 |
$maybeShowEditLink = false; |
| 4268 |
} else { |
| 4269 |
$maybeShowEditLink = true; |
| 4270 |
} |
| 4271 |
|
| 4272 |
|
| 4273 |
|
| 4274 |
|
| 4275 |
|
| 4276 |
$matches = []; |
| 4277 |
$numMatches = preg_match_all( |
| 4278 |
'/<H(?P<level>[1-6])(?P<attrib>.*?>)(?P<header>[\s\S]*?)<\/H[1-6] *>/i', |
| 4279 |
$text, |
| 4280 |
$matches |
| 4281 |
); |
| 4282 |
|
| 4283 |
|
| 4284 |
|
| 4285 |
$enoughToc = $this->mShowToc && |
| 4286 |
( ( $numMatches >= 4 ) || $this->mForceTocPosition ); |
| 4287 |
|
| 4288 |
|
| 4289 |
|
| 4290 |
if ( isset( $this->mDoubleUnderscores['newsectionlink'] ) ) { |
| 4291 |
$this->mOutput->setNewSection( true ); |
| 4292 |
} |
| 4293 |
|
| 4294 |
|
| 4295 |
|
| 4296 |
if ( isset( $this->mDoubleUnderscores['nonewsectionlink'] ) ) { |
| 4297 |
$this->mOutput->setHideNewSection( true ); |
| 4298 |
} |
| 4299 |
|
| 4300 |
|
| 4301 |
|
| 4302 |
if ( isset( $this->mDoubleUnderscores['forcetoc'] ) ) { |
| 4303 |
$this->mShowToc = true; |
| 4304 |
$enoughToc = true; |
| 4305 |
} |
| 4306 |
|
| 4307 |
|
| 4308 |
$headlineCount = 0; |
| 4309 |
$haveTocEntries = false; |
| 4310 |
|
| 4311 |
|
| 4312 |
|
| 4313 |
$full = ''; |
| 4314 |
$head = []; |
| 4315 |
$level = 0; |
| 4316 |
$tocData = new TOCData(); |
| 4317 |
$markerRegex = self::MARKER_PREFIX . "-h-(\d+)-" . self::MARKER_SUFFIX; |
| 4318 |
$baseTitleText = $this->getTitle()->getPrefixedDBkey(); |
| 4319 |
$oldType = $this->mOutputType; |
| 4320 |
$this->setOutputType( self::OT_WIKI ); |
| 4321 |
$frame = $this->getPreprocessor()->newFrame(); |
| 4322 |
$root = $this->preprocessToDom( $origText ); |
| 4323 |
$node = $root->getFirstChild(); |
| 4324 |
$cpOffset = 0; |
| 4325 |
$refers = []; |
| 4326 |
|
| 4327 |
$headlines = $numMatches !== false ? $matches[3] : []; |
| 4328 |
|
| 4329 |
$maxTocLevel = $this->svcOptions->get( MainConfigNames::MaxTocLevel ); |
| 4330 |
$domDocument = DOMUtils::parseHTML( '' ); |
| 4331 |
foreach ( $headlines as $headline ) { |
| 4332 |
$isTemplate = false; |
| 4333 |
$titleText = false; |
| 4334 |
$sectionIndex = false; |
| 4335 |
$markerMatches = []; |
| 4336 |
if ( preg_match( "/^$markerRegex/", $headline, $markerMatches ) ) { |
| 4337 |
$serial = (int)$markerMatches[1]; |
| 4338 |
[ $titleText, $sectionIndex ] = $this->mHeadings[$serial]; |
| 4339 |
$isTemplate = ( $titleText != $baseTitleText ); |
| 4340 |
$headline = preg_replace( "/^$markerRegex\\s*/", "", $headline ); |
| 4341 |
} |
| 4342 |
|
| 4343 |
$sectionMetadata = SectionMetadata::fromLegacy( [ |
| 4344 |
"fromtitle" => $titleText ?: null, |
| 4345 |
"index" => $sectionIndex === false |
| 4346 |
? '' : ( ( $isTemplate ? 'T-' : '' ) . $sectionIndex ) |
| 4347 |
] ); |
| 4348 |
$tocData->addSection( $sectionMetadata ); |
| 4349 |
|
| 4350 |
$oldLevel = $level; |
| 4351 |
$level = (int)$matches[1][$headlineCount]; |
| 4352 |
$tocData->processHeading( $oldLevel, $level, $sectionMetadata ); |
| 4353 |
|
| 4354 |
if ( $tocData->getCurrentTOCLevel() < $maxTocLevel ) { |
| 4355 |
$haveTocEntries = true; |
| 4356 |
} |
| 4357 |
|
| 4358 |
|
| 4359 |
|
| 4360 |
|
| 4361 |
|
| 4362 |
|
| 4363 |
|
| 4364 |
|
| 4365 |
$safeHeadline = $this->replaceLinkHoldersText( $headline ); |
| 4366 |
|
| 4367 |
|
| 4368 |
$safeHeadline = $this->mStripState->unstripBoth( $safeHeadline ); |
| 4369 |
|
| 4370 |
|
| 4371 |
|
| 4372 |
$safeHeadline = $this->tidy->tidy( $safeHeadline, [ Sanitizer::class, 'armorFrenchSpaces' ] ); |
| 4373 |
|
| 4374 |
|
| 4375 |
|
| 4376 |
|
| 4377 |
$wrappedHeadline = "<h$level" . $matches['attrib'][$headlineCount] . $safeHeadline . "</h$level>"; |
| 4378 |
|
| 4379 |
|
| 4380 |
|
| 4381 |
$headlineDom = DOMUtils::parseHTMLToFragment( $domDocument, $wrappedHeadline ); |
| 4382 |
|
| 4383 |
|
| 4384 |
|
| 4385 |
$h = $headlineDom->firstChild; |
| 4386 |
$headingId = ( $h instanceof Element && DOMUtils::isHeading( $h ) ) ? |
| 4387 |
DOMCompat::getAttribute( $h, 'id' ) : null; |
| 4388 |
|
| 4389 |
$this->cleanUpTocLine( $headlineDom ); |
| 4390 |
|
| 4391 |
|
| 4392 |
$tocline = trim( DOMUtils::getFragmentInnerHTML( $headlineDom ) ); |
| 4393 |
|
| 4394 |
|
| 4395 |
$safeHeadline = trim( $headlineDom->textContent ); |
| 4396 |
|
| 4397 |
|
| 4398 |
$headlineHint = htmlspecialchars( $safeHeadline ); |
| 4399 |
|
| 4400 |
$safeHeadline = Sanitizer::normalizeSectionNameWhitespace( $safeHeadline ); |
| 4401 |
$safeHeadline = self::normalizeSectionName( $safeHeadline ); |
| 4402 |
|
| 4403 |
if ( $headingId !== null && $headingId !== '' ) { |
| 4404 |
$safeHeadline = $headingId; |
| 4405 |
} |
| 4406 |
|
| 4407 |
$fallbackHeadline = Sanitizer::escapeIdForAttribute( $safeHeadline, Sanitizer::ID_FALLBACK ); |
| 4408 |
$linkAnchor = Sanitizer::escapeIdForLink( $safeHeadline ); |
| 4409 |
$safeHeadline = Sanitizer::escapeIdForAttribute( $safeHeadline, Sanitizer::ID_PRIMARY ); |
| 4410 |
if ( $fallbackHeadline === $safeHeadline ) { |
| 4411 |
|
| 4412 |
$fallbackHeadline = false; |
| 4413 |
} |
| 4414 |
|
| 4415 |
|
| 4416 |
$arrayKey = strtolower( $safeHeadline ); |
| 4417 |
if ( $fallbackHeadline === false ) { |
| 4418 |
$fallbackArrayKey = false; |
| 4419 |
} else { |
| 4420 |
$fallbackArrayKey = strtolower( $fallbackHeadline ); |
| 4421 |
} |
| 4422 |
|
| 4423 |
|
| 4424 |
$anchor = $safeHeadline; |
| 4425 |
$fallbackAnchor = $fallbackHeadline; |
| 4426 |
if ( isset( $refers[$arrayKey] ) ) { |
| 4427 |
for ( $i = 2; isset( $refers["{$arrayKey}_$i"] ); ++$i ); |
| 4428 |
$anchor .= "_$i"; |
| 4429 |
$linkAnchor .= "_$i"; |
| 4430 |
$refers["{$arrayKey}_$i"] = true; |
| 4431 |
} else { |
| 4432 |
$refers[$arrayKey] = true; |
| 4433 |
} |
| 4434 |
if ( $fallbackHeadline !== false && isset( $refers[$fallbackArrayKey] ) ) { |
| 4435 |
for ( $i = 2; isset( $refers["{$fallbackArrayKey}_$i"] ); ++$i ); |
| 4436 |
$fallbackAnchor .= "_$i"; |
| 4437 |
$refers["{$fallbackArrayKey}_$i"] = true; |
| 4438 |
} else { |
| 4439 |
$refers[$fallbackArrayKey] = true; |
| 4440 |
} |
| 4441 |
|
| 4442 |
|
| 4443 |
|
| 4444 |
$noOffset = ( $isTemplate || $sectionIndex === false ); |
| 4445 |
while ( $node && !$noOffset ) { |
| 4446 |
if ( $node->getName() === 'h' ) { |
| 4447 |
$bits = $node->splitHeading(); |
| 4448 |
if ( $bits['i'] == $sectionIndex ) { |
| 4449 |
break; |
| 4450 |
} |
| 4451 |
} |
| 4452 |
$cpOffset += mb_strlen( |
| 4453 |
$this->mStripState->unstripBoth( |
| 4454 |
$frame->expand( $node, PPFrame::RECOVER_ORIG ) |
| 4455 |
) |
| 4456 |
); |
| 4457 |
$node = $node->getNextSibling(); |
| 4458 |
} |
| 4459 |
$sectionMetadata->line = $tocline; |
| 4460 |
$sectionMetadata->codepointOffset = ( $noOffset ? null : $cpOffset ); |
| 4461 |
$sectionMetadata->anchor = $anchor; |
| 4462 |
$sectionMetadata->linkAnchor = $linkAnchor; |
| 4463 |
|
| 4464 |
if ( $maybeShowEditLink && $sectionIndex !== false ) { |
| 4465 |
|
| 4466 |
if ( $isTemplate ) { |
| 4467 |
|
| 4468 |
|
| 4469 |
$editsectionPage = $titleText; |
| 4470 |
$editsectionSection = "T-$sectionIndex"; |
| 4471 |
} else { |
| 4472 |
$editsectionPage = $this->getTitle()->getPrefixedText(); |
| 4473 |
$editsectionSection = $sectionIndex; |
| 4474 |
} |
| 4475 |
|
| 4476 |
|
| 4477 |
|
| 4478 |
|
| 4479 |
|
| 4480 |
|
| 4481 |
|
| 4482 |
|
| 4483 |
|
| 4484 |
$editlink = '<mw:editsection page="' . htmlspecialchars( $editsectionPage, ENT_COMPAT ); |
| 4485 |
$editlink .= '" section="' . htmlspecialchars( $editsectionSection, ENT_COMPAT ) . '"'; |
| 4486 |
$editlink .= '>' . $headlineHint . '</mw:editsection>'; |
| 4487 |
} else { |
| 4488 |
$editlink = ''; |
| 4489 |
} |
| 4490 |
|
| 4491 |
|
| 4492 |
|
| 4493 |
|
| 4494 |
|
| 4495 |
|
| 4496 |
|
| 4497 |
$head[$headlineCount] = "<h$level" . Html::expandAttributes( [ |
| 4498 |
'data-mw-anchor' => $anchor, |
| 4499 |
'data-mw-fallback-anchor' => $fallbackAnchor, |
| 4500 |
] ) . $matches['attrib'][$headlineCount] . $headline . $editlink . "</h$level>"; |
| 4501 |
|
| 4502 |
$headlineCount++; |
| 4503 |
} |
| 4504 |
|
| 4505 |
$this->setOutputType( $oldType ); |
| 4506 |
|
| 4507 |
|
| 4508 |
$suppressToc = $this->mOptions->getSuppressTOC(); |
| 4509 |
if ( !$haveTocEntries ) { |
| 4510 |
$enoughToc = false; |
| 4511 |
} |
| 4512 |
$addTOCPlaceholder = false; |
| 4513 |
|
| 4514 |
if ( $isMain && !$suppressToc ) { |
| 4515 |
|
| 4516 |
|
| 4517 |
|
| 4518 |
|
| 4519 |
|
| 4520 |
|
| 4521 |
|
| 4522 |
$this->mOutput->setTOCData( $tocData ); |
| 4523 |
|
| 4524 |
|
| 4525 |
|
| 4526 |
|
| 4527 |
if ( $enoughToc ) { |
| 4528 |
$this->mOutput->setOutputFlag( ParserOutputFlags::SHOW_TOC ); |
| 4529 |
if ( !$this->mForceTocPosition ) { |
| 4530 |
$addTOCPlaceholder = true; |
| 4531 |
} |
| 4532 |
} |
| 4533 |
|
| 4534 |
|
| 4535 |
|
| 4536 |
|
| 4537 |
|
| 4538 |
|
| 4539 |
if ( !$this->mShowToc ) { |
| 4540 |
$this->mOutput->setOutputFlag( ParserOutputFlags::NO_TOC ); |
| 4541 |
} |
| 4542 |
} |
| 4543 |
|
| 4544 |
|
| 4545 |
$blocks = preg_split( '/<h[1-6]\b[^>]*>.*?<\/h[1-6]>/is', $text ); |
| 4546 |
$i = 0; |
| 4547 |
|
| 4548 |
|
| 4549 |
$sections = []; |
| 4550 |
foreach ( $blocks as $block ) { |
| 4551 |
|
| 4552 |
if ( empty( $head[$i - 1] ) ) { |
| 4553 |
$sections[$i] = $block; |
| 4554 |
} else { |
| 4555 |
$sections[$i] = $head[$i - 1] . $block; |
| 4556 |
} |
| 4557 |
|
| 4558 |
$i++; |
| 4559 |
} |
| 4560 |
|
| 4561 |
if ( $addTOCPlaceholder ) { |
| 4562 |
|
| 4563 |
|
| 4564 |
|
| 4565 |
$sections[0] .= self::TOC_PLACEHOLDER . "\n"; |
| 4566 |
} |
| 4567 |
|
| 4568 |
$full .= implode( '', $sections ); |
| 4569 |
|
| 4570 |
return $full; |
| 4571 |
} |
| 4572 |
|
| 4573 |
|
| 4574 |
|
| 4575 |
|
| 4576 |
|
| 4577 |
|
| 4578 |
|
| 4579 |
|
| 4580 |
|
| 4581 |
|
| 4582 |
private static function localizeTOC( |
| 4583 |
?TOCData $tocData, Language $lang, ?ILanguageConverter $converter |
| 4584 |
) { |
| 4585 |
if ( $tocData === null ) { |
| 4586 |
return; |
| 4587 |
} |
| 4588 |
foreach ( $tocData->getSections() as $s ) { |
| 4589 |
|
| 4590 |
if ( $converter ) { |
| 4591 |
|
| 4592 |
|
| 4593 |
$s->line = $converter->convertTo( |
| 4594 |
$s->line, $converter->getPreferredVariant(), false |
| 4595 |
); |
| 4596 |
} |
| 4597 |
|
| 4598 |
$dot = '.'; |
| 4599 |
$pieces = explode( $dot, $s->number ); |
| 4600 |
$numbering = ''; |
| 4601 |
foreach ( $pieces as $i => $p ) { |
| 4602 |
if ( $i > 0 ) { |
| 4603 |
$numbering .= $dot; |
| 4604 |
} |
| 4605 |
$numbering .= $lang->formatNum( $p ); |
| 4606 |
} |
| 4607 |
$s->number = $numbering; |
| 4608 |
} |
| 4609 |
} |
| 4610 |
|
| 4611 |
|
| 4612 |
|
| 4613 |
|
| 4614 |
|
| 4615 |
|
| 4616 |
|
| 4617 |
|
| 4618 |
|
| 4619 |
|
| 4620 |
|
| 4621 |
|
| 4622 |
|
| 4623 |
public function preSaveTransform( |
| 4624 |
$text, |
| 4625 |
PageReference $page, |
| 4626 |
UserIdentity $user, |
| 4627 |
ParserOptions $options, |
| 4628 |
$clearState = true |
| 4629 |
) { |
| 4630 |
if ( $clearState ) { |
| 4631 |
$magicScopeVariable = $this->lock(); |
| 4632 |
} |
| 4633 |
$this->startParse( $page, $options, self::OT_WIKI, $clearState ); |
| 4634 |
$this->setUser( $user ); |
| 4635 |
|
| 4636 |
|
| 4637 |
$text = str_replace( "\000", '', $text ); |
| 4638 |
|
| 4639 |
|
| 4640 |
|
| 4641 |
|
| 4642 |
$text = TextContent::normalizeLineEndings( $text ); |
| 4643 |
|
| 4644 |
if ( $options->getPreSaveTransform() ) { |
| 4645 |
$text = $this->pstPass2( $text, $user ); |
| 4646 |
} |
| 4647 |
$text = $this->mStripState->unstripBoth( $text ); |
| 4648 |
|
| 4649 |
|
| 4650 |
$text = rtrim( $text ); |
| 4651 |
|
| 4652 |
$this->hookRunner->onParserPreSaveTransformComplete( $this, $text ); |
| 4653 |
|
| 4654 |
$this->setUser( null ); |
| 4655 |
|
| 4656 |
return $text; |
| 4657 |
} |
| 4658 |
|
| 4659 |
|
| 4660 |
|
| 4661 |
|
| 4662 |
|
| 4663 |
|
| 4664 |
|
| 4665 |
|
| 4666 |
|
| 4667 |
private function pstPass2( $text, UserIdentity $user ) { |
| 4668 |
|
| 4669 |
|
| 4670 |
|
| 4671 |
$ts = $this->mOptions->getTimestamp(); |
| 4672 |
$timestamp = MWTimestamp::getLocalInstance( $ts ); |
| 4673 |
$ts = $timestamp->format( 'YmdHis' ); |
| 4674 |
$tzMsg = $timestamp->getTimezoneMessage()->inContentLanguage()->text(); |
| 4675 |
|
| 4676 |
$d = $this->contLang->timeanddate( $ts, false, false ) . " ($tzMsg)"; |
| 4677 |
|
| 4678 |
|
| 4679 |
|
| 4680 |
$text = $this->replaceVariables( $text ); |
| 4681 |
|
| 4682 |
|
| 4683 |
|
| 4684 |
|
| 4685 |
|
| 4686 |
if ( strpos( $text, '~~~' ) !== false ) { |
| 4687 |
$sigText = $this->getUserSig( $user ); |
| 4688 |
$text = strtr( $text, [ |
| 4689 |
'~~~~~' => $d, |
| 4690 |
'~~~~' => "$sigText $d", |
| 4691 |
'~~~' => $sigText |
| 4692 |
] ); |
| 4693 |
|
| 4694 |
$this->setOutputFlag( ParserOutputFlags::USER_SIGNATURE, 'User signature detected' ); |
| 4695 |
} |
| 4696 |
|
| 4697 |
|
| 4698 |
$tc = '[' . Title::legalChars() . ']'; |
| 4699 |
$nc = '[ _0-9A-Za-z\x80-\xff-]'; |
| 4700 |
|
| 4701 |
|
| 4702 |
$p1 = "/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\))\\|]]/"; |
| 4703 |
257) |
| 4704 |
$p4 = "/\[\[(:?$nc+:|:|)($tc+?)( ?($tc+))\\|]]/"; |
| 4705 |
|
| 4706 |
$p3 = "/\[\[(:?$nc+:|:|)($tc+?)( ?\\($tc+\\)|)((?:, |,|، )$tc+|)\\|]]/"; |
| 4707 |
|
| 4708 |
$p2 = "/\[\[\\|($tc+)]]/"; |
| 4709 |
|
| 4710 |
|
| 4711 |
$text = preg_replace( $p1, '[[\\1\\2\\3|\\2]]', $text ); |
| 4712 |
$text = preg_replace( $p4, '[[\\1\\2\\3|\\2]]', $text ); |
| 4713 |
$text = preg_replace( $p3, '[[\\1\\2\\3\\4|\\2]]', $text ); |
| 4714 |
|
| 4715 |
$t = $this->getTitle()->getText(); |
| 4716 |
$m = []; |
| 4717 |
if ( preg_match( "/^($nc+:|)$tc+?( \\($tc+\\))$/", $t, $m ) ) { |
| 4718 |
$text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text ); |
| 4719 |
} elseif ( preg_match( "/^($nc+:|)$tc+?(, $tc+|)$/", $t, $m ) && "$m[1]$m[2]" != '' ) { |
| 4720 |
$text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text ); |
| 4721 |
} else { |
| 4722 |
|
| 4723 |
$text = preg_replace( $p2, '[[\\1]]', $text ); |
| 4724 |
} |
| 4725 |
|
| 4726 |
return $text; |
| 4727 |
} |
| 4728 |
|
| 4729 |
|
| 4730 |
|
| 4731 |
|
| 4732 |
|
| 4733 |
|
| 4734 |
|
| 4735 |
|
| 4736 |
|
| 4737 |
|
| 4738 |
|
| 4739 |
|
| 4740 |
|
| 4741 |
|
| 4742 |
|
| 4743 |
|
| 4744 |
public function getUserSig( UserIdentity $user, $nickname = false, $fancySig = null ) { |
| 4745 |
$username = $user->getName(); |
| 4746 |
|
| 4747 |
|
| 4748 |
if ( $nickname === false ) { |
| 4749 |
$nickname = $this->userOptionsLookup->getOption( $user, 'nickname' ); |
| 4750 |
} |
| 4751 |
|
| 4752 |
$fancySig ??= $this->userOptionsLookup->getBoolOption( $user, 'fancysig' ); |
| 4753 |
|
| 4754 |
if ( $nickname === null || $nickname === '' ) { |
| 4755 |
|
| 4756 |
$nickname = $username; |
| 4757 |
} elseif ( mb_strlen( $nickname ) > $this->svcOptions->get( MainConfigNames::MaxSigChars ) ) { |
| 4758 |
$nickname = $username; |
| 4759 |
$this->logger->debug( __METHOD__ . ": $username has overlong signature." ); |
| 4760 |
} elseif ( $fancySig !== false ) { |
| 4761 |
|
| 4762 |
$isValid = $this->validateSig( $nickname ) !== false; |
| 4763 |
|
| 4764 |
|
| 4765 |
$sigValidation = $this->svcOptions->get( MainConfigNames::SignatureValidation ); |
| 4766 |
if ( $isValid && $sigValidation === 'disallow' ) { |
| 4767 |
$parserOpts = new ParserOptions( |
| 4768 |
$this->mOptions->getUserIdentity(), |
| 4769 |
$this->contLang |
| 4770 |
); |
| 4771 |
$validator = $this->signatureValidatorFactory |
| 4772 |
->newSignatureValidator( $user, null, $parserOpts ); |
| 4773 |
$isValid = !$validator->validateSignature( $nickname ); |
| 4774 |
} |
| 4775 |
|
| 4776 |
if ( $isValid ) { |
| 4777 |
|
| 4778 |
return $this->cleanSig( $nickname, true ); |
| 4779 |
} else { |
| 4780 |
|
| 4781 |
$nickname = $username; |
| 4782 |
$this->logger->debug( __METHOD__ . ": $username has invalid signature." ); |
| 4783 |
} |
| 4784 |
} |
| 4785 |
|
| 4786 |
|
| 4787 |
$nickname = self::cleanSigInSig( $nickname ); |
| 4788 |
|
| 4789 |
|
| 4790 |
$userText = wfEscapeWikiText( $username ); |
| 4791 |
$nickText = wfEscapeWikiText( $nickname ); |
| 4792 |
if ( $this->userNameUtils->isTemp( $username ) ) { |
| 4793 |
$msgName = 'signature-temp'; |
| 4794 |
} elseif ( $user->isRegistered() ) { |
| 4795 |
$msgName = 'signature'; |
| 4796 |
} else { |
| 4797 |
$msgName = 'signature-anon'; |
| 4798 |
} |
| 4799 |
|
| 4800 |
return wfMessage( $msgName, $userText, $nickText )->inContentLanguage() |
| 4801 |
->page( $this->getPage() )->text(); |
| 4802 |
} |
| 4803 |
|
| 4804 |
|
| 4805 |
|
| 4806 |
|
| 4807 |
|
| 4808 |
|
| 4809 |
|
| 4810 |
|
| 4811 |
public function validateSig( $text ) { |
| 4812 |
return Xml::isWellFormedXmlFragment( $text ) ? $text : false; |
| 4813 |
} |
| 4814 |
|
| 4815 |
|
| 4816 |
|
| 4817 |
|
| 4818 |
|
| 4819 |
|
| 4820 |
|
| 4821 |
|
| 4822 |
|
| 4823 |
|
| 4824 |
|
| 4825 |
|
| 4826 |
public function cleanSig( $text, $parsing = false ) { |
| 4827 |
if ( !$parsing ) { |
| 4828 |
$magicScopeVariable = $this->lock(); |
| 4829 |
$this->startParse( |
| 4830 |
$this->mTitle, |
| 4831 |
ParserOptions::newFromUser( RequestContext::getMain()->getUser() ), |
| 4832 |
self::OT_PREPROCESS, |
| 4833 |
true |
| 4834 |
); |
| 4835 |
} |
| 4836 |
|
| 4837 |
|
| 4838 |
if ( !$this->mOptions->getCleanSignatures() ) { |
| 4839 |
return $text; |
| 4840 |
} |
| 4841 |
|
| 4842 |
|
| 4843 |
|
| 4844 |
$substWord = $this->magicWordFactory->get( 'subst' ); |
| 4845 |
$substRegex = '/\{\{(?!(?:' . $substWord->getBaseRegex() . '))/x' . $substWord->getRegexCase(); |
| 4846 |
$substText = '{{' . $substWord->getSynonym( 0 ); |
| 4847 |
|
| 4848 |
$text = preg_replace( $substRegex, $substText, $text ); |
| 4849 |
$text = self::cleanSigInSig( $text ); |
| 4850 |
$dom = $this->preprocessToDom( $text ); |
| 4851 |
$frame = $this->getPreprocessor()->newFrame(); |
| 4852 |
$text = $frame->expand( $dom ); |
| 4853 |
|
| 4854 |
if ( !$parsing ) { |
| 4855 |
$text = $this->mStripState->unstripBoth( $text ); |
| 4856 |
} |
| 4857 |
|
| 4858 |
return $text; |
| 4859 |
} |
| 4860 |
|
| 4861 |
|
| 4862 |
|
| 4863 |
|
| 4864 |
|
| 4865 |
|
| 4866 |
|
| 4867 |
|
| 4868 |
public static function cleanSigInSig( $text ) { |
| 4869 |
$text = preg_replace( '/~{3,5}/', '', $text ); |
| 4870 |
return $text; |
| 4871 |
} |
| 4872 |
|
| 4873 |
|
| 4874 |
|
| 4875 |
|
| 4876 |
|
| 4877 |
|
| 4878 |
|
| 4879 |
|
| 4880 |
|
| 4881 |
|
| 4882 |
|
| 4883 |
|
| 4884 |
|
| 4885 |
|
| 4886 |
|
| 4887 |
|
| 4888 |
|
| 4889 |
public static function replaceTableOfContentsMarker( $text, $toc ) { |
| 4890 |
$replaced = false; |
| 4891 |
return HtmlHelper::modifyElements( |
| 4892 |
$text, |
| 4893 |
static function ( SerializerNode $node ): bool { |
| 4894 |
$prop = $node->attrs['property'] ?? ''; |
| 4895 |
return $node->name === 'meta' && $prop === 'mw:PageProp/toc'; |
| 4896 |
}, |
| 4897 |
static function ( SerializerNode $node ) use ( &$replaced, $toc ) { |
| 4898 |
if ( $replaced ) { |
| 4899 |
|
| 4900 |
|
| 4901 |
|
| 4902 |
return ''; |
| 4903 |
} |
| 4904 |
$replaced = true; |
| 4905 |
return $toc; |
| 4906 |
}, |
| 4907 |
false |
| 4908 |
); |
| 4909 |
} |
| 4910 |
|
| 4911 |
|
| 4912 |
|
| 4913 |
|
| 4914 |
|
| 4915 |
|
| 4916 |
|
| 4917 |
ts |
| 4918 |
|
| 4919 |
|
| 4920 |
|
| 4921 |
|
| 4922 |
public function startExternalParse( ?PageReference $page, ParserOptions $options, |
| 4923 |
$outputType, $clearState = true, $revId = null |
| 4924 |
) { |
| 4925 |
$this->startParse( $page, $options, $outputType, $clearState ); |
| 4926 |
if ( $revId !== null ) { |
| 4927 |
$this->mRevisionId = $revId; |
| 4928 |
} |
| 4929 |
} |
| 4930 |
|
| 4931 |
|
| 4932 |
|
| 4933 |
|
| 4934 |
|
| 4935 |
|
| 4936 |
|
| 4937 |
private function startParse( ?PageReference $page, ParserOptions $options, |
| 4938 |
$outputType, $clearState = true |
| 4939 |
) { |
| 4940 |
$this->setPage( $page ); |
| 4941 |
$this->mOptions = $options; |
| 4942 |
$this->setOutputType( $outputType ); |
| 4943 |
if ( $clearState ) { |
| 4944 |
$this->clearState(); |
| 4945 |
} |
| 4946 |
} |
| 4947 |
|
| 4948 |
|
| 4949 |
|
| 4950 |
|
| 4951 |
|
| 4952 |
|
| 4953 |
|
| 4954 |
|
| 4955 |
|
| 4956 |
|
| 4957 |
public function transformMsg( $text, ParserOptions $options, ?PageReference $page = null ) { |
| 4958 |
static $executing = false; |
| 4959 |
|
| 4960 |
|
| 4961 |
if ( $executing ) { |
| 4962 |
return $text; |
| 4963 |
} |
| 4964 |
$executing = true; |
| 4965 |
|
| 4966 |
$text = $this->preprocess( $text, $page ?? $this->mTitle, $options ); |
| 4967 |
|
| 4968 |
$executing = false; |
| 4969 |
return $text; |
| 4970 |
} |
| 4971 |
|
| 4972 |
|
| 4973 |
|
| 4974 |
|
| 4975 |
|
| 4976 |
|
| 4977 |
|
| 4978 |
|
| 4979 |
|
| 4980 |
|
| 4981 |
|
| 4982 |
|
| 4983 |
|
| 4984 |
|
| 4985 |
|
| 4986 |
|
| 4987 |
|
| 4988 |
|
| 4989 |
|
| 4990 |
|
| 4991 |
public function setHook( $tag, callable $callback ) { |
| 4992 |
$tag = strtolower( $tag ); |
| 4993 |
if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) { |
| 4994 |
throw new InvalidArgumentException( "Invalid character {$m[0]} in setHook('$tag', ...) call" ); |
| 4995 |
} |
| 4996 |
$oldVal = $this->mTagHooks[$tag] ?? null; |
| 4997 |
$this->mTagHooks[$tag] = $callback; |
| 4998 |
if ( !in_array( $tag, $this->mStripList ) ) { |
| 4999 |
$this->mStripList[] = $tag; |
| 5000 |
} |
| 5001 |
|
| 5002 |
return $oldVal; |
| 5003 |
} |
| 5004 |
|
| 5005 |
|
| 5006 |
|
| 5007 |
|
| 5008 |
|
| 5009 |
public function clearTagHooks() { |
| 5010 |
$this->mTagHooks = []; |
| 5011 |
$this->mStripList = []; |
| 5012 |
} |
| 5013 |
|
| 5014 |
|
| 5015 |
|
| 5016 |
|
| 5017 |
|
| 5018 |
|
| 5019 |
|
| 5020 |
|
| 5021 |
|
| 5022 |
|
| 5023 |
|
| 5024 |
|
| 5025 |
|
| 5026 |
|
| 5027 |
|
| 5028 |
|
| 5029 |
|
| 5030 |
|
| 5031 |
|
| 5032 |
|
| 5033 |
|
| 5034 |
|
| 5035 |
|
| 5036 |
|
| 5037 |
|
| 5038 |
|
| 5039 |
|
| 5040 |
|
| 5041 |
|
| 5042 |
|
| 5043 |
|
| 5044 |
|
| 5045 |
|
| 5046 |
|
| 5047 |
|
| 5048 |
|
| 5049 |
|
| 5050 |
|
| 5051 |
|
| 5052 |
|
| 5053 |
|
| 5054 |
|
| 5055 |
|
| 5056 |
|
| 5057 |
public function setFunctionHook( $id, callable $callback, $flags = 0 ) { |
| 5058 |
$oldVal = $this->mFunctionHooks[$id][0] ?? null; |
| 5059 |
$this->mFunctionHooks[$id] = [ $callback, $flags ]; |
| 5060 |
|
| 5061 |
|
| 5062 |
$mw = $this->magicWordFactory->get( $id ); |
| 5063 |
|
| 5064 |
$synonyms = $mw->getSynonyms(); |
| 5065 |
$sensitive = intval( $mw->isCaseSensitive() ); |
| 5066 |
|
| 5067 |
foreach ( $synonyms as $syn ) { |
| 5068 |
|
| 5069 |
if ( !$sensitive ) { |
| 5070 |
$syn = $this->contLang->lc( $syn ); |
| 5071 |
} |
| 5072 |
|
| 5073 |
if ( !( $flags & self::SFH_NO_HASH ) ) { |
| 5074 |
$syn = '#' . $syn; |
| 5075 |
} |
| 5076 |
|
| 5077 |
if ( substr( $syn, -1, 1 ) === ':' ) { |
| 5078 |
$syn = substr( $syn, 0, -1 ); |
| 5079 |
} |
| 5080 |
$this->mFunctionSynonyms[$sensitive][$syn] = $id; |
| 5081 |
} |
| 5082 |
return $oldVal; |
| 5083 |
} |
| 5084 |
|
| 5085 |
|
| 5086 |
|
| 5087 |
|
| 5088 |
|
| 5089 |
|
| 5090 |
|
| 5091 |
public function getFunctionHooks() { |
| 5092 |
return array_keys( $this->mFunctionHooks ); |
| 5093 |
} |
| 5094 |
|
| 5095 |
|
| 5096 |
|
| 5097 |
|
| 5098 |
|
| 5099 |
|
| 5100 |
|
| 5101 |
|
| 5102 |
public function replaceLinkHolders( &$text ) { |
| 5103 |
$this->replaceLinkHoldersPrivate( $text ); |
| 5104 |
} |
| 5105 |
|
| 5106 |
|
| 5107 |
|
| 5108 |
|
| 5109 |
|
| 5110 |
|
| 5111 |
|
| 5112 |
private function replaceLinkHoldersPrivate( &$text ) { |
| 5113 |
$this->mLinkHolders->replace( $text ); |
| 5114 |
} |
| 5115 |
|
| 5116 |
|
| 5117 |
|
| 5118 |
|
| 5119 |
|
| 5120 |
|
| 5121 |
|
| 5122 |
|
| 5123 |
private function replaceLinkHoldersText( $text ) { |
| 5124 |
return $this->mLinkHolders->replaceText( $text ); |
| 5125 |
} |
| 5126 |
|
| 5127 |
|
| 5128 |
|
| 5129 |
|
| 5130 |
|
| 5131 |
|
| 5132 |
|
| 5133 |
|
| 5134 |
|
| 5135 |
|
| 5136 |
|
| 5137 |
|
| 5138 |
|
| 5139 |
|
| 5140 |
|
| 5141 |
public function renderImageGallery( $text, array $params ) { |
| 5142 |
$mode = false; |
| 5143 |
if ( isset( $params['mode'] ) ) { |
| 5144 |
$mode = $params['mode']; |
| 5145 |
} |
| 5146 |
|
| 5147 |
try { |
| 5148 |
$ig = ImageGalleryBase::factory( $mode ); |
| 5149 |
} catch ( ImageGalleryClassNotFoundException $e ) { |
| 5150 |
|
| 5151 |
$ig = ImageGalleryBase::factory( false ); |
| 5152 |
} |
| 5153 |
|
| 5154 |
$ig->setContextTitle( $this->getTitle() ); |
| 5155 |
$ig->setShowBytes( false ); |
| 5156 |
$ig->setShowDimensions( false ); |
| 5157 |
$ig->setShowFilename( false ); |
| 5158 |
$ig->setParser( $this ); |
| 5159 |
$ig->setHideBadImages(); |
| 5160 |
$ig->setAttributes( Sanitizer::validateTagAttributes( $params, 'ul' ) ); |
| 5161 |
|
| 5162 |
if ( isset( $params['showfilename'] ) ) { |
| 5163 |
$ig->setShowFilename( true ); |
| 5164 |
} else { |
| 5165 |
$ig->setShowFilename( false ); |
| 5166 |
} |
| 5167 |
if ( isset( $params['caption'] ) ) { |
| 5168 |
|
| 5169 |
|
| 5170 |
|
| 5171 |
$caption = $this->recursiveTagParse( $params['caption'] ); |
| 5172 |
$ig->setCaptionHtml( $caption ); |
| 5173 |
} |
| 5174 |
if ( isset( $params['perrow'] ) ) { |
| 5175 |
$ig->setPerRow( $params['perrow'] ); |
| 5176 |
} |
| 5177 |
if ( isset( $params['widths'] ) ) { |
| 5178 |
$ig->setWidths( $params['widths'] ); |
| 5179 |
} |
| 5180 |
if ( isset( $params['heights'] ) ) { |
| 5181 |
$ig->setHeights( $params['heights'] ); |
| 5182 |
} |
| 5183 |
$ig->setAdditionalOptions( $params ); |
| 5184 |
|
| 5185 |
$enableLegacyMediaDOM = $this->svcOptions->get( MainConfigNames::ParserEnableLegacyMediaDOM ); |
| 5186 |
|
| 5187 |
$lines = StringUtils::explode( "\n", $text ); |
| 5188 |
foreach ( $lines as $line ) { |
| 5189 |
|
| 5190 |
|
| 5191 |
$matches = []; |
| 5192 |
preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches ); |
| 5193 |
|
| 5194 |
if ( count( $matches ) == 0 ) { |
| 5195 |
continue; |
| 5196 |
} |
| 5197 |
|
| 5198 |
if ( strpos( $matches[0], '%' ) !== false ) { |
| 5199 |
$matches[1] = rawurldecode( $matches[1] ); |
| 5200 |
} |
| 5201 |
$title = Title::newFromText( $matches[1], NS_FILE ); |
| 5202 |
if ( $title === null ) { |
| 5203 |
|
| 5204 |
continue; |
| 5205 |
} |
| 5206 |
|
| 5207 |
|
| 5208 |
|
| 5209 |
|
| 5210 |
$options = []; |
| 5211 |
$descQuery = false; |
| 5212 |
$this->hookRunner->onBeforeParserFetchFileAndTitle( |
| 5213 |
|
| 5214 |
$this, $title, $options, $descQuery |
| 5215 |
); |
| 5216 |
|
| 5217 |
$file = $this->fetchFileNoRegister( $title, $options ); |
| 5218 |
$handler = $file ? $file->getHandler() : false; |
| 5219 |
|
| 5220 |
$paramMap = [ |
| 5221 |
'img_alt' => 'gallery-internal-alt', |
| 5222 |
'img_link' => 'gallery-internal-link', |
| 5223 |
]; |
| 5224 |
if ( $handler ) { |
| 5225 |
$paramMap += $handler->getParamMap(); |
| 5226 |
|
| 5227 |
|
| 5228 |
unset( $paramMap['img_width'] ); |
| 5229 |
} |
| 5230 |
|
| 5231 |
$mwArray = $this->magicWordFactory->newArray( array_keys( $paramMap ) ); |
| 5232 |
|
| 5233 |
$label = ''; |
| 5234 |
$alt = null; |
| 5235 |
$handlerOptions = []; |
| 5236 |
$imageOptions = []; |
| 5237 |
$hasAlt = false; |
| 5238 |
|
| 5239 |
if ( isset( $matches[3] ) ) { |
| 5240 |
|
| 5241 |
|
| 5242 |
|
| 5243 |
|
| 5244 |
|
| 5245 |
|
| 5246 |
$matches[3] = $this->recursiveTagParse( $matches[3] ); |
| 5247 |
|
| 5248 |
$parameterMatches = StringUtils::delimiterExplode( |
| 5249 |
'-{', '}-', |
| 5250 |
'|', |
| 5251 |
$matches[3], |
| 5252 |
true |
| 5253 |
); |
| 5254 |
|
| 5255 |
foreach ( $parameterMatches as $parameterMatch ) { |
| 5256 |
[ $magicName, $match ] = $mwArray->matchVariableStartToEnd( trim( $parameterMatch ) ); |
| 5257 |
if ( !$magicName ) { |
| 5258 |
|
| 5259 |
$label = $parameterMatch; |
| 5260 |
continue; |
| 5261 |
} |
| 5262 |
|
| 5263 |
$paramName = $paramMap[$magicName]; |
| 5264 |
switch ( $paramName ) { |
| 5265 |
case 'gallery-internal-alt': |
| 5266 |
$hasAlt = true; |
| 5267 |
$alt = $this->stripAltText( $match, false ); |
| 5268 |
break; |
| 5269 |
case 'gallery-internal-link': |
| 5270 |
$linkValue = $this->stripAltText( $match, false ); |
| 5271 |
if ( preg_match( '/^-{R\|(.*)}-$/', $linkValue ) ) { |
| 5272 |
|
| 5273 |
|
| 5274 |
$linkValue = substr( $linkValue, 4, -2 ); |
| 5275 |
} |
| 5276 |
[ $type, $target ] = $this->parseLinkParameter( $linkValue ); |
| 5277 |
if ( $type ) { |
| 5278 |
if ( $type === 'no-link' ) { |
| 5279 |
$target = true; |
| 5280 |
} |
| 5281 |
$imageOptions[$type] = $target; |
| 5282 |
} |
| 5283 |
break; |
| 5284 |
default: |
| 5285 |
|
| 5286 |
if ( $handler->validateParam( $paramName, $match ) ) { |
| 5287 |
$handlerOptions[$paramName] = $match; |
| 5288 |
} else { |
| 5289 |
|
| 5290 |
$this->logger->debug( |
| 5291 |
"$parameterMatch failed parameter validation" ); |
| 5292 |
$label = $parameterMatch; |
| 5293 |
} |
| 5294 |
} |
| 5295 |
} |
| 5296 |
} |
| 5297 |
|
| 5298 |
|
| 5299 |
if ( !$hasAlt ) { |
| 5300 |
if ( $label !== '' ) { |
| 5301 |
$alt = $this->stripAltText( $label, false ); |
| 5302 |
} else { |
| 5303 |
if ( $enableLegacyMediaDOM ) { |
| 5304 |
$alt = $title->getText(); |
| 5305 |
} |
| 5306 |
} |
| 5307 |
} |
| 5308 |
$imageOptions['title'] = $this->stripAltText( $label, false ); |
| 5309 |
|
| 5310 |
|
| 5311 |
$handlerOptions['targetlang'] = $this->getTargetLanguage()->getCode(); |
| 5312 |
|
| 5313 |
$ig->add( |
| 5314 |
$title, $label, $alt, '', $handlerOptions, |
| 5315 |
ImageGalleryBase::LOADING_DEFAULT, $imageOptions |
| 5316 |
); |
| 5317 |
} |
| 5318 |
$html = $ig->toHTML(); |
| 5319 |
$this->hookRunner->onAfterParserFetchFileAndTitle( $this, $ig, $html ); |
| 5320 |
return $html; |
| 5321 |
} |
| 5322 |
|
| 5323 |
|
| 5324 |
|
| 5325 |
|
| 5326 |
|
| 5327 |
private function getImageParams( $handler ) { |
| 5328 |
if ( $handler ) { |
| 5329 |
$handlerClass = get_class( $handler ); |
| 5330 |
} else { |
| 5331 |
$handlerClass = ''; |
| 5332 |
} |
| 5333 |
if ( !isset( $this->mImageParams[$handlerClass] ) ) { |
| 5334 |
|
| 5335 |
static $internalParamNames = [ |
| 5336 |
'horizAlign' => [ 'left', 'right', 'center', 'none' ], |
| 5337 |
'vertAlign' => [ 'baseline', 'sub', 'super', 'top', 'text-top', 'middle', |
| 5338 |
'bottom', 'text-bottom' ], |
| 5339 |
'frame' => [ 'thumbnail', 'framed', 'frameless', 'border', |
| 5340 |
|
| 5341 |
|
| 5342 |
'manualthumb', 'upright', 'link', 'alt', 'class' ], |
| 5343 |
]; |
| 5344 |
static $internalParamMap; |
| 5345 |
if ( !$internalParamMap ) { |
| 5346 |
$internalParamMap = []; |
| 5347 |
foreach ( $internalParamNames as $type => $names ) { |
| 5348 |
foreach ( $names as $name ) { |
| 5349 |
|
| 5350 |
|
| 5351 |
|
| 5352 |
|
| 5353 |
|
| 5354 |
$magicName = str_replace( '-', '_', "img_$name" ); |
| 5355 |
$internalParamMap[$magicName] = [ $type, $name ]; |
| 5356 |
} |
| 5357 |
} |
| 5358 |
} |
| 5359 |
|
| 5360 |
|
| 5361 |
|
| 5362 |
|
| 5363 |
$paramMap = $internalParamMap; |
| 5364 |
if ( $handler ) { |
| 5365 |
$handlerParamMap = $handler->getParamMap(); |
| 5366 |
foreach ( $handlerParamMap as $magic => $paramName ) { |
| 5367 |
$paramMap[$magic] = [ 'handler', $paramName ]; |
| 5368 |
} |
| 5369 |
} else { |
| 5370 |
|
| 5371 |
$paramMap[ 'img_width' ] = [ 'handler', 'width' ]; |
| 5372 |
} |
| 5373 |
$this->mImageParams[$handlerClass] = $paramMap; |
| 5374 |
$this->mImageParamsMagicArray[$handlerClass] = |
| 5375 |
$this->magicWordFactory->newArray( array_keys( $paramMap ) ); |
| 5376 |
} |
| 5377 |
return [ $this->mImageParams[$handlerClass], $this->mImageParamsMagicArray[$handlerClass] ]; |
| 5378 |
} |
| 5379 |
|
| 5380 |
|
| 5381 |
|
| 5382 |
|
| 5383 |
|
| 5384 |
|
| 5385 |
|
| 5386 |
|
| 5387 |
|
| 5388 |
|
| 5389 |
public function makeImage( LinkTarget $link, $options, $holders = false ) { |
| 5390 |
|
| 5391 |
|
| 5392 |
|
| 5393 |
|
| 5394 |
|
| 5395 |
|
| 5396 |
|
| 5397 |
|
| 5398 |
|
| 5399 |
|
| 5400 |
|
| 5401 |
|
| 5402 |
|
| 5403 |
|
| 5404 |
|
| 5405 |
|
| 5406 |
|
| 5407 |
|
| 5408 |
|
| 5409 |
|
| 5410 |
|
| 5411 |
|
| 5412 |
|
| 5413 |
|
| 5414 |
|
| 5415 |
|
| 5416 |
$parts = StringUtils::delimiterExplode( |
| 5417 |
'-{', '}-', '|', $options, true |
| 5418 |
); |
| 5419 |
|
| 5420 |
|
| 5421 |
$options = []; |
| 5422 |
$descQuery = false; |
| 5423 |
$title = Title::castFromLinkTarget( $link ); |
| 5424 |
$this->hookRunner->onBeforeParserFetchFileAndTitle( |
| 5425 |
|
| 5426 |
$this, $title, $options, $descQuery |
| 5427 |
); |
| 5428 |
|
| 5429 |
[ $file, $link ] = $this->fetchFileAndTitle( $link, $options ); |
| 5430 |
|
| 5431 |
|
| 5432 |
$handler = $file ? $file->getHandler() : false; |
| 5433 |
|
| 5434 |
[ $paramMap, $mwArray ] = $this->getImageParams( $handler ); |
| 5435 |
|
| 5436 |
if ( !$file ) { |
| 5437 |
$this->addTrackingCategory( 'broken-file-category' ); |
| 5438 |
} |
| 5439 |
|
| 5440 |
|
| 5441 |
$caption = ''; |
| 5442 |
$params = [ 'frame' => [], 'handler' => [], |
| 5443 |
'horizAlign' => [], 'vertAlign' => [] ]; |
| 5444 |
$seenformat = false; |
| 5445 |
foreach ( $parts as $part ) { |
| 5446 |
[ $magicName, $value ] = $mwArray->matchVariableStartToEnd( trim( $part ) ); |
| 5447 |
$validated = false; |
| 5448 |
if ( isset( $paramMap[$magicName] ) ) { |
| 5449 |
[ $type, $paramName ] = $paramMap[$magicName]; |
| 5450 |
|
| 5451 |
|
| 5452 |
if ( $type === 'handler' && $paramName === 'width' ) { |
| 5453 |
|
| 5454 |
$parsedWidthParam = $this->parseWidthParam( $value, true, true ); |
| 5455 |
|
| 5456 |
|
| 5457 |
$validateFunc = static function ( $name, $value ) use ( $handler ) { |
| 5458 |
return $handler |
| 5459 |
? $handler->validateParam( $name, $value ) |
| 5460 |
: $value > 0; |
| 5461 |
}; |
| 5462 |
if ( isset( $parsedWidthParam['width'] ) ) { |
| 5463 |
$width = $parsedWidthParam['width']; |
| 5464 |
if ( $validateFunc( 'width', $width ) ) { |
| 5465 |
$params[$type]['width'] = $width; |
| 5466 |
$validated = true; |
| 5467 |
} |
| 5468 |
} |
| 5469 |
if ( isset( $parsedWidthParam['height'] ) ) { |
| 5470 |
$height = $parsedWidthParam['height']; |
| 5471 |
if ( $validateFunc( 'height', $height ) ) { |
| 5472 |
$params[$type]['height'] = $height; |
| 5473 |
$validated = true; |
| 5474 |
} |
| 5475 |
} |
| 5476 |
|
| 5477 |
} else { |
| 5478 |
if ( $type === 'handler' ) { |
| 5479 |
|
| 5480 |
$validated = $handler->validateParam( $paramName, $value ); |
| 5481 |
} else { |
| 5482 |
|
| 5483 |
switch ( $paramName ) { |
| 5484 |
case 'alt': |
| 5485 |
case 'class': |
| 5486 |
$validated = true; |
| 5487 |
$value = $this->stripAltText( $value, $holders ); |
| 5488 |
break; |
| 5489 |
case 'link': |
| 5490 |
[ $paramName, $value ] = |
| 5491 |
$this->parseLinkParameter( |
| 5492 |
$this->stripAltText( $value, $holders ) |
| 5493 |
); |
| 5494 |
if ( $paramName ) { |
| 5495 |
$validated = true; |
| 5496 |
if ( $paramName === 'no-link' ) { |
| 5497 |
$value = true; |
| 5498 |
} |
| 5499 |
} |
| 5500 |
break; |
| 5501 |
case 'manualthumb': |
| 5502 |
|
| 5503 |
|
| 5504 |
|
| 5505 |
$value = $this->stripAltText( $value, $holders ); |
| 5506 |
|
| 5507 |
case 'frameless': |
| 5508 |
case 'framed': |
| 5509 |
case 'thumbnail': |
| 5510 |
|
| 5511 |
$validated = !$seenformat; |
| 5512 |
$seenformat = true; |
| 5513 |
break; |
| 5514 |
default: |
| 5515 |
|
| 5516 |
$validated = ( $value === false || is_numeric( trim( $value ) ) ); |
| 5517 |
} |
| 5518 |
} |
| 5519 |
|
| 5520 |
if ( $validated ) { |
| 5521 |
$params[$type][$paramName] = $value; |
| 5522 |
} |
| 5523 |
} |
| 5524 |
} |
| 5525 |
if ( !$validated ) { |
| 5526 |
$caption = $part; |
| 5527 |
} |
| 5528 |
} |
| 5529 |
|
| 5530 |
|
| 5531 |
if ( $params['horizAlign'] !== [] ) { |
| 5532 |
$params['frame']['align'] = array_key_first( $params['horizAlign'] ); |
| 5533 |
} |
| 5534 |
if ( $params['vertAlign'] !== [] ) { |
| 5535 |
$params['frame']['valign'] = array_key_first( $params['vertAlign'] ); |
| 5536 |
} |
| 5537 |
|
| 5538 |
$params['frame']['caption'] = $caption; |
| 5539 |
|
| 5540 |
$enableLegacyMediaDOM = $this->svcOptions->get( MainConfigNames::ParserEnableLegacyMediaDOM ); |
| 5541 |
|
| 5542 |
|
| 5543 |
|
| 5544 |
$hasVisibleCaption = isset( $params['frame']['framed'] ) |
| 5545 |
|
| 5546 |
|| isset( $params['frame']['thumbnail'] ) |
| 5547 |
|
| 5548 |
|| isset( $params['frame']['manualthumb'] ); |
| 5549 |
|
| 5550 |
|
| 5551 |
|
| 5552 |
|
| 5553 |
|
| 5554 |
|
| 5555 |
|
| 5556 |
|
| 5557 |
|
| 5558 |
|
| 5559 |
|
| 5560 |
|
| 5561 |
|
| 5562 |
|
| 5563 |
|
| 5564 |
if ( $hasVisibleCaption ) { |
| 5565 |
if ( |
| 5566 |
|
| 5567 |
$caption === '' && !isset( $params['frame']['alt'] ) && |
| 5568 |
$enableLegacyMediaDOM |
| 5569 |
) { |
| 5570 |
|
| 5571 |
|
| 5572 |
$params['frame']['alt'] = $link->getText(); |
| 5573 |
} |
| 5574 |
|
| 5575 |
|
| 5576 |
} else { |
| 5577 |
|
| 5578 |
if ( !isset( $params['frame']['alt'] ) ) { |
| 5579 |
|
| 5580 |
if ( $caption !== '' ) { |
| 5581 |
$params['frame']['alt'] = $this->stripAltText( $caption, $holders ); |
| 5582 |
} elseif ( $enableLegacyMediaDOM ) { |
| 5583 |
|
| 5584 |
|
| 5585 |
$params['frame']['alt'] = $link->getText(); |
| 5586 |
} |
| 5587 |
} |
| 5588 |
|
| 5589 |
$params['frame']['title'] = $this->stripAltText( $caption, $holders ); |
| 5590 |
} |
| 5591 |
$params['handler']['targetlang'] = $this->getTargetLanguage()->getCode(); |
| 5592 |
|
| 5593 |
|
| 5594 |
$title = Title::castFromLinkTarget( $link ); |
| 5595 |
$this->hookRunner->onParserMakeImageParams( $title, $file, $params, $this ); |
| 5596 |
|
| 5597 |
|
| 5598 |
$time = $options['time'] ?? false; |
| 5599 |
|
| 5600 |
$ret = Linker::makeImageLink( $this, $link, $file, $params['frame'], $params['handler'], |
| 5601 |
$time, $descQuery, $this->mOptions->getThumbSize() ); |
| 5602 |
|
| 5603 |
|
| 5604 |
if ( $handler ) { |
| 5605 |
$handler->parserTransformHook( $this, $file ); |
| 5606 |
} |
| 5607 |
if ( $file ) { |
| 5608 |
$this->modifyImageHtml( $file, $params, $ret ); |
| 5609 |
} |
| 5610 |
|
| 5611 |
return $ret; |
| 5612 |
} |
| 5613 |
|
| 5614 |
|
| 5615 |
|
| 5616 |
|
| 5617 |
|
| 5618 |
|
| 5619 |
|
| 5620 |
|
| 5621 |
|
| 5622 |
|
| 5623 |
|
| 5624 |
|
| 5625 |
|
| 5626 |
|
| 5627 |
|
| 5628 |
|
| 5629 |
|
| 5630 |
|
| 5631 |
|
| 5632 |
private function parseLinkParameter( $value ) { |
| 5633 |
$chars = self::EXT_LINK_URL_CLASS; |
| 5634 |
$addr = self::EXT_LINK_ADDR; |
| 5635 |
$prots = $this->urlUtils->validProtocols(); |
| 5636 |
$type = null; |
| 5637 |
$target = false; |
| 5638 |
if ( $value === '' ) { |
| 5639 |
$type = 'no-link'; |
| 5640 |
} elseif ( preg_match( "/^((?i)$prots)/", $value ) ) { |
| 5641 |
if ( preg_match( "/^((?i)$prots)$addr$chars*$/u", $value ) ) { |
| 5642 |
$this->mOutput->addExternalLink( $value ); |
| 5643 |
$type = 'link-url'; |
| 5644 |
$target = $value; |
| 5645 |
} |
| 5646 |
} else { |
| 5647 |
|
| 5648 |
|
| 5649 |
|
| 5650 |
|
| 5651 |
|
| 5652 |
|
| 5653 |
|
| 5654 |
|
| 5655 |
|
| 5656 |
|
| 5657 |
|
| 5658 |
|
| 5659 |
|
| 5660 |
|
| 5661 |
|
| 5662 |
|
| 5663 |
|
| 5664 |
|
| 5665 |
|
| 5666 |
|
| 5667 |
|
| 5668 |
|
| 5669 |
|
| 5670 |
|
| 5671 |
|
| 5672 |
if ( strpos( $value, '%' ) !== false ) { |
| 5673 |
$value = rawurldecode( $value ); |
| 5674 |
} |
| 5675 |
$linkTitle = Title::newFromText( $value ); |
| 5676 |
if ( $linkTitle ) { |
| 5677 |
$this->mOutput->addLink( $linkTitle ); |
| 5678 |
$type = 'link-title'; |
| 5679 |
$target = $linkTitle; |
| 5680 |
} |
| 5681 |
} |
| 5682 |
return [ $type, $target ]; |
| 5683 |
} |
| 5684 |
|
| 5685 |
|
| 5686 |
|
| 5687 |
|
| 5688 |
|
| 5689 |
|
| 5690 |
|
| 5691 |
|
| 5692 |
public function modifyImageHtml( File $file, array $params, string &$html ) { |
| 5693 |
$this->hookRunner->onParserModifyImageHTML( $this, $file, $params, $html ); |
| 5694 |
} |
| 5695 |
|
| 5696 |
|
| 5697 |
|
| 5698 |
|
| 5699 |
|
| 5700 |
|
| 5701 |
private function stripAltText( $caption, $holders ) { |
| 5702 |
|
| 5703 |
|
| 5704 |
|
| 5705 |
if ( $holders ) { |
| 5706 |
$tooltip = $holders->replaceText( $caption ); |
| 5707 |
} else { |
| 5708 |
$tooltip = $this->replaceLinkHoldersText( $caption ); |
| 5709 |
} |
| 5710 |
|
| 5711 |
|
| 5712 |
|
| 5713 |
|
| 5714 |
$tooltip = $this->mStripState->unstripBoth( $tooltip ); |
| 5715 |
|
| 5716 |
|
| 5717 |
|
| 5718 |
|
| 5719 |
|
| 5720 |
|
| 5721 |
|
| 5722 |
|
| 5723 |
$tooltip = preg_replace( "/ |
| 5724 |
& # 1. entity prefix |
| 5725 |
(?= # 2. followed by: |
| 5726 |
(?: # a. one of the legacy semicolon-less named entities |
| 5727 |
A(?:Elig|MP|acute|circ|grave|ring|tilde|uml)| |
| 5728 |
C(?:OPY|cedil)|E(?:TH|acute|circ|grave|uml)| |
| 5729 |
GT|I(?:acute|circ|grave|uml)|LT|Ntilde| |
| 5730 |
O(?:acute|circ|grave|slash|tilde|uml)|QUOT|REG|THORN| |
| 5731 |
U(?:acute|circ|grave|uml)|Yacute| |
| 5732 |
a(?:acute|c(?:irc|ute)|elig|grave|mp|ring|tilde|uml)|brvbar| |
| 5733 |
c(?:cedil|edil|urren)|cent(?!erdot;)|copy(?!sr;)|deg| |
| 5734 |
divide(?!ontimes;)|e(?:acute|circ|grave|th|uml)| |
| 5735 |
frac(?:1(?:2|4)|34)| |
| 5736 |
gt(?!c(?:c|ir)|dot|lPar|quest|r(?:a(?:pprox|rr)|dot|eq(?:less|qless)|less|sim);)| |
| 5737 |
i(?:acute|circ|excl|grave|quest|uml)|laquo| |
| 5738 |
lt(?!c(?:c|ir)|dot|hree|imes|larr|quest|r(?:Par|i(?:e|f|));)| |
| 5739 |
m(?:acr|i(?:cro|ddot))|n(?:bsp|tilde)| |
| 5740 |
not(?!in(?:E|dot|v(?:a|b|c)|)|ni(?:v(?:a|b|c)|);)| |
| 5741 |
o(?:acute|circ|grave|rd(?:f|m)|slash|tilde|uml)| |
| 5742 |
p(?:lusmn|ound)|para(?!llel;)|quot|r(?:aquo|eg)| |
| 5743 |
s(?:ect|hy|up(?:1|2|3)|zlig)|thorn|times(?!b(?:ar|)|d;)| |
| 5744 |
u(?:acute|circ|grave|ml|uml)|y(?:acute|en|uml) |
| 5745 |
) |
| 5746 |
(?:[^;]|$)) # b. and not followed by a semicolon |
| 5747 |
# S = study, for efficiency |
| 5748 |
/Sx", '&', $tooltip ); |
| 5749 |
$tooltip = Sanitizer::stripAllTags( $tooltip ); |
| 5750 |
|
| 5751 |
return $tooltip; |
| 5752 |
} |
| 5753 |
|
| 5754 |
|
| 5755 |
|
| 5756 |
|
| 5757 |
|
| 5758 |
|
| 5759 |
|
| 5760 |
|
| 5761 |
|
| 5762 |
|
| 5763 |
public function attributeStripCallback( &$text, $frame = false ) { |
| 5764 |
wfDeprecated( __METHOD__, '1.35' ); |
| 5765 |
$text = $this->replaceVariables( $text, $frame ); |
| 5766 |
$text = $this->mStripState->unstripBoth( $text ); |
| 5767 |
return $text; |
| 5768 |
} |
| 5769 |
|
| 5770 |
|
| 5771 |
|
| 5772 |
|
| 5773 |
|
| 5774 |
|
| 5775 |
|
| 5776 |
public function getTags(): array { |
| 5777 |
return array_keys( $this->mTagHooks ); |
| 5778 |
} |
| 5779 |
|
| 5780 |
|
| 5781 |
|
| 5782 |
|
| 5783 |
|
| 5784 |
public function getFunctionSynonyms() { |
| 5785 |
return $this->mFunctionSynonyms; |
| 5786 |
} |
| 5787 |
|
| 5788 |
|
| 5789 |
|
| 5790 |
|
| 5791 |
|
| 5792 |
public function getUrlProtocols() { |
| 5793 |
return $this->urlUtils->validProtocols(); |
| 5794 |
} |
| 5795 |
|
| 5796 |
|
| 5797 |
|
| 5798 |
|
| 5799 |
|
| 5800 |
|
| 5801 |
|
| 5802 |
|
| 5803 |
|
| 5804 |
|
| 5805 |
|
| 5806 |
|
| 5807 |
|
| 5808 |
|
| 5809 |
|
| 5810 |
|
| 5811 |
|
| 5812 |
|
| 5813 |
|
| 5814 |
|
| 5815 |
|
| 5816 |
|
| 5817 |
|
| 5818 |
|
| 5819 |
|
| 5820 |
|
| 5821 |
|
| 5822 |
|
| 5823 |
|
| 5824 |
|
| 5825 |
|
| 5826 |
private function extractSections( $text, $sectionId, $mode, $newText, ?PageReference $page = null ) { |
| 5827 |
$magicScopeVariable = $this->lock(); |
| 5828 |
$this->startParse( |
| 5829 |
$page, |
| 5830 |
ParserOptions::newFromUser( RequestContext::getMain()->getUser() ), |
| 5831 |
self::OT_PLAIN, |
| 5832 |
true |
| 5833 |
); |
| 5834 |
$outText = ''; |
| 5835 |
$frame = $this->getPreprocessor()->newFrame(); |
| 5836 |
|
| 5837 |
|
| 5838 |
$flags = 0; |
| 5839 |
$sectionParts = explode( '-', $sectionId ); |
| 5840 |
|
| 5841 |
|
| 5842 |
|
| 5843 |
$sectionIndex = (int)array_pop( $sectionParts ); |
| 5844 |
foreach ( $sectionParts as $part ) { |
| 5845 |
if ( $part === 'T' ) { |
| 5846 |
$flags |= Preprocessor::DOM_FOR_INCLUSION; |
| 5847 |
} |
| 5848 |
} |
| 5849 |
|
| 5850 |
|
| 5851 |
if ( strval( $text ) === '' ) { |
| 5852 |
|
| 5853 |
if ( $sectionIndex === 0 ) { |
| 5854 |
if ( $mode === 'get' ) { |
| 5855 |
return ''; |
| 5856 |
} |
| 5857 |
|
| 5858 |
return $newText; |
| 5859 |
} else { |
| 5860 |
if ( $mode === 'get' ) { |
| 5861 |
return $newText; |
| 5862 |
} |
| 5863 |
|
| 5864 |
return $text; |
| 5865 |
} |
| 5866 |
} |
| 5867 |
|
| 5868 |
|
| 5869 |
$root = $this->preprocessToDom( $text, $flags ); |
| 5870 |
|
| 5871 |
|
| 5872 |
|
| 5873 |
$node = $root->getFirstChild(); |
| 5874 |
|
| 5875 |
|
| 5876 |
if ( $sectionIndex === 0 ) { |
| 5877 |
|
| 5878 |
$targetLevel = 1000; |
| 5879 |
} else { |
| 5880 |
while ( $node ) { |
| 5881 |
if ( $node->getName() === 'h' ) { |
| 5882 |
$bits = $node->splitHeading(); |
| 5883 |
if ( $bits['i'] == $sectionIndex ) { |
| 5884 |
$targetLevel = $bits['level']; |
| 5885 |
break; |
| 5886 |
} |
| 5887 |
} |
| 5888 |
if ( $mode === 'replace' ) { |
| 5889 |
$outText .= $frame->expand( $node, PPFrame::RECOVER_ORIG ); |
| 5890 |
} |
| 5891 |
$node = $node->getNextSibling(); |
| 5892 |
} |
| 5893 |
} |
| 5894 |
|
| 5895 |
if ( !$node ) { |
| 5896 |
|
| 5897 |
if ( $mode === 'get' ) { |
| 5898 |
return $newText; |
| 5899 |
} else { |
| 5900 |
return $text; |
| 5901 |
} |
| 5902 |
} |
| 5903 |
|
| 5904 |
|
| 5905 |
do { |
| 5906 |
if ( $node->getName() === 'h' ) { |
| 5907 |
$bits = $node->splitHeading(); |
| 5908 |
$curLevel = $bits['level']; |
| 5909 |
|
| 5910 |
if ( $bits['i'] != $sectionIndex && $curLevel <= $targetLevel ) { |
| 5911 |
break; |
| 5912 |
} |
| 5913 |
} |
| 5914 |
if ( $mode === 'get' ) { |
| 5915 |
$outText .= $frame->expand( $node, PPFrame::RECOVER_ORIG ); |
| 5916 |
} |
| 5917 |
$node = $node->getNextSibling(); |
| 5918 |
} while ( $node ); |
| 5919 |
|
| 5920 |
|
| 5921 |
if ( $mode === 'replace' ) { |
| 5922 |
|
| 5923 |
|
| 5924 |
|
| 5925 |
|
| 5926 |
if ( $newText != "" ) { |
| 5927 |
$outText .= $newText . "\n\n"; |
| 5928 |
} |
| 5929 |
|
| 5930 |
while ( $node ) { |
| 5931 |
$outText .= $frame->expand( $node, PPFrame::RECOVER_ORIG ); |
| 5932 |
$node = $node->getNextSibling(); |
| 5933 |
} |
| 5934 |
} |
| 5935 |
|
| 5936 |
|
| 5937 |
$outText = rtrim( $this->mStripState->unstripBoth( $outText ) ); |
| 5938 |
|
| 5939 |
return $outText; |
| 5940 |
} |
| 5941 |
|
| 5942 |
|
| 5943 |
|
| 5944 |
|
| 5945 |
|
| 5946 |
|
| 5947 |
|
| 5948 |
|
| 5949 |
|
| 5950 |
|
| 5951 |
|
| 5952 |
|
| 5953 |
|
| 5954 |
|
| 5955 |
|
| 5956 |
|
| 5957 |
public function getSection( $text, $sectionId, $defaultText = '' ) { |
| 5958 |
return $this->extractSections( $text, $sectionId, 'get', $defaultText ); |
| 5959 |
} |
| 5960 |
|
| 5961 |
|
| 5962 |
|
| 5963 |
|
| 5964 |
|
| 5965 |
|
| 5966 |
|
| 5967 |
|
| 5968 |
|
| 5969 |
|
| 5970 |
|
| 5971 |
|
| 5972 |
|
| 5973 |
|
| 5974 |
public function replaceSection( $oldText, $sectionId, $newText ) { |
| 5975 |
return $this->extractSections( $oldText, $sectionId, 'replace', $newText ); |
| 5976 |
} |
| 5977 |
|
| 5978 |
|
| 5979 |
|
| 5980 |
|
| 5981 |
|
| 5982 |
|
| 5983 |
|
| 5984 |
|
| 5985 |
|
| 5986 |
|
| 5987 |
|
| 5988 |
|
| 5989 |
|
| 5990 |
|
| 5991 |
|
| 5992 |
|
| 5993 |
|
| 5994 |
|
| 5995 |
|
| 5996 |
|
| 5997 |
|
| 5998 |
|
| 5999 |
|
| 6000 |
|
| 6001 |
|
| 6002 |
|
| 6003 |
|
| 6004 |
|
| 6005 |
|
| 6006 |
|
| 6007 |
public function getFlatSectionInfo( $text ) { |
| 6008 |
$magicScopeVariable = $this->lock(); |
| 6009 |
$this->startParse( |
| 6010 |
null, |
| 6011 |
ParserOptions::newFromUser( RequestContext::getMain()->getUser() ), |
| 6012 |
self::OT_PLAIN, |
| 6013 |
true |
| 6014 |
); |
| 6015 |
$frame = $this->getPreprocessor()->newFrame(); |
| 6016 |
$root = $this->preprocessToDom( $text, 0 ); |
| 6017 |
$node = $root->getFirstChild(); |
| 6018 |
$offset = 0; |
| 6019 |
$currentSection = [ |
| 6020 |
'index' => 0, |
| 6021 |
'level' => 0, |
| 6022 |
'offset' => 0, |
| 6023 |
'heading' => '', |
| 6024 |
'text' => '' |
| 6025 |
]; |
| 6026 |
$sections = []; |
| 6027 |
|
| 6028 |
while ( $node ) { |
| 6029 |
$nodeText = $frame->expand( $node, PPFrame::RECOVER_ORIG ); |
| 6030 |
if ( $node->getName() === 'h' ) { |
| 6031 |
$bits = $node->splitHeading(); |
| 6032 |
$sections[] = $currentSection; |
| 6033 |
$currentSection = [ |
| 6034 |
'index' => $bits['i'], |
| 6035 |
'level' => $bits['level'], |
| 6036 |
'offset' => $offset, |
| 6037 |
'heading' => $nodeText, |
| 6038 |
'text' => $nodeText |
| 6039 |
]; |
| 6040 |
} else { |
| 6041 |
$currentSection['text'] .= $nodeText; |
| 6042 |
} |
| 6043 |
$offset += strlen( $nodeText ); |
| 6044 |
$node = $node->getNextSibling(); |
| 6045 |
} |
| 6046 |
$sections[] = $currentSection; |
| 6047 |
return $sections; |
| 6048 |
} |
| 6049 |
|
| 6050 |
|
| 6051 |
|
| 6052 |
|
| 6053 |
|
| 6054 |
|
| 6055 |
|
| 6056 |
|
| 6057 |
|
| 6058 |
|
| 6059 |
|
| 6060 |
|
| 6061 |
public function getRevisionId() { |
| 6062 |
return $this->mRevisionId; |
| 6063 |
} |
| 6064 |
|
| 6065 |
|
| 6066 |
|
| 6067 |
|
| 6068 |
|
| 6069 |
|
| 6070 |
|
| 6071 |
public function getRevisionRecordObject() { |
| 6072 |
if ( $this->mRevisionRecordObject ) { |
| 6073 |
return $this->mRevisionRecordObject; |
| 6074 |
} |
| 6075 |
|
| 6076 |
|
| 6077 |
|
| 6078 |
|
| 6079 |
|
| 6080 |
|
| 6081 |
|
| 6082 |
|
| 6083 |
$rev = call_user_func( |
| 6084 |
$this->mOptions->getCurrentRevisionRecordCallback(), |
| 6085 |
$this->getTitle(), |
| 6086 |
$this |
| 6087 |
); |
| 6088 |
|
| 6089 |
if ( !$rev ) { |
| 6090 |
|
| 6091 |
|
| 6092 |
|
| 6093 |
|
| 6094 |
return null; |
| 6095 |
} |
| 6096 |
|
| 6097 |
if ( $this->mRevisionId === null && $rev->getId() ) { |
| 6098 |
|
| 6099 |
|
| 6100 |
|
| 6101 |
|
| 6102 |
|
| 6103 |
return null; |
| 6104 |
} |
| 6105 |
|
| 6106 |
|
| 6107 |
|
| 6108 |
|
| 6109 |
if ( $this->mRevisionId && $rev->getId() != $this->mRevisionId ) { |
| 6110 |
$rev = MediaWikiServices::getInstance() |
| 6111 |
->getRevisionLookup() |
| 6112 |
->getRevisionById( $this->mRevisionId ); |
| 6113 |
} |
| 6114 |
|
| 6115 |
$this->mRevisionRecordObject = $rev; |
| 6116 |
|
| 6117 |
return $this->mRevisionRecordObject; |
| 6118 |
} |
| 6119 |
|
| 6120 |
|
| 6121 |
|
| 6122 |
|
| 6123 |
|
| 6124 |
|
| 6125 |
|
| 6126 |
public function getRevisionTimestamp() { |
| 6127 |
if ( $this->mRevisionTimestamp !== null ) { |
| 6128 |
return $this->mRevisionTimestamp; |
| 6129 |
} |
| 6130 |
|
| 6131 |
|
| 6132 |
$revObject = $this->getRevisionRecordObject(); |
| 6133 |
$timestamp = $revObject && $revObject->getTimestamp() |
| 6134 |
? $revObject->getTimestamp() |
| 6135 |
: $this->mOptions->getTimestamp(); |
| 6136 |
$this->mOutput->setRevisionTimestampUsed( $timestamp ); |
| 6137 |
|
| 6138 |
|
| 6139 |
|
| 6140 |
|
| 6141 |
|
| 6142 |
|
| 6143 |
$this->mRevisionTimestamp = $this->contLang->userAdjust( $timestamp, '' ); |
| 6144 |
|
| 6145 |
return $this->mRevisionTimestamp; |
| 6146 |
} |
| 6147 |
|
| 6148 |
|
| 6149 |
|
| 6150 |
|
| 6151 |
|
| 6152 |
|
| 6153 |
|
| 6154 |
public function getRevisionUser(): ?string { |
| 6155 |
if ( $this->mRevisionUser === null ) { |
| 6156 |
$revObject = $this->getRevisionRecordObject(); |
| 6157 |
|
| 6158 |
|
| 6159 |
|
| 6160 |
if ( $revObject && $revObject->getUser() ) { |
| 6161 |
$this->mRevisionUser = $revObject->getUser()->getName(); |
| 6162 |
} elseif ( $this->ot['wiki'] || $this->mOptions->getIsPreview() ) { |
| 6163 |
$this->mRevisionUser = $this->getUserIdentity()->getName(); |
| 6164 |
} else { |
| 6165 |
|
| 6166 |
|
| 6167 |
} |
| 6168 |
} |
| 6169 |
return $this->mRevisionUser; |
| 6170 |
} |
| 6171 |
|
| 6172 |
|
| 6173 |
|
| 6174 |
|
| 6175 |
|
| 6176 |
|
| 6177 |
|
| 6178 |
public function getRevisionSize() { |
| 6179 |
if ( $this->mRevisionSize === null ) { |
| 6180 |
$revObject = $this->getRevisionRecordObject(); |
| 6181 |
|
| 6182 |
|
| 6183 |
|
| 6184 |
|
| 6185 |
if ( $revObject ) { |
| 6186 |
$this->mRevisionSize = $revObject->getSize(); |
| 6187 |
} else { |
| 6188 |
$this->mRevisionSize = $this->mInputSize; |
| 6189 |
} |
| 6190 |
} |
| 6191 |
return $this->mRevisionSize; |
| 6192 |
} |
| 6193 |
|
| 6194 |
|
| 6195 |
|
| 6196 |
|
| 6197 |
|
| 6198 |
|
| 6199 |
|
| 6200 |
|
| 6201 |
|
| 6202 |
|
| 6203 |
|
| 6204 |
|
| 6205 |
|
| 6206 |
|
| 6207 |
public function getDefaultSort() { |
| 6208 |
wfDeprecated( __METHOD__, '1.38' ); |
| 6209 |
return $this->mOutput->getPageProperty( 'defaultsort' ) ?? ''; |
| 6210 |
} |
| 6211 |
|
| 6212 |
private static function getSectionNameFromStrippedText( $text ) { |
| 6213 |
$text = Sanitizer::normalizeSectionNameWhitespace( $text ); |
| 6214 |
$text = Sanitizer::decodeCharReferences( $text ); |
| 6215 |
$text = self::normalizeSectionName( $text ); |
| 6216 |
return $text; |
| 6217 |
} |
| 6218 |
|
| 6219 |
private static function makeAnchor( $sectionName ) { |
| 6220 |
return '#' . Sanitizer::escapeIdForLink( $sectionName ); |
| 6221 |
} |
| 6222 |
|
| 6223 |
private function makeLegacyAnchor( $sectionName ) { |
| 6224 |
$fragmentMode = $this->svcOptions->get( MainConfigNames::FragmentMode ); |
| 6225 |
if ( isset( $fragmentMode[1] ) && $fragmentMode[1] === 'legacy' ) { |
| 6226 |
|
| 6227 |
$id = Sanitizer::escapeIdForAttribute( $sectionName, Sanitizer::ID_FALLBACK ); |
| 6228 |
} else { |
| 6229 |
$id = Sanitizer::escapeIdForLink( $sectionName ); |
| 6230 |
} |
| 6231 |
|
| 6232 |
return "#$id"; |
| 6233 |
} |
| 6234 |
|
| 6235 |
|
| 6236 |
|
| 6237 |
|
| 6238 |
|
| 6239 |
|
| 6240 |
|
| 6241 |
|
| 6242 |
|
| 6243 |
|
| 6244 |
public function guessSectionNameFromWikiText( $text ) { |
| 6245 |
|
| 6246 |
$text = $this->stripSectionName( $text ); |
| 6247 |
$sectionName = self::getSectionNameFromStrippedText( $text ); |
| 6248 |
return self::makeAnchor( $sectionName ); |
| 6249 |
} |
| 6250 |
|
| 6251 |
|
| 6252 |
|
| 6253 |
|
| 6254 |
|
| 6255 |
|
| 6256 |
|
| 6257 |
|
| 6258 |
|
| 6259 |
|
| 6260 |
|
| 6261 |
public function guessLegacySectionNameFromWikiText( $text ) { |
| 6262 |
|
| 6263 |
$text = $this->stripSectionName( $text ); |
| 6264 |
$sectionName = self::getSectionNameFromStrippedText( $text ); |
| 6265 |
return $this->makeLegacyAnchor( $sectionName ); |
| 6266 |
} |
| 6267 |
|
| 6268 |
|
| 6269 |
|
| 6270 |
|
| 6271 |
|
| 6272 |
|
| 6273 |
|
| 6274 |
public static function guessSectionNameFromStrippedText( $text ) { |
| 6275 |
$sectionName = self::getSectionNameFromStrippedText( $text ); |
| 6276 |
return self::makeAnchor( $sectionName ); |
| 6277 |
} |
| 6278 |
|
| 6279 |
|
| 6280 |
|
| 6281 |
|
| 6282 |
|
| 6283 |
|
| 6284 |
|
| 6285 |
private static function normalizeSectionName( $text ) { |
| 6286 |
|
| 6287 |
|
| 6288 |
$titleParser = MediaWikiServices::getInstance()->getTitleParser(); |
| 6289 |
'@phan-var MediaWikiTitleCodec $titleParser'; |
| 6290 |
try { |
| 6291 |
|
| 6292 |
$parts = $titleParser->splitTitleString( "#$text" ); |
| 6293 |
} catch ( MalformedTitleException $ex ) { |
| 6294 |
return $text; |
| 6295 |
} |
| 6296 |
return $parts['fragment']; |
| 6297 |
} |
| 6298 |
|
| 6299 |
|
| 6300 |
|
| 6301 |
|
| 6302 |
|
| 6303 |
|
| 6304 |
|
| 6305 |
|
| 6306 |
|
| 6307 |
|
| 6308 |
|
| 6309 |
|
| 6310 |
|
| 6311 |
|
| 6312 |
|
| 6313 |
|
| 6314 |
public function stripSectionName( $text ) { |
| 6315 |
|
| 6316 |
$text = preg_replace( '/\[\[:?([^[|]+)\|([^[]+)\]\]/', '$2', $text ); |
| 6317 |
$text = preg_replace( '/\[\[:?([^[]+)\|?\]\]/', '$1', $text ); |
| 6318 |
|
| 6319 |
|
| 6320 |
|
| 6321 |
|
| 6322 |
|
| 6323 |
$text = preg_replace( |
| 6324 |
'/\[(?i:' . $this->urlUtils->validProtocols() . ')([^ ]+?) ([^[]+)\]/', '$2', $text ); |
| 6325 |
|
| 6326 |
|
| 6327 |
$text = $this->doQuotes( $text ); |
| 6328 |
|
| 6329 |
|
| 6330 |
$text = StringUtils::delimiterReplace( '<', '>', '', $text ); |
| 6331 |
return $text; |
| 6332 |
} |
| 6333 |
|
| 6334 |
|
| 6335 |
|
| 6336 |
|
| 6337 |
|
| 6338 |
|
| 6339 |
|
| 6340 |
|
| 6341 |
|
| 6342 |
|
| 6343 |
|
| 6344 |
|
| 6345 |
|
| 6346 |
|
| 6347 |
|
| 6348 |
|
| 6349 |
|
| 6350 |
|
| 6351 |
|
| 6352 |
public function markerSkipCallback( $s, callable $callback ) { |
| 6353 |
$i = 0; |
| 6354 |
$out = ''; |
| 6355 |
while ( $i < strlen( $s ) ) { |
| 6356 |
$markerStart = strpos( $s, self::MARKER_PREFIX, $i ); |
| 6357 |
if ( $markerStart === false ) { |
| 6358 |
$out .= call_user_func( $callback, substr( $s, $i ) ); |
| 6359 |
break; |
| 6360 |
} else { |
| 6361 |
$out .= call_user_func( $callback, substr( $s, $i, $markerStart - $i ) ); |
| 6362 |
$markerEnd = strpos( $s, self::MARKER_SUFFIX, $markerStart ); |
| 6363 |
if ( $markerEnd === false ) { |
| 6364 |
$out .= substr( $s, $markerStart ); |
| 6365 |
break; |
| 6366 |
} else { |
| 6367 |
$markerEnd += strlen( self::MARKER_SUFFIX ); |
| 6368 |
$out .= substr( $s, $markerStart, $markerEnd - $markerStart ); |
| 6369 |
$i = $markerEnd; |
| 6370 |
} |
| 6371 |
} |
| 6372 |
} |
| 6373 |
return $out; |
| 6374 |
} |
| 6375 |
|
| 6376 |
|
| 6377 |
|
| 6378 |
|
| 6379 |
|
| 6380 |
|
| 6381 |
|
| 6382 |
|
| 6383 |
public function killMarkers( $text ) { |
| 6384 |
return $this->mStripState->killMarkers( $text ); |
| 6385 |
} |
| 6386 |
|
| 6387 |
|
| 6388 |
|
| 6389 |
|
| 6390 |
|
| 6391 |
|
| 6392 |
|
| 6393 |
|
| 6394 |
|
| 6395 |
|
| 6396 |
|
| 6397 |
|
| 6398 |
|
| 6399 |
|
| 6400 |
public function parseWidthParam( $value, $parseHeight = true, bool $localized = false ) { |
| 6401 |
$parsedWidthParam = []; |
| 6402 |
if ( $value === '' ) { |
| 6403 |
return $parsedWidthParam; |
| 6404 |
} |
| 6405 |
$m = []; |
| 6406 |
if ( !$localized ) { |
| 6407 |
|
| 6408 |
$mwArray = $this->magicWordFactory->newArray( [ 'img_width' ] ); |
| 6409 |
[ $magicWord, $newValue ] = $mwArray->matchVariableStartToEnd( $value ); |
| 6410 |
$value = $magicWord ? $newValue : $value; |
| 6411 |
} |
| 6412 |
|
| 6413 |
|
| 6414 |
|
| 6415 |
if ( $parseHeight && preg_match( '/^([0-9]*)x([0-9]*)\s*(px)?\s*$/', $value, $m ) ) { |
| 6416 |
$width = intval( $m[1] ); |
| 6417 |
$height = intval( $m[2] ); |
| 6418 |
$parsedWidthParam['width'] = $width; |
| 6419 |
$parsedWidthParam['height'] = $height; |
| 6420 |
if ( $m[3] ?? false ) { |
| 6421 |
$this->addTrackingCategory( 'double-px-category' ); |
| 6422 |
} |
| 6423 |
} elseif ( preg_match( '/^([0-9]*)\s*(px)?\s*$/', $value, $m ) ) { |
| 6424 |
$width = intval( $m[1] ); |
| 6425 |
$parsedWidthParam['width'] = $width; |
| 6426 |
if ( $m[2] ?? false ) { |
| 6427 |
$this->addTrackingCategory( 'double-px-category' ); |
| 6428 |
} |
| 6429 |
} |
| 6430 |
return $parsedWidthParam; |
| 6431 |
} |
| 6432 |
|
| 6433 |
|
| 6434 |
|
| 6435 |
|
| 6436 |
|
| 6437 |
|
| 6438 |
|
| 6439 |
|
| 6440 |
|
| 6441 |
protected function lock() { |
| 6442 |
if ( $this->mInParse ) { |
| 6443 |
$message = 'Parser state cleared while parsing. Did you call Parser::parse recursively?'; |
| 6444 |
$xdebugMode = ini_get( 'xdebug.mode' ); |
| 6445 |
if ( $xdebugMode !== false && str_contains( $xdebugMode, 'develop' ) ) { |
| 6446 |
$message .= PHP_EOL . 'xdebug.mode=develop is known to cause this issue ' . |
| 6447 |
'(xdebug bug #2222); consider '; |
| 6448 |
$improvedMode = implode( ',', |
| 6449 |
array_diff( explode( ',', $xdebugMode ), |
| 6450 |
[ 'develop' ] ) ); |
| 6451 |
if ( $improvedMode !== '' ) { |
| 6452 |
$message .= "using xdebug.mode=$improvedMode instead or "; |
| 6453 |
} |
| 6454 |
$message .= "disabling xdebug."; |
| 6455 |
} |
| 6456 |
$message .= PHP_EOL . 'lock is held by: ' . $this->mInParse; |
| 6457 |
throw new LogicException( $message ); |
| 6458 |
} |
| 6459 |
|
| 6460 |
|
| 6461 |
|
| 6462 |
$e = new RuntimeException; |
| 6463 |
$this->mInParse = $e->getTraceAsString(); |
| 6464 |
|
| 6465 |
$recursiveCheck = new ScopedCallback( function () { |
| 6466 |
$this->mInParse = false; |
| 6467 |
} ); |
| 6468 |
|
| 6469 |
return $recursiveCheck; |
| 6470 |
} |
| 6471 |
|
| 6472 |
|
| 6473 |
|
| 6474 |
|
| 6475 |
|
| 6476 |
|
| 6477 |
|
| 6478 |
|
| 6479 |
public function isLocked() { |
| 6480 |
return (bool)$this->mInParse; |
| 6481 |
} |
| 6482 |
|
| 6483 |
|
| 6484 |
|
| 6485 |
|
| 6486 |
|
| 6487 |
|
| 6488 |
|
| 6489 |
|
| 6490 |
|
| 6491 |
|
| 6492 |
|
| 6493 |
public static function stripOuterParagraph( $html ) { |
| 6494 |
$m = []; |
| 6495 |
if ( preg_match( '/^<p>(.*)\n?<\/p>\n?$/sU', $html, $m ) && strpos( $m[1], '</p>' ) === false ) { |
| 6496 |
$html = $m[1]; |
| 6497 |
} |
| 6498 |
|
| 6499 |
return $html; |
| 6500 |
} |
| 6501 |
|
| 6502 |
|
| 6503 |
|
| 6504 |
|
| 6505 |
|
| 6506 |
|
| 6507 |
|
| 6508 |
|
| 6509 |
|
| 6510 |
|
| 6511 |
|
| 6512 |
public static function formatPageTitle( $nsText, $nsSeparator, $mainText ): string { |
| 6513 |
$html = ''; |
| 6514 |
if ( $nsText !== '' ) { |
| 6515 |
$html .= '<span class="mw-page-title-namespace">' . HtmlArmor::getHtml( $nsText ) . '</span>'; |
| 6516 |
$html .= '<span class="mw-page-title-separator">' . HtmlArmor::getHtml( $nsSeparator ) . '</span>'; |
| 6517 |
} |
| 6518 |
$html .= '<span class="mw-page-title-main">' . HtmlArmor::getHtml( $mainText ) . '</span>'; |
| 6519 |
return $html; |
| 6520 |
} |
| 6521 |
|
| 6522 |
|
| 6523 |
|
| 6524 |
|
| 6525 |
|
| 6526 |
|
| 6527 |
|
| 6528 |
public static function extractBody( string $text ): string { |
| 6529 |
$text = preg_replace( '!^(?>.*?<body)[^>]*+>!s', '', $text, 1 ); |
| 6530 |
if ( $text === null ) { |
| 6531 |
|
| 6532 |
throw new RuntimeException( 'Regex failed: ' . preg_last_error() ); |
| 6533 |
} |
| 6534 |
$text = preg_replace( '!</body>\s*+</html>\s*+$!', '', $text, 1 ); |
| 6535 |
return $text; |
| 6536 |
} |
| 6537 |
|
| 6538 |
|
| 6539 |
|
| 6540 |
|
| 6541 |
|
| 6542 |
|
| 6543 |
|
| 6544 |
|
| 6545 |
public function enableOOUI() { |
| 6546 |
wfDeprecated( __METHOD__, '1.35' ); |
| 6547 |
OutputPage::setupOOUI(); |
| 6548 |
$this->mOutput->setEnableOOUI( true ); |
| 6549 |
} |
| 6550 |
|
| 6551 |
|
| 6552 |
|
| 6553 |
|
| 6554 |
|
| 6555 |
|
| 6556 |
|
| 6557 |
private function setOutputFlag( string $flag, string $reason ): void { |
| 6558 |
$this->mOutput->setOutputFlag( $flag ); |
| 6559 |
$name = $this->getTitle()->getPrefixedText(); |
| 6560 |
$this->logger->debug( __METHOD__ . ": set $flag flag on '$name'; $reason" ); |
| 6561 |
} |
| 6562 |
} |
| 6563 |
|
| 6564 |
|
| 6565 |
class_alias( Parser::class, 'Parser' ); |