Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.38% |
47 / 52 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
PoolWorkArticleViewCurrent | |
92.16% |
47 / 51 |
|
25.00% |
1 / 4 |
18.16 | |
0.00% |
0 / 1 |
__construct | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
3.01 | |||
doWork | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
5.01 | |||
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\Page\PageRecord; |
26 | use MediaWiki\Page\WikiPageFactory; |
27 | use MediaWiki\Parser\ParserOutput; |
28 | use MediaWiki\Revision\RevisionRecord; |
29 | use MediaWiki\Revision\RevisionRenderer; |
30 | use MediaWiki\Status\Status; |
31 | use MediaWiki\Utils\MWTimestamp; |
32 | use ParserCache; |
33 | use ParserOptions; |
34 | use Wikimedia\Rdbms\ChronologyProtector; |
35 | use Wikimedia\Rdbms\ILBFactory; |
36 | |
37 | /** |
38 | * PoolWorkArticleView for the current revision of a page, using ParserCache. |
39 | * |
40 | * @internal |
41 | */ |
42 | class PoolWorkArticleViewCurrent extends PoolWorkArticleView { |
43 | /** @var string */ |
44 | private $workKey; |
45 | /** @var PageRecord */ |
46 | private $page; |
47 | /** @var ParserCache */ |
48 | private $parserCache; |
49 | /** @var ILBFactory */ |
50 | private $lbFactory; |
51 | /** @var WikiPageFactory */ |
52 | private $wikiPageFactory; |
53 | /** @var bool Whether it should trigger an opportunistic LinksUpdate or not */ |
54 | private bool $triggerLinksUpdate; |
55 | private ChronologyProtector $chronologyProtector; |
56 | |
57 | /** |
58 | * @param string $workKey |
59 | * @param PageRecord $page |
60 | * @param RevisionRecord $revision Revision to render |
61 | * @param ParserOptions $parserOptions ParserOptions to use for the parse |
62 | * @param RevisionRenderer $revisionRenderer |
63 | * @param ParserCache $parserCache |
64 | * @param ILBFactory $lbFactory |
65 | * @param ChronologyProtector $chronologyProtector |
66 | * @param LoggerSpi $loggerSpi |
67 | * @param WikiPageFactory $wikiPageFactory |
68 | * @param bool $cacheable Whether it should store the result in cache or not |
69 | * @param bool $triggerLinksUpdate Whether it should trigger an opportunistic LinksUpdate or not |
70 | */ |
71 | public function __construct( |
72 | string $workKey, |
73 | PageRecord $page, |
74 | RevisionRecord $revision, |
75 | ParserOptions $parserOptions, |
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 | |
103 | /** |
104 | * @return Status |
105 | */ |
106 | public function doWork() { |
107 | $status = $this->renderRevision(); |
108 | /** @var ParserOutput|null $output */ |
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 | |
128 | /** |
129 | * @return Status|false |
130 | */ |
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 | |
140 | /** |
141 | * @param bool $fast Fast stale request |
142 | * @return Status|false |
143 | */ |
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 | |
193 | /** @deprecated class alias since 1.41 */ |
194 | class_alias( PoolWorkArticleViewCurrent::class, 'PoolWorkArticleViewCurrent' ); |