Debian Codegraph @

includes/parser/Parsoid/ParsoidOutputAccess.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright (C) 2011-2022 Wikimedia Foundation and others.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 namespace MediaWiki\Parser\Parsoid;
21
22 use MediaWiki\Content\IContentHandlerFactory;
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\Page\PageIdentity;
25 use MediaWiki\Page\PageLookup;
26 use MediaWiki\Page\PageRecord;
27 use MediaWiki\Page\ParserOutputAccess;
28 use MediaWiki\Parser\ParserOptions;
29 use MediaWiki\Parser\ParserOutput;
30 use MediaWiki\Parser\Parsoid\Config\SiteConfig;
31 use MediaWiki\Revision\RevisionAccessException;
32 use MediaWiki\Revision\RevisionLookup;
33 use MediaWiki\Revision\RevisionRecord;
34 use MediaWiki\Revision\SlotRecord;
35 use MediaWiki\Status\Status;
36 use Wikimedia\Parsoid\Core\ClientError;
37 use Wikimedia\Parsoid\Core\ResourceLimitExceededException;
38
39 /**
40 * MediaWiki service for getting rendered page content.
41 *
42 * This is very similar to ParserOutputAccess and only exists as a
43 * separate class as an interim solution and should be removed soon.
44 *
45 * It is different from ParserOutputAccess in two aspects:
46 * - it forces Parsoid to be used when possible
47 * - it supports on-the-fly parsing through parseUncacheable()
48 *
49 * @since 1.39
50 * @unstable
51 * @deprecated since 1.43
52 */
53 class ParsoidOutputAccess {
54 private ParsoidParserFactory $parsoidParserFactory;
55 private PageLookup $pageLookup;
56 private RevisionLookup $revisionLookup;
57 private ParserOutputAccess $parserOutputAccess;
58 private SiteConfig $siteConfig;
59 private IContentHandlerFactory $contentHandlerFactory;
60
61 /**
62 * @param ParsoidParserFactory $parsoidParserFactory
63 * @param ParserOutputAccess $parserOutputAccess
64 * @param PageLookup $pageLookup
65 * @param RevisionLookup $revisionLookup
66 * @param SiteConfig $siteConfig
67 * @param IContentHandlerFactory $contentHandlerFactory
68 */
69 public function __construct(
70 ParsoidParserFactory $parsoidParserFactory,
71 ParserOutputAccess $parserOutputAccess,
72 PageLookup $pageLookup,
73 RevisionLookup $revisionLookup,
74 SiteConfig $siteConfig,
75 IContentHandlerFactory $contentHandlerFactory
76 ) {
77 $this->parsoidParserFactory = $parsoidParserFactory;
78 $this->parserOutputAccess = $parserOutputAccess;
79 $this->pageLookup = $pageLookup;
80 $this->revisionLookup = $revisionLookup;
81 $this->siteConfig = $siteConfig;
82 $this->contentHandlerFactory = $contentHandlerFactory;
83 }
84
85 /**
86 * @param PageIdentity $page
87 * @param ParserOptions $parserOpts
88 * @param RevisionRecord|int|null $revision
89 * @param int $options See the OPT_XXX constants
90 * @param bool $lenientRevHandling
91 *
92 * @return Status<ParserOutput>
93 * @deprecated since 1.43
94 */
95 public function getParserOutput(
96 PageIdentity $page,
97 ParserOptions $parserOpts,
98 $revision = null,
99 int $options = 0,
100 bool $lenientRevHandling = false
101 ): Status {
102 wfDeprecated( __METHOD__, '1.43' );
103 [ $page, $revision, $uncacheable ] = $this->resolveRevision( $page, $revision, $lenientRevHandling );
104
105 try {
106 if ( $uncacheable ) {
107 $options |= ParserOutputAccess::OPT_NO_UPDATE_CACHE;
108 }
109
110 $this->adjustParserOptions( $revision, $parserOpts );
111 $status = $this->parserOutputAccess->getParserOutput(
112 $page, $parserOpts, $revision, $options
113 );
114 } catch ( ClientError $e ) {
115 $status = Status::newFatal( 'parsoid-client-error', $e->getMessage() );
116 } catch ( ResourceLimitExceededException $e ) {
117 $status = Status::newFatal( 'parsoid-resource-limit-exceeded', $e->getMessage() );
118 }
119 return $status;
120 }
121
122 /**
123 * @param PageIdentity $page
124 * @param ParserOptions $parserOpts
125 * @param RevisionRecord|int|null $revision
126 * @param bool $lenientRevHandling
127 *
128 * @return ?ParserOutput
129 * @deprecated since 1.43
130 */
131 public function getCachedParserOutput(
132 PageIdentity $page,
133 ParserOptions $parserOpts,
134 $revision = null,
135 bool $lenientRevHandling = false
136 ): ?ParserOutput {
137 wfDeprecated( __METHOD__, '1.43' );
138 [ $page, $revision, $ignored ] = $this->resolveRevision( $page, $revision, $lenientRevHandling );
139
140 $this->adjustParserOptions( $revision, $parserOpts );
141 return $this->parserOutputAccess->getCachedParserOutput( $page, $parserOpts, $revision );
142 }
143
144 /**
145 * This is to be called only for parsing posted wikitext that is actually
146 * not part of any real revision.
147 *
148 * @param PageIdentity $page
149 * @param ParserOptions $parserOpts
150 * @param RevisionRecord|int|null $revision
151 * @param bool $lenientRevHandling
152 *
153 * @return Status
154 * @deprecated since 1.43
155 */
156 public function parseUncacheable(
157 PageIdentity $page,
158 ParserOptions $parserOpts,
159 $revision,
160 bool $lenientRevHandling = false
161 ): Status {
162 wfDeprecated( __METHOD__, '1.43' );
163 // NOTE: If we have a RevisionRecord already, just use it, there is no need to resolve $page to
164 // a PageRecord (and it may not be possible if the page doesn't exist).
165 if ( !$revision instanceof RevisionRecord ) {
166 [ $page, $revision, $ignored ] = $this->resolveRevision( $page, $revision, $lenientRevHandling );
167 }
168
169 // Enforce caller expectation
170 $revId = $revision->getId();
171 if ( $revId !== 0 && $revId !== null ) {
172 return Status::newFatal( 'parsoid-revision-access',
173 "parseUncacheable should not be called for a real revision" );
174 }
175
176 try {
177 // Since we aren't caching this output, there is no need to
178 // call setUseParsoid() here.
179 $parser = $this->parsoidParserFactory->create();
180 $parserOutput = $this->parsoidParserFactory->create()->parseFakeRevision(
181 $revision, $page, $parserOpts );
182 $parserOutput->updateCacheExpiry( 0 ); // Ensure this isn't accidentally cached
183 // set up (fake) render id and other properties
184 $globalIdGenerator = MediaWikiServices::getInstance()->getGlobalIdGenerator();
185 $parserOutput->setRenderId( $globalIdGenerator->newUUIDv1() );
186 $parserOutput->setCacheRevisionId( $revision->getId() );
187 $parserOutput->setRevisionTimestamp( $revision->getTimestamp() );
188 $parserOutput->setCacheTime( wfTimestampNow() );
189
190 $status = Status::newGood( $parserOutput );
191 } catch ( RevisionAccessException $e ) {
192 return Status::newFatal( 'parsoid-revision-access', $e->getMessage() );
193 } catch ( ClientError $e ) {
194 $status = Status::newFatal( 'parsoid-client-error', $e->getMessage() );
195 } catch ( ResourceLimitExceededException $e ) {
196 $status = Status::newFatal( 'parsoid-resource-limit-exceeded', $e->getMessage() );
197 }
198 return $status;
199 }
200
201 /**
202 * @param PageIdentity $page
203 * @param RevisionRecord|int|null $revision
204 * @param bool $lenientRevHandling
205 *
206 * @return array [ PageRecord $page, RevisionRecord $revision ]
207 */
208 private function resolveRevision( PageIdentity $page, $revision, bool $lenientRevHandling = false ): array {
209 $uncacheable = false;
210 if ( !$page instanceof PageRecord ) {
211 $name = "$page";
212 $page = $this->pageLookup->getPageByReference( $page );
213 if ( !$page ) {
214 throw new RevisionAccessException(
215 'Page {name} not found',
216 [ 'name' => $name ]
217 );
218 }
219 }
220
221 $revision ??= $page->getLatest();
222
223 if ( is_int( $revision ) ) {
224 $revId = $revision;
225 $revision = $this->revisionLookup->getRevisionById( $revId );
226
227 if ( !$revision ) {
228 throw new RevisionAccessException(
229 'Revision {revId} not found',
230 [ 'revId' => $revId ]
231 );
232 }
233 }
234
235 if ( $page->getId() !== $revision->getPageId() ) {
236 if ( $lenientRevHandling ) {
237 $page = $this->pageLookup->getPageById( $revision->getPageId() );
238 if ( !$page ) {
239 // This should ideally never trigger!
240 throw new \RuntimeException(
241 "Unexpected NULL page for pageid " . $revision->getPageId() .
242 " from revision " . $revision->getId()
243 );
244 }
245 // Don't cache this!
246 $uncacheable = true;
247 } else {
248 throw new RevisionAccessException(
249 'Revision {revId} does not belong to page {name}',
250 [ 'name' => $page->getDBkey(), 'revId' => $revision->getId() ]
251 );
252 }
253 }
254
255 return [ $page, $revision, $uncacheable ];
256 }
257
258 private function adjustParserOptions( RevisionRecord $revision, ParserOptions $parserOpts ): void {
259 $mainSlot = $revision->getSlot( SlotRecord::MAIN );
260 $contentModel = $mainSlot->getModel();
261 if ( $this->siteConfig->supportsContentModel( $contentModel ) ) {
262 // Since we know Parsoid supports this content model, explicitly
263 // call ParserOptions::setUseParsoid. This ensures that when
264 // we query the parser-cache, the right cache key is called.
265 // This is an optional transition step to using ParserOutputAccess.
266 $parserOpts->setUseParsoid();
267 }
268 }
269 }