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