Merge "Simplify HTMLTitleTextField::validate"
[lhc/web/wiklou.git] / includes / poolcounter / PoolWorkArticleView.php
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 use MediaWiki\MediaWikiServices;
22 use MediaWiki\Revision\RevisionRenderer;
23 use MediaWiki\Storage\MutableRevisionRecord;
24 use MediaWiki\Storage\RevisionRecord;
25 use MediaWiki\Storage\RevisionStore;
26 use MediaWiki\Storage\SlotRecord;
27
28 class PoolWorkArticleView extends PoolCounterWork {
29 /** @var WikiPage */
30 private $page;
31
32 /** @var string */
33 private $cacheKey;
34
35 /** @var int */
36 private $revid;
37
38 /** @var ParserCache */
39 private $parserCache;
40
41 /** @var ParserOptions */
42 private $parserOptions;
43
44 /** @var RevisionRecord|null */
45 private $revision = null;
46
47 /** @var RevisionStore */
48 private $revisionStore = null;
49
50 /** @var RevisionRenderer */
51 private $renderer = null;
52
53 /** @var ParserOutput|bool */
54 private $parserOutput = false;
55
56 /** @var bool */
57 private $isDirty = false;
58
59 /** @var Status|bool */
60 private $error = false;
61
62 /**
63 * @param WikiPage $page
64 * @param ParserOptions $parserOptions ParserOptions to use for the parse
65 * @param int $revid ID of the revision being parsed.
66 * @param bool $useParserCache Whether to use the parser cache.
67 * operation.
68 * @param RevisionRecord|Content|string|null $revision Revision to render, or null to load it;
69 * may also be given as a wikitext string, or a Content object, for BC.
70 */
71 public function __construct( WikiPage $page, ParserOptions $parserOptions,
72 $revid, $useParserCache, $revision = null
73 ) {
74 if ( is_string( $revision ) ) { // BC: very old style call
75 $modelId = $page->getRevision()->getContentModel();
76 $format = $page->getRevision()->getContentFormat();
77 $revision = ContentHandler::makeContent( $revision, $page->getTitle(), $modelId, $format );
78 }
79
80 if ( $revision instanceof Content ) { // BC: old style call
81 $content = $revision;
82 $revision = new MutableRevisionRecord( $page->getTitle() );
83 $revision->setId( $revid );
84 $revision->setPageId( $page->getId() );
85 $revision->setContent( SlotRecord::MAIN, $content );
86 }
87
88 if ( $revision ) {
89 // Check that the RevisionRecord matches $revid and $page, but still allow
90 // fake RevisionRecords coming from errors or hooks in Article to be rendered.
91 if ( $revision->getId() && $revision->getId() !== $revid ) {
92 throw new InvalidArgumentException( '$revid parameter mismatches $revision parameter' );
93 }
94 if ( $revision->getPageId()
95 && $revision->getPageId() !== $page->getTitle()->getArticleID()
96 ) {
97 throw new InvalidArgumentException( '$page parameter mismatches $revision parameter' );
98 }
99 }
100
101 // TODO: DI: inject services
102 $this->renderer = MediaWikiServices::getInstance()->getRevisionRenderer();
103 $this->revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
104 $this->parserCache = MediaWikiServices::getInstance()->getParserCache();
105
106 $this->page = $page;
107 $this->revid = $revid;
108 $this->cacheable = $useParserCache;
109 $this->parserOptions = $parserOptions;
110 $this->revision = $revision;
111 $this->cacheKey = $this->parserCache->getKey( $page, $parserOptions );
112 $keyPrefix = $this->cacheKey ?: wfMemcKey( 'articleview', 'missingcachekey' );
113
114 parent::__construct( 'ArticleView', $keyPrefix . ':revid:' . $revid );
115 }
116
117 /**
118 * Get the ParserOutput from this object, or false in case of failure
119 *
120 * @return ParserOutput|bool
121 */
122 public function getParserOutput() {
123 return $this->parserOutput;
124 }
125
126 /**
127 * Get whether the ParserOutput is a dirty one (i.e. expired)
128 *
129 * @return bool
130 */
131 public function getIsDirty() {
132 return $this->isDirty;
133 }
134
135 /**
136 * Get a Status object in case of error or false otherwise
137 *
138 * @return Status|bool
139 */
140 public function getError() {
141 return $this->error;
142 }
143
144 /**
145 * @return bool
146 */
147 public function doWork() {
148 global $wgUseFileCache;
149
150 // @todo several of the methods called on $this->page are not declared in Page, but present
151 // in WikiPage and delegated by Article.
152
153 $isCurrent = $this->revid === $this->page->getLatest();
154
155 // Bypass audience check for current revision
156 $audience = $isCurrent ? RevisionRecord::RAW : RevisionRecord::FOR_PUBLIC;
157
158 if ( $this->revision !== null ) {
159 $rev = $this->revision;
160 } elseif ( $isCurrent ) {
161 $rev = $this->page->getRevision()
162 ? $this->page->getRevision()->getRevisionRecord()
163 : null;
164 } else {
165 $rev = $this->revisionStore->getRevisionByTitle( $this->page->getTitle(), $this->revid );
166 }
167
168 if ( !$rev ) {
169 // couldn't load
170 return false;
171 }
172
173 $renderedRevision = $this->renderer->getRenderedRevision(
174 $rev,
175 $this->parserOptions,
176 null,
177 [ 'audience' => $audience ]
178 );
179
180 if ( !$renderedRevision ) {
181 // audience check failed
182 return false;
183 }
184
185 // Reduce effects of race conditions for slow parses (T48014)
186 $cacheTime = wfTimestampNow();
187
188 $time = - microtime( true );
189 $this->parserOutput = $renderedRevision->getRevisionParserOutput();
190 $time += microtime( true );
191
192 // Timing hack
193 if ( $time > 3 ) {
194 // TODO: Use Parser's logger (once it has one)
195 $logger = MediaWiki\Logger\LoggerFactory::getInstance( 'slow-parse' );
196 $logger->info( '{time} {title}', [
197 'time' => number_format( $time, 2 ),
198 'title' => $this->page->getTitle()->getPrefixedDBkey(),
199 'ns' => $this->page->getTitle()->getNamespace(),
200 'trigger' => 'view',
201 ] );
202 }
203
204 if ( $this->cacheable && $this->parserOutput->isCacheable() && $isCurrent ) {
205 $this->parserCache->save(
206 $this->parserOutput, $this->page, $this->parserOptions, $cacheTime, $this->revid );
207 }
208
209 // Make sure file cache is not used on uncacheable content.
210 // Output that has magic words in it can still use the parser cache
211 // (if enabled), though it will generally expire sooner.
212 if ( !$this->parserOutput->isCacheable() ) {
213 $wgUseFileCache = false;
214 }
215
216 if ( $isCurrent ) {
217 $this->page->triggerOpportunisticLinksUpdate( $this->parserOutput );
218 }
219
220 return true;
221 }
222
223 /**
224 * @return bool
225 */
226 public function getCachedWork() {
227 $this->parserOutput = $this->parserCache->get( $this->page, $this->parserOptions );
228
229 if ( $this->parserOutput === false ) {
230 wfDebug( __METHOD__ . ": parser cache miss\n" );
231 return false;
232 } else {
233 wfDebug( __METHOD__ . ": parser cache hit\n" );
234 return true;
235 }
236 }
237
238 /**
239 * @return bool
240 */
241 public function fallback() {
242 $this->parserOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions );
243
244 if ( $this->parserOutput === false ) {
245 wfDebugLog( 'dirty', 'dirty missing' );
246 wfDebug( __METHOD__ . ": no dirty cache\n" );
247 return false;
248 } else {
249 wfDebug( __METHOD__ . ": sending dirty output\n" );
250 wfDebugLog( 'dirty', "dirty output {$this->cacheKey}" );
251 $this->isDirty = true;
252 return true;
253 }
254 }
255
256 /**
257 * @param Status $status
258 * @return bool
259 */
260 public function error( $status ) {
261 $this->error = $status;
262 return false;
263 }
264 }