123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- use MediaWiki\MediaWikiServices;
- class PoolWorkArticleView extends PoolCounterWork {
-
- private $page;
-
- private $cacheKey;
-
- private $revid;
-
- private $parserCache;
-
- private $parserOptions;
-
- private $content = null;
-
- private $parserOutput = false;
-
- private $isDirty = false;
-
- private $error = false;
-
- public function __construct( WikiPage $page, ParserOptions $parserOptions,
- $revid, $useParserCache, $content = null
- ) {
- if ( is_string( $content ) ) {
- $modelId = $page->getRevision()->getContentModel();
- $format = $page->getRevision()->getContentFormat();
- $content = ContentHandler::makeContent( $content, $page->getTitle(), $modelId, $format );
- }
- $this->page = $page;
- $this->revid = $revid;
- $this->cacheable = $useParserCache;
- $this->parserOptions = $parserOptions;
- $this->content = $content;
- $this->parserCache = MediaWikiServices::getInstance()->getParserCache();
- $this->cacheKey = $this->parserCache->getKey( $page, $parserOptions );
- $keyPrefix = $this->cacheKey ?: wfMemcKey( 'articleview', 'missingcachekey' );
- parent::__construct( 'ArticleView', $keyPrefix . ':revid:' . $revid );
- }
-
- public function getParserOutput() {
- return $this->parserOutput;
- }
-
- public function getIsDirty() {
- return $this->isDirty;
- }
-
- public function getError() {
- return $this->error;
- }
-
- public function doWork() {
- global $wgUseFileCache;
-
-
- $isCurrent = $this->revid === $this->page->getLatest();
- if ( $this->content !== null ) {
- $content = $this->content;
- } elseif ( $isCurrent ) {
-
- $content = $this->page->getContent( Revision::RAW );
- } else {
- $rev = Revision::newFromTitle( $this->page->getTitle(), $this->revid );
- if ( $rev === null ) {
- $content = null;
- } else {
-
- $content = $rev->getContent();
- }
- }
- if ( $content === null ) {
- return false;
- }
-
- $cacheTime = wfTimestampNow();
- $time = - microtime( true );
- $this->parserOutput = $content->getParserOutput(
- $this->page->getTitle(),
- $this->revid,
- $this->parserOptions
- );
- $time += microtime( true );
-
- if ( $time > 3 ) {
-
- $logger = MediaWiki\Logger\LoggerFactory::getInstance( 'slow-parse' );
- $logger->info( '{time} {title}', [
- 'time' => number_format( $time, 2 ),
- 'title' => $this->page->getTitle()->getPrefixedDBkey(),
- 'ns' => $this->page->getTitle()->getNamespace(),
- 'trigger' => 'view',
- ] );
- }
- if ( $this->cacheable && $this->parserOutput->isCacheable() && $isCurrent ) {
- $this->parserCache->save(
- $this->parserOutput, $this->page, $this->parserOptions, $cacheTime, $this->revid );
- }
-
-
-
- if ( !$this->parserOutput->isCacheable() ) {
- $wgUseFileCache = false;
- }
- if ( $isCurrent ) {
- $this->page->triggerOpportunisticLinksUpdate( $this->parserOutput );
- }
- return true;
- }
-
- public function getCachedWork() {
- $this->parserOutput = $this->parserCache->get( $this->page, $this->parserOptions );
- if ( $this->parserOutput === false ) {
- wfDebug( __METHOD__ . ": parser cache miss\n" );
- return false;
- } else {
- wfDebug( __METHOD__ . ": parser cache hit\n" );
- return true;
- }
- }
-
- public function fallback() {
- $this->parserOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions );
- if ( $this->parserOutput === false ) {
- wfDebugLog( 'dirty', 'dirty missing' );
- wfDebug( __METHOD__ . ": no dirty cache\n" );
- return false;
- } else {
- wfDebug( __METHOD__ . ": sending dirty output\n" );
- wfDebugLog( 'dirty', "dirty output {$this->cacheKey}" );
- $this->isDirty = true;
- return true;
- }
- }
-
- public function error( $status ) {
- $this->error = $status;
- return false;
- }
- }
|