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