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\RevisionRenderer
;
23 use MediaWiki\Storage\MutableRevisionRecord
;
24 use MediaWiki\Storage\RevisionRecord
;
25 use MediaWiki\Storage\RevisionStore
;
26 use MediaWiki\Storage\SlotRecord
;
28 class PoolWorkArticleView
extends PoolCounterWork
{
38 /** @var ParserCache */
41 /** @var ParserOptions */
42 private $parserOptions;
44 /** @var RevisionRecord|null */
45 private $revision = null;
47 /** @var RevisionStore */
48 private $revisionStore = null;
50 /** @var RevisionRenderer */
51 private $renderer = null;
53 /** @var ParserOutput|bool */
54 private $parserOutput = false;
57 private $isDirty = false;
59 /** @var Status|bool */
60 private $error = false;
63 * @param WikiPage $page
64 * @param ParserOptions $parserOptions ParserOptions to use for the parse
65 * @param int $revid ID of the revision being parsed.
66 * @param bool $useParserCache Whether to use the parser cache.
68 * @param RevisionRecord|Content|string|null $revision Revision to render, or null to load it;
69 * may also be given as a wikitext string, or a Content object, for BC.
71 public function __construct( WikiPage
$page, ParserOptions
$parserOptions,
72 $revid, $useParserCache, $revision = null
74 if ( is_string( $revision ) ) { // BC: very old style call
75 $modelId = $page->getRevision()->getContentModel();
76 $format = $page->getRevision()->getContentFormat();
77 $revision = ContentHandler
::makeContent( $revision, $page->getTitle(), $modelId, $format );
80 if ( $revision instanceof Content
) { // BC: old style call
82 $revision = new MutableRevisionRecord( $page->getTitle() );
83 $revision->setId( $revid );
84 $revision->setPageId( $page->getId() );
85 $revision->setContent( SlotRecord
::MAIN
, $content );
89 // Check that the RevisionRecord matches $revid and $page, but still allow
90 // fake RevisionRecords coming from errors or hooks in Article to be rendered.
91 if ( $revision->getId() && $revision->getId() !== $revid ) {
92 throw new InvalidArgumentException( '$revid parameter mismatches $revision parameter' );
94 if ( $revision->getPageId()
95 && $revision->getPageId() !== $page->getTitle()->getArticleID()
97 throw new InvalidArgumentException( '$page parameter mismatches $revision parameter' );
101 // TODO: DI: inject services
102 $this->renderer
= MediaWikiServices
::getInstance()->getRevisionRenderer();
103 $this->revisionStore
= MediaWikiServices
::getInstance()->getRevisionStore();
104 $this->parserCache
= MediaWikiServices
::getInstance()->getParserCache();
107 $this->revid
= $revid;
108 $this->cacheable
= $useParserCache;
109 $this->parserOptions
= $parserOptions;
110 $this->revision
= $revision;
111 $this->cacheKey
= $this->parserCache
->getKey( $page, $parserOptions );
112 $keyPrefix = $this->cacheKey ?
: wfMemcKey( 'articleview', 'missingcachekey' );
114 parent
::__construct( 'ArticleView', $keyPrefix . ':revid:' . $revid );
118 * Get the ParserOutput from this object, or false in case of failure
120 * @return ParserOutput|bool
122 public function getParserOutput() {
123 return $this->parserOutput
;
127 * Get whether the ParserOutput is a dirty one (i.e. expired)
131 public function getIsDirty() {
132 return $this->isDirty
;
136 * Get a Status object in case of error or false otherwise
138 * @return Status|bool
140 public function getError() {
147 public function doWork() {
148 global $wgUseFileCache;
150 // @todo several of the methods called on $this->page are not declared in Page, but present
151 // in WikiPage and delegated by Article.
153 $isCurrent = $this->revid
=== $this->page
->getLatest();
155 // Bypass audience check for current revision
156 $audience = $isCurrent ? RevisionRecord
::RAW
: RevisionRecord
::FOR_PUBLIC
;
158 if ( $this->revision
!== null ) {
159 $rev = $this->revision
;
160 } elseif ( $isCurrent ) {
161 $rev = $this->page
->getRevision()
162 ?
$this->page
->getRevision()->getRevisionRecord()
165 $rev = $this->revisionStore
->getRevisionByTitle( $this->page
->getTitle(), $this->revid
);
173 $renderedRevision = $this->renderer
->getRenderedRevision(
175 $this->parserOptions
,
177 [ 'audience' => $audience ]
180 if ( !$renderedRevision ) {
181 // audience check failed
185 // Reduce effects of race conditions for slow parses (T48014)
186 $cacheTime = wfTimestampNow();
188 $time = - microtime( true );
189 $this->parserOutput
= $renderedRevision->getRevisionParserOutput();
190 $time +
= microtime( true );
194 // TODO: Use Parser's logger (once it has one)
195 $logger = MediaWiki\Logger\LoggerFactory
::getInstance( 'slow-parse' );
196 $logger->info( '{time} {title}', [
197 'time' => number_format( $time, 2 ),
198 'title' => $this->page
->getTitle()->getPrefixedDBkey(),
199 'ns' => $this->page
->getTitle()->getNamespace(),
204 if ( $this->cacheable
&& $this->parserOutput
->isCacheable() && $isCurrent ) {
205 $this->parserCache
->save(
206 $this->parserOutput
, $this->page
, $this->parserOptions
, $cacheTime, $this->revid
);
209 // Make sure file cache is not used on uncacheable content.
210 // Output that has magic words in it can still use the parser cache
211 // (if enabled), though it will generally expire sooner.
212 if ( !$this->parserOutput
->isCacheable() ) {
213 $wgUseFileCache = false;
217 $this->page
->triggerOpportunisticLinksUpdate( $this->parserOutput
);
226 public function getCachedWork() {
227 $this->parserOutput
= $this->parserCache
->get( $this->page
, $this->parserOptions
);
229 if ( $this->parserOutput
=== false ) {
230 wfDebug( __METHOD__
. ": parser cache miss\n" );
233 wfDebug( __METHOD__
. ": parser cache hit\n" );
241 public function fallback() {
242 $this->parserOutput
= $this->parserCache
->getDirty( $this->page
, $this->parserOptions
);
244 if ( $this->parserOutput
=== false ) {
245 wfDebugLog( 'dirty', 'dirty missing' );
246 wfDebug( __METHOD__
. ": no dirty cache\n" );
249 wfDebug( __METHOD__
. ": sending dirty output\n" );
250 wfDebugLog( 'dirty', "dirty output {$this->cacheKey}" );
251 $this->isDirty
= true;
257 * @param Status $status
260 public function error( $status ) {
261 $this->error
= $status;