3 * Base code for "query" special pages.
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.
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.
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
21 * @ingroup SpecialPage
24 use MediaWiki\Linker\LinkTarget
;
25 use MediaWiki\MediaWikiServices
;
26 use Wikimedia\Rdbms\IResultWrapper
;
27 use Wikimedia\Rdbms\IDatabase
;
28 use Wikimedia\Rdbms\DBError
;
31 * This is a class for doing query pages; since they're almost all the same,
32 * we factor out some of the functionality into a superclass, and let
33 * subclasses derive from it.
34 * @ingroup SpecialPage
36 abstract class QueryPage
extends SpecialPage
{
37 /** @var bool Whether or not we want plain listoutput rather than an ordered list */
38 protected $listoutput = false;
40 /** @var int The offset and limit in use, as passed to the query() function */
41 protected $offset = 0;
47 * The number of rows returned by the query. Reading this variable
48 * only makes sense in functions that are run after the query has been
49 * done, such as preprocessResults() and formatRow().
58 protected $cachedTimestamp = null;
61 * @var bool Whether to show prev/next links
63 protected $shownavigation = true;
66 * Get a list of query page classes and their associated special pages,
67 * for periodic updates.
69 * DO NOT CHANGE THIS LIST without testing that
70 * maintenance/updateSpecialPages.php still works.
74 public static function getPages() {
78 // QueryPage subclass, Special page name
80 [ AncientPagesPage
::class, 'Ancientpages' ],
81 [ BrokenRedirectsPage
::class, 'BrokenRedirects' ],
82 [ DeadendPagesPage
::class, 'Deadendpages' ],
83 [ DoubleRedirectsPage
::class, 'DoubleRedirects' ],
84 [ FileDuplicateSearchPage
::class, 'FileDuplicateSearch' ],
85 [ ListDuplicatedFilesPage
::class, 'ListDuplicatedFiles' ],
86 [ LinkSearchPage
::class, 'LinkSearch' ],
87 [ ListredirectsPage
::class, 'Listredirects' ],
88 [ LonelyPagesPage
::class, 'Lonelypages' ],
89 [ LongPagesPage
::class, 'Longpages' ],
90 [ MediaStatisticsPage
::class, 'MediaStatistics' ],
91 [ MIMEsearchPage
::class, 'MIMEsearch' ],
92 [ MostcategoriesPage
::class, 'Mostcategories' ],
93 [ MostimagesPage
::class, 'Mostimages' ],
94 [ MostinterwikisPage
::class, 'Mostinterwikis' ],
95 [ MostlinkedCategoriesPage
::class, 'Mostlinkedcategories' ],
96 [ MostlinkedTemplatesPage
::class, 'Mostlinkedtemplates' ],
97 [ MostlinkedPage
::class, 'Mostlinked' ],
98 [ MostrevisionsPage
::class, 'Mostrevisions' ],
99 [ FewestrevisionsPage
::class, 'Fewestrevisions' ],
100 [ ShortPagesPage
::class, 'Shortpages' ],
101 [ UncategorizedCategoriesPage
::class, 'Uncategorizedcategories' ],
102 [ UncategorizedPagesPage
::class, 'Uncategorizedpages' ],
103 [ UncategorizedImagesPage
::class, 'Uncategorizedimages' ],
104 [ UncategorizedTemplatesPage
::class, 'Uncategorizedtemplates' ],
105 [ UnusedCategoriesPage
::class, 'Unusedcategories' ],
106 [ UnusedimagesPage
::class, 'Unusedimages' ],
107 [ WantedCategoriesPage
::class, 'Wantedcategories' ],
108 [ WantedFilesPage
::class, 'Wantedfiles' ],
109 [ WantedPagesPage
::class, 'Wantedpages' ],
110 [ WantedTemplatesPage
::class, 'Wantedtemplates' ],
111 [ UnwatchedpagesPage
::class, 'Unwatchedpages' ],
112 [ UnusedtemplatesPage
::class, 'Unusedtemplates' ],
113 [ WithoutInterwikiPage
::class, 'Withoutinterwiki' ],
115 Hooks
::run( 'wgQueryPages', [ &$qp ] );
122 * A mutator for $this->listoutput;
126 function setListoutput( $bool ) {
127 $this->listoutput
= $bool;
131 * Subclasses return an SQL query here, formatted as an array with the
133 * tables => Table(s) for passing to Database::select()
134 * fields => Field(s) for passing to Database::select(), may be *
135 * conds => WHERE conditions
137 * join_conds => JOIN conditions
139 * Note that the query itself should return the following three columns:
140 * 'namespace', 'title', and 'value'. 'value' is used for sorting.
142 * These may be stored in the querycache table for expensive queries,
143 * and that cached data will be returned sometimes, so the presence of
144 * extra fields can't be relied upon. The cached 'value' column will be
145 * an integer; non-numeric values are useful only for sorting the
146 * initial query (except if they're timestamps, see usesTimestamps()).
148 * Don't include an ORDER or LIMIT clause, they will be added.
150 * If this function is not overridden or returns something other than
151 * an array, getSQL() will be used instead. This is for backwards
152 * compatibility only and is strongly deprecated.
156 public function getQueryInfo() {
161 * For back-compat, subclasses may return a raw SQL query here, as a string.
162 * This is strongly deprecated; getQueryInfo() should be overridden instead.
163 * @throws MWException
167 /* Implement getQueryInfo() instead */
168 throw new MWException( "Bug in a QueryPage: doesn't implement getQueryInfo() nor "
169 . "getQuery() properly" );
173 * Subclasses return an array of fields to order by here. Don't append
174 * DESC to the field names, that'll be done automatically if
175 * sortDescending() returns true.
179 function getOrderFields() {
184 * Does this query return timestamps rather than integers in its
185 * 'value' field? If true, this class will convert 'value' to a
186 * UNIX timestamp for caching.
187 * NOTE: formatRow() may get timestamps in TS_MW (mysql), TS_DB (pgsql)
188 * or TS_UNIX (querycache) format, so be sure to always run them
189 * through wfTimestamp()
193 public function usesTimestamps() {
198 * Override to sort by increasing values
202 function sortDescending() {
207 * Is this query expensive (for some definition of expensive)? Then we
208 * don't let it run in miser mode. $wgDisableQueryPages causes all query
209 * pages to be declared expensive. Some query pages are always expensive.
213 public function isExpensive() {
214 return $this->getConfig()->get( 'DisableQueryPages' );
218 * Is the output of this query cacheable? Non-cacheable expensive pages
219 * will be disabled in miser mode and will not have their results written
220 * to the querycache table.
224 public function isCacheable() {
229 * Whether or not the output of the page in question is retrieved from
230 * the database cache.
234 public function isCached() {
235 return $this->isExpensive() && $this->getConfig()->get( 'MiserMode' );
239 * Sometime we don't want to build rss / atom feeds.
243 function isSyndicated() {
248 * Formats the results of the query for display. The skin is the current
249 * skin; you can use it for making links. The result is a single row of
250 * result data. You should be able to grab SQL results off of it.
251 * If the function returns false, the line output will be skipped.
253 * @param object $result Result row
254 * @return string|bool String or false to skip
256 abstract function formatResult( $skin, $result );
259 * The content returned by this function will be output before any result
263 function getPageHeader() {
268 * Outputs some kind of an informative message (via OutputPage) to let the
269 * user know that the query returned nothing and thus there's nothing to
274 protected function showEmptyText() {
275 $this->getOutput()->addWikiMsg( 'specialpage-empty' );
279 * If using extra form wheely-dealies, return a set of parameters here
280 * as an associative array. They will be encoded and added to the paging
281 * links (prev/next/lengths).
285 function linkParameters() {
290 * Clear the cache and save new results
292 * @param int|bool $limit Limit for SQL statement
293 * @param bool $ignoreErrors Whether to ignore database errors
294 * @throws DBError|Exception
297 public function recache( $limit, $ignoreErrors = true ) {
298 if ( !$this->isCacheable() ) {
302 $fname = static::class . '::recache';
303 $dbw = wfGetDB( DB_MASTER
);
310 $res = $this->reallyDoQuery( $limit, false );
313 $num = $res->numRows();
316 foreach ( $res as $i => $row ) {
317 if ( isset( $row->value
) ) {
318 if ( $this->usesTimestamps() ) {
319 $value = wfTimestamp( TS_UNIX
,
322 $value = intval( $row->value
); // T16414
329 'qc_type' => $this->getName(),
330 'qc_namespace' => $row->namespace,
331 'qc_title' => $row->title
,
336 $dbw->doAtomicSection(
338 function ( IDatabase
$dbw, $fname ) use ( $vals ) {
339 # Clear out any old cached data
340 $dbw->delete( 'querycache',
341 [ 'qc_type' => $this->getName() ],
344 # Save results into the querycache table on the master
345 if ( count( $vals ) ) {
346 $dbw->insert( 'querycache', $vals, $fname );
348 # Update the querycache_info record for the page
349 $dbw->delete( 'querycache_info',
350 [ 'qci_type' => $this->getName() ],
353 $dbw->insert( 'querycache_info',
354 [ 'qci_type' => $this->getName(),
355 'qci_timestamp' => $dbw->timestamp() ],
361 } catch ( DBError
$e ) {
362 if ( !$ignoreErrors ) {
363 throw $e; // report query error
365 $num = false; // set result to false to indicate error
372 * Get a DB connection to be used for slow recache queries
375 function getRecacheDB() {
376 return wfGetDB( DB_REPLICA
, [ $this->getName(), 'QueryPage::recache', 'vslow' ] );
380 * Remove a cached result.
381 * Useful for interactive backlogs where the user can fix problems in-place.
382 * @param LinkTarget $title The page to remove.
385 public function delete( LinkTarget
$title ) {
386 if ( $this->isCached() ) {
387 $dbw = wfGetDB( DB_MASTER
);
388 $dbw->delete( 'querycache', [
389 'qc_type' => $this->getName(),
390 'qc_namespace' => $title->getNamespace(),
391 'qc_title' => $title->getDBkey(),
397 * Run the query and return the result
398 * @param int|bool $limit Numerical limit or false for no limit
399 * @param int|bool $offset Numerical offset or false for no offset
400 * @return IResultWrapper
403 public function reallyDoQuery( $limit, $offset = false ) {
404 $fname = static::class . '::reallyDoQuery';
405 $dbr = $this->getRecacheDB();
406 $query = $this->getQueryInfo();
407 $order = $this->getOrderFields();
409 if ( $this->sortDescending() ) {
410 foreach ( $order as &$field ) {
415 if ( is_array( $query ) ) {
416 $tables = isset( $query['tables'] ) ?
(array)$query['tables'] : [];
417 $fields = isset( $query['fields'] ) ?
(array)$query['fields'] : [];
418 $conds = isset( $query['conds'] ) ?
(array)$query['conds'] : [];
419 $options = isset( $query['options'] ) ?
(array)$query['options'] : [];
420 $join_conds = isset( $query['join_conds'] ) ?
(array)$query['join_conds'] : [];
423 $options['ORDER BY'] = $order;
426 if ( $limit !== false ) {
427 $options['LIMIT'] = intval( $limit );
430 if ( $offset !== false ) {
431 $options['OFFSET'] = intval( $offset );
434 $res = $dbr->select( $tables, $fields, $conds, $fname,
435 $options, $join_conds
438 // Old-fashioned raw SQL style, deprecated
439 $sql = $this->getSQL();
440 $sql .= ' ORDER BY ' . implode( ', ', $order );
441 $sql = $dbr->limitResult( $sql, $limit, $offset );
442 $res = $dbr->query( $sql, $fname );
449 * Somewhat deprecated, you probably want to be using execute()
450 * @param int|bool $offset
451 * @param int|bool $limit
452 * @return IResultWrapper
454 public function doQuery( $offset = false, $limit = false ) {
455 if ( $this->isCached() && $this->isCacheable() ) {
456 return $this->fetchFromCache( $limit, $offset );
458 return $this->reallyDoQuery( $limit, $offset );
463 * Fetch the query results from the query cache
464 * @param int|bool $limit Numerical limit or false for no limit
465 * @param int|bool $offset Numerical offset or false for no offset
466 * @return IResultWrapper
469 public function fetchFromCache( $limit, $offset = false ) {
470 $dbr = wfGetDB( DB_REPLICA
);
473 if ( $limit !== false ) {
474 $options['LIMIT'] = intval( $limit );
477 if ( $offset !== false ) {
478 $options['OFFSET'] = intval( $offset );
481 $order = $this->getCacheOrderFields();
482 if ( $this->sortDescending() ) {
483 foreach ( $order as &$field ) {
488 $options['ORDER BY'] = $order;
491 return $dbr->select( 'querycache',
493 'namespace' => 'qc_namespace',
494 'title' => 'qc_title',
495 'value' => 'qc_value' ],
496 [ 'qc_type' => $this->getName() ],
503 * Return the order fields for fetchFromCache. Default is to always use
504 * "ORDER BY value" which was the default prior to this function.
508 function getCacheOrderFields() {
515 public function getCachedTimestamp() {
516 if ( is_null( $this->cachedTimestamp
) ) {
517 $dbr = wfGetDB( DB_REPLICA
);
518 $fname = static::class . '::getCachedTimestamp';
519 $this->cachedTimestamp
= $dbr->selectField( 'querycache_info', 'qci_timestamp',
520 [ 'qci_type' => $this->getName() ], $fname );
522 return $this->cachedTimestamp
;
526 * Returns limit and offset, as returned by $this->getRequest()->getLimitOffset().
527 * Subclasses may override this to further restrict or modify limit and offset.
529 * @note Restricts the offset parameter, as most query pages have inefficient paging
531 * Its generally expected that the returned limit will not be 0, and the returned
532 * offset will be less than the max results.
535 * @return int[] list( $limit, $offset )
537 protected function getLimitOffset() {
538 list( $limit, $offset ) = $this->getRequest()->getLimitOffset();
539 if ( $this->getConfig()->get( 'MiserMode' ) ) {
540 $maxResults = $this->getMaxResults();
541 // Can't display more than max results on a page
542 $limit = min( $limit, $maxResults );
543 // Can't skip over more than the end of $maxResults
544 $offset = min( $offset, $maxResults +
1 );
546 return [ $limit, $offset ];
550 * What is limit to fetch from DB
552 * Used to make it appear the DB stores less results then it actually does
553 * @param int $uiLimit Limit from UI
554 * @param int $uiOffset Offset from UI
555 * @return int Limit to use for DB (not including extra row to see if at end)
557 protected function getDBLimit( $uiLimit, $uiOffset ) {
558 $maxResults = $this->getMaxResults();
559 if ( $this->getConfig()->get( 'MiserMode' ) ) {
560 $limit = min( $uiLimit +
1, $maxResults - $uiOffset );
561 return max( $limit, 0 );
568 * Get max number of results we can return in miser mode.
570 * Most QueryPage subclasses use inefficient paging, so limit the max amount we return
571 * This matters for uncached query pages that might otherwise accept an offset of 3 million
576 protected function getMaxResults() {
577 // Max of 10000, unless we store more than 10000 in query cache.
578 return max( $this->getConfig()->get( 'QueryCacheLimit' ), 10000 );
582 * This is the actual workhorse. It does everything needed to make a
583 * real, honest-to-gosh query page.
584 * @param string|null $par
586 public function execute( $par ) {
587 $user = $this->getUser();
588 if ( !$this->userCanExecute( $user ) ) {
589 $this->displayRestrictionError();
594 $this->outputHeader();
596 $out = $this->getOutput();
598 if ( $this->isCached() && !$this->isCacheable() ) {
599 $out->addWikiMsg( 'querypage-disabled' );
603 $out->setSyndicated( $this->isSyndicated() );
605 if ( $this->limit
== 0 && $this->offset
== 0 ) {
606 list( $this->limit
, $this->offset
) = $this->getLimitOffset();
608 $dbLimit = $this->getDBLimit( $this->limit
, $this->offset
);
609 // @todo Use doQuery()
610 if ( !$this->isCached() ) {
611 # select one extra row for navigation
612 $res = $this->reallyDoQuery( $dbLimit, $this->offset
);
614 # Get the cached result, select one extra row for navigation
615 $res = $this->fetchFromCache( $dbLimit, $this->offset
);
616 if ( !$this->listoutput
) {
617 # Fetch the timestamp of this update
618 $ts = $this->getCachedTimestamp();
619 $lang = $this->getLanguage();
620 $maxResults = $lang->formatNum( $this->getConfig()->get( 'QueryCacheLimit' ) );
623 $updated = $lang->userTimeAndDate( $ts, $user );
624 $updateddate = $lang->userDate( $ts, $user );
625 $updatedtime = $lang->userTime( $ts, $user );
626 $out->addMeta( 'Data-Cache-Time', $ts );
627 $out->addJsConfigVars( 'dataCacheTime', $ts );
628 $out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
630 $out->addWikiMsg( 'perfcached', $maxResults );
633 # If updates on this page have been disabled, let the user know
634 # that the data set won't be refreshed for now
635 if ( is_array( $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
636 && in_array( $this->getName(), $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
639 "<div class=\"mw-querypage-no-updates\">\n$1\n</div>",
640 'querypage-no-updates'
646 $this->numRows
= $res->numRows();
648 $dbr = $this->getRecacheDB();
649 $this->preprocessResults( $dbr, $res );
651 $out->addHTML( Xml
::openElement( 'div', [ 'class' => 'mw-spcontent' ] ) );
653 # Top header and navigation
654 if ( $this->shownavigation
) {
655 $out->addHTML( $this->getPageHeader() );
656 if ( $this->numRows
> 0 ) {
657 $out->addHTML( $this->msg( 'showingresultsinrange' )->numParams(
658 min( $this->numRows
, $this->limit
), # do not show the one extra row, if exist
659 $this->offset +
1, ( min( $this->numRows
, $this->limit
) +
$this->offset
) )->parseAsBlock() );
660 # Disable the "next" link when we reach the end
661 $miserMaxResults = $this->getConfig()->get( 'MiserMode' )
662 && ( $this->offset +
$this->limit
>= $this->getMaxResults() );
663 $atEnd = ( $this->numRows
<= $this->limit
) ||
$miserMaxResults;
664 $paging = $this->buildPrevNextNavigation( $this->offset
,
665 $this->limit
, $this->linkParameters(), $atEnd, $par );
666 $out->addHTML( '<p>' . $paging . '</p>' );
668 # No results to show, so don't bother with "showing X of Y" etc.
669 # -- just let the user know and give up now
670 $this->showEmptyText();
671 $out->addHTML( Xml
::closeElement( 'div' ) );
676 # The actual results; specialist subclasses will want to handle this
677 # with more than a straight list, so we hand them the info, plus
678 # an OutputPage, and let them get on with it
679 $this->outputResults( $out,
681 $dbr, # Should use IResultWrapper for this
683 min( $this->numRows
, $this->limit
), # do not format the one extra row, if exist
686 # Repeat the paging links at the bottom
687 if ( $this->shownavigation
) {
688 $out->addHTML( '<p>' . $paging . '</p>' );
691 $out->addHTML( Xml
::closeElement( 'div' ) );
695 * Format and output report results using the given information plus
698 * @param OutputPage $out OutputPage to print to
699 * @param Skin $skin User skin to use
700 * @param IDatabase $dbr Database (read) connection to use
701 * @param IResultWrapper $res Result pointer
702 * @param int $num Number of available result rows
703 * @param int $offset Paging offset
705 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
708 if ( !$this->listoutput
) {
709 $html[] = $this->openList( $offset );
712 # $res might contain the whole 1,000 rows, so we read up to
713 # $num [should update this to use a Pager]
714 for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++
) {
715 $line = $this->formatResult( $skin, $row );
717 $html[] = $this->listoutput
719 : "<li>{$line}</li>\n";
723 if ( !$this->listoutput
) {
724 $html[] = $this->closeList();
727 $html = $this->listoutput
728 ? MediaWikiServices
::getInstance()->getContentLanguage()->listToText( $html )
729 : implode( '', $html );
731 $out->addHTML( $html );
739 function openList( $offset ) {
740 return "\n<ol start='" . ( $offset +
1 ) . "' class='special'>\n";
746 function closeList() {
751 * Do any necessary preprocessing of the result object.
752 * @param IDatabase $db
753 * @param IResultWrapper $res
755 function preprocessResults( $db, $res ) {
759 * Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include
760 * title and optional the namespace field) and executes the batch. This operation will pre-cache
761 * LinkCache information like page existence and information for stub color and redirect hints.
763 * @param IResultWrapper $res The result wrapper to process. Needs to include the title
764 * field and namespace field, if the $ns parameter isn't set.
765 * @param null $ns Use this namespace for the given titles in the result wrapper,
766 * instead of the namespace value of $res.
768 protected function executeLBFromResultWrapper( IResultWrapper
$res, $ns = null ) {
769 if ( !$res->numRows() ) {
773 $batch = new LinkBatch
;
774 foreach ( $res as $row ) {
775 $batch->add( $ns ??
$row->namespace, $row->title
);