API: minor code cleanup. Bug that exposed secure wiki pages should be fixed.
authorYuri Astrakhan <yurik@users.mediawiki.org>
Sat, 19 May 2007 06:42:08 +0000 (06:42 +0000)
committerYuri Astrakhan <yurik@users.mediawiki.org>
Sat, 19 May 2007 06:42:08 +0000 (06:42 +0000)
includes/api/ApiBase.php
includes/api/ApiQueryAllpages.php
includes/api/ApiQueryBase.php

index aeac8c3..0450f88 100644 (file)
  */
 
 /**
- * @todo Document - e.g. Provide top-level description of this class.
+ * This abstract class implements many basic API functions, and is the base of all API classes.
+ * The class functions are divided into several areas of functionality:
+ * 
+ * Module parameters: Derived classes can define getAllowedParams() to specify which parameters to expect,
+ *     how to parse and validate them.
+ * 
+ * Profiling: various methods to allow keeping tabs on various tasks and their time costs
+ * 
+ * Self-documentation: code to allow api to document its own state.
+ * 
  * @addtogroup API
  */
 abstract class ApiBase {
index 02f5e11..5e2fb0a 100644 (file)
@@ -50,11 +50,8 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
 
        private function run($resultPageSet = null) {
 
-               wfProfileIn($this->getModuleProfileName() . '-getDB');
                $db = $this->getDB();
-               wfProfileOut($this->getModuleProfileName() . '-getDB');
 
-               wfProfileIn($this->getModuleProfileName() . '-parseParams');
                $limit = $from = $namespace = $filterredir = $prefix = null;
                extract($this->extractRequestParams());
 
@@ -81,12 +78,8 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
                $this->addOption('LIMIT', $limit +1);
                $this->addOption('ORDER BY', 'page_namespace, page_title');
 
-               wfProfileOut($this->getModuleProfileName() . '-parseParams');
-
                $res = $this->select(__METHOD__);
 
-               wfProfileIn($this->getModuleProfileName() . '-saveResults');
-
                $data = array ();
                $count = 0;
                while ($row = $db->fetchObject($res)) {
@@ -97,9 +90,13 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
                        }
 
                        if (is_null($resultPageSet)) {
-                               $vals = $this->addRowInfo('page', $row);
-                               if ($vals)
-                                       $data[intval($row->page_id)] = $vals;
+                               $title = Title :: makeTitle($row->page_namespace, $row->page_title);
+                               if ($title->userCanRead()) {
+                                       $data[intval($row->page_id)] = array(
+                                               'pageid' => $row->page_id,
+                                               'ns' => $title->getNamespace(),
+                                               'title' => $title->getPrefixedText());
+                               }
                        } else {
                                $resultPageSet->processDbRow($row);
                        }
@@ -111,8 +108,6 @@ class ApiQueryAllpages extends ApiQueryGeneratorBase {
                        $result->setIndexedTagName($data, 'p');
                        $result->addValue('query', $this->getModuleName(), $data);
                }
-
-               wfProfileOut($this->getModuleProfileName() . '-saveResults');
        }
 
        protected function getAllowedParams() {
index de1e6ed..523c41e 100644 (file)
@@ -165,8 +165,7 @@ abstract class ApiQueryBase extends ApiBase {
                                if ($title) {
                                        if (!$title->userCanRead())
                                                return false;
-                                       $vals['new_ns'] = $title->getNamespace();
-                                       $vals['new_title'] = $title->getPrefixedText();
+                                       ApiQueryBase :: addTitleInfo($vals, $title, 'new_');
                                }
 
                                if ( isset( $row->rc_patrolled ) )
@@ -185,8 +184,7 @@ abstract class ApiQueryBase extends ApiBase {
                                        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();
+                                                       ApiQueryBase :: addTitleInfo($vals, $newTitle, 'new_');
                                                        $params = null;
                                                }
                                        }
@@ -207,78 +205,68 @@ abstract class ApiQueryBase extends ApiBase {
                }
 
                // Type
-               @ $tmp = $row-> {
-                       $prefix . '_type' };
+               @ $tmp = $row-> { $prefix . '_type' };
                if (!is_null($tmp))
                        $vals['type'] = $tmp;
 
                // Action
-               @ $tmp = $row-> {
-                       $prefix . '_action' };
+               @ $tmp = $row-> { $prefix . '_action' };
                if (!is_null($tmp))
                        $vals['action'] = $tmp;
 
                // Old ID
-               @ $tmp = $row-> {
-                       $prefix . '_text_id' };
+               @ $tmp = $row-> { $prefix . '_text_id' };
                if (!is_null($tmp))
                        $vals['oldid'] = intval($tmp);
 
                // User Name / Anon IP
-               @ $tmp = $row-> {
-                       $prefix . '_user_text' };
+               @ $tmp = $row-> { $prefix . '_user_text' };
                if (is_null($tmp))
                        @ $tmp = $row->user_name;
                if (!is_null($tmp)) {
                        $vals['user'] = $tmp;
-                       @ $tmp = !$row-> {
-                               $prefix . '_user' };
+                       @ $tmp = !$row-> { $prefix . '_user' };
                        if (!is_null($tmp) && $tmp)
                                $vals['anon'] = '';
                }
 
                // Bot Edit
-               @ $tmp = $row-> {
-                       $prefix . '_bot' };
+               @ $tmp = $row-> { $prefix . '_bot' };
                if (!is_null($tmp) && $tmp)
                        $vals['bot'] = '';
 
                // New Edit
-               @ $tmp = $row-> {
-                       $prefix . '_new' };
+               @ $tmp = $row-> { $prefix . '_new' };
                if (is_null($tmp))
-                       @ $tmp = $row-> {
-                       $prefix . '_is_new' };
+                       @ $tmp = $row-> { $prefix . '_is_new' };
                if (!is_null($tmp) && $tmp)
                        $vals['new'] = '';
 
                // Minor Edit
-               @ $tmp = $row-> {
-                       $prefix . '_minor_edit' };
+               @ $tmp = $row-> { $prefix . '_minor_edit' };
                if (is_null($tmp))
-                       @ $tmp = $row-> {
-                       $prefix . '_minor' };
+                       @ $tmp = $row-> { $prefix . '_minor' };
                if (!is_null($tmp) && $tmp)
                        $vals['minor'] = '';
 
                // Timestamp
-               @ $tmp = $row-> {
-                       $prefix . '_timestamp' };
+               @ $tmp = $row-> { $prefix . '_timestamp' };
                if (!is_null($tmp))
                        $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $tmp);
 
                // Comment
-               @ $tmp = $row-> {
-                       $prefix . '_comment' };
+               @ $tmp = $row-> { $prefix . '_comment' };
                if (!empty ($tmp)) // optimize bandwidth
                        $vals['comment'] = $tmp;
 
                return $vals;
        }
 
-       protected static function addTitleInfo(&$arr, $title) {
-               $arr['ns'] = $title->getNamespace();
-               $arr['title'] = $title->getPrefixedText();
+       protected static function addTitleInfo(&$arr, $title, $prefix='') {
+               $arr[$prefix . 'ns'] = $title->getNamespace();
+               $arr[$prefix . 'title'] = $title->getPrefixedText();
+               if (!$title->userCanRead())
+                       $arr[$prefix . 'inaccessible'] = "";
        }
        
        private static function addRowInfo_title($row, $nsfld, $titlefld) {