}
if ( $this->fld_watchers ) {
- if ( $this->watchers[$ns][$dbkey] !== 0 || $this->showZeroWatchers ) {
+ if ( $this->watchers !== null && $this->watchers[$ns][$dbkey] !== 0 ) {
$pageInfo['watchers'] = $this->watchers[$ns][$dbkey];
+ } elseif ( $this->showZeroWatchers ) {
+ $pageInfo['watchers'] = 0;
}
}
if ( $this->fld_visitingwatchers ) {
- if ( isset( $this->visitingwatchers[$ns][$dbkey] ) ) {
+ if ( $this->visitingwatchers !== null && $this->visitingwatchers[$ns][$dbkey] !== 0 ) {
$pageInfo['visitingwatchers'] = $this->visitingwatchers[$ns][$dbkey];
} elseif ( $this->showZeroWatchers ) {
$pageInfo['visitingwatchers'] = 0;
$this->showZeroWatchers = $canUnwatchedpages;
- // Assemble a WHERE condition to find:
- // * if the page exists, number of users watching who have
- // visited the page recently
- // * if the page doesn't exist, number of users that have
- // the page on their watchlist
- $whereStrings = [];
-
- // For pages that exist
+ $titlesWithThresholds = [];
if ( $this->titles ) {
$lb = new LinkBatch( $this->titles );
$this->addOption( 'GROUP BY', [ 'page_namespace', 'page_title' ] );
$timestampRes = $this->select( __METHOD__ );
- // Assemble SQL WHERE condition to find number of page watchers who also
- // visited a "recent" edit (last visited about 26 weeks before latest edit)
$age = $config->get( 'WatchersMaxAge' );
$timestamps = [];
foreach ( $timestampRes as $row ) {
$revTimestamp = wfTimestamp( TS_UNIX, (int)$row->rev_timestamp );
- $threshold = $db->timestamp( $revTimestamp - $age );
- $timestamps[$row->page_namespace][$row->page_title] = $threshold;
- }
-
- foreach ( $timestamps as $ns_key => $namespace ) {
- $pageStrings = [];
- foreach ( $namespace as $pg_key => $threshold ) {
- $pageStrings[] = "wl_title = '$pg_key' AND" .
- ' (wl_notificationtimestamp >= ' .
- $db->addQuotes( $threshold ) .
- ' OR wl_notificationtimestamp IS NULL)';
- }
- $whereStrings[] = "wl_namespace = '$ns_key' AND (" .
- $db->makeList( $pageStrings, LIST_OR ) . ')';
+ $timestamps[$row->page_namespace][$row->page_title] = $revTimestamp - $age;
}
+ $titlesWithThresholds = array_map(
+ function( LinkTarget $target ) use ( $timestamps ) {
+ return [
+ $target, $timestamps[$target->getNamespace()][$target->getDBkey()]
+ ];
+ },
+ $this->titles
+ );
}
- // For nonexistant pages
if ( $this->missing ) {
- $lb = new LinkBatch( $this->missing );
- $whereStrings[] = $lb->constructSet( 'wl', $db );
- }
-
- // Make the actual string and do the query
- $whereString = $db->makeList( $whereStrings, LIST_OR );
-
- $this->resetQueryParams();
- $this->addTables( [ 'watchlist' ] );
- $this->addFields( [
- 'wl_namespace',
- 'wl_title',
- 'count' => 'COUNT(*)'
- ] );
- $this->addWhere( [ $whereString ] );
- $this->addOption( 'GROUP BY', [ 'wl_namespace', 'wl_title' ] );
- if ( !$canUnwatchedpages ) {
- $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
- }
-
- $res = $this->select( __METHOD__ );
- foreach ( $res as $row ) {
- $this->visitingwatchers[$row->wl_namespace][$row->wl_title] = (int)$row->count;
- }
+ $titlesWithThresholds = array_merge(
+ $titlesWithThresholds,
+ array_map(
+ function( LinkTarget $target ) {
+ return [ $target, null ];
+ },
+ $this->missing
+ )
+ );
+ }
+
+ $this->visitingwatchers = WatchedItemStore::getDefaultInstance()->countVisitingWatchersMultiple(
+ $titlesWithThresholds,
+ !$canUnwatchedpages ? $unwatchedPageThreshold : null
+ );
}
public function getCacheMode( $params ) {