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
;
27 class PoolWorkArticleView
extends PoolCounterWork
{
37 /** @var ParserCache */
40 /** @var ParserOptions */
41 private $parserOptions;
43 /** @var RevisionRecord|null */
44 private $revision = null;
46 /** @var RevisionStore */
47 private $revisionStore = null;
49 /** @var RevisionRenderer */
50 private $renderer = null;
52 /** @var ParserOutput|bool */
53 private $parserOutput = false;
56 private $isDirty = false;
58 /** @var Status|bool */
59 private $error = false;
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.
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.
70 public function __construct( WikiPage
$page, ParserOptions
$parserOptions,
71 $revid, $useParserCache, $revision = null
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 );
79 if ( $revision instanceof Content
) { // BC: old style call
81 $revision = new MutableRevisionRecord( $page->getTitle() );
82 $revision->setId( $revid );
83 $revision->setPageId( $page->getId() );
84 $revision->setContent( 'main', $content );
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' );
93 if ( $revision->getPageId()
94 && $revision->getPageId() !== $page->getTitle()->getArticleID()
96 throw new InvalidArgumentException( '$page parameter mismatches $revision parameter' );
100 // TODO: DI: inject services
101 $this->renderer
= MediaWikiServices
::getInstance()->getRevisionRenderer();
102 $this->revisionStore
= MediaWikiServices
::getInstance()->getRevisionStore();
103 $this->parserCache
= MediaWikiServices
::getInstance()->getParserCache();
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' );
113 parent
::__construct( 'ArticleView', $keyPrefix . ':revid:' . $revid );
117 * Get the ParserOutput from this object, or false in case of failure
119 * @return ParserOutput|bool
121 public function getParserOutput() {
122 return $this->parserOutput
;
126 * Get whether the ParserOutput is a dirty one (i.e. expired)
130 public function getIsDirty() {
131 return $this->isDirty
;
135 * Get a Status object in case of error or false otherwise
137 * @return Status|bool
139 public function getError() {
146 public function doWork() {
147 global $wgUseFileCache;
149 // @todo several of the methods called on $this->page are not declared in Page, but present
150 // in WikiPage and delegated by Article.
152 $isCurrent = $this->revid
=== $this->page
->getLatest();
154 // Bypass audience check for current revision
155 $audience = $isCurrent ? RevisionRecord
::RAW
: RevisionRecord
::FOR_PUBLIC
;
157 if ( $this->revision
!== null ) {
158 $rev = $this->revision
;
159 } elseif ( $isCurrent ) {
160 $rev = $this->page
->getRevision()
161 ?
$this->page
->getRevision()->getRevisionRecord()
164 $rev = $this->revisionStore
->getRevisionByTitle( $this->page
->getTitle(), $this->revid
);
172 $renderedRevision = $this->renderer
->getRenderedRevision(
174 $this->parserOptions
,
176 [ 'audience' => $audience ]
179 if ( !$renderedRevision ) {
180 // audience check failed
184 // Reduce effects of race conditions for slow parses (T48014)
185 $cacheTime = wfTimestampNow();
187 $time = - microtime( true );
188 $this->parserOutput
= $renderedRevision->getRevisionParserOutput();
189 $time +
= microtime( true );
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(),
203 if ( $this->cacheable
&& $this->parserOutput
->isCacheable() && $isCurrent ) {
204 $this->parserCache
->save(
205 $this->parserOutput
, $this->page
, $this->parserOptions
, $cacheTime, $this->revid
);
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;
216 $this->page
->triggerOpportunisticLinksUpdate( $this->parserOutput
);
225 public function getCachedWork() {
226 $this->parserOutput
= $this->parserCache
->get( $this->page
, $this->parserOptions
);
228 if ( $this->parserOutput
=== false ) {
229 wfDebug( __METHOD__
. ": parser cache miss\n" );
232 wfDebug( __METHOD__
. ": parser cache hit\n" );
240 public function fallback() {
241 $this->parserOutput
= $this->parserCache
->getDirty( $this->page
, $this->parserOptions
);
243 if ( $this->parserOutput
=== false ) {
244 wfDebugLog( 'dirty', 'dirty missing' );
245 wfDebug( __METHOD__
. ": no dirty cache\n" );
248 wfDebug( __METHOD__
. ": sending dirty output\n" );
249 wfDebugLog( 'dirty', "dirty output {$this->cacheKey}" );
250 $this->isDirty
= true;
256 * @param Status $status
259 public function error( $status ) {
260 $this->error
= $status;