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