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