Add some more detailed info about the xslt param of format=xml
[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 * There are several noteworthy cases:
87 *
88 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
89 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
90 *
91 * 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.
92 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
93 *
94 * If neither key is found, all keys become element names, and values become element content.
95 * The method is recursive, so the same rules apply to any sub-arrays.
96 *
97 * @param $elemName
98 * @param $elemValue
99 * @param $indent
100 * @param $doublequote bool
101 *
102 * @return string
103 */
104 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
105 $retval = '';
106 if ( !is_null( $indent ) ) {
107 $indent += 2;
108 $indstr = "\n" . str_repeat( ' ', $indent );
109 } else {
110 $indstr = '';
111 }
112 $elemName = str_replace( ' ', '_', $elemName );
113
114 switch ( gettype( $elemValue ) ) {
115 case 'array':
116 if ( isset( $elemValue['*'] ) ) {
117 $subElemContent = $elemValue['*'];
118 if ( $doublequote ) {
119 $subElemContent = Sanitizer::encodeAttribute( $subElemContent );
120 }
121 unset( $elemValue['*'] );
122
123 // Add xml:space="preserve" to the
124 // element so XML parsers will leave
125 // whitespace in the content alone
126 $elemValue['xml:space'] = 'preserve';
127 } else {
128 $subElemContent = null;
129 }
130
131 if ( isset( $elemValue['_element'] ) ) {
132 $subElemIndName = $elemValue['_element'];
133 unset( $elemValue['_element'] );
134 } else {
135 $subElemIndName = null;
136 }
137
138 $indElements = array();
139 $subElements = array();
140 foreach ( $elemValue as $subElemId => & $subElemValue ) {
141 if ( is_string( $subElemValue ) && $doublequote ) {
142 $subElemValue = Sanitizer::encodeAttribute( $subElemValue );
143 }
144
145 if ( gettype( $subElemId ) === 'integer' ) {
146 $indElements[] = $subElemValue;
147 unset( $elemValue[$subElemId] );
148 } elseif ( is_array( $subElemValue ) ) {
149 $subElements[$subElemId] = $subElemValue;
150 unset ( $elemValue[$subElemId] );
151 }
152 }
153
154 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
155 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
156 }
157
158 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
159 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
160 }
161
162 if ( !is_null( $subElemContent ) ) {
163 $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
164 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
165 $retval .= $indstr . Xml::element( $elemName, $elemValue );
166 } else {
167 $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
168
169 foreach ( $subElements as $subElemId => & $subElemValue ) {
170 $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
171 }
172
173 foreach ( $indElements as &$subElemValue ) {
174 $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
175 }
176
177 $retval .= $indstr . Xml::closeElement( $elemName );
178 }
179 break;
180 case 'object':
181 // ignore
182 break;
183 default:
184 $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
185 break;
186 }
187 return $retval;
188 }
189
190 function addXslt() {
191 $nt = Title::newFromText( $this->mXslt );
192 if ( is_null( $nt ) || !$nt->exists() ) {
193 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
194 return;
195 }
196 if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
197 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
198 return;
199 }
200 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
201 $this->setWarning( 'Stylesheet should have .xsl extension.' );
202 return;
203 }
204 $this->printText( '<?xml-stylesheet href="' . htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
205 }
206
207 public function getAllowedParams() {
208 return array(
209 'xmldoublequote' => false,
210 'xslt' => null,
211 'includexmlnamespace' => false,
212 );
213 }
214
215 public function getParamDescription() {
216 return array(
217 'xmldoublequote' => 'If specified, double quotes all attributes and content',
218 'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
219 . 'in the MediaWiki namespace whose page name ends with ".xsl"',
220 'includexmlnamespace' => 'If specified, adds an XML namespace'
221 );
222 }
223
224 public function getDescription() {
225 return 'Output data in XML format' . parent::getDescription();
226 }
227
228 public function getVersion() {
229 return __CLASS__ . ': $Id$';
230 }
231 }