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
20 use MediaWiki\MediaWikiServices;
22 class PoolWorkArticleView extends PoolCounterWork {
32 /** @var ParserCache */
35 /** @var ParserOptions */
36 private $parserOptions;
38 /** @var Content|null */
39 private $content = null;
41 /** @var ParserOutput|bool */
42 private $parserOutput = false;
45 private $isDirty = false;
47 /** @var Status|bool */
48 private $error = false;
51 * @param WikiPage $page
52 * @param ParserOptions $parserOptions ParserOptions to use for the parse
53 * @param int $revid ID of the revision being parsed.
54 * @param bool $useParserCache Whether to use the parser cache.
56 * @param Content|string $content Content to parse or null to load it; may
57 * also be given as a wikitext string, for BC.
59 public function __construct( WikiPage $page, ParserOptions $parserOptions,
60 $revid, $useParserCache, $content = null
62 if ( is_string( $content ) ) { // BC: old style call
63 $modelId = $page->getRevision()->getContentModel();
64 $format = $page->getRevision()->getContentFormat();
65 $content = ContentHandler::makeContent( $content, $page->getTitle(), $modelId, $format );
69 $this->revid = $revid;
70 $this->cacheable = $useParserCache;
71 $this->parserOptions = $parserOptions;
72 $this->content = $content;
73 $this->parserCache = MediaWikiServices::getInstance()->getParserCache();
74 $this->cacheKey = $this->parserCache->getKey( $page, $parserOptions );
75 $keyPrefix = $this->cacheKey ?: wfMemcKey( 'articleview', 'missingcachekey' );
76 parent::__construct( 'ArticleView', $keyPrefix . ':revid:' . $revid );
80 * Get the ParserOutput from this object, or false in case of failure
82 * @return ParserOutput
84 public function getParserOutput() {
85 return $this->parserOutput;
89 * Get whether the ParserOutput is a dirty one (i.e. expired)
93 public function getIsDirty() {
94 return $this->isDirty;
98 * Get a Status object in case of error or false otherwise
100 * @return Status|bool
102 public function getError() {
109 public function doWork() {
110 global $wgUseFileCache;
112 // @todo several of the methods called on $this->page are not declared in Page, but present
113 // in WikiPage and delegated by Article.
115 $isCurrent = $this->revid === $this->page->getLatest();
117 if ( $this->content !== null ) {
118 $content = $this->content;
119 } elseif ( $isCurrent ) {
120 // XXX: why use RAW audience here, and PUBLIC (default) below?
121 $content = $this->page->getContent( Revision::RAW );
123 $rev = Revision::newFromTitle( $this->page->getTitle(), $this->revid );
125 if ( $rev === null ) {
128 // XXX: why use PUBLIC audience here (default), and RAW above?
129 $content = $rev->getContent();
133 if ( $content === null ) {
137 // Reduce effects of race conditions for slow parses (T48014)
138 $cacheTime = wfTimestampNow();
140 $time = - microtime( true );
141 $this->parserOutput = $content->getParserOutput(
142 $this->page->getTitle(),
146 $time += microtime( true );
150 // TODO: Use Parser's logger (once it has one)
151 $logger = MediaWiki\Logger\LoggerFactory::getInstance( 'slow-parse' );
152 $logger->info( '{time} {title}', [
153 'time' => number_format( $time, 2 ),
154 'title' => $this->page->getTitle()->getPrefixedDBkey(),
155 'ns' => $this->page->getTitle()->getNamespace(),
160 if ( $this->cacheable && $this->parserOutput->isCacheable() && $isCurrent ) {
161 $this->parserCache->save(
162 $this->parserOutput, $this->page, $this->parserOptions, $cacheTime, $this->revid );
165 // Make sure file cache is not used on uncacheable content.
166 // Output that has magic words in it can still use the parser cache
167 // (if enabled), though it will generally expire sooner.
168 if ( !$this->parserOutput->isCacheable() ) {
169 $wgUseFileCache = false;
173 $this->page->triggerOpportunisticLinksUpdate( $this->parserOutput );
182 public function getCachedWork() {
183 $this->parserOutput = $this->parserCache->get( $this->page, $this->parserOptions );
185 if ( $this->parserOutput === false ) {
186 wfDebug( __METHOD__ . ": parser cache miss\n" );
189 wfDebug( __METHOD__ . ": parser cache hit\n" );
197 public function fallback() {
198 $this->parserOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions );
200 if ( $this->parserOutput === false ) {
201 wfDebugLog( 'dirty', 'dirty missing' );
202 wfDebug( __METHOD__ . ": no dirty cache\n" );
205 wfDebug( __METHOD__ . ": sending dirty output\n" );
206 wfDebugLog( 'dirty', "dirty output {$this->cacheKey}" );
207 $this->isDirty = true;
213 * @param Status $status
216 public function error( $status ) {
217 $this->error = $status;