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