* API: A new ApiPageSet class to retrieve page data and resolve redirects.
[lhc/web/wiklou.git] / includes / api / ApiResult.php
1 <?php
2
3
4 /*
5 * Created on Sep 4, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
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.
15 *
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.
20 *
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
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ("ApiBase.php");
30 }
31
32 class ApiResult extends ApiBase {
33
34 private $mData;
35
36 /**
37 * Constructor
38 */
39 public function __construct($main) {
40 parent :: __construct($main);
41 $this->Reset();
42 }
43
44 public function Reset() {
45 $this->mData = array();
46 }
47
48 function GetData() {
49 return $this->mData;
50 }
51
52 function AddMessage($mainSection, $subSection, $value, $multiitem = false, $preserveXmlSpacing = false) {
53 if (!array_key_exists($mainSection, $this->mData)) {
54 $this->mData[$mainSection] = array ();
55 }
56 if ($subSection !== null) {
57 if (!array_key_exists($subSection, $this->mData[$mainSection])) {
58 $this->mData[$mainSection][$subSection] = array ();
59 }
60 $element = & $this->mData[$mainSection][$subSection];
61 } else {
62 $element = & $this->mData[$mainSection];
63 }
64 if( $multiitem ) {
65 $element['_element'] = $multiitem;
66 $element[] = $value;
67 } else {
68 if (is_array($value)) {
69 $element = array_merge($element, $value);
70 if (!array_key_exists('*', $element)) {
71 $element['*'] = '';
72 }
73 } else {
74 if (array_key_exists('*', $element)) {
75 $element['*'] .= $value;
76 } else {
77 $element['*'] = $value;
78 }
79 if ($preserveXmlSpacing) {
80 $element['xml:space'] = 'preserve';
81 }
82 }
83 }
84 }
85
86 /**
87 * Recursivelly removes any elements from the array that begin with an '_'.
88 * The content element '*' is the only special element that is left.
89 * Use this method when the entire data object gets sent to the user.
90 */
91 public function SanitizeData() {
92 ApiResult :: SanitizeDataInt($this->mData);
93 }
94
95 private static function SanitizeDataInt(& $data) {
96 foreach ($data as $key => & $value) {
97 if ($key[0] === '_') {
98 unset ($data[$key]);
99 }
100 elseif ($key === '*' && $value === '') {
101 unset ($data[$key]);
102 }
103 elseif (is_array($value)) {
104 ApiResult :: SanitizeDataInt($value);
105 }
106 }
107 }
108
109 public function Execute() {
110 $this->DieDebug("Execute() is not supported on Result object");
111 }
112 }
113 ?>