MediaWiki  master
PoolWorkArticleViewCurrent.php
Go to the documentation of this file.
1 <?php
21 use MediaWiki\Logger\Spi as LoggerSpi;
27 
34 
36  private $workKey;
37 
39  private $page;
40 
42  private $parserCache;
43 
45  private $lbFactory;
46 
49 
61  public function __construct(
62  string $workKey,
66  RevisionRenderer $revisionRenderer,
69  LoggerSpi $loggerSpi,
71  ) {
72  // TODO: Remove support for partially initialized RevisionRecord instances once
73  // Article no longer uses fake revisions.
74  if ( $revision->getPageId() && $revision->getPageId() !== $page->getId() ) {
75  throw new InvalidArgumentException( '$page parameter mismatches $revision parameter' );
76  }
77 
78  parent::__construct( $workKey, $revision, $parserOptions, $revisionRenderer, $loggerSpi );
79 
80  $this->workKey = $workKey;
81  $this->page = $page;
82  $this->parserCache = $parserCache;
83  $this->lbFactory = $lbFactory;
84  $this->wikiPageFactory = $wikiPageFactory;
85  $this->cacheable = true;
86  }
87 
92  protected function saveInCache( ParserOutput $output, string $cacheTime ) {
93  $this->parserCache->save(
94  $output,
95  $this->page,
96  $this->parserOptions,
97  $cacheTime,
98  $this->revision->getId()
99  );
100  }
101 
105  protected function afterWork( ParserOutput $output ) {
106  $this->wikiPageFactory->newFromTitle( $this->page )
107  ->triggerOpportunisticLinksUpdate( $this->parserOutput );
108  }
109 
113  public function getCachedWork() {
114  $this->parserOutput = $this->parserCache->get( $this->page, $this->parserOptions );
115 
116  $logger = $this->getLogger();
117  if ( $this->parserOutput === false ) {
118  $logger->debug( 'parser cache miss' );
119  return false;
120  } else {
121  $logger->debug( 'parser cache hit' );
122  return true;
123  }
124  }
125 
130  public function fallback( $fast ) {
131  $this->parserOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions );
132 
133  $logger = $this->getLogger( 'dirty' );
134 
135  $fastMsg = '';
136  if ( $this->parserOutput && $fast ) {
137  /* Check if the stale response is from before the last write to the
138  * DB by this user. Declining to return a stale response in this
139  * case ensures that the user will see their own edit after page
140  * save.
141  *
142  * Note that the CP touch time is the timestamp of the shutdown of
143  * the save request, so there is a bias towards avoiding fast stale
144  * responses of potentially several seconds.
145  */
146  $lastWriteTime = $this->lbFactory->getChronologyProtectorTouched();
147  $cacheTime = MWTimestamp::convert( TS_UNIX, $this->parserOutput->getCacheTime() );
148  if ( $lastWriteTime && $cacheTime <= $lastWriteTime ) {
149  $logger->info(
150  'declining to send dirty output since cache time ' .
151  '{cacheTime} is before last write time {lastWriteTime}',
152  [
153  'workKey' => $this->workKey,
154  'cacheTime' => $cacheTime,
155  'lastWriteTime' => $lastWriteTime,
156  ]
157  );
158  // Forget this ParserOutput -- we will request it again if
159  // necessary in slow mode. There might be a newer entry
160  // available by that time.
161  $this->parserOutput = false;
162  return false;
163  }
164  $this->isFast = true;
165  $fastMsg = 'fast ';
166  }
167 
168  if ( $this->parserOutput === false ) {
169  $logger->info( 'dirty missing' );
170  return false;
171  } else {
172  $logger->info( "{$fastMsg}dirty output", [ 'workKey' => $this->workKey ] );
173  $this->isDirty = true;
174  return true;
175  }
176  }
177 }
PoolWorkArticleViewCurrent\$parserCache
ParserCache $parserCache
Definition: PoolWorkArticleViewCurrent.php:42
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:45
Page\PageRecord
Data record representing a page that is (or used to be, or could be) an editable page on a wiki.
Definition: PageRecord.php:25
PoolWorkArticleViewCurrent
PoolWorkArticleView for the current revision of a page, using ParserCache.
Definition: PoolWorkArticleViewCurrent.php:33
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
ParserOutput
Definition: ParserOutput.php:31
PoolWorkArticleViewCurrent\__construct
__construct(string $workKey, PageRecord $page, RevisionRecord $revision, ParserOptions $parserOptions, RevisionRenderer $revisionRenderer, ParserCache $parserCache, ILBFactory $lbFactory, LoggerSpi $loggerSpi, WikiPageFactory $wikiPageFactory)
Definition: PoolWorkArticleViewCurrent.php:61
PoolWorkArticleViewCurrent\$workKey
string $workKey
Definition: PoolWorkArticleViewCurrent.php:36
PoolWorkArticleViewCurrent\fallback
fallback( $fast)
Definition: PoolWorkArticleViewCurrent.php:130
PoolWorkArticleViewCurrent\getCachedWork
getCachedWork()
Definition: PoolWorkArticleViewCurrent.php:113
MediaWiki\Revision\RevisionRecord\getPageId
getPageId( $wikiId=self::LOCAL)
Get the page ID.
Definition: RevisionRecord.php:335
PoolWorkArticleView
PoolCounter protected work wrapping RenderedRevision->getRevisionParserOutput.
Definition: PoolWorkArticleView.php:34
PoolWorkArticleViewCurrent\$wikiPageFactory
WikiPageFactory $wikiPageFactory
Definition: PoolWorkArticleViewCurrent.php:48
PoolWorkArticleView\$parserOptions
ParserOptions $parserOptions
Definition: PoolWorkArticleView.php:37
PoolWorkArticleView\$loggerSpi
LoggerSpi $loggerSpi
Definition: PoolWorkArticleView.php:58
Page\ProperPageIdentity\getId
getId( $wikiId=self::LOCAL)
Returns the page ID.
Page\WikiPageFactory
Definition: WikiPageFactory.php:20
PoolWorkArticleViewCurrent\$page
PageRecord $page
Definition: PoolWorkArticleViewCurrent.php:39
MediaWiki\Revision\RevisionRenderer
The RevisionRenderer service provides access to rendered output for revisions.
Definition: RevisionRenderer.php:45
PoolWorkArticleViewCurrent\saveInCache
saveInCache(ParserOutput $output, string $cacheTime)
Definition: PoolWorkArticleViewCurrent.php:92
PoolWorkArticleView\getLogger
getLogger( $name='PoolWorkArticleView')
Definition: PoolWorkArticleView.php:195
PoolWorkArticleViewCurrent\afterWork
afterWork(ParserOutput $output)
Definition: PoolWorkArticleViewCurrent.php:105
ParserCache
Cache for ParserOutput objects corresponding to the latest page revisions.
Definition: ParserCache.php:63
PoolWorkArticleViewCurrent\$lbFactory
ILBFactory $lbFactory
Definition: PoolWorkArticleViewCurrent.php:45
MediaWiki\Logger\Spi
Service provider interface for \Psr\Log\LoggerInterface implementation libraries.
Definition: Spi.php:38
PoolWorkArticleView\$revision
RevisionRecord null $revision
Definition: PoolWorkArticleView.php:40
Wikimedia\Rdbms\ILBFactory
An interface for generating database load balancers.
Definition: ILBFactory.php:33