* API: fixed watchlist db selection ('watchlist' db group)
authorYuri Astrakhan <yurik@users.mediawiki.org>
Tue, 15 May 2007 02:16:48 +0000 (02:16 +0000)
committerYuri Astrakhan <yurik@users.mediawiki.org>
Tue, 15 May 2007 02:16:48 +0000 (02:16 +0000)
includes/api/ApiQuery.php
includes/api/ApiQueryBase.php
includes/api/ApiQueryWatchlist.php

index 04dee33..7a7c938 100644 (file)
@@ -71,6 +71,7 @@ class ApiQuery extends ApiBase {
        //      'userinfo' => 'ApiQueryUserinfo',
 
        private $mSlaveDB = null;
+       private $mNamedDB = array();
 
        public function __construct($main, $action) {
                parent :: __construct($main, $action);
@@ -92,6 +93,21 @@ class ApiQuery extends ApiBase {
                return $this->mSlaveDB;
        }
 
+       /**
+        * Get the query database connection with the given name.
+        * If no such connection has been requested before, it will be created. 
+        * Subsequent calls with the same $name will return the same connection 
+        * as the first, regardless of $db or $groups new values. 
+        */
+       public function getNamedDB($name, $db, $groups) {
+               if (!array_key_exists($name, $this->mNamedDB)) {
+                       $this->profileDBIn();
+                       $this->mNamedDB[$name] = wfGetDB($db, $groups);
+                       $this->profileDBOut();
+               }
+               return $this->mNamedDB[$name];
+       }
+
        public function getPageSet() {
                return $this->mPageSet;
        }
index 89b2b6d..de1e6ed 100644 (file)
@@ -33,11 +33,12 @@ if (!defined('MEDIAWIKI')) {
  */
 abstract class ApiQueryBase extends ApiBase {
 
-       private $mQueryModule, $tables, $where, $fields, $options;
+       private $mQueryModule, $mDb, $tables, $where, $fields, $options;
 
        public function __construct($query, $moduleName, $paramPrefix = '') {
                parent :: __construct($query->getMain(), $moduleName, $paramPrefix);
                $this->mQueryModule = $query;
+               $this->mDb = null;
                $this->resetQueryParams();
        }
 
@@ -315,7 +316,19 @@ abstract class ApiQueryBase extends ApiBase {
         * Get the Query database connection (readonly)
         */
        protected function getDB() {
-               return $this->getQuery()->getDB();
+               if (is_null($this->mDb))
+                       $this->mDb = $this->getQuery()->getDB();
+               return $this->mDb;
+       }
+
+       /**
+        * Selects the query database connection with the given name.
+        * If no such connection has been requested before, it will be created. 
+        * Subsequent calls with the same $name will return the same connection 
+        * as the first, regardless of $db or $groups new values. 
+        */
+       public function selectNamedDB($name, $db, $groups) {
+               $this->mDb = $this->getQuery()->getNamedDB($name, $db, $groups);        
        }
 
        /**
@@ -323,7 +336,7 @@ abstract class ApiQueryBase extends ApiBase {
         * @return ApiPageSet data
         */
        protected function getPageSet() {
-               return $this->mQueryModule->getPageSet();
+               return $this->getQuery()->getPageSet();
        }
 
        /**
index 99e7151..1b7b72d 100644 (file)
@@ -48,6 +48,8 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
        private function run($resultPageSet = null) {
                global $wgUser;
 
+               $this->selectNamedDB('watchlist', DB_SLAVE, 'watchlist');
+
                if (!$wgUser->isLoggedIn())
                        $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');