raise timeout for CdbTest::testCdb()
[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 __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 $this->mIncludeNamespace = $params['includexmlnamespace'];
59 $this->mXslt = $params['xslt'];
60
61 $this->printText( '<?xml version="1.0"?>' );
62 if ( !is_null( $this->mXslt ) ) {
63 $this->addXslt();
64 }
65 if ( $this->mIncludeNamespace ) {
66 // If the result data already contains an 'xmlns' namespace added
67 // for custom XML output types, it will override the one for the
68 // generic API results.
69 // This allows API output of other XML types like Atom, RSS, RSD.
70 $data = $this->getResultData() + array( 'xmlns' => self::$namespace );
71 } else {
72 $data = $this->getResultData();
73 }
74
75 $this->printText(
76 self::recXmlPrint( $this->mRootElemName,
77 $data,
78 $this->getIsHtml() ? - 2 : null,
79 $this->mDoubleQuote
80 )
81 );
82 }
83
84 /**
85 * This method takes an array and converts it to XML.
86 *
87 * There are several noteworthy cases:
88 *
89 * If array contains a key '_element', then the code assumes that ALL
90 * other keys are not important and replaces them with the
91 * value['_element'].
92 *
93 * @par Example:
94 * @verbatim
95 * name='root', value = array( '_element'=>'page', 'x', 'y', 'z')
96 * @endverbatim
97 * creates:
98 * @verbatim
99 * <root> <page>x</page> <page>y</page> <page>z</page> </root>
100 * @endverbatim
101 *
102 * If any of the array's element key is '*', then the code treats all
103 * other key->value pairs as attributes, and the value['*'] as the
104 * element's content.
105 *
106 * @par Example:
107 * @verbatim
108 * name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10)
109 * @endverbatim
110 * creates:
111 * @verbatim
112 * <root lang='en' id='10'>text</root>
113 * @endverbatim
114 *
115 * Finally neither key is found, all keys become element names, and values
116 * become element content.
117 *
118 * @note The method is recursive, so the same rules apply to any
119 * sub-arrays.
120 *
121 * @param $elemName
122 * @param $elemValue
123 * @param $indent
124 * @param $doublequote bool
125 *
126 * @return string
127 */
128 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
129 $retval = '';
130 if ( !is_null( $indent ) ) {
131 $indent += 2;
132 $indstr = "\n" . str_repeat( ' ', $indent );
133 } else {
134 $indstr = '';
135 }
136 $elemName = str_replace( ' ', '_', $elemName );
137
138 switch ( gettype( $elemValue ) ) {
139 case 'array':
140 if ( isset( $elemValue['*'] ) ) {
141 $subElemContent = $elemValue['*'];
142 if ( $doublequote ) {
143 $subElemContent = Sanitizer::encodeAttribute( $subElemContent );
144 }
145 unset( $elemValue['*'] );
146
147 // Add xml:space="preserve" to the
148 // element so XML parsers will leave
149 // whitespace in the content alone
150 $elemValue['xml:space'] = 'preserve';
151 } else {
152 $subElemContent = null;
153 }
154
155 if ( isset( $elemValue['_element'] ) ) {
156 $subElemIndName = $elemValue['_element'];
157 unset( $elemValue['_element'] );
158 } else {
159 $subElemIndName = null;
160 }
161
162 $indElements = array();
163 $subElements = array();
164 foreach ( $elemValue as $subElemId => & $subElemValue ) {
165 if ( is_string( $subElemValue ) && $doublequote ) {
166 $subElemValue = Sanitizer::encodeAttribute( $subElemValue );
167 }
168
169 if ( gettype( $subElemId ) === 'integer' ) {
170 $indElements[] = $subElemValue;
171 unset( $elemValue[$subElemId] );
172 } elseif ( is_array( $subElemValue ) ) {
173 $subElements[$subElemId] = $subElemValue;
174 unset ( $elemValue[$subElemId] );
175 }
176 }
177
178 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
179 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
180 }
181
182 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
183 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
184 }
185
186 if ( !is_null( $subElemContent ) ) {
187 $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
188 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
189 $retval .= $indstr . Xml::element( $elemName, $elemValue );
190 } else {
191 $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
192
193 foreach ( $subElements as $subElemId => & $subElemValue ) {
194 $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
195 }
196
197 foreach ( $indElements as &$subElemValue ) {
198 $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
199 }
200
201 $retval .= $indstr . Xml::closeElement( $elemName );
202 }
203 break;
204 case 'object':
205 // ignore
206 break;
207 default:
208 // to make sure null value doesn't produce unclosed element,
209 // which is what Xml::element( $elemName, null, null ) returns
210 if ( $elemValue === null ) {
211 $retval .= $indstr . Xml::element( $elemName );
212 } else {
213 $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
214 }
215 break;
216 }
217 return $retval;
218 }
219
220 function addXslt() {
221 $nt = Title::newFromText( $this->mXslt );
222 if ( is_null( $nt ) || !$nt->exists() ) {
223 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
224 return;
225 }
226 if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
227 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
228 return;
229 }
230 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
231 $this->setWarning( 'Stylesheet should have .xsl extension.' );
232 return;
233 }
234 $this->printText( '<?xml-stylesheet href="' . htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
235 }
236
237 public function getAllowedParams() {
238 return array(
239 'xmldoublequote' => false,
240 'xslt' => null,
241 'includexmlnamespace' => false,
242 );
243 }
244
245 public function getParamDescription() {
246 return array(
247 'xmldoublequote' => 'If specified, double quotes all attributes and content',
248 'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
249 . 'in the MediaWiki namespace whose page name ends with ".xsl"',
250 'includexmlnamespace' => 'If specified, adds an XML namespace'
251 );
252 }
253
254 public function getDescription() {
255 return 'Output data in XML format' . parent::getDescription();
256 }
257 }