(bug 19528) Revert 55749: "according to the XSLT 1.0 spec, the MIME type should be...
[lhc/web/wiklou.git] / includes / api / ApiFormatXml.php
1 <?php
2
3 /*
4 * Created on Sep 19, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiFormatBase.php' );
29 }
30
31 /**
32 * @ingroup API
33 */
34 class ApiFormatXml extends ApiFormatBase {
35
36 private $mRootElemName = 'api';
37 private $mDoubleQuote = false;
38 private $mXslt = null;
39
40 public function __construct( $main, $format ) {
41 parent :: __construct( $main, $format );
42 }
43
44 public function getMimeType() {
45 return 'text/xml';
46 }
47
48 public function getNeedsRawData() {
49 return true;
50 }
51
52 public function setRootElement( $rootElemName ) {
53 $this->mRootElemName = $rootElemName;
54 }
55
56 public function execute() {
57 $params = $this->extractRequestParams();
58 $this->mDoubleQuote = $params['xmldoublequote'];
59 $this->mXslt = $params['xslt'];
60
61 $this->printText( '<?xml version="1.0"?>' );
62 if ( !is_null( $this->mXslt ) )
63 $this->addXslt();
64 $this->printText( self::recXmlPrint( $this->mRootElemName,
65 $this->getResultData(),
66 $this->getIsHtml() ? - 2 : null,
67 $this->mDoubleQuote ) );
68 }
69
70 /**
71 * This method takes an array and converts it to XML.
72 * There are several noteworthy cases:
73 *
74 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
75 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
76 *
77 * 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.
78 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
79 *
80 * If neither key is found, all keys become element names, and values become element content.
81 * The method is recursive, so the same rules apply to any sub-arrays.
82 */
83 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
84 $retval = '';
85 if ( !is_null( $indent ) ) {
86 $indent += 2;
87 $indstr = "\n" . str_repeat( " ", $indent );
88 } else {
89 $indstr = '';
90 }
91 $elemName = str_replace( ' ', '_', $elemName );
92
93 switch ( gettype( $elemValue ) ) {
94 case 'array' :
95 if ( isset ( $elemValue['*'] ) ) {
96 $subElemContent = $elemValue['*'];
97 if ( $doublequote )
98 $subElemContent = Sanitizer::encodeAttribute( $subElemContent );
99 unset ( $elemValue['*'] );
100
101 // Add xml:space="preserve" to the
102 // element so XML parsers will leave
103 // whitespace in the content alone
104 $elemValue['xml:space'] = 'preserve';
105 } else {
106 $subElemContent = null;
107 }
108
109 if ( isset ( $elemValue['_element'] ) ) {
110 $subElemIndName = $elemValue['_element'];
111 unset ( $elemValue['_element'] );
112 } else {
113 $subElemIndName = null;
114 }
115
116 $indElements = array ();
117 $subElements = array ();
118 foreach ( $elemValue as $subElemId => & $subElemValue ) {
119 if ( is_string( $subElemValue ) && $doublequote )
120 $subElemValue = Sanitizer::encodeAttribute( $subElemValue );
121
122 if ( gettype( $subElemId ) === 'integer' ) {
123 $indElements[] = $subElemValue;
124 unset ( $elemValue[$subElemId] );
125 } elseif ( is_array( $subElemValue ) ) {
126 $subElements[$subElemId] = $subElemValue;
127 unset ( $elemValue[$subElemId] );
128 }
129 }
130
131 if ( is_null( $subElemIndName ) && count( $indElements ) )
132 ApiBase :: dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
133
134 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) )
135 ApiBase :: dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
136
137 if ( !is_null( $subElemContent ) ) {
138 $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
139 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
140 $retval .= $indstr . Xml::element( $elemName, $elemValue );
141 } else {
142 $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
143
144 foreach ( $subElements as $subElemId => & $subElemValue )
145 $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
146
147 foreach ( $indElements as $subElemId => & $subElemValue )
148 $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
149
150 $retval .= $indstr . Xml::closeElement( $elemName );
151 }
152 break;
153 case 'object' :
154 // ignore
155 break;
156 default :
157 $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
158 break;
159 }
160 return $retval;
161 }
162 function addXslt() {
163 $nt = Title::newFromText( $this->mXslt );
164 if ( is_null( $nt ) || !$nt->exists() ) {
165 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
166 return;
167 }
168 if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
169 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
170 return;
171 }
172 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
173 $this->setWarning( 'Stylesheet should have .xsl extension.' );
174 return;
175 }
176 $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
177 }
178
179 public function getAllowedParams() {
180 return array (
181 'xmldoublequote' => false,
182 'xslt' => null,
183 );
184 }
185
186 public function getParamDescription() {
187 return array (
188 'xmldoublequote' => 'If specified, double quotes all attributes and content.',
189 'xslt' => 'If specified, adds <xslt> as stylesheet',
190 );
191 }
192
193
194 public function getDescription() {
195 return 'Output data in XML format' . parent :: getDescription();
196 }
197
198 public function getVersion() {
199 return __CLASS__ . ': $Id$';
200 }
201 }