Cast API timeSpentBackend to an int
[lhc/web/wiklou.git] / includes / api / ApiQueryInfo.php
index ea7818c..2d382dd 100644 (file)
@@ -453,7 +453,7 @@ class ApiQueryInfo extends ApiQueryBase {
                }
 
                if ( $this->fld_watchers ) {
-                       if ( isset( $this->watchers[$ns][$dbkey] ) ) {
+                       if ( $this->watchers !== null && $this->watchers[$ns][$dbkey] !== 0 ) {
                                $pageInfo['watchers'] = $this->watchers[$ns][$dbkey];
                        } elseif ( $this->showZeroWatchers ) {
                                $pageInfo['watchers'] = 0;
@@ -461,7 +461,7 @@ class ApiQueryInfo extends ApiQueryBase {
                }
 
                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;
@@ -799,28 +799,17 @@ class ApiQueryInfo extends ApiQueryBase {
                        return;
                }
 
-               $this->watchers = [];
                $this->showZeroWatchers = $canUnwatchedpages;
-               $db = $this->getDB();
-
-               $lb = new LinkBatch( $this->everything );
 
-               $this->resetQueryParams();
-               $this->addTables( [ 'watchlist' ] );
-               $this->addFields( [ 'wl_title', 'wl_namespace', 'count' => 'COUNT(*)' ] );
-               $this->addWhere( [
-                       $lb->constructSet( 'wl', $db )
-               ] );
-               $this->addOption( 'GROUP BY', [ 'wl_namespace', 'wl_title' ] );
+               $countOptions = [];
                if ( !$canUnwatchedpages ) {
-                       $this->addOption( 'HAVING', "COUNT(*) >= $unwatchedPageThreshold" );
+                       $countOptions['minimumWatchers'] = $unwatchedPageThreshold;
                }
 
-               $res = $this->select( __METHOD__ );
-
-               foreach ( $res as $row ) {
-                       $this->watchers[$row->wl_namespace][$row->wl_title] = (int)$row->count;
-               }
+               $this->watchers = WatchedItemStore::getDefaultInstance()->countWatchersMultiple(
+                       $this->everything,
+                       $countOptions
+               );
        }
 
        /**
@@ -842,14 +831,7 @@ class ApiQueryInfo extends ApiQueryBase {
 
                $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 );
 
@@ -864,55 +846,38 @@ class ApiQueryInfo extends ApiQueryBase {
                        $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 ) {