5 * Created on Sep 19, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
28 * API XML output formatter
31 class ApiFormatXml
extends ApiFormatBase
{
33 private $mRootElemName = 'api';
34 public static $namespace = 'http://www.mediawiki.org/xml/api/';
35 private $mDoubleQuote = false;
36 private $mIncludeNamespace = false;
37 private $mXslt = null;
39 public function getMimeType() {
43 public function getNeedsRawData() {
47 public function setRootElement( $rootElemName ) {
48 $this->mRootElemName
= $rootElemName;
51 public function execute() {
52 $params = $this->extractRequestParams();
53 $this->mDoubleQuote
= $params['xmldoublequote'];
54 $this->mIncludeNamespace
= $params['includexmlnamespace'];
55 $this->mXslt
= $params['xslt'];
57 $this->printText( '<?xml version="1.0"?>' );
58 if ( !is_null( $this->mXslt
) ) {
61 if ( $this->mIncludeNamespace
) {
62 // If the result data already contains an 'xmlns' namespace added
63 // for custom XML output types, it will override the one for the
64 // generic API results.
65 // This allows API output of other XML types like Atom, RSS, RSD.
66 $data = $this->getResultData() +
array( 'xmlns' => self
::$namespace );
68 $data = $this->getResultData();
72 self
::recXmlPrint( $this->mRootElemName
,
74 $this->getIsHtml() ?
- 2 : null,
81 * This method takes an array and converts it to XML.
83 * There are several noteworthy cases:
85 * If array contains a key '_element', then the code assumes that ALL
86 * other keys are not important and replaces them with the
91 * name='root', value = array( '_element'=>'page', 'x', 'y', 'z')
95 * <root> <page>x</page> <page>y</page> <page>z</page> </root>
98 * If any of the array's element key is '*', then the code treats all
99 * other key->value pairs as attributes, and the value['*'] as the
104 * name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10)
108 * <root lang='en' id='10'>text</root>
111 * Finally neither key is found, all keys become element names, and values
112 * become element content.
114 * @note The method is recursive, so the same rules apply to any
120 * @param $doublequote bool
124 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
126 if ( !is_null( $indent ) ) {
128 $indstr = "\n" . str_repeat( ' ', $indent );
132 $elemName = str_replace( ' ', '_', $elemName );
134 switch ( gettype( $elemValue ) ) {
136 if ( isset( $elemValue['*'] ) ) {
137 $subElemContent = $elemValue['*'];
138 if ( $doublequote ) {
139 $subElemContent = Sanitizer
::encodeAttribute( $subElemContent );
141 unset( $elemValue['*'] );
143 // Add xml:space="preserve" to the
144 // element so XML parsers will leave
145 // whitespace in the content alone
146 $elemValue['xml:space'] = 'preserve';
148 $subElemContent = null;
151 if ( isset( $elemValue['_element'] ) ) {
152 $subElemIndName = $elemValue['_element'];
153 unset( $elemValue['_element'] );
155 $subElemIndName = null;
158 $indElements = array();
159 $subElements = array();
160 foreach ( $elemValue as $subElemId => & $subElemValue ) {
161 if ( is_string( $subElemValue ) && $doublequote ) {
162 $subElemValue = Sanitizer
::encodeAttribute( $subElemValue );
165 if ( gettype( $subElemId ) === 'integer' ) {
166 $indElements[] = $subElemValue;
167 unset( $elemValue[$subElemId] );
168 } elseif ( is_array( $subElemValue ) ) {
169 $subElements[$subElemId] = $subElemValue;
170 unset ( $elemValue[$subElemId] );
174 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
175 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
178 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
179 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has content and subelements" );
182 if ( !is_null( $subElemContent ) ) {
183 $retval .= $indstr . Xml
::element( $elemName, $elemValue, $subElemContent );
184 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
185 $retval .= $indstr . Xml
::element( $elemName, $elemValue );
187 $retval .= $indstr . Xml
::element( $elemName, $elemValue, null );
189 foreach ( $subElements as $subElemId => & $subElemValue ) {
190 $retval .= self
::recXmlPrint( $subElemId, $subElemValue, $indent );
193 foreach ( $indElements as &$subElemValue ) {
194 $retval .= self
::recXmlPrint( $subElemIndName, $subElemValue, $indent );
197 $retval .= $indstr . Xml
::closeElement( $elemName );
204 // to make sure null value doesn't produce unclosed element,
205 // which is what Xml::element( $elemName, null, null ) returns
206 if ( $elemValue === null ) {
207 $retval .= $indstr . Xml
::element( $elemName );
209 $retval .= $indstr . Xml
::element( $elemName, null, $elemValue );
217 $nt = Title
::newFromText( $this->mXslt
);
218 if ( is_null( $nt ) ||
!$nt->exists() ) {
219 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
222 if ( $nt->getNamespace() != NS_MEDIAWIKI
) {
223 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
226 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
227 $this->setWarning( 'Stylesheet should have .xsl extension.' );
230 $this->printText( '<?xml-stylesheet href="' . htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
233 public function getAllowedParams() {
235 'xmldoublequote' => false,
237 'includexmlnamespace' => false,
241 public function getParamDescription() {
243 'xmldoublequote' => 'If specified, double quotes all attributes and content',
244 'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
245 . 'in the MediaWiki namespace whose page name ends with ".xsl"',
246 'includexmlnamespace' => 'If specified, adds an XML namespace'
250 public function getDescription() {
251 return 'Output data in XML format' . parent
::getDescription();