Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / poolcounter / PoolWorkArticleView.php
blob534e86b6bead93ab0a443165808e03258b9a8b9d
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.
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.
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
18 * @file
21 class PoolWorkArticleView extends PoolCounterWork {
22 /** @var WikiPage */
23 private $page;
25 /** @var string */
26 private $cacheKey;
28 /** @var int */
29 private $revid;
31 /** @var ParserOptions */
32 private $parserOptions;
34 /** @var Content|null */
35 private $content = null;
37 /** @var ParserOutput|bool */
38 private $parserOutput = false;
40 /** @var bool */
41 private $isDirty = false;
43 /** @var Status|bool */
44 private $error = false;
46 /**
47 * @param WikiPage $page
48 * @param ParserOptions $parserOptions ParserOptions to use for the parse
49 * @param int $revid ID of the revision being parsed.
50 * @param bool $useParserCache Whether to use the parser cache.
51 * operation.
52 * @param Content|string $content Content to parse or null to load it; may
53 * also be given as a wikitext string, for BC.
55 public function __construct( WikiPage $page, ParserOptions $parserOptions,
56 $revid, $useParserCache, $content = null
57 ) {
58 if ( is_string( $content ) ) { // BC: old style call
59 $modelId = $page->getRevision()->getContentModel();
60 $format = $page->getRevision()->getContentFormat();
61 $content = ContentHandler::makeContent( $content, $page->getTitle(), $modelId, $format );
64 $this->page = $page;
65 $this->revid = $revid;
66 $this->cacheable = $useParserCache;
67 $this->parserOptions = $parserOptions;
68 $this->content = $content;
69 $this->cacheKey = ParserCache::singleton()->getKey( $page, $parserOptions );
70 $keyPrefix = $this->cacheKey ?: wfMemcKey( 'articleview', 'missingcachekey' );
71 parent::__construct( 'ArticleView', $keyPrefix . ':revid:' . $revid );
74 /**
75 * Get the ParserOutput from this object, or false in case of failure
77 * @return ParserOutput
79 public function getParserOutput() {
80 return $this->parserOutput;
83 /**
84 * Get whether the ParserOutput is a dirty one (i.e. expired)
86 * @return bool
88 public function getIsDirty() {
89 return $this->isDirty;
92 /**
93 * Get a Status object in case of error or false otherwise
95 * @return Status|bool
97 public function getError() {
98 return $this->error;
102 * @return bool
104 public function doWork() {
105 global $wgUseFileCache;
107 // @todo several of the methods called on $this->page are not declared in Page, but present
108 // in WikiPage and delegated by Article.
110 $isCurrent = $this->revid === $this->page->getLatest();
112 if ( $this->content !== null ) {
113 $content = $this->content;
114 } elseif ( $isCurrent ) {
115 // XXX: why use RAW audience here, and PUBLIC (default) below?
116 $content = $this->page->getContent( Revision::RAW );
117 } else {
118 $rev = Revision::newFromTitle( $this->page->getTitle(), $this->revid );
120 if ( $rev === null ) {
121 $content = null;
122 } else {
123 // XXX: why use PUBLIC audience here (default), and RAW above?
124 $content = $rev->getContent();
128 if ( $content === null ) {
129 return false;
132 // Reduce effects of race conditions for slow parses (bug 46014)
133 $cacheTime = wfTimestampNow();
135 $time = - microtime( true );
136 $this->parserOutput = $content->getParserOutput(
137 $this->page->getTitle(),
138 $this->revid,
139 $this->parserOptions
141 $time += microtime( true );
143 // Timing hack
144 if ( $time > 3 ) {
145 // TODO: Use Parser's logger (once it has one)
146 $logger = MediaWiki\Logger\LoggerFactory::getInstance( 'slow-parse' );
147 $logger->info( '{time} {title}', [
148 'time' => number_format( $time, 2 ),
149 'title' => $this->page->getTitle()->getPrefixedDBkey(),
150 'ns' => $this->page->getTitle()->getNamespace(),
151 'trigger' => 'view',
152 ] );
155 if ( $this->cacheable && $this->parserOutput->isCacheable() && $isCurrent ) {
156 ParserCache::singleton()->save(
157 $this->parserOutput, $this->page, $this->parserOptions, $cacheTime, $this->revid );
160 // Make sure file cache is not used on uncacheable content.
161 // Output that has magic words in it can still use the parser cache
162 // (if enabled), though it will generally expire sooner.
163 if ( !$this->parserOutput->isCacheable() ) {
164 $wgUseFileCache = false;
167 if ( $isCurrent ) {
168 $this->page->triggerOpportunisticLinksUpdate( $this->parserOutput );
171 return true;
175 * @return bool
177 public function getCachedWork() {
178 $this->parserOutput = ParserCache::singleton()->get( $this->page, $this->parserOptions );
180 if ( $this->parserOutput === false ) {
181 wfDebug( __METHOD__ . ": parser cache miss\n" );
182 return false;
183 } else {
184 wfDebug( __METHOD__ . ": parser cache hit\n" );
185 return true;
190 * @return bool
192 public function fallback() {
193 $this->parserOutput = ParserCache::singleton()->getDirty( $this->page, $this->parserOptions );
195 if ( $this->parserOutput === false ) {
196 wfDebugLog( 'dirty', 'dirty missing' );
197 wfDebug( __METHOD__ . ": no dirty cache\n" );
198 return false;
199 } else {
200 wfDebug( __METHOD__ . ": sending dirty output\n" );
201 wfDebugLog( 'dirty', "dirty output {$this->cacheKey}" );
202 $this->isDirty = true;
203 return true;
208 * @param Status $status
209 * @return bool
211 public function error( $status ) {
212 $this->error = $status;
213 return false;