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
25 * This is a class for doing query pages; since they're almost all the same,
26 * we factor out some of the functionality into a superclass, and let
27 * subclasses derive from it.
28 * @ingroup SpecialPage
30 abstract class QueryPage
extends SpecialPage
{
31 /** @var bool Whether or not we want plain listoutput rather than an ordered list */
32 protected $listoutput = false;
34 /** @var int The offset and limit in use, as passed to the query() function */
35 protected $offset = 0;
41 * The number of rows returned by the query. Reading this variable
42 * only makes sense in functions that are run after the query has been
43 * done, such as preprocessResults() and formatRow().
47 protected $cachedTimestamp = null;
50 * Whether to show prev/next links
52 protected $shownavigation = true;
55 * Get a list of query page classes and their associated special pages,
56 * for periodic updates.
58 * DO NOT CHANGE THIS LIST without testing that
59 * maintenance/updateSpecialPages.php still works.
62 public static function getPages() {
66 // QueryPage subclass, Special page name
68 [ 'AncientPagesPage', 'Ancientpages' ],
69 [ 'BrokenRedirectsPage', 'BrokenRedirects' ],
70 [ 'DeadendPagesPage', 'Deadendpages' ],
71 [ 'DoubleRedirectsPage', 'DoubleRedirects' ],
72 [ 'FileDuplicateSearchPage', 'FileDuplicateSearch' ],
73 [ 'ListDuplicatedFilesPage', 'ListDuplicatedFiles' ],
74 [ 'LinkSearchPage', 'LinkSearch' ],
75 [ 'ListredirectsPage', 'Listredirects' ],
76 [ 'LonelyPagesPage', 'Lonelypages' ],
77 [ 'LongPagesPage', 'Longpages' ],
78 [ 'MediaStatisticsPage', 'MediaStatistics' ],
79 [ 'MIMEsearchPage', 'MIMEsearch' ],
80 [ 'MostcategoriesPage', 'Mostcategories' ],
81 [ 'MostimagesPage', 'Mostimages' ],
82 [ 'MostinterwikisPage', 'Mostinterwikis' ],
83 [ 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ],
84 [ 'MostlinkedTemplatesPage', 'Mostlinkedtemplates' ],
85 [ 'MostlinkedPage', 'Mostlinked' ],
86 [ 'MostrevisionsPage', 'Mostrevisions' ],
87 [ 'FewestrevisionsPage', 'Fewestrevisions' ],
88 [ 'ShortPagesPage', 'Shortpages' ],
89 [ 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ],
90 [ 'UncategorizedPagesPage', 'Uncategorizedpages' ],
91 [ 'UncategorizedImagesPage', 'Uncategorizedimages' ],
92 [ 'UncategorizedTemplatesPage', 'Uncategorizedtemplates' ],
93 [ 'UnusedCategoriesPage', 'Unusedcategories' ],
94 [ 'UnusedimagesPage', 'Unusedimages' ],
95 [ 'WantedCategoriesPage', 'Wantedcategories' ],
96 [ 'WantedFilesPage', 'Wantedfiles' ],
97 [ 'WantedPagesPage', 'Wantedpages' ],
98 [ 'WantedTemplatesPage', 'Wantedtemplates' ],
99 [ 'UnwatchedpagesPage', 'Unwatchedpages' ],
100 [ 'UnusedtemplatesPage', 'Unusedtemplates' ],
101 [ 'WithoutInterwikiPage', 'Withoutinterwiki' ],
103 Hooks
::run( 'wgQueryPages', [ &$qp ] );
110 * A mutator for $this->listoutput;
114 function setListoutput( $bool ) {
115 $this->listoutput
= $bool;
119 * Subclasses return an SQL query here, formatted as an array with the
121 * tables => Table(s) for passing to Database::select()
122 * fields => Field(s) for passing to Database::select(), may be *
123 * conds => WHERE conditions
125 * join_conds => JOIN conditions
127 * Note that the query itself should return the following three columns:
128 * 'namespace', 'title', and 'value'. 'value' is used for sorting.
130 * These may be stored in the querycache table for expensive queries,
131 * and that cached data will be returned sometimes, so the presence of
132 * extra fields can't be relied upon. The cached 'value' column will be
133 * an integer; non-numeric values are useful only for sorting the
134 * initial query (except if they're timestamps, see usesTimestamps()).
136 * Don't include an ORDER or LIMIT clause, they will be added.
138 * If this function is not overridden or returns something other than
139 * an array, getSQL() will be used instead. This is for backwards
140 * compatibility only and is strongly deprecated.
144 public function getQueryInfo() {
149 * For back-compat, subclasses may return a raw SQL query here, as a string.
150 * This is strongly deprecated; getQueryInfo() should be overridden instead.
151 * @throws MWException
155 /* Implement getQueryInfo() instead */
156 throw new MWException( "Bug in a QueryPage: doesn't implement getQueryInfo() nor "
157 . "getQuery() properly" );
161 * Subclasses return an array of fields to order by here. Don't append
162 * DESC to the field names, that'll be done automatically if
163 * sortDescending() returns true.
167 function getOrderFields() {
172 * Does this query return timestamps rather than integers in its
173 * 'value' field? If true, this class will convert 'value' to a
174 * UNIX timestamp for caching.
175 * NOTE: formatRow() may get timestamps in TS_MW (mysql), TS_DB (pgsql)
176 * or TS_UNIX (querycache) format, so be sure to always run them
177 * through wfTimestamp()
181 public function usesTimestamps() {
186 * Override to sort by increasing values
190 function sortDescending() {
195 * Is this query expensive (for some definition of expensive)? Then we
196 * don't let it run in miser mode. $wgDisableQueryPages causes all query
197 * pages to be declared expensive. Some query pages are always expensive.
201 public function isExpensive() {
202 return $this->getConfig()->get( 'DisableQueryPages' );
206 * Is the output of this query cacheable? Non-cacheable expensive pages
207 * will be disabled in miser mode and will not have their results written
208 * to the querycache table.
212 public function isCacheable() {
217 * Whether or not the output of the page in question is retrieved from
218 * the database cache.
222 public function isCached() {
223 return $this->isExpensive() && $this->getConfig()->get( 'MiserMode' );
227 * Sometime we don't want to build rss / atom feeds.
231 function isSyndicated() {
236 * Formats the results of the query for display. The skin is the current
237 * skin; you can use it for making links. The result is a single row of
238 * result data. You should be able to grab SQL results off of it.
239 * If the function returns false, the line output will be skipped.
241 * @param object $result Result row
242 * @return string|bool String or false to skip
244 abstract function formatResult( $skin, $result );
247 * The content returned by this function will be output before any result
251 function getPageHeader() {
256 * Outputs some kind of an informative message (via OutputPage) to let the
257 * user know that the query returned nothing and thus there's nothing to
262 protected function showEmptyText() {
263 $this->getOutput()->addWikiMsg( 'specialpage-empty' );
267 * If using extra form wheely-dealies, return a set of parameters here
268 * as an associative array. They will be encoded and added to the paging
269 * links (prev/next/lengths).
273 function linkParameters() {
278 * Some special pages (for example SpecialListusers used to) might not return the
279 * current object formatted, but return the previous one instead.
280 * Setting this to return true will ensure formatResult() is called
281 * one more time to make sure that the very last result is formatted
284 * @deprecated since 1.27
288 function tryLastResult() {
293 * Clear the cache and save new results
295 * @param int|bool $limit Limit for SQL statement
296 * @param bool $ignoreErrors Whether to ignore database errors
297 * @throws DBError|Exception
300 public function recache( $limit, $ignoreErrors = true ) {
301 if ( !$this->isCacheable() ) {
305 $fname = get_class( $this ) . '::recache';
306 $dbw = wfGetDB( DB_MASTER
);
313 $res = $this->reallyDoQuery( $limit, false );
316 $num = $res->numRows();
319 foreach ( $res as $row ) {
320 if ( isset( $row->value
) ) {
321 if ( $this->usesTimestamps() ) {
322 $value = wfTimestamp( TS_UNIX
,
325 $value = intval( $row->value
); // @bug 14414
332 'qc_type' => $this->getName(),
333 'qc_namespace' => $row->namespace,
334 'qc_title' => $row->title
,
339 $dbw->doAtomicSection(
341 function ( IDatabase
$dbw, $fname ) use ( $vals ) {
342 # Clear out any old cached data
343 $dbw->delete( 'querycache',
344 [ 'qc_type' => $this->getName() ],
347 # Save results into the querycache table on the master
348 if ( count( $vals ) ) {
349 $dbw->insert( 'querycache', $vals, $fname );
351 # Update the querycache_info record for the page
352 $dbw->delete( 'querycache_info',
353 [ 'qci_type' => $this->getName() ],
356 $dbw->insert( 'querycache_info',
357 [ 'qci_type' => $this->getName(),
358 'qci_timestamp' => $dbw->timestamp() ],
364 } catch ( DBError
$e ) {
365 if ( !$ignoreErrors ) {
366 throw $e; // report query error
368 $num = false; // set result to false to indicate error
375 * Get a DB connection to be used for slow recache queries
378 function getRecacheDB() {
379 return wfGetDB( DB_SLAVE
, [ $this->getName(), 'QueryPage::recache', 'vslow' ] );
383 * Run the query and return the result
384 * @param int|bool $limit Numerical limit or false for no limit
385 * @param int|bool $offset Numerical offset or false for no offset
386 * @return ResultWrapper
389 public function reallyDoQuery( $limit, $offset = false ) {
390 $fname = get_class( $this ) . "::reallyDoQuery";
391 $dbr = $this->getRecacheDB();
392 $query = $this->getQueryInfo();
393 $order = $this->getOrderFields();
395 if ( $this->sortDescending() ) {
396 foreach ( $order as &$field ) {
401 if ( is_array( $query ) ) {
402 $tables = isset( $query['tables'] ) ?
(array)$query['tables'] : [];
403 $fields = isset( $query['fields'] ) ?
(array)$query['fields'] : [];
404 $conds = isset( $query['conds'] ) ?
(array)$query['conds'] : [];
405 $options = isset( $query['options'] ) ?
(array)$query['options'] : [];
406 $join_conds = isset( $query['join_conds'] ) ?
(array)$query['join_conds'] : [];
408 if ( count( $order ) ) {
409 $options['ORDER BY'] = $order;
412 if ( $limit !== false ) {
413 $options['LIMIT'] = intval( $limit );
416 if ( $offset !== false ) {
417 $options['OFFSET'] = intval( $offset );
420 $res = $dbr->select( $tables, $fields, $conds, $fname,
421 $options, $join_conds
424 // Old-fashioned raw SQL style, deprecated
425 $sql = $this->getSQL();
426 $sql .= ' ORDER BY ' . implode( ', ', $order );
427 $sql = $dbr->limitResult( $sql, $limit, $offset );
428 $res = $dbr->query( $sql, $fname );
435 * Somewhat deprecated, you probably want to be using execute()
436 * @param int|bool $offset
437 * @param int|bool $limit
438 * @return ResultWrapper
440 public function doQuery( $offset = false, $limit = false ) {
441 if ( $this->isCached() && $this->isCacheable() ) {
442 return $this->fetchFromCache( $limit, $offset );
444 return $this->reallyDoQuery( $limit, $offset );
449 * Fetch the query results from the query cache
450 * @param int|bool $limit Numerical limit or false for no limit
451 * @param int|bool $offset Numerical offset or false for no offset
452 * @return ResultWrapper
455 public function fetchFromCache( $limit, $offset = false ) {
456 $dbr = wfGetDB( DB_SLAVE
);
458 if ( $limit !== false ) {
459 $options['LIMIT'] = intval( $limit );
461 if ( $offset !== false ) {
462 $options['OFFSET'] = intval( $offset );
464 if ( $this->sortDescending() ) {
465 $options['ORDER BY'] = 'qc_value DESC';
467 $options['ORDER BY'] = 'qc_value ASC';
469 return $dbr->select( 'querycache', [ 'qc_type',
470 'namespace' => 'qc_namespace',
471 'title' => 'qc_title',
472 'value' => 'qc_value' ],
473 [ 'qc_type' => $this->getName() ],
478 public function getCachedTimestamp() {
479 if ( is_null( $this->cachedTimestamp
) ) {
480 $dbr = wfGetDB( DB_SLAVE
);
481 $fname = get_class( $this ) . '::getCachedTimestamp';
482 $this->cachedTimestamp
= $dbr->selectField( 'querycache_info', 'qci_timestamp',
483 [ 'qci_type' => $this->getName() ], $fname );
485 return $this->cachedTimestamp
;
489 * Returns limit and offset, as returned by $this->getRequest()->getLimitOffset().
490 * Subclasses may override this to further restrict or modify limit and offset.
492 * @note Restricts the offset parameter, as most query pages have inefficient paging
495 * @return int[] list( $limit, $offset )
497 protected function getLimitOffset() {
498 list( $limit, $offset ) = $this->getRequest()->getLimitOffset();
499 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
500 $maxResults = $this->getMaxResults();
501 // Can't display more than max results on a page
502 $limit = min( $limit, $maxResults );
503 // Can't skip over more than $maxResults
504 $offset = min( $offset, $maxResults );
505 // Can't let $offset + $limit > $maxResults
506 $limit = min( $limit, $maxResults - $offset );
508 return [ $limit, $offset ];
512 * Get max number of results we can return in miser mode.
514 * Most QueryPage subclasses use inefficient paging, so limit the max amount we return
515 * This matters for uncached query pages that might otherwise accept an offset of 3 million
520 protected function getMaxResults() {
521 // Max of 10000, unless we store more than 5000 in query cache.
522 return max( $this->getConfig()->get( 'QueryCacheLimit' ), 10000 );
526 * This is the actual workhorse. It does everything needed to make a
527 * real, honest-to-gosh query page.
530 public function execute( $par ) {
531 $user = $this->getUser();
532 if ( !$this->userCanExecute( $user ) ) {
533 $this->displayRestrictionError();
538 $this->outputHeader();
540 $out = $this->getOutput();
542 if ( $this->isCached() && !$this->isCacheable() ) {
543 $out->addWikiMsg( 'querypage-disabled' );
547 $out->setSyndicated( $this->isSyndicated() );
549 if ( $this->limit
== 0 && $this->offset
== 0 ) {
550 list( $this->limit
, $this->offset
) = $this->getLimitOffset();
553 // @todo Use doQuery()
554 if ( !$this->isCached() ) {
555 # select one extra row for navigation
556 $res = $this->reallyDoQuery( $this->limit +
1, $this->offset
);
558 # Get the cached result, select one extra row for navigation
559 $res = $this->fetchFromCache( $this->limit +
1, $this->offset
);
560 if ( !$this->listoutput
) {
562 # Fetch the timestamp of this update
563 $ts = $this->getCachedTimestamp();
564 $lang = $this->getLanguage();
565 $maxResults = $lang->formatNum( $this->getConfig()->get( 'QueryCacheLimit' ) );
568 $updated = $lang->userTimeAndDate( $ts, $user );
569 $updateddate = $lang->userDate( $ts, $user );
570 $updatedtime = $lang->userTime( $ts, $user );
571 $out->addMeta( 'Data-Cache-Time', $ts );
572 $out->addJsConfigVars( 'dataCacheTime', $ts );
573 $out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
575 $out->addWikiMsg( 'perfcached', $maxResults );
578 # If updates on this page have been disabled, let the user know
579 # that the data set won't be refreshed for now
580 if ( is_array( $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
581 && in_array( $this->getName(), $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
584 "<div class=\"mw-querypage-no-updates\">\n$1\n</div>",
585 'querypage-no-updates'
591 $this->numRows
= $res->numRows();
593 $dbr = $this->getRecacheDB();
594 $this->preprocessResults( $dbr, $res );
596 $out->addHTML( Xml
::openElement( 'div', [ 'class' => 'mw-spcontent' ] ) );
598 # Top header and navigation
599 if ( $this->shownavigation
) {
600 $out->addHTML( $this->getPageHeader() );
601 if ( $this->numRows
> 0 ) {
602 $out->addHTML( $this->msg( 'showingresultsinrange' )->numParams(
603 min( $this->numRows
, $this->limit
), # do not show the one extra row, if exist
604 $this->offset +
1, ( min( $this->numRows
, $this->limit
) +
$this->offset
) )->parseAsBlock() );
605 # Disable the "next" link when we reach the end
606 $atEnd = ( $this->numRows
<= $this->limit
)
607 ||
( $this->offset +
$this-> limit
>= $this->getMaxResults() );
608 $paging = $this->getLanguage()->viewPrevNext( $this->getPageTitle( $par ), $this->offset
,
609 $this->limit
, $this->linkParameters(), $atEnd );
610 $out->addHTML( '<p>' . $paging . '</p>' );
612 # No results to show, so don't bother with "showing X of Y" etc.
613 # -- just let the user know and give up now
614 $this->showEmptyText();
615 $out->addHTML( Xml
::closeElement( 'div' ) );
620 # The actual results; specialist subclasses will want to handle this
621 # with more than a straight list, so we hand them the info, plus
622 # an OutputPage, and let them get on with it
623 $this->outputResults( $out,
625 $dbr, # Should use a ResultWrapper for this
627 min( $this->numRows
, $this->limit
), # do not format the one extra row, if exist
630 # Repeat the paging links at the bottom
631 if ( $this->shownavigation
) {
632 $out->addHTML( '<p>' . $paging . '</p>' );
635 $out->addHTML( Xml
::closeElement( 'div' ) );
639 * Format and output report results using the given information plus
642 * @param OutputPage $out OutputPage to print to
643 * @param Skin $skin User skin to use
644 * @param IDatabase $dbr Database (read) connection to use
645 * @param ResultWrapper $res Result pointer
646 * @param int $num Number of available result rows
647 * @param int $offset Paging offset
649 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
654 if ( !$this->listoutput
) {
655 $html[] = $this->openList( $offset );
658 # $res might contain the whole 1,000 rows, so we read up to
659 # $num [should update this to use a Pager]
660 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
661 for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++
) {
662 // @codingStandardsIgnoreEnd
663 $line = $this->formatResult( $skin, $row );
665 $html[] = $this->listoutput
667 : "<li>{$line}</li>\n";
671 # Flush the final result
672 if ( $this->tryLastResult() ) {
674 $line = $this->formatResult( $skin, $row );
676 $html[] = $this->listoutput
678 : "<li>{$line}</li>\n";
682 if ( !$this->listoutput
) {
683 $html[] = $this->closeList();
686 $html = $this->listoutput
687 ?
$wgContLang->listToText( $html )
688 : implode( '', $html );
690 $out->addHTML( $html );
698 function openList( $offset ) {
699 return "\n<ol start='" . ( $offset +
1 ) . "' class='special'>\n";
705 function closeList() {
710 * Do any necessary preprocessing of the result object.
711 * @param IDatabase $db
712 * @param ResultWrapper $res
714 function preprocessResults( $db, $res ) {
718 * Similar to above, but packaging in a syndicated feed instead of a web page
719 * @param string $class
723 function doFeed( $class = '', $limit = 50 ) {
724 if ( !$this->getConfig()->get( 'Feed' ) ) {
725 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
729 $limit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
731 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
732 if ( isset( $feedClasses[$class] ) ) {
733 /** @var RSSFeed|AtomFeed $feed */
734 $feed = new $feedClasses[$class](
740 $res = $this->reallyDoQuery( $limit, 0 );
741 foreach ( $res as $obj ) {
742 $item = $this->feedResult( $obj );
744 $feed->outItem( $item );
756 * Override for custom handling. If the titles/links are ok, just do
759 * @return FeedItem|null
761 function feedResult( $row ) {
762 if ( !isset( $row->title
) ) {
765 $title = Title
::makeTitle( intval( $row->namespace ), $row->title
);
767 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
770 $talkpage = $title->getTalkPage();
771 $comments = $talkpage->getFullURL();
775 $title->getPrefixedText(),
776 $this->feedItemDesc( $row ),
777 $title->getFullURL(),
779 $this->feedItemAuthor( $row ),
786 function feedItemDesc( $row ) {
787 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
790 function feedItemAuthor( $row ) {
791 return isset( $row->user_text
) ?
$row->user_text
: '';
794 function feedTitle() {
795 $desc = $this->getDescription();
796 $code = $this->getConfig()->get( 'LanguageCode' );
797 $sitename = $this->getConfig()->get( 'Sitename' );
798 return "$sitename - $desc [$code]";
801 function feedDesc() {
802 return $this->msg( 'tagline' )->text();
806 return $this->getPageTitle()->getFullURL();