Debian Codegraph @

vendor/wikimedia/parsoid/src/Core/ContentMetadataCollector.php

raw
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Wikimedia\Parsoid\Core;
5
6 /**
7 * Interface for collecting the results of a parse.
8 *
9 * This class is used by Parsoid to record metainformation about a
10 * particular bit of parsed content which is extracted during the
11 * parse. This includes (for example) table of contents information,
12 * and lists of links/categories/templates/images present in the
13 * content. Expected cache lifetime of this parsed content is also
14 * recorded here, as it is influenced by certain things which may
15 * be encountered during the parse.
16 *
17 * In core this is implemented by ParserOutput. Core uses
18 * ParserOutput to record the rendered HTML (and rendered table of
19 * contents HTML), but on the Parsoid side we're going to keep
20 * rendered HTML DOM out of this interface (we use PageBundle for
21 * this).
22 */
23 interface ContentMetadataCollector {
24 /*
25 * Internal implementation notes:
26 * This class was refactored out of ParserOutput in core.
27 *
28 * == Deliberately omitted ==
29 * ::get*()/::has*() and other getters
30 * This is a builder-only interface. This also avoids ordering
31 * issues if/when Parsoid passes this class to sub-parses/extensions.
32 * ::setSpeculativeRevIdUsed()
33 * ::setRevisionTimestampUsed()
34 * ::setRevisionUsedSha1Base36()
35 * ::setSpeculativePageIdUsed()
36 * T292865: these should be plumbed through direct from ParserOptions
37 * or use the ::setOutputFlag() or addOutputData() mechanism.
38 * ::setTimestamp()
39 * This is used by ParserCache and is a little optimization used to
40 * show the correct 'article was last edited on blablablah' box on
41 * page views. Parsoid shouldn't need to worry about this; probably
42 * part of T292865.
43 * ::addCacheMessage()
44 * This is marked @internal in core.
45 * Not clear yet whether Parsoid needs this.
46 * ::getText()/::setText()
47 * T293512: rendered HTML doesn't belong in ParserOutput
48 * ::addWrapperDivClass()/::clearWrapperDivClass()
49 * Has to do with ::getText() implementation, see above
50 * ::setTitleText()
51 * Omited because it contains rendered HTML
52 * (should become a method which takes a DOM tree instead?)
53 * ::setTOCHTML()
54 * Omitted because it contains rendered HTML.
55 * T293513 will remove this method from ParserOutput
56 * ::addHeadItem()
57 * Not clear this is needed by Parsoid (but maybe some of the stuff
58 * Parsoid adds to head could be refactored to use this interface).
59 * Should be DOM not string data!
60 * ::addOutputPageMetadata()
61 * OutputPage isn't a Parsoid interface, so this shouldn't be needed
62 * by Parsoid.
63 * ::setDisplayTitle()
64 * T293514: This desugars to calls to two other methods in
65 * ContentOutputBuilder; callers can refactor to invoke those directly.
66 * ::unsetPageProperty()
67 * If parse fragment A is setting a property
68 * and parse fragment B is unsetting the property, we've introduced
69 * an ordering dependency. We'd like to avoid that code pattern.
70 * ::resetParseStartTime()/::getTimeSinceStart()
71 * Not needed by parsoid?
72 * ::finalizeAdaptiveCacheExpiry()
73 * Same as above, can probably be invoked by caller of parsoid,
74 * doesn't need to be in Parsoid library code.
75 * ::mergeInternalMetaDataFrom()
76 * ::mergeHtmlMetaDataFrom()
77 * ::mergeTrackingMetaDataFrom()
78 * Rather than explicitly merging ContentMetadataCollectors, we'd
79 * prefer to pass a single ContentOutputBuilder around to accumulate
80 * results. We're going to wait and see to what extent methods like
81 * this are necessary.
82 * (ParserOutput will implement a ::mergeTo(ContentMetadataCollector)
83 * method, as it has read access to its own contents.)
84 * ::setNoGallery()/::setEnableOOUI()/::setNewSection()/::setHideNewSection()
85 * ::setPreventClickjacking()/::setIndexPolicy()/
86 * Available via ::setOutputFlag() (see T292868)
87 * ::setCategories()
88 * Doesn't seem necessary, we have ::addCategory().
89 * (And adding the ability to overwrite categories would be bad.)
90 * ::addTrackingCategory()
91 * This was moved to Parser / the TrackingCategories service, and
92 * equivalently DataAccess in Parsoid.
93 * ::isLinkInternal()
94 * T296036: Should be non-public or at least @internal?
95 * ::addInterwikiLink()
96 * invoked from ::addLink() if the link is external, we don't
97 * need a separate entry point.
98 * ::setSections()
99 * T296025: replaced with ::setTOCData()
100 * ::setLanguageLinks()
101 * Deprecated; replaced with ::addLanguageLink()
102 * ::addExtraCSPDefaultSrc()
103 * ::addExtraCSPStyleSrc()
104 * ::addExtraCSPScriptSrc()
105 * ::updateRuntimeAdaptiveExpiry()
106 * T296345: handled through ::appendOutputStrings()
107 *
108 * == Temporarily omitted ==
109 * ::addTemplate()
110 * T296038: Requires page id and revision id. In addition, this
111 * interacts with user hooks. The MediaWiki side should probably be
112 * responsible for updating the Template dependencies not Parsoid.
113 * OTOH, we need to return *something* like a Title back because
114 * eventually Parsoid has to fetch the template to expand it.
115 * ::setTitleText()
116 * T293514: This contains the title in HTML and is redundant with
117 * ::setDisplayTitle()
118 */
119
120 /**
121 * Merge strategy to use for ContentMetadataCollector
122 * accumulators: "union" means that values are strings, stored as
123 * a set, and exposed as a PHP associative array mapping from
124 * values to `true`.
125 *
126 * This constant should be treated as @internal until we expose
127 * alternative merge strategies for external use.
128 * @internal
129 */
130 public const MERGE_STRATEGY_UNION = 'union';
131
132 /**
133 * Add a category, with the given sort key.
134 *
135 * @param LinkTarget $c Category name
136 * @param string $sort Sort key (pass the empty string to use the default)
137 */
138 public function addCategory( $c, $sort = '' ): void;
139
140 /**
141 * Record a local or interwiki inline link for saving in future link tables.
142 *
143 * @param LinkTarget $link (used to require Title until 1.38)
144 * @param int|null $id Optional known page_id so we can skip the lookup
145 * (generally not used by Parsoid)
146 */
147 public function addLink( LinkTarget $link, $id = null ): void;
148
149 /**
150 * Register a file dependency for this output
151 * @param LinkTarget $name Title dbKey
152 * @param string|false|null $timestamp MW timestamp of file creation (or false if non-existing)
153 * @param string|false|null $sha1 Base 36 SHA-1 of file (or false if non-existing)
154 */
155 public function addImage( LinkTarget $name, $timestamp = null, $sha1 = null ): void;
156
157 /**
158 * Add a language link.
159 * @param LinkTarget $lt
160 */
161 public function addLanguageLink( LinkTarget $lt ): void;
162
163 /**
164 * Add a warning to the output for this page.
165 * @param string $msg The localization message key for the warning
166 * @param mixed ...$args Optional arguments for the message
167 */
168 public function addWarningMsg( string $msg, ...$args ): void;
169
170 /**
171 * @param string $url External link URL
172 */
173 public function addExternalLink( string $url ): void;
174
175 /**
176 * Provides a uniform interface to various boolean flags stored
177 * in the content metadata. Flags internal to MediaWiki core should
178 * have names which are constants in ParserOutputFlags. Extensions
179 * should use ::setExtensionData() rather than creating new flags
180 * with ::setOutputFlag() in order to prevent namespace conflicts.
181 *
182 * @param string $name A flag name
183 * @param bool $val
184 */
185 public function setOutputFlag( string $name, bool $val = true ): void;
186
187 /**
188 * Provides a uniform interface to various appendable lists of strings
189 * stored in the content metadata. Strings internal to MediaWiki core should
190 * have names which are constants in ParserOutputStrings. Extensions
191 * should use ::setExtensionData() rather than creating new keys here
192 * in order to prevent namespace conflicts.
193 *
194 * @param string $name A string name
195 * @param string[] $value
196 */
197 public function appendOutputStrings( string $name, array $value ): void;
198
199 /**
200 * Set a numeric page property whose *value* is intended to be sorted
201 * and indexed. The sort key used for the property will be the value,
202 * coerced to a number. It is also possible to efficiently look up all
203 * the pages with a certain property (the "presence" of the
204 * property is also indexed; see Special:PagesWithProp,
205 * list=pageswithprop).
206 *
207 * The page property is stored in the page_props database
208 * table. The page_props table is a key-value store indexed by the
209 * page ID. This allows the parser to set a property on a page
210 * whose value can then be quickly retrieved given the page ID or via a
211 * DB join when given the page title. The page_props table is *also*
212 * indexed on the numeric sort key passed as $numericValue to this
213 * method. This allows for efficient "top k" queries of pages with
214 * respect to a given property.
215 *
216 * In the future, we may allow the value to be specified independent
217 * of sort key (T357783).
218 *
219 * The setNumericPageProperty() method is thus used to propagate
220 * properties from the parsed page to request contexts other than
221 * a page view of the currently parsed article.
222 *
223 * Some applications examples:
224 *
225 * * The Proofread page extension stores
226 * `proofread_page_quality_level` as a numeric property to allow
227 * efficient retrieval of pages of a certain quality level.
228 *
229 * * Keeping a count of the number of errors found in a page property
230 * to allow listing pages in order from most errors to least.
231 *
232 * If you need a placeholder value, you likely should be using
233 * ::setUnsortedPageProperty() instead.
234 *
235 * @note Note that the PageProp service always returns strings
236 * for the value of the page property, while values retrieved
237 * from this ParserOutput will be numeric. Be careful to distinguish
238 * these two cases.
239 *
240 * @note Do not use setNumericPageProperty() to set a property
241 * which is only used in a context where the ParserOutput object
242 * itself is already available, for example a normal page
243 * view. There is no need to save such a property in the database
244 * since the text is already parsed; use ::setExtensionData()
245 * instead.
246 *
247 * @par Example:
248 * @code
249 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
250 * @endcode
251 *
252 * And then later, in the OutputPageParserOutput hook or similar:
253 *
254 * @par Example:
255 * @code
256 * $output->getExtensionData( 'my_ext_foo' );
257 * @endcode
258 *
259 * @param string $propName The name of the page property
260 * @param int|float|string $numericValue the numeric value
261 * @since 1.42
262 */
263 public function setNumericPageProperty( string $propName, $numericValue ): void;
264
265 /**
266 * Set a page property whose *value* is not intended to be sorted
267 * and indexed. It is still possible to efficiently look up all
268 * the pages with a certain property (the "presence" of the
269 * property *is* indexed; see Special:PagesWithProp,
270 * list=pageswithprop).
271 *
272 * The page property is stored in the page_props database
273 * table. The page_props table is a key-value store indexed by the
274 * page ID. This allows the parser to set a property on a page
275 * whose value can then be quickly retrieved given the page ID or via a
276 * DB join when given the page title.
277 *
278 * The setUnsortedPageProperty() method is thus used to propagate
279 * properties from the parsed page to request contexts other than
280 * a page view of the currently parsed article.
281 *
282 * Some applications examples:
283 *
284 * * To implement hidden categories, hiding pages from category listings
285 * by storing a page property.
286 *
287 * * Overriding the displayed article title
288 * (ParserOutput::setDisplayTitle()).
289 *
290 * * To implement image tagging, for example displaying an icon on an
291 * image thumbnail to indicate that it is listed for deletion on
292 * Wikimedia Commons.
293 * (This is not actually implemented yet but would be pretty cool.)
294 *
295 * It is recommended to use the empty string if you need a
296 * placeholder value (ie, if it is the *presence* of the property
297 * which is important, not the *value* the property is set to).
298 *
299 * @note Do not use setUnsortedPageProperty() to set a property
300 * which is only used in a context where the ParserOutput object
301 * itself is already available, for example a normal page
302 * view. There is no need to save such a property in the database
303 * since the text is already parsed; use ::setExtensionData()
304 * instead.
305 *
306 * @par Example:
307 * @code
308 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
309 * @endcode
310 *
311 * And then later, in the OutputPageParserOutput hook or similar:
312 *
313 * @par Example:
314 * @code
315 * $output->getExtensionData( 'my_ext_foo' );
316 * @endcode
317 *
318 * @param string $propName The name of the page property
319 * @param string $value Optional value; defaults to the empty string.
320 * @since 1.42
321 */
322 public function setUnsortedPageProperty( string $propName, string $value = '' ): void;
323
324 /**
325 * Attaches arbitrary data to this content. This can be used to
326 * store some information for later use during page output. The
327 * data will be cached along with the parsed page, but unlike data
328 * set using set*PageProperty(), it is not recorded in the
329 * database.
330 *
331 * To use setExtensionData() to pass extension information from a
332 * hook inside the parser to a hook in the page output, use this
333 * in the parser hook:
334 *
335 * @par Example:
336 * @code
337 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
338 * @endcode
339 *
340 * And then later, in OutputPageParserOutput or similar:
341 *
342 * @par Example:
343 * @code
344 * $output->getExtensionData( 'my_ext_foo' );
345 * @endcode
346 *
347 * @note Only scalar values, e.g. numbers, strings, arrays or
348 * MediaWiki\Json\JsonUnserializable instances are supported as a
349 * value. Attempt to set other class instance as a extension data
350 * will break ParserCache for the page.
351 *
352 * @note As with ::setJsConfigVar(), setting a page property to multiple
353 * conflicting values during the parse is not supported.
354 *
355 * @param string $key The key for accessing the data. Extensions
356 * should take care to avoid conflicts in naming keys. It is
357 * suggested to use the extension's name as a prefix. Keys
358 * beginning with `mw-` are reserved for use by mediawiki core.
359 *
360 * @param mixed $value The value to set.
361 * Setting a value to null is equivalent to removing the value.
362 */
363 public function setExtensionData( string $key, $value ): void;
364
365 /**
366 * Appends arbitrary data to this ParserObject. This can be used
367 * to store some information in the ParserOutput object for later
368 * use during page output. The data will be cached along with the
369 * ParserOutput object, but unlike data set using
370 * set*PageProperty(), it is not recorded in the database.
371 *
372 * See ::setExtensionData() for more details on rationale and use.
373 *
374 * In order to provide for out-of-order/asynchronous/incremental
375 * parsing, this method appends values to a set. See
376 * ::setExtensionData() for the flag-like version of this method.
377 *
378 * @note Only values which can be array keys are currently supported
379 * as values. Be aware that array keys which 'look like' numbers are
380 * converted to ints by PHP, and so if you put in `"0"` as a value you
381 * will get `[0=>true]` out.
382 *
383 * @param string $key The key for accessing the data. Extensions should take care to avoid
384 * conflicts in naming keys. It is suggested to use the extension's name as a prefix.
385 *
386 * @param int|string $value The value to append to the list.
387 * @param string $strategy Merge strategy:
388 * only MW_MERGE_STRATEGY_UNION is currently supported and external callers
389 * should treat this parameter as @internal at this time and omit it.
390 */
391 public function appendExtensionData(
392 string $key,
393 $value,
394 string $strategy = self::MERGE_STRATEGY_UNION
395 ): void;
396
397 /**
398 * Add a variable to be set in mw.config in JavaScript.
399 *
400 * In order to ensure the result is independent of the parse order, the values
401 * set here must be unique -- that is, you can pass the same $key
402 * multiple times but ONLY if the $value is identical each time.
403 * If you want to collect multiple pieces of data under a single key,
404 * use ::appendJsConfigVar().
405 *
406 * @param string $key Key to use under mw.config
407 * @param mixed|null $value Value of the configuration variable.
408 */
409 public function setJsConfigVar( string $key, $value ): void;
410
411 /**
412 * Append a value to a variable to be set in mw.config in JavaScript.
413 *
414 * In order to ensure the result is independent of the parse order,
415 * the value of this key will be an associative array, mapping all of
416 * the values set under that key to true. (The array is implicitly
417 * ordered in PHP, but you should treat it as unordered.)
418 * If you want a non-array type for the key, and can ensure that only
419 * a single value will be set, you should use ::setJsConfigVar() instead.
420 *
421 * @note Only values which can be array keys are currently supported
422 * as values. Be aware that array keys which 'look like' numbers are
423 * converted to ints by PHP, and so if you put in `"0"` as a value you
424 * will get `[0=>true]` out.
425 *
426 * @param string $key Key to use under mw.config
427 * @param string $value Value to append to the configuration variable.
428 * @param string $strategy Merge strategy:
429 * only MW_MERGE_STRATEGY_UNION is currently supported and external callers
430 * should treat this parameter as @internal at this time and omit it.
431 */
432 public function appendJsConfigVar(
433 string $key,
434 string $value,
435 string $strategy = self::MERGE_STRATEGY_UNION
436 ): void;
437
438 /**
439 * @see OutputPage::addModules
440 * @param string[] $modules
441 * @deprecated use ::appendOutputStrings(::MODULE, ...)
442 */
443 public function addModules( array $modules ): void;
444
445 /**
446 * @see OutputPage::addModuleStyles
447 * @param string[] $modules
448 * @deprecated use ::appendOutputStrings(::MODULE_STYLE, ...)
449 */
450 public function addModuleStyles( array $modules ): void;
451
452 /**
453 * Sets parser limit report data for a key
454 *
455 * The key is used as the prefix for various messages used for formatting:
456 * - $key: The label for the field in the limit report
457 * - $key-value-text: Message used to format the value in the "NewPP limit
458 * report" HTML comment. If missing, uses $key-format.
459 * - $key-value-html: Message used to format the value in the preview
460 * limit report table. If missing, uses $key-format.
461 * - $key-value: Message used to format the value. If missing, uses "$1".
462 *
463 * Note that all values are interpreted as wikitext, and so should be
464 * encoded with htmlspecialchars() as necessary, but should avoid complex
465 * HTML for sanity of display in the "NewPP limit report" comment.
466 *
467 * @param string $key Message key
468 * @param mixed $value Appropriate for Message::params()
469 */
470 public function setLimitReportData( string $key, $value ): void;
471
472 /**
473 * Sets Table of Contents data for this page.
474 *
475 * Note that merging of TOCData is not supported; exactly one fragment
476 * should set TOCData.
477 *
478 * @param TOCData $tocData
479 */
480 public function setTOCData( TOCData $tocData ): void;
481
482 /**
483 * Set the content for an indicator.
484 *
485 * @param string $name
486 * @param string $content
487 * @param-taint $content exec_html
488 */
489 public function setIndicator( $name, $content ): void;
490 }