2006-10-01 04:38:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Created on Sep 4, 2006
|
|
|
|
|
*
|
|
|
|
|
* API for MediaWiki 1.8+
|
|
|
|
|
*
|
2007-05-20 23:31:44 +00:00
|
|
|
* Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
|
2006-10-01 04:38:31 +00:00
|
|
|
*
|
|
|
|
|
* 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 ('ApiBase.php');
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
2007-05-20 10:08:40 +00:00
|
|
|
* This class represents the result of the API operations.
|
|
|
|
|
* It simply wraps a nested array() structure, adding some functions to simplify array's modifications.
|
|
|
|
|
* As various modules execute, they add different pieces of information to this result,
|
|
|
|
|
* structuring it as it will be given to the client.
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
2007-05-20 10:08:40 +00:00
|
|
|
* Each subarray may either be a dictionary - key-value pairs with unique keys,
|
|
|
|
|
* or lists, where the items are added using $data[] = $value notation.
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
2007-05-20 10:08:40 +00:00
|
|
|
* There are two special key values that change how XML output is generated:
|
|
|
|
|
* '_element' This key sets the tag name for the rest of the elements in the current array.
|
|
|
|
|
* It is only inserted if the formatter returned true for getNeedsRawData()
|
|
|
|
|
* '*' This key has special meaning only to the XML formatter, and is outputed as is
|
2008-04-14 07:45:50 +00:00
|
|
|
* for all others. In XML it becomes the content of the current element.
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2007-04-20 08:55:14 +00:00
|
|
|
*/
|
2006-10-01 04:38:31 +00:00
|
|
|
class ApiResult extends ApiBase {
|
|
|
|
|
|
2006-10-18 05:35:24 +00:00
|
|
|
private $mData, $mIsRawMode;
|
2006-10-01 04:38:31 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
*/
|
|
|
|
|
public function __construct($main) {
|
2006-10-03 05:41:55 +00:00
|
|
|
parent :: __construct($main, 'result');
|
2006-10-18 05:35:24 +00:00
|
|
|
$this->mIsRawMode = false;
|
2006-10-18 05:27:43 +00:00
|
|
|
$this->reset();
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* Clear the current result data.
|
|
|
|
|
*/
|
2006-10-18 05:27:43 +00:00
|
|
|
public function reset() {
|
2006-10-01 04:38:31 +00:00
|
|
|
$this->mData = array ();
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-10-18 05:27:43 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Call this function when special elements such as '_element'
|
|
|
|
|
* are needed by the formatter, for example in XML printing.
|
2006-10-18 05:27:43 +00:00
|
|
|
*/
|
|
|
|
|
public function setRawMode() {
|
2006-10-18 05:35:24 +00:00
|
|
|
$this->mIsRawMode = true;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if the result is being created for the formatter that requested raw data.
|
|
|
|
|
*/
|
2006-10-18 05:35:24 +00:00
|
|
|
public function getIsRawMode() {
|
|
|
|
|
return $this->mIsRawMode;
|
2006-10-18 05:27:43 +00:00
|
|
|
}
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* Get result's internal data array
|
|
|
|
|
*/
|
2006-10-18 05:35:24 +00:00
|
|
|
public function & getData() {
|
2006-10-01 04:38:31 +00:00
|
|
|
return $this->mData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add an output value to the array by name.
|
|
|
|
|
* Verifies that value with the same name has not been added before.
|
|
|
|
|
*/
|
2006-10-01 20:17:16 +00:00
|
|
|
public static function setElement(& $arr, $name, $value) {
|
2006-10-01 04:38:31 +00:00
|
|
|
if ($arr === null || $name === null || $value === null || !is_array($arr) || is_array($name))
|
2006-10-01 20:17:16 +00:00
|
|
|
ApiBase :: dieDebug(__METHOD__, 'Bad parameter');
|
|
|
|
|
|
|
|
|
|
if (!isset ($arr[$name])) {
|
|
|
|
|
$arr[$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
elseif (is_array($arr[$name]) && is_array($value)) {
|
|
|
|
|
$merged = array_intersect_key($arr[$name], $value);
|
2008-10-25 14:04:43 +00:00
|
|
|
if (!count($merged))
|
2006-10-01 20:17:16 +00:00
|
|
|
$arr[$name] += $value;
|
|
|
|
|
else
|
|
|
|
|
ApiBase :: dieDebug(__METHOD__, "Attempting to merge element $name");
|
|
|
|
|
} else
|
|
|
|
|
ApiBase :: dieDebug(__METHOD__, "Attempting to add element $name=$value, existing value is {$arr[$name]}");
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds the content element to the array.
|
|
|
|
|
* Use this function instead of hardcoding the '*' element.
|
2006-10-16 00:08:03 +00:00
|
|
|
* @param string $subElemName when present, content element is created as a sub item of the arr.
|
|
|
|
|
* Use this parameter to create elements in format <elem>text</elem> without attributes
|
2006-10-01 04:38:31 +00:00
|
|
|
*/
|
2006-10-16 00:08:03 +00:00
|
|
|
public static function setContent(& $arr, $value, $subElemName = null) {
|
2006-10-01 04:38:31 +00:00
|
|
|
if (is_array($value))
|
2006-10-01 20:17:16 +00:00
|
|
|
ApiBase :: dieDebug(__METHOD__, 'Bad parameter');
|
2006-10-16 00:08:03 +00:00
|
|
|
if (is_null($subElemName)) {
|
|
|
|
|
ApiResult :: setElement($arr, '*', $value);
|
|
|
|
|
} else {
|
|
|
|
|
if (!isset ($arr[$subElemName]))
|
|
|
|
|
$arr[$subElemName] = array ();
|
|
|
|
|
ApiResult :: setElement($arr[$subElemName], '*', $value);
|
|
|
|
|
}
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* In case the array contains indexed values (in addition to named),
|
|
|
|
|
* all indexed values will have the given tag name.
|
|
|
|
|
*/
|
2006-10-18 05:27:43 +00:00
|
|
|
public function setIndexedTagName(& $arr, $tag) {
|
|
|
|
|
// In raw mode, add the '_element', otherwise just ignore
|
2006-10-18 05:35:24 +00:00
|
|
|
if (!$this->getIsRawMode())
|
2006-10-18 05:27:43 +00:00
|
|
|
return;
|
2006-10-01 04:38:31 +00:00
|
|
|
if ($arr === null || $tag === null || !is_array($arr) || is_array($tag))
|
2006-10-01 20:17:16 +00:00
|
|
|
ApiBase :: dieDebug(__METHOD__, 'Bad parameter');
|
2006-10-18 05:27:43 +00:00
|
|
|
// Do not use setElement() as it is ok to call this more than once
|
2006-10-01 04:38:31 +00:00
|
|
|
$arr['_element'] = $tag;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-10-20 18:27:39 +00:00
|
|
|
/**
|
|
|
|
|
* Calls setIndexedTagName() on $arr and each sub-array
|
|
|
|
|
*/
|
|
|
|
|
public function setIndexedTagName_recursive(&$arr, $tag)
|
|
|
|
|
{
|
|
|
|
|
if(!is_array($arr))
|
|
|
|
|
return;
|
2008-04-26 14:40:54 +00:00
|
|
|
foreach($arr as &$a)
|
2007-10-20 18:27:39 +00:00
|
|
|
{
|
|
|
|
|
if(!is_array($a))
|
|
|
|
|
continue;
|
|
|
|
|
$this->setIndexedTagName($a, $tag);
|
|
|
|
|
$this->setIndexedTagName_recursive($a, $tag);
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-10-01 04:38:31 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add value to the output data at the given path.
|
|
|
|
|
* Path is an indexed array, each element specifing the branch at which to add the new value
|
2007-05-19 20:26:08 +00:00
|
|
|
* Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value
|
2008-04-14 07:45:50 +00:00
|
|
|
* If $name is empty, the $value is added as a next list element data[] = $value
|
2006-10-01 04:38:31 +00:00
|
|
|
*/
|
|
|
|
|
public function addValue($path, $name, $value) {
|
|
|
|
|
|
|
|
|
|
$data = & $this->getData();
|
|
|
|
|
|
2006-10-15 07:43:52 +00:00
|
|
|
if (!is_null($path)) {
|
2006-10-01 04:38:31 +00:00
|
|
|
if (is_array($path)) {
|
|
|
|
|
foreach ($path as $p) {
|
|
|
|
|
if (!isset ($data[$p]))
|
|
|
|
|
$data[$p] = array ();
|
|
|
|
|
$data = & $data[$p];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (!isset ($data[$path]))
|
|
|
|
|
$data[$path] = array ();
|
|
|
|
|
$data = & $data[$path];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-25 14:04:43 +00:00
|
|
|
if (!$name)
|
2007-05-19 20:26:08 +00:00
|
|
|
$data[] = $value; // Add list element
|
|
|
|
|
else
|
|
|
|
|
ApiResult :: setElement($data, $name, $value); // Add named element
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-14 21:22:00 +00:00
|
|
|
/**
|
|
|
|
|
* Ensure all values in this result are valid UTF-8.
|
|
|
|
|
*/
|
|
|
|
|
public function cleanUpUTF8()
|
|
|
|
|
{
|
|
|
|
|
$data = & $this->getData();
|
2009-01-21 09:26:48 +00:00
|
|
|
array_walk_recursive($data, array('ApiResult', 'cleanUp_helper'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static function cleanUp_helper($s)
|
|
|
|
|
{
|
|
|
|
|
if(!is_string($s))
|
|
|
|
|
return $s;
|
|
|
|
|
return UtfNormal::cleanUp($s);
|
2009-01-14 21:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
2006-10-01 04:38:31 +00:00
|
|
|
public function execute() {
|
2006-10-01 20:17:16 +00:00
|
|
|
ApiBase :: dieDebug(__METHOD__, 'execute() is not supported on Result object');
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
2006-10-01 21:20:55 +00:00
|
|
|
|
|
|
|
|
public function getVersion() {
|
|
|
|
|
return __CLASS__ . ': $Id$';
|
|
|
|
|
}
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
2007-06-29 01:19:14 +00:00
|
|
|
|
2007-10-12 04:04:12 +00:00
|
|
|
/* For compatibility with PHP versions < 5.1.0, define our own array_intersect_key function. */
|
|
|
|
|
if (!function_exists('array_intersect_key')) {
|
|
|
|
|
function array_intersect_key($isec, $keys) {
|
|
|
|
|
$argc = func_num_args();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-10-12 04:04:12 +00:00
|
|
|
if ($argc > 2) {
|
2008-10-25 14:04:43 +00:00
|
|
|
for ($i = 1; $isec && $i < $argc; $i++) {
|
2007-10-12 04:04:12 +00:00
|
|
|
$arr = func_get_arg($i);
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-10-12 04:04:12 +00:00
|
|
|
foreach (array_keys($isec) as $key) {
|
2008-04-14 07:45:50 +00:00
|
|
|
if (!isset($arr[$key]))
|
2007-10-12 04:04:12 +00:00
|
|
|
unset($isec[$key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-10-12 04:04:12 +00:00
|
|
|
return $isec;
|
|
|
|
|
} else {
|
|
|
|
|
$res = array();
|
|
|
|
|
foreach (array_keys($isec) as $key) {
|
|
|
|
|
if (isset($keys[$key]))
|
|
|
|
|
$res[$key] = $isec[$key];
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-10-12 04:04:12 +00:00
|
|
|
return $res;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|