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
21 use MediaWiki\MediaWikiServices
;
22 use MediaWiki\Revision\MutableRevisionRecord
;
23 use MediaWiki\Revision\RevisionRecord
;
24 use MediaWiki\Revision\RevisionRenderer
;
25 use MediaWiki\Revision\RevisionStore
;
26 use MediaWiki\Revision\SlotRecord
;
28 class PoolWorkArticleView
extends PoolCounterWork
{
38 /** @var ParserCache */
41 /** @var ParserOptions */
42 private $parserOptions;
44 /** @var RevisionRecord|null */
45 private $revision = null;
50 /** @var RevisionStore */
51 private $revisionStore = null;
53 /** @var RevisionRenderer */
54 private $renderer = null;
56 /** @var ParserOutput|bool */
57 private $parserOutput = false;
60 private $isDirty = false;
62 /** @var Status|bool */
63 private $error = false;
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.
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
75 public function __construct( WikiPage
$page, ParserOptions
$parserOptions,
76 $revid, $useParserCache, $revision = null, $audience = RevisionRecord
::FOR_PUBLIC
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 );
84 if ( $revision instanceof Content
) { // BC: old style call
86 $revision = new MutableRevisionRecord( $page->getTitle() );
87 $revision->setId( $revid );
88 $revision->setPageId( $page->getId() );
89 $revision->setContent( SlotRecord
::MAIN
, $content );
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' );
98 if ( $revision->getPageId()
99 && $revision->getPageId() !== $page->getTitle()->getArticleID()
101 throw new InvalidArgumentException( '$page parameter mismatches $revision parameter' );
105 // TODO: DI: inject services
106 $this->renderer
= MediaWikiServices
::getInstance()->getRevisionRenderer();
107 $this->revisionStore
= MediaWikiServices
::getInstance()->getRevisionStore();
108 $this->parserCache
= MediaWikiServices
::getInstance()->getParserCache();
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 ?
: ObjectCache
::getLocalClusterInstance()->makeKey(
118 'articleview', 'missingcachekey'
121 parent
::__construct( 'ArticleView', $keyPrefix . ':revid:' . $revid );
125 * Get the ParserOutput from this object, or false in case of failure
127 * @return ParserOutput|bool
129 public function getParserOutput() {
130 return $this->parserOutput
;
134 * Get whether the ParserOutput is a dirty one (i.e. expired)
138 public function getIsDirty() {
139 return $this->isDirty
;
143 * Get a Status object in case of error or false otherwise
145 * @return Status|bool
147 public function getError() {
154 public function doWork() {
155 global $wgUseFileCache;
157 // @todo several of the methods called on $this->page are not declared in Page, but present
158 // in WikiPage and delegated by Article.
160 $isCurrent = $this->revid
=== $this->page
->getLatest();
162 // The current revision cannot be hidden so we can skip some checks.
163 $audience = $isCurrent ? RevisionRecord
::RAW
: $this->audience
;
165 if ( $this->revision
!== null ) {
166 $rev = $this->revision
;
167 } elseif ( $isCurrent ) {
168 $rev = $this->page
->getRevision()
169 ?
$this->page
->getRevision()->getRevisionRecord()
172 $rev = $this->revisionStore
->getRevisionByTitle( $this->page
->getTitle(), $this->revid
);
180 $renderedRevision = $this->renderer
->getRenderedRevision(
182 $this->parserOptions
,
184 [ 'audience' => $audience ]
187 if ( !$renderedRevision ) {
188 // audience check failed
192 // Reduce effects of race conditions for slow parses (T48014)
193 $cacheTime = wfTimestampNow();
195 $time = - microtime( true );
196 $this->parserOutput
= $renderedRevision->getRevisionParserOutput();
197 $time +
= microtime( true );
201 // TODO: Use Parser's logger (once it has one)
202 $logger = MediaWiki\Logger\LoggerFactory
::getInstance( 'slow-parse' );
203 $logger->info( '{time} {title}', [
204 'time' => number_format( $time, 2 ),
205 'title' => $this->page
->getTitle()->getPrefixedDBkey(),
206 'ns' => $this->page
->getTitle()->getNamespace(),
211 if ( $this->cacheable
&& $this->parserOutput
->isCacheable() && $isCurrent ) {
212 $this->parserCache
->save(
213 $this->parserOutput
, $this->page
, $this->parserOptions
, $cacheTime, $this->revid
);
216 // Make sure file cache is not used on uncacheable content.
217 // Output that has magic words in it can still use the parser cache
218 // (if enabled), though it will generally expire sooner.
219 if ( !$this->parserOutput
->isCacheable() ) {
220 $wgUseFileCache = false;
224 $this->page
->triggerOpportunisticLinksUpdate( $this->parserOutput
);
233 public function getCachedWork() {
234 $this->parserOutput
= $this->parserCache
->get( $this->page
, $this->parserOptions
);
236 if ( $this->parserOutput
=== false ) {
237 wfDebug( __METHOD__
. ": parser cache miss\n" );
240 wfDebug( __METHOD__
. ": parser cache hit\n" );
248 public function fallback() {
249 $this->parserOutput
= $this->parserCache
->getDirty( $this->page
, $this->parserOptions
);
251 if ( $this->parserOutput
=== false ) {
252 wfDebugLog( 'dirty', 'dirty missing' );
253 wfDebug( __METHOD__
. ": no dirty cache\n" );
256 wfDebug( __METHOD__
. ": sending dirty output\n" );
257 wfDebugLog( 'dirty', "dirty output {$this->cacheKey}" );
258 $this->isDirty
= true;
264 * @param Status $status
267 public function error( $status ) {
268 $this->error
= $status;