119 private $loadBalancer =
null;
122 private $databaseProvider =
null;
125 private $linkBatchFactory =
null;
140 if ( $qp ===
null ) {
142 [ SpecialAncientPages::class,
'Ancientpages' ],
143 [ SpecialBrokenRedirects::class,
'BrokenRedirects' ],
144 [ SpecialDeadendPages::class,
'Deadendpages' ],
145 [ SpecialDoubleRedirects::class,
'DoubleRedirects' ],
146 [ SpecialListDuplicatedFiles::class,
'ListDuplicatedFiles' ],
147 [ SpecialLinkSearch::class,
'LinkSearch' ],
148 [ SpecialListRedirects::class,
'Listredirects' ],
149 [ SpecialLonelyPages::class,
'Lonelypages' ],
150 [ SpecialLongPages::class,
'Longpages' ],
151 [ SpecialMediaStatistics::class,
'MediaStatistics', SpecialMediaStatistics::MAX_LIMIT ],
152 [ SpecialMIMESearch::class,
'MIMEsearch' ],
153 [ SpecialMostCategories::class,
'Mostcategories' ],
154 [ SpecialMostImages::class,
'Mostimages' ],
155 [ SpecialMostInterwikis::class,
'Mostinterwikis' ],
156 [ SpecialMostLinkedCategories::class,
'Mostlinkedcategories' ],
157 [ SpecialMostLinkedTemplates::class,
'Mostlinkedtemplates' ],
158 [ SpecialMostLinked::class,
'Mostlinked' ],
159 [ SpecialMostRevisions::class,
'Mostrevisions' ],
160 [ SpecialFewestRevisions::class,
'Fewestrevisions' ],
161 [ SpecialShortPages::class,
'Shortpages' ],
162 [ SpecialUncategorizedCategories::class,
'Uncategorizedcategories' ],
163 [ SpecialUncategorizedPages::class,
'Uncategorizedpages' ],
164 [ SpecialUncategorizedImages::class,
'Uncategorizedimages' ],
165 [ SpecialUncategorizedTemplates::class,
'Uncategorizedtemplates' ],
166 [ SpecialUnusedCategories::class,
'Unusedcategories' ],
167 [ SpecialUnusedImages::class,
'Unusedimages' ],
168 [ SpecialWantedCategories::class,
'Wantedcategories' ],
169 [ SpecialWantedFiles::class,
'Wantedfiles' ],
170 [ SpecialWantedPages::class,
'Wantedpages' ],
171 [ SpecialWantedTemplates::class,
'Wantedtemplates' ],
172 [ SpecialUnwatchedPages::class,
'Unwatchedpages' ],
173 [ SpecialUnusedTemplates::class,
'Unusedtemplates' ],
174 [ SpecialWithoutInterwiki::class,
'Withoutinterwiki' ],
187 $this->linkBatchFactory = $linkBatchFactory;
195 if ( $this->linkBatchFactory === null ) {
200 return $this->linkBatchFactory;
211 if ( !is_array( $disableQueryPageUpdate ) ) {
216 foreach ( $disableQueryPageUpdate as $name => $runMode ) {
217 if ( is_int( $name ) ) {
219 $pages[$runMode] =
'disabled';
221 $pages[$name] = $runMode;
233 $this->listoutput = $bool;
276 throw new LogicException(
"Bug in a QueryPage: doesn't implement getQueryInfo() nor "
277 .
"getQuery() properly" );
403 $this->getOutput()->addWikiMsg(
'specialpage-empty' );
428 public function recache( $limit, $unused =
true ) {
429 if ( !$this->isCacheable() ) {
433 $fname = static::class .
'::recache';
434 $dbw = $this->getDatabaseProvider()->getPrimaryDatabase();
437 $res = $this->reallyDoQuery( $limit,
false );
440 $num = $res->numRows();
443 foreach ( $res as $i => $row ) {
444 if ( isset( $row->value ) ) {
445 if ( $this->usesTimestamps() ) {
448 $value = intval( $row->value );
455 'qc_type' => $this->getName(),
456 'qc_namespace' => $row->namespace,
457 'qc_title' => $row->title,
462 $dbw->doAtomicSection(
464 function (
IDatabase $dbw, $fname ) use ( $vals ) {
467 ->deleteFrom(
'querycache' )
468 ->where( [
'qc_type' => $this->getName() ] )
469 ->caller( $fname )->execute();
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();
481 if ( count( $vals ) ) {
482 foreach ( array_chunk( $vals, 500 ) as $chunk ) {
484 ->insertInto(
'querycache' )
486 ->caller( $fname )->execute();
500 return $this->getDBLoadBalancer()
501 ->getConnection( ILoadBalancer::DB_REPLICA,
'vslow' );
511 if ( $this->isCached() ) {
512 $dbw = $this->getDatabaseProvider()->getPrimaryDatabase();
513 $dbw->newDeleteQueryBuilder()
514 ->deleteFrom(
'querycache' )
516 'qc_type' => $this->getName(),
517 'qc_namespace' => $title->getNamespace(),
518 'qc_title' => $title->getDBkey(),
520 ->caller( __METHOD__ )->execute();
530 $fname = static::class .
'::' . __FUNCTION__;
531 $dbw = $this->getDatabaseProvider()->getPrimaryDatabase();
532 $dbw->newDeleteQueryBuilder()
533 ->deleteFrom(
'querycache' )
534 ->where( [
'qc_type' => $this->getName() ] )
535 ->caller( $fname )->execute();
536 $dbw->newDeleteQueryBuilder()
537 ->deleteFrom(
'querycachetwo' )
538 ->where( [
'qcc_type' => $this->getName() ] )
539 ->caller( $fname )->execute();
540 $dbw->newDeleteQueryBuilder()
541 ->deleteFrom(
'querycache_info' )
542 ->where( [
'qci_type' => $this->getName() ] )
543 ->caller( $fname )->execute();
555 $fname = static::class .
'::reallyDoQuery';
556 $dbr = $this->getRecacheDB();
557 $query = $this->getQueryInfo();
558 $order = $this->getOrderFields();
560 if ( $this->sortDescending() ) {
561 foreach ( $order as &$field ) {
566 if ( is_array( $query ) ) {
567 $tables = isset( $query[
'tables'] ) ? (array)$query[
'tables'] : [];
568 $fields = isset( $query[
'fields'] ) ? (array)$query[
'fields'] : [];
569 $conds = isset( $query[
'conds'] ) ? (array)$query[
'conds'] : [];
570 $options = isset( $query[
'options'] ) ? (array)$query[
'options'] : [];
571 $join_conds = isset( $query[
'join_conds'] ) ? (array)$query[
'join_conds'] : [];
573 $queryBuilder = $dbr->newSelectQueryBuilder()
578 ->options( $options )
579 ->joinConds( $join_conds );
581 $queryBuilder->orderBy( $order );
584 if ( $limit !==
false ) {
585 $queryBuilder->limit( intval( $limit ) );
588 if ( $offset !==
false ) {
589 $queryBuilder->offset( intval( $offset ) );
592 $res = $queryBuilder->fetchResultSet();
595 MWDebug::detectDeprecatedOverride(
601 $sql = $this->getSQL();
602 $sql .=
' ORDER BY ' . implode(
', ', $order );
603 $sql = $dbr->limitResult( $sql, $limit, $offset );
605 $res = $dbr->query( $sql, $fname );
617 public function doQuery( $offset =
false, $limit =
false ) {
618 if ( $this->isCached() && $this->isCacheable() ) {
619 return $this->fetchFromCache( $limit, $offset );
621 return $this->reallyDoQuery( $limit, $offset );
635 $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
636 $queryBuilder = $dbr->newSelectQueryBuilder()
637 ->select( [
'qc_type',
'namespace' =>
'qc_namespace',
'title' =>
'qc_title',
'value' =>
'qc_value' ] )
638 ->from(
'querycache' )
639 ->where( [
'qc_type' => $this->getName() ] );
641 if ( $limit !==
false ) {
642 $queryBuilder->limit( intval( $limit ) );
645 if ( $offset !==
false ) {
646 $queryBuilder->offset( intval( $offset ) );
649 $order = $this->getCacheOrderFields();
650 if ( $this->sortDescending() ) {
651 $queryBuilder->orderBy( $order, SelectQueryBuilder::SORT_DESC );
653 $queryBuilder->orderBy( $order );
656 return $queryBuilder->caller( __METHOD__ )->fetchResultSet();
674 if ( $this->cachedTimestamp ===
null ) {
675 $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
676 $fname = static::class .
'::getCachedTimestamp';
677 $this->cachedTimestamp = $dbr->newSelectQueryBuilder()
678 ->select(
'qci_timestamp' )
679 ->from(
'querycache_info' )
680 ->where( [
'qci_type' => $this->getName() ] )
681 ->caller( $fname )->fetchField();
683 return $this->cachedTimestamp;
700 ->getLimitOffsetForUser( $this->
getUser() );
702 $maxResults = $this->getMaxResults();
704 $limit = min( $limit, $maxResults );
706 $offset = min( $offset, $maxResults + 1 );
708 return [ $limit, $offset ];
720 $maxResults = $this->getMaxResults();
722 $limit = min( $uiLimit + 1, $maxResults - $uiOffset );
723 return max( $limit, 0 );
751 $this->checkPermissions();
754 $this->outputHeader();
756 $out = $this->getOutput();
758 if ( $this->isCached() && !$this->isCacheable() ) {
759 $out->addWikiMsg(
'querypage-disabled' );
763 $out->setSyndicated( $this->isSyndicated() );
765 if ( $this->limit == 0 && $this->offset == 0 ) {
766 [ $this->limit, $this->offset ] = $this->getLimitOffset();
768 $dbLimit = $this->getDBLimit( $this->limit, $this->offset );
770 if ( !$this->isCached() ) {
772 $res = $this->reallyDoQuery( $dbLimit, $this->offset );
775 $res = $this->fetchFromCache( $dbLimit, $this->offset );
776 if ( !$this->listoutput ) {
778 $ts = $this->getCachedTimestamp();
779 $lang = $this->getLanguage();
780 $maxResults = $lang->formatNum( $this->getConfig()->
get(
785 $updated = $lang->userTimeAndDate( $ts, $user );
786 $updateddate = $lang->userDate( $ts, $user );
787 $updatedtime = $lang->userTime( $ts, $user );
788 $out->addMeta(
'Data-Cache-Time', $ts );
789 $out->addJsConfigVars(
'dataCacheTime', $ts );
790 $out->addWikiMsg(
'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
792 $out->addWikiMsg(
'perfcached', $maxResults );
797 $disabledQueryPages = self::getDisabledQueryPages( $this->getConfig() );
798 if ( isset( $disabledQueryPages[$this->getName()] ) ) {
799 $runMode = $disabledQueryPages[$this->getName()];
800 if ( $runMode ===
'disabled' ) {
802 "<div class=\"mw-querypage-no-updates\">\n$1\n</div>",
803 'querypage-no-updates'
808 "<div class=\"mw-querypage-updates-" . $runMode .
"\">\n$1\n</div>",
809 'querypage-updates-' . $runMode
816 $this->numRows = $res->numRows();
818 $dbr = $this->getRecacheDB();
819 $this->preprocessResults( $dbr, $res );
824 if ( $this->shownavigation ) {
825 $out->addHTML( $this->getPageHeader() );
826 if ( $this->numRows > 0 ) {
827 $out->addHTML( $this->msg(
'showingresultsinrange' )->numParams(
828 min( $this->numRows, $this->limit ),
829 $this->offset + 1, ( min( $this->numRows, $this->limit ) + $this->offset ) )->parseAsBlock() );
832 && ( $this->offset + $this->limit >= $this->getMaxResults() );
833 $atEnd = ( $this->numRows <= $this->limit ) || $miserMaxResults;
834 $paging = $this->buildPrevNextNavigation( $this->offset,
835 $this->limit, $this->linkParameters(), $atEnd, $par );
836 $out->addHTML(
'<p>' . $paging .
'</p>' );
840 $this->showEmptyText();
849 $this->outputResults( $out,
853 min( $this->numRows, $this->limit ),
857 if ( $this->shownavigation ) {
859 $out->addHTML(
'<p>' . $paging .
'</p>' );
878 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
881 if ( !$this->listoutput ) {
882 $html[] = $this->openList( $offset );
887 for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++ ) {
888 $line = $this->formatResult( $skin, $row );
890 $html[] = $this->listoutput
892 :
"<li>{$line}</li>\n";
896 if ( !$this->listoutput ) {
897 $html[] = $this->closeList();
900 $html = $this->listoutput
901 ? $this->getContentLanguage()->listToText( $html )
902 : implode(
'', $html );
904 $out->addHTML( $html );
913 return "\n<ol start='" . ( $offset + 1 ) .
"' class='special'>\n";
949 $batch = $this->getLinkBatchFactory()->newLinkBatch();
950 foreach ( $res as $row ) {
951 $batch->add( $ns ?? (
int)$row->namespace, $row->title );
964 $this->loadBalancer = $loadBalancer;
973 if ( $this->loadBalancer === null ) {
978 return $this->loadBalancer;
986 $this->databaseProvider = $databaseProvider;
994 if ( $this->databaseProvider === null ) {
995 $this->databaseProvider = MediaWikiServices::getInstance()->getConnectionProvider();
997 return $this->databaseProvider;
1002class_alias( QueryPage::class,
'QueryPage' );
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
const DisableQueryPages
Name constant for the DisableQueryPages setting, for use with Config::get()
const QueryCacheLimit
Name constant for the QueryCacheLimit setting, for use with Config::get()
const MiserMode
Name constant for the MiserMode setting, for use with Config::get()
const DisableQueryPageUpdate
Name constant for the DisableQueryPageUpdate setting, for use with Config::get()
This is one of the Core classes and should be read at least once by any new developers.
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
getPageHeader()
The content returned by this function will be output before any result.
linkParameters()
If using extra form wheely-dealies, return a set of parameters here as an associative array.
bool $shownavigation
Whether to show prev/next links.
bool $listoutput
Whether or not we want plain listoutput rather than an ordered list.
getOrderFields()
Subclasses return an array of fields to order by here.
preprocessResults( $db, $res)
Do any necessary preprocessing of the result object.
setDatabaseProvider(IConnectionProvider $databaseProvider)
getLimitOffset()
Returns limit and offset, as returned by $this->getRequest()->getLimitOffsetForUser().
int $offset
The offset and limit in use, as passed to the query() function.
getDBLimit( $uiLimit, $uiOffset)
What is limit to fetch from DB.
isCached()
Whether or not the output of the page in question is retrieved from the database cache.
execute( $par)
This is the actual workhorse.
setListoutput( $bool)
A mutator for $this->listoutput;.
executeLBFromResultWrapper(IResultWrapper $res, $ns=null)
Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include title and...
getMaxResults()
Get max number of results we can return in miser mode.
isExpensive()
Should this query page only be updated offline on large wikis?
static getPages()
Get a list of query page classes and their associated special pages, for periodic updates.
fetchFromCache( $limit, $offset=false)
Fetch the query results from the query cache.
setDBLoadBalancer(ILoadBalancer $loadBalancer)
string null false $cachedTimestamp
deleteAllCachedData()
Remove all cached value This is needed when the page is no longer using the cache.
showEmptyText()
Outputs some kind of an informative message (via OutputPage) to let the user know that the query retu...
doQuery( $offset=false, $limit=false)
Somewhat deprecated, you probably want to be using execute()
outputResults( $out, $skin, $dbr, $res, $num, $offset)
Format and output report results using the given information plus OutputPage.
getRecacheDB()
Get a DB connection to be used for slow recache queries.
static getDisabledQueryPages(Config $config)
Get a list of disabled query pages and their run mode.
sortDescending()
Override to sort by increasing values.
int $numRows
The number of rows returned by the query.
recache( $limit, $unused=true)
Clear the cache and save new results.
usesTimestamps()
Does this query return timestamps rather than integers in its 'value' field? If true,...
isSyndicated()
Sometimes we don't want to build rss / atom feeds.
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
formatResult( $skin, $result)
Formats the results of the query for display.
getCacheOrderFields()
Return the order fields for fetchFromCache.
isCacheable()
Is the output of this query cacheable? Non-cacheable expensive pages will be disabled in miser mode a...
getSQL()
For back-compat, subclasses may return a raw SQL query here, as a string.
reallyDoQuery( $limit, $offset=false)
Run the query and return the result.
Parent class for all special pages.
Implements Special:Ancientpages.
List of pages that contain no links to other pages.
List of articles with no article linking to them, thus being lonely.
Implements Special:Longpages.
List of the shortest pages in the database.
List of pages without any category.
List of pages that are not on anyone's watchlist.
List of the most-linked pages that do not exist.
The base class for all skins.