Add "check" parameter to action=email
[lhc/web/wiklou.git] / includes / api / ApiFormatXml.php
1 <?php
2
3 /*
4 * Created on Sep 19, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiFormatBase.php');
29 }
30
31 /**
32 * @ingroup API
33 */
34 class ApiFormatXml extends ApiFormatBase {
35
36 private $mRootElemName = 'api';
37 private $mDoubleQuote = false;
38
39 public function __construct($main, $format) {
40 parent :: __construct($main, $format);
41 }
42
43 public function getMimeType() {
44 return 'text/xml';
45 }
46
47 public function getNeedsRawData() {
48 return true;
49 }
50
51 public function setRootElement($rootElemName) {
52 $this->mRootElemName = $rootElemName;
53 }
54
55 public function execute() {
56 $params = $this->extractRequestParams();
57 $this->mDoubleQuote = $params['xmldoublequote'];
58
59 $this->printText('<?xml version="1.0"?>');
60 $this->recXmlPrint($this->mRootElemName, $this->getResultData(), $this->getIsHtml() ? -2 : null);
61 }
62
63 /**
64 * This method takes an array and converts it to XML.
65 * There are several noteworthy cases:
66 *
67 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
68 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
69 *
70 * 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.
71 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
72 *
73 * If neither key is found, all keys become element names, and values become element content.
74 * The method is recursive, so the same rules apply to any sub-arrays.
75 */
76 function recXmlPrint($elemName, $elemValue, $indent) {
77 if (!is_null($indent)) {
78 $indent += 2;
79 $indstr = "\n" . str_repeat(" ", $indent);
80 } else {
81 $indstr = '';
82 }
83 $elemName = str_replace(' ', '_', $elemName);
84
85 switch (gettype($elemValue)) {
86 case 'array' :
87 if (isset ($elemValue['*'])) {
88 $subElemContent = $elemValue['*'];
89 if ($this->mDoubleQuote)
90 $subElemContent = $this->doubleQuote($subElemContent);
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 (is_string($subElemValue) && $this->mDoubleQuote)
107 $subElemValue = $this->doubleQuote($subElemValue);
108
109 // Replace spaces with underscores
110 $newSubElemId = str_replace(' ', '_', $subElemId);
111 if($newSubElemId != $subElemId) {
112 $elemValue[$newSubElemId] = $subElemValue;
113 unset($elemValue[$subElemId]);
114 $subElemId = $newSubElemId;
115 }
116
117 if (gettype($subElemId) === 'integer') {
118 $indElements[] = $subElemValue;
119 unset ($elemValue[$subElemId]);
120 } elseif (is_array($subElemValue)) {
121 $subElements[$subElemId] = $subElemValue;
122 unset ($elemValue[$subElemId]);
123 }
124 }
125
126 if (is_null($subElemIndName) && count($indElements))
127 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().");
128
129 if (count($subElements) && count($indElements) && !is_null($subElemContent))
130 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
131
132 if (!is_null($subElemContent)) {
133 $this->printText($indstr . Xml::element($elemName, $elemValue, $subElemContent));
134 } elseif (!count($indElements) && !count($subElements)) {
135 $this->printText($indstr . Xml::element($elemName, $elemValue));
136 } else {
137 $this->printText($indstr . Xml::element($elemName, $elemValue, null));
138
139 foreach ($subElements as $subElemId => & $subElemValue)
140 $this->recXmlPrint($subElemId, $subElemValue, $indent);
141
142 foreach ($indElements as $subElemId => & $subElemValue)
143 $this->recXmlPrint($subElemIndName, $subElemValue, $indent);
144
145 $this->printText($indstr . Xml::closeElement($elemName));
146 }
147 break;
148 case 'object' :
149 // ignore
150 break;
151 default :
152 $this->printText($indstr . Xml::element($elemName, null, $elemValue));
153 break;
154 }
155 }
156 private function doubleQuote( $text ) {
157 return Sanitizer::encodeAttribute( $text );
158 }
159
160 public function getAllowedParams() {
161 return array (
162 'xmldoublequote' => false
163 );
164 }
165
166 public function getParamDescription() {
167 return array (
168 'xmldoublequote' => 'If specified, double quotes all attributes and content.',
169 );
170 }
171
172
173 public function getDescription() {
174 return 'Output data in XML format' . parent :: getDescription();
175 }
176
177 public function getVersion() {
178 return __CLASS__ . ': $Id$';
179 }
180 }