Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.62% |
116 / 128 |
|
72.73% |
8 / 11 |
CRAP | |
0.00% |
0 / 1 |
CachedPageViewService | |
90.62% |
116 / 128 |
|
72.73% |
8 / 11 |
40.25 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
setLogger | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setCachedDays | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPageData | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
getSiteData | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
getTopPages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCacheExpiry | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getWithCache | |
96.30% |
26 / 27 |
|
0.00% |
0 / 1 |
8 | |||
getTitlesWithCache | |
85.29% |
58 / 68 |
|
0.00% |
0 / 1 |
15.72 | |||
extendDateRange | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\PageViewInfo; |
4 | |
5 | use BagOStuff; |
6 | use InvalidArgumentException; |
7 | use MediaWiki\Message\Message; |
8 | use MediaWiki\Page\PageReference; |
9 | use MediaWiki\Status\Status; |
10 | use MediaWiki\Title\TitleFormatter; |
11 | use Psr\Log\LoggerAwareInterface; |
12 | use Psr\Log\LoggerInterface; |
13 | use Psr\Log\NullLogger; |
14 | use StatusValue; |
15 | |
16 | /** |
17 | * Wraps a PageViewService and caches the results. |
18 | */ |
19 | class CachedPageViewService implements PageViewService, LoggerAwareInterface { |
20 | private const ERROR_EXPIRY = 1800; |
21 | |
22 | /** @var PageViewService */ |
23 | protected $service; |
24 | |
25 | /** @var BagOStuff */ |
26 | protected $cache; |
27 | |
28 | /** @var LoggerInterface */ |
29 | protected $logger; |
30 | |
31 | private TitleFormatter $titleFormatter; |
32 | |
33 | /** @var string Cache prefix, in case multiple instances of this service coexist */ |
34 | protected $prefix; |
35 | |
36 | /** @var int */ |
37 | protected $cachedDays = 30; |
38 | |
39 | public function __construct( |
40 | PageViewService $service, |
41 | BagOStuff $cache, |
42 | TitleFormatter $titleFormatter, |
43 | string $prefix = '' |
44 | ) { |
45 | $this->service = $service; |
46 | $this->logger = new NullLogger(); |
47 | $this->cache = $cache; |
48 | $this->titleFormatter = $titleFormatter; |
49 | $this->prefix = $prefix; |
50 | } |
51 | |
52 | public function setLogger( LoggerInterface $logger ) { |
53 | $this->logger = $logger; |
54 | } |
55 | |
56 | /** |
57 | * Set the number of days that will be cached. To avoid cache fragmentation, the inner service |
58 | * is always called with this number of days; if necessary, the response will be expanded with |
59 | * nulls. |
60 | * @param int $cachedDays |
61 | */ |
62 | public function setCachedDays( $cachedDays ) { |
63 | $this->cachedDays = $cachedDays; |
64 | } |
65 | |
66 | public function supports( $metric, $scope ) { |
67 | return $this->service->supports( $metric, $scope ); |
68 | } |
69 | |
70 | public function getPageData( array $titles, $days, $metric = self::METRIC_VIEW ) { |
71 | $status = $this->getTitlesWithCache( $metric, $titles ); |
72 | $data = $status->getValue(); |
73 | foreach ( $data as $title => $titleData ) { |
74 | if ( $days < $this->cachedDays ) { |
75 | $data[$title] = array_slice( $titleData, -$days, null, true ); |
76 | } elseif ( $days > $this->cachedDays ) { |
77 | $data[$title] = $this->extendDateRange( $titleData, $days ); |
78 | } |
79 | } |
80 | $status->setResult( $status->isOK(), $data ); |
81 | return $status; |
82 | } |
83 | |
84 | public function getSiteData( $days, $metric = self::METRIC_VIEW ) { |
85 | $status = $this->getWithCache( $metric, self::SCOPE_SITE ); |
86 | if ( $status->isOK() ) { |
87 | $data = $status->getValue(); |
88 | if ( $days < $this->cachedDays ) { |
89 | $data = array_slice( $data, -$days, null, true ); |
90 | } elseif ( $days > $this->cachedDays ) { |
91 | $data = $this->extendDateRange( $data, $days ); |
92 | } |
93 | $status->setResult( true, $data ); |
94 | } |
95 | return $status; |
96 | } |
97 | |
98 | public function getTopPages( $metric = self::METRIC_VIEW ) { |
99 | return $this->getWithCache( $metric, self::SCOPE_TOP ); |
100 | } |
101 | |
102 | public function getCacheExpiry( $metric, $scope ) { |
103 | // add some random delay to avoid cache stampedes |
104 | return $this->service->getCacheExpiry( $metric, $scope ) + mt_rand( 0, 600 ); |
105 | } |
106 | |
107 | /** |
108 | * Like BagOStuff::getWithSetCallback, but returns a StatusValue like PageViewService calls do. |
109 | * Returns (and caches) null wrapped in a StatusValue on error. |
110 | * @param string $metric A METRIC_* constant |
111 | * @param string $scope A SCOPE_* constant (except SCOPE_ARTICLE which has its own method) |
112 | * @return StatusValue |
113 | */ |
114 | protected function getWithCache( $metric, $scope ) { |
115 | $key = $this->cache->makeKey( |
116 | 'pvi', |
117 | $this->prefix, |
118 | ( $scope === self::SCOPE_SITE ) ? $this->cachedDays : "", |
119 | $metric, |
120 | $scope |
121 | ); |
122 | $data = $this->cache->get( $key ); |
123 | |
124 | if ( $data === false ) { |
125 | // no cached data |
126 | /** @var StatusValue $status */ |
127 | switch ( $scope ) { |
128 | case self::SCOPE_SITE: |
129 | $status = $this->service->getSiteData( $this->cachedDays, $metric ); |
130 | break; |
131 | case self::SCOPE_TOP: |
132 | $status = $this->service->getTopPages( $metric ); |
133 | break; |
134 | default: |
135 | throw new InvalidArgumentException( "invalid scope: $scope" ); |
136 | } |
137 | if ( $status->isOK() ) { |
138 | $data = $status->getValue(); |
139 | $expiry = $this->getCacheExpiry( $metric, $scope ); |
140 | } else { |
141 | $data = null; |
142 | $expiry = self::ERROR_EXPIRY; |
143 | } |
144 | $this->cache->set( $key, $data, $expiry ); |
145 | } elseif ( $data === null ) { |
146 | // cached error |
147 | $status = StatusValue::newGood( [] ); |
148 | $status->fatal( 'pvi-cached-error', Message::durationParam( self::ERROR_EXPIRY ) ); |
149 | } else { |
150 | // valid cached data |
151 | $status = StatusValue::newGood( $data ); |
152 | } |
153 | return $status; |
154 | } |
155 | |
156 | /** |
157 | * The equivalent of getWithCache for multiple titles (ie. for SCOPE_ARTICLE). |
158 | * Errors are also handled per-article. |
159 | * @param string $metric A METRIC_* constant |
160 | * @param PageReference[] $titles |
161 | * @return StatusValue |
162 | * @suppress SecurityCheck-DoubleEscaped |
163 | */ |
164 | protected function getTitlesWithCache( $metric, array $titles ) { |
165 | if ( !$titles ) { |
166 | return StatusValue::newGood( [] ); |
167 | } |
168 | |
169 | // Set up the response array, without any values. This will help preserve the order of titles. |
170 | $data = array_fill_keys( array_map( function ( PageReference $t ) { |
171 | return $this->titleFormatter->getPrefixedDBkey( $t ); |
172 | }, $titles ), false ); |
173 | |
174 | // Fetch data for all titles from cache. Hopefully we are using a cache which has |
175 | // a cheap getMulti implementation. |
176 | $titleToCacheKey = $statuses = []; |
177 | foreach ( $titles as $title ) { |
178 | $dbKey = $this->titleFormatter->getPrefixedDBkey( $title ); |
179 | $titleToCacheKey[$dbKey] = $this->cache->makeKey( |
180 | 'pvi', $this->prefix, |
181 | $this->cachedDays, |
182 | $metric, |
183 | self::SCOPE_ARTICLE, |
184 | md5( $dbKey ) |
185 | ); |
186 | } |
187 | $cacheKeyToTitle = array_flip( $titleToCacheKey ); |
188 | $rawData = $this->cache->getMulti( array_keys( $cacheKeyToTitle ) ); |
189 | foreach ( $rawData as $key => $value ) { |
190 | // BagOStuff::getMulti is unclear on how missing items should be handled; let's |
191 | // assume some implementations might return that key with a value of false |
192 | if ( $value !== false ) { |
193 | $statuses[$cacheKeyToTitle[$key]] = empty( $value['#error'] ) ? StatusValue::newGood() |
194 | : StatusValue::newFatal( |
195 | 'pvi-cached-error-title', |
196 | wfEscapeWikiText( $cacheKeyToTitle[$key] ), |
197 | Message::durationParam( self::ERROR_EXPIRY ) |
198 | ); |
199 | unset( $value['#error'] ); |
200 | $data[$cacheKeyToTitle[$key]] = $value; |
201 | } |
202 | } |
203 | |
204 | // Now get and cache the data for the remaining titles from the real service. It might not |
205 | // return data for all of them. |
206 | foreach ( $titles as $i => $titleObj ) { |
207 | if ( $data[$this->titleFormatter->getPrefixedDBkey( $titleObj )] !== false ) { |
208 | unset( $titles[$i] ); |
209 | } |
210 | } |
211 | $uncachedStatus = $this->service->getPageData( $titles, $this->cachedDays, $metric ); |
212 | foreach ( $uncachedStatus->success as $title => $success ) { |
213 | $titleData = $uncachedStatus->getValue()[$title] ?? null; |
214 | if ( !is_array( $titleData ) || count( $titleData ) < $this->cachedDays ) { |
215 | // PageViewService is expected to return [ date => null ] for all requested dates |
216 | $this->logger->warning( 'Upstream service returned invalid data for {title}', [ |
217 | 'title' => $title, |
218 | 'statusMessage' => Status::wrap( $uncachedStatus ) |
219 | ->getWikiText( false, false, 'en' ), |
220 | ] ); |
221 | $titleData = $this->extendDateRange( |
222 | is_array( $titleData ) ? $titleData : [], |
223 | $this->cachedDays |
224 | ); |
225 | } |
226 | $data[$title] = $titleData; |
227 | if ( $success ) { |
228 | $statuses[$title] = StatusValue::newGood(); |
229 | $expiry = $this->getCacheExpiry( $metric, self::SCOPE_ARTICLE ); |
230 | } else { |
231 | $data[$title]['#error'] = true; |
232 | $statuses[$title] = StatusValue::newFatal( |
233 | 'pvi-cached-error-title', |
234 | wfEscapeWikiText( $title ), |
235 | Message::durationParam( self::ERROR_EXPIRY ) |
236 | ); |
237 | $expiry = self::ERROR_EXPIRY; |
238 | } |
239 | $this->cache->set( $titleToCacheKey[$title], $data[$title], $expiry ); |
240 | unset( $data[$title]['#error'] ); |
241 | } |
242 | |
243 | // Almost done; we need to truncate the data at the first "hole" (title not returned |
244 | // either by getMulti or getPageData) so we return a consecutive prefix of the |
245 | // requested titles and do not mess up continuation. |
246 | $holeIndex = array_search( false, array_values( $data ), true ); |
247 | $data = array_slice( $data, 0, $holeIndex ?: null, true ); |
248 | $statuses = array_slice( $statuses, 0, $holeIndex ?: null, true ); |
249 | |
250 | $status = StatusValue::newGood( $data ); |
251 | array_walk( $statuses, [ $status, 'merge' ] ); |
252 | $status->success = array_map( static function ( StatusValue $s ) { |
253 | return $s->isOK(); |
254 | }, $statuses ); |
255 | $status->successCount = count( array_filter( $status->success ) ); |
256 | $status->failCount = count( $status->success ) - $status->successCount; |
257 | $status->setResult( (bool)$status->successCount, $data ); |
258 | return $status; |
259 | } |
260 | |
261 | /** |
262 | * Add extra days (with a null value) to the beginning of a date range to make it have at least |
263 | * ::$cachedDays days. |
264 | * @param array $data YYYY-MM-DD => count, ordered, has less than $cachedDays items |
265 | * @param int $days |
266 | * @return array |
267 | */ |
268 | protected function extendDateRange( $data, $days ) { |
269 | // set to noon to avoid skip second and similar problems |
270 | $day = strtotime( array_key_first( $data ) . 'T00:00Z' ) + 12 * 3600; |
271 | for ( $i = $days - count( $data ); $i > 0; $i-- ) { |
272 | $day -= 24 * 3600; |
273 | $data = [ gmdate( 'Y-m-d', $day ) => null ] + $data; |
274 | } |
275 | return $data; |
276 | } |
277 | } |