MediaWiki  master
PoolWorkArticleViewCurrent.php
Go to the documentation of this file.
1 <?php
21 use MediaWiki\Logger\Spi as LoggerSpi;
25 
32 
34  private $workKey;
35 
37  private $page;
38 
40  private $parserCache;
41 
43  private $lbFactory;
44 
55  public function __construct(
56  string $workKey,
60  RevisionRenderer $revisionRenderer,
63  LoggerSpi $loggerSpi
64  ) {
65  // TODO: Remove support for partially initialized RevisionRecord instances once
66  // Article no longer uses fake revisions.
67  if ( $revision->getPageId() && $revision->getPageId() !== $page->getTitle()->getArticleID() ) {
68  throw new InvalidArgumentException( '$page parameter mismatches $revision parameter' );
69  }
70 
71  parent::__construct( $workKey, $revision, $parserOptions, $revisionRenderer, $loggerSpi );
72 
73  $this->workKey = $workKey;
74  $this->page = $page;
75  $this->parserCache = $parserCache;
76  $this->lbFactory = $lbFactory;
77  $this->cacheable = true;
78  }
79 
84  protected function saveInCache( ParserOutput $output, string $cacheTime ) {
85  $this->parserCache->save(
86  $output,
87  $this->page,
88  $this->parserOptions,
89  $cacheTime,
90  $this->revision->getId()
91  );
92  }
93 
97  protected function afterWork( ParserOutput $output ) {
98  $this->page->triggerOpportunisticLinksUpdate( $this->parserOutput );
99  }
100 
104  public function getCachedWork() {
105  $this->parserOutput = $this->parserCache->get( $this->page, $this->parserOptions );
106 
107  $logger = $this->getLogger();
108  if ( $this->parserOutput === false ) {
109  $logger->debug( 'parser cache miss' );
110  return false;
111  } else {
112  $logger->debug( 'parser cache hit' );
113  return true;
114  }
115  }
116 
121  public function fallback( $fast ) {
122  $this->parserOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions );
123 
124  $logger = $this->getLogger( 'dirty' );
125 
126  $fastMsg = '';
127  if ( $this->parserOutput && $fast ) {
128  /* Check if the stale response is from before the last write to the
129  * DB by this user. Declining to return a stale response in this
130  * case ensures that the user will see their own edit after page
131  * save.
132  *
133  * Note that the CP touch time is the timestamp of the shutdown of
134  * the save request, so there is a bias towards avoiding fast stale
135  * responses of potentially several seconds.
136  */
137  $lastWriteTime = $this->lbFactory->getChronologyProtectorTouched();
138  $cacheTime = MWTimestamp::convert( TS_UNIX, $this->parserOutput->getCacheTime() );
139  if ( $lastWriteTime && $cacheTime <= $lastWriteTime ) {
140  $logger->info(
141  'declining to send dirty output since cache time ' .
142  '{cacheTime} is before last write time {lastWriteTime}',
143  [
144  'workKey' => $this->workKey,
145  'cacheTime' => $cacheTime,
146  'lastWriteTime' => $lastWriteTime,
147  ]
148  );
149  // Forget this ParserOutput -- we will request it again if
150  // necessary in slow mode. There might be a newer entry
151  // available by that time.
152  $this->parserOutput = false;
153  return false;
154  }
155  $this->isFast = true;
156  $fastMsg = 'fast ';
157  }
158 
159  if ( $this->parserOutput === false ) {
160  $logger->info( 'dirty missing' );
161  return false;
162  } else {
163  $logger->info( "{$fastMsg}dirty output", [ 'workKey' => $this->workKey ] );
164  $this->isDirty = true;
165  return true;
166  }
167  }
168 }
PoolWorkArticleViewCurrent\$parserCache
ParserCache $parserCache
Definition: PoolWorkArticleViewCurrent.php:40
PoolWorkArticleViewCurrent\$page
WikiPage $page
Definition: PoolWorkArticleViewCurrent.php:37
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:44
PoolWorkArticleViewCurrent
PoolWorkArticleView for the current revision of a page, using ParserCache.
Definition: PoolWorkArticleViewCurrent.php:31
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
ParserOutput
Definition: ParserOutput.php:31
PoolWorkArticleViewCurrent\$workKey
string $workKey
Definition: PoolWorkArticleViewCurrent.php:34
PoolWorkArticleViewCurrent\fallback
fallback( $fast)
Definition: PoolWorkArticleViewCurrent.php:121
PoolWorkArticleViewCurrent\getCachedWork
getCachedWork()
Definition: PoolWorkArticleViewCurrent.php:104
PoolWorkArticleViewCurrent\__construct
__construct(string $workKey, WikiPage $page, RevisionRecord $revision, ParserOptions $parserOptions, RevisionRenderer $revisionRenderer, ParserCache $parserCache, ILBFactory $lbFactory, LoggerSpi $loggerSpi)
Definition: PoolWorkArticleViewCurrent.php:55
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:55
PoolWorkArticleView
PoolCounter protected work wrapping RenderedRevision->getRevisionParserOutput.
Definition: PoolWorkArticleView.php:34
PoolWorkArticleView\$parserOptions
ParserOptions $parserOptions
Definition: PoolWorkArticleView.php:37
PoolWorkArticleView\$loggerSpi
LoggerSpi $loggerSpi
Definition: PoolWorkArticleView.php:58
WikiPage\getTitle
getTitle()
Get the title object of the article.
Definition: WikiPage.php:295
Revision\RevisionRecord\getPageId
getPageId()
Get the page ID.
Definition: RevisionRecord.php:324
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:84
PoolWorkArticleView\getLogger
getLogger( $name='PoolWorkArticleView')
Definition: PoolWorkArticleView.php:196
PoolWorkArticleViewCurrent\afterWork
afterWork(ParserOutput $output)
Definition: PoolWorkArticleViewCurrent.php:97
ParserCache
Cache for ParserOutput objects corresponding to the latest page revisions.
Definition: ParserCache.php:61
PoolWorkArticleViewCurrent\$lbFactory
ILBFactory $lbFactory
Definition: PoolWorkArticleViewCurrent.php:43
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