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