(bug 43251) API prop=pageprops ppprop should accept multiple values
[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 switch ( gettype( $elemValue ) ) {
135 case 'array':
136 if ( isset( $elemValue['*'] ) ) {
137 $subElemContent = $elemValue['*'];
138 if ( $doublequote ) {
139 $subElemContent = Sanitizer::encodeAttribute( $subElemContent );
140 }
141 unset( $elemValue['*'] );
142
143 // Add xml:space="preserve" to the
144 // element so XML parsers will leave
145 // whitespace in the content alone
146 $elemValue['xml:space'] = 'preserve';
147 } else {
148 $subElemContent = null;
149 }
150
151 if ( isset( $elemValue['_element'] ) ) {
152 $subElemIndName = $elemValue['_element'];
153 unset( $elemValue['_element'] );
154 } else {
155 $subElemIndName = null;
156 }
157
158 $indElements = array();
159 $subElements = array();
160 foreach ( $elemValue as $subElemId => & $subElemValue ) {
161 if ( is_string( $subElemValue ) && $doublequote ) {
162 $subElemValue = Sanitizer::encodeAttribute( $subElemValue );
163 }
164
165 if ( gettype( $subElemId ) === 'integer' ) {
166 $indElements[] = $subElemValue;
167 unset( $elemValue[$subElemId] );
168 } elseif ( is_array( $subElemValue ) ) {
169 $subElements[$subElemId] = $subElemValue;
170 unset ( $elemValue[$subElemId] );
171 }
172 }
173
174 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
175 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
176 }
177
178 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
179 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
180 }
181
182 if ( !is_null( $subElemContent ) ) {
183 $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
184 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
185 $retval .= $indstr . Xml::element( $elemName, $elemValue );
186 } else {
187 $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
188
189 foreach ( $subElements as $subElemId => & $subElemValue ) {
190 $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
191 }
192
193 foreach ( $indElements as &$subElemValue ) {
194 $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
195 }
196
197 $retval .= $indstr . Xml::closeElement( $elemName );
198 }
199 break;
200 case 'object':
201 // ignore
202 break;
203 default:
204 // to make sure null value doesn't produce unclosed element,
205 // which is what Xml::element( $elemName, null, null ) returns
206 if ( $elemValue === null ) {
207 $retval .= $indstr . Xml::element( $elemName );
208 } else {
209 $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
210 }
211 break;
212 }
213 return $retval;
214 }
215
216 function addXslt() {
217 $nt = Title::newFromText( $this->mXslt );
218 if ( is_null( $nt ) || !$nt->exists() ) {
219 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
220 return;
221 }
222 if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
223 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
224 return;
225 }
226 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
227 $this->setWarning( 'Stylesheet should have .xsl extension.' );
228 return;
229 }
230 $this->printText( '<?xml-stylesheet href="' . htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
231 }
232
233 public function getAllowedParams() {
234 return array(
235 'xmldoublequote' => false,
236 'xslt' => null,
237 'includexmlnamespace' => false,
238 );
239 }
240
241 public function getParamDescription() {
242 return array(
243 'xmldoublequote' => 'If specified, double quotes all attributes and content',
244 'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
245 . 'in the MediaWiki namespace whose page name ends with ".xsl"',
246 'includexmlnamespace' => 'If specified, adds an XML namespace'
247 );
248 }
249
250 public function getDescription() {
251 return 'Output data in XML format' . parent::getDescription();
252 }
253 }