| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Config; |
| 5 |
|
| 6 |
use Wikimedia\Assert\Assert; |
| 7 |
use Wikimedia\Bcp47Code\Bcp47Code; |
| 8 |
use Wikimedia\Parsoid\Core\ContentMetadataCollector; |
| 9 |
use Wikimedia\Parsoid\Core\ContentModelHandler; |
| 10 |
use Wikimedia\Parsoid\Core\ResourceLimitExceededException; |
| 11 |
use Wikimedia\Parsoid\Core\Sanitizer; |
| 12 |
use Wikimedia\Parsoid\Core\TOCData; |
| 13 |
use Wikimedia\Parsoid\DOM\Document; |
| 14 |
use Wikimedia\Parsoid\DOM\DocumentFragment; |
| 15 |
use Wikimedia\Parsoid\Logger\ParsoidLogger; |
| 16 |
use Wikimedia\Parsoid\Parsoid; |
| 17 |
use Wikimedia\Parsoid\Tokens\Token; |
| 18 |
use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 19 |
use Wikimedia\Parsoid\Utils\PHPUtils; |
| 20 |
use Wikimedia\Parsoid\Utils\Title; |
| 21 |
use Wikimedia\Parsoid\Utils\TitleException; |
| 22 |
use Wikimedia\Parsoid\Utils\TokenUtils; |
| 23 |
use Wikimedia\Parsoid\Utils\UrlUtils; |
| 24 |
use Wikimedia\Parsoid\Utils\Utils; |
| 25 |
use Wikimedia\Parsoid\Wikitext\ContentModelHandler as WikitextContentModelHandler; |
| 26 |
use Wikimedia\Parsoid\Wt2Html\Frame; |
| 27 |
use Wikimedia\Parsoid\Wt2Html\PageConfigFrame; |
| 28 |
use Wikimedia\Parsoid\Wt2Html\ParserPipelineFactory; |
| 29 |
use Wikimedia\Parsoid\Wt2Html\TreeBuilder\RemexPipeline; |
| 30 |
|
| 31 |
/** |
| 32 |
* Environment/Envelope class for Parsoid |
| 33 |
* |
| 34 |
* Carries around the SiteConfig and PageConfig during an operation |
| 35 |
* and provides certain other services. |
| 36 |
*/ |
| 37 |
class Env { |
| 38 |
|
| 39 |
/** @var SiteConfig */ |
| 40 |
private $siteConfig; |
| 41 |
|
| 42 |
/** @var PageConfig */ |
| 43 |
private $pageConfig; |
| 44 |
|
| 45 |
/** @var DataAccess */ |
| 46 |
private $dataAccess; |
| 47 |
|
| 48 |
/** @var ContentMetadataCollector */ |
| 49 |
private $metadata; |
| 50 |
|
| 51 |
/** @var TOCData Table of Contents metadata for the article */ |
| 52 |
private $tocData; |
| 53 |
|
| 54 |
/** |
| 55 |
* The top-level frame for this conversion. This largely wraps the |
| 56 |
* PageConfig. |
| 57 |
* |
| 58 |
* In the future we may replace PageConfig with the Frame, and add |
| 59 |
* a |
| 60 |
* @var Frame |
| 61 |
*/ |
| 62 |
public $topFrame; |
| 63 |
// XXX In the future, perhaps replace PageConfig with the Frame, and |
| 64 |
// add $this->currentFrame (relocated from TokenTransformManager) if/when |
| 65 |
// we've removed async parsing. |
| 66 |
|
| 67 |
/** |
| 68 |
* @var bool Are we using native template expansion? |
| 69 |
* |
| 70 |
* Parsoid implements native template expansion, which is currently |
| 71 |
* only used during parser tests; in production, template expansion |
| 72 |
* is done via MediaWiki's legacy preprocessor. |
| 73 |
* |
| 74 |
* FIXME: Hopefully this distinction can be removed when we're entirely |
| 75 |
* in PHP land. |
| 76 |
*/ |
| 77 |
private $nativeTemplateExpansion; |
| 78 |
|
| 79 |
/** @var array<string,int> */ |
| 80 |
private $wt2htmlUsage = []; |
| 81 |
|
| 82 |
/** @var array<string,int> */ |
| 83 |
private $html2wtUsage = []; |
| 84 |
|
| 85 |
/** @var bool */ |
| 86 |
private $profiling = false; |
| 87 |
|
| 88 |
/** @var array<Profile> */ |
| 89 |
private $profileStack = []; |
| 90 |
|
| 91 |
/** @var bool */ |
| 92 |
private $wrapSections; |
| 93 |
|
| 94 |
/** @var string */ |
| 95 |
private $requestOffsetType = 'byte'; |
| 96 |
|
| 97 |
/** @var string */ |
| 98 |
private $currentOffsetType = 'byte'; |
| 99 |
|
| 100 |
/** @var bool */ |
| 101 |
private $skipLanguageConversionPass = false; |
| 102 |
|
| 103 |
/** @var array<string,mixed> */ |
| 104 |
private $behaviorSwitches = []; |
| 105 |
|
| 106 |
/** |
| 107 |
* Maps fragment id to the fragment forest (array of Nodes). |
| 108 |
* @var array<string,DocumentFragment> |
| 109 |
*/ |
| 110 |
private $fragmentMap = []; |
| 111 |
|
| 112 |
/** |
| 113 |
* @var int used to generate fragment ids as needed during parse |
| 114 |
*/ |
| 115 |
private $fid = 1; |
| 116 |
|
| 117 |
/** @var int used to generate uids as needed during this parse */ |
| 118 |
private $uid = 1; |
| 119 |
|
| 120 |
/** @var int used to generate annotation uids as needed during this parse */ |
| 121 |
private $annUid = 0; |
| 122 |
|
| 123 |
/** @var array[] Lints recorded */ |
| 124 |
private $lints = []; |
| 125 |
|
| 126 |
/** @var bool logLinterData */ |
| 127 |
public $logLinterData = false; |
| 128 |
|
| 129 |
/** @var array linterOverrides */ |
| 130 |
private $linterOverrides = []; |
| 131 |
|
| 132 |
/** @var bool[] */ |
| 133 |
private $traceFlags; |
| 134 |
|
| 135 |
/** @var bool[] */ |
| 136 |
private $dumpFlags; |
| 137 |
|
| 138 |
/** @var bool[] */ |
| 139 |
private $debugFlags; |
| 140 |
|
| 141 |
/** @var ParsoidLogger */ |
| 142 |
private $parsoidLogger; |
| 143 |
|
| 144 |
/** |
| 145 |
* The default content version that Parsoid assumes it's serializing or |
| 146 |
* updating in the pb2pb endpoints |
| 147 |
* |
| 148 |
* @var string |
| 149 |
*/ |
| 150 |
private $inputContentVersion; |
| 151 |
|
| 152 |
/** |
| 153 |
* The default content version that Parsoid will generate. |
| 154 |
* |
| 155 |
* @var string |
| 156 |
*/ |
| 157 |
private $outputContentVersion; |
| 158 |
|
| 159 |
/** |
| 160 |
* If non-null, the language variant used for Parsoid HTML; |
| 161 |
* we convert to this if wt2html, or from this if html2wt. |
| 162 |
* @var ?Bcp47Code |
| 163 |
*/ |
| 164 |
private $htmlVariantLanguage; |
| 165 |
|
| 166 |
/** |
| 167 |
* If non-null, the language variant to be used for wikitext. |
| 168 |
* If null, heuristics will be used to identify the original wikitext variant |
| 169 |
* in wt2html mode, and in html2wt mode new or edited HTML will be left unconverted. |
| 170 |
* @var ?Bcp47Code |
| 171 |
*/ |
| 172 |
private $wtVariantLanguage; |
| 173 |
|
| 174 |
/** @var ParserPipelineFactory */ |
| 175 |
private $pipelineFactory; |
| 176 |
|
| 177 |
/** |
| 178 |
* FIXME Used in DedupeStyles::dedupe() |
| 179 |
* @var array |
| 180 |
*/ |
| 181 |
public $styleTagKeys = []; |
| 182 |
|
| 183 |
/** @var bool */ |
| 184 |
public $pageBundle; |
| 185 |
|
| 186 |
/** @var bool */ |
| 187 |
public $discardDataParsoid = false; |
| 188 |
|
| 189 |
/** @var Document */ |
| 190 |
private $domDiff; |
| 191 |
|
| 192 |
/** @var bool */ |
| 193 |
public $hasAnnotations; |
| 194 |
|
| 195 |
/** |
| 196 |
* PORT-FIXME: public currently |
| 197 |
* Cache of wikitext source for a title |
| 198 |
* @var array |
| 199 |
*/ |
| 200 |
public $pageCache = []; |
| 201 |
|
| 202 |
/** |
| 203 |
* PORT-FIXME: public currently |
| 204 |
* HTML Cache of expanded transclusions to support |
| 205 |
* reusing expansions from HTML of previous revision. |
| 206 |
* @var array |
| 207 |
*/ |
| 208 |
public $transclusionCache = []; |
| 209 |
|
| 210 |
/** |
| 211 |
* PORT-FIXME: public currently |
| 212 |
* HTML Cache of expanded media wikiext to support |
| 213 |
* reusing expansions from HTML of previous revision. |
| 214 |
* @var array |
| 215 |
*/ |
| 216 |
public $mediaCache = []; |
| 217 |
|
| 218 |
/** |
| 219 |
* PORT-FIXME: public currently |
| 220 |
* HTML Cache of expanded extension tags to support |
| 221 |
* reusing expansions from HTML of previous revision. |
| 222 |
* @var array |
| 223 |
*/ |
| 224 |
public $extensionCache = []; |
| 225 |
|
| 226 |
/** |
| 227 |
* The current top-level document. During wt2html, this will be the document |
| 228 |
* associated with the RemexPipeline. During html2wt, this will be the |
| 229 |
* input document, typically passed as a constructor option. |
| 230 |
* |
| 231 |
* @var Document |
| 232 |
*/ |
| 233 |
public $topLevelDoc; |
| 234 |
|
| 235 |
/** |
| 236 |
* The RemexPipeline used during a wt2html operation. |
| 237 |
* |
| 238 |
* @var RemexPipeline|null |
| 239 |
*/ |
| 240 |
private $remexPipeline; |
| 241 |
|
| 242 |
/** |
| 243 |
* @var WikitextContentModelHandler |
| 244 |
*/ |
| 245 |
private $wikitextContentModelHandler; |
| 246 |
|
| 247 |
private ?Title $cachedContextTitle = null; |
| 248 |
|
| 249 |
/** |
| 250 |
* @param SiteConfig $siteConfig |
| 251 |
* @param PageConfig $pageConfig |
| 252 |
* @param DataAccess $dataAccess |
| 253 |
* @param ContentMetadataCollector $metadata |
| 254 |
* @param ?array $options |
| 255 |
* - wrapSections: (bool) Whether `<section>` wrappers should be added. |
| 256 |
* - pageBundle: (bool) Sets ids on nodes and stores data-* attributes in a JSON blob. |
| 257 |
* - traceFlags: (array) Flags indicating which components need to be traced |
| 258 |
* - dumpFlags: (bool[]) Dump flags |
| 259 |
* - debugFlags: (bool[]) Debug flags |
| 260 |
* - nativeTemplateExpansion: boolean |
| 261 |
* - discardDataParsoid: boolean |
| 262 |
* - offsetType: 'byte' (default), 'ucs2', 'char' |
| 263 |
* See `Parsoid\Wt2Html\DOM\Processors\ConvertOffsets`. |
| 264 |
* - logLinterData: (bool) Should we log linter data if linting is enabled? |
| 265 |
* - linterOverrides: (array) Override the site linting configs. |
| 266 |
* - skipLanguageConversionPass: (bool) Should we skip the language |
| 267 |
* conversion pass? (defaults to false) |
| 268 |
* - htmlVariantLanguage: Bcp47Code|null |
| 269 |
* If non-null, the language variant used for Parsoid HTML |
| 270 |
* as a BCP 47 object. |
| 271 |
* We convert to this if wt2html, or from this if html2wt. |
| 272 |
* - wtVariantLanguage: Bcp47Code|null |
| 273 |
* If non-null, the language variant to be used for wikitext |
| 274 |
* as a BCP 47 object. |
| 275 |
* If null, heuristics will be used to identify the original |
| 276 |
* wikitext variant in wt2html mode, and in html2wt mode new |
| 277 |
* or edited HTML will be left unconverted. |
| 278 |
* - logLevels: (string[]) Levels to log |
| 279 |
* - topLevelDoc: (Document) Set explicitly when serializing otherwise |
| 280 |
* it gets initialized for parsing. |
| 281 |
*/ |
| 282 |
public function __construct( |
| 283 |
SiteConfig $siteConfig, |
| 284 |
PageConfig $pageConfig, |
| 285 |
DataAccess $dataAccess, |
| 286 |
ContentMetadataCollector $metadata, |
| 287 |
?array $options = null |
| 288 |
) { |
| 289 |
self::checkPlatform(); |
| 290 |
$options ??= []; |
| 291 |
$this->siteConfig = $siteConfig; |
| 292 |
$this->pageConfig = $pageConfig; |
| 293 |
$this->dataAccess = $dataAccess; |
| 294 |
$this->metadata = $metadata; |
| 295 |
$this->tocData = new TOCData(); |
| 296 |
$this->topFrame = new PageConfigFrame( $this, $pageConfig, $siteConfig ); |
| 297 |
$this->wrapSections = (bool)( $options['wrapSections'] ?? true ); |
| 298 |
$this->pageBundle = (bool)( $options['pageBundle'] ?? false ); |
| 299 |
$this->pipelineFactory = new ParserPipelineFactory( $this ); |
| 300 |
$defaultContentVersion = Parsoid::defaultHTMLVersion(); |
| 301 |
$this->inputContentVersion = $options['inputContentVersion'] ?? $defaultContentVersion; |
| 302 |
// FIXME: We should have a check for the supported input content versions as well. |
| 303 |
// That will require a separate constant. |
| 304 |
$this->outputContentVersion = $options['outputContentVersion'] ?? $defaultContentVersion; |
| 305 |
if ( !in_array( $this->outputContentVersion, Parsoid::AVAILABLE_VERSIONS, true ) ) { |
| 306 |
throw new \UnexpectedValueException( |
| 307 |
$this->outputContentVersion . ' is not an available content version.' ); |
| 308 |
} |
| 309 |
$this->skipLanguageConversionPass = |
| 310 |
$options['skipLanguageConversionPass'] ?? false; |
| 311 |
$this->htmlVariantLanguage = !empty( $options['htmlVariantLanguage'] ) ? |
| 312 |
Utils::mwCodeToBcp47( |
| 313 |
$options['htmlVariantLanguage'], |
| 314 |
// Be strict in what we accept here. |
| 315 |
true, $this->siteConfig->getLogger() |
| 316 |
) : null; |
| 317 |
$this->wtVariantLanguage = !empty( $options['wtVariantLanguage'] ) ? |
| 318 |
Utils::mwCodeToBcp47( |
| 319 |
$options['wtVariantLanguage'], |
| 320 |
// Be strict in what we accept here. |
| 321 |
true, $this->siteConfig->getLogger() |
| 322 |
) : null; |
| 323 |
$this->nativeTemplateExpansion = !empty( $options['nativeTemplateExpansion'] ); |
| 324 |
$this->discardDataParsoid = !empty( $options['discardDataParsoid'] ); |
| 325 |
$this->requestOffsetType = $options['offsetType'] ?? 'byte'; |
| 326 |
$this->logLinterData = !empty( $options['logLinterData'] ); |
| 327 |
$this->linterOverrides = $options['linterOverrides'] ?? []; |
| 328 |
$this->traceFlags = $options['traceFlags'] ?? []; |
| 329 |
$this->dumpFlags = $options['dumpFlags'] ?? []; |
| 330 |
$this->debugFlags = $options['debugFlags'] ?? []; |
| 331 |
$this->parsoidLogger = new ParsoidLogger( $this->siteConfig->getLogger(), [ |
| 332 |
'logLevels' => $options['logLevels'] ?? [ 'fatal', 'error', 'warn', 'info' ], |
| 333 |
'debugFlags' => $this->debugFlags, |
| 334 |
'dumpFlags' => $this->dumpFlags, |
| 335 |
'traceFlags' => $this->traceFlags |
| 336 |
] ); |
| 337 |
if ( $this->hasTraceFlag( 'time' ) ) { |
| 338 |
$this->profiling = true; |
| 339 |
} |
| 340 |
$this->setupTopLevelDoc( $options['topLevelDoc'] ?? null ); |
| 341 |
// NOTE: |
| 342 |
// Don't try to do this in setupTopLevelDoc since it is called on existing Env objects |
| 343 |
// in a couple of places. That then leads to a multiple-write to tocdata property on |
| 344 |
// the metadata object. |
| 345 |
// |
| 346 |
// setupTopLevelDoc is called outside Env in these couple cases: |
| 347 |
// 1. html2wt in ContentModelHandler for dealing with |
| 348 |
// missing original HTML. |
| 349 |
// 2. ParserTestRunner's html2html tests |
| 350 |
// |
| 351 |
// That is done to either reuse an existing Env object (as in 1.) |
| 352 |
// OR to refresh the attached DOC (html2html as in 2.). |
| 353 |
// Constructing a new Env in both cases could eliminate this issue. |
| 354 |
$this->metadata->setTOCData( $this->tocData ); |
| 355 |
|
| 356 |
$this->wikitextContentModelHandler = new WikitextContentModelHandler( $this ); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Check to see if the PHP platform is sensible |
| 361 |
*/ |
| 362 |
private static function checkPlatform() { |
| 363 |
static $checked; |
| 364 |
if ( !$checked ) { |
| 365 |
$highBytes = |
| 366 |
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" . |
| 367 |
"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" . |
| 368 |
"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" . |
| 369 |
"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" . |
| 370 |
"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" . |
| 371 |
"\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" . |
| 372 |
"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef" . |
| 373 |
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"; |
| 374 |
if ( strtolower( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $highBytes ) |
| 375 |
!== 'abcdefghijklmnopqrstuvwxyz' . $highBytes |
| 376 |
) { |
| 377 |
throw new \RuntimeException( 'strtolower() doesn\'t work -- ' . |
| 378 |
'please set the locale to C or a UTF-8 variant such as C.UTF-8' ); |
| 379 |
} |
| 380 |
$checked = true; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Is profiling enabled? |
| 386 |
* @return bool |
| 387 |
*/ |
| 388 |
public function profiling(): bool { |
| 389 |
return $this->profiling; |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Get the profile at the top of the stack |
| 394 |
* |
| 395 |
* FIXME: This implicitly assumes sequential in-order processing |
| 396 |
* This wouldn't have worked in Parsoid/JS and may not work in the future |
| 397 |
* depending on how / if we restructure the pipeline for concurrency, etc. |
| 398 |
* |
| 399 |
* @return Profile |
| 400 |
*/ |
| 401 |
public function getCurrentProfile(): Profile { |
| 402 |
return PHPUtils::lastItem( $this->profileStack ); |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* New pipeline started. Push profile. |
| 407 |
* @return Profile |
| 408 |
*/ |
| 409 |
public function pushNewProfile(): Profile { |
| 410 |
$currProfile = count( $this->profileStack ) > 0 ? $this->getCurrentProfile() : null; |
| 411 |
$profile = new Profile(); |
| 412 |
$this->profileStack[] = $profile; |
| 413 |
if ( $currProfile !== null ) { |
| 414 |
$currProfile->pushNestedProfile( $profile ); |
| 415 |
} |
| 416 |
return $profile; |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Pipeline ended. Pop profile. |
| 421 |
* @return Profile |
| 422 |
*/ |
| 423 |
public function popProfile(): Profile { |
| 424 |
return array_pop( $this->profileStack ); |
| 425 |
} |
| 426 |
|
| 427 |
public function hasTraceFlags(): bool { |
| 428 |
return !empty( $this->traceFlags ); |
| 429 |
} |
| 430 |
|
| 431 |
/** |
| 432 |
* Test which trace information to log |
| 433 |
* |
| 434 |
* @param string $flag Flag name. |
| 435 |
* @return bool |
| 436 |
*/ |
| 437 |
public function hasTraceFlag( string $flag ): bool { |
| 438 |
return isset( $this->traceFlags[$flag] ); |
| 439 |
} |
| 440 |
|
| 441 |
public function hasDumpFlags(): bool { |
| 442 |
return !empty( $this->dumpFlags ); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Test which state to dump |
| 447 |
* |
| 448 |
* @param string $flag Flag name. |
| 449 |
* @return bool |
| 450 |
*/ |
| 451 |
public function hasDumpFlag( string $flag ): bool { |
| 452 |
return isset( $this->dumpFlags[$flag] ); |
| 453 |
} |
| 454 |
|
| 455 |
/** |
| 456 |
* Write out a string (because it was requested by dumpFlags) |
| 457 |
* @param string $str |
| 458 |
*/ |
| 459 |
public function writeDump( string $str ) { |
| 460 |
$this->log( 'dump', $str ); |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Get the site config |
| 465 |
* @return SiteConfig |
| 466 |
*/ |
| 467 |
public function getSiteConfig(): SiteConfig { |
| 468 |
return $this->siteConfig; |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* Get the page config |
| 473 |
* @return PageConfig |
| 474 |
*/ |
| 475 |
public function getPageConfig(): PageConfig { |
| 476 |
return $this->pageConfig; |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* Get the data access object |
| 481 |
* @return DataAccess |
| 482 |
*/ |
| 483 |
public function getDataAccess(): DataAccess { |
| 484 |
return $this->dataAccess; |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
* Return the ContentMetadataCollector. |
| 489 |
* @return ContentMetadataCollector |
| 490 |
*/ |
| 491 |
public function getMetadata(): ContentMetadataCollector { |
| 492 |
return $this->metadata; |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* Return the Table of Contents information for the article. |
| 497 |
* @return TOCData |
| 498 |
*/ |
| 499 |
public function getTOCData(): TOCData { |
| 500 |
return $this->tocData; |
| 501 |
} |
| 502 |
|
| 503 |
public function nativeTemplateExpansionEnabled(): bool { |
| 504 |
return $this->nativeTemplateExpansion; |
| 505 |
} |
| 506 |
|
| 507 |
/** |
| 508 |
* Get the current uid counter value |
| 509 |
* @return int |
| 510 |
*/ |
| 511 |
public function getUID(): int { |
| 512 |
return $this->uid; |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* Get the current fragment id counter value |
| 517 |
* @return int |
| 518 |
*/ |
| 519 |
public function getFID(): int { |
| 520 |
return $this->fid; |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* Whether `<section>` wrappers should be added. |
| 525 |
* @todo Does this actually belong here? Should it be a behavior switch? |
| 526 |
* @return bool |
| 527 |
*/ |
| 528 |
public function getWrapSections(): bool { |
| 529 |
return $this->wrapSections; |
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* Get the pipeline factory. |
| 534 |
* @return ParserPipelineFactory |
| 535 |
*/ |
| 536 |
public function getPipelineFactory(): ParserPipelineFactory { |
| 537 |
return $this->pipelineFactory; |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* Return the external format of character offsets in source ranges. |
| 542 |
* Internally we always keep DomSourceRange and SourceRange information |
| 543 |
* as UTF-8 byte offsets for efficiency (matches the native string |
| 544 |
* representation), but for external use we can convert these to |
| 545 |
* other formats when we output wt2html or input for html2wt. |
| 546 |
* |
| 547 |
* @see Parsoid\Wt2Html\DOM\Processors\ConvertOffsets |
| 548 |
* @return string 'byte', 'ucs2', or 'char' |
| 549 |
*/ |
| 550 |
public function getRequestOffsetType(): string { |
| 551 |
return $this->requestOffsetType; |
| 552 |
} |
| 553 |
|
| 554 |
/** |
| 555 |
* Return the current format of character offsets in source ranges. |
| 556 |
* This allows us to track whether the internal byte offsets have |
| 557 |
* been converted to the external format (as returned by |
| 558 |
* `getRequestOffsetType`) yet. |
| 559 |
* |
| 560 |
* @see Parsoid\Wt2Html\DOM\Processors\ConvertOffsets |
| 561 |
* @return string 'byte', 'ucs2', or 'char' |
| 562 |
*/ |
| 563 |
public function getCurrentOffsetType(): string { |
| 564 |
return $this->currentOffsetType; |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* Update the current offset type. Only |
| 569 |
* Parsoid\Wt2Html\DOM\Processors\ConvertOffsets should be doing this. |
| 570 |
* @param string $offsetType 'byte', 'ucs2', or 'char' |
| 571 |
*/ |
| 572 |
public function setCurrentOffsetType( string $offsetType ) { |
| 573 |
$this->currentOffsetType = $offsetType; |
| 574 |
} |
| 575 |
|
| 576 |
/** |
| 577 |
* Return the title from the PageConfig, as a Parsoid title. |
| 578 |
* @return Title |
| 579 |
*/ |
| 580 |
public function getContextTitle(): Title { |
| 581 |
if ( $this->cachedContextTitle === null ) { |
| 582 |
$this->cachedContextTitle = Title::newFromLinkTarget( |
| 583 |
$this->pageConfig->getLinkTarget(), $this->siteConfig |
| 584 |
); |
| 585 |
} |
| 586 |
return $this->cachedContextTitle; |
| 587 |
} |
| 588 |
|
| 589 |
/** |
| 590 |
* Resolve strings that are page-fragments or subpage references with |
| 591 |
* respect to the current page name. |
| 592 |
* |
| 593 |
* @param string $str Page fragment or subpage reference. Not URL encoded. |
| 594 |
* @param bool $resolveOnly If true, only trim and add the current title to |
| 595 |
* lone fragments. TODO: This parameter seems poorly named. |
| 596 |
* @return string Resolved title |
| 597 |
*/ |
| 598 |
public function resolveTitle( string $str, bool $resolveOnly = false ): string { |
| 599 |
$origName = $str; |
| 600 |
$str = trim( $str ); |
| 601 |
|
| 602 |
$pageConfig = $this->getPageConfig(); |
| 603 |
$title = $this->getContextTitle(); |
| 604 |
|
| 605 |
// Resolve lonely fragments (important if the current page is a subpage, |
| 606 |
// otherwise the relative link will be wrong) |
| 607 |
if ( $str !== '' && $str[0] === '#' ) { |
| 608 |
return $title->getPrefixedText() . $str; |
| 609 |
} |
| 610 |
|
| 611 |
// Default return value |
| 612 |
$titleKey = $str; |
| 613 |
if ( $this->getSiteConfig()->namespaceHasSubpages( $title->getNamespace() ) ) { |
| 614 |
// Resolve subpages |
| 615 |
$reNormalize = false; |
| 616 |
if ( preg_match( '!^(?:\.\./)+!', $str, $relUp ) ) { |
| 617 |
$levels = strlen( $relUp[0] ) / 3; // Levels are indicated by '../'. |
| 618 |
$titleBits = explode( '/', $title->getPrefixedText() ); |
| 619 |
if ( $titleBits[0] === '' ) { |
| 620 |
// FIXME: Punt on subpages of titles starting with "/" for now |
| 621 |
return $origName; |
| 622 |
} |
| 623 |
if ( count( $titleBits ) <= $levels ) { |
| 624 |
// Too many levels -- invalid relative link |
| 625 |
return $origName; |
| 626 |
} |
| 627 |
$newBits = array_slice( $titleBits, 0, -$levels ); |
| 628 |
if ( $str !== $relUp[0] ) { |
| 629 |
$newBits[] = substr( $str, $levels * 3 ); |
| 630 |
} |
| 631 |
$str = implode( '/', $newBits ); |
| 632 |
$reNormalize = true; |
| 633 |
} elseif ( $str !== '' && $str[0] === '/' ) { |
| 634 |
// Resolve absolute subpage links |
| 635 |
$str = $title->getPrefixedText() . $str; |
| 636 |
$reNormalize = true; |
| 637 |
} |
| 638 |
|
| 639 |
if ( $reNormalize && !$resolveOnly ) { |
| 640 |
// Remove final slashes if present. |
| 641 |
// See https://gerrit.wikimedia.org/r/173431 |
| 642 |
$str = rtrim( $str, '/' ); |
| 643 |
$titleKey = (string)$this->normalizedTitleKey( $str ); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
// Strip leading ':' |
| 648 |
if ( $titleKey !== '' && $titleKey[0] === ':' && !$resolveOnly ) { |
| 649 |
$titleKey = substr( $titleKey, 1 ); |
| 650 |
} |
| 651 |
return $titleKey; |
| 652 |
} |
| 653 |
|
| 654 |
/** |
| 655 |
* Convert a Title to a string |
| 656 |
* @param Title $title |
| 657 |
* @param bool $ignoreFragment |
| 658 |
* @return string |
| 659 |
*/ |
| 660 |
private function titleToString( Title $title, bool $ignoreFragment = false ): string { |
| 661 |
$ret = $title->getPrefixedDBKey(); |
| 662 |
if ( !$ignoreFragment ) { |
| 663 |
$fragment = $title->getFragment(); |
| 664 |
if ( $fragment !== '' ) { |
| 665 |
$ret .= '#' . $fragment; |
| 666 |
} |
| 667 |
} |
| 668 |
return $ret; |
| 669 |
} |
| 670 |
|
| 671 |
/** |
| 672 |
* Get normalized title key for a title string. |
| 673 |
* |
| 674 |
* @param string $str Should be in url-decoded format. |
| 675 |
* @param bool $noExceptions Return null instead of throwing exceptions. |
| 676 |
* @param bool $ignoreFragment Ignore the fragment, if any. |
| 677 |
* @return string|null Normalized title key for a title string (or null for invalid titles). |
| 678 |
*/ |
| 679 |
public function normalizedTitleKey( |
| 680 |
string $str, bool $noExceptions = false, bool $ignoreFragment = false |
| 681 |
): ?string { |
| 682 |
$title = $this->makeTitleFromURLDecodedStr( $str, 0, $noExceptions ); |
| 683 |
if ( !$title ) { |
| 684 |
return null; |
| 685 |
} |
| 686 |
return $this->titleToString( $title, $ignoreFragment ); |
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* Create a Title object |
| 691 |
* @param string $text URL-decoded text |
| 692 |
* @param ?int $defaultNs |
| 693 |
* @param bool $noExceptions |
| 694 |
* @return Title|null |
| 695 |
*/ |
| 696 |
private function makeTitle( string $text, ?int $defaultNs = null, bool $noExceptions = false ): ?Title { |
| 697 |
try { |
| 698 |
if ( preg_match( '!^(?:[#/]|\.\./)!', $text ) ) { |
| 699 |
$defaultNs = $this->getContextTitle()->getNamespace(); |
| 700 |
} |
| 701 |
$text = $this->resolveTitle( $text ); |
| 702 |
return Title::newFromText( $text, $this->getSiteConfig(), $defaultNs ); |
| 703 |
} catch ( TitleException $e ) { |
| 704 |
if ( $noExceptions ) { |
| 705 |
return null; |
| 706 |
} |
| 707 |
throw $e; |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
/** |
| 712 |
* Create a Title object |
| 713 |
* @see Title::newFromURL in MediaWiki |
| 714 |
* @param string $str URL-encoded text |
| 715 |
* @param ?int $defaultNs |
| 716 |
* @param bool $noExceptions |
| 717 |
* @return Title|null |
| 718 |
*/ |
| 719 |
public function makeTitleFromText( |
| 720 |
string $str, ?int $defaultNs = null, bool $noExceptions = false |
| 721 |
): ?Title { |
| 722 |
return $this->makeTitle( Utils::decodeURIComponent( $str ), $defaultNs, $noExceptions ); |
| 723 |
} |
| 724 |
|
| 725 |
/** |
| 726 |
* Create a Title object |
| 727 |
* @see Title::newFromText in MediaWiki |
| 728 |
* @param string $str URL-decoded text |
| 729 |
* @param ?int $defaultNs |
| 730 |
* @param bool $noExceptions |
| 731 |
* @return Title|null |
| 732 |
*/ |
| 733 |
public function makeTitleFromURLDecodedStr( |
| 734 |
string $str, ?int $defaultNs = null, bool $noExceptions = false |
| 735 |
): ?Title { |
| 736 |
return $this->makeTitle( $str, $defaultNs, $noExceptions ); |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
* Make a link to a Title |
| 741 |
* @param Title $title |
| 742 |
* @return string |
| 743 |
*/ |
| 744 |
public function makeLink( Title $title ): string { |
| 745 |
return $this->getSiteConfig()->relativeLinkPrefix() . Sanitizer::sanitizeTitleURI( |
| 746 |
$this->titleToString( $title ), |
| 747 |
false |
| 748 |
); |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Test if an href attribute value could be a valid link target |
| 753 |
* @param string|(Token|string)[] $href |
| 754 |
* @return bool |
| 755 |
*/ |
| 756 |
public function isValidLinkTarget( $href ): bool { |
| 757 |
$href = TokenUtils::tokensToString( $href ); |
| 758 |
|
| 759 |
// decode percent-encoding so that we can reliably detect |
| 760 |
// bad page title characters |
| 761 |
$hrefToken = Utils::decodeURIComponent( $href ); |
| 762 |
return $this->normalizedTitleKey( $this->resolveTitle( $hrefToken, true ), true ) !== null; |
| 763 |
} |
| 764 |
|
| 765 |
/** |
| 766 |
* Generate a new uid |
| 767 |
* @return int |
| 768 |
*/ |
| 769 |
public function generateUID(): int { |
| 770 |
return $this->uid++; |
| 771 |
} |
| 772 |
|
| 773 |
/** |
| 774 |
* Generate a new object id |
| 775 |
* @return string |
| 776 |
*/ |
| 777 |
public function newObjectId(): string { |
| 778 |
return "mwt" . $this->generateUID(); |
| 779 |
} |
| 780 |
|
| 781 |
/** |
| 782 |
* Generate a new annotation uid |
| 783 |
* @return int |
| 784 |
*/ |
| 785 |
public function generateAnnotationUID(): int { |
| 786 |
return $this->annUid++; |
| 787 |
} |
| 788 |
|
| 789 |
/** |
| 790 |
* Generate a new annotation id |
| 791 |
* @return string |
| 792 |
*/ |
| 793 |
public function newAnnotationId(): string { |
| 794 |
return "mwa" . $this->generateAnnotationUID(); |
| 795 |
} |
| 796 |
|
| 797 |
/** |
| 798 |
* Generate a new about id |
| 799 |
* @return string |
| 800 |
*/ |
| 801 |
public function newAboutId(): string { |
| 802 |
return "#" . $this->newObjectId(); |
| 803 |
} |
| 804 |
|
| 805 |
/** |
| 806 |
* Store reference to DOM diff document |
| 807 |
* @param Document $doc |
| 808 |
*/ |
| 809 |
public function setDOMDiff( $doc ): void { |
| 810 |
$this->domDiff = $doc; |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* Return reference to DOM diff document |
| 815 |
* @return Document|null |
| 816 |
*/ |
| 817 |
public function getDOMDiff(): ?Document { |
| 818 |
return $this->domDiff; |
| 819 |
} |
| 820 |
|
| 821 |
/** |
| 822 |
* Generate a new fragment id |
| 823 |
* @return string |
| 824 |
*/ |
| 825 |
public function newFragmentId(): string { |
| 826 |
return "mwf" . (string)$this->fid++; |
| 827 |
} |
| 828 |
|
| 829 |
/** |
| 830 |
* When an environment is constructed, we initialize a document (and |
| 831 |
* RemexPipeline) to be used throughout the parse. |
| 832 |
* |
| 833 |
* @param ?Document $topLevelDoc |
| 834 |
*/ |
| 835 |
public function setupTopLevelDoc( ?Document $topLevelDoc = null ) { |
| 836 |
if ( $topLevelDoc ) { |
| 837 |
$this->remexPipeline = null; |
| 838 |
$this->topLevelDoc = $topLevelDoc; |
| 839 |
} else { |
| 840 |
$this->remexPipeline = new RemexPipeline( $this ); |
| 841 |
$this->topLevelDoc = $this->remexPipeline->doc; |
| 842 |
} |
| 843 |
DOMDataUtils::prepareDoc( $this->topLevelDoc ); |
| 844 |
} |
| 845 |
|
| 846 |
public function fetchRemexPipeline( bool $toFragment ): RemexPipeline { |
| 847 |
if ( !$toFragment ) { |
| 848 |
return $this->remexPipeline; |
| 849 |
} else { |
| 850 |
$pipeline = new RemexPipeline( $this ); |
| 851 |
// Attach the top-level bag to the document, for the convenience |
| 852 |
// of code that modifies the data within the RemexHtml TreeBuilder |
| 853 |
// pipeline, prior to the migration of nodes to the top-level |
| 854 |
// document. |
| 855 |
DOMDataUtils::prepareChildDoc( $this->topLevelDoc, $pipeline->doc ); |
| 856 |
return $pipeline; |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
/** |
| 861 |
* Record a behavior switch. |
| 862 |
* |
| 863 |
* @param string $switch Switch name |
| 864 |
* @param mixed $state Relevant state data to record |
| 865 |
*/ |
| 866 |
public function setBehaviorSwitch( string $switch, $state ): void { |
| 867 |
$this->behaviorSwitches[$switch] = $state; |
| 868 |
} |
| 869 |
|
| 870 |
/** |
| 871 |
* Fetch the state of a previously-recorded behavior switch. |
| 872 |
* |
| 873 |
* @param string $switch Switch name |
| 874 |
* @param mixed $default Default value if the switch was never set |
| 875 |
* @return mixed State data that was previously passed to setBehaviorSwitch(), or $default |
| 876 |
*/ |
| 877 |
public function getBehaviorSwitch( string $switch, $default = null ) { |
| 878 |
return $this->behaviorSwitches[$switch] ?? $default; |
| 879 |
} |
| 880 |
|
| 881 |
/** |
| 882 |
* @return array<string,DocumentFragment> |
| 883 |
*/ |
| 884 |
public function getDOMFragmentMap(): array { |
| 885 |
return $this->fragmentMap; |
| 886 |
} |
| 887 |
|
| 888 |
/** |
| 889 |
* @param string $id Fragment id |
| 890 |
* @return DocumentFragment |
| 891 |
*/ |
| 892 |
public function getDOMFragment( string $id ): DocumentFragment { |
| 893 |
return $this->fragmentMap[$id]; |
| 894 |
} |
| 895 |
|
| 896 |
/** |
| 897 |
* @param string $id Fragment id |
| 898 |
* @param DocumentFragment $forest DOM forest |
| 899 |
* to store against the fragment id |
| 900 |
*/ |
| 901 |
public function setDOMFragment( |
| 902 |
string $id, DocumentFragment $forest |
| 903 |
): void { |
| 904 |
$this->fragmentMap[$id] = $forest; |
| 905 |
} |
| 906 |
|
| 907 |
public function removeDOMFragment( string $id ): void { |
| 908 |
$domFragment = $this->fragmentMap[$id]; |
| 909 |
Assert::invariant( |
| 910 |
!$domFragment->hasChildNodes(), 'Fragment should be empty.' |
| 911 |
); |
| 912 |
unset( $this->fragmentMap[$id] ); |
| 913 |
} |
| 914 |
|
| 915 |
/** |
| 916 |
* Record a lint |
| 917 |
* @param string $type Lint type key |
| 918 |
* @param array $lintData Data for the lint. |
| 919 |
* - dsr: (SourceRange) |
| 920 |
* - params: (array) |
| 921 |
* - templateInfo: (array|null) |
| 922 |
*/ |
| 923 |
public function recordLint( string $type, array $lintData ): void { |
| 924 |
if ( !$this->linting( $type ) ) { |
| 925 |
return; |
| 926 |
} |
| 927 |
|
| 928 |
if ( empty( $lintData['dsr'] ) ) { |
| 929 |
$this->log( 'error/lint', "Missing DSR; msg=", $lintData ); |
| 930 |
return; |
| 931 |
} |
| 932 |
|
| 933 |
// This will always be recorded as a native 'byte' offset |
| 934 |
$lintData['dsr'] = $lintData['dsr']->toJsonArray(); |
| 935 |
$lintData['params'] ??= []; |
| 936 |
|
| 937 |
$this->lints[] = [ 'type' => $type ] + $lintData; |
| 938 |
} |
| 939 |
|
| 940 |
/** |
| 941 |
* Retrieve recorded lints |
| 942 |
* @return array[] |
| 943 |
*/ |
| 944 |
public function getLints(): array { |
| 945 |
return $this->lints; |
| 946 |
} |
| 947 |
|
| 948 |
/** |
| 949 |
* Init lints to the passed array. |
| 950 |
* |
| 951 |
* FIXME: This is currently needed to reset lints after converting |
| 952 |
* DSR offsets because of ordering of DOM passes. So, in reality, |
| 953 |
* there should be no real use case for setting this anywhere else |
| 954 |
* but from that single callsite. |
| 955 |
* |
| 956 |
* @param array $lints |
| 957 |
*/ |
| 958 |
public function setLints( array $lints ): void { |
| 959 |
$this->lints = $lints; |
| 960 |
} |
| 961 |
|
| 962 |
/** |
| 963 |
* @param string $prefix |
| 964 |
* @param mixed ...$args |
| 965 |
*/ |
| 966 |
public function log( string $prefix, ...$args ): void { |
| 967 |
$this->parsoidLogger->log( $prefix, ...$args ); |
| 968 |
} |
| 969 |
|
| 970 |
/** |
| 971 |
* Bump usage of some limited parser resource |
| 972 |
* (ex: tokens, # transclusions, # list items, etc.) |
| 973 |
* |
| 974 |
* @param string $resource |
| 975 |
* @param int $count How much of the resource is used? |
| 976 |
* @return ?bool Returns `null` if the limit was already reached, `false` when exceeded |
| 977 |
*/ |
| 978 |
public function bumpWt2HtmlResourceUse( string $resource, int $count = 1 ): ?bool { |
| 979 |
$n = $this->wt2htmlUsage[$resource] ?? 0; |
| 980 |
if ( !$this->compareWt2HtmlLimit( $resource, $n ) ) { |
| 981 |
return null; |
| 982 |
} |
| 983 |
$n += $count; |
| 984 |
$this->wt2htmlUsage[$resource] = $n; |
| 985 |
return $this->compareWt2HtmlLimit( $resource, $n ); |
| 986 |
} |
| 987 |
|
| 988 |
/** |
| 989 |
* @param string $resource |
| 990 |
* @param int $n |
| 991 |
* @return bool Return `false` when exceeded |
| 992 |
*/ |
| 993 |
public function compareWt2HtmlLimit( string $resource, int $n ): bool { |
| 994 |
$wt2htmlLimits = $this->siteConfig->getWt2HtmlLimits(); |
| 995 |
return !( isset( $wt2htmlLimits[$resource] ) && $n > $wt2htmlLimits[$resource] ); |
| 996 |
} |
| 997 |
|
| 998 |
/** |
| 999 |
* Bump usage of some limited serializer resource |
| 1000 |
* (ex: html size) |
| 1001 |
* |
| 1002 |
* @param string $resource |
| 1003 |
* @param int $count How much of the resource is used? (defaults to 1) |
| 1004 |
* @throws ResourceLimitExceededException |
| 1005 |
*/ |
| 1006 |
public function bumpHtml2WtResourceUse( string $resource, int $count = 1 ): void { |
| 1007 |
$n = $this->html2wtUsage[$resource] ?? 0; |
| 1008 |
$n += $count; |
| 1009 |
$this->html2wtUsage[$resource] = $n; |
| 1010 |
$html2wtLimits = $this->siteConfig->getHtml2WtLimits(); |
| 1011 |
if ( |
| 1012 |
isset( $html2wtLimits[$resource] ) && |
| 1013 |
$n > $html2wtLimits[$resource] |
| 1014 |
) { |
| 1015 |
throw new ResourceLimitExceededException( "html2wt: $resource limit exceeded: $n" ); |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
/** |
| 1020 |
* Get an appropriate content handler, given a contentmodel. |
| 1021 |
* |
| 1022 |
* @param ?string &$contentmodel An optional content model which |
| 1023 |
* will override whatever the source specifies. It gets set to the |
| 1024 |
* handler which is used. |
| 1025 |
* @return ContentModelHandler An appropriate content handler |
| 1026 |
*/ |
| 1027 |
public function getContentHandler( |
| 1028 |
?string &$contentmodel = null |
| 1029 |
): ContentModelHandler { |
| 1030 |
$contentmodel ??= $this->pageConfig->getContentModel(); |
| 1031 |
$handler = $this->siteConfig->getContentModelHandler( $contentmodel ); |
| 1032 |
if ( !$handler && $contentmodel !== 'wikitext' ) { |
| 1033 |
// For now, fallback to 'wikitext' as the default handler |
| 1034 |
// FIXME: This is bogus, but this is just so suppress noise in our |
| 1035 |
// logs till we get around to handling all these other content models. |
| 1036 |
// $this->log( 'warn', "Unknown contentmodel $contentmodel" ); |
| 1037 |
} |
| 1038 |
return $handler ?? $this->wikitextContentModelHandler; |
| 1039 |
} |
| 1040 |
|
| 1041 |
/** |
| 1042 |
* Is the language converter enabled on this page? |
| 1043 |
* |
| 1044 |
* @return bool |
| 1045 |
*/ |
| 1046 |
public function langConverterEnabled(): bool { |
| 1047 |
return $this->siteConfig->langConverterEnabledBcp47( |
| 1048 |
$this->pageConfig->getPageLanguageBcp47() |
| 1049 |
); |
| 1050 |
} |
| 1051 |
|
| 1052 |
/** |
| 1053 |
* The HTML content version of the input document (for html2wt and html2html conversions). |
| 1054 |
* @see https://www.mediawiki.org/wiki/Parsoid/API#Content_Negotiation |
| 1055 |
* @see https://www.mediawiki.org/wiki/Specs/HTML#Versioning |
| 1056 |
* @return string A semver version number |
| 1057 |
*/ |
| 1058 |
public function getInputContentVersion(): string { |
| 1059 |
return $this->inputContentVersion; |
| 1060 |
} |
| 1061 |
|
| 1062 |
/** |
| 1063 |
* The HTML content version of the input document (for html2wt and html2html conversions). |
| 1064 |
* @see https://www.mediawiki.org/wiki/Parsoid/API#Content_Negotiation |
| 1065 |
* @see https://www.mediawiki.org/wiki/Specs/HTML#Versioning |
| 1066 |
* @return string A semver version number |
| 1067 |
*/ |
| 1068 |
public function getOutputContentVersion(): string { |
| 1069 |
return $this->outputContentVersion; |
| 1070 |
} |
| 1071 |
|
| 1072 |
/** |
| 1073 |
* If non-null, the language variant used for Parsoid HTML; we convert |
| 1074 |
* to this if wt2html, or from this (if html2wt). |
| 1075 |
* |
| 1076 |
* @return ?Bcp47Code a BCP-47 language code |
| 1077 |
*/ |
| 1078 |
public function getHtmlVariantLanguageBcp47(): ?Bcp47Code { |
| 1079 |
return $this->htmlVariantLanguage; // Stored as BCP-47 |
| 1080 |
} |
| 1081 |
|
| 1082 |
/** |
| 1083 |
* If non-null, the language variant to be used for wikitext. If null, |
| 1084 |
* heuristics will be used to identify the original wikitext variant |
| 1085 |
* in wt2html mode, and in html2wt mode new or edited HTML will be left |
| 1086 |
* unconverted. |
| 1087 |
* |
| 1088 |
* @return ?Bcp47Code a BCP-47 language code |
| 1089 |
*/ |
| 1090 |
public function getWtVariantLanguageBcp47(): ?Bcp47Code { |
| 1091 |
return $this->wtVariantLanguage; |
| 1092 |
} |
| 1093 |
|
| 1094 |
public function getSkipLanguageConversionPass(): bool { |
| 1095 |
return $this->skipLanguageConversionPass; |
| 1096 |
} |
| 1097 |
|
| 1098 |
/** |
| 1099 |
* Determine appropriate vary headers for the HTML form of this page. |
| 1100 |
* @return string |
| 1101 |
*/ |
| 1102 |
public function htmlVary(): string { |
| 1103 |
$varies = [ 'Accept' ]; // varies on Content-Type |
| 1104 |
if ( $this->langConverterEnabled() ) { |
| 1105 |
$varies[] = 'Accept-Language'; |
| 1106 |
} |
| 1107 |
|
| 1108 |
sort( $varies ); |
| 1109 |
return implode( ', ', $varies ); |
| 1110 |
} |
| 1111 |
|
| 1112 |
/** |
| 1113 |
* Determine an appropriate content-language for the HTML form of this page. |
| 1114 |
* @return Bcp47Code a BCP-47 language code. |
| 1115 |
*/ |
| 1116 |
public function htmlContentLanguageBcp47(): Bcp47Code { |
| 1117 |
// PageConfig::htmlVariant is set iff we do variant conversion on the |
| 1118 |
// HTML |
| 1119 |
return $this->pageConfig->getVariantBcp47() ?? |
| 1120 |
$this->pageConfig->getPageLanguageBcp47(); |
| 1121 |
} |
| 1122 |
|
| 1123 |
/** |
| 1124 |
* Get an array of attributes to apply to an anchor linking to $url |
| 1125 |
*/ |
| 1126 |
public function getExternalLinkAttribs( string $url ): array { |
| 1127 |
$siteConfig = $this->getSiteConfig(); |
| 1128 |
$noFollowConfig = $siteConfig->getNoFollowConfig(); |
| 1129 |
$attribs = []; |
| 1130 |
$ns = $this->getContextTitle()->getNamespace(); |
| 1131 |
if ( |
| 1132 |
$noFollowConfig['nofollow'] && |
| 1133 |
!in_array( $ns, $noFollowConfig['nsexceptions'], true ) && |
| 1134 |
!UrlUtils::matchesDomainList( |
| 1135 |
$url, |
| 1136 |
// Cast to an array because parserTests sets it as a string |
| 1137 |
(array)$noFollowConfig['domainexceptions'] |
| 1138 |
) |
| 1139 |
) { |
| 1140 |
$attribs['rel'] = [ 'nofollow' ]; |
| 1141 |
} |
| 1142 |
$target = $siteConfig->getExternalLinkTarget(); |
| 1143 |
if ( $target ) { |
| 1144 |
$attribs['target'] = $target; |
| 1145 |
if ( !in_array( $target, [ '_self', '_parent', '_top' ], true ) ) { |
| 1146 |
// T133507. New windows can navigate parent cross-origin. |
| 1147 |
// Including noreferrer due to lacking browser |
| 1148 |
// support of noopener. Eventually noreferrer should be removed. |
| 1149 |
if ( !isset( $attribs['rel'] ) ) { |
| 1150 |
$attribs['rel'] = []; |
| 1151 |
} |
| 1152 |
array_push( $attribs['rel'], 'noreferrer', 'noopener' ); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
return $attribs; |
| 1156 |
} |
| 1157 |
|
| 1158 |
/** |
| 1159 |
* @return array |
| 1160 |
*/ |
| 1161 |
public function getLinterConfig(): array { |
| 1162 |
return $this->linterOverrides + $this->getSiteConfig()->getLinterSiteConfig(); |
| 1163 |
} |
| 1164 |
|
| 1165 |
/** |
| 1166 |
* Whether to enable linter Backend. |
| 1167 |
* Consults the allow list and block list from ::getLinterConfig(). |
| 1168 |
* |
| 1169 |
* @param null $type If $type is null or omitted, returns true if *any* linting |
| 1170 |
* type is enabled; otherwise returns true only if the specified |
| 1171 |
* linting type is enabled. |
| 1172 |
* @return bool If $type is null or omitted, returns true if *any* linting |
| 1173 |
* type is enabled; otherwise returns true only if the specified |
| 1174 |
* linting type is enabled. |
| 1175 |
*/ |
| 1176 |
public function linting( ?string $type = null ) { |
| 1177 |
if ( !$this->getSiteConfig()->linterEnabled() ) { |
| 1178 |
return false; |
| 1179 |
} |
| 1180 |
$lintConfig = $this->getLinterConfig(); |
| 1181 |
// Allow list |
| 1182 |
$allowList = $lintConfig['enabled'] ?? null; |
| 1183 |
if ( is_array( $allowList ) ) { |
| 1184 |
if ( $type === null ) { |
| 1185 |
return count( $allowList ) > 0; |
| 1186 |
} |
| 1187 |
return in_array( $type, $allowList, true ); |
| 1188 |
} |
| 1189 |
// Block list |
| 1190 |
if ( $type === null ) { |
| 1191 |
return true; |
| 1192 |
} |
| 1193 |
$blockList = $lintConfig['disabled'] ?? null; |
| 1194 |
if ( is_array( $blockList ) ) { |
| 1195 |
return !in_array( $type, $blockList, true ); |
| 1196 |
} |
| 1197 |
// No specific configuration |
| 1198 |
return true; |
| 1199 |
} |
| 1200 |
} |