* API: Query Meta SiteInfo module
authorYuri Astrakhan <yurik@users.mediawiki.org>
Tue, 26 Sep 2006 01:44:13 +0000 (01:44 +0000)
committerYuri Astrakhan <yurik@users.mediawiki.org>
Tue, 26 Sep 2006 01:44:13 +0000 (01:44 +0000)
* API: Improved query help screen

api.php
includes/api/ApiBase.php
includes/api/ApiQuery.php
includes/api/ApiQueryBase.php
includes/api/ApiQueryContent.php
includes/api/ApiQueryInfo.php [new file with mode: 0644]
includes/api/ApiQuerySiteinfo.php [new file with mode: 0644]
includes/api/ApiResult.php

diff --git a/api.php b/api.php
index 25191ca..48ae7e4 100644 (file)
--- a/api.php
+++ b/api.php
 * http://www.gnu.org/copyleft/gpl.html\r
 */\r
 \r
-$apiStartTime = microtime(true);\r
+$wgApiStartTime = microtime(true);\r
 \r
 /**\r
  * When no format parameter is given, this format will be used\r
  */\r
 define('API_DEFAULT_FORMAT', 'xmlfm');\r
 \r
-$apidir = 'includes/api';\r
+/**\r
+ * All API classes reside in this directory\r
+ */\r
+$wgApiDirectory = 'includes/api/';\r
+\r
 /**\r
  * List of classes and containing files.\r
  */\r
