4 * Created on Sep 4, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiBase.php');
34 class ApiResult
extends ApiBase
{
36 private $mData, $mIsRawMode;
41 public function __construct($main) {
42 parent
:: __construct($main, 'result');
43 $this->mIsRawMode
= false;
47 public function reset() {
48 $this->mData
= array ();
52 * Call this function when special elements such as '_element'
53 * are needed by the formatter, for example in XML printing.
55 public function setRawMode() {
56 $this->mIsRawMode
= true;
59 public function getIsRawMode() {
60 return $this->mIsRawMode
;
63 public function & getData() {
68 * Add an output value to the array by name.
69 * Verifies that value with the same name has not been added before.
71 public static function setElement(& $arr, $name, $value) {
72 if ($arr === null ||
$name === null ||
$value === null ||
!is_array($arr) ||
is_array($name))
73 ApiBase
:: dieDebug(__METHOD__
, 'Bad parameter');
75 if (!isset ($arr[$name])) {
78 elseif (is_array($arr[$name]) && is_array($value)) {
79 $merged = array_intersect_key($arr[$name], $value);
81 $arr[$name] +
= $value;
83 ApiBase
:: dieDebug(__METHOD__
, "Attempting to merge element $name");
85 ApiBase
:: dieDebug(__METHOD__
, "Attempting to add element $name=$value, existing value is {$arr[$name]}");
89 * Adds the content element to the array.
90 * Use this function instead of hardcoding the '*' element.
91 * @param string $subElemName when present, content element is created as a sub item of the arr.
92 * Use this parameter to create elements in format <elem>text</elem> without attributes
94 public static function setContent(& $arr, $value, $subElemName = null) {
96 ApiBase
:: dieDebug(__METHOD__
, 'Bad parameter');
97 if (is_null($subElemName)) {
98 ApiResult
:: setElement($arr, '*', $value);
100 if (!isset ($arr[$subElemName]))
101 $arr[$subElemName] = array ();
102 ApiResult
:: setElement($arr[$subElemName], '*', $value);
106 // public static function makeContentElement($tag, $value) {
107 // $result = array();
108 // ApiResult::setContent($result, )
112 * In case the array contains indexed values (in addition to named),
113 * all indexed values will have the given tag name.
115 public function setIndexedTagName(& $arr, $tag) {
116 // In raw mode, add the '_element', otherwise just ignore
117 if (!$this->getIsRawMode())
119 if ($arr === null ||
$tag === null ||
!is_array($arr) ||
is_array($tag))
120 ApiBase
:: dieDebug(__METHOD__
, 'Bad parameter');
121 // Do not use setElement() as it is ok to call this more than once
122 $arr['_element'] = $tag;
126 * Add value to the output data at the given path.
127 * Path is an indexed array, each element specifing the branch at which to add the new value
128 * Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value
130 public function addValue($path, $name, $value) {
132 $data = & $this->getData();
134 if (!is_null($path)) {
135 if (is_array($path)) {
136 foreach ($path as $p) {
137 if (!isset ($data[$p]))
138 $data[$p] = array ();
142 if (!isset ($data[$path]))
143 $data[$path] = array ();
144 $data = & $data[$path];
148 ApiResult
:: setElement($data, $name, $value);
151 public function execute() {
152 ApiBase
:: dieDebug(__METHOD__
, 'execute() is not supported on Result object');
155 public function getVersion() {
156 return __CLASS__
. ': $Id$';