API * simple backlinks module (no redirect resolution yet)
authorYuri Astrakhan <yurik@users.mediawiki.org>
Wed, 25 Oct 2006 03:54:56 +0000 (03:54 +0000)
committerYuri Astrakhan <yurik@users.mediawiki.org>
Wed, 25 Oct 2006 03:54:56 +0000 (03:54 +0000)
* Cleaned up references

14 files changed:
includes/AutoLoader.php
includes/api/ApiFeedWatchlist.php
includes/api/ApiFormatBase.php
includes/api/ApiMain.php
includes/api/ApiOpenSearch.php
includes/api/ApiPageSet.php
includes/api/ApiQuery.php
includes/api/ApiQueryAllpages.php
includes/api/ApiQueryBase.php
includes/api/ApiQueryInfo.php
includes/api/ApiQueryLogEvents.php
includes/api/ApiQueryRecentChanges.php
includes/api/ApiQueryRevisions.php
includes/api/ApiQueryWatchlist.php

index b1525c5..21e395f 100644 (file)
@@ -255,6 +255,7 @@ function __autoload($className) {
                'ApiQuery' => 'includes/api/ApiQuery.php',
                'ApiQueryAllpages' => 'includes/api/ApiQueryAllpages.php',
                'ApiQueryBase' => 'includes/api/ApiQueryBase.php',
+               'ApiQueryBacklinks' => 'includes/api/ApiQueryBacklinks.php',
                'ApiQueryInfo' => 'includes/api/ApiQueryInfo.php',
                'ApiQueryLogEvents' => 'includes/api/ApiQueryLogEvents.php',
                'ApiQueryRecentChanges'=> 'includes/api/ApiQueryRecentChanges.php',
index b26948f..b5003a2 100644 (file)
@@ -62,7 +62,7 @@ class ApiFeedWatchlist extends ApiBase {
                $module->execute();
 
                // Get data array
-               $data = $module->getResultData();
+               $data = $module->getResultData();
 
                $feedItems = array ();
                foreach ($data['query']['watchlist'] as $index => $info) {
index 1a415eb..cb3c114 100644 (file)
@@ -205,10 +205,10 @@ class ApiFormatFeedWrapper extends ApiFormatBase {
        }
 
        public function execute() {
-               $data = $this->getResultData();
+               $data = $this->getResultData();
                if (isset ($data['_feed']) && isset ($data['_feeditems'])) {
-                       $feed = $data['_feed'];
-                       $items = $data['_feeditems'];
+                       $feed = $data['_feed'];
+                       $items = $data['_feeditems'];
 
                        $feed->outHeader();
                        foreach ($items as & $item)
index 12ad331..77e230b 100644 (file)
@@ -77,12 +77,15 @@ class ApiMain extends ApiBase {
        * @param $enableWrite bool should be set to true if the api may modify data
        */
        public function __construct($request, $enableWrite = false) {
+
+               $this->mInternalMode = ($request instanceof FauxRequest);
+
                // Special handling for the main module: $parent === $this
-               parent :: __construct($this, 'main');
+               parent :: __construct($this, $this->mInternalMode ? 'main_int' : 'main');
 
-               $this->mModules = self :: $Modules;
+               $this->mModules = self :: $Modules;
                $this->mModuleNames = array_keys($this->mModules); // todo: optimize
-               $this->mFormats = self :: $Formats;
+               $this->mFormats = self :: $Formats;
                $this->mFormatNames = array_keys($this->mFormats); // todo: optimize
 
                $this->mResult = new ApiResult($this);
@@ -91,8 +94,6 @@ class ApiMain extends ApiBase {
 
                $this->mRequest = & $request;
 
-               $this->mInternalMode = ($request instanceof FauxRequest);
-
                $this->mSquidMaxage = 0;
        }
 
index 50b8e9c..19b30b1 100644 (file)
@@ -64,7 +64,7 @@ class ApiOpenSearch extends ApiBase {
                $module->execute();
 
                // Get resulting data
-               $data = $module->getResultData();
+               $data = $module->getResultData();
 
                // Reformat useful data for future printing by JSON engine
                $srchres = array ();
index 3cf480b..33ebbf9 100644 (file)
@@ -243,20 +243,25 @@ class ApiPageSet extends ApiQueryBase {
         * Extract all requested fields from the row received from the database
         */
        public function processDbRow($row) {
-               $pageId = intval($row->page_id);
-
+       
                // Store Title object in various data structures
                $title = Title :: makeTitle($row->page_namespace, $row->page_title);
-               $this->mAllPages[$row->page_namespace][$row->page_title] = $pageId;
+       
+               // skip any pages that user has no rights to read
+               if ($title->userCanRead()) {
 
-               if ($this->mResolveRedirects && $row->page_is_redirect == '1') {
-                       $this->mPendingRedirectIDs[$pageId] = $title;
-               } else {
-                       $this->mGoodTitles[$pageId] = $title;
+                       $pageId = intval($row->page_id);        
+                       $this->mAllPages[$row->page_namespace][$row->page_title] = $pageId;
+       
+                       if ($this->mResolveRedirects && $row->page_is_redirect == '1') {
+                               $this->mPendingRedirectIDs[$pageId] = $title;
+                       } else {
+                               $this->mGoodTitles[$pageId] = $title;
+                       }
+       
+                       foreach ($this->mRequestedPageFields as $fieldName => & $fieldValues)
+                               $fieldValues[$pageId] = $row-> $fieldName;
                }
-
-               foreach ($this->mRequestedPageFields as $fieldName => & $fieldValues)
-                       $fieldValues[$pageId] = $row-> $fieldName;
        }
        
        public function finishPageSetGeneration() {
@@ -280,13 +285,13 @@ class ApiPageSet extends ApiQueryBase {
         * #6 Repeat from step #1     
         */
        private function initFromTitles($titles) {
-               $db = $this->getDB();
 
                // Get validated and normalized title objects
                $linkBatch = $this->processTitlesStrArray($titles);
                if($linkBatch->isEmpty())
                        return;
                        
+               $db = & $this->getDB();
                $set = $linkBatch->constructSet('page', $db);
 
                // Get pageIDs data from the `page` table
@@ -305,11 +310,12 @@ class ApiPageSet extends ApiQueryBase {
                if(empty($pageids))
                        return;
                        
-               $db = $this->getDB();
                $set = array (
                        'page_id' => $pageids
                );
 
+               $db = & $this->getDB();
+
                // Get pageIDs data from the `page` table
                $this->profileDBIn();
                $res = $db->select('page', $this->getPageTableFields(), $set, __METHOD__);
@@ -381,7 +387,7 @@ class ApiPageSet extends ApiQueryBase {
                if(empty($revids))
                        return;
                        
-               $db = $this->getDB();
+               $db = $this->getDB();
                $pageids = array();
                $remaining = array_flip($revids);
                
@@ -413,7 +419,7 @@ class ApiPageSet extends ApiQueryBase {
        private function resolvePendingRedirects() {
 
                if($this->mResolveRedirects) {
-                       $db = $this->getDB();
+                       $db = $this->getDB();
                        $pageFlds = $this->getPageTableFields();
        
                        // Repeat until all redirects have been resolved
@@ -445,7 +451,7 @@ class ApiPageSet extends ApiQueryBase {
        private function getRedirectTargets() {
 
                $linkBatch = new LinkBatch();
-               $db = $this->getDB();
+               $db = $this->getDB();
 
                // find redirect targets for all redirect pages
                $this->profileDBIn();
index cda708d..6ddac0a 100644 (file)
@@ -49,9 +49,9 @@ class ApiQuery extends ApiBase {
                'allpages' => 'ApiQueryAllpages',
                'logevents' => 'ApiQueryLogEvents',
                'watchlist' => 'ApiQueryWatchlist',
-               'recentchanges' => 'ApiQueryRecentChanges'
+               'recentchanges' => 'ApiQueryRecentChanges',
+               'backlinks' => 'ApiQueryBacklinks'
        );
-       //      'backlinks' => 'ApiQueryBacklinks',
        //      'categorymembers' => 'ApiQueryCategorymembers',
        //      'embeddedin' => 'ApiQueryEmbeddedin',
        //      'imagelinks' => 'ApiQueryImagelinks',
@@ -79,9 +79,12 @@ class ApiQuery extends ApiBase {
                $this->mAllowedGenerators = array_merge($this->mListModuleNames, $this->mPropModuleNames);
        }
 
-       public function getDB() {
-               if (!isset ($this->mSlaveDB))
+       public function & getDB() {
+               if (!isset ($this->mSlaveDB)) {
+                       $this->profileDBIn();
                        $this->mSlaveDB = & wfGetDB(DB_SLAVE);
+                       $this->profileDBOut();
+               }
                return $this->mSlaveDB;
        }
 
@@ -198,13 +201,12 @@ class ApiQuery extends ApiBase {
                        $result->addValue('query', 'redirects', $redirValues);
                }
 
-
                //
                // Missing revision elements
                //
                $missingRevIDs = $pageSet->getMissingRevisionIDs();
-               if (!empty($missingRevIDs)) {
-                       $revids = array();
+               if (!empty ($missingRevIDs)) {
+                       $revids = array ();
                        foreach ($missingRevIDs as $revid) {
                                $revids[$revid] = array (
                                        'revid' => $revid
@@ -213,7 +215,7 @@ class ApiQuery extends ApiBase {
                        $result->setIndexedTagName($revids, 'rev');
                        $result->addValue('query', 'badrevids', $revids);
                }
-               
+
                //
                // Page elements
                //
@@ -237,7 +239,8 @@ class ApiQuery extends ApiBase {
                // Output general page information for found titles
                foreach ($pageSet->getGoodTitles() as $pageid => $title) {
                        $pages[$pageid] = array (
-                       'pageid' => $pageid, 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText());
+                               'pageid' => $pageid,
+                       'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText());
                }
 
                if (!empty ($pages)) {
@@ -272,13 +275,13 @@ class ApiQuery extends ApiBase {
 
                // execute current pageSet to get the data for the generator module
                $this->mPageSet->execute();
-               
+
                // populate resultPageSet with the generator output
                $generator->profileIn();
                $generator->executeGenerator($resultPageSet);
                $resultPageSet->finishPageSetGeneration();
                $generator->profileOut();
-               
+
                // Swap the resulting pageset back in
                $this->mPageSet = $resultPageSet;
        }
index bf51e1d..71868a0 100644 (file)
@@ -49,7 +49,7 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
        private function run($resultPageSet = null) {
 
                wfProfileIn($this->getModuleProfileName() . '-getDB');
-               $db = $this->getDB();
+               $db = $this->getDB();
                wfProfileOut($this->getModuleProfileName() . '-getDB');
 
                wfProfileIn($this->getModuleProfileName() . '-parseParams');
@@ -79,15 +79,14 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
                $this->addOption( 'LIMIT', $limit +1);
                $this->addOption( 'ORDER BY', 'page_namespace, page_title');
 
-               $data = array ();
-               $count = 0;
-
                wfProfileOut($this->getModuleProfileName() . '-parseParams');
 
                $res = $this->select(__METHOD__);
 
                wfProfileIn($this->getModuleProfileName() . '-saveResults');
 
+               $data = array ();
+               $count = 0;
                while ($row = $db->fetchObject($res)) {
                        if (++ $count > $limit) {
                                // We've reached the one extra which shows that there are additional pages to be had. Stop here...
@@ -100,11 +99,7 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
                                if($vals)
                                        $data[intval($row->page_id)] = $vals;
                        } else {
-                               $title = Title :: makeTitle($row->page_namespace, $row->page_title);
-                               // skip any pages that user has no rights to read
-                               if ($title->userCanRead()) {
-                                       $resultPageSet->processDbRow($row);
-                               }
+                               $resultPageSet->processDbRow($row);
                        }
                }
                $db->freeResult($res);
index 151dc72..e4db51c 100644 (file)
@@ -89,7 +89,7 @@ abstract class ApiQueryBase extends ApiBase {
                $isDirNewer = ($dir === 'newer');
                $after = ($isDirNewer ? '<=' : '>=');
                $before = ($isDirNewer ? '>=' : '<=');
-               $db = $this->getDB();
+               $db = $this->getDB();
 
                if (!is_null($start))
                        $this->addWhere($field . $after . $db->addQuotes($start));
@@ -105,9 +105,14 @@ abstract class ApiQueryBase extends ApiBase {
        }
        
        protected function select($method) {
+               
+               // getDB has its own profileDBIn/Out calls
+               $db = & $this->getDB();         
+               
                $this->profileDBIn();
-               $res = $this->getDB()->select($this->tables, $this->fields, $this->where, $method, $this->options);
+               $res = $db->select($this->tables, $this->fields, $this->where, $method, $this->options);
                $this->profileDBOut();
+               
                return $res;
        }
 
@@ -129,51 +134,62 @@ abstract class ApiQueryBase extends ApiBase {
                        $vals['title'] = $title->getPrefixedText();
                }       
 
-               if ($prefix === 'rc') {
-
-                       // PageId
-                       @$tmp = $row->rc_cur_id;
-                       if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
+               switch($prefix) {
 
-                       @$tmp = $row->rc_this_oldid;
-                       if(!is_null($tmp)) $vals['revid'] = intval($tmp);
+                       case 'page':
+                               // page_is_redirect
+                               @$tmp = $row->page_is_redirect;
+                               if($tmp) $vals['redirect'] = '';
 
-                       @$tmp = $row->rc_last_oldid;
-                       if(!is_null($tmp)) $vals['old_revid'] = intval($tmp);
+                               break;
 
-                       $title = ApiQueryBase::addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title');
-                       if ($title) {
-                               if (!$title->userCanRead())
-                                       return false;
-                               $vals['new_ns'] = $title->getNamespace();
-                               $vals['new_title'] = $title->getPrefixedText();
-                       }       
-
-                       @$tmp = $row->rc_patrolled;
-                       if(!is_null($tmp)) $vals['patrolled'] = '';
-
-               } elseif ($prefix === 'log') {
+                       case 'rc':
+                               // PageId
+                               @$tmp = $row->rc_cur_id;
+                               if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
+       
+                               @$tmp = $row->rc_this_oldid;
+                               if(!is_null($tmp)) $vals['revid'] = intval($tmp);
+       
+                               @$tmp = $row->rc_last_oldid;
+                               if(!is_null($tmp)) $vals['old_revid'] = intval($tmp);
+       
+                               $title = ApiQueryBase::addRowInfo_title($row, 'rc_moved_to_ns', 'rc_moved_to_title');
+                               if ($title) {
+                                       if (!$title->userCanRead())
+                                               return false;
+                                       $vals['new_ns'] = $title->getNamespace();
+                                       $vals['new_title'] = $title->getPrefixedText();
+                               }       
+       
+                               @$tmp = $row->rc_patrolled;
+                               if(!is_null($tmp)) $vals['patrolled'] = '';
 
-                       // PageId
-                       @$tmp = $row->page_id;
-                       if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
+                               break;
 
-                       if ($row->log_params !== '') {
-                               $params = explode("\n", $row->log_params);
-                               if ($row->log_type == 'move' && isset ($params[0])) {
-                                       $newTitle = Title :: newFromText($params[0]);
-                                       if ($newTitle) {
-                                               $vals['new_ns'] = $newTitle->getNamespace();
-                                               $vals['new_title'] = $newTitle->getPrefixedText();
-                                               $params = null;
+                       case 'log':
+                               // PageId
+                               @$tmp = $row->page_id;
+                               if(!is_null($tmp)) $vals['pageid'] = intval($tmp);
+       
+                               if ($row->log_params !== '') {
+                                       $params = explode("\n", $row->log_params);
+                                       if ($row->log_type == 'move' && isset ($params[0])) {
+                                               $newTitle = Title :: newFromText($params[0]);
+                                               if ($newTitle) {
+                                                       $vals['new_ns'] = $newTitle->getNamespace();
+                                                       $vals['new_title'] = $newTitle->getPrefixedText();
+                                                       $params = null;
+                                               }
+                                       }
+       
+                                       if (!empty ($params)) {
+                                               $this->getResult()->setIndexedTagName($params, 'param');
+                                               $vals = array_merge($vals, $params);
                                        }
                                }
 
-                               if (!empty ($params)) {
-                                       $this->getResult()->setIndexedTagName($params, 'param');
-                                       $vals = array_merge($vals, $params);
-                               }
-                       }
+                               break;
                }
 
                // Type
index fb1c9c0..93b4d08 100644 (file)
@@ -46,7 +46,7 @@ class ApiQueryInfo extends ApiQueryBase {
 
                $pageSet = $this->getPageSet();
                $titles = $pageSet->getGoodTitles();
-               $result = $this->getResult();
+               $result = $this->getResult();
 
                $pageIsRedir = $pageSet->getCustomField('page_is_redirect');
                $pageTouched = $pageSet->getCustomField('page_touched');
index fbe7740..40dd913 100644 (file)
@@ -39,7 +39,7 @@ class ApiQueryLogEvents extends ApiQueryBase {
                $limit = $type = $start = $end = $dir = $user = $title = null;
                extract($this->extractRequestParams());
 
-               $db = $this->getDB();
+               $db = $this->getDB();
 
                extract($db->tableNames('logging', 'page', 'user'), EXTR_PREFIX_ALL, 'tbl');
                $this->addTables("$tbl_logging LEFT OUTER JOIN $tbl_page ON " .
index 892e59e..6ed0d4b 100644 (file)
@@ -83,7 +83,7 @@ class ApiQueryRecentChanges extends ApiQueryBase {
 
                $data = array ();
                $count = 0;
-               $db = $this->getDB();
+               $db = $this->getDB();
                $res = $this->select(__METHOD__);
                while ($row = $db->fetchObject($res)) {
                        if (++ $count > $limit) {
index 0ff8b33..57e0de4 100644 (file)
@@ -147,8 +147,9 @@ class ApiQueryRevisions extends ApiQueryBase {
 
                $data = array ();
                $count = 0;
-               $db = $this->getDB();
                $res = $this->select(__METHOD__);
+
+               $db = & $this->getDB();
                while ($row = $db->fetchObject($res)) {
 
                        if (++ $count > $limit) {
@@ -176,7 +177,7 @@ class ApiQueryRevisions extends ApiQueryBase {
                // Ensure that all revisions are shown as '<rev>' elements
                $result = $this->getResult();
                if ($result->getIsRawMode()) {
-                       $data = $result->getData();
+                       $data = $result->getData();
                        foreach ($data['query']['pages'] as & $page) {
                                if (is_array($page) && array_key_exists('revisions', $page)) {
                                        $result->setIndexedTagName($page['revisions'], 'rev');
index 9cb5acc..dcd0c45 100644 (file)
@@ -125,7 +125,7 @@ class ApiQueryWatchlist extends ApiQueryGeneratorBase {
                $count = 0;
                $res = $this->select(__METHOD__);
 
-               $db = $this->getDB();
+               $db = $this->getDB();
                while ($row = $db->fetchObject($res)) {
                        if (++ $count > $limit) {
                                // We've reached the one extra which shows that there are additional pages to be had. Stop here...