66use Wikimedia\Timestamp\TimestampFormat as TS;
107 private $loadBalancer =
null;
110 private $databaseProvider =
null;
113 private $linkBatchFactory =
null;
116 private $httpRequestFactory =
null;
131 if ( $qp ===
null ) {
133 [ SpecialAncientPages::class,
'Ancientpages' ],
134 [ SpecialBrokenRedirects::class,
'BrokenRedirects' ],
135 [ SpecialDeadendPages::class,
'Deadendpages' ],
136 [ SpecialDoubleRedirects::class,
'DoubleRedirects' ],
137 [ SpecialListDuplicatedFiles::class,
'ListDuplicatedFiles' ],
138 [ SpecialLinkSearch::class,
'LinkSearch' ],
139 [ SpecialListRedirects::class,
'Listredirects' ],
140 [ SpecialLonelyPages::class,
'Lonelypages' ],
141 [ SpecialLongPages::class,
'Longpages' ],
142 [ SpecialMediaStatistics::class,
'MediaStatistics', SpecialMediaStatistics::MAX_LIMIT ],
143 [ SpecialMIMESearch::class,
'MIMEsearch' ],
144 [ SpecialMostCategories::class,
'Mostcategories' ],
145 [ SpecialMostImages::class,
'Mostimages' ],
146 [ SpecialMostInterwikis::class,
'Mostinterwikis' ],
147 [ SpecialMostLinkedCategories::class,
'Mostlinkedcategories' ],
148 [ SpecialMostLinkedTemplates::class,
'Mostlinkedtemplates' ],
149 [ SpecialMostLinked::class,
'Mostlinked' ],
150 [ SpecialMostRevisions::class,
'Mostrevisions' ],
151 [ SpecialFewestRevisions::class,
'Fewestrevisions' ],
152 [ SpecialShortPages::class,
'Shortpages' ],
153 [ SpecialUncategorizedCategories::class,
'Uncategorizedcategories' ],
154 [ SpecialUncategorizedPages::class,
'Uncategorizedpages' ],
155 [ SpecialUncategorizedImages::class,
'Uncategorizedimages' ],
156 [ SpecialUncategorizedTemplates::class,
'Uncategorizedtemplates' ],
157 [ SpecialUnusedCategories::class,
'Unusedcategories' ],
158 [ SpecialUnusedImages::class,
'Unusedimages' ],
159 [ SpecialWantedCategories::class,
'Wantedcategories' ],
160 [ SpecialWantedFiles::class,
'Wantedfiles' ],
161 [ SpecialWantedPages::class,
'Wantedpages' ],
162 [ SpecialWantedTemplates::class,
'Wantedtemplates' ],
163 [ SpecialUnwatchedPages::class,
'Unwatchedpages' ],
164 [ SpecialUnusedTemplates::class,
'Unusedtemplates' ],
165 [ SpecialWithoutInterwiki::class,
'Withoutinterwiki' ],
178 $this->linkBatchFactory = $linkBatchFactory;
186 if ( $this->linkBatchFactory === null ) {
191 return $this->linkBatchFactory;
198 if ( $this->httpRequestFactory === null ) {
203 return $this->httpRequestFactory;
212 $disableQueryPageUpdate = $config->
get( MainConfigNames::DisableQueryPageUpdate );
214 if ( !is_array( $disableQueryPageUpdate ) ) {
219 foreach ( $disableQueryPageUpdate as $name => $runMode ) {
220 if ( is_int( $name ) ) {
222 $pages[$runMode] =
'disabled';
224 $pages[$name] = $runMode;
236 $this->listoutput = $bool;
322 return $this->getConfig()->get( MainConfigNames::DisableQueryPages );
345 return $this->isExpensive() && $this->getConfig()->get( MainConfigNames::MiserMode );
366 $config = $this->getConfig();
367 $externalSources = $config->get( MainConfigNames::ExternalQuerySources );
368 $pageName = $this->getName();
370 return !empty( $externalSources[$pageName][
'enabled'] ) &&
371 !empty( $externalSources[$pageName][
'url'] );
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() ) {
446 $value = (int)
wfTimestamp( TS::UNIX, $row->value );
448 $value = intval( $row->value );
455 'qc_type' => $this->getName(),
456 'qc_namespace' => $row->namespace,
457 'qc_title' => $row->title,
462 $dbw->doAtomicSection(
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->getDatabaseProvider()->getReplicaDatabase(
false,
'vslow' );
510 if ( $this->isCached() ) {
511 $dbw = $this->getDatabaseProvider()->getPrimaryDatabase();
512 $dbw->newDeleteQueryBuilder()
513 ->deleteFrom(
'querycache' )
515 'qc_type' => $this->getName(),
516 'qc_namespace' => $title->getNamespace(),
517 'qc_title' => $title->getDBkey(),
519 ->caller( __METHOD__ )->execute();
529 $fname = static::class .
'::' . __FUNCTION__;
530 $dbw = $this->getDatabaseProvider()->getPrimaryDatabase();
531 $dbw->newDeleteQueryBuilder()
532 ->deleteFrom(
'querycache' )
533 ->where( [
'qc_type' => $this->getName() ] )
534 ->caller( $fname )->execute();
535 $dbw->newDeleteQueryBuilder()
536 ->deleteFrom(
'querycachetwo' )
537 ->where( [
'qcc_type' => $this->getName() ] )
538 ->caller( $fname )->execute();
539 $dbw->newDeleteQueryBuilder()
540 ->deleteFrom(
'querycache_info' )
541 ->where( [
'qci_type' => $this->getName() ] )
542 ->caller( $fname )->execute();
554 if ( $this->usesExternalSource() ) {
555 return $this->reallyDoQueryExternal();
558 return $this->reallyDoQueryInternal( $limit, $offset );
568 private function reallyDoQueryInternal( $limit, $offset =
false ) {
569 $fname = static::class .
'::reallyDoQueryInternal';
570 $dbr = $this->getRecacheDB();
571 $query = $this->getQueryInfo();
572 $order = $this->getOrderFields();
574 if ( $this->sortDescending() ) {
575 foreach ( $order as &$field ) {
580 $tables = isset( $query[
'tables'] ) ? (array)$query[
'tables'] : [];
581 $fields = isset( $query[
'fields'] ) ? (array)$query[
'fields'] : [];
582 $conds = isset( $query[
'conds'] ) ? (array)$query[
'conds'] : [];
583 $options = isset( $query[
'options'] ) ? (array)$query[
'options'] : [];
584 $join_conds = isset( $query[
'join_conds'] ) ? (array)$query[
'join_conds'] : [];
586 $queryBuilder = $dbr->newSelectQueryBuilder()
591 ->options( $options )
592 ->joinConds( $join_conds );
594 $queryBuilder->orderBy( $order );
597 if ( $limit !==
false ) {
598 $queryBuilder->limit( intval( $limit ) );
601 if ( $offset !==
false ) {
602 $queryBuilder->offset( intval( $offset ) );
605 return $queryBuilder->fetchResultSet();
613 private function reallyDoQueryExternal() {
614 $fname = static::class .
'::reallyDoQueryExternal';
615 $httpRequestFactory = $this->getHttpRequestFactory();
616 $externalSources = $this->
getConfig()->get( MainConfigNames::ExternalQuerySources );
618 $config = $externalSources[$pageName];
621 if ( isset( $config[
'timeout'] ) ) {
622 $options[
'timeout'] = (int)$config[
'timeout'];
625 $request = $httpRequestFactory->create( $config[
'url'], $options, $fname );
628 if ( !$status->isOK() ) {
629 throw new RuntimeException(
"Failed to fetch data from external source '{$pageName}': " .
630 $status->getMessage()->text() );
634 if ( $content ===
null || $content ===
'' ) {
635 throw new RuntimeException(
"Empty response received from external source '{$pageName}'" );
638 $decoded = json_decode( $content,
true );
639 if ( $decoded ===
null ) {
640 throw new RuntimeException(
"Invalid JSON response from external source '{$pageName}': " .
641 json_last_error_msg() );
644 if ( !is_array( $decoded ) ) {
645 throw new RuntimeException(
"Expected array data from external source '{$pageName}', got " .
646 gettype( $decoded ) );
650 foreach ( $decoded as $i => $row ) {
651 if ( !is_array( $row ) ) {
652 throw new RuntimeException(
"Invalid row data at index {$i} from external source '{$pageName}': " .
653 'expected array, got ' . gettype( $row ) );
656 $requiredFields = [
'qc_namespace',
'qc_title',
'qc_value' ];
657 foreach ( $requiredFields as $field ) {
658 if ( !array_key_exists( $field, $row ) ) {
659 throw new RuntimeException(
660 "Missing required field '{$field}' in row {$i} from external source '{$pageName}'" );
664 $result[] = (object)[
665 'namespace' => $row[
'qc_namespace'],
666 'title' => $row[
'qc_title'],
667 'value' => $row[
'qc_value']
671 return new FakeResultWrapper( $result );
680 public function doQuery( $offset =
false, $limit =
false ) {
681 if ( $this->isCached() && $this->isCacheable() ) {
682 return $this->fetchFromCache( $limit, $offset );
684 return $this->reallyDoQuery( $limit, $offset );
698 $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
699 $queryBuilder = $dbr->newSelectQueryBuilder()
700 ->select( [
'qc_type',
'namespace' =>
'qc_namespace',
'title' =>
'qc_title',
'value' =>
'qc_value' ] )
701 ->from(
'querycache' )
702 ->where( [
'qc_type' => $this->getName() ] );
704 if ( $limit !==
false ) {
705 $queryBuilder->limit( intval( $limit ) );
708 if ( $offset !==
false ) {
709 $queryBuilder->offset( intval( $offset ) );
712 $order = $this->getCacheOrderFields();
713 if ( $this->sortDescending() ) {
714 $queryBuilder->orderBy( $order, SelectQueryBuilder::SORT_DESC );
716 $queryBuilder->orderBy( $order );
719 return $queryBuilder->caller( __METHOD__ )->fetchResultSet();
737 if ( $this->cachedTimestamp ===
null ) {
738 $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
739 $fname = static::class .
'::getCachedTimestamp';
740 $this->cachedTimestamp = $dbr->newSelectQueryBuilder()
741 ->select(
'qci_timestamp' )
742 ->from(
'querycache_info' )
743 ->where( [
'qci_type' => $this->getName() ] )
744 ->caller( $fname )->fetchField();
746 return $this->cachedTimestamp;
762 [ $limit, $offset ] = $this->getRequest()
763 ->getLimitOffsetForUser( $this->getUser() );
764 if ( $this->getConfig()->
get( MainConfigNames::MiserMode ) ) {
765 $maxResults = $this->getMaxResults();
767 $limit = min( $limit, $maxResults );
769 $offset = min( $offset, $maxResults + 1 );
771 return [ $limit, $offset ];
783 $maxResults = $this->getMaxResults();
784 if ( $this->getConfig()->
get( MainConfigNames::MiserMode ) ) {
785 $limit = min( $uiLimit + 1, $maxResults - $uiOffset );
786 return max( $limit, 0 );
804 return max( $this->getConfig()->
get( MainConfigNames::QueryCacheLimit ), 10000 );
814 $this->checkPermissions();
817 $this->outputHeader();
819 $out = $this->getOutput();
821 if ( $this->isCached() && !$this->isCacheable() ) {
822 $out->addWikiMsg(
'querypage-disabled' );
826 $out->setSyndicated( $this->isSyndicated() );
828 if ( $this->limit == 0 && $this->offset == 0 ) {
829 [ $this->limit, $this->offset ] = $this->getLimitOffset();
831 $dbLimit = $this->getDBLimit( $this->limit, $this->offset );
833 if ( !$this->isCached() ) {
835 $res = $this->reallyDoQuery( $dbLimit, $this->offset );
838 $res = $this->fetchFromCache( $dbLimit, $this->offset );
839 if ( !$this->listoutput ) {
841 $ts = $this->getCachedTimestamp();
842 $lang = $this->getLanguage();
843 $maxResults = $lang->formatNum( $this->getConfig()->
get(
844 MainConfigNames::QueryCacheLimit ) );
847 $user = $this->getUser();
848 $updated = $lang->userTimeAndDate( $ts, $user );
849 $updateddate = $lang->userDate( $ts, $user );
850 $updatedtime = $lang->userTime( $ts, $user );
851 $out->addMeta(
'Data-Cache-Time', $ts );
852 $out->addJsConfigVars(
'dataCacheTime', $ts );
853 $out->addWikiMsg(
'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults );
855 $out->addWikiMsg(
'perfcached', $maxResults );
860 $disabledQueryPages = self::getDisabledQueryPages( $this->getConfig() );
861 if ( isset( $disabledQueryPages[$this->getName()] ) ) {
862 $runMode = $disabledQueryPages[$this->getName()];
863 if ( $runMode ===
'disabled' ) {
865 "<div class=\"mw-querypage-no-updates\">\n$1\n</div>",
866 'querypage-no-updates'
871 "<div class=\"mw-querypage-updates-" . $runMode .
"\">\n$1\n</div>",
872 'querypage-updates-' . $runMode
879 $this->numRows = $res->numRows();
881 $dbr = $this->getRecacheDB();
882 $this->preprocessResults( $dbr, $res );
884 $out->addHTML( Html::openElement(
'div', [
'class' =>
'mw-spcontent' ] ) );
887 if ( $this->shownavigation ) {
888 $out->addHTML( $this->getPageHeader() );
889 if ( $this->numRows > 0 ) {
890 $out->addHTML( $this->msg(
'showingresultsinrange' )->numParams(
891 min( $this->numRows, $this->limit ),
892 $this->offset + 1, ( min( $this->numRows, $this->limit ) + $this->offset ) )->parseAsBlock() );
894 $miserMaxResults = $this->getConfig()->get( MainConfigNames::MiserMode )
895 && ( $this->offset + $this->limit >= $this->getMaxResults() );
896 $atEnd = ( $this->numRows <= $this->limit ) || $miserMaxResults;
897 $paging = $this->buildPrevNextNavigation( $this->offset,
898 $this->limit, $this->linkParameters(), $atEnd, $par );
899 $out->addHTML(
'<p>' . $paging .
'</p>' );
903 $this->showEmptyText();
904 $out->addHTML( Html::closeElement(
'div' ) );
912 $this->outputResults( $out,
916 min( $this->numRows, $this->limit ),
920 if ( $this->shownavigation ) {
922 $out->addHTML(
'<p>' . $paging .
'</p>' );
925 $out->addHTML( Html::closeElement(
'div' ) );
941 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
944 if ( !$this->listoutput ) {
945 $html[] = $this->openList( $offset );
951 for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++ ) {
952 $line = $this->formatResult( $skin, $row );
954 $html[] = $this->listoutput
956 :
"<li>{$line}</li>\n";
960 if ( !$this->listoutput ) {
961 $html[] = $this->closeList();
964 $html = $this->listoutput
965 ? $this->getContentLanguage()->listToText( $html )
966 : implode(
'', $html );
968 $out->addHTML( $html );
977 return "\n<ol start='" . ( $offset + 1 ) .
"' class='special'>\n";
1013 $batch = $this->getLinkBatchFactory()->newLinkBatch()->setCaller( __METHOD__ );
1014 foreach ( $res as $row ) {
1015 $batch->add( $ns ?? (
int)$row->namespace, $row->title );
1028 $this->loadBalancer = $loadBalancer;
1037 if ( $this->loadBalancer === null ) {
1040 $this->loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
1042 return $this->loadBalancer;
1050 $this->databaseProvider = $databaseProvider;
1058 if ( $this->databaseProvider === null ) {
1059 $this->databaseProvider = MediaWikiServices::getInstance()->getConnectionProvider();
1061 return $this->databaseProvider;
1066class_alias( QueryPage::class,
'QueryPage' );
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
A class containing constants representing the names of configuration variables.
This is one of the Core classes and should be read at least once by any new developers.
Factory for LinkBatch objects to batch query page metadata.
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.
usesExternalSource()
Check if this query page is configured to fetch data from an external source via HTTP.
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...
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.