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