3f39eb9b4311ade4d93d9243ea7c25a0e8a6d15e
[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 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
91 $retval = '';
92 if ( !is_null( $indent ) ) {
93 $indent += 2;
94 $indstr = "\n" . str_repeat( ' ', $indent );
95 } else {
96 $indstr = '';
97 }
98 $elemName = str_replace( ' ', '_', $elemName );
99
100 switch ( gettype( $elemValue ) ) {
101 case 'array':
102 if ( isset( $elemValue['*'] ) ) {
103 $subElemContent = $elemValue['*'];
104 if ( $doublequote ) {
105 $subElemContent = Sanitizer::encodeAttribute( $subElemContent );
106 }
107 unset( $elemValue['*'] );
108
109 // Add xml:space="preserve" to the
110 // element so XML parsers will leave
111 // whitespace in the content alone
112 $elemValue['xml:space'] = 'preserve';
113 } else {
114 $subElemContent = null;
115 }
116
117 if ( isset( $elemValue['_element'] ) ) {
118 $subElemIndName = $elemValue['_element'];
119 unset( $elemValue['_element'] );
120 } else {
121 $subElemIndName = null;
122 }
123
124 $indElements = array();
125 $subElements = array();
126 foreach ( $elemValue as $subElemId => & $subElemValue ) {
127 if ( is_string( $subElemValue ) && $doublequote ) {
128 $subElemValue = Sanitizer::encodeAttribute( $subElemValue );
129 }
130
131 if ( gettype( $subElemId ) === 'integer' ) {
132 $indElements[] = $subElemValue;
133 unset( $elemValue[$subElemId] );
134 } elseif ( is_array( $subElemValue ) ) {
135 $subElements[$subElemId] = $subElemValue;
136 unset ( $elemValue[$subElemId] );
137 }
138 }
139
140 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
141 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
142 }
143
144 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
145 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
146 }
147
148 if ( !is_null( $subElemContent ) ) {
149 $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
150 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
151 $retval .= $indstr . Xml::element( $elemName, $elemValue );
152 } else {
153 $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
154
155 foreach ( $subElements as $subElemId => & $subElemValue ) {
156 $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
157 }
158
159 foreach ( $indElements as &$subElemValue ) {
160 $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
161 }
162
163 $retval .= $indstr . Xml::closeElement( $elemName );
164 }
165 break;
166 case 'object':
167 // ignore
168 break;
169 default:
170 $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
171 break;
172 }
173 return $retval;
174 }
175
176 function addXslt() {
177 $nt = Title::newFromText( $this->mXslt );
178 if ( is_null( $nt ) || !$nt->exists() ) {
179 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
180 return;
181 }
182 if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
183 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
184 return;
185 }
186 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
187 $this->setWarning( 'Stylesheet should have .xsl extension.' );
188 return;
189 }
190 $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
191 }
192
193 public function getAllowedParams() {
194 return array(
195 'xmldoublequote' => false,
196 'xslt' => null,
197 );
198 }
199
200 public function getParamDescription() {
201 return array(
202 'xmldoublequote' => 'If specified, double quotes all attributes and content',
203 'xslt' => 'If specified, adds <xslt> as stylesheet',
204 );
205 }
206
207 public function getDescription() {
208 return 'Output data in XML format' . parent::getDescription();
209 }
210
211 public function getVersion() {
212 return __CLASS__ . ': $Id$';
213 }
214 }