Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
36.25% |
91 / 251 |
|
8.47% |
5 / 59 |
CRAP | |
0.00% |
0 / 1 |
| Env | |
36.25% |
91 / 251 |
|
8.47% |
5 / 59 |
3361.19 | |
0.00% |
0 / 1 |
| __construct | |
84.62% |
33 / 39 |
|
0.00% |
0 / 1 |
4.06 | |||
| profiling | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCurrentProfile | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| pushNewProfile | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| popProfile | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasTraceFlags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasTraceFlag | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasDumpFlags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| hasDumpFlag | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| writeDump | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSiteConfig | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPageConfig | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDataAccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMetadata | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getTOCData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| nativeTemplateExpansionEnabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getWrapSections | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPipelineFactory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCache | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getRequestOffsetType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCurrentOffsetType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setCurrentOffsetType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getContextTitle | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| resolveTitle | |
93.75% |
30 / 32 |
|
0.00% |
0 / 1 |
15.05 | |||
| normalizedTitleKey | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
3.33 | |||
| makeTitle | |
37.50% |
3 / 8 |
|
0.00% |
0 / 1 |
7.91 | |||
| makeTitleFromText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| makeTitleFromURLDecodedStr | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| makeLink | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| isValidLinkTarget | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| newAnnotationId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| newAboutId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setDOMDiff | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDOMDiff | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setupTopLevelDoc | |
66.67% |
12 / 18 |
|
0.00% |
0 / 1 |
4.59 | |||
| getTopLevelDoc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fetchRemexPipeline | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| setBehaviorSwitch | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getBehaviorSwitch | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPFragment | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addToPFragmentMap | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| pFragmentMapToString | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| recordLint | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| getLints | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setLints | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| log | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| trace | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
| bumpWt2HtmlResourceUse | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| compareWt2HtmlLimit | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| bumpHtml2WtResourceUse | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| getContentHandler | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| langConverterEnabled | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getInputContentVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOutputContentVersion | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| htmlVary | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| htmlContentLanguageBcp47 | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getExternalLinkAttribs | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
42 | |||
| getLinterConfig | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| linting | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\Parsoid\Config; |
| 5 | |
| 6 | use Closure; |
| 7 | use Wikimedia\Assert\Assert; |
| 8 | use Wikimedia\Bcp47Code\Bcp47Code; |
| 9 | use Wikimedia\Parsoid\Core\ContentMetadataCollector; |
| 10 | use Wikimedia\Parsoid\Core\ContentModelHandler; |
| 11 | use Wikimedia\Parsoid\Core\DOMCompat; |
| 12 | use Wikimedia\Parsoid\Core\DomPageBundle; |
| 13 | use Wikimedia\Parsoid\Core\ResourceLimitExceededException; |
| 14 | use Wikimedia\Parsoid\Core\Sanitizer; |
| 15 | use Wikimedia\Parsoid\Core\TOCData; |
| 16 | use Wikimedia\Parsoid\DOM\Document; |
| 17 | use Wikimedia\Parsoid\Fragments\PFragment; |
| 18 | use Wikimedia\Parsoid\Logger\ParsoidLogger; |
| 19 | use Wikimedia\Parsoid\Parsoid; |
| 20 | use Wikimedia\Parsoid\Tokens\Token; |
| 21 | use Wikimedia\Parsoid\Utils\DOMDataUtils; |
| 22 | use Wikimedia\Parsoid\Utils\PHPUtils; |
| 23 | use Wikimedia\Parsoid\Utils\Title; |
| 24 | use Wikimedia\Parsoid\Utils\TitleException; |
| 25 | use Wikimedia\Parsoid\Utils\TokenUtils; |
| 26 | use Wikimedia\Parsoid\Utils\Utils; |
| 27 | use Wikimedia\Parsoid\Wikitext\ContentModelHandler as WikitextContentModelHandler; |
| 28 | use Wikimedia\Parsoid\Wt2Html\Frame; |
| 29 | use Wikimedia\Parsoid\Wt2Html\PageConfigFrame; |
| 30 | use Wikimedia\Parsoid\Wt2Html\ParserPipelineFactory; |
| 31 | use Wikimedia\Parsoid\Wt2Html\PipelineContentCache; |
| 32 | use Wikimedia\Parsoid\Wt2Html\TreeBuilder\RemexPipeline; |
| 33 | |
| 34 | /** |
| 35 | * Environment/Envelope class for Parsoid |
| 36 | * |
| 37 | * Carries around the SiteConfig and PageConfig during an operation |
| 38 | * and provides certain other services. |
| 39 | */ |
| 40 | class Env { |
| 41 | private SiteConfig $siteConfig; |
| 42 | private PageConfig $pageConfig; |
| 43 | private DataAccess $dataAccess; |
| 44 | private ContentMetadataCollector $metadata; |
| 45 | |
| 46 | /** Table of Contents metadata for the article */ |
| 47 | private TOCData $tocData; |
| 48 | |
| 49 | /** |
| 50 | * The top-level frame for this conversion. |
| 51 | * This largely wraps the PageConfig. |
| 52 | * In the future we may replace PageConfig with the Frame |
| 53 | */ |
| 54 | public Frame $topFrame; |
| 55 | // XXX In the future, perhaps replace PageConfig with the Frame, and |
| 56 | // add $this->currentFrame (relocated from TokenHandlerPipeline) if/when |
| 57 | // we've removed async parsing. |
| 58 | |
| 59 | /** |
| 60 | * Are we using native template expansion? |
| 61 | * |
| 62 | * Parsoid implements native template expansion, which is currently |
| 63 | * only used during parser tests; in production, template expansion |
| 64 | * is done via MediaWiki's legacy preprocessor. |
| 65 | * |
| 66 | * FIXME: Hopefully this distinction can be removed when we're entirely |
| 67 | * in PHP land. |
| 68 | */ |
| 69 | private bool $nativeTemplateExpansion; |
| 70 | |
| 71 | /** @var array<string,int> */ |
| 72 | private array $wt2htmlUsage = []; |
| 73 | |
| 74 | /** @var array<string,int> */ |
| 75 | private array $html2wtUsage = []; |
| 76 | private bool $profiling = false; |
| 77 | |
| 78 | /** @var array<Profile> */ |
| 79 | private array $profileStack = []; |
| 80 | private bool $wrapSections; |
| 81 | |
| 82 | /** @var ('byte'|'ucs2'|'char') */ |
| 83 | private string $requestOffsetType = 'byte'; |
| 84 | |
| 85 | /** @var ('byte'|'ucs2'|'char') */ |
| 86 | private string $currentOffsetType = 'byte'; |
| 87 | |
| 88 | /** @var array<string,mixed> */ |
| 89 | private array $behaviorSwitches = []; |
| 90 | |
| 91 | /** |
| 92 | * Maps pfragment id to a PFragment. |
| 93 | * @var array<string,PFragment> |
| 94 | */ |
| 95 | private array $pFragmentMap = []; |
| 96 | |
| 97 | /** Lints recorded */ |
| 98 | private array $lints = []; |
| 99 | public bool $logLinterData = false; |
| 100 | private array $linterOverrides = []; |
| 101 | |
| 102 | /** @var bool[] */ |
| 103 | private array $traceFlags; |
| 104 | |
| 105 | /** @var bool[] */ |
| 106 | private array $dumpFlags; |
| 107 | |
| 108 | /** @var bool[] */ |
| 109 | private array $debugFlags; |
| 110 | private ParsoidLogger $parsoidLogger; |
| 111 | |
| 112 | /** |
| 113 | * The default content version that Parsoid assumes it's serializing or |
| 114 | * updating in the pb2pb endpoints |
| 115 | */ |
| 116 | private string $inputContentVersion; |
| 117 | |
| 118 | /** |
| 119 | * The default content version that Parsoid will generate. |
| 120 | */ |
| 121 | private string $outputContentVersion; |
| 122 | |
| 123 | private ParserPipelineFactory $pipelineFactory; |
| 124 | |
| 125 | /** |
| 126 | * FIXME Used in DedupeStyles::dedupe() |
| 127 | */ |
| 128 | public array $styleTagKeys = []; |
| 129 | |
| 130 | /** |
| 131 | * The DomPageBundle holding the JSON data for data-parsoid and data-mw |
| 132 | * attributes, or `null` if these are to be encoded as inline HTML |
| 133 | * attributes. |
| 134 | */ |
| 135 | public ?DomPageBundle $pageBundle = null; |
| 136 | private ?Document $domDiff = null; |
| 137 | public bool $hasAnnotations = false; |
| 138 | |
| 139 | /** |
| 140 | * Cache of wikitext source for a title; only used for ParserTests. |
| 141 | * @var array<string,string> |
| 142 | */ |
| 143 | public array $pageCache = []; |
| 144 | |
| 145 | /** |
| 146 | * Token caches used in the pipeline |
| 147 | * @var array<PipelineContentCache> |
| 148 | */ |
| 149 | private array $pipelineContentCaches = []; |
| 150 | |
| 151 | /** |
| 152 | * The current top-level document. During wt2html, this will be the document |
| 153 | * associated with the RemexPipeline. During html2wt, this will be the |
| 154 | * input document, typically passed as a constructor option. |
| 155 | * |
| 156 | * This document should be prepared and loaded; see |
| 157 | * ContentUtils::createAndLoadDocument(). |
| 158 | */ |
| 159 | private Document $topLevelDoc; |
| 160 | |
| 161 | /** |
| 162 | * The RemexPipeline used during a wt2html operation. |
| 163 | */ |
| 164 | private ?RemexPipeline $remexPipeline; |
| 165 | private WikitextContentModelHandler $wikitextContentModelHandler; |
| 166 | private ?Title $cachedContextTitle = null; |
| 167 | |
| 168 | /** |
| 169 | * @param SiteConfig $siteConfig |
| 170 | * @param PageConfig $pageConfig |
| 171 | * @param DataAccess $dataAccess |
| 172 | * @param ContentMetadataCollector $metadata |
| 173 | * @param ?array $options |
| 174 | * - wrapSections: (bool) Whether `<section>` wrappers should be added. |
| 175 | * - pageBundle: (bool) When true, sets ids on nodes and stores |
| 176 | * data-* attributes in a JSON blob in Env::$pageBundle |
| 177 | * - traceFlags: (array) Flags indicating which components need to be traced |
| 178 | * - dumpFlags: (bool[]) Dump flags |
| 179 | * - debugFlags: (bool[]) Debug flags |
| 180 | * - nativeTemplateExpansion: boolean |
| 181 | * - offsetType: 'byte' (default), 'ucs2', 'char' |
| 182 | * See `Parsoid\Wt2Html\DOM\Processors\ConvertOffsets`. |
| 183 | * - logLinterData: (bool) Should we log linter data if linting is enabled? |
| 184 | * - linterOverrides: (array) Override the site linting configs. |
| 185 | * - logLevels: (string[]) Levels to log |
| 186 | * - topLevelDoc: Document Set explicitly |
| 187 | * when serializing otherwise it gets initialized for parsing. |
| 188 | * This should be a "prepared & loaded" document. |
| 189 | */ |
| 190 | public function __construct( |
| 191 | SiteConfig $siteConfig, |
| 192 | PageConfig $pageConfig, |
| 193 | DataAccess $dataAccess, |
| 194 | ContentMetadataCollector $metadata, |
| 195 | ?array $options = null |
| 196 | ) { |
| 197 | $options ??= []; |
| 198 | $this->siteConfig = $siteConfig; |
| 199 | $this->pageConfig = $pageConfig; |
| 200 | $this->dataAccess = $dataAccess; |
| 201 | $this->metadata = $metadata; |
| 202 | $this->tocData = new TOCData(); |
| 203 | $this->topFrame = new PageConfigFrame( $this, $pageConfig, $siteConfig ); |
| 204 | $this->wrapSections = (bool)( $options['wrapSections'] ?? true ); |
| 205 | $this->pipelineFactory = new ParserPipelineFactory( $this ); |
| 206 | $defaultContentVersion = Parsoid::defaultHTMLVersion(); |
| 207 | $this->inputContentVersion = $options['inputContentVersion'] ?? $defaultContentVersion; |
| 208 | // FIXME: We should have a check for the supported input content versions as well. |
| 209 | // That will require a separate constant. |
| 210 | $this->outputContentVersion = $options['outputContentVersion'] ?? $defaultContentVersion; |
| 211 | if ( !in_array( $this->outputContentVersion, Parsoid::AVAILABLE_VERSIONS, true ) ) { |
| 212 | throw new \UnexpectedValueException( |
| 213 | $this->outputContentVersion . ' is not an available content version.' ); |
| 214 | } |
| 215 | $this->nativeTemplateExpansion = !empty( $options['nativeTemplateExpansion'] ); |
| 216 | $this->requestOffsetType = $options['offsetType'] ?? 'byte'; |
| 217 | $this->logLinterData = !empty( $options['logLinterData'] ); |
| 218 | $this->linterOverrides = $options['linterOverrides'] ?? []; |
| 219 | $this->setupTopLevelDoc( $options['topLevelDoc'] ?? null ); |
| 220 | if ( $options['pageBundle'] ?? false ) { |
| 221 | $this->pageBundle = DomPageBundle::newEmpty( |
| 222 | $this->topLevelDoc |
| 223 | ); |
| 224 | } |
| 225 | // NOTE: |
| 226 | // Don't try to do this in setupTopLevelDoc since it is called on existing Env objects |
| 227 | // in a couple of places. That then leads to a multiple-write to tocdata property on |
| 228 | // the metadata object. |
| 229 | // |
| 230 | // setupTopLevelDoc is called outside Env in these couple cases: |
| 231 | // 1. html2wt in ContentModelHandler for dealing with |
| 232 | // missing original HTML. |
| 233 | // 2. ParserTestRunner's html2html tests |
| 234 | // |
| 235 | // That is done to either reuse an existing Env object (as in 1.) |
| 236 | // OR to refresh the attached DOC (html2html as in 2.). |
| 237 | // Constructing a new Env in both cases could eliminate this issue. |
| 238 | $this->metadata->setTOCData( $this->tocData ); |
| 239 | |
| 240 | $this->traceFlags = $options['traceFlags'] ?? []; |
| 241 | $this->dumpFlags = $options['dumpFlags'] ?? []; |
| 242 | $this->debugFlags = $options['debugFlags'] ?? []; |
| 243 | $this->parsoidLogger = new ParsoidLogger( $this->siteConfig->getLogger(), [ |
| 244 | 'logLevels' => $options['logLevels'] ?? [ 'fatal', 'error', 'warn', 'info' ], |
| 245 | 'debugFlags' => $this->debugFlags, |
| 246 | 'dumpFlags' => $this->dumpFlags, |
| 247 | 'traceFlags' => $this->traceFlags, |
| 248 | 'ownerDoc' => $this->getTopLevelDoc(), |
| 249 | 'siteConfig' => $this->siteConfig, |
| 250 | ] ); |
| 251 | if ( $this->hasTraceFlag( 'time' ) ) { |
| 252 | $this->profiling = true; |
| 253 | } |
| 254 | |
| 255 | $this->wikitextContentModelHandler = new WikitextContentModelHandler( $this ); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Is profiling enabled? |
| 260 | * @return bool |
| 261 | */ |
| 262 | public function profiling(): bool { |
| 263 | return $this->profiling; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Get the profile at the top of the stack |
| 268 | * |
| 269 | * FIXME: This implicitly assumes sequential in-order processing |
| 270 | * This wouldn't have worked in Parsoid/JS and may not work in the future |
| 271 | * depending on how / if we restructure the pipeline for concurrency, etc. |
| 272 | * |
| 273 | * @return Profile |
| 274 | */ |
| 275 | public function getCurrentProfile(): Profile { |
| 276 | return PHPUtils::lastItem( $this->profileStack ); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * New pipeline started. Push profile. |
| 281 | * @return Profile |
| 282 | */ |
| 283 | public function pushNewProfile(): Profile { |
| 284 | $currProfile = count( $this->profileStack ) > 0 ? $this->getCurrentProfile() : null; |
| 285 | $profile = new Profile( $this, isset( $this->debugFlags['oom'] ) ); |
| 286 | $this->profileStack[] = $profile; |
| 287 | if ( $currProfile !== null ) { |
| 288 | $currProfile->pushNestedProfile( $profile ); |
| 289 | } |
| 290 | return $profile; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Pipeline ended. Pop profile. |
| 295 | * @return Profile |
| 296 | */ |
| 297 | public function popProfile(): Profile { |
| 298 | return array_pop( $this->profileStack ); |
| 299 | } |
| 300 | |
| 301 | public function hasTraceFlags(): bool { |
| 302 | return !empty( $this->traceFlags ); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Test which trace information to log |
| 307 | * |
| 308 | * @param string $flag Flag name. |
| 309 | * @return bool |
| 310 | */ |
| 311 | public function hasTraceFlag( string $flag ): bool { |
| 312 | return isset( $this->traceFlags[$flag] ); |
| 313 | } |
| 314 | |
| 315 | public function hasDumpFlags(): bool { |
| 316 | return !empty( $this->dumpFlags ); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Test which state to dump |
| 321 | * |
| 322 | * @param string $flag Flag name. |
| 323 | * @return bool |
| 324 | */ |
| 325 | public function hasDumpFlag( string $flag ): bool { |
| 326 | return isset( $this->dumpFlags[$flag] ); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Write out a string (because it was requested by dumpFlags) |
| 331 | * |
| 332 | * @param string $str |
| 333 | */ |
| 334 | public function writeDump( string $str ): void { |
| 335 | $this->log( 'dump', $str ); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Get the site config |
| 340 | * @return SiteConfig |
| 341 | */ |
| 342 | public function getSiteConfig(): SiteConfig { |
| 343 | return $this->siteConfig; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Get the page config |
| 348 | * @return PageConfig |
| 349 | */ |
| 350 | public function getPageConfig(): PageConfig { |
| 351 | return $this->pageConfig; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Get the data access object |
| 356 | * @return DataAccess |
| 357 | */ |
| 358 | public function getDataAccess(): DataAccess { |
| 359 | return $this->dataAccess; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Return the ContentMetadataCollector. |
| 364 | * @return ContentMetadataCollector |
| 365 | */ |
| 366 | public function getMetadata(): ContentMetadataCollector { |
| 367 | return $this->metadata; |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Return the Table of Contents information for the article. |
| 372 | * @return TOCData |
| 373 | */ |
| 374 | public function getTOCData(): TOCData { |
| 375 | return $this->tocData; |
| 376 | } |
| 377 | |
| 378 | public function nativeTemplateExpansionEnabled(): bool { |
| 379 | return $this->nativeTemplateExpansion; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Whether `<section>` wrappers should be added. |
| 384 | * @todo Does this actually belong here? Should it be a behavior switch? |
| 385 | * @return bool |
| 386 | */ |
| 387 | public function getWrapSections(): bool { |
| 388 | return $this->wrapSections; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Get the pipeline factory. |
| 393 | * @return ParserPipelineFactory |
| 394 | */ |
| 395 | public function getPipelineFactory(): ParserPipelineFactory { |
| 396 | return $this->pipelineFactory; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Get a token cache for a given cache name. A cache is shared across all pipelines |
| 401 | * and processing that happens in the lifetime of this Env object. |
| 402 | * @param string $cacheName Key to retrieve a token cache |
| 403 | * @param array{repeatThreshold:int,cloneValue:bool|Closure} $newCacheOpts Opts for the new cache |
| 404 | */ |
| 405 | public function getCache( string $cacheName, array $newCacheOpts ): PipelineContentCache { |
| 406 | if ( !isset( $this->pipelineContentCaches[$cacheName] ) ) { |
| 407 | $this->pipelineContentCaches[$cacheName] = new PipelineContentCache( |
| 408 | $newCacheOpts['repeatThreshold'], |
| 409 | $newCacheOpts['cloneValue'] |
| 410 | ); |
| 411 | } |
| 412 | |
| 413 | return $this->pipelineContentCaches[$cacheName]; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Return the external format of character offsets in source ranges. |
| 418 | * Internally we always keep DomSourceRange and SourceRange information |
| 419 | * as UTF-8 byte offsets for efficiency (matches the native string |
| 420 | * representation), but for external use we can convert these to |
| 421 | * other formats when we output wt2html or input for html2wt. |
| 422 | * |
| 423 | * @see Parsoid\Wt2Html\DOM\Processors\ConvertOffsets |
| 424 | * @return ('byte'|'ucs2'|'char') |
| 425 | */ |
| 426 | public function getRequestOffsetType(): string { |
| 427 | return $this->requestOffsetType; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Return the current format of character offsets in source ranges. |
| 432 | * This allows us to track whether the internal byte offsets have |
| 433 | * been converted to the external format (as returned by |
| 434 | * `getRequestOffsetType`) yet. |
| 435 | * |
| 436 | * @see Parsoid\Wt2Html\DOM\Processors\ConvertOffsets |
| 437 | * @return ('byte'|'ucs2'|'char') |
| 438 | */ |
| 439 | public function getCurrentOffsetType(): string { |
| 440 | return $this->currentOffsetType; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Update the current offset type. Only |
| 445 | * Parsoid\Wt2Html\DOM\Processors\ConvertOffsets should be doing this. |
| 446 | * |
| 447 | * @param ('byte'|'ucs2'|'char') $offsetType 'byte', 'ucs2', or 'char' |
| 448 | */ |
| 449 | public function setCurrentOffsetType( string $offsetType ): void { |
| 450 | $this->currentOffsetType = $offsetType; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Return the title from the PageConfig, as a Parsoid title. |
| 455 | * @return Title |
| 456 | */ |
| 457 | public function getContextTitle(): Title { |
| 458 | if ( $this->cachedContextTitle === null ) { |
| 459 | $this->cachedContextTitle = Title::newFromLinkTarget( |
| 460 | $this->pageConfig->getLinkTarget(), $this->siteConfig |
| 461 | ); |
| 462 | } |
| 463 | return $this->cachedContextTitle; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Resolve strings that are page-fragments or subpage references with |
| 468 | * respect to the current page name. |
| 469 | * |
| 470 | * @param string $str Page fragment or subpage reference. Not URL encoded. |
| 471 | * @param bool $resolveOnly If true, only trim and add the current title to |
| 472 | * lone fragments. TODO: This parameter seems poorly named. |
| 473 | * @return string Resolved title |
| 474 | */ |
| 475 | public function resolveTitle( string $str, bool $resolveOnly = false ): string { |
| 476 | $origName = $str; |
| 477 | $str = trim( $str ); |
| 478 | |
| 479 | $title = $this->getContextTitle(); |
| 480 | |
| 481 | // Resolve lonely fragments (important if the current page is a subpage, |
| 482 | // otherwise the relative link will be wrong) |
| 483 | if ( $str !== '' && $str[0] === '#' ) { |
| 484 | return $title->getPrefixedText() . $str; |
| 485 | } |
| 486 | |
| 487 | // Default return value |
| 488 | $titleKey = $str; |
| 489 | if ( $this->getSiteConfig()->namespaceHasSubpages( $title->getNamespace() ) ) { |
| 490 | // Resolve subpages |
| 491 | $reNormalize = false; |
| 492 | if ( preg_match( '!^(?:\.\./)+!', $str, $relUp ) ) { |
| 493 | $levels = strlen( $relUp[0] ) / 3; // Levels are indicated by '../'. |
| 494 | $titleBits = explode( '/', $title->getPrefixedText() ); |
| 495 | if ( $titleBits[0] === '' ) { |
| 496 | // FIXME: Punt on subpages of titles starting with "/" for now |
| 497 | return $origName; |
| 498 | } |
| 499 | if ( count( $titleBits ) <= $levels ) { |
| 500 | // Too many levels -- invalid relative link |
| 501 | return $origName; |
| 502 | } |
| 503 | $newBits = array_slice( $titleBits, 0, -$levels ); |
| 504 | if ( $str !== $relUp[0] ) { |
| 505 | $lastBit = substr( $str, $levels * 3 ); |
| 506 | if ( str_starts_with( $lastBit, "#" ) ) { |
| 507 | // Fragments should not get appended after a trailing "/" |
| 508 | $newBits[count( $newBits ) - 1] .= $lastBit; |
| 509 | } else { |
| 510 | $newBits[] = $lastBit; |
| 511 | } |
| 512 | } |
| 513 | $str = implode( '/', $newBits ); |
| 514 | $reNormalize = true; |
| 515 | } elseif ( $str !== '' && $str[0] === '/' ) { |
| 516 | // Resolve absolute subpage links |
| 517 | $str = $title->getPrefixedText() . $str; |
| 518 | $reNormalize = true; |
| 519 | } |
| 520 | |
| 521 | if ( $reNormalize && !$resolveOnly ) { |
| 522 | // Remove final slashes if present. |
| 523 | // See https://gerrit.wikimedia.org/r/173431 |
| 524 | $str = rtrim( $str, '/' ); |
| 525 | $titleKey = (string)$this->normalizedTitleKey( $str ); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | // Strip leading ':' |
| 530 | if ( !$resolveOnly && str_starts_with( $titleKey, ':' ) ) { |
| 531 | $titleKey = substr( $titleKey, 1 ); |
| 532 | } |
| 533 | return $titleKey; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Get normalized title key for a title string. |
| 538 | * |
| 539 | * @param string $str Should be in url-decoded format. |
| 540 | * @param bool $noExceptions Return null instead of throwing exceptions. |
| 541 | * @param bool $ignoreFragment Ignore the fragment, if any. |
| 542 | * @return string|null Normalized title key for a title string (or null for invalid titles). |
| 543 | */ |
| 544 | public function normalizedTitleKey( |
| 545 | string $str, bool $noExceptions = false, bool $ignoreFragment = false |
| 546 | ): ?string { |
| 547 | $title = $this->makeTitleFromURLDecodedStr( $str, 0, $noExceptions ); |
| 548 | if ( !$title ) { |
| 549 | return null; |
| 550 | } |
| 551 | return $ignoreFragment ? |
| 552 | $title->getPrefixedDBKey() : |
| 553 | $title->getFullDBKey(); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Create a Title object |
| 558 | * @param string $text URL-decoded text |
| 559 | * @param ?int $defaultNs |
| 560 | * @param bool $noExceptions |
| 561 | * @return Title|null |
| 562 | */ |
| 563 | private function makeTitle( string $text, ?int $defaultNs = null, bool $noExceptions = false ): ?Title { |
| 564 | try { |
| 565 | if ( preg_match( '!^(?:[#/]|\.\./)!', $text ) ) { |
| 566 | $defaultNs = $this->getContextTitle()->getNamespace(); |
| 567 | } |
| 568 | $text = $this->resolveTitle( $text ); |
| 569 | return Title::newFromText( $text, $this->getSiteConfig(), $defaultNs ); |
| 570 | } catch ( TitleException $e ) { |
| 571 | if ( $noExceptions ) { |
| 572 | return null; |
| 573 | } |
| 574 | throw $e; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Create a Title object |
| 580 | * @see Title::newFromURL in MediaWiki |
| 581 | * @param string $str URL-encoded text |
| 582 | * @param ?int $defaultNs |
| 583 | * @param bool $noExceptions |
| 584 | * @return Title|null |
| 585 | */ |
| 586 | public function makeTitleFromText( |
| 587 | string $str, ?int $defaultNs = null, bool $noExceptions = false |
| 588 | ): ?Title { |
| 589 | return $this->makeTitle( Utils::decodeURIComponent( $str ), $defaultNs, $noExceptions ); |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Create a Title object |
| 594 | * @see Title::newFromText in MediaWiki |
| 595 | * @param string $str URL-decoded text |
| 596 | * @param ?int $defaultNs |
| 597 | * @param bool $noExceptions |
| 598 | * @return Title|null |
| 599 | */ |
| 600 | public function makeTitleFromURLDecodedStr( |
| 601 | string $str, ?int $defaultNs = null, bool $noExceptions = false |
| 602 | ): ?Title { |
| 603 | return $this->makeTitle( $str, $defaultNs, $noExceptions ); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Make a link to a local Title |
| 608 | * @param Title $title |
| 609 | * @return string |
| 610 | */ |
| 611 | public function makeLink( Title $title ): string { |
| 612 | // T380676: This method *should* be used only for local titles, |
| 613 | // (ie $title->getInterwiki() should be '') but apparently we |
| 614 | // are using it for interwiki/interlanguage links as well. |
| 615 | return $this->getSiteConfig()->relativeLinkPrefix() . Sanitizer::sanitizeTitleURI( |
| 616 | $title->getFullDBKey(), |
| 617 | false |
| 618 | ); |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Test if an href attribute value could be a valid link target |
| 623 | * @param string|(Token|string)[] $href |
| 624 | * @return bool |
| 625 | */ |
| 626 | public function isValidLinkTarget( $href ): bool { |
| 627 | $href = TokenUtils::tokensToString( $href ); |
| 628 | |
| 629 | // decode percent-encoding so that we can reliably detect |
| 630 | // bad page title characters |
| 631 | $hrefToken = Utils::decodeURIComponent( $href ); |
| 632 | return $this->normalizedTitleKey( $this->resolveTitle( $hrefToken, true ), true ) !== null; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Generate a new annotation id |
| 637 | * @return string |
| 638 | */ |
| 639 | public function newAnnotationId(): string { |
| 640 | return DOMDataUtils::getBag( $this->topLevelDoc )->newAnnotationId(); |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Generate a new about id |
| 645 | * @return string |
| 646 | */ |
| 647 | public function newAboutId(): string { |
| 648 | return DOMDataUtils::getBag( $this->topLevelDoc )->newAboutId(); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Store reference to DOM diff document |
| 653 | * @param Document $doc |
| 654 | */ |
| 655 | public function setDOMDiff( $doc ): void { |
| 656 | $this->domDiff = $doc; |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Return reference to DOM diff document |
| 661 | * @return Document|null |
| 662 | */ |
| 663 | public function getDOMDiff(): ?Document { |
| 664 | return $this->domDiff; |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * When an environment is constructed, we initialize a document (and |
| 669 | * RemexPipeline) to be used throughout the parse. |
| 670 | * |
| 671 | * @param ?Document $topLevelDoc if non-null, the document should be prepared and loaded. |
| 672 | */ |
| 673 | public function setupTopLevelDoc( ?Document $topLevelDoc = null ): void { |
| 674 | if ( $topLevelDoc ) { |
| 675 | $this->remexPipeline = null; |
| 676 | // This is a prepared & loaded Document. |
| 677 | Assert::invariant( |
| 678 | DOMDataUtils::isPreparedAndLoaded( $topLevelDoc ), |
| 679 | "toplevelDoc should be prepared and loaded already" |
| 680 | ); |
| 681 | $this->topLevelDoc = $topLevelDoc; |
| 682 | // if it is already prepared and loaded, then serializeNewEmptyDp |
| 683 | // is already set as well; leave it alone. |
| 684 | } else { |
| 685 | $this->topLevelDoc = DOMCompat::newDocument( isHtml: true ); |
| 686 | $documentElement = $this->topLevelDoc->documentElement; |
| 687 | if ( !$documentElement ) { |
| 688 | $documentElement = $this->topLevelDoc->createElement( 'html' ); |
| 689 | $this->topLevelDoc->appendChild( $documentElement ); |
| 690 | } |
| 691 | $body = DOMCompat::getBody( $this->topLevelDoc ); |
| 692 | if ( !$body ) { |
| 693 | $body = $this->topLevelDoc->createElement( 'body' ); |
| 694 | $documentElement->appendChild( $body ); |
| 695 | } |
| 696 | $this->remexPipeline = new RemexPipeline( $this ); |
| 697 | // Prepare and load. |
| 698 | DOMDataUtils::prepareAndLoadDoc( $this->topLevelDoc, $this->siteConfig, [ 'serializeNewEmptyDp' => true ] ); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /** |
| 703 | * Return the current top-level document. During wt2html, this |
| 704 | * will be the document associated with the RemexPipeline. During |
| 705 | * html2wt, this will be the input document, typically passed as a |
| 706 | * constructor option. |
| 707 | * |
| 708 | * This document will be prepared and loaded; see |
| 709 | * ContentUtils::createAndLoadDocument(). |
| 710 | */ |
| 711 | public function getTopLevelDoc(): Document { |
| 712 | return $this->topLevelDoc; |
| 713 | } |
| 714 | |
| 715 | public function fetchRemexPipeline( bool $toFragment ): RemexPipeline { |
| 716 | if ( !$toFragment ) { |
| 717 | return $this->remexPipeline; |
| 718 | } else { |
| 719 | return new RemexPipeline( $this ); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Record a behavior switch. |
| 725 | * |
| 726 | * @param string $switch Switch name |
| 727 | * @param mixed $state Relevant state data to record |
| 728 | */ |
| 729 | public function setBehaviorSwitch( string $switch, $state ): void { |
| 730 | $this->behaviorSwitches[$switch] = $state; |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Fetch the state of a previously-recorded behavior switch. |
| 735 | * |
| 736 | * @param string $switch Switch name |
| 737 | * @param mixed $default Default value if the switch was never set |
| 738 | * @return mixed State data that was previously passed to setBehaviorSwitch(), or $default |
| 739 | */ |
| 740 | public function getBehaviorSwitch( string $switch, $default = null ) { |
| 741 | return $this->behaviorSwitches[$switch] ?? $default; |
| 742 | } |
| 743 | |
| 744 | public function getPFragment( string $id ): PFragment { |
| 745 | return $this->pFragmentMap[$id]; |
| 746 | } |
| 747 | |
| 748 | /** @param array<string,PFragment> $mapping */ |
| 749 | public function addToPFragmentMap( array $mapping ): void { |
| 750 | $this->pFragmentMap += $mapping; |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * @internal |
| 755 | * Serialize pfragment map to string for debugging dumps |
| 756 | */ |
| 757 | public function pFragmentMapToString(): string { |
| 758 | $codec = DOMDataUtils::getCodec( $this->getTopLevelDoc() ); |
| 759 | $buf = ''; |
| 760 | foreach ( $this->pFragmentMap as $k => $v ) { |
| 761 | $buf .= "$k = " . $codec->toJsonString( $v, PFragment::hint() ); |
| 762 | } |
| 763 | return $buf; |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Record a lint |
| 768 | * @param string $type Lint type key |
| 769 | * @param array $lintData Data for the lint. |
| 770 | * - dsr: (SourceRange) |
| 771 | * - params: (array) |
| 772 | * - templateInfo: (array|null) |
| 773 | */ |
| 774 | public function recordLint( string $type, array $lintData ): void { |
| 775 | if ( !$this->linting( $type ) ) { |
| 776 | return; |
| 777 | } |
| 778 | |
| 779 | if ( empty( $lintData['dsr'] ) ) { |
| 780 | $this->log( 'error/lint', "Missing DSR; msg=", $lintData ); |
| 781 | return; |
| 782 | } |
| 783 | $source = $lintData['dsr']?->source ?? $this->topFrame->getSource(); |
| 784 | if ( $source !== $this->topFrame->getSource() ) { |
| 785 | $this->log( 'error/lint', "Bad source; msg=", $lintData ); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | // This will always be recorded as a native 'byte' offset |
| 790 | $lintData['dsr'] = $lintData['dsr']->toJsonArray(); |
| 791 | $lintData['params'] ??= []; |
| 792 | |
| 793 | $this->lints[] = [ 'type' => $type ] + $lintData; |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * Retrieve recorded lints |
| 798 | * @return array[] |
| 799 | */ |
| 800 | public function getLints(): array { |
| 801 | return $this->lints; |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Init lints to the passed array. |
| 806 | * |
| 807 | * FIXME: This is currently needed to reset lints after converting |
| 808 | * DSR offsets because of ordering of DOM passes. So, in reality, |
| 809 | * there should be no real use case for setting this anywhere else |
| 810 | * but from that single callsite. |
| 811 | * |
| 812 | * @param array $lints |
| 813 | */ |
| 814 | public function setLints( array $lints ): void { |
| 815 | $this->lints = $lints; |
| 816 | } |
| 817 | |
| 818 | /** |
| 819 | * @param string $prefix |
| 820 | * @param mixed ...$args |
| 821 | */ |
| 822 | public function log( string $prefix, ...$args ): void { |
| 823 | $this->parsoidLogger->log( $prefix, ...$args ); |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * Shortcut helper that also allows early exit if tracing is not enabled. |
| 828 | * @param string $prefix |
| 829 | * @param mixed ...$args |
| 830 | */ |
| 831 | public function trace( string $prefix, ...$args ): void { |
| 832 | if ( $this->traceFlags ) { |
| 833 | $this->parsoidLogger->log( $prefix ? "trace/$prefix" : "trace", ...$args ); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * Bump usage of some limited parser resource |
| 839 | * (ex: tokens, # transclusions, # list items, etc.) |
| 840 | * |
| 841 | * @param string $resource |
| 842 | * @param int $count How much of the resource is used? |
| 843 | * @return ?bool Returns `null` if the limit was already reached, `false` when exceeded |
| 844 | */ |
| 845 | public function bumpWt2HtmlResourceUse( string $resource, int $count = 1 ): ?bool { |
| 846 | $n = $this->wt2htmlUsage[$resource] ?? 0; |
| 847 | if ( !$this->compareWt2HtmlLimit( $resource, $n ) ) { |
| 848 | return null; |
| 849 | } |
| 850 | $n += $count; |
| 851 | $this->wt2htmlUsage[$resource] = $n; |
| 852 | return $this->compareWt2HtmlLimit( $resource, $n ); |
| 853 | } |
| 854 | |
| 855 | /** |
| 856 | * @param string $resource |
| 857 | * @param int $n |
| 858 | * @return bool Return `false` when exceeded |
| 859 | */ |
| 860 | public function compareWt2HtmlLimit( string $resource, int $n ): bool { |
| 861 | $wt2htmlLimits = $this->siteConfig->getWt2HtmlLimits(); |
| 862 | return !( isset( $wt2htmlLimits[$resource] ) && $n > $wt2htmlLimits[$resource] ); |
| 863 | } |
| 864 | |
| 865 | /** |
| 866 | * Bump usage of some limited serializer resource |
| 867 | * (ex: html size) |
| 868 | * |
| 869 | * @param string $resource |
| 870 | * @param int $count How much of the resource is used? (defaults to 1) |
| 871 | * @throws ResourceLimitExceededException |
| 872 | */ |
| 873 | public function bumpHtml2WtResourceUse( string $resource, int $count = 1 ): void { |
| 874 | $n = $this->html2wtUsage[$resource] ?? 0; |
| 875 | $n += $count; |
| 876 | $this->html2wtUsage[$resource] = $n; |
| 877 | $html2wtLimits = $this->siteConfig->getHtml2WtLimits(); |
| 878 | if ( |
| 879 | isset( $html2wtLimits[$resource] ) && |
| 880 | $n > $html2wtLimits[$resource] |
| 881 | ) { |
| 882 | throw new ResourceLimitExceededException( "html2wt: $resource limit exceeded: $n" ); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * Get an appropriate content handler, given a contentmodel. |
| 888 | * |
| 889 | * @param ?string &$contentmodel An optional content model which |
| 890 | * will override whatever the source specifies. It gets set to the |
| 891 | * handler which is used. |
| 892 | * @return ContentModelHandler An appropriate content handler |
| 893 | */ |
| 894 | public function getContentHandler( |
| 895 | ?string &$contentmodel = null |
| 896 | ): ContentModelHandler { |
| 897 | $contentmodel ??= $this->pageConfig->getContentModel(); |
| 898 | $handler = $this->siteConfig->getContentModelHandler( $contentmodel ); |
| 899 | if ( !$handler && $contentmodel !== 'wikitext' ) { |
| 900 | // For now, fallback to 'wikitext' as the default handler |
| 901 | // FIXME: This is bogus, but this is just so suppress noise in our |
| 902 | // logs till we get around to handling all these other content models. |
| 903 | // $this->log( 'warn', "Unknown contentmodel $contentmodel" ); |
| 904 | } |
| 905 | return $handler ?? $this->wikitextContentModelHandler; |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * Is the language converter enabled on this page? |
| 910 | * |
| 911 | * @return bool |
| 912 | */ |
| 913 | public function langConverterEnabled(): bool { |
| 914 | return $this->siteConfig->langConverterEnabledBcp47( |
| 915 | $this->pageConfig->getPageLanguageBcp47() |
| 916 | ); |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * The HTML content version of the input document (for html2wt and html2html conversions). |
| 921 | * @see https://www.mediawiki.org/wiki/Parsoid/API#Content_Negotiation |
| 922 | * @see https://www.mediawiki.org/wiki/Specs/HTML#Versioning |
| 923 | * @return string A semver version number |
| 924 | */ |
| 925 | public function getInputContentVersion(): string { |
| 926 | return $this->inputContentVersion; |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * The HTML content version of the input document (for html2wt and html2html conversions). |
| 931 | * @see https://www.mediawiki.org/wiki/Parsoid/API#Content_Negotiation |
| 932 | * @see https://www.mediawiki.org/wiki/Specs/HTML#Versioning |
| 933 | * @return string A semver version number |
| 934 | */ |
| 935 | public function getOutputContentVersion(): string { |
| 936 | return $this->outputContentVersion; |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Determine appropriate vary headers for the HTML form of this page. |
| 941 | * @return string |
| 942 | */ |
| 943 | public function htmlVary(): string { |
| 944 | $varies = [ 'Accept' ]; // varies on Content-Type |
| 945 | if ( $this->langConverterEnabled() ) { |
| 946 | $varies[] = 'Accept-Language'; |
| 947 | } |
| 948 | |
| 949 | sort( $varies ); |
| 950 | return implode( ', ', $varies ); |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * Determine an appropriate content-language for the HTML form of this page. |
| 955 | * @return Bcp47Code a BCP-47 language code. |
| 956 | */ |
| 957 | public function htmlContentLanguageBcp47(): Bcp47Code { |
| 958 | // PageConfig::htmlVariant is set iff we do variant conversion on the |
| 959 | // HTML |
| 960 | return $this->pageConfig->getVariantBcp47() ?? |
| 961 | $this->pageConfig->getPageLanguageBcp47(); |
| 962 | } |
| 963 | |
| 964 | /** |
| 965 | * Get an array of attributes to apply to an anchor linking to $url |
| 966 | * |
| 967 | * @return array{class:list<string>,rel:list<string>,title?:string,href?:string} |
| 968 | */ |
| 969 | public function getExternalLinkAttribs( string $url ): array { |
| 970 | $siteConfig = $this->getSiteConfig(); |
| 971 | $dataAccess = $this->getDataAccess(); |
| 972 | // Even though this API allows for batch updates, we're just passsing 1 |
| 973 | if ( $url ) { |
| 974 | $info = $dataAccess->getExternalUrlInfo( |
| 975 | $siteConfig, [ $url ], $this->getContextTitle() |
| 976 | )[0]; |
| 977 | } else { |
| 978 | $info = [ 'href' => $url ]; |
| 979 | } |
| 980 | $split = static fn ( $s ) => preg_split( '/\s+/', $s, -1, PREG_SPLIT_NO_EMPTY ); |
| 981 | $attribs = [ |
| 982 | 'href' => $info['href'] ?? null, |
| 983 | 'class' => $split( $info['class'] ?? '' ), |
| 984 | 'rel' => $split( $info['rel'] ?? '' ), |
| 985 | ]; |
| 986 | if ( ( $info['title'] ?? null ) !== null ) { |
| 987 | $attribs['title'] = $info['title']; |
| 988 | } |
| 989 | if ( $attribs['href'] !== $url ) { |
| 990 | $attribs['data-mw-original-href'] = $url; |
| 991 | } |
| 992 | if ( $attribs['href'] === null ) { |
| 993 | unset( $attribs['href'] ); |
| 994 | } |
| 995 | $target = $siteConfig->getExternalLinkTarget(); |
| 996 | if ( $target ) { |
| 997 | $attribs['target'] = $target; |
| 998 | // T133507/T427561: we used to set `rel` attributes here, but |
| 999 | // no longer need to do so. |
| 1000 | } |
| 1001 | return $attribs; |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * @return array |
| 1006 | */ |
| 1007 | public function getLinterConfig(): array { |
| 1008 | return $this->linterOverrides + $this->getSiteConfig()->getLinterSiteConfig(); |
| 1009 | } |
| 1010 | |
| 1011 | /** |
| 1012 | * Whether to enable linter Backend. |
| 1013 | * Consults the allow list and block list from ::getLinterConfig(). |
| 1014 | * |
| 1015 | * @param string|null $type If $type is null or omitted, returns true if *any* linting |
| 1016 | * type is enabled; otherwise returns true only if the specified |
| 1017 | * linting type is enabled. |
| 1018 | * @return bool If $type is null or omitted, returns true if *any* linting |
| 1019 | * type is enabled; otherwise returns true only if the specified |
| 1020 | * linting type is enabled. |
| 1021 | */ |
| 1022 | public function linting( ?string $type = null ) { |
| 1023 | if ( !$this->getSiteConfig()->linterEnabled() ) { |
| 1024 | return false; |
| 1025 | } |
| 1026 | $lintConfig = $this->getLinterConfig(); |
| 1027 | // Allow list |
| 1028 | $allowList = $lintConfig['enabled'] ?? null; |
| 1029 | if ( is_array( $allowList ) ) { |
| 1030 | if ( $type === null ) { |
| 1031 | return count( $allowList ) > 0; |
| 1032 | } |
| 1033 | return in_array( $type, $allowList, true ); |
| 1034 | } |
| 1035 | // Block list |
| 1036 | if ( $type === null ) { |
| 1037 | return true; |
| 1038 | } |
| 1039 | $blockList = $lintConfig['disabled'] ?? null; |
| 1040 | if ( is_array( $blockList ) ) { |
| 1041 | return !in_array( $type, $blockList, true ); |
| 1042 | } |
| 1043 | // No specific configuration |
| 1044 | return true; |
| 1045 | } |
| 1046 | } |