Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
11.36% |
36 / 317 |
|
15.38% |
6 / 39 |
CRAP | |
0.00% |
0 / 1 |
QueryPage | |
11.39% |
36 / 316 |
|
15.38% |
6 / 39 |
7056.85 | |
0.00% |
0 / 1 |
getPages | |
7.69% |
3 / 39 |
|
0.00% |
0 / 1 |
5.15 | |||
setLinkBatchFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLinkBatchFactory | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getDisabledQueryPages | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
setListoutput | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getQueryInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getSQL | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getOrderFields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
usesTimestamps | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
sortDescending | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isExpensive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isCacheable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isCached | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
isSyndicated | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
formatResult | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getPageHeader | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
showEmptyText | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
linkParameters | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
recache | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
110 | |||
getRecacheDB | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
delete | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
deleteAllCachedData | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
reallyDoQuery | |
66.67% |
22 / 33 |
|
0.00% |
0 / 1 |
17.33 | |||
doQuery | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
fetchFromCache | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
getCacheOrderFields | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCachedTimestamp | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
getLimitOffset | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
getDBLimit | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getMaxResults | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 68 |
|
0.00% |
0 / 1 |
240 | |||
outputResults | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
90 | |||
openList | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
closeList | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
preprocessResults | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
executeLBFromResultWrapper | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
setDBLoadBalancer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDBLoadBalancer | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setDatabaseProvider | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDatabaseProvider | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 |
1 | <?php |
2 | /** |
3 | * Base code for "query" special pages. |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | * @ingroup SpecialPage |
22 | */ |
23 | |
24 | namespace MediaWiki\SpecialPage; |
25 | |
26 | use Exception; |
27 | use LogicException; |
28 | use MediaWiki\Cache\LinkBatchFactory; |
29 | use MediaWiki\Config\Config; |
30 | use MediaWiki\HookContainer\HookRunner; |
31 | use MediaWiki\Linker\LinkTarget; |
32 | use MediaWiki\MainConfigNames; |
33 | use MediaWiki\MediaWikiServices; |
34 | use MediaWiki\Output\OutputPage; |
35 | use MediaWiki\Specials\SpecialAncientPages; |
36 | use MediaWiki\Specials\SpecialBrokenRedirects; |
37 | use MediaWiki\Specials\SpecialDeadendPages; |
38 | use MediaWiki\Specials\SpecialDoubleRedirects; |
39 | use MediaWiki\Specials\SpecialFewestRevisions; |
40 | use MediaWiki\Specials\SpecialLinkSearch; |
41 | use MediaWiki\Specials\SpecialListDuplicatedFiles; |
42 | use MediaWiki\Specials\SpecialListRedirects; |
43 | use MediaWiki\Specials\SpecialLonelyPages; |
44 | use MediaWiki\Specials\SpecialLongPages; |
45 | use MediaWiki\Specials\SpecialMediaStatistics; |
46 | use MediaWiki\Specials\SpecialMIMESearch; |
47 | use MediaWiki\Specials\SpecialMostCategories; |
48 | use MediaWiki\Specials\SpecialMostImages; |
49 | use MediaWiki\Specials\SpecialMostInterwikis; |
50 | use MediaWiki\Specials\SpecialMostLinked; |
51 | use MediaWiki\Specials\SpecialMostLinkedCategories; |
52 | use MediaWiki\Specials\SpecialMostLinkedTemplates; |
53 | use MediaWiki\Specials\SpecialMostRevisions; |
54 | use MediaWiki\Specials\SpecialShortPages; |
55 | use MediaWiki\Specials\SpecialUncategorizedCategories; |
56 | use MediaWiki\Specials\SpecialUncategorizedImages; |
57 | use MediaWiki\Specials\SpecialUncategorizedPages; |
58 | use MediaWiki\Specials\SpecialUncategorizedTemplates; |
59 | use MediaWiki\Specials\SpecialUnusedCategories; |
60 | use MediaWiki\Specials\SpecialUnusedImages; |
61 | use MediaWiki\Specials\SpecialUnusedTemplates; |
62 | use MediaWiki\Specials\SpecialUnwatchedPages; |
63 | use MediaWiki\Specials\SpecialWantedCategories; |
64 | use MediaWiki\Specials\SpecialWantedFiles; |
65 | use MediaWiki\Specials\SpecialWantedPages; |
66 | use MediaWiki\Specials\SpecialWantedTemplates; |
67 | use MediaWiki\Specials\SpecialWithoutInterwiki; |
68 | use MWDebug; |
69 | use Skin; |
70 | use stdClass; |
71 | use Wikimedia\Rdbms\DBError; |
72 | use Wikimedia\Rdbms\IConnectionProvider; |
73 | use Wikimedia\Rdbms\IDatabase; |
74 | use Wikimedia\Rdbms\ILoadBalancer; |
75 | use Wikimedia\Rdbms\IReadableDatabase; |
76 | use Wikimedia\Rdbms\IResultWrapper; |
77 | use Wikimedia\Rdbms\SelectQueryBuilder; |
78 | use Xml; |
79 | |
80 | /** |
81 | * This is a class for doing query pages; since they're almost all the same, |
82 | * we factor out some of the functionality into a superclass, and let |
83 | * subclasses derive from it. |
84 | * |
85 | * @stable to extend |
86 | * |
87 | * @ingroup SpecialPage |
88 | */ |
89 | abstract class QueryPage extends SpecialPage { |
90 | /** @var bool Whether or not we want plain listoutput rather than an ordered list */ |
91 | protected $listoutput = false; |
92 | |
93 | /** @var int The offset and limit in use, as passed to the query() function */ |
94 | protected $offset = 0; |
95 | |
96 | /** @var int */ |
97 | protected $limit = 0; |
98 | |
99 | /** |
100 | * The number of rows returned by the query. Reading this variable |
101 | * only makes sense in functions that are run after the query has been |
102 | * done, such as preprocessResults() and formatRow(). |
103 | * |
104 | * @var int |
105 | */ |
106 | protected $numRows; |
107 | |
108 | /** |
109 | * @var string|null|false |
110 | */ |
111 | protected $cachedTimestamp = null; |
112 | |
113 | /** |
114 | * @var bool Whether to show prev/next links |
115 | */ |
116 | protected $shownavigation = true; |
117 | |
118 | /** @var ILoadBalancer|null */ |
119 | private $loadBalancer = null; |
120 | |
121 | /** @var IConnectionProvider|null */ |
122 | private $databaseProvider = null; |
123 | |
124 | /** @var LinkBatchFactory|null */ |
125 | private $linkBatchFactory = null; |
126 | |
127 | /** |
128 | * Get a list of query page classes and their associated special pages, |
129 | * for periodic updates. |
130 | * |
131 | * DO NOT CHANGE THIS LIST without testing that |
132 | * maintenance/updateSpecialPages.php still works. |
133 | * |
134 | * @return array[] List of [ string $class, string $specialPageName, ?int $limit (optional) ]. |
135 | * Limit defaults to $wgQueryCacheLimit if not given. |
136 | */ |
137 | public static function getPages() { |
138 | static $qp = null; |
139 | |
140 | if ( $qp === null ) { |
141 | $qp = [ |
142 | [ SpecialAncientPages::class, 'Ancientpages' ], |
143 | [ SpecialBrokenRedirects::class, 'BrokenRedirects' ], |
144 | [ SpecialDeadendPages::class, 'Deadendpages' ], |
145 | [ SpecialDoubleRedirects::class, 'DoubleRedirects' ], |
146 | [ SpecialListDuplicatedFiles::class, 'ListDuplicatedFiles' ], |
147 | [ SpecialLinkSearch::class, 'LinkSearch' ], |
148 | [ SpecialListRedirects::class, 'Listredirects' ], |
149 | [ SpecialLonelyPages::class, 'Lonelypages' ], |
150 | [ SpecialLongPages::class, 'Longpages' ], |
151 | [ SpecialMediaStatistics::class, 'MediaStatistics', SpecialMediaStatistics::MAX_LIMIT ], |
152 | [ SpecialMIMESearch::class, 'MIMEsearch' ], |
153 | [ SpecialMostCategories::class, 'Mostcategories' ], |
154 | [ SpecialMostImages::class, 'Mostimages' ], |
155 | [ SpecialMostInterwikis::class, 'Mostinterwikis' ], |
156 | [ SpecialMostLinkedCategories::class, 'Mostlinkedcategories' ], |
157 | [ SpecialMostLinkedTemplates::class, 'Mostlinkedtemplates' ], |
158 | [ SpecialMostLinked::class, 'Mostlinked' ], |
159 | [ SpecialMostRevisions::class, 'Mostrevisions' ], |
160 | [ SpecialFewestRevisions::class, 'Fewestrevisions' ], |
161 | [ SpecialShortPages::class, 'Shortpages' ], |
162 | [ SpecialUncategorizedCategories::class, 'Uncategorizedcategories' ], |
163 | [ SpecialUncategorizedPages::class, 'Uncategorizedpages' ], |
164 | [ SpecialUncategorizedImages::class, 'Uncategorizedimages' ], |
165 | [ SpecialUncategorizedTemplates::class, 'Uncategorizedtemplates' ], |
166 | [ SpecialUnusedCategories::class, 'Unusedcategories' ], |
167 | [ SpecialUnusedImages::class, 'Unusedimages' ], |
168 | [ SpecialWantedCategories::class, 'Wantedcategories' ], |
169 | [ SpecialWantedFiles::class, 'Wantedfiles' ], |
170 | [ SpecialWantedPages::class, 'Wantedpages' ], |
171 | [ SpecialWantedTemplates::class, 'Wantedtemplates' ], |
172 | [ SpecialUnwatchedPages::class, 'Unwatchedpages' ], |
173 | [ SpecialUnusedTemplates::class, 'Unusedtemplates' ], |
174 | [ SpecialWithoutInterwiki::class, 'Withoutinterwiki' ], |
175 | ]; |
176 | ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onWgQueryPages( $qp ); |
177 | } |
178 | |
179 | return $qp; |
180 | } |
181 | |
182 | /** |
183 | * @since 1.36 |
184 | * @param LinkBatchFactory $linkBatchFactory |
185 | */ |
186 | final protected function setLinkBatchFactory( LinkBatchFactory $linkBatchFactory ) { |
187 | $this->linkBatchFactory = $linkBatchFactory; |
188 | } |
189 | |
190 | /** |
191 | * @since 1.36 |
192 | * @return LinkBatchFactory |
193 | */ |
194 | final protected function getLinkBatchFactory(): LinkBatchFactory { |
195 | if ( $this->linkBatchFactory === null ) { |
196 | // Fallback if not provided |
197 | // TODO Change to wfWarn in a future release |
198 | $this->linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory(); |
199 | } |
200 | return $this->linkBatchFactory; |
201 | } |
202 | |
203 | /** |
204 | * Get a list of disabled query pages and their run mode |
205 | * @param Config $config |
206 | * @return string[] |
207 | */ |
208 | public static function getDisabledQueryPages( Config $config ) { |
209 | $disableQueryPageUpdate = $config->get( MainConfigNames::DisableQueryPageUpdate ); |
210 | |
211 | if ( !is_array( $disableQueryPageUpdate ) ) { |
212 | return []; |
213 | } |
214 | |
215 | $pages = []; |
216 | foreach ( $disableQueryPageUpdate as $name => $runMode ) { |
217 | if ( is_int( $name ) ) { |
218 | // The run mode may be omitted |
219 | $pages[$runMode] = 'disabled'; |
220 | } else { |
221 | $pages[$name] = $runMode; |
222 | } |
223 | } |
224 | return $pages; |
225 | } |
226 | |
227 | /** |
228 | * A mutator for $this->listoutput; |
229 | * |
230 | * @param bool $bool |
231 | */ |
232 | protected function setListoutput( $bool ) { |
233 | $this->listoutput = $bool; |
234 | } |
235 | |
236 | /** |
237 | * Subclasses return an SQL query here, formatted as an array with the |
238 | * following keys: |
239 | * tables => Table(s) for passing to Database::select() |
240 | * fields => Field(s) for passing to Database::select(), may be * |
241 | * conds => WHERE conditions |
242 | * options => options |
243 | * join_conds => JOIN conditions |
244 | * |
245 | * Note that the query itself should return the following three columns: |
246 | * 'namespace', 'title', and 'value'. 'value' is used for sorting. |
247 | * |
248 | * These may be stored in the querycache table for expensive queries, |
249 | * and that cached data will be returned sometimes, so the presence of |
250 | * extra fields can't be relied upon. The cached 'value' column will be |
251 | * an integer; non-numeric values are useful only for sorting the |
252 | * initial query (except if they're timestamps, see usesTimestamps()). |
253 | * |
254 | * Don't include an ORDER or LIMIT clause, they will be added. |
255 | * |
256 | * If this function is not overridden or returns something other than |
257 | * an array, getSQL() will be used instead. This is for backwards |
258 | * compatibility only and is strongly deprecated. |
259 | * @stable to override |
260 | * @return array |
261 | * @since 1.18 |
262 | */ |
263 | public function getQueryInfo() { |
264 | // @phan-suppress-next-line PhanTypeMismatchReturnProbablyReal null needed for b/c checks |
265 | return null; |
266 | } |
267 | |
268 | /** |
269 | * For back-compat, subclasses may return a raw SQL query here, as a string. |
270 | * @deprecated since 1.39; getQueryInfo() should be overridden instead. |
271 | * @return string |
272 | * @suppress PhanPluginNeverReturnMethod |
273 | */ |
274 | protected function getSQL() { |
275 | wfDeprecated( __METHOD__, '1.39' ); |
276 | throw new LogicException( "Bug in a QueryPage: doesn't implement getQueryInfo() nor " |
277 | . "getQuery() properly" ); |
278 | } |
279 | |
280 | /** |
281 | * Subclasses return an array of fields to order by here. Don't append |
282 | * DESC to the field names, that'll be done automatically if |
283 | * sortDescending() returns true. |
284 | * @stable to override |
285 | * @return string[] |
286 | * @since 1.18 |
287 | */ |
288 | protected function getOrderFields() { |
289 | return [ 'value' ]; |
290 | } |
291 | |
292 | /** |
293 | * Does this query return timestamps rather than integers in its |
294 | * 'value' field? If true, this class will convert 'value' to a |
295 | * UNIX timestamp for caching. |
296 | * NOTE: formatRow() may get timestamps in TS_MW (mysql), TS_DB (pgsql) |
297 | * or TS_UNIX (querycache) format, so be sure to always run them |
298 | * through wfTimestamp() |
299 | * @stable to override |
300 | * @return bool |
301 | * @since 1.18 |
302 | */ |
303 | public function usesTimestamps() { |
304 | return false; |
305 | } |
306 | |
307 | /** |
308 | * Override to sort by increasing values |
309 | * |
310 | * @stable to override |
311 | * @return bool |
312 | */ |
313 | protected function sortDescending() { |
314 | return true; |
315 | } |
316 | |
317 | /** |
318 | * Should this query page only be updated offline on large wikis? |
319 | * |
320 | * If the query for this page is considered too expensive to run on-demand |
321 | * for large wikis (e.g. every time the special page is viewed), then |
322 | * implement this as returning true. |
323 | * |
324 | * "Large wikis" are those that enable $wgMiserMode. The value of |
325 | * ::isExpensive() has no effect by default when $wgMiserMode is off. |
326 | * |
327 | * It is expected that such large wikis, periodically run the |
328 | * UpdateSpecialPages maintenance script to update these query pages. |
329 | * |
330 | * By enabling $wgDisableQueryPages, all query pages will be considered |
331 | * as expensive and updated offline only, even query pages that do not |
332 | * mark themselves as expensive. |
333 | * |
334 | * @stable to override |
335 | * @return bool |
336 | */ |
337 | public function isExpensive() { |
338 | return $this->getConfig()->get( MainConfigNames::DisableQueryPages ); |
339 | } |
340 | |
341 | /** |
342 | * Is the output of this query cacheable? Non-cacheable expensive pages |
343 | * will be disabled in miser mode and will not have their results written |
344 | * to the querycache table. |
345 | * @stable to override |
346 | * @return bool |
347 | * @since 1.18 |
348 | */ |
349 | public function isCacheable() { |
350 | return true; |
351 | } |
352 | |
353 | /** |
354 | * Whether or not the output of the page in question is retrieved from |
355 | * the database cache. |
356 | * |
357 | * @stable to override |
358 | * @return bool |
359 | */ |
360 | public function isCached() { |
361 | return $this->isExpensive() && $this->getConfig()->get( MainConfigNames::MiserMode ); |
362 | } |
363 | |
364 | /** |
365 | * Sometimes we don't want to build rss / atom feeds. |
366 | * |
367 | * @stable to override |
368 | * @return bool |
369 | */ |
370 | public function isSyndicated() { |
371 | return true; |
372 | } |
373 | |
374 | /** |
375 | * Formats the results of the query for display. The skin is the current |
376 | * skin; you can use it for making links. The result is a single row of |
377 | * result data. You should be able to grab SQL results from it. |
378 | * If the function returns false, the line output will be skipped. |
379 | * @param Skin $skin |
380 | * @param stdClass $result Result row |
381 | * @return string|bool String or false to skip |
382 | */ |
383 | abstract protected function formatResult( $skin, $result ); |
384 | |
385 | /** |
386 | * The content returned by this function will be output before any result |
387 | * |
388 | * @stable to override |
389 | * @return string |
390 | */ |
391 | protected function getPageHeader() { |
392 | return ''; |
393 | } |
394 | |
395 | /** |
396 | * Outputs some kind of an informative message (via OutputPage) to let the |
397 | * user know that the query returned nothing and thus there's nothing to |
398 | * show. |
399 | * |
400 | * @since 1.26 |
401 | */ |
402 | protected function showEmptyText() { |
403 | $this->getOutput()->addWikiMsg( 'specialpage-empty' ); |
404 | } |
405 | |
406 | /** |
407 | * If using extra form wheely-dealies, return a set of parameters here |
408 | * as an associative array. They will be encoded and added to the paging |
409 | * links (prev/next/lengths). |
410 | * |
411 | * @stable to override |
412 | * @return array |
413 | */ |
414 | protected function linkParameters() { |
415 | return []; |
416 | } |
417 | |
418 | /** |
419 | * Clear the cache and save new results |
420 | * |
421 | * @stable to override |
422 | * |
423 | * @param int|false $limit Limit for SQL statement or false for no limit |
424 | * @param bool $ignoreErrors Whether to ignore database errors |
425 | * @throws DBError|Exception |
426 | * @return bool|int |
427 | */ |
428 | public function recache( $limit, $ignoreErrors = true ) { |
429 | if ( !$this->isCacheable() ) { |
430 | return 0; |
431 | } |
432 | |
433 | $fname = static::class . '::recache'; |
434 | $dbw = $this->getDatabaseProvider()->getPrimaryDatabase(); |
435 | |
436 | try { |
437 | // Do query |
438 | $res = $this->reallyDoQuery( $limit, false ); |
439 | $num = false; |
440 | if ( $res ) { |
441 | $num = $res->numRows(); |
442 | // Fetch results |
443 | $vals = []; |
444 | foreach ( $res as $i => $row ) { |
445 | if ( isset( $row->value ) ) { |
446 | if ( $this->usesTimestamps() ) { |
447 | $value = (int)wfTimestamp( TS_UNIX, $row->value ); |
448 | } else { |
449 | $value = intval( $row->value ); // T16414 |
450 | } |
451 | } else { |
452 | $value = $i; |
453 | } |
454 | |
455 | $vals[] = [ |
456 | 'qc_type' => $this->getName(), |
457 | 'qc_namespace' => $row->namespace, |
458 | 'qc_title' => $row->title, |
459 | 'qc_value' => $value |
460 | ]; |
461 | } |
462 | |
463 | $dbw->doAtomicSection( |
464 | $fname, |
465 | function ( IDatabase $dbw, $fname ) use ( $vals ) { |
466 | // Clear out any old cached data |
467 | $dbw->newDeleteQueryBuilder() |
468 | ->deleteFrom( 'querycache' ) |
469 | ->where( [ 'qc_type' => $this->getName() ] ) |
470 | ->caller( $fname )->execute(); |
471 | // Update the querycache_info record for the page |
472 | $dbw->newInsertQueryBuilder() |
473 | ->insertInto( 'querycache_info' ) |
474 | ->row( [ 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ] ) |
475 | ->onDuplicateKeyUpdate() |
476 | ->uniqueIndexFields( [ 'qci_type' ] ) |
477 | ->set( [ 'qci_timestamp' => $dbw->timestamp() ] ) |
478 | ->caller( $fname )->execute(); |
479 | } |
480 | ); |
481 | // Save results into the querycache table on the primary DB |
482 | if ( count( $vals ) ) { |
483 | foreach ( array_chunk( $vals, 500 ) as $chunk ) { |
484 | $dbw->newInsertQueryBuilder() |
485 | ->insertInto( 'querycache' ) |
486 | ->rows( $chunk ) |
487 | ->caller( $fname )->execute(); |
488 | } |
489 | } |
490 | } |
491 | } catch ( DBError $e ) { |
492 | if ( !$ignoreErrors ) { |
493 | throw $e; // report query error |
494 | } |
495 | $num = false; // set result to false to indicate error |
496 | } |
497 | |
498 | return $num; |
499 | } |
500 | |
501 | /** |
502 | * Get a DB connection to be used for slow recache queries |
503 | * @stable to override |
504 | * @return IDatabase |
505 | */ |
506 | protected function getRecacheDB() { |
507 | return $this->getDBLoadBalancer() |
508 | ->getConnectionRef( ILoadBalancer::DB_REPLICA, 'vslow' ); |
509 | } |
510 | |
511 | /** |
512 | * Remove a cached result. |
513 | * Useful for interactive backlogs where the user can fix problems in-place. |
514 | * @param LinkTarget $title The page to remove. |
515 | * @since 1.34 |
516 | */ |
517 | public function delete( LinkTarget $title ) { |
518 | if ( $this->isCached() ) { |
519 | $dbw = $this->getDatabaseProvider()->getPrimaryDatabase(); |
520 | $dbw->newDeleteQueryBuilder() |
521 | ->deleteFrom( 'querycache' ) |
522 | ->where( [ |
523 | 'qc_type' => $this->getName(), |
524 | 'qc_namespace' => $title->getNamespace(), |
525 | 'qc_title' => $title->getDBkey(), |
526 | ] ) |
527 | ->caller( __METHOD__ )->execute(); |
528 | } |
529 | } |
530 | |
531 | /** |
532 | * Remove all cached value |
533 | * This is needed when the page is no longer using the cache |
534 | * @since 1.36 |
535 | */ |
536 | public function deleteAllCachedData() { |
537 | $fname = static::class . '::' . __FUNCTION__; |
538 | $dbw = $this->getDatabaseProvider()->getPrimaryDatabase(); |
539 | $dbw->newDeleteQueryBuilder() |
540 | ->deleteFrom( 'querycache' ) |
541 | ->where( [ 'qc_type' => $this->getName() ] ) |
542 | ->caller( $fname )->execute(); |
543 | $dbw->newDeleteQueryBuilder() |
544 | ->deleteFrom( 'querycachetwo' ) |
545 | ->where( [ 'qcc_type' => $this->getName() ] ) |
546 | ->caller( $fname )->execute(); |
547 | $dbw->newDeleteQueryBuilder() |
548 | ->deleteFrom( 'querycache_info' ) |
549 | ->where( [ 'qci_type' => $this->getName() ] ) |
550 | ->caller( $fname )->execute(); |
551 | } |
552 | |
553 | /** |
554 | * Run the query and return the result |
555 | * @stable to override |
556 | * @param int|false $limit Numerical limit or false for no limit |
557 | * @param int|false $offset Numerical offset or false for no offset |
558 | * @return IResultWrapper |
559 | * @since 1.18 |
560 | */ |
561 | public function reallyDoQuery( $limit, $offset = false ) { |
562 | $fname = static::class . '::reallyDoQuery'; |
563 | $dbr = $this->getRecacheDB(); |
564 | $query = $this->getQueryInfo(); |
565 | $order = $this->getOrderFields(); |
566 | |
567 | if ( $this->sortDescending() ) { |
568 | foreach ( $order as &$field ) { |
569 | $field .= ' DESC'; |
570 | } |
571 | } |
572 | |
573 | if ( is_array( $query ) ) { |
574 | $tables = isset( $query['tables'] ) ? (array)$query['tables'] : []; |
575 | $fields = isset( $query['fields'] ) ? (array)$query['fields'] : []; |
576 | $conds = isset( $query['conds'] ) ? (array)$query['conds'] : []; |
577 | $options = isset( $query['options'] ) ? (array)$query['options'] : []; |
578 | $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : []; |
579 | |
580 | if ( $order ) { |
581 | $options['ORDER BY'] = $order; |
582 | } |
583 | |
584 | if ( $limit !== false ) { |
585 | $options['LIMIT'] = intval( $limit ); |
586 | } |
587 | |
588 | if ( $offset !== false ) { |
589 | $options['OFFSET'] = intval( $offset ); |
590 | } |
591 | |
592 | $res = $dbr->select( $tables, $fields, $conds, $fname, |
593 | $options, $join_conds |
594 | ); |
595 | } else { |
596 | // Old-fashioned raw SQL style, deprecated |
597 | MWDebug::detectDeprecatedOverride( |
598 | $this, |
599 | __CLASS__, |
600 | 'getSQL', |
601 | '1.39' |
602 | ); |
603 | $sql = $this->getSQL(); |
604 | $sql .= ' ORDER BY ' . implode( ', ', $order ); |
605 | $sql = $dbr->limitResult( $sql, $limit, $offset ); |
606 | // phpcs:ignore MediaWiki.Usage.DbrQueryUsage.DbrQueryFound |
607 | $res = $dbr->query( $sql, $fname ); |
608 | } |
609 | |
610 | return $res; |
611 | } |
612 | |
613 | /** |
614 | * Somewhat deprecated, you probably want to be using execute() |
615 | * @param int|false $offset |
616 | * @param int|false $limit |
617 | * @return IResultWrapper |
618 | */ |
619 | public function doQuery( $offset = false, $limit = false ) { |
620 | if ( $this->isCached() && $this->isCacheable() ) { |
621 | return $this->fetchFromCache( $limit, $offset ); |
622 | } else { |
623 | return $this->reallyDoQuery( $limit, $offset ); |
624 | } |
625 | } |
626 | |
627 | /** |
628 | * Fetch the query results from the query cache |
629 | * @stable to override |
630 | * |
631 | * @param int|false $limit Numerical limit or false for no limit |
632 | * @param int|false $offset Numerical offset or false for no offset |
633 | * @return IResultWrapper |
634 | * @since 1.18 |
635 | */ |
636 | public function fetchFromCache( $limit, $offset = false ) { |
637 | $dbr = $this->getDatabaseProvider()->getReplicaDatabase(); |
638 | $queryBuilder = $dbr->newSelectQueryBuilder() |
639 | ->select( [ 'qc_type', 'namespace' => 'qc_namespace', 'title' => 'qc_title', 'value' => 'qc_value' ] ) |
640 | ->from( 'querycache' ) |
641 | ->where( [ 'qc_type' => $this->getName() ] ); |
642 | |
643 | if ( $limit !== false ) { |
644 | $queryBuilder->limit( intval( $limit ) ); |
645 | } |
646 | |
647 | if ( $offset !== false ) { |
648 | $queryBuilder->offset( intval( $offset ) ); |
649 | } |
650 | |
651 | $order = $this->getCacheOrderFields(); |
652 | if ( $this->sortDescending() ) { |
653 | $queryBuilder->orderBy( $order, SelectQueryBuilder::SORT_DESC ); |
654 | } else { |
655 | $queryBuilder->orderBy( $order ); |
656 | } |
657 | |
658 | return $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
659 | } |
660 | |
661 | /** |
662 | * Return the order fields for fetchFromCache. Default is to always use |
663 | * "ORDER BY value" which was the default prior to this function. |
664 | * @stable to override |
665 | * @return array |
666 | * @since 1.29 |
667 | */ |
668 | protected function getCacheOrderFields() { |
669 | return [ 'value' ]; |
670 | } |
671 | |
672 | /** |
673 | * @return string|false |
674 | */ |
675 | public function getCachedTimestamp() { |
676 | if ( $this->cachedTimestamp === null ) { |
677 | $dbr = $this->getDatabaseProvider()->getReplicaDatabase(); |
678 | $fname = static::class . '::getCachedTimestamp'; |
679 | $this->cachedTimestamp = $dbr->newSelectQueryBuilder() |
680 | ->select( 'qci_timestamp' ) |
681 | ->from( 'querycache_info' ) |
682 | ->where( [ 'qci_type' => $this->getName() ] ) |
683 | ->caller( $fname )->fetchField(); |
684 | } |
685 | return $this->cachedTimestamp; |
686 | } |
687 | |
688 | /** |
689 | * Returns limit and offset, as returned by $this->getRequest()->getLimitOffsetForUser(). |
690 | * Subclasses may override this to further restrict or modify limit and offset. |
691 | * |
692 | * @note Restricts the offset parameter, as most query pages have inefficient paging |
693 | * |
694 | * Its generally expected that the returned limit will not be 0, and the returned |
695 | * offset will be less than the max results. |
696 | * |
697 | * @since 1.26 |
698 | * @return int[] list( $limit, $offset ) |
699 | */ |
700 | protected function getLimitOffset() { |
701 | [ $limit, $offset ] = $this->getRequest() |
702 | ->getLimitOffsetForUser( $this->getUser() ); |
703 | if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
704 | $maxResults = $this->getMaxResults(); |
705 | // Can't display more than max results on a page |
706 | $limit = min( $limit, $maxResults ); |
707 | // Can't skip over more than the end of $maxResults |
708 | $offset = min( $offset, $maxResults + 1 ); |
709 | } |
710 | return [ $limit, $offset ]; |
711 | } |
712 | |
713 | /** |
714 | * What is limit to fetch from DB |
715 | * |
716 | * Used to make it appear the DB stores less results then it actually does |
717 | * @param int $uiLimit Limit from UI |
718 | * @param int $uiOffset Offset from UI |
719 | * @return int Limit to use for DB (not including extra row to see if at end) |
720 | */ |
721 | protected function getDBLimit( $uiLimit, $uiOffset ) { |
722 | $maxResults = $this->getMaxResults(); |
723 | if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
724 | $limit = min( $uiLimit + 1, $maxResults - $uiOffset ); |
725 | return max( $limit, 0 ); |
726 | } else { |
727 | return $uiLimit + 1; |
728 | } |
729 | } |
730 | |
731 | /** |
732 | * Get max number of results we can return in miser mode. |
733 | * |
734 | * Most QueryPage subclasses use inefficient paging, so limit the max amount we return |
735 | * This matters for uncached query pages that might otherwise accept an offset of 3 million |
736 | * |
737 | * @stable to override |
738 | * @since 1.27 |
739 | * @return int |
740 | */ |
741 | protected function getMaxResults() { |
742 | // Max of 10000, unless we store more than 10000 in query cache. |
743 | return max( $this->getConfig()->get( MainConfigNames::QueryCacheLimit ), 10000 ); |
744 | } |
745 | |
746 | /** |
747 | * This is the actual workhorse. It does everything needed to make a |
748 | * real, honest-to-gosh query page. |
749 | * @stable to override |
750 | * @param string|null $par |
751 | */ |
752 | public function execute( $par ) { |
753 | $this->checkPermissions(); |
754 | |
755 | $this->setHeaders(); |
756 | $this->outputHeader(); |
757 | |
758 | $out = $this->getOutput(); |
759 | |
760 | if ( $this->isCached() && !$this->isCacheable() ) { |
761 | $out->addWikiMsg( 'querypage-disabled' ); |
762 | return; |
763 | } |
764 | |
765 | $out->setSyndicated( $this->isSyndicated() ); |
766 | |
767 | if ( $this->limit == 0 && $this->offset == 0 ) { |
768 | [ $this->limit, $this->offset ] = $this->getLimitOffset(); |
769 | } |
770 | $dbLimit = $this->getDBLimit( $this->limit, $this->offset ); |
771 | // @todo Use doQuery() |
772 | if ( !$this->isCached() ) { |
773 | // select one extra row for navigation |
774 | $res = $this->reallyDoQuery( $dbLimit, $this->offset ); |
775 | } else { |
776 | // Get the cached result, select one extra row for navigation |
777 | $res = $this->fetchFromCache( $dbLimit, $this->offset ); |
778 | if ( !$this->listoutput ) { |
779 | // Fetch the timestamp of this update |
780 | $ts = $this->getCachedTimestamp(); |
781 | $lang = $this->getLanguage(); |
782 | $maxResults = $lang->formatNum( $this->getConfig()->get( |
783 | MainConfigNames::QueryCacheLimit ) ); |
784 | |
785 | if ( $ts ) { |
786 | $user = $this->getUser(); |
787 | $updated = $lang->userTimeAndDate( $ts, $user ); |
788 | $updateddate = $lang->userDate( $ts, $user ); |
789 | $updatedtime = $lang->userTime( $ts, $user ); |
790 | $out->addMeta( 'Data-Cache-Time', $ts ); |
791 | $out->addJsConfigVars( 'dataCacheTime', $ts ); |
792 | $out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults ); |
793 | } else { |
794 | $out->addWikiMsg( 'perfcached', $maxResults ); |
795 | } |
796 | |
797 | // If updates on this page have been disabled, let the user know |
798 | // that the data set won't be refreshed for now |
799 | $disabledQueryPages = self::getDisabledQueryPages( $this->getConfig() ); |
800 | if ( isset( $disabledQueryPages[$this->getName()] ) ) { |
801 | $runMode = $disabledQueryPages[$this->getName()]; |
802 | if ( $runMode === 'disabled' ) { |
803 | $out->wrapWikiMsg( |
804 | "<div class=\"mw-querypage-no-updates\">\n$1\n</div>", |
805 | 'querypage-no-updates' |
806 | ); |
807 | } else { |
808 | // Messages used here: querypage-updates-periodical |
809 | $out->wrapWikiMsg( |
810 | "<div class=\"mw-querypage-updates-" . $runMode . "\">\n$1\n</div>", |
811 | 'querypage-updates-' . $runMode |
812 | ); |
813 | } |
814 | } |
815 | } |
816 | } |
817 | |
818 | $this->numRows = $res->numRows(); |
819 | |
820 | $dbr = $this->getRecacheDB(); |
821 | $this->preprocessResults( $dbr, $res ); |
822 | |
823 | $out->addHTML( Xml::openElement( 'div', [ 'class' => 'mw-spcontent' ] ) ); |
824 | |
825 | // Top header and navigation |
826 | if ( $this->shownavigation ) { |
827 | $out->addHTML( $this->getPageHeader() ); |
828 | if ( $this->numRows > 0 ) { |
829 | $out->addHTML( $this->msg( 'showingresultsinrange' )->numParams( |
830 | min( $this->numRows, $this->limit ), // do not show the one extra row, if exist |
831 | $this->offset + 1, ( min( $this->numRows, $this->limit ) + $this->offset ) )->parseAsBlock() ); |
832 | // Disable the "next" link when we reach the end |
833 | $miserMaxResults = $this->getConfig()->get( MainConfigNames::MiserMode ) |
834 | && ( $this->offset + $this->limit >= $this->getMaxResults() ); |
835 | $atEnd = ( $this->numRows <= $this->limit ) || $miserMaxResults; |
836 | $paging = $this->buildPrevNextNavigation( $this->offset, |
837 | $this->limit, $this->linkParameters(), $atEnd, $par ); |
838 | $out->addHTML( '<p>' . $paging . '</p>' ); |
839 | } else { |
840 | // No results to show, so don't bother with "showing X of Y" etc. |
841 | // -- just let the user know and give up now |
842 | $this->showEmptyText(); |
843 | $out->addHTML( Xml::closeElement( 'div' ) ); |
844 | return; |
845 | } |
846 | } |
847 | |
848 | // The actual results; specialist subclasses will want to handle this |
849 | // with more than a straight list, so we hand them the info, plus |
850 | // an OutputPage, and let them get on with it |
851 | $this->outputResults( $out, |
852 | $this->getSkin(), |
853 | $dbr, // Should use IResultWrapper for this |
854 | $res, |
855 | min( $this->numRows, $this->limit ), // do not format the one extra row, if exist |
856 | $this->offset ); |
857 | |
858 | // Repeat the paging links at the bottom |
859 | if ( $this->shownavigation ) { |
860 | // @phan-suppress-next-line PhanPossiblyUndeclaredVariable paging is set when used here |
861 | $out->addHTML( '<p>' . $paging . '</p>' ); |
862 | } |
863 | |
864 | $out->addHTML( Xml::closeElement( 'div' ) ); |
865 | } |
866 | |
867 | /** |
868 | * Format and output report results using the given information plus |
869 | * OutputPage |
870 | * |
871 | * @stable to override |
872 | * |
873 | * @param OutputPage $out OutputPage to print to |
874 | * @param Skin $skin User skin to use |
875 | * @param IReadableDatabase $dbr Database (read) connection to use |
876 | * @param IResultWrapper $res Result pointer |
877 | * @param int $num Number of available result rows |
878 | * @param int $offset Paging offset |
879 | */ |
880 | protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) { |
881 | if ( $num > 0 ) { |
882 | $html = []; |
883 | if ( !$this->listoutput ) { |
884 | $html[] = $this->openList( $offset ); |
885 | } |
886 | |
887 | // $res might contain the whole 1,000 rows, so we read up to |
888 | // $num [should update this to use a Pager] |
889 | for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++ ) { |
890 | $line = $this->formatResult( $skin, $row ); |
891 | if ( $line ) { |
892 | $html[] = $this->listoutput |
893 | ? $line |
894 | : "<li>{$line}</li>\n"; |
895 | } |
896 | } |
897 | |
898 | if ( !$this->listoutput ) { |
899 | $html[] = $this->closeList(); |
900 | } |
901 | |
902 | $html = $this->listoutput |
903 | ? $this->getContentLanguage()->listToText( $html ) |
904 | : implode( '', $html ); |
905 | |
906 | $out->addHTML( $html ); |
907 | } |
908 | } |
909 | |
910 | /** |
911 | * @param int $offset |
912 | * @return string |
913 | */ |
914 | protected function openList( $offset ) { |
915 | return "\n<ol start='" . ( $offset + 1 ) . "' class='special'>\n"; |
916 | } |
917 | |
918 | /** |
919 | * @return string |
920 | */ |
921 | protected function closeList() { |
922 | return "</ol>\n"; |
923 | } |
924 | |
925 | /** |
926 | * Do any necessary preprocessing of the result object. |
927 | * @stable to override |
928 | * @param IDatabase $db |
929 | * @param IResultWrapper $res |
930 | */ |
931 | protected function preprocessResults( $db, $res ) { |
932 | } |
933 | |
934 | /** |
935 | * Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include |
936 | * title and optional the namespace field) and executes the batch. This operation will pre-cache |
937 | * LinkCache information like page existence and information for stub color and redirect hints. |
938 | * |
939 | * @note Call self::setLinkBatchFactory from special page constructor when use |
940 | * |
941 | * @param IResultWrapper $res The result wrapper to process. Needs to include the title |
942 | * field and namespace field, if the $ns parameter isn't set. |
943 | * @param int|null $ns Use this namespace for the given titles in the result wrapper, |
944 | * instead of the namespace value of $res. |
945 | */ |
946 | protected function executeLBFromResultWrapper( IResultWrapper $res, $ns = null ) { |
947 | if ( !$res->numRows() ) { |
948 | return; |
949 | } |
950 | |
951 | $batch = $this->getLinkBatchFactory()->newLinkBatch(); |
952 | foreach ( $res as $row ) { |
953 | $batch->add( $ns ?? (int)$row->namespace, $row->title ); |
954 | } |
955 | $batch->execute(); |
956 | |
957 | $res->seek( 0 ); |
958 | } |
959 | |
960 | /** |
961 | * @since 1.36 |
962 | * @param ILoadBalancer $loadBalancer |
963 | */ |
964 | final protected function setDBLoadBalancer( ILoadBalancer $loadBalancer ) { |
965 | $this->loadBalancer = $loadBalancer; |
966 | } |
967 | |
968 | /** |
969 | * @since 1.36 |
970 | * @return ILoadBalancer |
971 | */ |
972 | final protected function getDBLoadBalancer(): ILoadBalancer { |
973 | if ( $this->loadBalancer === null ) { |
974 | // Fallback if not provided |
975 | // TODO Change to wfWarn in a future release |
976 | $this->loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer(); |
977 | } |
978 | return $this->loadBalancer; |
979 | } |
980 | |
981 | /** |
982 | * @since 1.41 |
983 | * @param IConnectionProvider $databaseProvider |
984 | */ |
985 | final protected function setDatabaseProvider( IConnectionProvider $databaseProvider ) { |
986 | $this->databaseProvider = $databaseProvider; |
987 | } |
988 | |
989 | /** |
990 | * @since 1.41 |
991 | * @return IConnectionProvider |
992 | */ |
993 | final protected function getDatabaseProvider(): IConnectionProvider { |
994 | if ( $this->databaseProvider === null ) { |
995 | $this->databaseProvider = MediaWikiServices::getInstance()->getConnectionProvider(); |
996 | } |
997 | return $this->databaseProvider; |
998 | } |
999 | } |
1000 | |
1001 | /** @deprecated class alias since 1.41 */ |
1002 | class_alias( QueryPage::class, 'QueryPage' ); |