-$apiAutoloadClasses = array (\r
+$wgApiAutoloadClasses = array (\r
 \r
-       'ApiMain' => "$apidir/ApiMain.php",\r
+       'ApiMain' => 'ApiMain.php',\r
 \r
                // Utility classes\r
-       'ApiBase' => "$apidir/ApiBase.php",\r
-       'ApiQueryBase' => "$apidir/ApiQueryBase.php",\r
-       'ApiResult' => "$apidir/ApiResult.php",\r
+       'ApiBase' => 'ApiBase.php',\r
+       'ApiQueryBase' => 'ApiQueryBase.php',\r
+       'ApiResult' => 'ApiResult.php',\r
+       'ApiPageSet' => 'ApiPageSet.php',\r
 \r
                // Formats\r
-       'ApiFormatBase' => "$apidir/ApiFormatBase.php",\r
-       'ApiFormatYaml' => "$apidir/ApiFormatYaml.php",\r
-       'ApiFormatXml' => "$apidir/ApiFormatXml.php",\r
-       'ApiFormatJson' => "$apidir/ApiFormatJson.php",\r
+       'ApiFormatBase' => 'ApiFormatBase.php',\r
+       'ApiFormatYaml' => 'ApiFormatYaml.php',\r
+       'ApiFormatXml' => 'ApiFormatXml.php',\r
+       'ApiFormatJson' => 'ApiFormatJson.php',\r
 \r
                // Modules (action=...) - should match the $apiModules list\r
-       'ApiHelp' => "$apidir/ApiHelp.php",\r
-       'ApiLogin' => "$apidir/ApiLogin.php",\r
-       'ApiQuery' => "$apidir/ApiQuery.php",\r
-\r
-               // Query items (what/list=...)\r
-       'ApiQueryContent' => "$apidir/ApiQueryContent.php",\r
-\r
-       'ApiPageSet' => "$apidir/ApiPageSet.php"\r
+       'ApiHelp' => 'ApiHelp.php',\r
+       'ApiLogin' => 'ApiLogin.php',\r
+       'ApiQuery' => 'ApiQuery.php',\r
+\r
+               // Query items (meta/prop/list=...)\r
+       'ApiQuerySiteinfo' => 'ApiQuerySiteinfo.php',\r
+       'ApiQueryInfo' => 'ApiQueryInfo.php',\r
+       'ApiQueryContent' => 'ApiQueryContent.php'\r
 );\r
 \r
 /**\r
  * List of available modules: action name => module class\r
- * The class must also be listed in the $apiAutoloadClasses array. \r
+ * The class must also be listed in the $wgApiAutoloadClasses array. \r
  */\r
-$apiModules = array (\r
+$wgApiModules = array (\r
        'help' => 'ApiHelp',\r
        'login' => 'ApiLogin',\r
        'query' => 'ApiQuery'\r
@@ -71,9 +76,9 @@ $apiModules = array (
 \r
 /**\r
  * List of available formats: format name => format class\r
- * The class must also be listed in the $apiAutoloadClasses array. \r
+ * The class must also be listed in the $wgApiAutoloadClasses array. \r
  */\r
-$apiFormats = array (\r
+$wgApiFormats = array (\r
        'json' => 'ApiFormatJson',\r
        'jsonfm' => 'ApiFormatJson',\r
        'xml' => 'ApiFormatXml',\r
@@ -96,22 +101,24 @@ if (!isset ($wgEnableAPI) || !$wgEnableAPI) {
        die(-1);\r
 }\r
 \r
-ApiInitAutoloadClasses($apiAutoloadClasses);\r
-$processor = new ApiMain($apiStartTime, $apiModules, $apiFormats);\r
+ApiInitAutoloadClasses($wgApiAutoloadClasses, $wgApiDirectory);\r
+$processor = new ApiMain($wgApiStartTime, $wgApiModules, $wgApiFormats);\r
 $processor->Execute();\r
 \r
 wfProfileOut('api.php');\r
 wfLogProfilingData();\r
 exit; // Done!\r
 \r
-function ApiInitAutoloadClasses($apiAutoloadClasses) {\r
+function ApiInitAutoloadClasses($apiAutoloadClasses, $apiDirectory) {\r
 \r
-       // Append $apiAutoloadClasses to $wgAutoloadClasses\r
+       // Prefix each api class with the proper prefix,\r
+       // and append them to $wgAutoloadClasses\r
        global $wgAutoloadClasses;\r
-       if (isset ($wgAutoloadClasses)) {\r
-               $wgAutoloadClasses = array_merge($wgAutoloadClasses, $apiAutoloadClasses);\r
-       } else {\r
-               $wgAutoloadClasses = $apiAutoloadClasses;\r
-       }\r
+       \r
+       if (!isset ($wgAutoloadClasses))\r
+               $wgAutoloadClasses = array();\r
+\r
+       foreach ($apiAutoloadClasses as $className => $classFile)\r
+               $wgAutoloadClasses[$className] = $apiDirectory . $classFile;\r
 }\r
 ?>
\ No newline at end of file
index b374d73..a6b4229 100644 (file)
@@ -96,6 +96,8 @@ abstract class ApiBase {
                                $msg .= "Parameters:\n";\r
                                foreach (array_keys($params) as $paramName) {\r
                                        $desc = isset ($paramsDescription[$paramName]) ? $paramsDescription[$paramName] : '';\r
+                                       if (is_array($desc))\r
+                                               $desc = implode("\n" . str_repeat(' ', 19), $desc);\r
                                        $msg .= sprintf("  %-14s - %s\n", $paramName, $desc);\r
                                }\r
                        }\r
index 46fdc24..26d045c 100644 (file)
@@ -54,7 +54,7 @@ class ApiQuery extends ApiBase {
        );\r
 \r
        private $mQueryListModules = array (\r
-               'allpages' => 'ApiQueryAllpages',\r
+               // 'allpages' => 'ApiQueryAllpages',\r
                        //              'backlinks' => 'ApiQueryBacklinks',\r
                //              'categorymembers' => 'ApiQueryCategorymembers',\r
                //              'embeddedin' => 'ApiQueryEmbeddedin',\r
@@ -147,13 +147,13 @@ class ApiQuery extends ApiBase {
                $modules = array ();\r
                if (isset($meta))\r
                        foreach ($meta as $moduleName)\r
-                               $modules[] = new $this->mQueryMetaModules[$moduleName] ($this, $moduleName, $data);\r
+                               $modules[] = new $this->mQueryMetaModules[$moduleName] ($this->GetMain(), $moduleName, $data);\r
                if (isset($prop))\r
                        foreach ($prop as $moduleName)\r
-                               $modules[] = new $this->mQueryPropModules[$moduleName] ($this, $moduleName, $data);\r
+                               $modules[] = new $this->mQueryPropModules[$moduleName] ($this->GetMain(), $moduleName, $data);\r
                if (isset($list))\r
                        foreach ($list as $moduleName)\r
-                               $modules[] = new $this->mQueryListModules[$moduleName] ($this, $moduleName, $data);\r
+                               $modules[] = new $this->mQueryListModules[$moduleName] ($this->GetMain(), $moduleName, $data);\r
 \r
                // Title normalizations\r
                foreach ($data->GetNormalizedTitles() as $rawTitleStr => $titleStr) {\r
@@ -215,6 +215,42 @@ class ApiQuery extends ApiBase {
                );\r
        }\r
 \r
+       /**\r
+        * Override the parent to generate help messages for all available query modules.\r
+        */\r
+       public function MakeHelpMsg() {\r
+\r
+               // Use parent to make default message for the query module\r
+               $msg = parent :: MakeHelpMsg();\r
+\r
+               $astriks = str_repeat('--- ', 8);\r
+               $msg .= "\n$astriks Query: Meta  $astriks\n\n";\r
+               $msg .= $this->MakeHelpMsgHelper($this->mQueryMetaModules, 'meta');\r
+               $msg .= "\n$astriks Query: Prop  $astriks\n\n";\r
+               $msg .= $this->MakeHelpMsgHelper($this->mQueryPropModules, 'prop');\r
+               $msg .= "\n$astriks Query: List  $astriks\n\n";\r
+               $msg .= $this->MakeHelpMsgHelper($this->mQueryListModules, 'list');\r
+\r
+               return $msg;\r
+       }\r
+       \r
+       private function MakeHelpMsgHelper($moduleList, $paramName) {\r
+               $msg = '';\r
+\r
+               foreach ($moduleList as $moduleName => $moduleClass) {\r
+                       $msg .= "* $paramName=$moduleName *";\r
+                       $module = new $moduleClass ($this->GetMain(), $moduleName, null);\r
+                       $msg2 = $module->MakeHelpMsg();\r
+                       if ($msg2 !== false)\r
+                               $msg .= $msg2;\r
+                       $msg .= "\n";\r
+                       if ($module->GetCanGenerate())\r
+                               $msg .= "  * Can be used as a generator\n";\r
+               }\r
+\r
+               return $msg;\r
+       }\r
+\r
        protected function GetParamDescription() {\r
                return array (\r
                        'meta' => 'Which meta data to get about the site',\r
index 0b5879a..ff6cd56 100644 (file)
@@ -48,7 +48,9 @@ abstract class ApiQueryBase extends ApiBase {
        /**\r
         * Derived classes return true when they can be used as title generators for other query modules.\r
         */\r
-       protected static abstract function GetCanGenerate();\r
+       public function GetCanGenerate() {\r
+               return false;\r
+       }\r
        \r
        /**\r
         * Return true if this instance is being used as a generator.\r
index 491518e..8a2fee6 100644 (file)
@@ -34,8 +34,8 @@ class ApiQueryContent extends ApiQueryBase {
        /**\r
        * Constructor\r
        */\r
-       public function __construct($main, $action) {\r
-               parent :: __construct($main);\r
+       public function __construct($main, $query, $data) {\r
+               parent :: __construct($main, $query);\r
        }\r
 \r
        public function Execute() {\r
diff --git a/includes/api/ApiQueryInfo.php b/includes/api/ApiQueryInfo.php
new file mode 100644 (file)
index 0000000..32b095d
--- /dev/null
@@ -0,0 +1,70 @@
+<?php
+
+
+/*
+ * Created on Sep 25, 2006
+ *
+ * API for MediaWiki 1.8+
+ *
+ * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+if (!defined('MEDIAWIKI')) {
+       // Eclipse helper - will be ignored in production
+       require_once ("ApiQueryBase.php");
+}
+
+class ApiQueryInfo extends ApiQueryBase {
+
+       public function __construct($main, $query, $data) {
+               parent :: __construct($main, $query);
+       }
+
+       public function Execute() {
+               
+       }
+
+       protected function GetAllowedParams() {
+               return array (
+                       'param' => 'default',
+                       'enumparam' => array (
+                               GN_ENUM_DFLT => 'default',
+                               GN_ENUM_ISMULTI => false,
+                               GN_ENUM_CHOICES => array (
+                                       'a',
+                                       'b'
+                               )
+                       )
+               );
+       }
+
+       protected function GetParamDescription() {
+               return array();
+       }
+
+       protected function GetDescription() {
+               return 'module a';
+       }
+
+       protected function GetExamples() {
+               return array (
+                       'http://...'
+               );
+       }
+}
+?>
\ No newline at end of file
diff --git a/includes/api/ApiQuerySiteinfo.php b/includes/api/ApiQuerySiteinfo.php
new file mode 100644 (file)
index 0000000..16ba59a
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+
+
+/*
+ * Created on Sep 25, 2006
+ *
+ * API for MediaWiki 1.8+
+ *
+ * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+if (!defined('MEDIAWIKI')) {
+       // Eclipse helper - will be ignored in production
+       require_once ("ApiQueryBase.php");
+}
+
+class ApiQuerySiteinfo extends ApiQueryBase {
+
+       public function __construct($main, $query, $data) {
+               parent :: __construct($main, $query);
+       }
+
+       public function Execute() {
+               $siprop = null;
+               extract($this->ExtractRequestParams());
+
+               foreach ($siprop as $prop) {
+                       switch ($prop) {
+
+                               case 'general' :
+
+                                       global $wgSitename, $wgVersion, $wgCapitalLinks;
+                                       $data = array ();
+                                       $mainPage = Title :: newFromText(wfMsgForContent('mainpage'));
+                                       $data['mainpage'] = $mainPage->getText();
+                                       $data['base'] = $mainPage->getFullUrl();
+                                       $data['sitename'] = $wgSitename;
+                                       $data['generator'] = "MediaWiki $wgVersion";
+                                       $data['case'] = $wgCapitalLinks ? 'first-letter' : 'case-sensitive'; // "case-insensitive" option is reserved for future
+                                       $this->GetResult()->AddMessage('query', $prop, $data);
+                                       break;
+
+                               case 'namespaces' :
+
+                                       global $wgContLang;
+                                       $data = array ();
+                                       $data['_element'] = 'ns';
+                                       foreach ($wgContLang->getFormattedNamespaces() as $ns => $title)
+                                               $data[$ns] = array (
+                                                       'id' => $ns,
+                                                       '*' => $title
+                                               );
+                                       $this->GetResult()->AddMessage('query', $prop, $data);
+                                       break;
+
+                               default :
+                                       $this->DieDebug("Unknown siprop=$prop");
+                       }
+               }
+       }
+
+       protected function GetAllowedParams() {
+               return array (
+                       'siprop' => array (
+                               GN_ENUM_DFLT => 'general',
+                               GN_ENUM_ISMULTI => true,
+                               GN_ENUM_CHOICES => array (
+                                       'general',
+                                       'namespaces'
+                               )
+                       )
+               );
+       }
+
+       protected function GetParamDescription() {
+               return array (
+                       'siprop' => array (
+                               'Which sysinfo properties to get:',
+                               ' "general"    - Overall system information',
+                               ' "namespaces" - List of registered namespaces (localized)'
+                       )
+               );
+       }
+
+       protected function GetDescription() {
+               return 'Return general information about the site.';
+       }
+
+       protected function GetExamples() {
+               return 'api.php?action=query&meta=siteinfo&siprop=general|namespaces';
+       }
+}
+?>
\ No newline at end of file
index f99273b..43eaaf1 100644 (file)
@@ -40,9 +40,9 @@ class ApiResult extends ApiBase {
                parent :: __construct($main);\r
                $this->Reset();\r
        }\r
-       \r
+\r
        public function Reset() {\r
-               $this->mData = array();\r
+               $this->mData = array ();\r
        }\r
 \r
        function GetData() {\r
@@ -61,15 +61,12 @@ class ApiResult extends ApiBase {
                } else {\r
                        $element = & $this->mData[$mainSection];\r
                }\r
-               if( $multiitem ) {\r
+               if ($multiitem) {\r
                        $element['_element'] = $multiitem;\r
                        $element[] = $value;\r
                } else {\r
                        if (is_array($value)) {\r
                                $element = array_merge($element, $value);\r
-                               if (!array_key_exists('*', $element)) {\r
-                                       $element['*'] = '';\r
-                               }\r
                        } else {\r
                                if (array_key_exists('*', $element)) {\r
                                        $element['*'] .= $value;\r
@@ -105,7 +102,7 @@ class ApiResult extends ApiBase {
                        }\r
                }\r
        }\r
-       \r
+\r
        public function Execute() {\r
                $this->DieDebug("Execute() is not supported on Result object");\r
        }\r