From e57335a63366be3ce1a213dd781bab66dab07da2 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 26 Sep 2006 01:44:13 +0000 Subject: [PATCH] * API: Query Meta SiteInfo module * API: Improved query help screen --- api.php | 71 +++++++++++--------- includes/api/ApiBase.php | 2 + includes/api/ApiQuery.php | 44 ++++++++++-- includes/api/ApiQueryBase.php | 4 +- includes/api/ApiQueryContent.php | 4 +- includes/api/ApiQueryInfo.php | 70 +++++++++++++++++++ includes/api/ApiQuerySiteinfo.php | 108 ++++++++++++++++++++++++++++++ includes/api/ApiResult.php | 11 ++- 8 files changed, 268 insertions(+), 46 deletions(-) create mode 100644 includes/api/ApiQueryInfo.php create mode 100644 includes/api/ApiQuerySiteinfo.php diff --git a/api.php b/api.php index 25191ca8a5..48ae7e4485 100644 --- a/api.php +++ b/api.php @@ -22,48 +22,53 @@ * http://www.gnu.org/copyleft/gpl.html */ -$apiStartTime = microtime(true); +$wgApiStartTime = microtime(true); /** * When no format parameter is given, this format will be used */ define('API_DEFAULT_FORMAT', 'xmlfm'); -$apidir = 'includes/api'; +/** + * All API classes reside in this directory + */ +$wgApiDirectory = 'includes/api/'; + /** * List of classes and containing files. */ -$apiAutoloadClasses = array ( +$wgApiAutoloadClasses = array ( - 'ApiMain' => "$apidir/ApiMain.php", + 'ApiMain' => 'ApiMain.php', // Utility classes - 'ApiBase' => "$apidir/ApiBase.php", - 'ApiQueryBase' => "$apidir/ApiQueryBase.php", - 'ApiResult' => "$apidir/ApiResult.php", + 'ApiBase' => 'ApiBase.php', + 'ApiQueryBase' => 'ApiQueryBase.php', + 'ApiResult' => 'ApiResult.php', + 'ApiPageSet' => 'ApiPageSet.php', // Formats - 'ApiFormatBase' => "$apidir/ApiFormatBase.php", - 'ApiFormatYaml' => "$apidir/ApiFormatYaml.php", - 'ApiFormatXml' => "$apidir/ApiFormatXml.php", - 'ApiFormatJson' => "$apidir/ApiFormatJson.php", + 'ApiFormatBase' => 'ApiFormatBase.php', + 'ApiFormatYaml' => 'ApiFormatYaml.php', + 'ApiFormatXml' => 'ApiFormatXml.php', + 'ApiFormatJson' => 'ApiFormatJson.php', // Modules (action=...) - should match the $apiModules list - 'ApiHelp' => "$apidir/ApiHelp.php", - 'ApiLogin' => "$apidir/ApiLogin.php", - 'ApiQuery' => "$apidir/ApiQuery.php", - - // Query items (what/list=...) - 'ApiQueryContent' => "$apidir/ApiQueryContent.php", - - 'ApiPageSet' => "$apidir/ApiPageSet.php" + 'ApiHelp' => 'ApiHelp.php', + 'ApiLogin' => 'ApiLogin.php', + 'ApiQuery' => 'ApiQuery.php', + + // Query items (meta/prop/list=...) + 'ApiQuerySiteinfo' => 'ApiQuerySiteinfo.php', + 'ApiQueryInfo' => 'ApiQueryInfo.php', + 'ApiQueryContent' => 'ApiQueryContent.php' ); /** * List of available modules: action name => module class - * The class must also be listed in the $apiAutoloadClasses array. + * The class must also be listed in the $wgApiAutoloadClasses array. */ -$apiModules = array ( +$wgApiModules = array ( 'help' => 'ApiHelp', 'login' => 'ApiLogin', 'query' => 'ApiQuery' @@ -71,9 +76,9 @@ $apiModules = array ( /** * List of available formats: format name => format class - * The class must also be listed in the $apiAutoloadClasses array. + * The class must also be listed in the $wgApiAutoloadClasses array. */ -$apiFormats = array ( +$wgApiFormats = array ( 'json' => 'ApiFormatJson', 'jsonfm' => 'ApiFormatJson', 'xml' => 'ApiFormatXml', @@ -96,22 +101,24 @@ if (!isset ($wgEnableAPI) || !$wgEnableAPI) { die(-1); } -ApiInitAutoloadClasses($apiAutoloadClasses); -$processor = new ApiMain($apiStartTime, $apiModules, $apiFormats); +ApiInitAutoloadClasses($wgApiAutoloadClasses, $wgApiDirectory); +$processor = new ApiMain($wgApiStartTime, $wgApiModules, $wgApiFormats); $processor->Execute(); wfProfileOut('api.php'); wfLogProfilingData(); exit; // Done! -function ApiInitAutoloadClasses($apiAutoloadClasses) { +function ApiInitAutoloadClasses($apiAutoloadClasses, $apiDirectory) { - // Append $apiAutoloadClasses to $wgAutoloadClasses + // Prefix each api class with the proper prefix, + // and append them to $wgAutoloadClasses global $wgAutoloadClasses; - if (isset ($wgAutoloadClasses)) { - $wgAutoloadClasses = array_merge($wgAutoloadClasses, $apiAutoloadClasses); - } else { - $wgAutoloadClasses = $apiAutoloadClasses; - } + + if (!isset ($wgAutoloadClasses)) + $wgAutoloadClasses = array(); + + foreach ($apiAutoloadClasses as $className => $classFile) + $wgAutoloadClasses[$className] = $apiDirectory . $classFile; } ?> \ No newline at end of file diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index b374d7325f..a6b4229434 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -96,6 +96,8 @@ abstract class ApiBase { $msg .= "Parameters:\n"; foreach (array_keys($params) as $paramName) { $desc = isset ($paramsDescription[$paramName]) ? $paramsDescription[$paramName] : ''; + if (is_array($desc)) + $desc = implode("\n" . str_repeat(' ', 19), $desc); $msg .= sprintf(" %-14s - %s\n", $paramName, $desc); } } diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index 46fdc24188..26d045c761 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -54,7 +54,7 @@ class ApiQuery extends ApiBase { ); private $mQueryListModules = array ( - 'allpages' => 'ApiQueryAllpages', + // 'allpages' => 'ApiQueryAllpages', // 'backlinks' => 'ApiQueryBacklinks', // 'categorymembers' => 'ApiQueryCategorymembers', // 'embeddedin' => 'ApiQueryEmbeddedin', @@ -147,13 +147,13 @@ class ApiQuery extends ApiBase { $modules = array (); if (isset($meta)) foreach ($meta as $moduleName) - $modules[] = new $this->mQueryMetaModules[$moduleName] ($this, $moduleName, $data); + $modules[] = new $this->mQueryMetaModules[$moduleName] ($this->GetMain(), $moduleName, $data); if (isset($prop)) foreach ($prop as $moduleName) - $modules[] = new $this->mQueryPropModules[$moduleName] ($this, $moduleName, $data); + $modules[] = new $this->mQueryPropModules[$moduleName] ($this->GetMain(), $moduleName, $data); if (isset($list)) foreach ($list as $moduleName) - $modules[] = new $this->mQueryListModules[$moduleName] ($this, $moduleName, $data); + $modules[] = new $this->mQueryListModules[$moduleName] ($this->GetMain(), $moduleName, $data); // Title normalizations foreach ($data->GetNormalizedTitles() as $rawTitleStr => $titleStr) { @@ -215,6 +215,42 @@ class ApiQuery extends ApiBase { ); } + /** + * Override the parent to generate help messages for all available query modules. + */ + public function MakeHelpMsg() { + + // Use parent to make default message for the query module + $msg = parent :: MakeHelpMsg(); + + $astriks = str_repeat('--- ', 8); + $msg .= "\n$astriks Query: Meta $astriks\n\n"; + $msg .= $this->MakeHelpMsgHelper($this->mQueryMetaModules, 'meta'); + $msg .= "\n$astriks Query: Prop $astriks\n\n"; + $msg .= $this->MakeHelpMsgHelper($this->mQueryPropModules, 'prop'); + $msg .= "\n$astriks Query: List $astriks\n\n"; + $msg .= $this->MakeHelpMsgHelper($this->mQueryListModules, 'list'); + + return $msg; + } + + private function MakeHelpMsgHelper($moduleList, $paramName) { + $msg = ''; + + foreach ($moduleList as $moduleName => $moduleClass) { + $msg .= "* $paramName=$moduleName *"; + $module = new $moduleClass ($this->GetMain(), $moduleName, null); + $msg2 = $module->MakeHelpMsg(); + if ($msg2 !== false) + $msg .= $msg2; + $msg .= "\n"; + if ($module->GetCanGenerate()) + $msg .= " * Can be used as a generator\n"; + } + + return $msg; + } + protected function GetParamDescription() { return array ( 'meta' => 'Which meta data to get about the site', diff --git a/includes/api/ApiQueryBase.php b/includes/api/ApiQueryBase.php index 0b5879a3ff..ff6cd5672e 100644 --- a/includes/api/ApiQueryBase.php +++ b/includes/api/ApiQueryBase.php @@ -48,7 +48,9 @@ abstract class ApiQueryBase extends ApiBase { /** * Derived classes return true when they can be used as title generators for other query modules. */ - protected static abstract function GetCanGenerate(); + public function GetCanGenerate() { + return false; + } /** * Return true if this instance is being used as a generator. diff --git a/includes/api/ApiQueryContent.php b/includes/api/ApiQueryContent.php index 491518e96b..8a2fee6611 100644 --- a/includes/api/ApiQueryContent.php +++ b/includes/api/ApiQueryContent.php @@ -34,8 +34,8 @@ class ApiQueryContent extends ApiQueryBase { /** * Constructor */ - public function __construct($main, $action) { - parent :: __construct($main); + public function __construct($main, $query, $data) { + parent :: __construct($main, $query); } public function Execute() { diff --git a/includes/api/ApiQueryInfo.php b/includes/api/ApiQueryInfo.php new file mode 100644 index 0000000000..32b095dee8 --- /dev/null +++ b/includes/api/ApiQueryInfo.php @@ -0,0 +1,70 @@ + + * + * 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 index 0000000000..16ba59ab6d --- /dev/null +++ b/includes/api/ApiQuerySiteinfo.php @@ -0,0 +1,108 @@ + + * + * 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 diff --git a/includes/api/ApiResult.php b/includes/api/ApiResult.php index f99273bd10..43eaaf19d5 100644 --- a/includes/api/ApiResult.php +++ b/includes/api/ApiResult.php @@ -40,9 +40,9 @@ class ApiResult extends ApiBase { parent :: __construct($main); $this->Reset(); } - + public function Reset() { - $this->mData = array(); + $this->mData = array (); } function GetData() { @@ -61,15 +61,12 @@ class ApiResult extends ApiBase { } else { $element = & $this->mData[$mainSection]; } - if( $multiitem ) { + if ($multiitem) { $element['_element'] = $multiitem; $element[] = $value; } else { if (is_array($value)) { $element = array_merge($element, $value); - if (!array_key_exists('*', $element)) { - $element['*'] = ''; - } } else { if (array_key_exists('*', $element)) { $element['*'] .= $value; @@ -105,7 +102,7 @@ class ApiResult extends ApiBase { } } } - + public function Execute() { $this->DieDebug("Execute() is not supported on Result object"); } -- 2.20.1