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 Wikimedia\Rdbms\ResultWrapper
;
27 * This is a class for doing query pages; since they're almost all the same,
28 * we factor out some of the functionality into a superclass, and let
29 * subclasses derive from it.
30 * @ingroup SpecialPage
32 abstract class QueryPage
extends SpecialPage
{
33 /** @var bool Whether or not we want plain listoutput rather than an ordered list */
34 protected $listoutput = false;
36 /** @var int The offset and limit in use, as passed to the query() function */
37 protected $offset = 0;
43 * The number of rows returned by the query. Reading this variable
44 * only makes sense in functions that are run after the query has been
45 * done, such as preprocessResults() and formatRow().
49 protected $cachedTimestamp = null;
52 * Whether to show prev/next links
54 protected $shownavigation = true;
57 * Get a list of query page classes and their associated special pages,
58 * for periodic updates.
60 * DO NOT CHANGE THIS LIST without testing that
61 * maintenance/updateSpecialPages.php still works.
64 public static function getPages() {
68 // QueryPage subclass, Special page name
70 [ 'AncientPagesPage', 'Ancientpages' ],
71 [ 'BrokenRedirectsPage', 'BrokenRedirects' ],
72 [ 'DeadendPagesPage', 'Deadendpages' ],
73 [ 'DoubleRedirectsPage', 'DoubleRedirects' ],
74 [ 'FileDuplicateSearchPage', 'FileDuplicateSearch' ],
75 [ 'ListDuplicatedFilesPage', 'ListDuplicatedFiles' ],
76 [ 'LinkSearchPage', 'LinkSearch' ],
77 [ 'ListredirectsPage', 'Listredirects' ],
78 [ 'LonelyPagesPage', 'Lonelypages' ],
79 [ 'LongPagesPage', 'Longpages' ],
80 [ 'MediaStatisticsPage', 'MediaStatistics' ],
81 [ 'MIMEsearchPage', 'MIMEsearch' ],
82 [ 'MostcategoriesPage', 'Mostcategories' ],
83 [ 'MostimagesPage', 'Mostimages' ],
84 [ 'MostinterwikisPage', 'Mostinterwikis' ],
85 [ 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ],
86 [ 'MostlinkedTemplatesPage', 'Mostlinkedtemplates' ],
87 [ 'MostlinkedPage', 'Mostlinked' ],
88 [ 'MostrevisionsPage', 'Mostrevisions' ],
89 [ 'FewestrevisionsPage', 'Fewestrevisions' ],
90 [ 'ShortPagesPage', 'Shortpages' ],
91 [ 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ],
92 [ 'UncategorizedPagesPage', 'Uncategorizedpages' ],
93 [ 'UncategorizedImagesPage', 'Uncategorizedimages' ],
94 [ 'UncategorizedTemplatesPage', 'Uncategorizedtemplates' ],
95 [ 'UnusedCategoriesPage', 'Unusedcategories' ],
96 [ 'UnusedimagesPage', 'Unusedimages' ],
97 [ 'WantedCategoriesPage', 'Wantedcategories' ],
98 [ 'WantedFilesPage', 'Wantedfiles' ],
99 [ 'WantedPagesPage', 'Wantedpages' ],
100 [ 'WantedTemplatesPage', 'Wantedtemplates' ],
101 [ 'UnwatchedpagesPage', 'Unwatchedpages' ],
102 [ 'UnusedtemplatesPage', 'Unusedtemplates' ],
103 [ 'WithoutInterwikiPage', 'Withoutinterwiki' ],
105 Hooks
::run( 'wgQueryPages', [ &$qp ] );
112 * A mutator for $this->listoutput;
116 function setListoutput( $bool ) {
117 $this->listoutput
= $bool;
121 * Subclasses return an SQL query here, formatted as an array with the
123 * tables => Table(s) for passing to Database::select()
124 * fields => Field(s) for passing to Database::select(), may be *
125 * conds => WHERE conditions
127 * join_conds => JOIN conditions
129 * Note that the query itself should return the following three columns:
130 * 'namespace', 'title', and 'value'. 'value' is used for sorting.
132 * These may be stored in the querycache table for expensive queries,
133 * and that cached data will be returned sometimes, so the presence of
134 * extra fields can't be relied upon. The cached 'value' column will be
135 * an integer; non-numeric values are useful only for sorting the
136 * initial query (except if they're timestamps, see usesTimestamps()).
138 * Don't include an ORDER or LIMIT clause, they will be added.
140 * If this function is not overridden or returns something other than
141 * an array, getSQL() will be used instead. This is for backwards
142 * compatibility only and is strongly deprecated.
146 public function getQueryInfo() {
151 * For back-compat, subclasses may return a raw SQL query here, as a string.
152 * This is strongly deprecated; getQueryInfo() should be overridden instead.
153 * @throws MWException
157 /* Implement getQueryInfo() instead */
158 throw new MWException( "Bug in a QueryPage: doesn't implement getQueryInfo() nor "
159 . "getQuery() properly" );
163 * Subclasses return an array of fields to order by here. Don't append
164 * DESC to the field names, that'll be done automatically if
165 * sortDescending() returns true.
169 function getOrderFields() {
174 * Does this query return timestamps rather than integers in its
175 * 'value' field? If true, this class will convert 'value' to a
176 * UNIX timestamp for caching.
177 * NOTE: formatRow() may get timestamps in TS_MW (mysql), TS_DB (pgsql)
178 * or TS_UNIX (querycache) format, so be sure to always run them
179 * through wfTimestamp()
183 public function usesTimestamps() {
188 * Override to sort by increasing values
192 function sortDescending() {
197 * Is this query expensive (for some definition of expensive)? Then we
198 * don't let it run in miser mode. $wgDisableQueryPages causes all query
199 * pages to be declared expensive. Some query pages are always expensive.
203 public function isExpensive() {
204 return $this->getConfig()->get( 'DisableQueryPages' );
208 * Is the output of this query cacheable? Non-cacheable expensive pages
209 * will be disabled in miser mode and will not have their results written
210 * to the querycache table.
214 public function isCacheable() {
219 * Whether or not the output of the page in question is retrieved from
220 * the database cache.
224 public function isCached() {
225 return $this->isExpensive() && $this->getConfig()->get( 'MiserMode' );
229 * Sometime we don't want to build rss / atom feeds.
233 function isSyndicated() {
238 * Formats the results of the query for display. The skin is the current
239 * skin; you can use it for making links. The result is a single row of
240 * result data. You should be able to grab SQL results off of it.
241 * If the function returns false, the line output will be skipped.
243 * @param object $result Result row
244 * @return string|bool String or false to skip
246 abstract function formatResult( $skin, $result );
249 * The content returned by this function will be output before any result
253 function getPageHeader() {
258 * Outputs some kind of an informative message (via OutputPage) to let the
259 * user know that the query returned nothing and thus there's nothing to
264 protected function showEmptyText() {
265 $this->getOutput()->addWikiMsg( 'specialpage-empty' );
269 * If using extra form wheely-dealies, return a set of parameters here
270 * as an associative array. They will be encoded and added to the paging
271 * links (prev/next/lengths).
275 function linkParameters() {
280 * Some special pages (for example SpecialListusers used to) might not return the
281 * current object formatted, but return the previous one instead.
282 * Setting this to return true will ensure formatResult() is called
283 * one more time to make sure that the very last result is formatted
286 * @deprecated since 1.27
290 function tryLastResult() {
295 * Clear the cache and save new results
297 * @param int|bool $limit Limit for SQL statement
298 * @param bool $ignoreErrors Whether to ignore database errors
299 * @throws DBError|Exception
302 public function recache( $limit, $ignoreErrors = true ) {
303 if ( !$this->isCacheable() ) {
307 $fname = static::class . '::recache';
308 $dbw = wfGetDB( DB_MASTER
);
315 $res = $this->reallyDoQuery( $limit, false );
318 $num = $res->numRows();
321 foreach ( $res as $row ) {
322 if ( isset( $row->value
) ) {
323 if ( $this->usesTimestamps() ) {
324 $value = wfTimestamp( TS_UNIX
,
327 $value = intval( $row->value
); // T16414
334 'qc_type' => $this->getName(),
335 'qc_namespace' => $row->namespace,
336 'qc_title' => $row->title
,
341 $dbw->doAtomicSection(
343 function ( IDatabase
$dbw, $fname ) use ( $vals ) {
344 # Clear out any old cached data
345 $dbw->delete( 'querycache',
346 [ 'qc_type' => $this->getName() ],
349 # Save results into the querycache table on the master
350 if ( count( $vals ) ) {
351 $dbw->insert( 'querycache', $vals, $fname );
353 # Update the querycache_info record for the page
354 $dbw->delete( 'querycache_info',
355 [ 'qci_type' => $this->getName() ],
358 $dbw->insert( 'querycache_info',
359 [ 'qci_type' => $this->getName(),
360 'qci_timestamp' => $dbw->timestamp() ],
366 } catch ( DBError
$e ) {
367 if ( !$ignoreErrors ) {
368 throw $e; // report query error
370 $num = false; // set result to false to indicate error
377 * Get a DB connection to be used for slow recache queries
380 function getRecacheDB() {
381 return wfGetDB( DB_REPLICA
, [ $this->getName(), 'QueryPage::recache', 'vslow' ] );
385 * Run the query and return the result
386 * @param int|bool $limit Numerical limit or false for no limit
387 * @param int|bool $offset Numerical offset or false for no offset
388 * @return ResultWrapper
391 public function reallyDoQuery( $limit, $offset = false ) {
392 $fname = static::class . '::reallyDoQuery';
393 $dbr = $this->getRecacheDB();
394 $query = $this->getQueryInfo();
395 $order = $this->getOrderFields();
397 if ( $this->sortDescending() ) {
398 foreach ( $order as &$field ) {
403 if ( is_array( $query ) ) {
404 $tables = isset( $query['tables'] ) ?
(array)$query['tables'] : [];
405 $fields = isset( $query['fields'] ) ?
(array)$query['fields'] : [];
406 $conds = isset( $query['conds'] ) ?
(array)$query['conds'] : [];
407 $options = isset( $query['options'] ) ?
(array)$query['options'] : [];
408 $join_conds = isset( $query['join_conds'] ) ?
(array)$query['join_conds'] : [];
411 $options['ORDER BY'] = $order;
414 if ( $limit !== false ) {
415 $options['LIMIT'] = intval( $limit );
418 if ( $offset !== false ) {
419 $options['OFFSET'] = intval( $offset );
422 $res = $dbr->select( $tables, $fields, $conds, $fname,
423 $options, $join_conds
426 // Old-fashioned raw SQL style, deprecated
427 $sql = $this->getSQL();
428 $sql .= ' ORDER BY ' . implode( ', ', $order );
429 $sql = $dbr->limitResult( $sql, $limit, $offset );
430 $res = $dbr->query( $sql, $fname );
437 * Somewhat deprecated, you probably want to be using execute()
438 * @param int|bool $offset
439 * @param int|bool $limit
440 * @return ResultWrapper
442 public function doQuery( $offset = false, $limit = false ) {
443 if ( $this->isCached() && $this->isCacheable() ) {
444 return $this->fetchFromCache( $limit, $offset );
446 return $this->reallyDoQuery( $limit, $offset );
451 * Fetch the query results from the query cache
452 * @param int|bool $limit Numerical limit or false for no limit
453 * @param int|bool $offset Numerical offset or false for no offset
454 * @return ResultWrapper
457 public function fetchFromCache( $limit, $offset = false ) {
458 $dbr = wfGetDB( DB_REPLICA
);
461 if ( $limit !== false ) {
462 $options['LIMIT'] = intval( $limit );
465 if ( $offset !== false ) {
466 $options['OFFSET'] = intval( $offset );
469 $order = $this->getCacheOrderFields();
470 if ( $this->sortDescending() ) {
471 foreach ( $order as &$field ) {
476 $options['ORDER BY'] = $order;
479 return $dbr->select( 'querycache',
481 'namespace' => 'qc_namespace',
482 'title' => 'qc_title',
483 'value' => 'qc_value' ],
484 [ 'qc_type' => $this->getName() ],
491 * Return the order fields for fetchFromCache. Default is to always use
492 * "ORDER BY value" which was the default prior to this function.
496 function getCacheOrderFields() {
500 public function getCachedTimestamp() {
501 if ( is_null( $this->cachedTimestamp
) ) {
502 $dbr = wfGetDB( DB_REPLICA
);
503 $fname = static::class . '::getCachedTimestamp';
504 $this->cachedTimestamp
= $dbr->selectField( 'querycache_info', 'qci_timestamp',
505 [ 'qci_type' => $this->getName() ], $fname );
507 return $this->cachedTimestamp
;
511 * Returns limit and offset, as returned by $this->getRequest()->getLimitOffset().
512 * Subclasses may override this to further restrict or modify limit and offset.
514 * @note Restricts the offset parameter, as most query pages have inefficient paging
516 * Its generally expected that the returned limit will not be 0, and the returned
517 * offset will be less than the max results.
520 * @return int[] list( $limit, $offset )
522 protected function getLimitOffset() {
523 list( $limit, $offset ) = $this->getRequest()->getLimitOffset();
524 if ( $this->getConfig()->get( 'MiserMode' ) ) {
525 $maxResults = $this->getMaxResults();
526 // Can't display more than max results on a page
527 $limit = min( $limit, $maxResults );
528 // Can't skip over more than the end of $maxResults
529 $offset = min( $offset, $maxResults +
1 );
531 return [ $limit, $offset ];
535 * What is limit to fetch from DB
537 * Used to make it appear the DB stores less results then it actually does
538 * @param int $uiLimit Limit from UI
539 * @param int $uiOffset Offset from UI
540 * @return int Limit to use for DB (not including extra row to see if at end)
542 protected function getDBLimit( $uiLimit, $uiOffset ) {
543 $maxResults = $this->getMaxResults();
544 if ( $this->getConfig()->get( 'MiserMode' ) ) {
545 $limit = min( $uiLimit +
1, $maxResults - $uiOffset );
546 return max( $limit, 0 );
553 * Get max number of results we can return in miser mode.
555 * Most QueryPage subclasses use inefficient paging, so limit the max amount we return
556 * This matters for uncached query pages that might otherwise accept an offset of 3 million
561 protected function getMaxResults() {
562 // Max of 10000, unless we store more than 10000 in query cache.
563 return max( $this->getConfig()->get( 'QueryCacheLimit' ), 10000 );
567 * This is the actual workhorse. It does everything needed to make a
568 * real, honest-to-gosh query page.
571 public function execute( $par ) {
572 $user = $this->getUser();
573 if ( !$this->userCanExecute( $user ) ) {
574 $this->displayRestrictionError();
579 $this->outputHeader();
581 $out = $this->getOutput();
583 if ( $this->isCached() && !$this->isCacheable() ) {
584 $out->addWikiMsg( 'querypage-disabled' );
588 $out->setSyndicated( $this->isSyndicated() );
590 if ( $this->limit
== 0 && $this->offset
== 0 ) {
591 list( $this->limit
, $this->offset
) = $this->getLimitOffset();
593 $dbLimit = $this->getDBLimit( $this->limit
, $this->offset
);
594 // @todo Use doQuery()
595 if ( !$this->isCached() ) {
596 # select one extra row for navigation
597 $res = $this->reallyDoQuery( $dbLimit, $this->offset
);
599 # Get the cached result, select one extra row for navigation
600 $res = $this->fetchFromCache( $dbLimit, $this->offset
);
601 if ( !$this->listoutput
) {
603 # Fetch the timestamp of this update
604 $ts = $this->getCachedTimestamp();
605 $lang = $this->getLanguage();
606 $maxResults = $lang->formatNum( $this->getConfig()->get( 'QueryCacheLimit' ) );
609 $updated = $lang->userTimeAndDate( $ts, $user );
610 $updateddate = $lang->userDate( $ts, $user );
611 $updatedtime = $lang->userTime( $ts, $user );
612 $out->addMeta( 'Data-Cache-Time', $ts );
613 $out->addJsConfigVars( 'dataCacheTime', $ts );
614 $out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
616 $out->addWikiMsg( 'perfcached', $maxResults );
619 # If updates on this page have been disabled, let the user know
620 # that the data set won't be refreshed for now
621 if ( is_array( $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
622 && in_array( $this->getName(), $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
625 "<div class=\"mw-querypage-no-updates\">\n$1\n</div>",
626 'querypage-no-updates'
632 $this->numRows
= $res->numRows();
634 $dbr = $this->getRecacheDB();
635 $this->preprocessResults( $dbr, $res );
637 $out->addHTML( Xml
::openElement( 'div', [ 'class' => 'mw-spcontent' ] ) );
639 # Top header and navigation
640 if ( $this->shownavigation
) {
641 $out->addHTML( $this->getPageHeader() );
642 if ( $this->numRows
> 0 ) {
643 $out->addHTML( $this->msg( 'showingresultsinrange' )->numParams(
644 min( $this->numRows
, $this->limit
), # do not show the one extra row, if exist
645 $this->offset +
1, ( min( $this->numRows
, $this->limit
) +
$this->offset
) )->parseAsBlock() );
646 # Disable the "next" link when we reach the end
647 $miserMaxResults = $this->getConfig()->get( 'MiserMode' )
648 && ( $this->offset +
$this->limit
>= $this->getMaxResults() );
649 $atEnd = ( $this->numRows
<= $this->limit
) ||
$miserMaxResults;
650 $paging = $this->getLanguage()->viewPrevNext( $this->getPageTitle( $par ), $this->offset
,
651 $this->limit
, $this->linkParameters(), $atEnd );
652 $out->addHTML( '<p>' . $paging . '</p>' );
654 # No results to show, so don't bother with "showing X of Y" etc.
655 # -- just let the user know and give up now
656 $this->showEmptyText();
657 $out->addHTML( Xml
::closeElement( 'div' ) );
662 # The actual results; specialist subclasses will want to handle this
663 # with more than a straight list, so we hand them the info, plus
664 # an OutputPage, and let them get on with it
665 $this->outputResults( $out,
667 $dbr, # Should use a ResultWrapper for this
669 min( $this->numRows
, $this->limit
), # do not format the one extra row, if exist
672 # Repeat the paging links at the bottom
673 if ( $this->shownavigation
) {
674 $out->addHTML( '<p>' . $paging . '</p>' );
677 $out->addHTML( Xml
::closeElement( 'div' ) );
681 * Format and output report results using the given information plus
684 * @param OutputPage $out OutputPage to print to
685 * @param Skin $skin User skin to use
686 * @param IDatabase $dbr Database (read) connection to use
687 * @param ResultWrapper $res Result pointer
688 * @param int $num Number of available result rows
689 * @param int $offset Paging offset
691 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
696 if ( !$this->listoutput
) {
697 $html[] = $this->openList( $offset );
700 # $res might contain the whole 1,000 rows, so we read up to
701 # $num [should update this to use a Pager]
702 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
703 for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++
) {
704 // @codingStandardsIgnoreEnd
705 $line = $this->formatResult( $skin, $row );
707 $html[] = $this->listoutput
709 : "<li>{$line}</li>\n";
713 # Flush the final result
714 if ( $this->tryLastResult() ) {
716 $line = $this->formatResult( $skin, $row );
718 $html[] = $this->listoutput
720 : "<li>{$line}</li>\n";
724 if ( !$this->listoutput
) {
725 $html[] = $this->closeList();
728 $html = $this->listoutput
729 ?
$wgContLang->listToText( $html )
730 : implode( '', $html );
732 $out->addHTML( $html );
740 function openList( $offset ) {
741 return "\n<ol start='" . ( $offset +
1 ) . "' class='special'>\n";
747 function closeList() {
752 * Do any necessary preprocessing of the result object.
753 * @param IDatabase $db
754 * @param ResultWrapper $res
756 function preprocessResults( $db, $res ) {
760 * Similar to above, but packaging in a syndicated feed instead of a web page
761 * @param string $class
765 function doFeed( $class = '', $limit = 50 ) {
766 if ( !$this->getConfig()->get( 'Feed' ) ) {
767 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
771 $limit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
773 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
774 if ( isset( $feedClasses[$class] ) ) {
775 /** @var RSSFeed|AtomFeed $feed */
776 $feed = new $feedClasses[$class](
782 $res = $this->reallyDoQuery( $limit, 0 );
783 foreach ( $res as $obj ) {
784 $item = $this->feedResult( $obj );
786 $feed->outItem( $item );
798 * Override for custom handling. If the titles/links are ok, just do
801 * @return FeedItem|null
803 function feedResult( $row ) {
804 if ( !isset( $row->title
) ) {
807 $title = Title
::makeTitle( intval( $row->namespace ), $row->title
);
809 $date = isset( $row->timestamp
) ?
$row->timestamp
: '';
812 $talkpage = $title->getTalkPage();
813 $comments = $talkpage->getFullURL();
817 $title->getPrefixedText(),
818 $this->feedItemDesc( $row ),
819 $title->getFullURL(),
821 $this->feedItemAuthor( $row ),
828 function feedItemDesc( $row ) {
829 return isset( $row->comment
) ?
htmlspecialchars( $row->comment
) : '';
832 function feedItemAuthor( $row ) {
833 return isset( $row->user_text
) ?
$row->user_text
: '';
836 function feedTitle() {
837 $desc = $this->getDescription();
838 $code = $this->getConfig()->get( 'LanguageCode' );
839 $sitename = $this->getConfig()->get( 'Sitename' );
840 return "$sitename - $desc [$code]";
843 function feedDesc() {
844 return $this->msg( 'tagline' )->text();
848 return $this->getPageTitle()->getFullURL();
852 * Creates a new LinkBatch object, adds all pages from the passed ResultWrapper (MUST include
853 * title and optional the namespace field) and executes the batch. This operation will pre-cache
854 * LinkCache information like page existence and information for stub color and redirect hints.
856 * @param ResultWrapper $res The ResultWrapper object to process. Needs to include the title
857 * field and namespace field, if the $ns parameter isn't set.
858 * @param null $ns Use this namespace for the given titles in the ResultWrapper object,
859 * instead of the namespace value of $res.
861 protected function executeLBFromResultWrapper( ResultWrapper
$res, $ns = null ) {
862 if ( !$res->numRows() ) {
866 $batch = new LinkBatch
;
867 foreach ( $res as $row ) {
868 $batch->add( $ns !== null ?
$ns : $row->namespace, $row->title
);