Merge "Add attributes parameter to ShowSearchHitTitle"
[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 use MediaWiki\MediaWikiServices;
21
22 class PoolWorkArticleView extends PoolCounterWork {
23 /** @var WikiPage */
24 private $page;
25
26 /** @var string */
27 private $cacheKey;
28
29 /** @var int */
30 private $revid;
31
32 /** @var ParserCache */
33 private $parserCache;
34
35 /** @var ParserOptions */
36 private $parserOptions;
37
38 /** @var Content|null */
39 private $content = null;
40
41 /** @var ParserOutput|bool */
42 private $parserOutput = false;
43
44 /** @var bool */
45 private $isDirty = false;
46
47 /** @var Status|bool */
48 private $error = false;
49
50 /**
51 * @param WikiPage $page
52 * @param ParserOptions $parserOptions ParserOptions to use for the parse
53 * @param int $revid ID of the revision being parsed.
54 * @param bool $useParserCache Whether to use the parser cache.
55 * operation.
56 * @param Content|string $content Content to parse or null to load it; may
57 * also be given as a wikitext string, for BC.
58 */
59 public function __construct( WikiPage $page, ParserOptions $parserOptions,
60 $revid, $useParserCache, $content = null
61 ) {
62 if ( is_string( $content ) ) { // BC: old style call
63 $modelId = $page->getRevision()->getContentModel();
64 $format = $page->getRevision()->getContentFormat();
65 $content = ContentHandler::makeContent( $content, $page->getTitle(), $modelId, $format );
66 }
67
68 $this->page = $page;
69 $this->revid = $revid;
70 $this->cacheable = $useParserCache;
71 $this->parserOptions = $parserOptions;
72 $this->content = $content;
73 $this->parserCache = MediaWikiServices::getInstance()->getParserCache();
74 $this->cacheKey = $this->parserCache->getKey( $page, $parserOptions );
75 $keyPrefix = $this->cacheKey ?: wfMemcKey( 'articleview', 'missingcachekey' );
76 parent::__construct( 'ArticleView', $keyPrefix . ':revid:' . $revid );
77 }
78
79 /**
80 * Get the ParserOutput from this object, or false in case of failure
81 *
82 * @return ParserOutput
83 */
84 public function getParserOutput() {
85 return $this->parserOutput;
86 }
87
88 /**
89 * Get whether the ParserOutput is a dirty one (i.e. expired)
90 *
91 * @return bool
92 */
93 public function getIsDirty() {
94 return $this->isDirty;
95 }
96
97 /**
98 * Get a Status object in case of error or false otherwise
99 *
100 * @return Status|bool
101 */
102 public function getError() {
103 return $this->error;
104 }
105
106 /**
107 * @return bool
108 */
109 public function doWork() {
110 global $wgUseFileCache;
111
112 // @todo several of the methods called on $this->page are not declared in Page, but present
113 // in WikiPage and delegated by Article.
114
115 $isCurrent = $this->revid === $this->page->getLatest();
116
117 if ( $this->content !== null ) {
118 $content = $this->content;
119 } elseif ( $isCurrent ) {
120 // XXX: why use RAW audience here, and PUBLIC (default) below?
121 $content = $this->page->getContent( Revision::RAW );
122 } else {
123 $rev = Revision::newFromTitle( $this->page->getTitle(), $this->revid );
124
125 if ( $rev === null ) {
126 $content = null;
127 } else {
128 // XXX: why use PUBLIC audience here (default), and RAW above?
129 $content = $rev->getContent();
130 }
131 }
132
133 if ( $content === null ) {
134 return false;
135 }
136
137 // Reduce effects of race conditions for slow parses (T48014)
138 $cacheTime = wfTimestampNow();
139
140 $time = - microtime( true );
141 $this->parserOutput = $content->getParserOutput(
142 $this->page->getTitle(),
143 $this->revid,
144 $this->parserOptions
145 );
146 $time += microtime( true );
147
148 // Timing hack
149 if ( $time > 3 ) {
150 // TODO: Use Parser's logger (once it has one)
151 $logger = MediaWiki\Logger\LoggerFactory::getInstance( 'slow-parse' );
152 $logger->info( '{time} {title}', [
153 'time' => number_format( $time, 2 ),
154 'title' => $this->page->getTitle()->getPrefixedDBkey(),
155 'ns' => $this->page->getTitle()->getNamespace(),
156 'trigger' => 'view',
157 ] );
158 }
159
160 if ( $this->cacheable && $this->parserOutput->isCacheable() && $isCurrent ) {
161 $this->parserCache->save(
162 $this->parserOutput, $this->page, $this->parserOptions, $cacheTime, $this->revid );
163 }
164
165 // Make sure file cache is not used on uncacheable content.
166 // Output that has magic words in it can still use the parser cache
167 // (if enabled), though it will generally expire sooner.
168 if ( !$this->parserOutput->isCacheable() ) {
169 $wgUseFileCache = false;
170 }
171
172 if ( $isCurrent ) {
173 $this->page->triggerOpportunisticLinksUpdate( $this->parserOutput );
174 }
175
176 return true;
177 }
178
179 /**
180 * @return bool
181 */
182 public function getCachedWork() {
183 $this->parserOutput = $this->parserCache->get( $this->page, $this->parserOptions );
184
185 if ( $this->parserOutput === false ) {
186 wfDebug( __METHOD__ . ": parser cache miss\n" );
187 return false;
188 } else {
189 wfDebug( __METHOD__ . ": parser cache hit\n" );
190 return true;
191 }
192 }
193
194 /**
195 * @return bool
196 */
197 public function fallback() {
198 $this->parserOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions );
199
200 if ( $this->parserOutput === false ) {
201 wfDebugLog( 'dirty', 'dirty missing' );
202 wfDebug( __METHOD__ . ": no dirty cache\n" );
203 return false;
204 } else {
205 wfDebug( __METHOD__ . ": sending dirty output\n" );
206 wfDebugLog( 'dirty', "dirty output {$this->cacheKey}" );
207 $this->isDirty = true;
208 return true;
209 }
210 }
211
212 /**
213 * @param Status $status
214 * @return bool
215 */
216 public function error( $status ) {
217 $this->error = $status;
218 return false;
219 }
220 }