Merge "Protected function UploadBase->validateName changed to public"
[lhc/web/wiklou.git] / includes / api / ApiFormatXml.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 19, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * API XML output formatter
29 * @ingroup API
30 */
31 class ApiFormatXml extends ApiFormatBase {
32
33 private $mRootElemName = 'api';
34 public static $namespace = 'http://www.mediawiki.org/xml/api/';
35 private $mDoubleQuote = false;
36 private $mIncludeNamespace = false;
37 private $mXslt = null;
38
39 public function getMimeType() {
40 return 'text/xml';
41 }
42
43 public function getNeedsRawData() {
44 return true;
45 }
46
47 public function setRootElement( $rootElemName ) {
48 $this->mRootElemName = $rootElemName;
49 }
50
51 public function execute() {
52 $params = $this->extractRequestParams();
53 $this->mDoubleQuote = $params['xmldoublequote'];
54 $this->mIncludeNamespace = $params['includexmlnamespace'];
55 $this->mXslt = $params['xslt'];
56
57 $this->printText( '<?xml version="1.0"?>' );
58 if ( !is_null( $this->mXslt ) ) {
59 $this->addXslt();
60 }
61 if ( $this->mIncludeNamespace ) {
62 // If the result data already contains an 'xmlns' namespace added
63 // for custom XML output types, it will override the one for the
64 // generic API results.
65 // This allows API output of other XML types like Atom, RSS, RSD.
66 $data = $this->getResultData() + array( 'xmlns' => self::$namespace );
67 } else {
68 $data = $this->getResultData();
69 }
70
71 $this->printText(
72 self::recXmlPrint( $this->mRootElemName,
73 $data,
74 $this->getIsHtml() ? - 2 : null,
75 $this->mDoubleQuote
76 )
77 );
78 }
79
80 /**
81 * This method takes an array and converts it to XML.
82 *
83 * There are several noteworthy cases:
84 *
85 * If array contains a key '_element', then the code assumes that ALL
86 * other keys are not important and replaces them with the
87 * value['_element'].
88 *
89 * @par Example:
90 * @verbatim
91 * name='root', value = array( '_element'=>'page', 'x', 'y', 'z')
92 * @endverbatim
93 * creates:
94 * @verbatim
95 * <root> <page>x</page> <page>y</page> <page>z</page> </root>
96 * @endverbatim
97 *
98 * If any of the array's element key is '*', then the code treats all
99 * other key->value pairs as attributes, and the value['*'] as the
100 * element's content.
101 *
102 * @par Example:
103 * @verbatim
104 * name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10)
105 * @endverbatim
106 * creates:
107 * @verbatim
108 * <root lang='en' id='10'>text</root>
109 * @endverbatim
110 *
111 * Finally neither key is found, all keys become element names, and values
112 * become element content.
113 *
114 * @note The method is recursive, so the same rules apply to any
115 * sub-arrays.
116 *
117 * @param $elemName
118 * @param $elemValue
119 * @param $indent
120 * @param $doublequote bool
121 *
122 * @return string
123 */
124 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
125 $retval = '';
126 if ( !is_null( $indent ) ) {
127 $indent += 2;
128 $indstr = "\n" . str_repeat( ' ', $indent );
129 } else {
130 $indstr = '';
131 }
132 $elemName = str_replace( ' ', '_', $elemName );
133
134 if ( is_array( $elemValue ) ) {
135 if ( isset( $elemValue['*'] ) ) {
136 $subElemContent = $elemValue['*'];
137 if ( $doublequote ) {
138 $subElemContent = Sanitizer::encodeAttribute( $subElemContent );
139 }
140 unset( $elemValue['*'] );
141
142 // Add xml:space="preserve" to the
143 // element so XML parsers will leave
144 // whitespace in the content alone
145 $elemValue['xml:space'] = 'preserve';
146 } else {
147 $subElemContent = null;
148 }
149
150 if ( isset( $elemValue['_element'] ) ) {
151 $subElemIndName = $elemValue['_element'];
152 unset( $elemValue['_element'] );
153 } else {
154 $subElemIndName = null;
155 }
156
157 $indElements = array();
158 $subElements = array();
159 foreach ( $elemValue as $subElemId => & $subElemValue ) {
160 if ( is_string( $subElemValue ) && $doublequote ) {
161 $subElemValue = Sanitizer::encodeAttribute( $subElemValue );
162 }
163
164 if ( is_int( $subElemId ) ) {
165 $indElements[] = $subElemValue;
166 unset( $elemValue[$subElemId] );
167 } elseif ( is_array( $subElemValue ) ) {
168 $subElements[$subElemId] = $subElemValue;
169 unset ( $elemValue[$subElemId] );
170 }
171 }
172
173 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
174 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
175 }
176
177 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
178 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
179 }
180
181 if ( !is_null( $subElemContent ) ) {
182 $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
183 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
184 $retval .= $indstr . Xml::element( $elemName, $elemValue );
185 } else {
186 $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
187
188 foreach ( $subElements as $subElemId => & $subElemValue ) {
189 $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
190 }
191
192 foreach ( $indElements as &$subElemValue ) {
193 $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
194 }
195
196 $retval .= $indstr . Xml::closeElement( $elemName );
197 }
198 } elseif ( !is_object( $elemValue ) ) {
199 // to make sure null value doesn't produce unclosed element,
200 // which is what Xml::element( $elemName, null, null ) returns
201 if ( $elemValue === null ) {
202 $retval .= $indstr . Xml::element( $elemName );
203 } else {
204 $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
205 }
206 }
207 return $retval;
208 }
209
210 function addXslt() {
211 $nt = Title::newFromText( $this->mXslt );
212 if ( is_null( $nt ) || !$nt->exists() ) {
213 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
214 return;
215 }
216 if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
217 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
218 return;
219 }
220 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
221 $this->setWarning( 'Stylesheet should have .xsl extension.' );
222 return;
223 }
224 $this->printText( '<?xml-stylesheet href="' . htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
225 }
226
227 public function getAllowedParams() {
228 return array(
229 'xmldoublequote' => false,
230 'xslt' => null,
231 'includexmlnamespace' => false,
232 );
233 }
234
235 public function getParamDescription() {
236 return array(
237 'xmldoublequote' => 'If specified, double quotes all attributes and content',
238 'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
239 . 'in the MediaWiki namespace whose page name ends with ".xsl"',
240 'includexmlnamespace' => 'If specified, adds an XML namespace'
241 );
242 }
243
244 public function getDescription() {
245 return 'Output data in XML format' . parent::getDescription();
246 }
247 }