Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.83% |
53 / 59 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
PoolWorkArticleViewCurrent | |
91.38% |
53 / 58 |
|
25.00% |
1 / 4 |
22.31 | |
0.00% |
0 / 1 |
__construct | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
3.01 | |||
doWork | |
89.47% |
17 / 19 |
|
0.00% |
0 / 1 |
9.09 | |||
getCachedWork | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
fallback | |
91.67% |
22 / 24 |
|
0.00% |
0 / 1 |
7.03 |
1 | <?php |
2 | /** |
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. |
7 | * |
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. |
12 | * |
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 |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\PoolCounter; |
22 | |
23 | use InvalidArgumentException; |
24 | use MediaWiki\Logger\Spi as LoggerSpi; |
25 | use MediaWiki\MainConfigNames; |
26 | use MediaWiki\MediaWikiServices; |
27 | use MediaWiki\Page\PageRecord; |
28 | use MediaWiki\Page\WikiPageFactory; |
29 | use MediaWiki\Parser\ParserOutput; |
30 | use MediaWiki\Revision\RevisionRecord; |
31 | use MediaWiki\Revision\RevisionRenderer; |
32 | use MediaWiki\Status\Status; |
33 | use MediaWiki\Utils\MWTimestamp; |
34 | use ParserCache; |
35 | use ParserOptions; |
36 | use Wikimedia\Rdbms\ChronologyProtector; |
37 | use Wikimedia\Rdbms\ILBFactory; |
38 | |
39 | /** |
40 | * PoolWorkArticleView for the current revision of a page, using ParserCache. |
41 | * |
42 | * @internal |
43 | */ |
44 | class PoolWorkArticleViewCurrent extends PoolWorkArticleView { |
45 | /** @var string */ |
46 | private $workKey; |
47 | /** @var PageRecord */ |
48 | private $page; |
49 | /** @var ParserCache */ |
50 | private $parserCache; |
51 | /** @var ILBFactory */ |
52 | private $lbFactory; |
53 | /** @var WikiPageFactory */ |
54 | private $wikiPageFactory; |
55 | /** @var bool Whether it should trigger an opportunistic LinksUpdate or not */ |
56 | private bool $triggerLinksUpdate; |
57 | private ChronologyProtector $chronologyProtector; |
58 | |
59 | /** |
60 | * @param string $workKey |
61 | * @param PageRecord $page |
62 | * @param RevisionRecord $revision Revision to render |
63 | * @param ParserOptions $parserOptions ParserOptions to use for the parse |
64 | * @param RevisionRenderer $revisionRenderer |
65 | * @param ParserCache $parserCache |
66 | * @param ILBFactory $lbFactory |
67 | * @param ChronologyProtector $chronologyProtector |
68 | * @param LoggerSpi $loggerSpi |
69 | * @param WikiPageFactory $wikiPageFactory |
70 | * @param bool $cacheable Whether it should store the result in cache or not |
71 | * @param bool $triggerLinksUpdate Whether it should trigger an opportunistic LinksUpdate or not |
72 | */ |
73 | public function __construct( |
74 | string $workKey, |
75 | PageRecord $page, |
76 | RevisionRecord $revision, |
77 | ParserOptions $parserOptions, |
78 | RevisionRenderer $revisionRenderer, |
79 | ParserCache $parserCache, |
80 | ILBFactory $lbFactory, |
81 | ChronologyProtector $chronologyProtector, |
82 | LoggerSpi $loggerSpi, |
83 | WikiPageFactory $wikiPageFactory, |
84 | bool $cacheable = true, |
85 | bool $triggerLinksUpdate = false |
86 | ) { |
87 | // TODO: Remove support for partially initialized RevisionRecord instances once |
88 | // Article no longer uses fake revisions. |
89 | if ( $revision->getPageId() && $revision->getPageId() !== $page->getId() ) { |
90 | throw new InvalidArgumentException( '$page parameter mismatches $revision parameter' ); |
91 | } |
92 | |
93 | parent::__construct( $workKey, $revision, $parserOptions, $revisionRenderer, $loggerSpi ); |
94 | |
95 | $this->workKey = $workKey; |
96 | $this->page = $page; |
97 | $this->parserCache = $parserCache; |
98 | $this->lbFactory = $lbFactory; |
99 | $this->chronologyProtector = $chronologyProtector; |
100 | $this->wikiPageFactory = $wikiPageFactory; |
101 | $this->cacheable = $cacheable; |
102 | $this->triggerLinksUpdate = $triggerLinksUpdate; |
103 | } |
104 | |
105 | /** |
106 | * @return Status |
107 | */ |
108 | public function doWork() { |
109 | // T371713: Temporary statistics collection code to determine |
110 | // feasibility of Parsoid selective update |
111 | $sampleRate = MediaWikiServices::getInstance()->getMainConfig()->get( |
112 | MainConfigNames::ParsoidSelectiveUpdateSampleRate |
113 | ); |
114 | $doSample = ( $sampleRate && mt_rand( 1, $sampleRate ) === 1 ); |
115 | |
116 | $previousOutput = null; |
117 | if ( $this->parserOptions->getUseParsoid() || $doSample ) { |
118 | // Parsoid can do selective updates, so it is worth checking the |
119 | // cache for an existing entry. Not worth it for the legacy |
120 | // parser, though. |
121 | $previousOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions ) ?: null; |
122 | } |
123 | $status = $this->renderRevision( $previousOutput, $doSample, 'PoolWorkArticleViewCurrent' ); |
124 | /** @var ParserOutput|null $output */ |
125 | $output = $status->getValue(); |
126 | |
127 | if ( $output ) { |
128 | if ( $this->cacheable && $output->isCacheable() ) { |
129 | $this->parserCache->save( |
130 | $output, |
131 | $this->page, |
132 | $this->parserOptions |
133 | ); |
134 | } |
135 | |
136 | if ( $this->triggerLinksUpdate ) { |
137 | $this->wikiPageFactory->newFromTitle( $this->page )->triggerOpportunisticLinksUpdate( $output ); |
138 | } |
139 | } |
140 | |
141 | return $status; |
142 | } |
143 | |
144 | /** |
145 | * @return Status|false |
146 | */ |
147 | public function getCachedWork() { |
148 | $parserOutput = $this->parserCache->get( $this->page, $this->parserOptions ); |
149 | |
150 | $logger = $this->loggerSpi->getLogger( 'PoolWorkArticleView' ); |
151 | $logger->debug( $parserOutput ? 'parser cache hit' : 'parser cache miss' ); |
152 | |
153 | return $parserOutput ? Status::newGood( $parserOutput ) : false; |
154 | } |
155 | |
156 | /** |
157 | * @param bool $fast Fast stale request |
158 | * @return Status|false |
159 | */ |
160 | public function fallback( $fast ) { |
161 | $parserOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions ); |
162 | |
163 | $logger = $this->loggerSpi->getLogger( 'dirty' ); |
164 | |
165 | if ( !$parserOutput ) { |
166 | $logger->info( 'dirty missing' ); |
167 | return false; |
168 | } |
169 | |
170 | if ( $fast ) { |
171 | /* Check if the stale response is from before the last write to the |
172 | * DB by this user. Declining to return a stale response in this |
173 | * case ensures that the user will see their own edit after page |
174 | * save. |
175 | * |
176 | * Note that the CP touch time is the timestamp of the shutdown of |
177 | * the save request, so there is a bias towards avoiding fast stale |
178 | * responses of potentially several seconds. |
179 | */ |
180 | $lastWriteTime = $this->chronologyProtector->getTouched( $this->lbFactory->getMainLB() ); |
181 | $cacheTime = MWTimestamp::convert( TS_UNIX, $parserOutput->getCacheTime() ); |
182 | if ( $lastWriteTime && $cacheTime <= $lastWriteTime ) { |
183 | $logger->info( |
184 | 'declining to send dirty output since cache time ' . |
185 | '{cacheTime} is before last write time {lastWriteTime}', |
186 | [ |
187 | 'workKey' => $this->workKey, |
188 | 'cacheTime' => $cacheTime, |
189 | 'lastWriteTime' => $lastWriteTime, |
190 | ] |
191 | ); |
192 | // Forget this ParserOutput -- we will request it again if |
193 | // necessary in slow mode. There might be a newer entry |
194 | // available by that time. |
195 | return false; |
196 | } |
197 | } |
198 | |
199 | $logger->info( $fast ? 'fast dirty output' : 'dirty output', [ 'workKey' => $this->workKey ] ); |
200 | |
201 | $status = Status::newGood( $parserOutput ); |
202 | $status->warning( 'view-pool-dirty-output' ); |
203 | $status->warning( $fast ? 'view-pool-contention' : 'view-pool-overload' ); |
204 | return $status; |
205 | } |
206 | |
207 | } |
208 | |
209 | /** @deprecated class alias since 1.42 */ |
210 | class_alias( PoolWorkArticleViewCurrent::class, 'PoolWorkArticleViewCurrent' ); |