Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
79.19% |
609 / 769 |
|
69.01% |
98 / 142 |
CRAP | |
0.00% |
0 / 1 |
ParserOutput | |
79.30% |
609 / 768 |
|
69.01% |
98 / 142 |
1444.26 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
hasText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRawText | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getText | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
1 | |||
addCacheMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addWrapperDivClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
clearWrapperDivClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getWrapperDivClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setSpeculativeRevIdUsed | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSpeculativeRevIdUsed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setSpeculativePageIdUsed | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSpeculativePageIdUsed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setRevisionTimestampUsed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRevisionTimestampUsed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setRevisionUsedSha1Base36 | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
4.59 | |||
getRevisionUsedSha1Base36 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLanguageLinks | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getInterwikiLinks | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCategoryNames | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCategoryMap | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCategorySortKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getIndicators | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTitleText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTOCData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCacheMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSections | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
getLinks | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLinksSpecial | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTemplates | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTemplateIds | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getImages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFileSearchOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExternalLinks | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setNoGallery | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getNoGallery | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getHeadItems | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getModules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getModuleStyles | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getJsConfigVars | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
getWarnings | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getIndexPolicy | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getRevisionTimestamp | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTimestamp | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLimitReportData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLimitReportJSData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getEnableOOUI | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExtraCSPDefaultSrcs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExtraCSPScriptSrcs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExtraCSPStyleSrcs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setRawText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setLanguageLinks | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setTitleText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setTOCData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setSections | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setIndexPolicy | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
setRevisionTimestamp | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setTimestamp | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addCategory | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setCategories | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setIndicator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setEnableOOUI | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addLanguageLink | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
addWarningMsg | |
69.23% |
9 / 13 |
|
0.00% |
0 / 1 |
2.12 | |||
setNewSection | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setHideNewSection | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHideNewSection | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNewSection | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isLinkInternal | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
addExternalLink | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
addLink | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
6 | |||
addImage | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
addTemplate | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
2.09 | |||
addInterwikiLink | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
addHeadItem | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
addModules | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addModuleStyles | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addJsConfigVars | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
setJsConfigVar | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
appendJsConfigVar | |
72.73% |
8 / 11 |
|
0.00% |
0 / 1 |
5.51 | |||
addOutputPageMetadata | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
setDisplayTitle | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getDisplayTitle | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getLanguage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
setLanguage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRedirectHeader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setRedirectHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setRenderId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getRenderId | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getAllFlags | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setPageProperty | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
setNumericPageProperty | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
setUnsortedPageProperty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPageProperty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
unsetPageProperty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPageProperties | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
setOutputFlag | |
40.00% |
10 / 25 |
|
0.00% |
0 / 1 |
31.60 | |||
getOutputFlag | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
9 | |||
appendOutputStrings | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
10.01 | |||
getOutputStrings | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
7.04 | |||
setExtensionData | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
appendExtensionData | |
72.73% |
8 / 11 |
|
0.00% |
0 / 1 |
5.51 | |||
getExtensionData | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getTimes | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
resetParseStartTime | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
recordTimeProfile | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
getTimeProfile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTimeSinceStart | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
setLimitReportData | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
6.02 | |||
hasReducedExpiry | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setPreventClickjacking | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getPreventClickjacking | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
updateRuntimeAdaptiveExpiry | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addExtraCSPDefaultSrc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addExtraCSPStyleSrc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addExtraCSPScriptSrc | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
finalizeAdaptiveCacheExpiry | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
setFromParserOptions | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
__sleep | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
mergeInternalMetaDataFrom | |
70.00% |
28 / 40 |
|
0.00% |
0 / 1 |
22.91 | |||
mergeHtmlMetaDataFrom | |
97.67% |
42 / 43 |
|
0.00% |
0 / 1 |
14 | |||
mergeTrackingMetaDataFrom | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
1 | |||
collectMetadata | |
0.00% |
0 / 53 |
|
0.00% |
0 / 1 |
930 | |||
mergeMixedList | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
mergeList | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
mergeMap | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
mergeMapStrategy | |
85.71% |
18 / 21 |
|
0.00% |
0 / 1 |
12.42 | |||
merge2D | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
6.03 | |||
useEachMinValue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
useEachTotalValue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
useMaxValue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
toJsonArray | |
98.15% |
53 / 54 |
|
0.00% |
0 / 1 |
5 | |||
newFromJsonArray | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
initFromJson | |
98.04% |
50 / 51 |
|
0.00% |
0 / 1 |
5 | |||
detectAndEncodeBinary | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
detectAndDecodeBinary | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 | |||
__wakeup | |
66.67% |
12 / 18 |
|
0.00% |
0 / 1 |
13.70 | |||
__clone | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getContentHolderText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setContentHolderText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__get | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
__set | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Parser; |
22 | |
23 | use CacheTime; |
24 | use InvalidArgumentException; |
25 | use LogicException; |
26 | use MediaWiki\Edit\ParsoidRenderID; |
27 | use MediaWiki\Json\JsonUnserializable; |
28 | use MediaWiki\Json\JsonUnserializableTrait; |
29 | use MediaWiki\Json\JsonUnserializer; |
30 | use MediaWiki\MainConfigNames; |
31 | use MediaWiki\MediaWikiServices; |
32 | use MediaWiki\Output\OutputPage; |
33 | use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter; |
34 | use MediaWiki\Title\Title; |
35 | use MediaWiki\Title\TitleValue; |
36 | use ParserOptions; |
37 | use UnexpectedValueException; |
38 | use Wikimedia\Bcp47Code\Bcp47Code; |
39 | use Wikimedia\Bcp47Code\Bcp47CodeValue; |
40 | use Wikimedia\Parsoid\Core\ContentMetadataCollector; |
41 | use Wikimedia\Parsoid\Core\ContentMetadataCollectorCompat; |
42 | use Wikimedia\Parsoid\Core\LinkTarget as ParsoidLinkTarget; |
43 | use Wikimedia\Parsoid\Core\TOCData; |
44 | use Wikimedia\Reflection\GhostFieldAccessTrait; |
45 | |
46 | /** |
47 | * ParserOutput is a rendering of a Content object or a message. |
48 | * Content objects and messages often contain wikitext, but not always. |
49 | * |
50 | * `ParserOutput` object combine the HTML rendering of Content objects |
51 | * or messages, available via `::getRawText()`, with various bits of |
52 | * metadata generated during rendering, which may include categories, |
53 | * links, page properties, and extension data, among others. |
54 | * |
55 | * `ParserOutput` objects corresponding to the content of page revisions |
56 | * are created by the `ParserOutputAccess` service, which |
57 | * automatically caches them via `ParserCache` where appropriate and |
58 | * produces new output via `ContentHandler` as needed. |
59 | * |
60 | * In addition, wikitext from system messages as well as odd bits of |
61 | * wikitext rendered to create special pages and other UX elements are |
62 | * rendered to `ParserOutput` objects. In these cases the metadata |
63 | * from the `ParserOutput` is generally discarded and the |
64 | * `ParserOutput` is not cached. These bits of wikitext are generally |
65 | * rendered with `ParserOptions::setInterfaceMessage(true)` when |
66 | * content is intended to be in the user interface language, but |
67 | * sometimes rendered to the content language and displayed in the |
68 | * content area instead. |
69 | * |
70 | * A `ParserOutput` object corresponding to a given revision may be a |
71 | * combination of the renderings of multiple "slots": |
72 | * the Multi-Content Revisions (MCR) work allows articles to be |
73 | * composed from multiple `Content` objects. Each `Content` renders |
74 | * to a `ParserOutput`, and those `ParserOutput`s are merged by |
75 | * `RevisionRenderer::combineSlotOutput()` to create the final article |
76 | * output. |
77 | * |
78 | * Similarly, `OutputPage` maintains metadata overlapping |
79 | * with the metadata kept by `ParserOutput` (T301020) and may merge |
80 | * several `ParserOutput`s using `OutputPage::addParserOutput()` to |
81 | * create the final output page. Parsoid parses certain transclusions |
82 | * in independent top-level contexts using |
83 | * `Parser::parseExtensionTagAsTopLevelDoc()` and these also result in |
84 | * `ParserOutput`s which are merged via |
85 | * `ParserOutput::collectMetadata()`. |
86 | * |
87 | * Future plans for incremental parsing and asynchronous rendering may |
88 | * result in several of these component `ParserOutput` objects being |
89 | * cached independently and then recombined asynchronously, so |
90 | * operations on `ParserOutput` objects should be compatible with that |
91 | * model (T300979). |
92 | * |
93 | * @ingroup Parser |
94 | */ |
95 | class ParserOutput extends CacheTime implements ContentMetadataCollector { |
96 | use GhostFieldAccessTrait; |
97 | use JsonUnserializableTrait; |
98 | // This is used to break cyclic dependencies and allow a measure |
99 | // of compatibility when new methods are added to ContentMetadataCollector |
100 | // by Parsoid. |
101 | use ContentMetadataCollectorCompat; |
102 | |
103 | /** |
104 | * Feature flags to indicate to extensions that MediaWiki core supports and |
105 | * uses getText() stateless transforms. |
106 | * |
107 | * @since 1.31 |
108 | */ |
109 | public const SUPPORTS_STATELESS_TRANSFORMS = 1; |
110 | |
111 | /** |
112 | * @since 1.31 |
113 | */ |
114 | public const SUPPORTS_UNWRAP_TRANSFORM = 1; |
115 | |
116 | /** |
117 | * @internal |
118 | * @since 1.38 |
119 | */ |
120 | public const MW_MERGE_STRATEGY_KEY = '_mw-strategy'; |
121 | |
122 | /** |
123 | * Merge strategy to use for ParserOutput accumulators: "union" |
124 | * means that values are strings, stored as a set, and exposed as |
125 | * a PHP associative array mapping from values to `true`. |
126 | * |
127 | * This constant should be treated as @internal until we expose |
128 | * alternative merge strategies for external use. |
129 | * @internal |
130 | * @since 1.38 |
131 | */ |
132 | public const MW_MERGE_STRATEGY_UNION = 'union'; |
133 | |
134 | /** |
135 | * @var string|null The output text |
136 | */ |
137 | private $mRawText = null; |
138 | |
139 | /** |
140 | * @var string[] List of the full text of language links, in the order they appear. |
141 | */ |
142 | private $mLanguageLinks; |
143 | |
144 | /** |
145 | * @var array<string,string> Map of category names to sort keys |
146 | */ |
147 | private $mCategories; |
148 | |
149 | /** |
150 | * @var array<string,string> Page status indicators, usually displayed in top-right corner. |
151 | */ |
152 | private $mIndicators = []; |
153 | |
154 | /** |
155 | * @var string Title text of the chosen language variant, as HTML. |
156 | */ |
157 | private $mTitleText; |
158 | |
159 | /** |
160 | * @var array<int,array<string,int>> 2-D map of NS/DBK to ID for the links in the document. |
161 | * ID=zero for broken. |
162 | */ |
163 | private $mLinks = []; |
164 | |
165 | /** |
166 | * @var array<string,int> Keys are DBKs for the links to special pages in the document. |
167 | * @since 1.35 |
168 | */ |
169 | private $mLinksSpecial = []; |
170 | |
171 | /** |
172 | * @var array<int,array<string,int>> 2-D map of NS/DBK to ID for the template references. |
173 | * ID=zero for broken. |
174 | */ |
175 | private $mTemplates = []; |
176 | |
177 | /** |
178 | * @var array<int,array<string,int>> 2-D map of NS/DBK to rev ID for the template references. |
179 | * ID=zero for broken. |
180 | */ |
181 | private $mTemplateIds = []; |
182 | |
183 | /** |
184 | * @var array<string,int> DB keys of the images used, in the array key only |
185 | */ |
186 | private $mImages = []; |
187 | |
188 | /** |
189 | * @var array<string,array<string,string>> DB keys of the images used mapped to sha1 and MW timestamp. |
190 | */ |
191 | private $mFileSearchOptions = []; |
192 | |
193 | /** |
194 | * @var array<string,int> External link URLs, in the key only. |
195 | */ |
196 | private $mExternalLinks = []; |
197 | |
198 | /** |
199 | * @var array<string,array<string,int>> 2-D map of prefix/DBK (in keys only) |
200 | * for the inline interwiki links in the document. |
201 | */ |
202 | private $mInterwikiLinks = []; |
203 | |
204 | /** |
205 | * @var bool Show a new section link? |
206 | */ |
207 | private $mNewSection = false; |
208 | |
209 | /** |
210 | * @var bool Hide the new section link? |
211 | */ |
212 | private $mHideNewSection = false; |
213 | |
214 | /** |
215 | * @var bool No gallery on category page? (__NOGALLERY__). |
216 | */ |
217 | private $mNoGallery = false; |
218 | |
219 | /** |
220 | * @var string[] Items to put in the <head> section |
221 | */ |
222 | private $mHeadItems = []; |
223 | |
224 | /** |
225 | * @var array<string,true> Modules to be loaded by ResourceLoader |
226 | */ |
227 | private $mModuleSet = []; |
228 | |
229 | /** |
230 | * @var array<string,true> Modules of which only the CSS will be loaded by ResourceLoader. |
231 | */ |
232 | private $mModuleStyleSet = []; |
233 | |
234 | /** |
235 | * @var array JavaScript config variable for mw.config combined with this page. |
236 | */ |
237 | private $mJsConfigVars = []; |
238 | |
239 | /** |
240 | * @var array<string,int> Warning text to be returned to the user. |
241 | * Wikitext formatted, in the key only. |
242 | */ |
243 | private $mWarnings = []; |
244 | |
245 | /** |
246 | * @var array<string,array> *Unformatted* warning messages and |
247 | * arguments to be returned to the user. This is for internal use |
248 | * when merging ParserOutputs and are not serialized/deserialized. |
249 | */ |
250 | private $mWarningMsgs = []; |
251 | |
252 | /** |
253 | * @var ?TOCData Table of contents data, or null if it hasn't been set. |
254 | */ |
255 | private $mTOCData; |
256 | |
257 | /** |
258 | * @var array Name/value pairs to be cached in the DB. |
259 | */ |
260 | private $mProperties = []; |
261 | |
262 | /** |
263 | * @var ?string Timestamp of the revision. |
264 | */ |
265 | private $mTimestamp; |
266 | |
267 | /** |
268 | * @var bool Whether OOUI should be enabled. |
269 | */ |
270 | private $mEnableOOUI = false; |
271 | |
272 | /** |
273 | * @var bool Whether the index policy has been set to 'index'. |
274 | */ |
275 | private $mIndexSet = false; |
276 | |
277 | /** |
278 | * @var bool Whether the index policy has been set to 'noindex'. |
279 | */ |
280 | private $mNoIndexSet = false; |
281 | |
282 | /** |
283 | * @var array extra data used by extensions. |
284 | */ |
285 | private $mExtensionData = []; |
286 | |
287 | /** |
288 | * @var array Parser limit report data. |
289 | */ |
290 | private $mLimitReportData = []; |
291 | |
292 | /** @var array Parser limit report data for JSON */ |
293 | private $mLimitReportJSData = []; |
294 | |
295 | /** @var string Debug message added by ParserCache */ |
296 | private $mCacheMessage = ''; |
297 | |
298 | /** |
299 | * @var array Timestamps for getTimeSinceStart(). |
300 | */ |
301 | private $mParseStartTime = []; |
302 | |
303 | /** |
304 | * @var array Durations for getTimeProfile(). |
305 | */ |
306 | private $mTimeProfile = []; |
307 | |
308 | /** |
309 | * @var bool Whether to emit X-Frame-Options: DENY. |
310 | */ |
311 | private $mPreventClickjacking = false; |
312 | |
313 | /** |
314 | * @var string[] Extra script-src for CSP |
315 | */ |
316 | private $mExtraScriptSrcs = []; |
317 | |
318 | /** |
319 | * @var string[] Extra default-src for CSP [Everything but script and style] |
320 | */ |
321 | private $mExtraDefaultSrcs = []; |
322 | |
323 | /** |
324 | * @var string[] Extra style-src for CSP |
325 | */ |
326 | private $mExtraStyleSrcs = []; |
327 | |
328 | /** |
329 | * @var array<string,true> Generic flags. |
330 | */ |
331 | private $mFlags = []; |
332 | |
333 | /** @var string[] */ |
334 | private const SPECULATIVE_FIELDS = [ |
335 | 'speculativePageIdUsed', |
336 | 'mSpeculativeRevId', |
337 | 'revisionTimestampUsed', |
338 | ]; |
339 | |
340 | /** @var int|null Assumed rev ID for {{REVISIONID}} if no revision is set */ |
341 | private $mSpeculativeRevId; |
342 | /** @var int|null Assumed page ID for {{PAGEID}} if no revision is set */ |
343 | private $speculativePageIdUsed; |
344 | /** @var string|null Assumed rev timestamp for {{REVISIONTIMESTAMP}} if no revision is set */ |
345 | private $revisionTimestampUsed; |
346 | |
347 | /** @var string|null SHA-1 base 36 hash of any self-transclusion */ |
348 | private $revisionUsedSha1Base36; |
349 | |
350 | /** string CSS classes to use for the wrapping div, stored in the array keys. |
351 | * If no class is given, no wrapper is added. |
352 | */ |
353 | private $mWrapperDivClasses = []; |
354 | |
355 | /** @var int Upper bound of expiry based on parse duration */ |
356 | private $mMaxAdaptiveExpiry = INF; |
357 | |
358 | // finalizeAdaptiveCacheExpiry() uses TTL = MAX( m * PARSE_TIME + b, MIN_AR_TTL) |
359 | // Current values imply that m=3933.333333 and b=-333.333333 |
360 | // See https://www.nngroup.com/articles/website-response-times/ |
361 | private const PARSE_FAST_SEC = 0.100; // perceived "fast" page parse |
362 | private const PARSE_SLOW_SEC = 1.0; // perceived "slow" page parse |
363 | private const FAST_AR_TTL = 60; // adaptive TTL for "fast" pages |
364 | private const SLOW_AR_TTL = 3600; // adaptive TTL for "slow" pages |
365 | private const MIN_AR_TTL = 15; // min adaptive TTL (for pool counter, and edit stashing) |
366 | |
367 | /** |
368 | * @param string|null $text HTML. Use null to indicate that this ParserOutput contains only |
369 | * meta-data, and the HTML output is undetermined, as opposed to empty. Passing null |
370 | * here causes hasText() to return false. In 1.39 the default value changed from '' |
371 | * to null. |
372 | * @param array $languageLinks |
373 | * @param array $categoryLinks |
374 | * @param bool $unused |
375 | * @param string $titletext |
376 | */ |
377 | public function __construct( $text = null, $languageLinks = [], $categoryLinks = [], |
378 | $unused = false, $titletext = '' |
379 | ) { |
380 | $this->mRawText = $text; |
381 | $this->mLanguageLinks = $languageLinks; |
382 | $this->mCategories = $categoryLinks; |
383 | $this->mTitleText = $titletext; |
384 | } |
385 | |
386 | /** |
387 | * Returns true if text was passed to the constructor, or set using setText(). Returns false |
388 | * if null was passed to the $text parameter of the constructor to indicate that this |
389 | * ParserOutput only contains meta-data, and the HTML output is undetermined. |
390 | * |
391 | * @since 1.32 |
392 | * |
393 | * @return bool Whether this ParserOutput contains rendered text. If this returns false, the |
394 | * ParserOutput contains meta-data only. |
395 | */ |
396 | public function hasText(): bool { |
397 | return ( $this->mRawText !== null ); |
398 | } |
399 | |
400 | /** |
401 | * Get the cacheable text with <mw:editsection> markers still in it. The |
402 | * return value is suitable for writing back via setText() but is not valid |
403 | * for display to the user. |
404 | * |
405 | * @return string |
406 | * @since 1.27 |
407 | */ |
408 | public function getRawText() { |
409 | if ( $this->mRawText === null ) { |
410 | throw new LogicException( 'This ParserOutput contains no text!' ); |
411 | } |
412 | |
413 | return $this->mRawText; |
414 | } |
415 | |
416 | /** |
417 | * Get the output HTML |
418 | * |
419 | * T293512: in the future, ParserOutput::getText() will be deprecated in favor of invoking the |
420 | * ParserOutputTransform pipeline directly on a ParserOutput. |
421 | * @param array $options (since 1.31) Transformations to apply to the HTML |
422 | * - allowTOC: (bool) Show the TOC, assuming there were enough headings |
423 | * to generate one and `__NOTOC__` wasn't used. Default is true, |
424 | * but might be statefully overridden. |
425 | * - injectTOC: (bool) Replace the TOC_PLACEHOLDER with TOC contents; |
426 | * otherwise the marker will be left in the article (and the skin |
427 | * will be responsible for replacing or removing it). Default is |
428 | * true. |
429 | * - enableSectionEditLinks: (bool) Include section edit links, assuming |
430 | * section edit link tokens are present in the HTML. Default is true, |
431 | * but might be statefully overridden. |
432 | * - userLang: (Language) Language object used for localizing UX messages, |
433 | * for example the heading of the table of contents. If omitted, will |
434 | * use the language of the main request context. |
435 | * - skin: (Skin) Skin object used for transforming section edit links. |
436 | * - unwrap: (bool) Return text without a wrapper div. Default is false, |
437 | * meaning a wrapper div will be added if getWrapperDivClass() returns |
438 | * a non-empty string. |
439 | * - wrapperDivClass: (string) Wrap the output in a div and apply the given |
440 | * CSS class to that div. This overrides the output of getWrapperDivClass(). |
441 | * Setting this to an empty string has the same effect as 'unwrap' => true. |
442 | * - deduplicateStyles: (bool) When true, which is the default, `<style>` |
443 | * tags with the `data-mw-deduplicate` attribute set are deduplicated by |
444 | * value of the attribute: all but the first will be replaced by `<link |
445 | * rel="mw-deduplicated-inline-style" href="mw-data:..."/>` tags, where |
446 | * the scheme-specific-part of the href is the (percent-encoded) value |
447 | * of the `data-mw-deduplicate` attribute. |
448 | * - absoluteURLs: (bool) use absolute URLs in all links. Default: false |
449 | * - includeDebugInfo: (bool) render PP limit report in HTML. Default: false |
450 | * @return string HTML |
451 | * @return-taint escaped |
452 | * @deprecated since 1.42, this method has side-effects on the ParserOutput |
453 | * (see T353257) and so should be avoided in favor of directly invoking |
454 | * the default output pipeline on a ParserOutput. |
455 | */ |
456 | public function getText( $options = [] ) { |
457 | $pipeline = MediaWikiServices::getInstance()->getDefaultOutputPipeline(); |
458 | $oldText = $this->mRawText; // T353257 |
459 | $options += [ |
460 | 'suppressClone' => true, // T353257 |
461 | 'allowTOC' => true, |
462 | 'injectTOC' => true, |
463 | 'enableSectionEditLinks' => !$this->getOutputFlag( ParserOutputFlags::NO_SECTION_EDIT_LINKS ), |
464 | 'userLang' => null, |
465 | 'skin' => null, |
466 | 'unwrap' => false, |
467 | 'wrapperDivClass' => $this->getWrapperDivClass(), |
468 | 'deduplicateStyles' => true, |
469 | 'absoluteURLs' => false, |
470 | 'includeDebugInfo' => false, |
471 | 'isParsoidContent' => PageBundleParserOutputConverter::hasPageBundle( $this ), |
472 | ]; |
473 | $po = $pipeline->run( $this, null, $options ); |
474 | $newText = $po->getContentHolderText(); |
475 | // T353257: for back-compat only mutations to metadata performed by |
476 | // the pipeline should be preserved; mutations to $mText should be |
477 | // discarded. |
478 | $this->setRawText( $oldText ); |
479 | return $newText; |
480 | } |
481 | |
482 | /** |
483 | * Adds a comment notice about cache state to the text of the page |
484 | * @param string $msg |
485 | * @internal used by ParserCache |
486 | */ |
487 | public function addCacheMessage( string $msg ): void { |
488 | $this->mCacheMessage .= $msg; |
489 | } |
490 | |
491 | /** |
492 | * Add a CSS class to use for the wrapping div. If no class is given, no wrapper is added. |
493 | * |
494 | * @param string $class |
495 | */ |
496 | public function addWrapperDivClass( $class ): void { |
497 | $this->mWrapperDivClasses[$class] = true; |
498 | } |
499 | |
500 | /** |
501 | * Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div |
502 | * until addWrapperDivClass() is called. |
503 | */ |
504 | public function clearWrapperDivClass(): void { |
505 | $this->mWrapperDivClasses = []; |
506 | } |
507 | |
508 | /** |
509 | * Returns the class (or classes) to be used with the wrapper div for this output. |
510 | * If there is no wrapper class given, no wrapper div should be added. |
511 | * The wrapper div is added automatically by getText(). |
512 | * |
513 | * @return string |
514 | */ |
515 | public function getWrapperDivClass(): string { |
516 | return implode( ' ', array_keys( $this->mWrapperDivClasses ) ); |
517 | } |
518 | |
519 | /** |
520 | * @param int $id |
521 | * @since 1.28 |
522 | */ |
523 | public function setSpeculativeRevIdUsed( $id ): void { |
524 | $this->mSpeculativeRevId = $id; |
525 | } |
526 | |
527 | /** |
528 | * @return int|null |
529 | * @since 1.28 |
530 | */ |
531 | public function getSpeculativeRevIdUsed(): ?int { |
532 | return $this->mSpeculativeRevId; |
533 | } |
534 | |
535 | /** |
536 | * @param int $id |
537 | * @since 1.34 |
538 | */ |
539 | public function setSpeculativePageIdUsed( $id ): void { |
540 | $this->speculativePageIdUsed = $id; |
541 | } |
542 | |
543 | /** |
544 | * @return int|null |
545 | * @since 1.34 |
546 | */ |
547 | public function getSpeculativePageIdUsed() { |
548 | return $this->speculativePageIdUsed; |
549 | } |
550 | |
551 | /** |
552 | * @param string $timestamp TS_MW timestamp |
553 | * @since 1.34 |
554 | */ |
555 | public function setRevisionTimestampUsed( $timestamp ): void { |
556 | $this->revisionTimestampUsed = $timestamp; |
557 | } |
558 | |
559 | /** |
560 | * @return string|null TS_MW timestamp or null if not used |
561 | * @since 1.34 |
562 | */ |
563 | public function getRevisionTimestampUsed() { |
564 | return $this->revisionTimestampUsed; |
565 | } |
566 | |
567 | /** |
568 | * @param string $hash Lowercase SHA-1 base 36 hash |
569 | * @since 1.34 |
570 | */ |
571 | public function setRevisionUsedSha1Base36( $hash ): void { |
572 | if ( $hash === null ) { |
573 | return; // e.g. RevisionRecord::getSha1() returned null |
574 | } |
575 | |
576 | if ( |
577 | $this->revisionUsedSha1Base36 !== null && |
578 | $this->revisionUsedSha1Base36 !== $hash |
579 | ) { |
580 | $this->revisionUsedSha1Base36 = ''; // mismatched |
581 | } else { |
582 | $this->revisionUsedSha1Base36 = $hash; |
583 | } |
584 | } |
585 | |
586 | /** |
587 | * @return string|null Lowercase SHA-1 base 36 hash, null if unused, or "" on inconsistency |
588 | * @since 1.34 |
589 | */ |
590 | public function getRevisionUsedSha1Base36() { |
591 | return $this->revisionUsedSha1Base36; |
592 | } |
593 | |
594 | public function &getLanguageLinks() { |
595 | return $this->mLanguageLinks; |
596 | } |
597 | |
598 | public function getInterwikiLinks() { |
599 | return $this->mInterwikiLinks; |
600 | } |
601 | |
602 | /** |
603 | * Return the names of the categories on this page. |
604 | * Unlike ::getCategories(), sort keys are *not* included in the |
605 | * return value. |
606 | * @return array<string> The names of the categories |
607 | * @since 1.38 |
608 | */ |
609 | public function getCategoryNames(): array { |
610 | # Note that numeric category names get converted to 'int' when |
611 | # stored as array keys; stringify the keys to ensure they |
612 | # return to original string form so as not to confuse callers. |
613 | return array_map( 'strval', array_keys( $this->mCategories ) ); |
614 | } |
615 | |
616 | /** |
617 | * Return category names and sort keys as a map. |
618 | * |
619 | * BEWARE that numeric category names get converted to 'int' when stored |
620 | * as array keys. Because of this, use of this method is not recommended |
621 | * in new code; using ::getCategoryNames() and ::getCategorySortKey() will |
622 | * be less error-prone. |
623 | * |
624 | * @return array<string|int,string> |
625 | * @internal |
626 | */ |
627 | public function getCategoryMap(): array { |
628 | return $this->mCategories; |
629 | } |
630 | |
631 | /** |
632 | * Return the sort key for a given category name, or `null` if the |
633 | * category is not present in this ParserOutput. Returns the |
634 | * empty string if the category is to use the default sort key. |
635 | * |
636 | * @note The effective sort key in the database may vary from what |
637 | * is returned here; see note in ParserOutput::addCategory(). |
638 | * |
639 | * @param string $name The category name |
640 | * @return ?string The sort key for the category, or `null` if the |
641 | * category is not present in this ParserOutput |
642 | * @since 1.40 |
643 | */ |
644 | public function getCategorySortKey( string $name ): ?string { |
645 | // This API avoids exposing the fact that numeric string category |
646 | // names are going to be converted to 'int' when used as array |
647 | // keys for the `mCategories` field. |
648 | return $this->mCategories[$name] ?? null; |
649 | } |
650 | |
651 | /** |
652 | * @return string[] |
653 | * @since 1.25 |
654 | */ |
655 | public function getIndicators(): array { |
656 | return $this->mIndicators; |
657 | } |
658 | |
659 | public function getTitleText() { |
660 | return $this->mTitleText; |
661 | } |
662 | |
663 | /** |
664 | * @return ?TOCData the table of contents data, or null if it hasn't been |
665 | * set. |
666 | */ |
667 | public function getTOCData(): ?TOCData { |
668 | return $this->mTOCData; |
669 | } |
670 | |
671 | /** |
672 | * @internal |
673 | * @return string |
674 | */ |
675 | public function getCacheMessage(): string { |
676 | return $this->mCacheMessage; |
677 | } |
678 | |
679 | /** |
680 | * @internal |
681 | * @return array |
682 | */ |
683 | public function getSections(): array { |
684 | if ( $this->mTOCData !== null ) { |
685 | return $this->mTOCData->toLegacy(); |
686 | } |
687 | // For compatibility |
688 | return []; |
689 | } |
690 | |
691 | public function &getLinks() { |
692 | return $this->mLinks; |
693 | } |
694 | |
695 | /** |
696 | * @return array Keys are DBKs for the links to special pages in the document |
697 | * @since 1.35 |
698 | */ |
699 | public function &getLinksSpecial() { |
700 | return $this->mLinksSpecial; |
701 | } |
702 | |
703 | public function &getTemplates() { |
704 | return $this->mTemplates; |
705 | } |
706 | |
707 | public function &getTemplateIds() { |
708 | return $this->mTemplateIds; |
709 | } |
710 | |
711 | public function &getImages() { |
712 | return $this->mImages; |
713 | } |
714 | |
715 | public function &getFileSearchOptions() { |
716 | return $this->mFileSearchOptions; |
717 | } |
718 | |
719 | public function &getExternalLinks() { |
720 | return $this->mExternalLinks; |
721 | } |
722 | |
723 | public function setNoGallery( $value ): void { |
724 | $this->mNoGallery = (bool)$value; |
725 | } |
726 | |
727 | public function getNoGallery() { |
728 | return $this->mNoGallery; |
729 | } |
730 | |
731 | public function getHeadItems() { |
732 | return $this->mHeadItems; |
733 | } |
734 | |
735 | public function getModules() { |
736 | return array_keys( $this->mModuleSet ); |
737 | } |
738 | |
739 | public function getModuleStyles() { |
740 | return array_keys( $this->mModuleStyleSet ); |
741 | } |
742 | |
743 | /** |
744 | * @param bool $showStrategyKeys Defaults to false; if set to true will |
745 | * expose the internal `MW_MERGE_STRATEGY_KEY` in the result. This |
746 | * should only be used internally to allow safe merge of config vars. |
747 | * @return array |
748 | * @since 1.23 |
749 | */ |
750 | public function getJsConfigVars( bool $showStrategyKeys = false ) { |
751 | $result = $this->mJsConfigVars; |
752 | // Don't expose the internal strategy key |
753 | foreach ( $result as &$value ) { |
754 | if ( is_array( $value ) && !$showStrategyKeys ) { |
755 | unset( $value[self::MW_MERGE_STRATEGY_KEY] ); |
756 | } |
757 | } |
758 | return $result; |
759 | } |
760 | |
761 | public function getWarnings(): array { |
762 | return array_keys( $this->mWarnings ); |
763 | } |
764 | |
765 | public function getIndexPolicy(): string { |
766 | // 'noindex' wins if both are set. (T16899) |
767 | if ( $this->mNoIndexSet ) { |
768 | return 'noindex'; |
769 | } elseif ( $this->mIndexSet ) { |
770 | return 'index'; |
771 | } |
772 | return ''; |
773 | } |
774 | |
775 | /** |
776 | * @return string|null TS_MW timestamp of the revision content |
777 | */ |
778 | public function getRevisionTimestamp(): ?string { |
779 | return $this->mTimestamp; |
780 | } |
781 | |
782 | /** |
783 | * @return string|null TS_MW timestamp of the revision content |
784 | * @deprecated since 1.42; use ::getRevisionTimestamp() instead |
785 | */ |
786 | public function getTimestamp() { |
787 | return $this->getRevisionTimestamp(); |
788 | } |
789 | |
790 | public function getLimitReportData() { |
791 | return $this->mLimitReportData; |
792 | } |
793 | |
794 | public function getLimitReportJSData() { |
795 | return $this->mLimitReportJSData; |
796 | } |
797 | |
798 | public function getEnableOOUI() { |
799 | return $this->mEnableOOUI; |
800 | } |
801 | |
802 | /** |
803 | * Get extra Content-Security-Policy 'default-src' directives |
804 | * @since 1.35 |
805 | * @return string[] |
806 | */ |
807 | public function getExtraCSPDefaultSrcs() { |
808 | return $this->mExtraDefaultSrcs; |
809 | } |
810 | |
811 | /** |
812 | * Get extra Content-Security-Policy 'script-src' directives |
813 | * @since 1.35 |
814 | * @return string[] |
815 | */ |
816 | public function getExtraCSPScriptSrcs() { |
817 | return $this->mExtraScriptSrcs; |
818 | } |
819 | |
820 | /** |
821 | * Get extra Content-Security-Policy 'style-src' directives |
822 | * @since 1.35 |
823 | * @return string[] |
824 | */ |
825 | public function getExtraCSPStyleSrcs() { |
826 | return $this->mExtraStyleSrcs; |
827 | } |
828 | |
829 | /** |
830 | * Set the raw text of the ParserOutput. |
831 | * |
832 | * If you did not generate html, pass null to mark it as such. |
833 | * |
834 | * @since 1.42 |
835 | * @param string|null $text HTML content of ParserOutput or null if not generated |
836 | * @param-taint $text exec_html |
837 | */ |
838 | public function setRawText( ?string $text ): void { |
839 | $this->mRawText = $text; |
840 | } |
841 | |
842 | /** |
843 | * Set the raw text of the ParserOutput. |
844 | * |
845 | * If you did not generate html, pass null to mark it as such. |
846 | * |
847 | * @since 1.39 You can now pass null to this function |
848 | * @param string|null $text HTML content of ParserOutput or null if not generated |
849 | * @param-taint $text exec_html |
850 | * @return string|null Previous value of ParserOutput's raw text |
851 | * @deprecated since 1.42; use ::setRawText() which matches the getter ::getRawText() |
852 | */ |
853 | public function setText( $text ) { |
854 | return wfSetVar( $this->mRawText, $text, true ); |
855 | } |
856 | |
857 | /** |
858 | * @deprecated since 1.42, use ::addLanguageLink() instead. |
859 | */ |
860 | public function setLanguageLinks( $ll ) { |
861 | return wfSetVar( $this->mLanguageLinks, $ll ); |
862 | } |
863 | |
864 | public function setTitleText( $t ) { |
865 | return wfSetVar( $this->mTitleText, $t ); |
866 | } |
867 | |
868 | /** |
869 | * @param TOCData $tocData Table of contents data for the page |
870 | */ |
871 | public function setTOCData( TOCData $tocData ): void { |
872 | $this->mTOCData = $tocData; |
873 | } |
874 | |
875 | /** |
876 | * @param array $sectionArray |
877 | * @return array Previous value of ::getSections() |
878 | */ |
879 | public function setSections( array $sectionArray ) { |
880 | $oldValue = $this->getSections(); |
881 | $this->setTOCData( TOCData::fromLegacy( $sectionArray ) ); |
882 | return $oldValue; |
883 | } |
884 | |
885 | public function setIndexPolicy( $policy ): string { |
886 | $old = $this->getIndexPolicy(); |
887 | if ( $policy === 'noindex' ) { |
888 | $this->mNoIndexSet = true; |
889 | } elseif ( $policy === 'index' ) { |
890 | $this->mIndexSet = true; |
891 | } |
892 | return $old; |
893 | } |
894 | |
895 | /** |
896 | * @param ?string $timestamp TS_MW timestamp of the revision content |
897 | */ |
898 | public function setRevisionTimestamp( ?string $timestamp ): void { |
899 | $this->mTimestamp = $timestamp; |
900 | } |
901 | |
902 | /** |
903 | * @param ?string $timestamp TS_MW timestamp of the revision content |
904 | * |
905 | * @return ?string The previous value of the timestamp |
906 | * @deprecated since 1.42; use ::setRevisionTimestamp() instead |
907 | */ |
908 | public function setTimestamp( $timestamp ) { |
909 | return wfSetVar( $this->mTimestamp, $timestamp ); |
910 | } |
911 | |
912 | /** |
913 | * Add a category. |
914 | * |
915 | * Although ParserOutput::getCategorySortKey() will return exactly |
916 | * the sort key you specify here, before storing in the database |
917 | * all sort keys will be language converted, HTML entities will be |
918 | * decoded, newlines stripped, and then they will be truncated to |
919 | * 255 bytes. Thus the "effective" sort key in the DB may be different |
920 | * from what is passed to `$sort` here and returned by |
921 | * ::getCategorySortKey(). |
922 | * |
923 | * @param string|ParsoidLinkTarget $c The category name |
924 | * @param string $sort The sort key; an empty string indicates |
925 | * that the default sort key for the page should be used. |
926 | */ |
927 | public function addCategory( $c, $sort = '' ): void { |
928 | if ( $c instanceof ParsoidLinkTarget ) { |
929 | $c = $c->getDBkey(); |
930 | } |
931 | $this->mCategories[$c] = $sort; |
932 | } |
933 | |
934 | /** |
935 | * Overwrite the category map. |
936 | * @param array<string,string> $c Map of category names to sort keys |
937 | * @since 1.38 |
938 | */ |
939 | public function setCategories( array $c ): void { |
940 | $this->mCategories = $c; |
941 | } |
942 | |
943 | /** |
944 | * @param string $id |
945 | * @param string $content |
946 | * @param-taint $content exec_html |
947 | * @since 1.25 |
948 | */ |
949 | public function setIndicator( $id, $content ): void { |
950 | $this->mIndicators[$id] = $content; |
951 | } |
952 | |
953 | /** |
954 | * Enables OOUI, if true, in any OutputPage instance this ParserOutput |
955 | * object is added to. |
956 | * |
957 | * @since 1.26 |
958 | * @param bool $enable If OOUI should be enabled or not |
959 | */ |
960 | public function setEnableOOUI( bool $enable = false ): void { |
961 | $this->mEnableOOUI = $enable; |
962 | } |
963 | |
964 | /** |
965 | * Add a language link. |
966 | * @param ParsoidLinkTarget|string $t |
967 | */ |
968 | public function addLanguageLink( $t ): void { |
969 | if ( $t instanceof ParsoidLinkTarget ) { |
970 | // language links are unusual in using 'text' rather than 'db key' |
971 | // T296019: This should be made more efficient so we don't need |
972 | // a full title lookup. |
973 | $t = Title::newfromLinkTarget( $t )->getFullText(); |
974 | } |
975 | $this->mLanguageLinks[] = $t; |
976 | } |
977 | |
978 | /** |
979 | * Add a warning to the output for this page. |
980 | * @param string $msg The localization message key for the warning |
981 | * @param mixed|JsonUnserializable ...$args Optional arguments for the |
982 | * message. These arguments must be serializable/unserializable with |
983 | * JsonCodec; see the @note on ParserOutput::setExtensionData() |
984 | * @since 1.38 |
985 | */ |
986 | public function addWarningMsg( string $msg, ...$args ): void { |
987 | // preserve original arguments in $mWarningMsgs to allow merge |
988 | // @todo: these aren't serialized/unserialized yet -- before we |
989 | // turn on serialization of $this->mWarningMsgs we need to ensure |
990 | // callers aren't passing nonserializable arguments: T343048. |
991 | $jsonCodec = MediaWikiServices::getInstance()->getJsonCodec(); |
992 | $path = $jsonCodec->detectNonSerializableData( $args, true ); |
993 | if ( $path !== null ) { |
994 | wfDeprecatedMsg( |
995 | "ParserOutput::addWarningMsg() called with nonserializable arguments: $path", |
996 | '1.41' |
997 | ); |
998 | } |
999 | $this->mWarningMsgs[$msg] = $args; |
1000 | $s = wfMessage( $msg, ...$args ) |
1001 | // some callers set the title here? |
1002 | ->inContentLanguage() // because this ends up in cache |
1003 | ->text(); |
1004 | $this->mWarnings[$s] = 1; |
1005 | } |
1006 | |
1007 | public function setNewSection( $value ): void { |
1008 | $this->mNewSection = (bool)$value; |
1009 | } |
1010 | |
1011 | /** |
1012 | * @param bool $value Hide the new section link? |
1013 | */ |
1014 | public function setHideNewSection( bool $value ): void { |
1015 | $this->mHideNewSection = $value; |
1016 | } |
1017 | |
1018 | public function getHideNewSection(): bool { |
1019 | return (bool)$this->mHideNewSection; |
1020 | } |
1021 | |
1022 | public function getNewSection(): bool { |
1023 | return (bool)$this->mNewSection; |
1024 | } |
1025 | |
1026 | /** |
1027 | * Checks, if a url is pointing to the own server |
1028 | * |
1029 | * @param string $internal The server to check against |
1030 | * @param string $url The url to check |
1031 | * @return bool |
1032 | * @internal |
1033 | */ |
1034 | public static function isLinkInternal( $internal, $url ): bool { |
1035 | return (bool)preg_match( '/^' . |
1036 | # If server is proto relative, check also for http/https links |
1037 | ( substr( $internal, 0, 2 ) === '//' ? '(?:https?:)?' : '' ) . |
1038 | preg_quote( $internal, '/' ) . |
1039 | # check for query/path/anchor or end of link in each case |
1040 | '(?:[\?\/\#]|$)/i', |
1041 | $url |
1042 | ); |
1043 | } |
1044 | |
1045 | public function addExternalLink( $url ): void { |
1046 | # We don't register links pointing to our own server, unless... :-) |
1047 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
1048 | $server = $config->get( MainConfigNames::Server ); |
1049 | $registerInternalExternals = $config->get( MainConfigNames::RegisterInternalExternals ); |
1050 | # Replace unnecessary URL escape codes with the referenced character |
1051 | # This prevents spammers from hiding links from the filters |
1052 | $url = Parser::normalizeLinkUrl( $url ); |
1053 | |
1054 | $registerExternalLink = true; |
1055 | if ( !$registerInternalExternals ) { |
1056 | $registerExternalLink = !self::isLinkInternal( $server, $url ); |
1057 | } |
1058 | if ( $registerExternalLink ) { |
1059 | $this->mExternalLinks[$url] = 1; |
1060 | } |
1061 | } |
1062 | |
1063 | /** |
1064 | * Record a local or interwiki inline link for saving in future link tables. |
1065 | * |
1066 | * @param ParsoidLinkTarget $link (used to require Title until 1.38) |
1067 | * @param int|null $id Optional known page_id so we can skip the lookup |
1068 | */ |
1069 | public function addLink( ParsoidLinkTarget $link, $id = null ): void { |
1070 | if ( $link->isExternal() ) { |
1071 | // Don't record interwikis in pagelinks |
1072 | $this->addInterwikiLink( $link ); |
1073 | return; |
1074 | } |
1075 | $ns = $link->getNamespace(); |
1076 | $dbk = $link->getDBkey(); |
1077 | if ( $ns === NS_MEDIA ) { |
1078 | // Normalize this pseudo-alias if it makes it down here... |
1079 | $ns = NS_FILE; |
1080 | } elseif ( $ns === NS_SPECIAL ) { |
1081 | // We don't want to record Special: links in the database, so put them in a separate place. |
1082 | // It might actually be wise to, but we'd need to do some normalization. |
1083 | $this->mLinksSpecial[$dbk] = 1; |
1084 | return; |
1085 | } elseif ( $dbk === '' ) { |
1086 | // Don't record self links - [[#Foo]] |
1087 | return; |
1088 | } |
1089 | if ( $id === null ) { |
1090 | // T357048: This actually kills performance; we should batch these. |
1091 | $page = MediaWikiServices::getInstance()->getPageStore()->getPageForLink( $link ); |
1092 | $id = $page->getId(); |
1093 | } |
1094 | $this->mLinks[$ns][$dbk] = $id; |
1095 | } |
1096 | |
1097 | /** |
1098 | * Register a file dependency for this output |
1099 | * @param string|ParsoidLinkTarget $name Title dbKey |
1100 | * @param string|false|null $timestamp MW timestamp of file creation (or false if non-existing) |
1101 | * @param string|false|null $sha1 Base 36 SHA-1 of file (or false if non-existing) |
1102 | */ |
1103 | public function addImage( $name, $timestamp = null, $sha1 = null ): void { |
1104 | if ( $name instanceof ParsoidLinkTarget ) { |
1105 | $name = $name->getDBkey(); |
1106 | } |
1107 | $this->mImages[$name] = 1; |
1108 | if ( $timestamp !== null && $sha1 !== null ) { |
1109 | $this->mFileSearchOptions[$name] = [ 'time' => $timestamp, 'sha1' => $sha1 ]; |
1110 | } |
1111 | } |
1112 | |
1113 | /** |
1114 | * Register a template dependency for this output |
1115 | * |
1116 | * @param ParsoidLinkTarget $link (used to require Title until 1.38) |
1117 | * @param int $page_id |
1118 | * @param int $rev_id |
1119 | */ |
1120 | public function addTemplate( $link, $page_id, $rev_id ): void { |
1121 | if ( $link->isExternal() ) { |
1122 | // Will throw an InvalidArgumentException in a future release. |
1123 | wfDeprecated( __METHOD__ . " with interwiki link", '1.42' ); |
1124 | return; |
1125 | } |
1126 | $ns = $link->getNamespace(); |
1127 | $dbk = $link->getDBkey(); |
1128 | // T357048: Parsoid doesn't have page_id |
1129 | $this->mTemplates[$ns][$dbk] = $page_id; |
1130 | $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning |
1131 | } |
1132 | |
1133 | /** |
1134 | * @param ParsoidLinkTarget $link must be an interwiki link |
1135 | * (used to require Title until 1.38). |
1136 | */ |
1137 | public function addInterwikiLink( $link ): void { |
1138 | if ( !$link->isExternal() ) { |
1139 | throw new InvalidArgumentException( 'Non-interwiki link passed, internal parser error.' ); |
1140 | } |
1141 | $prefix = $link->getInterwiki(); |
1142 | $this->mInterwikiLinks[$prefix][$link->getDBkey()] = 1; |
1143 | } |
1144 | |
1145 | /** |
1146 | * Add some text to the "<head>". |
1147 | * If $tag is set, the section with that tag will only be included once |
1148 | * in a given page. |
1149 | * @param string $section |
1150 | * @param string|false $tag |
1151 | */ |
1152 | public function addHeadItem( $section, $tag = false ): void { |
1153 | if ( $tag !== false ) { |
1154 | $this->mHeadItems[$tag] = $section; |
1155 | } else { |
1156 | $this->mHeadItems[] = $section; |
1157 | } |
1158 | } |
1159 | |
1160 | /** |
1161 | * @see OutputPage::addModules |
1162 | * @param string[] $modules |
1163 | */ |
1164 | public function addModules( array $modules ): void { |
1165 | $modules = array_fill_keys( $modules, true ); |
1166 | $this->mModuleSet = array_merge( $this->mModuleSet, $modules ); |
1167 | } |
1168 | |
1169 | /** |
1170 | * @see OutputPage::addModuleStyles |
1171 | * @param string[] $modules |
1172 | */ |
1173 | public function addModuleStyles( array $modules ): void { |
1174 | $modules = array_fill_keys( $modules, true ); |
1175 | $this->mModuleStyleSet = array_merge( $this->mModuleStyleSet, $modules ); |
1176 | } |
1177 | |
1178 | /** |
1179 | * Add one or more variables to be set in mw.config in JavaScript. |
1180 | * |
1181 | * @param string|array $keys Key or array of key/value pairs. |
1182 | * @param mixed|null $value [optional] Value of the configuration variable. |
1183 | * @since 1.23 |
1184 | * @deprecated since 1.38, use ::setJsConfigVar() or ::appendJsConfigVar() |
1185 | * which ensures compatibility with asynchronous parsing. |
1186 | */ |
1187 | public function addJsConfigVars( $keys, $value = null ): void { |
1188 | if ( is_array( $keys ) ) { |
1189 | foreach ( $keys as $key => $value ) { |
1190 | $this->mJsConfigVars[$key] = $value; |
1191 | } |
1192 | return; |
1193 | } |
1194 | |
1195 | $this->mJsConfigVars[$keys] = $value; |
1196 | } |
1197 | |
1198 | /** |
1199 | * Add a variable to be set in mw.config in JavaScript. |
1200 | * |
1201 | * In order to ensure the result is independent of the parse order, the values |
1202 | * set here must be unique -- that is, you can pass the same $key |
1203 | * multiple times but ONLY if the $value is identical each time. |
1204 | * If you want to collect multiple pieces of data under a single key, |
1205 | * use ::appendJsConfigVar(). |
1206 | * |
1207 | * @param string $key Key to use under mw.config |
1208 | * @param mixed|null $value Value of the configuration variable. |
1209 | * @since 1.38 |
1210 | */ |
1211 | public function setJsConfigVar( string $key, $value ): void { |
1212 | if ( |
1213 | array_key_exists( $key, $this->mJsConfigVars ) && |
1214 | $this->mJsConfigVars[$key] !== $value |
1215 | ) { |
1216 | // Ensure that a key is mapped to only a single value in order |
1217 | // to prevent the resulting array from varying if content |
1218 | // is parsed in a different order. |
1219 | throw new InvalidArgumentException( "Multiple conflicting values given for $key" ); |
1220 | } |
1221 | $this->mJsConfigVars[$key] = $value; |
1222 | } |
1223 | |
1224 | /** |
1225 | * Append a value to a variable to be set in mw.config in JavaScript. |
1226 | * |
1227 | * In order to ensure the result is independent of the parse order, |
1228 | * the value of this key will be an associative array, mapping all of |
1229 | * the values set under that key to true. (The array is implicitly |
1230 | * ordered in PHP, but you should treat it as unordered.) |
1231 | * If you want a non-array type for the key, and can ensure that only |
1232 | * a single value will be set, you should use ::setJsConfigVar() instead. |
1233 | * |
1234 | * @param string $key Key to use under mw.config |
1235 | * @param string $value Value to append to the configuration variable. |
1236 | * @param string $strategy Merge strategy: |
1237 | * only MW_MERGE_STRATEGY_UNION is currently supported and external callers |
1238 | * should treat this parameter as @internal at this time and omit it. |
1239 | * @since 1.38 |
1240 | */ |
1241 | public function appendJsConfigVar( |
1242 | string $key, |
1243 | string $value, |
1244 | string $strategy = self::MW_MERGE_STRATEGY_UNION |
1245 | ): void { |
1246 | if ( $strategy !== self::MW_MERGE_STRATEGY_UNION ) { |
1247 | throw new InvalidArgumentException( "Unknown merge strategy $strategy." ); |
1248 | } |
1249 | if ( !array_key_exists( $key, $this->mJsConfigVars ) ) { |
1250 | $this->mJsConfigVars[$key] = [ |
1251 | // Indicate how these values are to be merged. |
1252 | self::MW_MERGE_STRATEGY_KEY => $strategy, |
1253 | ]; |
1254 | } elseif ( !is_array( $this->mJsConfigVars[$key] ) ) { |
1255 | throw new InvalidArgumentException( "Mixing set and append for $key" ); |
1256 | } elseif ( ( $this->mJsConfigVars[$key][self::MW_MERGE_STRATEGY_KEY] ?? null ) !== $strategy ) { |
1257 | throw new InvalidArgumentException( "Conflicting merge strategies for $key" ); |
1258 | } |
1259 | $this->mJsConfigVars[$key][$value] = true; |
1260 | } |
1261 | |
1262 | /** |
1263 | * Accommodate very basic transcluding of a temporary OutputPage object into parser output. |
1264 | * |
1265 | * This is a fragile method that cannot be relied upon in any meaningful way. |
1266 | * It exists solely to support the wikitext feature of transcluding a SpecialPage, and |
1267 | * only has to work for that use case to ensure relevant styles are loaded, and that |
1268 | * essential config vars needed between SpecialPage and a JS feature are added. |
1269 | * |
1270 | * This relies on there being no overlap between modules or config vars added by |
1271 | * the SpecialPage and those added by parser extensions. If there is overlap, |
1272 | * then arise and break one or both sides. This is expected and unsupported. |
1273 | * |
1274 | * @internal For use by Parser for basic special page transclusion |
1275 | * @param OutputPage $out |
1276 | */ |
1277 | public function addOutputPageMetadata( OutputPage $out ): void { |
1278 | // This should eventually use the same merge mechanism used |
1279 | // internally to merge ParserOutputs together. |
1280 | |
1281 | // Take the strictest click-jacking policy. This is to ensure any one-click features |
1282 | // such as patrol or rollback on the transcluded special page will result in the wiki page |
1283 | // disallowing embedding in cross-origin iframes. Articles are generally allowed to be |
1284 | // embedded. Pages that transclude special pages are expected to be user pages or |
1285 | // other non-content pages that content re-users won't discover or care about. |
1286 | $this->mPreventClickjacking = $this->mPreventClickjacking || $out->getPreventClickjacking(); |
1287 | |
1288 | $this->addModuleStyles( $out->getModuleStyles() ); |
1289 | |
1290 | // TODO: Figure out if style modules suffice, or whether the below is needed as well. |
1291 | // Are there special pages that permit transcluding/including and also have JS modules |
1292 | // that should be activate on the host page? |
1293 | $this->addModules( $out->getModules() ); |
1294 | $this->mJsConfigVars = self::mergeMapStrategy( |
1295 | $this->mJsConfigVars, $out->getJsConfigVars() |
1296 | ); |
1297 | $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() ); |
1298 | } |
1299 | |
1300 | /** |
1301 | * Override the title to be used for display |
1302 | * |
1303 | * @note this is assumed to have been validated |
1304 | * (check equal normalisation, etc.) |
1305 | * |
1306 | * @note this is expected to be safe HTML, |
1307 | * ready to be served to the client. |
1308 | * |
1309 | * @param string $text Desired title text |
1310 | */ |
1311 | public function setDisplayTitle( $text ): void { |
1312 | $this->setTitleText( $text ); |
1313 | $this->setPageProperty( 'displaytitle', $text ); |
1314 | } |
1315 | |
1316 | /** |
1317 | * Get the title to be used for display. |
1318 | * |
1319 | * As per the contract of setDisplayTitle(), this is safe HTML, |
1320 | * ready to be served to the client. |
1321 | * |
1322 | * @return string|false HTML |
1323 | */ |
1324 | public function getDisplayTitle() { |
1325 | $t = $this->getTitleText(); |
1326 | if ( $t === '' ) { |
1327 | return false; |
1328 | } |
1329 | return $t; |
1330 | } |
1331 | |
1332 | /** |
1333 | * Get the primary language code of the output. |
1334 | * |
1335 | * This returns the primary language of the output, including |
1336 | * any LanguageConverter variant applied. |
1337 | * |
1338 | * NOTE: This may differ from the wiki's default content language |
1339 | * ($wgLanguageCode, MediaWikiServices::getContentLanguage), because |
1340 | * each page may have its own "page language" set (PageStoreRecord, |
1341 | * Title::getDbPageLanguageCode, ContentHandler::getPageLanguage). |
1342 | * |
1343 | * NOTE: This may differ from the "page language" when parsing |
1344 | * user interface messages, in which case this reflects the user |
1345 | * language (including any variant preference). |
1346 | * |
1347 | * NOTE: This may differ from the Parser's "target language" that was |
1348 | * set while the Parser was parsing the page, because the final output |
1349 | * is converted to the current user's preferred LanguageConverter variant |
1350 | * (assuming this is a variant of the target language). |
1351 | * See Parser::getTargetLanguageConverter()->getPreferredVariant(); use |
1352 | * LanguageFactory::getParentLanguage() on the language code to obtain |
1353 | * the base language code. LanguageConverter::getPreferredVariant() |
1354 | * depends on the global RequestContext for the URL and the User |
1355 | * language preference. |
1356 | * |
1357 | * Finally, note that a single ParserOutput object may contain |
1358 | * HTML content in multiple different languages and directions |
1359 | * (T114640). Authors of wikitext and of parser extensions are |
1360 | * expected to mark such subtrees with a `lang` attribute (set to |
1361 | * a BCP-47 value, see Language::toBcp47Code()) and a corresponding |
1362 | * `dir` attribute (see Language::getDir()). This method returns |
1363 | * the language code for wrapper of the HTML content. |
1364 | * |
1365 | * @see Parser::internalParseHalfParsed |
1366 | * @since 1.40 |
1367 | * @return ?Bcp47Code The primary language for this output, |
1368 | * or `null` if a language was not set. |
1369 | */ |
1370 | public function getLanguage(): ?Bcp47Code { |
1371 | // This information is temporarily stored in extension data (T303329) |
1372 | $code = $this->getExtensionData( 'core:target-lang-variant' ); |
1373 | // This is null if the ParserOutput was cached by MW 1.40 or earlier, |
1374 | // or not constructed by Parser/ParserCache. |
1375 | return $code === null ? null : new Bcp47CodeValue( $code ); |
1376 | } |
1377 | |
1378 | /** |
1379 | * Set the primary language of the output. |
1380 | * |
1381 | * See the discussion and caveats in ::getLanguage(). |
1382 | * |
1383 | * @param Bcp47Code $lang The primary language for this output, including |
1384 | * any variant specification. |
1385 | * @since 1.40 |
1386 | */ |
1387 | public function setLanguage( Bcp47Code $lang ): void { |
1388 | $this->setExtensionData( 'core:target-lang-variant', $lang->toBcp47Code() ); |
1389 | } |
1390 | |
1391 | /** |
1392 | * Return an HTML prefix to be applied on redirect pages, or null |
1393 | * if this is not a redirect. |
1394 | * @return ?string HTML to prepend to redirect pages, or null |
1395 | * @internal |
1396 | */ |
1397 | public function getRedirectHeader(): ?string { |
1398 | return $this->getExtensionData( 'core:redirect-header' ); |
1399 | } |
1400 | |
1401 | /** |
1402 | * Set an HTML prefix to be applied on redirect pages. |
1403 | * @param string $html HTML to prepend to redirect pages |
1404 | */ |
1405 | public function setRedirectHeader( string $html ): void { |
1406 | $this->setExtensionData( 'core:redirect-header', $html ); |
1407 | } |
1408 | |
1409 | /** |
1410 | * Store a unique rendering id for this ParserOutput. This is used |
1411 | * whenever a client needs to record a dependency on a specific parse. |
1412 | * It is typically set only when a parser output is cached. |
1413 | * |
1414 | * @param string $renderId a UUID identifying a specific parse |
1415 | * @internal |
1416 | */ |
1417 | public function setRenderId( string $renderId ): void { |
1418 | $this->setExtensionData( 'core:render-id', $renderId ); |
1419 | } |
1420 | |
1421 | /** |
1422 | * Return the unique rendering id for this ParserOutput. This is used |
1423 | * whenever a client needs to record a dependency on a specific parse. |
1424 | * |
1425 | * @return string|null |
1426 | * @internal |
1427 | */ |
1428 | public function getRenderId(): ?string { |
1429 | // Backward-compatibility with old cache contents |
1430 | // Can be removed after parser cache contents have expired |
1431 | $old = $this->getExtensionData( 'parsoid-render-id' ); |
1432 | if ( $old !== null ) { |
1433 | return ParsoidRenderId::newFromKey( $old )->getUniqueID(); |
1434 | } |
1435 | return $this->getExtensionData( 'core:render-id' ); |
1436 | } |
1437 | |
1438 | /** |
1439 | * @return string[] List of flags signifying special cases |
1440 | * @internal |
1441 | */ |
1442 | public function getAllFlags(): array { |
1443 | return array_keys( $this->mFlags ); |
1444 | } |
1445 | |
1446 | /** |
1447 | * Set a page property to be stored in the page_props database table. |
1448 | * |
1449 | * page_props is a key-value store indexed by the page ID. This allows |
1450 | * the parser to set a property on a page which can then be quickly |
1451 | * retrieved given the page ID or via a DB join when given the page |
1452 | * title. |
1453 | * |
1454 | * Since 1.23, page_props are also indexed by numeric value, to allow |
1455 | * for efficient "top k" queries of pages wrt a given property. |
1456 | * This only works if the value is passed as a int, float, or |
1457 | * bool. Since 1.42 you should use ::setNumericPageProperty() |
1458 | * if you want your page property value to be indexed, which will ensure |
1459 | * that the value is of the proper type. |
1460 | * |
1461 | * setPageProperty() is thus used to propagate properties from the parsed |
1462 | * page to request contexts other than a page view of the currently parsed |
1463 | * article. |
1464 | * |
1465 | * Some applications examples: |
1466 | * |
1467 | * * To implement hidden categories, hiding pages from category listings |
1468 | * by storing a page property. |
1469 | * |
1470 | * * Overriding the displayed article title (ParserOutput::setDisplayTitle()). |
1471 | * |
1472 | * * To implement image tagging, for example displaying an icon on an |
1473 | * image thumbnail to indicate that it is listed for deletion on |
1474 | * Wikimedia Commons. |
1475 | * This is not actually implemented, yet but would be pretty cool. |
1476 | * |
1477 | * @note Use of non-scalar values (anything other than |
1478 | * `string|int|float|bool`) has been deprecated in 1.42. |
1479 | * Although any JSON-serializable value can be stored/fetched in |
1480 | * ParserOutput, when the values are stored to the database |
1481 | * (in `deferred/LinksUpdate/PagePropsTable.php`) they will be |
1482 | * converted: booleans will be converted to '0' and '1', null |
1483 | * will become '', and everything else will be cast to string |
1484 | * (not JSON-serialized). Page properties obtained from the |
1485 | * PageProps service will thus always be strings. |
1486 | * |
1487 | * @note The sort key stored in the database *will be NULL* unless |
1488 | * the value passed here is an `int|float|bool`. If you *do not* |
1489 | * want your property *value* indexed and sorted (for example, the |
1490 | * value is a title string which can be numeric but only |
1491 | * incidentally, like when it gets retrieved from an array key) |
1492 | * be sure to cast to string or use |
1493 | * `::setUnsortedPageProperty()`. If you *do* want your property |
1494 | * *value* indexed and sorted, you should use |
1495 | * `::setNumericPageProperty()` instead as this will ensure the |
1496 | * value type is correct. Note that either way it is possible to |
1497 | * efficiently look up all the pages with a certain property; we |
1498 | * are only talking about sorting the *values* assigned to the |
1499 | * property, for example for a "top N values of the property" |
1500 | * query. |
1501 | * |
1502 | * @note Note that `::getPageProperty()`/`::setPageProperty()` do |
1503 | * not do any conversions themselves; you should therefore be |
1504 | * careful to distinguish values returned from the PageProp |
1505 | * service (always strings) from values retrieved from a |
1506 | * ParserOutput. |
1507 | * |
1508 | * @note Do not use setPageProperty() to set a property which is only used |
1509 | * in a context where the ParserOutput object itself is already available, |
1510 | * for example a normal page view. There is no need to save such a property |
1511 | * in the database since the text is already parsed; use |
1512 | * ::setExtensionData() instead. |
1513 | * |
1514 | * @par Example: |
1515 | * @code |
1516 | * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' ); |
1517 | * @endcode |
1518 | * |
1519 | * And then later, in OutputPageParserOutput or similar: |
1520 | * |
1521 | * @par Example: |
1522 | * @code |
1523 | * $output->getExtensionData( 'my_ext_foo' ); |
1524 | * @endcode |
1525 | * |
1526 | * @note The use of `null` as a value is deprecated since 1.42; use |
1527 | * the empty string instead if you need a placeholder value, or |
1528 | * ::unsetPageProperty() if you mean to remove a page property. |
1529 | * |
1530 | * @note The use of non-string values is deprecated since 1.42; if you |
1531 | * need an page property value with a sort index |
1532 | * use ::setNumericPageProperty(). |
1533 | * |
1534 | * @param string $name |
1535 | * @param int|float|string|bool|null $value |
1536 | * @since 1.38 |
1537 | */ |
1538 | public function setPageProperty( string $name, $value ): void { |
1539 | if ( !is_scalar( $value ) ) { |
1540 | wfDeprecated( __METHOD__ . " with non-scalar value for $name" ); |
1541 | } |
1542 | $this->mProperties[$name] = $value; |
1543 | } |
1544 | |
1545 | /** |
1546 | * Set a numeric page property whose *value* is intended to be sorted |
1547 | * and indexed. The sort key used for the property will be the value, |
1548 | * coerced to a number. |
1549 | * |
1550 | * See `::setPageProperty()` for details. |
1551 | * |
1552 | * In the future, we may allow the value to be specified independent |
1553 | * of sort key (T357783). |
1554 | * |
1555 | * @param string $propName The name of the page property |
1556 | * @param int|float|string $numericValue the numeric value |
1557 | * @since 1.42 |
1558 | */ |
1559 | public function setNumericPageProperty( string $propName, $numericValue ): void { |
1560 | if ( !is_numeric( $numericValue ) ) { |
1561 | throw new \TypeError( __METHOD__ . " with non-numeric value" ); |
1562 | } |
1563 | // Coerce numeric sort key to a number. |
1564 | $this->mProperties[$propName] = 0 + $numericValue; |
1565 | } |
1566 | |
1567 | /** |
1568 | * Set a page property whose *value* is not intended to be sorted and |
1569 | * indexed. |
1570 | * |
1571 | * See `::setPageProperty()` for details. It is recommended to |
1572 | * use the empty string if you need a placeholder value (ie, if |
1573 | * it is the *presence* of the property which is important, not |
1574 | * the *value* the property is set to). |
1575 | * |
1576 | * It is still possible to efficiently look up all the pages with |
1577 | * a certain property (the "presence" of it *is* indexed; see |
1578 | * Special:PagesWithProp, list=pageswithprop). |
1579 | * |
1580 | * @param string $propName The name of the page property |
1581 | * @param string $value Optional value; defaults to the empty string. |
1582 | * @since 1.42 |
1583 | */ |
1584 | public function setUnsortedPageProperty( string $propName, string $value = '' ): void { |
1585 | $this->mProperties[$propName] = $value; |
1586 | } |
1587 | |
1588 | /** |
1589 | * Look up a page property. |
1590 | * @param string $name The page property name to look up. |
1591 | * @return int|float|string|bool|null The value previously set using |
1592 | * ::setPageProperty(), ::setUnsortedPageProperty(), or |
1593 | * ::setNumericPageProperty(). |
1594 | * Returns null if no value was set for the given property name. |
1595 | * |
1596 | * @note You would need to use ::getPageProperties() to test for an |
1597 | * explicitly-set null value; but see the note in ::setPageProperty() |
1598 | * deprecating the use of null values. |
1599 | * @since 1.38 |
1600 | */ |
1601 | public function getPageProperty( string $name ) { |
1602 | return $this->mProperties[$name] ?? null; |
1603 | } |
1604 | |
1605 | /** |
1606 | * Remove a page property. |
1607 | * @param string $name The page property name. |
1608 | * @since 1.38 |
1609 | */ |
1610 | public function unsetPageProperty( string $name ): void { |
1611 | unset( $this->mProperties[$name] ); |
1612 | } |
1613 | |
1614 | /** |
1615 | * Return all the page properties set on this ParserOutput. |
1616 | * @return array<string,int|float|string|bool|null> |
1617 | * @since 1.38 |
1618 | */ |
1619 | public function getPageProperties(): array { |
1620 | if ( !isset( $this->mProperties ) ) { |
1621 | $this->mProperties = []; |
1622 | } |
1623 | return $this->mProperties; |
1624 | } |
1625 | |
1626 | /** |
1627 | * Provides a uniform interface to various boolean flags stored |
1628 | * in the ParserOutput. Flags internal to MediaWiki core should |
1629 | * have names which are constants in ParserOutputFlags. Extensions |
1630 | * should use ::setExtensionData() rather than creating new flags |
1631 | * with ::setOutputFlag() in order to prevent namespace conflicts. |
1632 | * |
1633 | * Flags are always combined with OR. That is, the flag is set in |
1634 | * the resulting ParserOutput if the flag is set in *any* of the |
1635 | * fragments composing the ParserOutput. |
1636 | * |
1637 | * @note The combination policy means that a ParserOutput may end |
1638 | * up with both INDEX_POLICY and NO_INDEX_POLICY set. It is |
1639 | * expected that NO_INDEX_POLICY "wins" in that case. (T16899) |
1640 | * (This resolution is implemented in ::getIndexPolicy().) |
1641 | * |
1642 | * @param string $name A flag name |
1643 | * @param bool $val |
1644 | * @since 1.38 |
1645 | */ |
1646 | public function setOutputFlag( string $name, bool $val = true ): void { |
1647 | switch ( $name ) { |
1648 | case ParserOutputFlags::NO_GALLERY: |
1649 | $this->setNoGallery( $val ); |
1650 | break; |
1651 | |
1652 | case ParserOutputFlags::ENABLE_OOUI: |
1653 | $this->setEnableOOUI( $val ); |
1654 | break; |
1655 | |
1656 | case ParserOutputFlags::NO_INDEX_POLICY: |
1657 | $this->mNoIndexSet = $val; |
1658 | break; |
1659 | |
1660 | case ParserOutputFlags::INDEX_POLICY: |
1661 | $this->mIndexSet = $val; |
1662 | break; |
1663 | |
1664 | case ParserOutputFlags::NEW_SECTION: |
1665 | $this->setNewSection( $val ); |
1666 | break; |
1667 | |
1668 | case ParserOutputFlags::HIDE_NEW_SECTION: |
1669 | $this->setHideNewSection( $val ); |
1670 | break; |
1671 | |
1672 | case ParserOutputFlags::PREVENT_CLICKJACKING: |
1673 | $this->setPreventClickjacking( $val ); |
1674 | break; |
1675 | |
1676 | default: |
1677 | if ( $val ) { |
1678 | $this->mFlags[$name] = true; |
1679 | } else { |
1680 | unset( $this->mFlags[$name] ); |
1681 | } |
1682 | break; |
1683 | } |
1684 | } |
1685 | |
1686 | /** |
1687 | * Provides a uniform interface to various boolean flags stored |
1688 | * in the ParserOutput. Flags internal to MediaWiki core should |
1689 | * have names which are constants in ParserOutputFlags. Extensions |
1690 | * should only use ::getOutputFlag() to query flags defined in |
1691 | * ParserOutputFlags in core; they should use ::getExtensionData() |
1692 | * to define their own flags. |
1693 | * |
1694 | * @param string $name A flag name |
1695 | * @return bool The flag value |
1696 | * @since 1.38 |
1697 | */ |
1698 | public function getOutputFlag( string $name ): bool { |
1699 | switch ( $name ) { |
1700 | case ParserOutputFlags::NO_GALLERY: |
1701 | return $this->getNoGallery(); |
1702 | |
1703 | case ParserOutputFlags::ENABLE_OOUI: |
1704 | return $this->getEnableOOUI(); |
1705 | |
1706 | case ParserOutputFlags::INDEX_POLICY: |
1707 | return $this->mIndexSet; |
1708 | |
1709 | case ParserOutputFlags::NO_INDEX_POLICY: |
1710 | return $this->mNoIndexSet; |
1711 | |
1712 | case ParserOutputFlags::NEW_SECTION: |
1713 | return $this->getNewSection(); |
1714 | |
1715 | case ParserOutputFlags::HIDE_NEW_SECTION: |
1716 | return $this->getHideNewSection(); |
1717 | |
1718 | case ParserOutputFlags::PREVENT_CLICKJACKING: |
1719 | return $this->getPreventClickjacking(); |
1720 | |
1721 | default: |
1722 | return isset( $this->mFlags[$name] ); |
1723 | |
1724 | } |
1725 | } |
1726 | |
1727 | /** |
1728 | * Provides a uniform interface to various string sets stored |
1729 | * in the ParserOutput. String sets internal to MediaWiki core should |
1730 | * have names which are constants in ParserOutputStringSets. Extensions |
1731 | * should use ::appendExtensionData() rather than creating new string sets |
1732 | * with ::appendOutputStrings() in order to prevent namespace conflicts. |
1733 | * |
1734 | * @param string $name A string set name |
1735 | * @param string[] $value |
1736 | * @since 1.41 |
1737 | */ |
1738 | public function appendOutputStrings( string $name, array $value ): void { |
1739 | switch ( $name ) { |
1740 | case ParserOutputStringSets::MODULE: |
1741 | $this->addModules( $value ); |
1742 | break; |
1743 | case ParserOutputStringSets::MODULE_STYLE: |
1744 | $this->addModuleStyles( $value ); |
1745 | break; |
1746 | case ParserOutputStringSets::EXTRA_CSP_DEFAULT_SRC: |
1747 | foreach ( $value as $v ) { |
1748 | $this->addExtraCSPDefaultSrc( $v ); |
1749 | } |
1750 | break; |
1751 | case ParserOutputStringSets::EXTRA_CSP_SCRIPT_SRC: |
1752 | foreach ( $value as $v ) { |
1753 | $this->addExtraCSPScriptSrc( $v ); |
1754 | } |
1755 | break; |
1756 | case ParserOutputStringSets::EXTRA_CSP_STYLE_SRC: |
1757 | foreach ( $value as $v ) { |
1758 | $this->addExtraCSPStyleSrc( $v ); |
1759 | } |
1760 | break; |
1761 | default: |
1762 | throw new UnexpectedValueException( "Unknown output string set name $name" ); |
1763 | } |
1764 | } |
1765 | |
1766 | /** |
1767 | * Provides a uniform interface to various boolean string sets stored |
1768 | * in the ParserOutput. String sets internal to MediaWiki core should |
1769 | * have names which are constants in ParserOutputStringSets. Extensions |
1770 | * should only use ::getOutputStrings() to query string sets defined in |
1771 | * ParserOutputStringSets in core; they should use ::appendExtensionData() |
1772 | * to define their own string sets. |
1773 | * |
1774 | * @param string $name A string set name |
1775 | * @return string[] The string set value |
1776 | * @since 1.41 |
1777 | */ |
1778 | public function getOutputStrings( string $name ): array { |
1779 | switch ( $name ) { |
1780 | case ParserOutputStringSets::MODULE: |
1781 | return $this->getModules(); |
1782 | case ParserOutputStringSets::MODULE_STYLE: |
1783 | return $this->getModuleStyles(); |
1784 | case ParserOutputStringSets::EXTRA_CSP_DEFAULT_SRC: |
1785 | return $this->getExtraCSPDefaultSrcs(); |
1786 | case ParserOutputStringSets::EXTRA_CSP_SCRIPT_SRC: |
1787 | return $this->getExtraCSPScriptSrcs(); |
1788 | case ParserOutputStringSets::EXTRA_CSP_STYLE_SRC: |
1789 | return $this->getExtraCSPStyleSrcs(); |
1790 | default: |
1791 | throw new UnexpectedValueException( "Unknown output string set name $name" ); |
1792 | } |
1793 | } |
1794 | |
1795 | /** |
1796 | * Attaches arbitrary data to this ParserObject. This can be used to store some information in |
1797 | * the ParserOutput object for later use during page output. The data will be cached along with |
1798 | * the ParserOutput object, but unlike data set using setPageProperty(), it is not recorded in the |
1799 | * database. |
1800 | * |
1801 | * This method is provided to overcome the unsafe practice of attaching extra information to a |
1802 | * ParserObject by directly assigning member variables. |
1803 | * |
1804 | * To use setExtensionData() to pass extension information from a hook inside the parser to a |
1805 | * hook in the page output, use this in the parser hook: |
1806 | * |
1807 | * @par Example: |
1808 | * @code |
1809 | * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' ); |
1810 | * @endcode |
1811 | * |
1812 | * And then later, in OutputPageParserOutput or similar: |
1813 | * |
1814 | * @par Example: |
1815 | * @code |
1816 | * $output->getExtensionData( 'my_ext_foo' ); |
1817 | * @endcode |
1818 | * |
1819 | * In MediaWiki 1.20 and older, you have to use a custom member variable |
1820 | * within the ParserOutput object: |
1821 | * |
1822 | * @par Example: |
1823 | * @code |
1824 | * $parser->getOutput()->my_ext_foo = '...'; |
1825 | * @endcode |
1826 | * |
1827 | * @note Only scalar values, e.g. numbers, strings, arrays or MediaWiki\Json\JsonUnserializable |
1828 | * instances are supported as a value. Attempt to set other class instance as extension data |
1829 | * will break ParserCache for the page. |
1830 | * |
1831 | * @note Since MW 1.38 the practice of setting conflicting values for |
1832 | * the same key has been deprecated. As with ::setJsConfigVar(), if |
1833 | * you set the same key multiple times on a ParserOutput, it is expected |
1834 | * that the value will be identical each time. If you want to collect |
1835 | * multiple pieces of data under a single key, use ::appendExtensionData(). |
1836 | * |
1837 | * @param string $key The key for accessing the data. Extensions should take care to avoid |
1838 | * conflicts in naming keys. It is suggested to use the extension's name as a prefix. |
1839 | * |
1840 | * @param mixed|JsonUnserializable $value The value to set. |
1841 | * Setting a value to null is equivalent to removing the value. |
1842 | * @since 1.21 |
1843 | */ |
1844 | public function setExtensionData( $key, $value ): void { |
1845 | if ( |
1846 | array_key_exists( $key, $this->mExtensionData ) && |
1847 | $this->mExtensionData[$key] !== $value |
1848 | ) { |
1849 | // This behavior was deprecated in 1.38. We will eventually |
1850 | // emit a warning here, then throw an exception. |
1851 | } |
1852 | if ( $value === null ) { |
1853 | unset( $this->mExtensionData[$key] ); |
1854 | } else { |
1855 | $this->mExtensionData[$key] = $value; |
1856 | } |
1857 | } |
1858 | |
1859 | /** |
1860 | * Appends arbitrary data to this ParserObject. This can be used |
1861 | * to store some information in the ParserOutput object for later |
1862 | * use during page output. The data will be cached along with the |
1863 | * ParserOutput object, but unlike data set using |
1864 | * setPageProperty(), it is not recorded in the database. |
1865 | * |
1866 | * See ::setExtensionData() for more details on rationale and use. |
1867 | * |
1868 | * In order to provide for out-of-order/asynchronous/incremental |
1869 | * parsing, this method appends values to a set. See |
1870 | * ::setExtensionData() for the flag-like version of this method. |
1871 | * |
1872 | * @note Only values which can be array keys are currently supported |
1873 | * as values. |
1874 | * |
1875 | * @param string $key The key for accessing the data. Extensions should take care to avoid |
1876 | * conflicts in naming keys. It is suggested to use the extension's name as a prefix. |
1877 | * |
1878 | * @param int|string $value The value to append to the list. |
1879 | * @param string $strategy Merge strategy: |
1880 | * only MW_MERGE_STRATEGY_UNION is currently supported and external callers |
1881 | * should treat this parameter as @internal at this time and omit it. |
1882 | * @since 1.38 |
1883 | */ |
1884 | public function appendExtensionData( |
1885 | string $key, |
1886 | $value, |
1887 | string $strategy = self::MW_MERGE_STRATEGY_UNION |
1888 | ): void { |
1889 | if ( $strategy !== self::MW_MERGE_STRATEGY_UNION ) { |
1890 | throw new InvalidArgumentException( "Unknown merge strategy $strategy." ); |
1891 | } |
1892 | if ( !array_key_exists( $key, $this->mExtensionData ) ) { |
1893 | $this->mExtensionData[$key] = [ |
1894 | // Indicate how these values are to be merged. |
1895 | self::MW_MERGE_STRATEGY_KEY => $strategy, |
1896 | ]; |
1897 | } elseif ( !is_array( $this->mExtensionData[$key] ) ) { |
1898 | throw new InvalidArgumentException( "Mixing set and append for $key" ); |
1899 | } elseif ( ( $this->mExtensionData[$key][self::MW_MERGE_STRATEGY_KEY] ?? null ) !== $strategy ) { |
1900 | throw new InvalidArgumentException( "Conflicting merge strategies for $key" ); |
1901 | } |
1902 | $this->mExtensionData[$key][$value] = true; |
1903 | } |
1904 | |
1905 | /** |
1906 | * Gets extensions data previously attached to this ParserOutput using setExtensionData(). |
1907 | * Typically, such data would be set while parsing the page, e.g. by a parser function. |
1908 | * |
1909 | * @since 1.21 |
1910 | * |
1911 | * @param string $key The key to look up. |
1912 | * |
1913 | * @return mixed|null The value previously set for the given key using setExtensionData() |
1914 | * or null if no value was set for this key. |
1915 | */ |
1916 | public function getExtensionData( $key ) { |
1917 | $value = $this->mExtensionData[$key] ?? null; |
1918 | if ( is_array( $value ) ) { |
1919 | // Don't expose our internal merge strategy key. |
1920 | unset( $value[self::MW_MERGE_STRATEGY_KEY] ); |
1921 | } |
1922 | return $value; |
1923 | } |
1924 | |
1925 | private static function getTimes( $clock = null ): array { |
1926 | $ret = []; |
1927 | if ( !$clock || $clock === 'wall' ) { |
1928 | $ret['wall'] = microtime( true ); |
1929 | } |
1930 | if ( !$clock || $clock === 'cpu' ) { |
1931 | $ru = getrusage( 0 /* RUSAGE_SELF */ ); |
1932 | $ret['cpu'] = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6; |
1933 | $ret['cpu'] += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6; |
1934 | } |
1935 | return $ret; |
1936 | } |
1937 | |
1938 | /** |
1939 | * Resets the parse start timestamps for future calls to getTimeSinceStart() |
1940 | * and recordTimeProfile(). |
1941 | * |
1942 | * @since 1.22 |
1943 | */ |
1944 | public function resetParseStartTime(): void { |
1945 | $this->mParseStartTime = self::getTimes(); |
1946 | $this->mTimeProfile = []; |
1947 | } |
1948 | |
1949 | /** |
1950 | * Record the time since resetParseStartTime() was last called. |
1951 | * The recorded time can be accessed using getTimeProfile(). |
1952 | * |
1953 | * After resetParseStartTime() was called, the first call to recordTimeProfile() |
1954 | * will record the time profile. Subsequent calls to recordTimeProfile() will have |
1955 | * no effect until resetParseStartTime() is called again. |
1956 | * |
1957 | * @since 1.42 |
1958 | */ |
1959 | public function recordTimeProfile() { |
1960 | if ( !$this->mParseStartTime ) { |
1961 | // If resetParseStartTime was never called, there is nothing to record |
1962 | return; |
1963 | } |
1964 | |
1965 | if ( $this->mTimeProfile !== [] ) { |
1966 | // Don't override the times recorded by the previous call to recordTimeProfile(). |
1967 | return; |
1968 | } |
1969 | |
1970 | $now = self::getTimes(); |
1971 | $this->mTimeProfile = [ |
1972 | 'wall' => $now['wall'] - $this->mParseStartTime['wall'], |
1973 | 'cpu' => $now['cpu'] - $this->mParseStartTime['cpu'], |
1974 | ]; |
1975 | } |
1976 | |
1977 | /** |
1978 | * Returns the time that elapsed between the most recent call to resetParseStartTime() |
1979 | * and the first call to recordTimeProfile() after that. |
1980 | * |
1981 | * Clocks available are: |
1982 | * - wall: Wall clock time |
1983 | * - cpu: CPU time (requires getrusage) |
1984 | * |
1985 | * If recordTimeProfile() has noit been called since the most recent call to |
1986 | * resetParseStartTime(), or if resetParseStartTime() was never called, then |
1987 | * this method will return null. |
1988 | * |
1989 | * @param string $clock |
1990 | * |
1991 | * @since 1.42 |
1992 | * @return float|null |
1993 | */ |
1994 | public function getTimeProfile( string $clock ) { |
1995 | return $this->mTimeProfile[ $clock ] ?? null; |
1996 | } |
1997 | |
1998 | /** |
1999 | * Returns the time since resetParseStartTime() was last called |
2000 | * |
2001 | * Clocks available are: |
2002 | * - wall: Wall clock time |
2003 | * - cpu: CPU time (requires getrusage) |
2004 | * |
2005 | * @since 1.22 |
2006 | * @deprecated since 1.42, use getTimeProfile() instead. |
2007 | * @param string $clock |
2008 | * @return float|null |
2009 | */ |
2010 | public function getTimeSinceStart( $clock ) { |
2011 | wfDeprecated( __METHOD__, '1.42' ); |
2012 | |
2013 | if ( !isset( $this->mParseStartTime[$clock] ) ) { |
2014 | return null; |
2015 | } |
2016 | |
2017 | $end = self::getTimes( $clock ); |
2018 | return $end[$clock] - $this->mParseStartTime[$clock]; |
2019 | } |
2020 | |
2021 | /** |
2022 | * Sets parser limit report data for a key |
2023 | * |
2024 | * The key is used as the prefix for various messages used for formatting: |
2025 | * - $key: The label for the field in the limit report |
2026 | * - $key-value-text: Message used to format the value in the "NewPP limit |
2027 | * report" HTML comment. If missing, uses $key-format. |
2028 | * - $key-value-html: Message used to format the value in the preview |
2029 | * limit report table. If missing, uses $key-format. |
2030 | * - $key-value: Message used to format the value. If missing, uses "$1". |
2031 | * |
2032 | * Note that all values are interpreted as wikitext, and so should be |
2033 | * encoded with htmlspecialchars() as necessary, but should avoid complex |
2034 | * HTML for display in the "NewPP limit report" comment. |
2035 | * |
2036 | * @since 1.22 |
2037 | * @param string $key Message key |
2038 | * @param mixed $value Appropriate for Message::params() |
2039 | */ |
2040 | public function setLimitReportData( $key, $value ): void { |
2041 | $this->mLimitReportData[$key] = $value; |
2042 | |
2043 | if ( is_array( $value ) ) { |
2044 | if ( array_keys( $value ) === [ 0, 1 ] |
2045 | && is_numeric( $value[0] ) |
2046 | && is_numeric( $value[1] ) |
2047 | ) { |
2048 | $data = [ 'value' => $value[0], 'limit' => $value[1] ]; |
2049 | } else { |
2050 | $data = $value; |
2051 | } |
2052 | } else { |
2053 | $data = $value; |
2054 | } |
2055 | |
2056 | if ( strpos( $key, '-' ) ) { |
2057 | [ $ns, $name ] = explode( '-', $key, 2 ); |
2058 | $this->mLimitReportJSData[$ns][$name] = $data; |
2059 | } else { |
2060 | $this->mLimitReportJSData[$key] = $data; |
2061 | } |
2062 | } |
2063 | |
2064 | /** |
2065 | * Check whether the cache TTL was lowered from the site default. |
2066 | * |
2067 | * When content is determined by more than hard state (e.g. page edits), |
2068 | * such as template/file transclusions based on the current timestamp or |
2069 | * extension tags that generate lists based on queries, this return true. |
2070 | * |
2071 | * This method mainly exists to facilitate the logic in |
2072 | * WikiPage::triggerOpportunisticLinksUpdate. As such, beware that reducing the TTL for |
2073 | * reasons that do not relate to "dynamic content", may have the side-effect of incurring |
2074 | * more RefreshLinksJob executions. |
2075 | * |
2076 | * @internal For use by Parser and WikiPage |
2077 | * @since 1.37 |
2078 | * @return bool |
2079 | */ |
2080 | public function hasReducedExpiry(): bool { |
2081 | $parserCacheExpireTime = MediaWikiServices::getInstance()->getMainConfig()->get( |
2082 | MainConfigNames::ParserCacheExpireTime ); |
2083 | |
2084 | return $this->getCacheExpiry() < $parserCacheExpireTime; |
2085 | } |
2086 | |
2087 | /** |
2088 | * Set the prevent-clickjacking flag. If set this will cause an |
2089 | * `X-Frame-Options` header appropriate for edit pages to be sent. |
2090 | * The header value is controlled by `$wgEditPageFrameOptions`. |
2091 | * |
2092 | * This is the default for special pages. If you display a CSRF-protected |
2093 | * form on an ordinary view page, then you need to call this function |
2094 | * with `$flag = true`. |
2095 | * |
2096 | * @param bool $flag New flag value |
2097 | * @since 1.38 |
2098 | */ |
2099 | public function setPreventClickjacking( bool $flag ) { |
2100 | $this->mPreventClickjacking = $flag; |
2101 | } |
2102 | |
2103 | /** |
2104 | * Get the prevent-clickjacking flag. |
2105 | * |
2106 | * @return bool Flag value |
2107 | * @since 1.38 |
2108 | * @see ::setPreventClickjacking |
2109 | */ |
2110 | public function getPreventClickjacking(): bool { |
2111 | return $this->mPreventClickjacking; |
2112 | } |
2113 | |
2114 | /** |
2115 | * Lower the runtime adaptive TTL to at most this value |
2116 | * |
2117 | * @param int $ttl |
2118 | * @since 1.28 |
2119 | */ |
2120 | public function updateRuntimeAdaptiveExpiry( $ttl ): void { |
2121 | $this->mMaxAdaptiveExpiry = min( $ttl, $this->mMaxAdaptiveExpiry ); |
2122 | $this->updateCacheExpiry( $ttl ); |
2123 | } |
2124 | |
2125 | /** |
2126 | * Add an extra value to Content-Security-Policy default-src directive |
2127 | * |
2128 | * Call this if you are including a resource (e.g. image) from a third party domain. |
2129 | * This is used for all source types except style and script. |
2130 | * |
2131 | * @since 1.35 |
2132 | * @param string $src CSP source e.g. example.com |
2133 | */ |
2134 | public function addExtraCSPDefaultSrc( $src ): void { |
2135 | $this->mExtraDefaultSrcs[] = $src; |
2136 | } |
2137 | |
2138 | /** |
2139 | * Add an extra value to Content-Security-Policy style-src directive |
2140 | * |
2141 | * @since 1.35 |
2142 | * @param string $src CSP source e.g. example.com |
2143 | */ |
2144 | public function addExtraCSPStyleSrc( $src ): void { |
2145 | $this->mExtraStyleSrcs[] = $src; |
2146 | } |
2147 | |
2148 | /** |
2149 | * Add an extra value to Content-Security-Policy script-src directive |
2150 | * |
2151 | * Call this if you are loading third-party Javascript |
2152 | * |
2153 | * @since 1.35 |
2154 | * @param string $src CSP source e.g. example.com |
2155 | */ |
2156 | public function addExtraCSPScriptSrc( $src ): void { |
2157 | $this->mExtraScriptSrcs[] = $src; |
2158 | } |
2159 | |
2160 | /** |
2161 | * Call this when parsing is done to lower the TTL based on low parse times |
2162 | * |
2163 | * @since 1.28 |
2164 | */ |
2165 | public function finalizeAdaptiveCacheExpiry(): void { |
2166 | if ( is_infinite( $this->mMaxAdaptiveExpiry ) ) { |
2167 | return; // not set |
2168 | } |
2169 | |
2170 | $runtime = $this->getTimeProfile( 'wall' ); |
2171 | if ( is_float( $runtime ) ) { |
2172 | $slope = ( self::SLOW_AR_TTL - self::FAST_AR_TTL ) |
2173 | / ( self::PARSE_SLOW_SEC - self::PARSE_FAST_SEC ); |
2174 | // SLOW_AR_TTL = PARSE_SLOW_SEC * $slope + $point |
2175 | $point = self::SLOW_AR_TTL - self::PARSE_SLOW_SEC * $slope; |
2176 | |
2177 | $adaptiveTTL = min( |
2178 | max( $slope * $runtime + $point, self::MIN_AR_TTL ), |
2179 | $this->mMaxAdaptiveExpiry |
2180 | ); |
2181 | $this->updateCacheExpiry( $adaptiveTTL ); |
2182 | } |
2183 | } |
2184 | |
2185 | /** |
2186 | * Transfer parser options which affect post-processing from ParserOptions |
2187 | * to this ParserOutput. |
2188 | * @param ParserOptions $parserOptions |
2189 | */ |
2190 | public function setFromParserOptions( ParserOptions $parserOptions ) { |
2191 | // Copied from Parser.php::parse and should probably be abstracted |
2192 | // into the parent base class (probably as part of T236809) |
2193 | // Wrap non-interface parser output in a <div> so it can be targeted |
2194 | // with CSS (T37247) |
2195 | $class = $parserOptions->getWrapOutputClass(); |
2196 | if ( $class !== false && !$parserOptions->getInterfaceMessage() ) { |
2197 | $this->addWrapperDivClass( $class ); |
2198 | } |
2199 | |
2200 | // Record whether we should suppress section edit links |
2201 | if ( $parserOptions->getSuppressSectionEditLinks() ) { |
2202 | $this->setOutputFlag( ParserOutputFlags::NO_SECTION_EDIT_LINKS ); |
2203 | } |
2204 | |
2205 | // Record whether this is a preview parse in the output (T341010) |
2206 | if ( $parserOptions->getIsPreview() ) { |
2207 | $this->setOutputFlag( ParserOutputFlags::IS_PREVIEW, true ); |
2208 | // Ensure that previews aren't cacheable, just to be safe. |
2209 | $this->updateCacheExpiry( 0 ); |
2210 | } |
2211 | } |
2212 | |
2213 | public function __sleep() { |
2214 | return array_filter( array_keys( get_object_vars( $this ) ), |
2215 | static function ( $field ) { |
2216 | if ( $field === 'mParseStartTime' || $field === 'mWarningMsgs' ) { |
2217 | return false; |
2218 | } |
2219 | // Unserializing unknown private fields in HHVM causes |
2220 | // member variables with nulls in their names (T229366) |
2221 | return strpos( $field, "\0" ) === false; |
2222 | } |
2223 | ); |
2224 | } |
2225 | |
2226 | /** |
2227 | * Merges internal metadata such as flags, accessed options, and profiling info |
2228 | * from $source into this ParserOutput. This should be used whenever the state of $source |
2229 | * has any impact on the state of this ParserOutput. |
2230 | * |
2231 | * @param ParserOutput $source |
2232 | */ |
2233 | public function mergeInternalMetaDataFrom( ParserOutput $source ): void { |
2234 | $this->mWarnings = self::mergeMap( $this->mWarnings, $source->mWarnings ); // don't use getter |
2235 | $this->mTimestamp = $this->useMaxValue( $this->mTimestamp, $source->getRevisionTimestamp() ); |
2236 | if ( $source->hasCacheTime() ) { |
2237 | $sourceCacheTime = $source->getCacheTime(); |
2238 | if ( |
2239 | !$this->hasCacheTime() || |
2240 | // "undocumented use of -1 to mean not cacheable" |
2241 | // deprecated, but still supported by ::setCacheTime() |
2242 | strval( $sourceCacheTime ) === '-1' || |
2243 | ( |
2244 | strval( $this->getCacheTime() ) !== '-1' && |
2245 | // use newer of the two times |
2246 | $this->getCacheTime() < $sourceCacheTime |
2247 | ) |
2248 | ) { |
2249 | $this->setCacheTime( $sourceCacheTime ); |
2250 | } |
2251 | } |
2252 | if ( $source->getRenderId() !== null ) { |
2253 | // Final render ID should be a function of all component POs |
2254 | $rid = ( $this->getRenderId() ?? '' ) . $source->getRenderId(); |
2255 | $this->setRenderId( $rid ); |
2256 | } |
2257 | if ( $source->getCacheRevisionId() !== null ) { |
2258 | $sourceCacheRevisionId = $source->getCacheRevisionId(); |
2259 | $thisCacheRevisionId = $this->getCacheRevisionId(); |
2260 | if ( $thisCacheRevisionId === null ) { |
2261 | $this->setCacheRevisionId( $sourceCacheRevisionId ); |
2262 | } elseif ( $sourceCacheRevisionId !== $thisCacheRevisionId ) { |
2263 | // May throw an exception here in the future |
2264 | wfDeprecated( |
2265 | __METHOD__ . ": conflicting revision IDs " . |
2266 | "$thisCacheRevisionId and $sourceCacheRevisionId" |
2267 | ); |
2268 | } |
2269 | } |
2270 | |
2271 | foreach ( self::SPECULATIVE_FIELDS as $field ) { |
2272 | if ( $this->$field && $source->$field && $this->$field !== $source->$field ) { |
2273 | wfLogWarning( __METHOD__ . ": inconsistent '$field' properties!" ); |
2274 | } |
2275 | $this->$field = $this->useMaxValue( $this->$field, $source->$field ); |
2276 | } |
2277 | |
2278 | $this->mParseStartTime = $this->useEachMinValue( |
2279 | $this->mParseStartTime, |
2280 | $source->mParseStartTime |
2281 | ); |
2282 | |
2283 | $this->mTimeProfile = $this->useEachTotalValue( |
2284 | $this->mTimeProfile, |
2285 | $source->mTimeProfile |
2286 | ); |
2287 | |
2288 | $this->mFlags = self::mergeMap( $this->mFlags, $source->mFlags ); |
2289 | $this->mParseUsedOptions = self::mergeMap( $this->mParseUsedOptions, $source->mParseUsedOptions ); |
2290 | |
2291 | // TODO: maintain per-slot limit reports! |
2292 | if ( !$this->mLimitReportData ) { |
2293 | $this->mLimitReportData = $source->mLimitReportData; |
2294 | } |
2295 | if ( !$this->mLimitReportJSData ) { |
2296 | $this->mLimitReportJSData = $source->mLimitReportJSData; |
2297 | } |
2298 | } |
2299 | |
2300 | /** |
2301 | * Merges HTML metadata such as head items, JS config vars, and HTTP cache control info |
2302 | * from $source into this ParserOutput. This should be used whenever the HTML in $source |
2303 | * has been somehow merged into the HTML of this ParserOutput. |
2304 | * |
2305 | * @param ParserOutput $source |
2306 | */ |
2307 | public function mergeHtmlMetaDataFrom( ParserOutput $source ): void { |
2308 | // HTML and HTTP |
2309 | $this->mHeadItems = self::mergeMixedList( $this->mHeadItems, $source->getHeadItems() ); |
2310 | $this->addModules( $source->getModules() ); |
2311 | $this->addModuleStyles( $source->getModuleStyles() ); |
2312 | $this->mJsConfigVars = self::mergeMapStrategy( $this->mJsConfigVars, $source->mJsConfigVars ); |
2313 | $this->mMaxAdaptiveExpiry = min( $this->mMaxAdaptiveExpiry, $source->mMaxAdaptiveExpiry ); |
2314 | $this->mExtraStyleSrcs = self::mergeList( |
2315 | $this->mExtraStyleSrcs, |
2316 | $source->getExtraCSPStyleSrcs() |
2317 | ); |
2318 | $this->mExtraScriptSrcs = self::mergeList( |
2319 | $this->mExtraScriptSrcs, |
2320 | $source->getExtraCSPScriptSrcs() |
2321 | ); |
2322 | $this->mExtraDefaultSrcs = self::mergeList( |
2323 | $this->mExtraDefaultSrcs, |
2324 | $source->getExtraCSPDefaultSrcs() |
2325 | ); |
2326 | |
2327 | // "noindex" always wins! |
2328 | $this->mIndexSet = $this->mIndexSet || $source->mIndexSet; |
2329 | $this->mNoIndexSet = $this->mNoIndexSet || $source->mNoIndexSet; |
2330 | |
2331 | // Skin control |
2332 | $this->mNewSection = $this->mNewSection || $source->getNewSection(); |
2333 | $this->mHideNewSection = $this->mHideNewSection || $source->getHideNewSection(); |
2334 | $this->mNoGallery = $this->mNoGallery || $source->getNoGallery(); |
2335 | $this->mEnableOOUI = $this->mEnableOOUI || $source->getEnableOOUI(); |
2336 | $this->mPreventClickjacking = $this->mPreventClickjacking || $source->getPreventClickjacking(); |
2337 | |
2338 | $tocData = $this->getTOCData(); |
2339 | $sourceTocData = $source->getTOCData(); |
2340 | if ( $tocData !== null ) { |
2341 | if ( $sourceTocData !== null ) { |
2342 | // T327429: Section merging is broken, since it doesn't respect |
2343 | // global numbering, but there are tests which expect section |
2344 | // metadata to be concatenated. |
2345 | // There should eventually be a deprecation warning here. |
2346 | foreach ( $sourceTocData->getSections() as $s ) { |
2347 | $tocData->addSection( $s ); |
2348 | } |
2349 | } |
2350 | } elseif ( $sourceTocData !== null ) { |
2351 | $this->setTOCData( $sourceTocData ); |
2352 | } |
2353 | |
2354 | // XXX: we don't want to concatenate title text, so first write wins. |
2355 | // We should use the first *modified* title text, but we don't have the original to check. |
2356 | if ( $this->mTitleText === null || $this->mTitleText === '' ) { |
2357 | $this->mTitleText = $source->mTitleText; |
2358 | } |
2359 | |
2360 | // class names are stored in array keys |
2361 | $this->mWrapperDivClasses = self::mergeMap( |
2362 | $this->mWrapperDivClasses, |
2363 | $source->mWrapperDivClasses |
2364 | ); |
2365 | |
2366 | // NOTE: last write wins, same as within one ParserOutput |
2367 | $this->mIndicators = self::mergeMap( $this->mIndicators, $source->getIndicators() ); |
2368 | |
2369 | // NOTE: include extension data in "tracking meta data" as well as "html meta data"! |
2370 | // TODO: add a $mergeStrategy parameter to setExtensionData to allow different |
2371 | // kinds of extension data to be merged in different ways. |
2372 | $this->mExtensionData = self::mergeMapStrategy( |
2373 | $this->mExtensionData, |
2374 | $source->mExtensionData |
2375 | ); |
2376 | } |
2377 | |
2378 | /** |
2379 | * Merges dependency tracking metadata such as backlinks, images used, and extension data |
2380 | * from $source into this ParserOutput. This allows dependency tracking to be done for the |
2381 | * combined output of multiple content slots. |
2382 | * |
2383 | * @param ParserOutput $source |
2384 | */ |
2385 | public function mergeTrackingMetaDataFrom( ParserOutput $source ): void { |
2386 | $this->mLanguageLinks = self::mergeList( $this->mLanguageLinks, $source->getLanguageLinks() ); |
2387 | $this->mCategories = self::mergeMap( $this->mCategories, $source->getCategoryMap() ); |
2388 | $this->mLinks = self::merge2D( $this->mLinks, $source->getLinks() ); |
2389 | $this->mTemplates = self::merge2D( $this->mTemplates, $source->getTemplates() ); |
2390 | $this->mTemplateIds = self::merge2D( $this->mTemplateIds, $source->getTemplateIds() ); |
2391 | $this->mImages = self::mergeMap( $this->mImages, $source->getImages() ); |
2392 | $this->mFileSearchOptions = self::mergeMap( |
2393 | $this->mFileSearchOptions, |
2394 | $source->getFileSearchOptions() |
2395 | ); |
2396 | $this->mExternalLinks = self::mergeMap( $this->mExternalLinks, $source->getExternalLinks() ); |
2397 | $this->mInterwikiLinks = self::merge2D( |
2398 | $this->mInterwikiLinks, |
2399 | $source->getInterwikiLinks() |
2400 | ); |
2401 | |
2402 | // TODO: add a $mergeStrategy parameter to setPageProperty to allow different |
2403 | // kinds of properties to be merged in different ways. |
2404 | // (Model this after ::appendJsConfigVar(); use ::mergeMapStrategy here) |
2405 | $this->mProperties = self::mergeMap( $this->mProperties, $source->getPageProperties() ); |
2406 | |
2407 | // NOTE: include extension data in "tracking meta data" as well as "html meta data"! |
2408 | $this->mExtensionData = self::mergeMapStrategy( |
2409 | $this->mExtensionData, |
2410 | $source->mExtensionData |
2411 | ); |
2412 | } |
2413 | |
2414 | /** |
2415 | * Adds the metadata collected in this ParserOutput to the supplied |
2416 | * ContentMetadataCollector. This is similar to ::mergeHtmlMetaDataFrom() |
2417 | * but in the opposite direction, since ParserOutput is read/write while |
2418 | * ContentMetadataCollector is write-only. |
2419 | * |
2420 | * @param ContentMetadataCollector $metadata |
2421 | * @since 1.38 |
2422 | */ |
2423 | public function collectMetadata( ContentMetadataCollector $metadata ): void { |
2424 | // Uniform handling of all boolean flags: they are OR'ed together. |
2425 | $flags = array_keys( |
2426 | $this->mFlags + array_flip( ParserOutputFlags::cases() ) |
2427 | ); |
2428 | foreach ( $flags as $name ) { |
2429 | if ( $this->getOutputFlag( $name ) ) { |
2430 | $metadata->setOutputFlag( $name ); |
2431 | } |
2432 | } |
2433 | |
2434 | // Uniform handling of string sets: they are unioned. |
2435 | // (This includes modules, style modes, and CSP src.) |
2436 | foreach ( ParserOutputStringSets::cases() as $name ) { |
2437 | $metadata->appendOutputStrings( |
2438 | $name, $this->getOutputStrings( $name ) |
2439 | ); |
2440 | } |
2441 | |
2442 | foreach ( $this->mCategories as $cat => $key ) { |
2443 | // Numeric category strings are going to come out of the |
2444 | // `mCategories` array as ints; cast back to string. |
2445 | // Also convert back to a LinkTarget! |
2446 | $lt = TitleValue::tryNew( NS_CATEGORY, (string)$cat ); |
2447 | $metadata->addCategory( $lt, $key ); |
2448 | } |
2449 | |
2450 | foreach ( $this->mJsConfigVars as $key => $value ) { |
2451 | if ( is_array( $value ) && isset( $value[self::MW_MERGE_STRATEGY_KEY] ) ) { |
2452 | $strategy = $value[self::MW_MERGE_STRATEGY_KEY]; |
2453 | foreach ( $value as $item => $ignore ) { |
2454 | if ( $item !== self::MW_MERGE_STRATEGY_KEY ) { |
2455 | $metadata->appendJsConfigVar( $key, $item, $strategy ); |
2456 | } |
2457 | } |
2458 | } elseif ( $metadata instanceof ParserOutput && |
2459 | array_key_exists( $key, $metadata->mJsConfigVars ) |
2460 | ) { |
2461 | // This behavior is deprecated, will likely result in |
2462 | // incorrect output, and we'll eventually emit a |
2463 | // warning here---but at the moment this is usually |
2464 | // caused by limitations in Parsoid and/or use of |
2465 | // the ParserAfterParse hook: T303015#7770480 |
2466 | $metadata->mJsConfigVars[$key] = $value; |
2467 | } else { |
2468 | $metadata->setJsConfigVar( $key, $value ); |
2469 | } |
2470 | } |
2471 | foreach ( $this->mExtensionData as $key => $value ) { |
2472 | if ( is_array( $value ) && isset( $value[self::MW_MERGE_STRATEGY_KEY] ) ) { |
2473 | $strategy = $value[self::MW_MERGE_STRATEGY_KEY]; |
2474 | foreach ( $value as $item => $ignore ) { |
2475 | if ( $item !== self::MW_MERGE_STRATEGY_KEY ) { |
2476 | $metadata->appendExtensionData( $key, $item, $strategy ); |
2477 | } |
2478 | } |
2479 | } elseif ( $metadata instanceof ParserOutput && |
2480 | array_key_exists( $key, $metadata->mExtensionData ) |
2481 | ) { |
2482 | // This behavior is deprecated, will likely result in |
2483 | // incorrect output, and we'll eventually emit a |
2484 | // warning here---but at the moment this is usually |
2485 | // caused by limitations in Parsoid and/or use of |
2486 | // the ParserAfterParse hook: T303015#7770480 |
2487 | $metadata->mExtensionData[$key] = $value; |
2488 | } else { |
2489 | $metadata->setExtensionData( $key, $value ); |
2490 | } |
2491 | } |
2492 | foreach ( $this->mExternalLinks as $url => $ignore ) { |
2493 | $metadata->addExternalLink( $url ); |
2494 | } |
2495 | foreach ( $this->mProperties as $prop => $value ) { |
2496 | $metadata->setPageProperty( $prop, $value ); |
2497 | } |
2498 | foreach ( $this->mWarningMsgs as $msg => $args ) { |
2499 | $metadata->addWarningMsg( $msg, ...$args ); |
2500 | } |
2501 | foreach ( $this->mLimitReportData as $key => $value ) { |
2502 | $metadata->setLimitReportData( $key, $value ); |
2503 | } |
2504 | |
2505 | // ParserOutput-only fields; maintained "behind the curtain" |
2506 | // since Parsoid doesn't have to know about them. |
2507 | // |
2508 | // In production use, the $metadata supplied to this method |
2509 | // will almost always be an instance of ParserOutput, passed to |
2510 | // Parsoid by core when parsing begins and returned to core by |
2511 | // Parsoid as a ContentMetadataCollector (Parsoid's name for |
2512 | // ParserOutput) when DataAccess::parseWikitext() is called. |
2513 | // |
2514 | // We may use still Parsoid's StubMetadataCollector for testing or |
2515 | // when running Parsoid in standalone mode, so forcing a downcast |
2516 | // here would lose some flexibility. |
2517 | |
2518 | if ( $metadata instanceof ParserOutput ) { |
2519 | foreach ( $this->getUsedOptions() as $opt ) { |
2520 | $metadata->recordOption( $opt ); |
2521 | } |
2522 | if ( $this->mCacheExpiry !== null ) { |
2523 | $metadata->updateCacheExpiry( $this->mCacheExpiry ); |
2524 | } |
2525 | if ( $this->mCacheTime !== '' ) { |
2526 | $metadata->setCacheTime( $this->mCacheTime ); |
2527 | } |
2528 | if ( $this->mCacheRevisionId !== null ) { |
2529 | $metadata->setCacheRevisionId( $this->mCacheRevisionId ); |
2530 | } |
2531 | // T293514: We should use the first *modified* title text, but |
2532 | // we don't have the original to check. |
2533 | $otherTitle = $metadata->getTitleText(); |
2534 | if ( $otherTitle === null || $otherTitle === '' ) { |
2535 | $metadata->setTitleText( $this->getTitleText() ); |
2536 | } |
2537 | } |
2538 | } |
2539 | |
2540 | private static function mergeMixedList( array $a, array $b ): array { |
2541 | return array_unique( array_merge( $a, $b ), SORT_REGULAR ); |
2542 | } |
2543 | |
2544 | private static function mergeList( array $a, array $b ): array { |
2545 | return array_values( array_unique( array_merge( $a, $b ), SORT_REGULAR ) ); |
2546 | } |
2547 | |
2548 | private static function mergeMap( array $a, array $b ): array { |
2549 | return array_replace( $a, $b ); |
2550 | } |
2551 | |
2552 | private static function mergeMapStrategy( array $a, array $b ): array { |
2553 | foreach ( $b as $key => $bValue ) { |
2554 | if ( !array_key_exists( $key, $a ) ) { |
2555 | $a[$key] = $bValue; |
2556 | } elseif ( |
2557 | is_array( $a[$key] ) && |
2558 | isset( $a[$key][self::MW_MERGE_STRATEGY_KEY] ) && |
2559 | isset( $bValue[self::MW_MERGE_STRATEGY_KEY] ) |
2560 | ) { |
2561 | $strategy = $bValue[self::MW_MERGE_STRATEGY_KEY]; |
2562 | if ( $strategy !== $a[$key][self::MW_MERGE_STRATEGY_KEY] ) { |
2563 | throw new InvalidArgumentException( "Conflicting merge strategy for $key" ); |
2564 | } |
2565 | if ( $strategy === self::MW_MERGE_STRATEGY_UNION ) { |
2566 | // Note the array_merge is *not* safe to use here, because |
2567 | // the $bValue is expected to be a map from items to `true`. |
2568 | // If the item is a numeric string like '1' then array_merge |
2569 | // will convert it to an integer and renumber the array! |
2570 | $a[$key] = array_replace( $a[$key], $bValue ); |
2571 | } else { |
2572 | throw new InvalidArgumentException( "Unknown merge strategy $strategy" ); |
2573 | } |
2574 | } else { |
2575 | $valuesSame = ( $a[$key] === $bValue ); |
2576 | if ( ( !$valuesSame ) && |
2577 | is_object( $a[$key] ) && |
2578 | is_object( $bValue ) |
2579 | ) { |
2580 | $jsonCodec = MediaWikiServices::getInstance()->getJsonCodec(); |
2581 | $valuesSame = ( $jsonCodec->serialize( $a[$key] ) === $jsonCodec->serialize( $bValue ) ); |
2582 | } |
2583 | if ( !$valuesSame ) { |
2584 | // Silently replace for now; in the future will first emit |
2585 | // a deprecation warning, and then (later) throw. |
2586 | $a[$key] = $bValue; |
2587 | } |
2588 | } |
2589 | } |
2590 | return $a; |
2591 | } |
2592 | |
2593 | private static function merge2D( array $a, array $b ): array { |
2594 | $values = []; |
2595 | $keys = array_merge( array_keys( $a ), array_keys( $b ) ); |
2596 | |
2597 | foreach ( $keys as $k ) { |
2598 | if ( empty( $a[$k] ) ) { |
2599 | $values[$k] = $b[$k]; |
2600 | } elseif ( empty( $b[$k] ) ) { |
2601 | $values[$k] = $a[$k]; |
2602 | } elseif ( is_array( $a[$k] ) && is_array( $b[$k] ) ) { |
2603 | $values[$k] = array_replace( $a[$k], $b[$k] ); |
2604 | } else { |
2605 | $values[$k] = $b[$k]; |
2606 | } |
2607 | } |
2608 | |
2609 | return $values; |
2610 | } |
2611 | |
2612 | private static function useEachMinValue( array $a, array $b ): array { |
2613 | $values = []; |
2614 | $keys = array_merge( array_keys( $a ), array_keys( $b ) ); |
2615 | |
2616 | foreach ( $keys as $k ) { |
2617 | $values[$k] = min( $a[$k] ?? INF, $b[$k] ?? INF ); |
2618 | } |
2619 | |
2620 | return $values; |
2621 | } |
2622 | |
2623 | private static function useEachTotalValue( array $a, array $b ): array { |
2624 | $values = []; |
2625 | $keys = array_merge( array_keys( $a ), array_keys( $b ) ); |
2626 | |
2627 | foreach ( $keys as $k ) { |
2628 | $values[$k] = ( $a[$k] ?? 0 ) + ( $b[$k] ?? 0 ); |
2629 | } |
2630 | |
2631 | return $values; |
2632 | } |
2633 | |
2634 | private static function useMaxValue( $a, $b ) { |
2635 | if ( $a === null ) { |
2636 | return $b; |
2637 | } |
2638 | |
2639 | if ( $b === null ) { |
2640 | return $a; |
2641 | } |
2642 | |
2643 | return max( $a, $b ); |
2644 | } |
2645 | |
2646 | /** |
2647 | * Returns a JSON serializable structure representing this ParserOutput instance. |
2648 | * @see newFromJson() |
2649 | * |
2650 | * @return array |
2651 | */ |
2652 | protected function toJsonArray(): array { |
2653 | // WARNING: When changing how this class is serialized, follow the instructions |
2654 | // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>! |
2655 | |
2656 | $data = [ |
2657 | 'Text' => $this->mRawText, |
2658 | 'LanguageLinks' => $this->mLanguageLinks, |
2659 | 'Categories' => $this->mCategories, |
2660 | 'Indicators' => $this->mIndicators, |
2661 | 'TitleText' => $this->mTitleText, |
2662 | 'Links' => $this->mLinks, |
2663 | 'LinksSpecial' => $this->mLinksSpecial, |
2664 | 'Templates' => $this->mTemplates, |
2665 | 'TemplateIds' => $this->mTemplateIds, |
2666 | 'Images' => $this->mImages, |
2667 | 'FileSearchOptions' => $this->mFileSearchOptions, |
2668 | 'ExternalLinks' => $this->mExternalLinks, |
2669 | 'InterwikiLinks' => $this->mInterwikiLinks, |
2670 | 'NewSection' => $this->mNewSection, |
2671 | 'HideNewSection' => $this->mHideNewSection, |
2672 | 'NoGallery' => $this->mNoGallery, |
2673 | 'HeadItems' => $this->mHeadItems, |
2674 | 'Modules' => array_keys( $this->mModuleSet ), |
2675 | 'ModuleStyles' => array_keys( $this->mModuleStyleSet ), |
2676 | 'JsConfigVars' => $this->mJsConfigVars, |
2677 | 'Warnings' => $this->mWarnings, |
2678 | 'Sections' => $this->getSections(), |
2679 | 'Properties' => self::detectAndEncodeBinary( $this->mProperties ), |
2680 | 'Timestamp' => $this->mTimestamp, |
2681 | 'EnableOOUI' => $this->mEnableOOUI, |
2682 | 'IndexPolicy' => $this->getIndexPolicy(), |
2683 | // may contain arbitrary structures! |
2684 | 'ExtensionData' => $this->mExtensionData, |
2685 | 'LimitReportData' => $this->mLimitReportData, |
2686 | 'LimitReportJSData' => $this->mLimitReportJSData, |
2687 | 'CacheMessage' => $this->mCacheMessage, |
2688 | 'TimeProfile' => $this->mTimeProfile, |
2689 | 'ParseStartTime' => $this->mParseStartTime, // useless |
2690 | 'PreventClickjacking' => $this->mPreventClickjacking, |
2691 | 'ExtraScriptSrcs' => $this->mExtraScriptSrcs, |
2692 | 'ExtraDefaultSrcs' => $this->mExtraDefaultSrcs, |
2693 | 'ExtraStyleSrcs' => $this->mExtraStyleSrcs, |
2694 | 'Flags' => $this->mFlags + ( |
2695 | // backward-compatibility: distinguish "no sections" from |
2696 | // "sections not set" (Will be unnecessary after T327439.) |
2697 | $this->mTOCData === null ? [] : [ 'mw:toc-set' => true ] |
2698 | ), |
2699 | 'SpeculativeRevId' => $this->mSpeculativeRevId, |
2700 | 'SpeculativePageIdUsed' => $this->speculativePageIdUsed, |
2701 | 'RevisionTimestampUsed' => $this->revisionTimestampUsed, |
2702 | 'RevisionUsedSha1Base36' => $this->revisionUsedSha1Base36, |
2703 | 'WrapperDivClasses' => $this->mWrapperDivClasses, |
2704 | ]; |
2705 | |
2706 | // Fill in missing fields from parents. Array addition does not override existing fields. |
2707 | $data += parent::toJsonArray(); |
2708 | |
2709 | // TODO: make more fields optional! |
2710 | |
2711 | if ( $this->mMaxAdaptiveExpiry !== INF ) { |
2712 | // NOTE: JSON can't encode infinity! |
2713 | $data['MaxAdaptiveExpiry'] = $this->mMaxAdaptiveExpiry; |
2714 | } |
2715 | |
2716 | if ( $this->mTOCData ) { |
2717 | // Temporarily add information from TOCData extension data |
2718 | // T327439: We should eventually make the entire mTOCData |
2719 | // serializable |
2720 | $toc = $this->mTOCData->jsonSerialize(); |
2721 | if ( isset( $toc['extensionData'] ) ) { |
2722 | $data['TOCExtensionData'] = $toc['extensionData']; |
2723 | } |
2724 | } |
2725 | |
2726 | return $data; |
2727 | } |
2728 | |
2729 | public static function newFromJsonArray( JsonUnserializer $unserializer, array $json ): ParserOutput { |
2730 | $parserOutput = new ParserOutput(); |
2731 | $parserOutput->initFromJson( $unserializer, $json ); |
2732 | return $parserOutput; |
2733 | } |
2734 | |
2735 | /** |
2736 | * Initialize member fields from an array returned by jsonSerialize(). |
2737 | * @param JsonUnserializer $unserializer |
2738 | * @param array $jsonData |
2739 | */ |
2740 | protected function initFromJson( JsonUnserializer $unserializer, array $jsonData ): void { |
2741 | parent::initFromJson( $unserializer, $jsonData ); |
2742 | |
2743 | // WARNING: When changing how this class is serialized, follow the instructions |
2744 | // at <https://www.mediawiki.org/wiki/Manual:Parser_cache/Serialization_compatibility>! |
2745 | |
2746 | $this->mRawText = $jsonData['Text']; |
2747 | $this->mLanguageLinks = $jsonData['LanguageLinks']; |
2748 | $this->mCategories = $jsonData['Categories']; |
2749 | $this->mIndicators = $jsonData['Indicators']; |
2750 | $this->mTitleText = $jsonData['TitleText']; |
2751 | $this->mLinks = $jsonData['Links']; |
2752 | $this->mLinksSpecial = $jsonData['LinksSpecial']; |
2753 | $this->mTemplates = $jsonData['Templates']; |
2754 | $this->mTemplateIds = $jsonData['TemplateIds']; |
2755 | $this->mImages = $jsonData['Images']; |
2756 | $this->mFileSearchOptions = $jsonData['FileSearchOptions']; |
2757 | $this->mExternalLinks = $jsonData['ExternalLinks']; |
2758 | $this->mInterwikiLinks = $jsonData['InterwikiLinks']; |
2759 | $this->mNewSection = $jsonData['NewSection']; |
2760 | $this->mHideNewSection = $jsonData['HideNewSection']; |
2761 | $this->mNoGallery = $jsonData['NoGallery']; |
2762 | $this->mHeadItems = $jsonData['HeadItems']; |
2763 | $this->mModuleSet = array_fill_keys( $jsonData['Modules'], true ); |
2764 | $this->mModuleStyleSet = array_fill_keys( $jsonData['ModuleStyles'], true ); |
2765 | $this->mJsConfigVars = $jsonData['JsConfigVars']; |
2766 | $this->mWarnings = $jsonData['Warnings']; |
2767 | $this->mFlags = $jsonData['Flags']; |
2768 | if ( |
2769 | $jsonData['Sections'] !== [] || |
2770 | // backward-compatibility: distinguish "no sections" from |
2771 | // "sections not set" (Will be unnecessary after T327439.) |
2772 | $this->getOutputFlag( 'mw:toc-set' ) |
2773 | ) { |
2774 | $this->setSections( $jsonData['Sections'] ); |
2775 | unset( $this->mFlags['mw:toc-set'] ); |
2776 | if ( isset( $jsonData['TOCExtensionData'] ) ) { |
2777 | $tocData = $this->getTOCData(); // created by setSections() above |
2778 | foreach ( $jsonData['TOCExtensionData'] as $key => $value ) { |
2779 | $tocData->setExtensionData( $key, $value ); |
2780 | } |
2781 | } |
2782 | } |
2783 | $this->mProperties = self::detectAndDecodeBinary( $jsonData['Properties'] ); |
2784 | $this->mTimestamp = $jsonData['Timestamp']; |
2785 | $this->mEnableOOUI = $jsonData['EnableOOUI']; |
2786 | $this->setIndexPolicy( $jsonData['IndexPolicy'] ); |
2787 | $this->mExtensionData = $jsonData['ExtensionData'] ?? []; |
2788 | $this->mLimitReportData = $jsonData['LimitReportData']; |
2789 | $this->mLimitReportJSData = $jsonData['LimitReportJSData']; |
2790 | $this->mCacheMessage = $jsonData['CacheMessage'] ?? ''; |
2791 | $this->mParseStartTime = $jsonData['ParseStartTime']; // useless! |
2792 | $this->mTimeProfile = $jsonData['TimeProfile'] ?? []; |
2793 | $this->mPreventClickjacking = $jsonData['PreventClickjacking']; |
2794 | $this->mExtraScriptSrcs = $jsonData['ExtraScriptSrcs']; |
2795 | $this->mExtraDefaultSrcs = $jsonData['ExtraDefaultSrcs']; |
2796 | $this->mExtraStyleSrcs = $jsonData['ExtraStyleSrcs']; |
2797 | $this->mSpeculativeRevId = $jsonData['SpeculativeRevId']; |
2798 | $this->speculativePageIdUsed = $jsonData['SpeculativePageIdUsed']; |
2799 | $this->revisionTimestampUsed = $jsonData['RevisionTimestampUsed']; |
2800 | $this->revisionUsedSha1Base36 = $jsonData['RevisionUsedSha1Base36']; |
2801 | $this->mWrapperDivClasses = $jsonData['WrapperDivClasses']; |
2802 | $this->mMaxAdaptiveExpiry = $jsonData['MaxAdaptiveExpiry'] ?? INF; |
2803 | } |
2804 | |
2805 | /** |
2806 | * Finds any non-utf8 strings in the given array and replaces them with |
2807 | * an associative array that wraps a base64 encoded version of the data. |
2808 | * Inverse of detectAndDecodeBinary(). |
2809 | * |
2810 | * @param array $properties |
2811 | * |
2812 | * @return array |
2813 | */ |
2814 | private static function detectAndEncodeBinary( array $properties ) { |
2815 | foreach ( $properties as $key => $value ) { |
2816 | if ( is_string( $value ) ) { |
2817 | if ( !mb_detect_encoding( $value, 'UTF-8', true ) ) { |
2818 | $properties[$key] = [ |
2819 | // T313818: This key name conflicts with JsonCodec |
2820 | '_type_' => 'string', |
2821 | '_encoding_' => 'base64', |
2822 | '_data_' => base64_encode( $value ), |
2823 | ]; |
2824 | } |
2825 | } |
2826 | } |
2827 | |
2828 | return $properties; |
2829 | } |
2830 | |
2831 | /** |
2832 | * Finds any associative arrays that represent encoded binary strings, and |
2833 | * replaces them with the decoded binary data. |
2834 | * |
2835 | * @param array $properties |
2836 | * |
2837 | * @return array |
2838 | */ |
2839 | private static function detectAndDecodeBinary( array $properties ) { |
2840 | foreach ( $properties as $key => $value ) { |
2841 | if ( is_array( $value ) && isset( $value['_encoding_'] ) ) { |
2842 | if ( $value['_encoding_'] === 'base64' ) { |
2843 | $properties[$key] = base64_decode( $value['_data_'] ); |
2844 | } |
2845 | } |
2846 | } |
2847 | |
2848 | return $properties; |
2849 | } |
2850 | |
2851 | public function __wakeup() { |
2852 | // Backwards compatibility, pre 1.36 |
2853 | $priorAccessedOptions = $this->getGhostFieldValue( 'mAccessedOptions' ); |
2854 | if ( $priorAccessedOptions ) { |
2855 | $this->mParseUsedOptions = $priorAccessedOptions; |
2856 | } |
2857 | // Backwards compatibility, pre 1.39 |
2858 | $priorIndexPolicy = $this->getGhostFieldValue( 'mIndexPolicy' ); |
2859 | if ( $priorIndexPolicy ) { |
2860 | $this->setIndexPolicy( $priorIndexPolicy ); |
2861 | } |
2862 | // Backwards compatibility, pre 1.40 |
2863 | $mSections = $this->getGhostFieldValue( 'mSections' ); |
2864 | if ( $mSections !== null && $mSections !== [] ) { |
2865 | $this->setSections( $mSections ); |
2866 | } |
2867 | // Backwards compatibility, pre 1.42 |
2868 | $mModules = $this->getGhostFieldValue( 'mModules' ); |
2869 | if ( $mModules !== null && $mModules !== [] ) { |
2870 | $this->addModules( $mModules ); |
2871 | } |
2872 | // Backwards compatibility, pre 1.42 |
2873 | $mModuleStyles = $this->getGhostFieldValue( 'mModuleStyles' ); |
2874 | if ( $mModuleStyles !== null && $mModuleStyles !== [] ) { |
2875 | $this->addModuleStyles( $mModuleStyles ); |
2876 | } |
2877 | // Backwards compatibility, pre 1.42 |
2878 | $mText = $this->getGhostFieldValue( 'mText' ); |
2879 | if ( $mText !== null ) { |
2880 | $this->setRawText( $mText ); |
2881 | } |
2882 | } |
2883 | |
2884 | public function __clone() { |
2885 | // It seems that very little of this object needs to be explicitly deep-cloned |
2886 | // while keeping copies reasonably separated. |
2887 | // Most of the non-scalar properties of this object are either |
2888 | // - (potentially multi-nested) arrays of scalars (which get deep-cloned), or |
2889 | // - arrays that may contain arbitrary elements (which don't necessarily get |
2890 | // deep-cloned), but for which no particular care elsewhere is given to |
2891 | // copying their references around (e.g. mJsConfigVars). |
2892 | // Hence, we are not going out of our way to ensure that the references to innermost |
2893 | // objects that may appear in a ParserOutput are unique. If that becomes the |
2894 | // expectation at any point, this method will require updating as well. |
2895 | // The exception is TOCData (which is an object), which we clone explicitly. |
2896 | if ( $this->mTOCData ) { |
2897 | $this->mTOCData = clone $this->mTOCData; |
2898 | } |
2899 | } |
2900 | |
2901 | /** |
2902 | * Returns the content holder text of the ParserOutput. |
2903 | * This will eventually be replaced by something like getContentHolder()->getText() when we have a |
2904 | * ContentHolder/HtmlHolder class. |
2905 | * @internal |
2906 | * @unstable |
2907 | * @return string |
2908 | */ |
2909 | public function getContentHolderText(): string { |
2910 | return $this->getRawText(); |
2911 | } |
2912 | |
2913 | /** |
2914 | * Sets the content holder text of the ParserOutput. |
2915 | * This will eventually be replaced by something like getContentHolder()->setText() when we have a |
2916 | * ContentHolder/HtmlHolder class. |
2917 | * @internal |
2918 | * @unstable |
2919 | */ |
2920 | public function setContentHolderText( string $s ): void { |
2921 | $this->setRawText( $s ); |
2922 | } |
2923 | |
2924 | public function __get( $name ) { |
2925 | if ( property_exists( get_called_class(), $name ) ) { |
2926 | // Direct access to a public property, deprecated. |
2927 | wfDeprecatedMsg( "ParserOutput::{$name} public read access deprecated", '1.38' ); |
2928 | return $this->$name; |
2929 | } elseif ( property_exists( $this, $name ) ) { |
2930 | // Dynamic property access, deprecated. |
2931 | wfDeprecatedMsg( "ParserOutput::{$name} dynamic property read access deprecated", '1.38' ); |
2932 | return $this->$name; |
2933 | } else { |
2934 | trigger_error( "Inaccessible property via __get(): $name" ); |
2935 | return null; |
2936 | } |
2937 | } |
2938 | |
2939 | public function __set( $name, $value ) { |
2940 | if ( property_exists( get_called_class(), $name ) ) { |
2941 | // Direct access to a public property, deprecated. |
2942 | wfDeprecatedMsg( "ParserOutput::$name public write access deprecated", '1.38' ); |
2943 | $this->$name = $value; |
2944 | } else { |
2945 | // Dynamic property access, deprecated. |
2946 | wfDeprecatedMsg( "ParserOutput::$name dynamic property write access deprecated", '1.38' ); |
2947 | $this->$name = $value; |
2948 | } |
2949 | } |
2950 | } |
2951 | |
2952 | /** @deprecated class alias since 1.41 */ |
2953 | class_alias( ParserOutput::class, 'ParserOutput' ); |