118 private $loadBalancer =
null;
121 private $databaseProvider =
null;
124 private $linkBatchFactory =
null;
139 if ( $qp ===
null ) {
141 [ SpecialAncientPages::class,
'Ancientpages' ],
142 [ SpecialBrokenRedirects::class,
'BrokenRedirects' ],
143 [ SpecialDeadendPages::class,
'Deadendpages' ],
144 [ SpecialDoubleRedirects::class,
'DoubleRedirects' ],
145 [ SpecialListDuplicatedFiles::class,
'ListDuplicatedFiles' ],
146 [ SpecialLinkSearch::class,
'LinkSearch' ],
147 [ SpecialListRedirects::class,
'Listredirects' ],
148 [ SpecialLonelyPages::class,
'Lonelypages' ],
149 [ SpecialLongPages::class,
'Longpages' ],
150 [ SpecialMediaStatistics::class,
'MediaStatistics', SpecialMediaStatistics::MAX_LIMIT ],
151 [ SpecialMIMESearch::class,
'MIMEsearch' ],
152 [ SpecialMostCategories::class,
'Mostcategories' ],
153 [ SpecialMostImages::class,
'Mostimages' ],
154 [ SpecialMostInterwikis::class,
'Mostinterwikis' ],
155 [ SpecialMostLinkedCategories::class,
'Mostlinkedcategories' ],
156 [ SpecialMostLinkedTemplates::class,
'Mostlinkedtemplates' ],
157 [ SpecialMostLinked::class,
'Mostlinked' ],
158 [ SpecialMostRevisions::class,
'Mostrevisions' ],
159 [ SpecialFewestRevisions::class,
'Fewestrevisions' ],
160 [ SpecialShortPages::class,
'Shortpages' ],
161 [ SpecialUncategorizedCategories::class,
'Uncategorizedcategories' ],
162 [ SpecialUncategorizedPages::class,
'Uncategorizedpages' ],
163 [ SpecialUncategorizedImages::class,
'Uncategorizedimages' ],
164 [ SpecialUncategorizedTemplates::class,
'Uncategorizedtemplates' ],
165 [ SpecialUnusedCategories::class,
'Unusedcategories' ],
166 [ SpecialUnusedImages::class,
'Unusedimages' ],
167 [ SpecialWantedCategories::class,
'Wantedcategories' ],
168 [ SpecialWantedFiles::class,
'Wantedfiles' ],
169 [ SpecialWantedPages::class,
'Wantedpages' ],
170 [ SpecialWantedTemplates::class,
'Wantedtemplates' ],
171 [ SpecialUnwatchedPages::class,
'Unwatchedpages' ],
172 [ SpecialUnusedTemplates::class,
'Unusedtemplates' ],
173 [ SpecialWithoutInterwiki::class,
'Withoutinterwiki' ],
186 $this->linkBatchFactory = $linkBatchFactory;
194 if ( $this->linkBatchFactory === null ) {
199 return $this->linkBatchFactory;
210 if ( !is_array( $disableQueryPageUpdate ) ) {
215 foreach ( $disableQueryPageUpdate as $name => $runMode ) {
216 if ( is_int( $name ) ) {
218 $pages[$runMode] =
'disabled';
220 $pages[$name] = $runMode;
232 $this->listoutput = $bool;
275 throw new LogicException(
"Bug in a QueryPage: doesn't implement getQueryInfo() nor "
276 .
"getQuery() properly" );
402 $this->getOutput()->addWikiMsg(
'specialpage-empty' );
427 public function recache( $limit, $ignoreErrors =
true ) {
428 if ( !$this->isCacheable() ) {
432 $fname = static::class .
'::recache';
433 $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();
491 if ( !$ignoreErrors ) {
506 return $this->getDBLoadBalancer()
507 ->getConnectionRef( ILoadBalancer::DB_REPLICA,
'vslow' );
517 if ( $this->isCached() ) {
518 $dbw = $this->getDatabaseProvider()->getPrimaryDatabase();
519 $dbw->newDeleteQueryBuilder()
520 ->deleteFrom(
'querycache' )
522 'qc_type' => $this->getName(),
523 'qc_namespace' => $title->getNamespace(),
524 'qc_title' => $title->getDBkey(),
526 ->caller( __METHOD__ )->execute();
536 $fname = static::class .
'::' . __FUNCTION__;
537 $dbw = $this->getDatabaseProvider()->getPrimaryDatabase();
538 $dbw->newDeleteQueryBuilder()
539 ->deleteFrom(
'querycache' )
540 ->where( [
'qc_type' => $this->getName() ] )
541 ->caller( $fname )->execute();
542 $dbw->newDeleteQueryBuilder()
543 ->deleteFrom(
'querycachetwo' )
544 ->where( [
'qcc_type' => $this->getName() ] )
545 ->caller( $fname )->execute();
546 $dbw->newDeleteQueryBuilder()
547 ->deleteFrom(
'querycache_info' )
548 ->where( [
'qci_type' => $this->getName() ] )
549 ->caller( $fname )->execute();
561 $fname = static::class .
'::reallyDoQuery';
562 $dbr = $this->getRecacheDB();
563 $query = $this->getQueryInfo();
564 $order = $this->getOrderFields();
566 if ( $this->sortDescending() ) {
567 foreach ( $order as &$field ) {
572 if ( is_array( $query ) ) {
573 $tables = isset( $query[
'tables'] ) ? (array)$query[
'tables'] : [];
574 $fields = isset( $query[
'fields'] ) ? (array)$query[
'fields'] : [];
575 $conds = isset( $query[
'conds'] ) ? (array)$query[
'conds'] : [];
576 $options = isset( $query[
'options'] ) ? (array)$query[
'options'] : [];
577 $join_conds = isset( $query[
'join_conds'] ) ? (array)$query[
'join_conds'] : [];
580 $options[
'ORDER BY'] = $order;
583 if ( $limit !==
false ) {
584 $options[
'LIMIT'] = intval( $limit );
587 if ( $offset !==
false ) {
588 $options[
'OFFSET'] = intval( $offset );
591 $res = $dbr->select( $tables, $fields, $conds, $fname,
592 $options, $join_conds
596 MWDebug::detectDeprecatedOverride(
602 $sql = $this->getSQL();
603 $sql .=
' ORDER BY ' . implode(
', ', $order );
604 $sql = $dbr->limitResult( $sql, $limit, $offset );
606 $res = $dbr->query( $sql, $fname );
618 public function doQuery( $offset =
false, $limit =
false ) {
619 if ( $this->isCached() && $this->isCacheable() ) {
620 return $this->fetchFromCache( $limit, $offset );
622 return $this->reallyDoQuery( $limit, $offset );
636 $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
637 $queryBuilder = $dbr->newSelectQueryBuilder()
638 ->select( [
'qc_type',
'namespace' =>
'qc_namespace',
'title' =>
'qc_title',
'value' =>
'qc_value' ] )
639 ->from(
'querycache' )
640 ->where( [
'qc_type' => $this->getName() ] );
642 if ( $limit !==
false ) {
643 $queryBuilder->limit( intval( $limit ) );
646 if ( $offset !==
false ) {
647 $queryBuilder->offset( intval( $offset ) );
650 $order = $this->getCacheOrderFields();
651 if ( $this->sortDescending() ) {
652 $queryBuilder->orderBy( $order, SelectQueryBuilder::SORT_DESC );
654 $queryBuilder->orderBy( $order );
657 return $queryBuilder->caller( __METHOD__ )->fetchResultSet();
675 if ( $this->cachedTimestamp ===
null ) {
676 $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
677 $fname = static::class .
'::getCachedTimestamp';
678 $this->cachedTimestamp = $dbr->newSelectQueryBuilder()
679 ->select(
'qci_timestamp' )
680 ->from(
'querycache_info' )
681 ->where( [
'qci_type' => $this->getName() ] )
682 ->caller( $fname )->fetchField();
684 return $this->cachedTimestamp;
701 ->getLimitOffsetForUser( $this->
getUser() );
703 $maxResults = $this->getMaxResults();
705 $limit = min( $limit, $maxResults );
707 $offset = min( $offset, $maxResults + 1 );
709 return [ $limit, $offset ];
721 $maxResults = $this->getMaxResults();
723 $limit = min( $uiLimit + 1, $maxResults - $uiOffset );
724 return max( $limit, 0 );
752 $this->checkPermissions();
755 $this->outputHeader();
757 $out = $this->getOutput();
759 if ( $this->isCached() && !$this->isCacheable() ) {
760 $out->addWikiMsg(
'querypage-disabled' );
764 $out->setSyndicated( $this->isSyndicated() );
766 if ( $this->limit == 0 && $this->offset == 0 ) {
767 [ $this->limit, $this->offset ] = $this->getLimitOffset();
769 $dbLimit = $this->getDBLimit( $this->limit, $this->offset );
771 if ( !$this->isCached() ) {
773 $res = $this->reallyDoQuery( $dbLimit, $this->offset );
776 $res = $this->fetchFromCache( $dbLimit, $this->offset );
777 if ( !$this->listoutput ) {
779 $ts = $this->getCachedTimestamp();
780 $lang = $this->getLanguage();
781 $maxResults = $lang->formatNum( $this->getConfig()->
get(
786 $updated = $lang->userTimeAndDate( $ts, $user );
787 $updateddate = $lang->userDate( $ts, $user );
788 $updatedtime = $lang->userTime( $ts, $user );
789 $out->addMeta(
'Data-Cache-Time', $ts );
790 $out->addJsConfigVars(
'dataCacheTime', $ts );
791 $out->addWikiMsg(
'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
793 $out->addWikiMsg(
'perfcached', $maxResults );
798 $disabledQueryPages = self::getDisabledQueryPages( $this->getConfig() );
799 if ( isset( $disabledQueryPages[$this->getName()] ) ) {
800 $runMode = $disabledQueryPages[$this->getName()];
801 if ( $runMode ===
'disabled' ) {
803 "<div class=\"mw-querypage-no-updates\">\n$1\n</div>",
804 'querypage-no-updates'
809 "<div class=\"mw-querypage-updates-" . $runMode .
"\">\n$1\n</div>",
810 'querypage-updates-' . $runMode
817 $this->numRows = $res->numRows();
819 $dbr = $this->getRecacheDB();
820 $this->preprocessResults( $dbr, $res );
822 $out->addHTML( Xml::openElement(
'div', [
'class' =>
'mw-spcontent' ] ) );
825 if ( $this->shownavigation ) {
826 $out->addHTML( $this->getPageHeader() );
827 if ( $this->numRows > 0 ) {
828 $out->addHTML( $this->msg(
'showingresultsinrange' )->numParams(
829 min( $this->numRows, $this->limit ),
830 $this->offset + 1, ( min( $this->numRows, $this->limit ) + $this->offset ) )->parseAsBlock() );
833 && ( $this->offset + $this->limit >= $this->getMaxResults() );
834 $atEnd = ( $this->numRows <= $this->limit ) || $miserMaxResults;
835 $paging = $this->buildPrevNextNavigation( $this->offset,
836 $this->limit, $this->linkParameters(), $atEnd, $par );
837 $out->addHTML(
'<p>' . $paging .
'</p>' );
841 $this->showEmptyText();
842 $out->addHTML( Xml::closeElement(
'div' ) );
850 $this->outputResults( $out,
854 min( $this->numRows, $this->limit ),
858 if ( $this->shownavigation ) {
860 $out->addHTML(
'<p>' . $paging .
'</p>' );
863 $out->addHTML( Xml::closeElement(
'div' ) );
879 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
882 if ( !$this->listoutput ) {
883 $html[] = $this->openList( $offset );
888 for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++ ) {
889 $line = $this->formatResult( $skin, $row );
891 $html[] = $this->listoutput
893 :
"<li>{$line}</li>\n";
897 if ( !$this->listoutput ) {
898 $html[] = $this->closeList();
901 $html = $this->listoutput
902 ? $this->getContentLanguage()->listToText( $html )
903 : implode(
'', $html );
905 $out->addHTML( $html );
914 return "\n<ol start='" . ( $offset + 1 ) .
"' class='special'>\n";
950 $batch = $this->getLinkBatchFactory()->newLinkBatch();
951 foreach ( $res as $row ) {
952 $batch->add( $ns ?? (
int)$row->namespace, $row->title );
964 $this->loadBalancer = $loadBalancer;
972 if ( $this->loadBalancer === null ) {
977 return $this->loadBalancer;
985 $this->databaseProvider = $databaseProvider;
993 if ( $this->databaseProvider === null ) {
994 $this->databaseProvider = MediaWikiServices::getInstance()->getConnectionProvider();
996 return $this->databaseProvider;
1004class_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...
recache( $limit, $ignoreErrors=true)
Clear the cache and save new results.
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.
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.
A special page that list pages that contain no link to other pages.
A special page looking for articles with no article linking to them, thus being lonely.
SpecialShortpages extends QueryPage.
A special page looking for page without any category.
A special page that displays a list of pages that are not on anyone's watchlist.
A special page that lists most linked pages that does not exist.
The base class for all skins.
Module of static functions for generating XML.