5 * Created on Sep 4, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
32 class ApiResult
extends ApiBase
{
39 public function __construct($main) {
40 parent
:: __construct($main);
44 public function Reset() {
45 $this->mData
= array ();
48 function & getData() {
53 * Add an output value to the array by name.
54 * Verifies that value with the same name has not been added before.
56 public static function addElement(& $arr, $name, $value) {
57 if ($arr === null ||
$name === null ||
$value === null ||
!is_array($arr) ||
is_array($name))
58 ApiBase
:: dieDebug('Bad parameter for ' . __FUNCTION__
);
59 if (isset ($arr[$name]))
60 ApiBase
:: dieDebug("Attempting to add element $name=$value, existing value is {$arr[$name]}");
65 * Adds the content element to the array.
66 * Use this function instead of hardcoding the '*' element.
68 public static function addContent(& $arr, $value) {
70 ApiBase
:: dieDebug('Bad parameter for ' . __FUNCTION__
);
71 ApiResult
:: addElement($arr, '*', $value);
74 // public static function makeContentElement($tag, $value) {
76 // ApiResult::addContent($result, )
80 * In case the array contains indexed values (in addition to named),
81 * all indexed values will have the given tag name.
83 public static function setIndexedTagName(& $arr, $tag) {
84 // Do not use addElement() as it is ok to call this more than once
85 if ($arr === null ||
$tag === null ||
!is_array($arr) ||
is_array($tag))
86 ApiBase
:: dieDebug('Bad parameter for ' . __FUNCTION__
);
87 $arr['_element'] = $tag;
91 * Add value to the output data at the given path.
92 * Path is an indexed array, each element specifing the branch at which to add the new value
93 * Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value
95 public function addValue($path, $name, $value) {
97 $data = & $this->getData();
100 if (is_array($path)) {
101 foreach ($path as $p) {
102 if (!isset ($data[$p]))
103 $data[$p] = array ();
107 if (!isset ($data[$path]))
108 $data[$path] = array ();
109 $data = & $data[$path];
113 ApiResult
:: addElement($data, $name, $value);
117 * Recursivelly removes any elements from the array that begin with an '_'.
118 * The content element '*' is the only special element that is left.
119 * Use this method when the entire data object gets sent to the user.
121 public function SanitizeData() {
122 ApiResult
:: SanitizeDataInt($this->mData
);
125 private static function SanitizeDataInt(& $data) {
126 foreach ($data as $key => & $value) {
127 if ($key[0] === '_') {
130 elseif ($key === '*' && $value === '') {
133 elseif (is_array($value)) {
134 ApiResult
:: SanitizeDataInt($value);
139 public function execute() {
140 $this->dieDebug('execute() is not supported on Result object');