Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / specialpage / QueryPage.php
blob1ff7e3fb98ea2470bf3de601c7e066e48e71f1e6
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
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;
37 /** @var int */
38 protected $limit = 0;
40 /**
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().
45 protected $numRows;
47 protected $cachedTimestamp = null;
49 /**
50 * Whether to show prev/next links
52 protected $shownavigation = true;
54 /**
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.
60 * @return array
62 public static function getPages() {
63 static $qp = null;
65 if ( $qp === null ) {
66 // QueryPage subclass, Special page name
67 $qp = array(
68 array( 'AncientPagesPage', 'Ancientpages' ),
69 array( 'BrokenRedirectsPage', 'BrokenRedirects' ),
70 array( 'DeadendPagesPage', 'Deadendpages' ),
71 array( 'DoubleRedirectsPage', 'DoubleRedirects' ),
72 array( 'FileDuplicateSearchPage', 'FileDuplicateSearch' ),
73 array( 'ListDuplicatedFilesPage', 'ListDuplicatedFiles'),
74 array( 'LinkSearchPage', 'LinkSearch' ),
75 array( 'ListredirectsPage', 'Listredirects' ),
76 array( 'LonelyPagesPage', 'Lonelypages' ),
77 array( 'LongPagesPage', 'Longpages' ),
78 array( 'MediaStatisticsPage', 'MediaStatistics' ),
79 array( 'MIMEsearchPage', 'MIMEsearch' ),
80 array( 'MostcategoriesPage', 'Mostcategories' ),
81 array( 'MostimagesPage', 'Mostimages' ),
82 array( 'MostinterwikisPage', 'Mostinterwikis' ),
83 array( 'MostlinkedCategoriesPage', 'Mostlinkedcategories' ),
84 array( 'MostlinkedTemplatesPage', 'Mostlinkedtemplates' ),
85 array( 'MostlinkedPage', 'Mostlinked' ),
86 array( 'MostrevisionsPage', 'Mostrevisions' ),
87 array( 'FewestrevisionsPage', 'Fewestrevisions' ),
88 array( 'ShortPagesPage', 'Shortpages' ),
89 array( 'UncategorizedCategoriesPage', 'Uncategorizedcategories' ),
90 array( 'UncategorizedPagesPage', 'Uncategorizedpages' ),
91 array( 'UncategorizedImagesPage', 'Uncategorizedimages' ),
92 array( 'UncategorizedTemplatesPage', 'Uncategorizedtemplates' ),
93 array( 'UnusedCategoriesPage', 'Unusedcategories' ),
94 array( 'UnusedimagesPage', 'Unusedimages' ),
95 array( 'WantedCategoriesPage', 'Wantedcategories' ),
96 array( 'WantedFilesPage', 'Wantedfiles' ),
97 array( 'WantedPagesPage', 'Wantedpages' ),
98 array( 'WantedTemplatesPage', 'Wantedtemplates' ),
99 array( 'UnwatchedpagesPage', 'Unwatchedpages' ),
100 array( 'UnusedtemplatesPage', 'Unusedtemplates' ),
101 array( 'WithoutInterwikiPage', 'Withoutinterwiki' ),
103 Hooks::run( 'wgQueryPages', array( &$qp ) );
106 return $qp;
110 * A mutator for $this->listoutput;
112 * @param bool $bool
114 function setListoutput( $bool ) {
115 $this->listoutput = $bool;
119 * Subclasses return an SQL query here, formatted as an array with the
120 * following keys:
121 * tables => Table(s) for passing to Database::select()
122 * fields => Field(s) for passing to Database::select(), may be *
123 * conds => WHERE conditions
124 * options => options
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.
141 * @return array
142 * @since 1.18
144 function getQueryInfo() {
145 return null;
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
152 * @return string
154 function getSQL() {
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.
164 * @return array
165 * @since 1.18
167 function getOrderFields() {
168 return array( 'value' );
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()
178 * @return bool
179 * @since 1.18
181 function usesTimestamps() {
182 return false;
186 * Override to sort by increasing values
188 * @return bool
190 function sortDescending() {
191 return true;
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.
199 * @return bool
201 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.
209 * @return bool
210 * @since 1.18
212 public function isCacheable() {
213 return true;
217 * Whether or not the output of the page in question is retrieved from
218 * the database cache.
220 * @return bool
222 function isCached() {
223 return $this->isExpensive() && $this->getConfig()->get( 'MiserMode' );
227 * Sometime we don't want to build rss / atom feeds.
229 * @return bool
231 function isSyndicated() {
232 return true;
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.
240 * @param Skin $skin
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
249 * @return string
251 function getPageHeader() {
252 return '';
256 * If using extra form wheely-dealies, return a set of parameters here
257 * as an associative array. They will be encoded and added to the paging
258 * links (prev/next/lengths).
260 * @return array
262 function linkParameters() {
263 return array();
267 * Some special pages (for example SpecialListusers) might not return the
268 * current object formatted, but return the previous one instead.
269 * Setting this to return true will ensure formatResult() is called
270 * one more time to make sure that the very last result is formatted
271 * as well.
272 * @return bool
274 function tryLastResult() {
275 return false;
279 * Clear the cache and save new results
281 * @param int|bool $limit Limit for SQL statement
282 * @param bool $ignoreErrors Whether to ignore database errors
283 * @throws DBError|Exception
284 * @return bool|int
286 function recache( $limit, $ignoreErrors = true ) {
287 if ( !$this->isCacheable() ) {
288 return 0;
291 $fname = get_class( $this ) . '::recache';
292 $dbw = wfGetDB( DB_MASTER );
293 if ( !$dbw ) {
294 return false;
297 try {
298 # Do query
299 $res = $this->reallyDoQuery( $limit, false );
300 $num = false;
301 if ( $res ) {
302 $num = $res->numRows();
303 # Fetch results
304 $vals = array();
305 foreach ( $res as $row ) {
306 if ( isset( $row->value ) ) {
307 if ( $this->usesTimestamps() ) {
308 $value = wfTimestamp( TS_UNIX,
309 $row->value );
310 } else {
311 $value = intval( $row->value ); // @bug 14414
313 } else {
314 $value = 0;
317 $vals[] = array( 'qc_type' => $this->getName(),
318 'qc_namespace' => $row->namespace,
319 'qc_title' => $row->title,
320 'qc_value' => $value );
323 $dbw->startAtomic( __METHOD__ );
324 # Clear out any old cached data
325 $dbw->delete( 'querycache', array( 'qc_type' => $this->getName() ), $fname );
326 # Save results into the querycache table on the master
327 if ( count( $vals ) ) {
328 $dbw->insert( 'querycache', $vals, __METHOD__ );
330 # Update the querycache_info record for the page
331 $dbw->delete( 'querycache_info', array( 'qci_type' => $this->getName() ), $fname );
332 $dbw->insert( 'querycache_info',
333 array( 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ),
334 $fname );
335 $dbw->endAtomic( __METHOD__ );
337 } catch ( DBError $e ) {
338 if ( !$ignoreErrors ) {
339 throw $e; // report query error
341 $num = false; // set result to false to indicate error
344 return $num;
348 * Get a DB connection to be used for slow recache queries
349 * @return IDatabase
351 function getRecacheDB() {
352 return wfGetDB( DB_SLAVE, array( $this->getName(), 'QueryPage::recache', 'vslow' ) );
356 * Run the query and return the result
357 * @param int|bool $limit Numerical limit or false for no limit
358 * @param int|bool $offset Numerical offset or false for no offset
359 * @return ResultWrapper
360 * @since 1.18
362 function reallyDoQuery( $limit, $offset = false ) {
363 $fname = get_class( $this ) . "::reallyDoQuery";
364 $dbr = $this->getRecacheDB();
365 $query = $this->getQueryInfo();
366 $order = $this->getOrderFields();
368 if ( $this->sortDescending() ) {
369 foreach ( $order as &$field ) {
370 $field .= ' DESC';
374 if ( is_array( $query ) ) {
375 $tables = isset( $query['tables'] ) ? (array)$query['tables'] : array();
376 $fields = isset( $query['fields'] ) ? (array)$query['fields'] : array();
377 $conds = isset( $query['conds'] ) ? (array)$query['conds'] : array();
378 $options = isset( $query['options'] ) ? (array)$query['options'] : array();
379 $join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : array();
381 if ( count( $order ) ) {
382 $options['ORDER BY'] = $order;
385 if ( $limit !== false ) {
386 $options['LIMIT'] = intval( $limit );
389 if ( $offset !== false ) {
390 $options['OFFSET'] = intval( $offset );
393 $res = $dbr->select( $tables, $fields, $conds, $fname,
394 $options, $join_conds
396 } else {
397 // Old-fashioned raw SQL style, deprecated
398 $sql = $this->getSQL();
399 $sql .= ' ORDER BY ' . implode( ', ', $order );
400 $sql = $dbr->limitResult( $sql, $limit, $offset );
401 $res = $dbr->query( $sql, $fname );
404 return $res;
408 * Somewhat deprecated, you probably want to be using execute()
409 * @param int|bool $offset
410 * @param int|bool $limit
411 * @return ResultWrapper
413 function doQuery( $offset = false, $limit = false ) {
414 if ( $this->isCached() && $this->isCacheable() ) {
415 return $this->fetchFromCache( $limit, $offset );
416 } else {
417 return $this->reallyDoQuery( $limit, $offset );
422 * Fetch the query results from the query cache
423 * @param int|bool $limit Numerical limit or false for no limit
424 * @param int|bool $offset Numerical offset or false for no offset
425 * @return ResultWrapper
426 * @since 1.18
428 function fetchFromCache( $limit, $offset = false ) {
429 $dbr = wfGetDB( DB_SLAVE );
430 $options = array();
431 if ( $limit !== false ) {
432 $options['LIMIT'] = intval( $limit );
434 if ( $offset !== false ) {
435 $options['OFFSET'] = intval( $offset );
437 if ( $this->sortDescending() ) {
438 $options['ORDER BY'] = 'qc_value DESC';
439 } else {
440 $options['ORDER BY'] = 'qc_value ASC';
442 $res = $dbr->select( 'querycache', array( 'qc_type',
443 'namespace' => 'qc_namespace',
444 'title' => 'qc_title',
445 'value' => 'qc_value' ),
446 array( 'qc_type' => $this->getName() ),
447 __METHOD__, $options
449 return $dbr->resultObject( $res );
452 public function getCachedTimestamp() {
453 if ( is_null( $this->cachedTimestamp ) ) {
454 $dbr = wfGetDB( DB_SLAVE );
455 $fname = get_class( $this ) . '::getCachedTimestamp';
456 $this->cachedTimestamp = $dbr->selectField( 'querycache_info', 'qci_timestamp',
457 array( 'qci_type' => $this->getName() ), $fname );
459 return $this->cachedTimestamp;
463 * This is the actual workhorse. It does everything needed to make a
464 * real, honest-to-gosh query page.
465 * @param string $par
467 function execute( $par ) {
468 $user = $this->getUser();
469 if ( !$this->userCanExecute( $user ) ) {
470 $this->displayRestrictionError();
471 return;
474 $this->setHeaders();
475 $this->outputHeader();
477 $out = $this->getOutput();
479 if ( $this->isCached() && !$this->isCacheable() ) {
480 $out->addWikiMsg( 'querypage-disabled' );
481 return;
484 $out->setSyndicated( $this->isSyndicated() );
486 if ( $this->limit == 0 && $this->offset == 0 ) {
487 list( $this->limit, $this->offset ) = $this->getRequest()->getLimitOffset();
490 // @todo Use doQuery()
491 if ( !$this->isCached() ) {
492 # select one extra row for navigation
493 $res = $this->reallyDoQuery( $this->limit + 1, $this->offset );
494 } else {
495 # Get the cached result, select one extra row for navigation
496 $res = $this->fetchFromCache( $this->limit + 1, $this->offset );
497 if ( !$this->listoutput ) {
499 # Fetch the timestamp of this update
500 $ts = $this->getCachedTimestamp();
501 $lang = $this->getLanguage();
502 $maxResults = $lang->formatNum( $this->getConfig()->get( 'QueryCacheLimit' ) );
504 if ( $ts ) {
505 $updated = $lang->userTimeAndDate( $ts, $user );
506 $updateddate = $lang->userDate( $ts, $user );
507 $updatedtime = $lang->userTime( $ts, $user );
508 $out->addMeta( 'Data-Cache-Time', $ts );
509 $out->addJsConfigVars( 'dataCacheTime', $ts );
510 $out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
511 } else {
512 $out->addWikiMsg( 'perfcached', $maxResults );
515 # If updates on this page have been disabled, let the user know
516 # that the data set won't be refreshed for now
517 if ( is_array( $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
518 && in_array( $this->getName(), $this->getConfig()->get( 'DisableQueryPageUpdate' ) )
520 $out->wrapWikiMsg(
521 "<div class=\"mw-querypage-no-updates\">\n$1\n</div>",
522 'querypage-no-updates'
528 $this->numRows = $res->numRows();
530 $dbr = wfGetDB( DB_SLAVE );
531 $this->preprocessResults( $dbr, $res );
533 $out->addHTML( Xml::openElement( 'div', array( 'class' => 'mw-spcontent' ) ) );
535 # Top header and navigation
536 if ( $this->shownavigation ) {
537 $out->addHTML( $this->getPageHeader() );
538 if ( $this->numRows > 0 ) {
539 $out->addHTML( $this->msg( 'showingresultsinrange' )->numParams(
540 min( $this->numRows, $this->limit ), # do not show the one extra row, if exist
541 $this->offset + 1, ( min( $this->numRows, $this->limit ) + $this->offset ) )->parseAsBlock() );
542 # Disable the "next" link when we reach the end
543 $paging = $this->getLanguage()->viewPrevNext( $this->getPageTitle( $par ), $this->offset,
544 $this->limit, $this->linkParameters(), ( $this->numRows <= $this->limit ) );
545 $out->addHTML( '<p>' . $paging . '</p>' );
546 } else {
547 # No results to show, so don't bother with "showing X of Y" etc.
548 # -- just let the user know and give up now
549 $out->addWikiMsg( 'specialpage-empty' );
550 $out->addHTML( Xml::closeElement( 'div' ) );
551 return;
555 # The actual results; specialist subclasses will want to handle this
556 # with more than a straight list, so we hand them the info, plus
557 # an OutputPage, and let them get on with it
558 $this->outputResults( $out,
559 $this->getSkin(),
560 $dbr, # Should use a ResultWrapper for this
561 $res,
562 min( $this->numRows, $this->limit ), # do not format the one extra row, if exist
563 $this->offset );
565 # Repeat the paging links at the bottom
566 if ( $this->shownavigation ) {
567 $out->addHTML( '<p>' . $paging . '</p>' );
570 $out->addHTML( Xml::closeElement( 'div' ) );
574 * Format and output report results using the given information plus
575 * OutputPage
577 * @param OutputPage $out OutputPage to print to
578 * @param Skin $skin User skin to use
579 * @param IDatabase $dbr Database (read) connection to use
580 * @param ResultWrapper $res Result pointer
581 * @param int $num Number of available result rows
582 * @param int $offset Paging offset
584 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
585 global $wgContLang;
587 if ( $num > 0 ) {
588 $html = array();
589 if ( !$this->listoutput ) {
590 $html[] = $this->openList( $offset );
593 # $res might contain the whole 1,000 rows, so we read up to
594 # $num [should update this to use a Pager]
595 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
596 for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++ ) {
597 // @codingStandardsIgnoreEnd
598 $line = $this->formatResult( $skin, $row );
599 if ( $line ) {
600 $attr = ( isset( $row->usepatrol ) && $row->usepatrol && $row->patrolled == 0 )
601 ? ' class="not-patrolled"'
602 : '';
603 $html[] = $this->listoutput
604 ? $line
605 : "<li{$attr}>{$line}</li>\n";
609 # Flush the final result
610 if ( $this->tryLastResult() ) {
611 $row = null;
612 $line = $this->formatResult( $skin, $row );
613 if ( $line ) {
614 $attr = ( isset( $row->usepatrol ) && $row->usepatrol && $row->patrolled == 0 )
615 ? ' class="not-patrolled"'
616 : '';
617 $html[] = $this->listoutput
618 ? $line
619 : "<li{$attr}>{$line}</li>\n";
623 if ( !$this->listoutput ) {
624 $html[] = $this->closeList();
627 $html = $this->listoutput
628 ? $wgContLang->listToText( $html )
629 : implode( '', $html );
631 $out->addHTML( $html );
636 * @param int $offset
637 * @return string
639 function openList( $offset ) {
640 return "\n<ol start='" . ( $offset + 1 ) . "' class='special'>\n";
644 * @return string
646 function closeList() {
647 return "</ol>\n";
651 * Do any necessary preprocessing of the result object.
652 * @param IDatabase $db
653 * @param ResultWrapper $res
655 function preprocessResults( $db, $res ) {
659 * Similar to above, but packaging in a syndicated feed instead of a web page
660 * @param string $class
661 * @param int $limit
662 * @return bool
664 function doFeed( $class = '', $limit = 50 ) {
665 if ( !$this->getConfig()->get( 'Feed' ) ) {
666 $this->getOutput()->addWikiMsg( 'feed-unavailable' );
667 return false;
670 $limit = min( $limit, $this->getConfig()->get( 'FeedLimit' ) );
672 $feedClasses = $this->getConfig()->get( 'FeedClasses' );
673 if ( isset( $feedClasses[$class] ) ) {
674 /** @var RSSFeed|AtomFeed $feed */
675 $feed = new $feedClasses[$class](
676 $this->feedTitle(),
677 $this->feedDesc(),
678 $this->feedUrl() );
679 $feed->outHeader();
681 $res = $this->reallyDoQuery( $limit, 0 );
682 foreach ( $res as $obj ) {
683 $item = $this->feedResult( $obj );
684 if ( $item ) {
685 $feed->outItem( $item );
689 $feed->outFooter();
690 return true;
691 } else {
692 return false;
697 * Override for custom handling. If the titles/links are ok, just do
698 * feedItemDesc()
699 * @param object $row
700 * @return FeedItem|null
702 function feedResult( $row ) {
703 if ( !isset( $row->title ) ) {
704 return null;
706 $title = Title::makeTitle( intval( $row->namespace ), $row->title );
707 if ( $title ) {
708 $date = isset( $row->timestamp ) ? $row->timestamp : '';
709 $comments = '';
710 if ( $title ) {
711 $talkpage = $title->getTalkPage();
712 $comments = $talkpage->getFullURL();
715 return new FeedItem(
716 $title->getPrefixedText(),
717 $this->feedItemDesc( $row ),
718 $title->getFullURL(),
719 $date,
720 $this->feedItemAuthor( $row ),
721 $comments );
722 } else {
723 return null;
727 function feedItemDesc( $row ) {
728 return isset( $row->comment ) ? htmlspecialchars( $row->comment ) : '';
731 function feedItemAuthor( $row ) {
732 return isset( $row->user_text ) ? $row->user_text : '';
735 function feedTitle() {
736 $desc = $this->getDescription();
737 $code = $this->getConfig()->get( 'LanguageCode' );
738 $sitename = $this->getConfig()->get( 'Sitename' );
739 return "$sitename - $desc [$code]";
742 function feedDesc() {
743 return $this->msg( 'tagline' )->text();
746 function feedUrl() {
747 return $this->getPageTitle()->getFullURL();