Partial revert of r101912 -- restores r99315 'includexmlnamespace' parameter for...
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiFormatBase.php' );
30 }
31
32 /**
33 * API XML output formatter
34 * @ingroup API
35 */
36 class ApiFormatXml extends ApiFormatBase {
37
38 private $mRootElemName = 'api';
39 public static $namespace = 'http://www.mediawiki.org/xml/api/';
40 private $mDoubleQuote = false;
41 private $mIncludeNamespace = false;
42 private $mXslt = null;
43
44 public function __construct( $main, $format ) {
45 parent::__construct( $main, $format );
46 }
47
48 public function getMimeType() {
49 return 'text/xml';
50 }
51
52 public function getNeedsRawData() {
53 return true;
54 }
55
56 public function setRootElement( $rootElemName ) {
57 $this->mRootElemName = $rootElemName;
58 }
59
60 public function execute() {
61 $params = $this->extractRequestParams();
62 $this->mDoubleQuote = $params['xmldoublequote'];
63 $this->mIncludeNamespace = $params['includexmlnamespace'];
64 $this->mXslt = $params['xslt'];
65
66 $this->printText( '<?xml version="1.0"?>' );
67 if ( !is_null( $this->mXslt ) ) {
68 $this->addXslt();
69 }
70 if ( $this->mIncludeNamespace ) {
71 // If the result data already contains an 'xmlns' namespace added
72 // for custom XML output types, it will override the one for the
73 // generic API results.
74 // This allows API output of other XML types like Atom, RSS, RSD.
75 $data = $this->getResultData() + array( 'xmlns' => self::$namespace );
76 } else {
77 $data = $this->getResultData();
78 }
79
80 $this->printText(
81 self::recXmlPrint( $this->mRootElemName,
82 $data,
83 $this->getIsHtml() ? - 2 : null,
84 $this->mDoubleQuote
85 )
86 );
87 }
88
89 /**
90 * This method takes an array and converts it to XML.
91 * There are several noteworthy cases:
92 *
93 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
94 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
95 *
96 * 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.
97 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
98 *
99 * If neither key is found, all keys become element names, and values become element content.
100 * The method is recursive, so the same rules apply to any sub-arrays.
101 *
102 * @param $elemName
103 * @param $elemValue
104 * @param $indent
105 * @param $doublequote bool
106 *
107 * @return string
108 */
109 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
110 $retval = '';
111 if ( !is_null( $indent ) ) {
112 $indent += 2;
113 $indstr = "\n" . str_repeat( ' ', $indent );
114 } else {
115 $indstr = '';
116 }
117 $elemName = str_replace( ' ', '_', $elemName );
118
119 switch ( gettype( $elemValue ) ) {
120 case 'array':
121 if ( isset( $elemValue['*'] ) ) {
122 $subElemContent = $elemValue['*'];
123 if ( $doublequote ) {
124 $subElemContent = Sanitizer::encodeAttribute( $subElemContent );
125 }
126 unset( $elemValue['*'] );
127
128 // Add xml:space="preserve" to the
129 // element so XML parsers will leave
130 // whitespace in the content alone
131 $elemValue['xml:space'] = 'preserve';
132 } else {
133 $subElemContent = null;
134 }
135
136 if ( isset( $elemValue['_element'] ) ) {
137 $subElemIndName = $elemValue['_element'];
138 unset( $elemValue['_element'] );
139 } else {
140 $subElemIndName = null;
141 }
142
143 $indElements = array();
144 $subElements = array();
145 foreach ( $elemValue as $subElemId => & $subElemValue ) {
146 if ( is_string( $subElemValue ) && $doublequote ) {
147 $subElemValue = Sanitizer::encodeAttribute( $subElemValue );
148 }
149
150 if ( gettype( $subElemId ) === 'integer' ) {
151 $indElements[] = $subElemValue;
152 unset( $elemValue[$subElemId] );
153 } elseif ( is_array( $subElemValue ) ) {
154 $subElements[$subElemId] = $subElemValue;
155 unset ( $elemValue[$subElemId] );
156 }
157 }
158
159 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
160 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
161 }
162
163 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
164 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
165 }
166
167 if ( !is_null( $subElemContent ) ) {
168 $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
169 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
170 $retval .= $indstr . Xml::element( $elemName, $elemValue );
171 } else {
172 $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
173
174 foreach ( $subElements as $subElemId => & $subElemValue ) {
175 $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
176 }
177
178 foreach ( $indElements as &$subElemValue ) {
179 $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
180 }
181
182 $retval .= $indstr . Xml::closeElement( $elemName );
183 }
184 break;
185 case 'object':
186 // ignore
187 break;
188 default:
189 $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
190 break;
191 }
192 return $retval;
193 }
194
195 function addXslt() {
196 $nt = Title::newFromText( $this->mXslt );
197 if ( is_null( $nt ) || !$nt->exists() ) {
198 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
199 return;
200 }
201 if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
202 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
203 return;
204 }
205 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
206 $this->setWarning( 'Stylesheet should have .xsl extension.' );
207 return;
208 }
209 $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
210 }
211
212 public function getAllowedParams() {
213 return array(
214 'xmldoublequote' => false,
215 'xslt' => null,
216 'includexmlnamespace' => false,
217 );
218 }
219
220 public function getParamDescription() {
221 return array(
222 'xmldoublequote' => 'If specified, double quotes all attributes and content',
223 'xslt' => 'If specified, adds <xslt> as stylesheet',
224 'includexmlnamespace' => 'If specified, adds an XML namespace'
225 );
226 }
227
228 public function getDescription() {
229 return 'Output data in XML format' . parent::getDescription();
230 }
231
232 public function getVersion() {
233 return __CLASS__ . ': $Id$';
234 }
235 }