--- /dev/null
+<?php\r
+\r
+\r
+/**\r
+* API for MediaWiki 1.8+\r
+*\r
+* Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>\r
+*\r
+* This program is free software; you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation; either version 2 of the License, or\r
+* (at your option) any later version.\r
+*\r
+* This program is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License along\r
+* with this program; if not, write to the Free Software Foundation, Inc.,\r
+* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
+* http://www.gnu.org/copyleft/gpl.html\r
+*/\r
+\r
+$apiStartTime = microtime(true);\r
+\r
+/**\r
+ * List of classes and containing files.\r
+ */\r
+$apiAutoloadClasses = array (\r
+ 'ApiBase' => 'includes/api/ApiBase.php',\r
+ 'ApiMain' => 'includes/api/ApiMain.php',\r
+ 'ApiResult' => 'includes/api/ApiResult.php',\r
+\r
+ // Available modules - should match the $apiModules list\r
+ 'ApiHelp' => 'includes/api/ApiHelp.php',\r
+ 'ApiLogin' => 'includes/api/ApiLogin.php',\r
+ 'ApiQuery' => 'includes/api/ApiQuery.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
+ */ \r
+$apiModules = array (\r
+ 'help' => 'ApiHelp',\r
+ 'login' => 'ApiLogin',\r
+ 'query' => 'ApiQuery'\r
+);\r
+\r
+\r
+// Initialise common code\r
+require_once ('./includes/WebStart.php');\r
+wfProfileIn('api.php');\r
+\r
+\r
+// Verify that the API has not been disabled\r
+// The next line should be \r
+// if (isset ($wgEnableAPI) && !$wgEnableAPI) {\r
+// but will be in a safe mode until api is stabler\r
+if (!isset ($wgEnableAPI) || !$wgEnableAPI) {\r
+ echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php';\r
+ echo '<pre><b>$wgEnableAPI=true;</b></pre>';\r
+ die(-1);\r
+}\r
+\r
+\r
+ApiInitAutoloadClasses($apiAutoloadClasses);\r
+$processor = new ApiMain($apiStartTime, $apiModules);\r
+$processor->Execute();\r
+\r
+wfProfileOut('api.php');\r
+wfLogProfilingData();\r
+exit; // Done!\r
+\r
+\r
+function ApiInitAutoloadClasses($apiAutoloadClasses) {\r
+\r
+ // Append $apiAutoloadClasses to $wgAutoloadClasses\r
+ global $wgAutoloadClasses;\r
+ if (isset ($wgAutoloadClasses)) {\r
+ $wgAutoloadClasses = array_merge($wgAutoloadClasses, $apiAutoloadClasses);\r
+ } else {\r
+ $wgAutoloadClasses = $apiAutoloadClasses;\r
+ }\r
+}\r
+?>\r
--- /dev/null
+<?php\r
+\r
+\r
+/*\r
+ * Created on Sep 5, 2006\r
+ *\r
+ * API for MediaWiki 1.8+\r
+ *\r
+ * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License along\r
+ * with this program; if not, write to the Free Software Foundation, Inc.,\r
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
+ * http://www.gnu.org/copyleft/gpl.html\r
+ */\r
+\r
+// Multi-valued enums, limit the values user can supply for the parameter\r
+define('GN_ENUM_DFLT', 0);\r
+define('GN_ENUM_ISMULTI', 1);\r
+define('GN_ENUM_CHOICES', 2);\r
+\r
+abstract class ApiBase {\r
+\r
+ private $mMainModule;\r
+\r
+ /**\r
+ * Constructor\r
+ */\r
+ public function __construct($mainModule) {\r
+ $this->mMainModule = $mainModule;\r
+ }\r
+\r
+ /**\r
+ * Executes this module\r
+ */\r
+ abstract function Execute();\r
+\r
+ /**\r
+ * Get main module\r
+ */\r
+ public function GetMain() {\r
+ return $this->mMainModule;\r
+ }\r
+\r
+ /**\r
+ * If this module's $this is the same as $this->mMainModule, its the root, otherwise no\r
+ */\r
+ public function IsMain() {\r
+ return $this === $this->mMainModule;\r
+ }\r
+\r
+ /**\r
+ * Get result object\r
+ */\r
+ public function GetResult() {\r
+ // Main module has GetResult() method overriden\r
+ // Safety - avoid infinite loop:\r
+ if ($this->IsMain())\r
+ $this->DieDebug(__METHOD__.' base method was called on main module. ');\r
+ return $this->GetMain()->GetResult();\r
+ }\r
+\r
+ /**\r
+ * Returns an array of allowed parameters (keys) => default value for that parameter\r
+ */\r
+ protected function GetAllowedParams() {\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Returns the description string for this module\r
+ */\r
+ protected function GetDescription() {\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Returns usage examples for this module. Return null if no examples are available.\r
+ */\r
+ protected function GetExamples() {\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Returns the description string for the given parameter.\r
+ */\r
+ protected function GetParamDescription($paramName) {\r
+ return '';\r
+ }\r
+\r
+ /**\r
+ * Generates help message for this module, or false if there is no description\r
+ */\r
+ public function MakeHelpMsg() {\r
+\r
+ $msg = $this->GetDescription();\r
+\r
+ if ($msg !== false) {\r
+ $msg .= "\n";\r
+\r
+ // Parameters\r
+ $params = $this->GetAllowedParams();\r
+ if ($params !== false) {\r
+ $msg .= "Supported Parameters:\n";\r
+ foreach (array_keys($params) as $paramName) {\r
+ $msg .= sprintf(" %-14s - %s\n", $paramName, $this->GetParamDescription($paramName));\r
+ }\r
+ }\r
+\r
+ // Examples\r
+ $examples = $this->GetExamples();\r
+ if ($examples !== false) {\r
+ $msg .= "Examples:\n ";\r
+ $msg .= implode("\n ", $examples) . "\n";\r
+ }\r
+ }\r
+\r
+ return $msg;\r
+ }\r
+\r
+ /**\r
+ * Using GetAllowedParams(), makes an array of the values provided by the user,\r
+ * with key being the name of the variable, and value - validated value from user or default.\r
+ * This method can be used to generate local variables using extract().\r
+ */\r
+ public function ExtractRequestParams() {\r
+ global $wgRequest;\r
+\r
+ $params = $this->GetAllowedParams();\r
+ $results = array ();\r
+\r
+ foreach ($params as $param => $dflt) {\r
+ switch (gettype($dflt)) {\r
+ case 'NULL' :\r
+ case 'string' :\r
+ $result = $wgRequest->getVal($param, $dflt);\r
+ break;\r
+ case 'integer' :\r
+ $result = $wgRequest->getInt($param, $dflt);\r
+ break;\r
+ case 'boolean' :\r
+ // Having a default value of 'true' is pointless\r
+ $result = $wgRequest->getCheck($param);\r
+ break;\r
+ case 'array' :\r
+ if (count($dflt) != 3)\r
+ $this->DieDebug("In '$param', the default enum must have 3 parts - default, allowmultiple, and array of values " . gettype($dflt));\r
+ $values = $wgRequest->getVal($param, $dflt[GN_ENUM_DFLT]);\r
+ $result = $this->ParseMultiValue($param, $values, $dflt[GN_ENUM_ISMULTI], $dflt[GN_ENUM_CHOICES]);\r
+ break;\r
+ default :\r
+ $this->DieDebug("In '$param', unprocessed type " . gettype($dflt));\r
+ }\r
+ $results[$param] = $result;\r
+ }\r
+\r
+ return $results;\r
+ }\r
+\r
+ /**\r
+ * Return an array of values that were given in a "a|b|c" notation, after it validates them against the list allowed values.\r
+ */\r
+ protected function ParseMultiValue($valueName, $values, $allowMultiple, $allowedValues) {\r
+ $valuesList = explode('|', $values);\r
+ if (!$allowMultiple && count($valuesList) != 1)\r
+ $this->DieUsage("Only one value is allowed: '" . implode("', '", $allowedValues) . "' for parameter '$valueName'", "multival_$valueName");\r
+ $unknownValues = array_diff($valuesList, $allowedValues);\r
+ if ($unknownValues) {\r
+ $this->DieUsage("Unrecognised value" . (count($unknownValues) > 1 ? "s '" : " '") . implode("', '", $unknownValues) . "' for parameter '$valueName'", "unknown_$valueName");\r
+ }\r
+\r
+ return $allowMultiple ? $valuesList : $valuesList[0];\r
+ }\r
+\r
+ /**\r
+ * Call main module's error handler \r
+ */\r
+ public function DieUsage($description, $errorCode, $httpRespCode = 0) {\r
+ $this->GetMain()->MainDieUsage($description, $errorCode, $httpRespCode);\r
+ }\r
+\r
+ protected function DieDebug($message) {\r
+ wfDebugDieBacktrace("Internal error in '{get_class($this)}': $message");\r
+ }\r
+}\r
+?>\r
--- /dev/null
+<?php\r
+\r
+\r
+/*\r
+ * Created on Sep 6, 2006\r
+ *\r
+ * API for MediaWiki 1.8+\r
+ *\r
+ * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License along\r
+ * with this program; if not, write to the Free Software Foundation, Inc.,\r
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
+ * http://www.gnu.org/copyleft/gpl.html\r
+ */\r
+\r
+if (!defined('MEDIAWIKI')) {\r
+ // Eclipse helper - will be ignored in production\r
+ require_once ("ApiBase.php");\r
+}\r
+\r
+class ApiHelp extends ApiBase {\r
+\r
+ /**\r
+ * Constructor\r
+ */\r
+ public function __construct($main, $action) {\r
+ parent :: __construct($main);\r
+ }\r
+\r
+ /**\r
+ * Stub module for displaying help when no parameters are given\r
+ */\r
+ public function Execute() {\r
+ $this->DieUsage('', 'help');\r
+ }\r
+}\r
+?>
\ No newline at end of file
--- /dev/null
+<?php\r
+\r
+\r
+/*\r
+ * Created on Sep 4, 2006\r
+ *\r
+ * API for MediaWiki 1.8+\r
+ *\r
+ * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License along\r
+ * with this program; if not, write to the Free Software Foundation, Inc.,\r
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
+ * http://www.gnu.org/copyleft/gpl.html\r
+ */\r
+\r
+if (!defined('MEDIAWIKI')) {\r
+ // Eclipse helper - will be ignored in production\r
+ require_once ("ApiBase.php");\r
+}\r
+\r
+class ApiMain extends ApiBase {\r
+\r
+ private $mModules, $mModuleNames, $mApiStartTime, $mResult;\r
+\r
+ /**\r
+ * Constructor\r
+ * $apiStartTime - time of the originating call for profiling purposes\r
+ * $modules - an array of actions (keys) and classes that handle them (values) \r
+ */\r
+ public function __construct($apiStartTime, $modules) {\r
+ // Special handling for the main module: $parent === $this\r
+ parent :: __construct($this);\r
+\r
+ $this->mModules = $modules;\r
+ $this->mModuleNames = array_keys($modules);\r
+ $this->mApiStartTime = $apiStartTime;\r
+ $this->mResult = new ApiResult($this);\r
+ }\r
+\r
+ public function GetResult() {\r
+ return $this->mResult;\r
+ }\r
+\r
+ protected function GetAllowedParams() {\r
+ return array (\r
+ 'format' => 'xmlfm',\r
+ 'action' => array (\r
+ GN_ENUM_DFLT => 'help',\r
+ GN_ENUM_ISMULTI => false,\r
+ GN_ENUM_CHOICES => $this->mModuleNames\r
+ )\r
+ );\r
+ }\r
+\r
+ public function Execute() {\r
+ $action = $format = null;\r
+ extract($this->ExtractRequestParams());\r
+\r
+ // Instantiate and execute module requested by the user\r
+ $module = new $this->mModules[$action] ($this, $action);\r
+ $module->Execute();\r
+ }\r
+\r
+ protected function GetDescription() {\r
+ return "This API allows programs to access various functions of MediaWiki software.";\r
+ } \r
+ \r
+ protected function GetParamDescription($paramName) {\r
+ switch($paramName) {\r
+ case 'format': return "The format of the output";\r
+ case 'action': return "What action you would like to perform";\r
+ default: return parent :: GetParamDescription($paramName);\r
+ }\r
+ }\r
+ \r
+ public function MainDieUsage($description, $errorCode, $httpRespCode = 0) {\r
+ $this->mResult->Reset();\r
+ $this->mResult->addMessage('error', null, $errorCode);\r
+ if ($httpRespCode === 0)\r
+ header($errorCode, true);\r
+ else\r
+ header($errorCode, true, $httpRespCode);\r
+\r
+ $this->mResult->addMessage('usage', null, $this->MakeHelpMsg());\r
+ \r
+ var_export($this->mResult->GetData());\r
+ }\r
+}\r
+?>\r
--- /dev/null
+<?php\r
+\r
+\r
+/*\r
+ * Created on Sep 7, 2006\r
+ *\r
+ * API for MediaWiki 1.8+\r
+ *\r
+ * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License along\r
+ * with this program; if not, write to the Free Software Foundation, Inc.,\r
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
+ * http://www.gnu.org/copyleft/gpl.html\r
+ */\r
+\r
+if (!defined('MEDIAWIKI')) {\r
+ // Eclipse helper - will be ignored in production\r
+ require_once ("ApiBase.php");\r
+}\r
+\r
+class ApiQuery extends ApiBase {\r
+\r
+ private static $apiAutoloadClasses_Query = array (\r
+ 'ApiQueryContent' => 'includes/api/ApiQueryContent.php',\r
+ \r
+ );\r
+\r
+ private static $sQueryModules = array (\r
+ 'content' => 'ApiQueryContent'\r
+ );\r
+\r
+ /**\r
+ * Constructor\r
+ */\r
+ public function __construct($main, $action) {\r
+ parent :: __construct($main);\r
+ ApiInitAutoloadClasses($this->apiAutoloadClasses_Query);\r
+ $this->mModuleNames = array_keys($this->sQueryModules);\r
+ $this->mDb =& wfGetDB( DB_SLAVE );\r
+ }\r
+ \r
+ public function GetDB() {\r
+ return $this->mDb;\r
+ }\r
+ \r
+\r
+ public function Execute() {\r
+\r
+ }\r
+\r
+ /**\r
+ * Returns an array of allowed parameters (keys) => default value for that parameter\r
+ */\r
+ protected function GetAllowedParams() {\r
+ return array (\r
+ 'what' => 'default',\r
+ 'enumparam' => array (\r
+ GN_ENUM_DFLT => null,\r
+ GN_ENUM_ISMULTI => true,\r
+ GN_ENUM_CHOICES => $this->mModuleNames\r
+ )\r
+ );\r
+ }\r
+\r
+ /**\r
+ * Returns the description string for this module\r
+ */\r
+ protected function GetDescription() {\r
+ return 'module a';\r
+ }\r
+\r
+ /**\r
+ * Returns usage examples for this module. Return null if no examples are available.\r
+ */\r
+ protected function GetExamples() {\r
+ return array (\r
+ 'http://...'\r
+ );\r
+ }\r
+\r
+ /**\r
+ * Returns the description string for the given parameter.\r
+ */\r
+ protected function GetParamDescription($paramName) {\r
+ switch ($paramName) {\r
+ case 'param' :\r
+ return 'description';\r
+ default :\r
+ return parent :: GetParamDescription($paramName);\r
+ }\r
+ }\r
+}\r
+?>
\ No newline at end of file
--- /dev/null
+<?php\r
+\r
+\r
+/*\r
+ * Created on Sep 7, 2006\r
+ *\r
+ * API for MediaWiki 1.8+\r
+ *\r
+ * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License along\r
+ * with this program; if not, write to the Free Software Foundation, Inc.,\r
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
+ * http://www.gnu.org/copyleft/gpl.html\r
+ */\r
+\r
+if (!defined('MEDIAWIKI')) {\r
+ // Eclipse helper - will be ignored in production\r
+ require_once ("ApiBase.php");\r
+}\r
+\r
+abstract class ApiQueryBase extends ApiBase {\r
+\r
+ private $mQueryModule;\r
+ \r
+ /**\r
+ * Constructor\r
+ */\r
+ public function __construct($main, $query) {\r
+ parent :: __construct($main);\r
+ $this->mQueryModule = $query;\r
+ }\r
+ \r
+ public function GetQuery() {\r
+ return $this->mQueryModule;\r
+ }\r
+}\r
+?>
\ No newline at end of file
--- /dev/null
+<?php\r
+\r
+\r
+/*\r
+ * Created on Sep 7, 2006\r
+ *\r
+ * API for MediaWiki 1.8+\r
+ *\r
+ * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License along\r
+ * with this program; if not, write to the Free Software Foundation, Inc.,\r
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
+ * http://www.gnu.org/copyleft/gpl.html\r
+ */\r
+\r
+if (!defined('MEDIAWIKI')) {\r
+ // Eclipse helper - will be ignored in production\r
+ require_once ("ApiQueryBase.php");\r
+}\r
+\r
+class ApiQueryContent extends ApiQueryBase {\r
+\r
+ /**\r
+ * Constructor\r
+ */\r
+ public function __construct($main, $action) {\r
+ parent :: __construct($main);\r
+ }\r
+\r
+ public function Execute() {\r
+ \r
+ }\r
+\r
+ /**\r
+ * Returns an array of allowed parameters (keys) => default value for that parameter\r
+ */\r
+ protected function GetAllowedParams() {\r
+ return array (\r
+ 'param' => 'default',\r
+ 'enumparam' => array (\r
+ GN_ENUM_DFLT => 'default',\r
+ GN_ENUM_ISMULTI => false,\r
+ GN_ENUM_CHOICES => array (\r
+ 'a',\r
+ 'b'\r
+ )\r
+ )\r
+ );\r
+ }\r
+\r
+ /**\r
+ * Returns the description string for this module\r
+ */\r
+ protected function GetDescription() {\r
+ return 'module a';\r
+ }\r
+\r
+ /**\r
+ * Returns usage examples for this module. Return null if no examples are available.\r
+ */\r
+ protected function GetExamples() {\r
+ return array (\r
+ 'http://...'\r
+ );\r
+ }\r
+\r
+ /**\r
+ * Returns the description string for the given parameter.\r
+ */\r
+ protected function GetParamDescription($paramName) {\r
+ switch ($paramName) {\r
+ case 'param' :\r
+ return 'description';\r
+ default :\r
+ return parent :: GetParamDescription($paramName);\r
+ }\r
+ }\r
+}\r
+?>
\ No newline at end of file
--- /dev/null
+<?php\r
+\r
+\r
+/*\r
+ * Created on Sep 4, 2006\r
+ *\r
+ * API for MediaWiki 1.8+\r
+ *\r
+ * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License along\r
+ * with this program; if not, write to the Free Software Foundation, Inc.,\r
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
+ * http://www.gnu.org/copyleft/gpl.html\r
+ */\r
+\r
+if (!defined('MEDIAWIKI')) {\r
+ // Eclipse helper - will be ignored in production\r
+ require_once ("ApiBase.php");\r
+}\r
+\r
+class ApiResult extends ApiBase {\r
+\r
+ private $mData;\r
+\r
+ /**\r
+ * Constructor\r
+ */\r
+ public function __construct($main) {\r
+ parent :: __construct($main);\r
+ $this->Reset();\r
+ }\r
+ \r
+ public function Reset() {\r
+ $this->mData = array();\r
+ }\r
+\r
+ function GetData() {\r
+ return $this->mData;\r
+ }\r
+\r
+ /* function addPage($title)\r
+ {\r
+ if (!isset($this->mPages))\r
+ $this->mPages &= $this->mData['pages'];\r
+ }\r
+ */\r
+ \r
+ function AddMessage($mainSection, $subSection, $value, $preserveXmlSpacing = false) {\r
+ if (!array_key_exists($mainSection, $this->mData)) {\r
+ $this->mData[$mainSection] = array ();\r
+ }\r
+ if ($subSection !== null) {\r
+ if (!array_key_exists($subSection, $this->mData[$mainSection])) {\r
+ $this->mData[$mainSection][$subSection] = array ();\r
+ }\r
+ $element = & $this->mData[$mainSection][$subSection];\r
+ } else {\r
+ $element = & $this->mData[$mainSection];\r
+ }\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
+ } else {\r
+ $element['*'] = $value;\r
+ }\r
+ if ($preserveXmlSpacing) {\r
+ $element['xml:space'] = 'preserve';\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Recursivelly removes any elements from the array that begin with an '_'.\r
+ * The content element '*' is the only special element that is left.\r
+ * Use this method when the entire data object gets sent to the user.\r
+ */\r
+ public function SanitizeData() {\r
+ ApiResult :: SanitizeDataInt($this->mData);\r
+ }\r
+\r
+ private static function SanitizeDataInt(& $data) {\r
+ foreach ($data as $key => & $value) {\r
+ if ($key[0] === '_') {\r
+ unset ($data[$key]);\r
+ }\r
+ elseif ($key === '*' && $value === '') {\r
+ unset ($data[$key]);\r
+ }\r
+ elseif (is_array($value)) {\r
+ ApiResult :: SanitizeDataInt($value);\r
+ }\r
+ }\r
+ }\r
+ \r
+ public function Execute() {\r
+ $this->DieDebug("Execute() is not supported on Result object");\r
+ }\r
+}\r
+?>\r