* API: help screen now shows default and allowed parameter values
[lhc/web/wiklou.git] / includes / api / ApiFormatXml.php
1 <?php
2
3
4 /*
5 * Created on Sep 19, 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 ('ApiFormatBase.php');
30 }
31
32 class ApiFormatXml extends ApiFormatBase {
33
34 private $mRootElemName = 'api';
35
36 public function __construct($main, $format) {
37 parent :: __construct($main, $format);
38 }
39
40 public function getMimeType() {
41 return 'text/xml';
42 }
43
44 public function getNeedsRawData() {
45 return true;
46 }
47
48 public function setRootElement($rootElemName) {
49 $this->mRootElemName = $rootElemName;
50 }
51
52 public function execute() {
53 $xmlindent = null;
54 extract($this->extractRequestParams());
55
56 if ($xmlindent || $this->getIsHtml())
57 $xmlindent = -2;
58 else
59 $xmlindent = null;
60
61 $this->printText('<?xml version="1.0" encoding="utf-8"?>');
62 $this->recXmlPrint($this->mRootElemName, $this->getResultData(), $xmlindent);
63 }
64
65 /**
66 * This method takes an array and converts it into an xml.
67 * There are several noteworthy cases:
68 *
69 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
70 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
71 *
72 * If any of the array's element key is '*', then the code treats all other key->value pairs as attributes, and the value['*'] as the element's content.
73 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
74 *
75 * If neither key is found, all keys become element names, and values become element content.
76 * The method is recursive, so the same rules apply to any sub-arrays.
77 */
78 function recXmlPrint($elemName, $elemValue, $indent) {
79 if (!is_null($indent)) {
80 $indent += 2;
81 $indstr = "\n" . str_repeat(" ", $indent);
82 } else {
83 $indstr = '';
84 }
85
86 switch (gettype($elemValue)) {
87 case 'array' :
88
89 if (isset ($elemValue['*'])) {
90 $subElemContent = $elemValue['*'];
91 unset ($elemValue['*']);
92 } else {
93 $subElemContent = null;
94 }
95
96 if (isset ($elemValue['_element'])) {
97 $subElemIndName = $elemValue['_element'];
98 unset ($elemValue['_element']);
99 } else {
100 $subElemIndName = null;
101 }
102
103 $indElements = array ();
104 $subElements = array ();
105 foreach ($elemValue as $subElemId => & $subElemValue) {
106 if (gettype($subElemId) === 'integer') {
107 if (!is_array($subElemValue))
108 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has a scalar indexed value.");
109 $indElements[] = $subElemValue;
110 unset ($elemValue[$subElemId]);
111 } elseif (is_array($subElemValue)) {
112 $subElements[$subElemId] = $subElemValue;
113 unset ($elemValue[$subElemId]);
114 }
115 }
116
117 if (is_null($subElemIndName) && !empty ($indElements))
118 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value");
119
120 if (!empty ($subElements) && !empty ($indElements) && !is_null($subElemContent))
121 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
122
123 if (!is_null($subElemContent)) {
124 $this->printText($indstr . wfElement($elemName, $elemValue, $subElemContent));
125 } elseif (empty ($indElements) && empty ($subElements)) {
126 $this->printText($indstr . wfElement($elemName, $elemValue));
127 } else {
128 $this->printText($indstr . wfElement($elemName, $elemValue, null));
129
130 foreach ($subElements as $subElemId => & $subElemValue)
131 $this->recXmlPrint($subElemId, $subElemValue, $indent);
132
133 foreach ($indElements as $subElemId => & $subElemValue)
134 $this->recXmlPrint($subElemIndName, $subElemValue, $indent);
135
136 $this->printText($indstr . wfCloseElement($elemName));
137 }
138 break;
139 case 'object' :
140 // ignore
141 break;
142 default :
143 $this->printText($indstr . wfElement($elemName, null, $elemValue));
144 break;
145 }
146 }
147 protected function getDescription() {
148 return 'Output data in XML format';
149 }
150
151 protected function getAllowedParams() {
152 return array (
153 'xmlindent' => false
154 );
155 }
156
157 protected function getParamDescription() {
158 return array (
159 'xmlindent' => 'Enable XML indentation'
160 );
161 }
162
163 public function getVersion() {
164 return __CLASS__ . ': $Id$';
165 }
166 }
167 ?>