MediaWiki master
PoolWorkArticleViewCurrent.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\PoolCounter;
22
23use InvalidArgumentException;
24use MediaWiki\Logger\Spi as LoggerSpi;
32use ParserCache;
36
44 private $workKey;
46 private $page;
48 private $parserCache;
50 private $lbFactory;
52 private $wikiPageFactory;
54 private bool $triggerLinksUpdate;
55 private ChronologyProtector $chronologyProtector;
56
71 public function __construct(
72 string $workKey,
73 PageRecord $page,
76 RevisionRenderer $revisionRenderer,
77 ParserCache $parserCache,
78 ILBFactory $lbFactory,
79 ChronologyProtector $chronologyProtector,
80 LoggerSpi $loggerSpi,
81 WikiPageFactory $wikiPageFactory,
82 bool $cacheable = true,
83 bool $triggerLinksUpdate = false
84 ) {
85 // TODO: Remove support for partially initialized RevisionRecord instances once
86 // Article no longer uses fake revisions.
87 if ( $revision->getPageId() && $revision->getPageId() !== $page->getId() ) {
88 throw new InvalidArgumentException( '$page parameter mismatches $revision parameter' );
89 }
90
91 parent::__construct( $workKey, $revision, $parserOptions, $revisionRenderer, $loggerSpi );
92
93 $this->workKey = $workKey;
94 $this->page = $page;
95 $this->parserCache = $parserCache;
96 $this->lbFactory = $lbFactory;
97 $this->chronologyProtector = $chronologyProtector;
98 $this->wikiPageFactory = $wikiPageFactory;
99 $this->cacheable = $cacheable;
100 $this->triggerLinksUpdate = $triggerLinksUpdate;
101 }
102
106 public function doWork() {
107 $status = $this->renderRevision();
109 $output = $status->getValue();
110
111 if ( $output ) {
112 if ( $this->cacheable && $output->isCacheable() ) {
113 $this->parserCache->save(
114 $output,
115 $this->page,
116 $this->parserOptions
117 );
118 }
119
120 if ( $this->triggerLinksUpdate ) {
121 $this->wikiPageFactory->newFromTitle( $this->page )->triggerOpportunisticLinksUpdate( $output );
122 }
123 }
124
125 return $status;
126 }
127
131 public function getCachedWork() {
132 $parserOutput = $this->parserCache->get( $this->page, $this->parserOptions );
133
134 $logger = $this->loggerSpi->getLogger( 'PoolWorkArticleView' );
135 $logger->debug( $parserOutput ? 'parser cache hit' : 'parser cache miss' );
136
137 return $parserOutput ? Status::newGood( $parserOutput ) : false;
138 }
139
144 public function fallback( $fast ) {
145 $parserOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions );
146
147 $logger = $this->loggerSpi->getLogger( 'dirty' );
148
149 if ( !$parserOutput ) {
150 $logger->info( 'dirty missing' );
151 return false;
152 }
153
154 if ( $fast ) {
155 /* Check if the stale response is from before the last write to the
156 * DB by this user. Declining to return a stale response in this
157 * case ensures that the user will see their own edit after page
158 * save.
159 *
160 * Note that the CP touch time is the timestamp of the shutdown of
161 * the save request, so there is a bias towards avoiding fast stale
162 * responses of potentially several seconds.
163 */
164 $lastWriteTime = $this->chronologyProtector->getTouched( $this->lbFactory->getMainLB() );
165 $cacheTime = MWTimestamp::convert( TS_UNIX, $parserOutput->getCacheTime() );
166 if ( $lastWriteTime && $cacheTime <= $lastWriteTime ) {
167 $logger->info(
168 'declining to send dirty output since cache time ' .
169 '{cacheTime} is before last write time {lastWriteTime}',
170 [
171 'workKey' => $this->workKey,
172 'cacheTime' => $cacheTime,
173 'lastWriteTime' => $lastWriteTime,
174 ]
175 );
176 // Forget this ParserOutput -- we will request it again if
177 // necessary in slow mode. There might be a newer entry
178 // available by that time.
179 return false;
180 }
181 }
182
183 $logger->info( $fast ? 'fast dirty output' : 'dirty output', [ 'workKey' => $this->workKey ] );
184
185 $status = Status::newGood( $parserOutput );
186 $status->warning( 'view-pool-dirty-output' );
187 $status->warning( $fast ? 'view-pool-contention' : 'view-pool-overload' );
188 return $status;
189 }
190
191}
192
194class_alias( PoolWorkArticleViewCurrent::class, 'PoolWorkArticleViewCurrent' );
Service for creating WikiPage objects.
ParserOutput is a rendering of a Content object or a message.
PoolWorkArticleView for the current revision of a page, using ParserCache.
__construct(string $workKey, PageRecord $page, RevisionRecord $revision, ParserOptions $parserOptions, RevisionRenderer $revisionRenderer, ParserCache $parserCache, ILBFactory $lbFactory, ChronologyProtector $chronologyProtector, LoggerSpi $loggerSpi, WikiPageFactory $wikiPageFactory, bool $cacheable=true, bool $triggerLinksUpdate=false)
PoolCounter protected work wrapping RenderedRevision->getRevisionParserOutput.
Page revision base class.
getPageId( $wikiId=self::LOCAL)
Get the page ID.
The RevisionRenderer service provides access to rendered output for revisions.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Library for creating and parsing MW-style timestamps.
Cache for ParserOutput objects corresponding to the latest page revisions.
Set options of the Parser.
Provide a given client with protection against visible database lag.
Service provider interface to create \Psr\Log\LoggerInterface objects.
Definition Spi.php:64
Data record representing a page that is (or used to be, or could be) an editable page on a wiki.
getId( $wikiId=self::LOCAL)
Returns the page ID.
Manager of ILoadBalancer objects and, indirectly, IDatabase connections.