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 if ( is_array( $elemValue ) ) {
135 if ( isset( $elemValue['*'] ) ) {
136 $subElemContent = $elemValue['*'];
137 if ( $doublequote ) {
138 $subElemContent = Sanitizer
::encodeAttribute( $subElemContent );
140 unset( $elemValue['*'] );
142 // Add xml:space="preserve" to the
143 // element so XML parsers will leave
144 // whitespace in the content alone
145 $elemValue['xml:space'] = 'preserve';
147 $subElemContent = null;
150 if ( isset( $elemValue['_element'] ) ) {
151 $subElemIndName = $elemValue['_element'];
152 unset( $elemValue['_element'] );
154 $subElemIndName = null;
157 $indElements = array();
158 $subElements = array();
159 foreach ( $elemValue as $subElemId => & $subElemValue ) {
160 if ( is_string( $subElemValue ) && $doublequote ) {
161 $subElemValue = Sanitizer
::encodeAttribute( $subElemValue );
164 if ( is_int( $subElemId ) ) {
165 $indElements[] = $subElemValue;
166 unset( $elemValue[$subElemId] );
167 } elseif ( is_array( $subElemValue ) ) {
168 $subElements[$subElemId] = $subElemValue;
169 unset ( $elemValue[$subElemId] );
173 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
174 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
177 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
178 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has content and subelements" );
181 if ( !is_null( $subElemContent ) ) {
182 $retval .= $indstr . Xml
::element( $elemName, $elemValue, $subElemContent );
183 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
184 $retval .= $indstr . Xml
::element( $elemName, $elemValue );
186 $retval .= $indstr . Xml
::element( $elemName, $elemValue, null );
188 foreach ( $subElements as $subElemId => & $subElemValue ) {
189 $retval .= self
::recXmlPrint( $subElemId, $subElemValue, $indent );
192 foreach ( $indElements as &$subElemValue ) {
193 $retval .= self
::recXmlPrint( $subElemIndName, $subElemValue, $indent );
196 $retval .= $indstr . Xml
::closeElement( $elemName );
198 } elseif ( !is_object( $elemValue ) ) {
199 // to make sure null value doesn't produce unclosed element,
200 // which is what Xml::element( $elemName, null, null ) returns
201 if ( $elemValue === null ) {
202 $retval .= $indstr . Xml
::element( $elemName );
204 $retval .= $indstr . Xml
::element( $elemName, null, $elemValue );
211 $nt = Title
::newFromText( $this->mXslt
);
212 if ( is_null( $nt ) ||
!$nt->exists() ) {
213 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
216 if ( $nt->getNamespace() != NS_MEDIAWIKI
) {
217 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
220 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
221 $this->setWarning( 'Stylesheet should have .xsl extension.' );
224 $this->printText( '<?xml-stylesheet href="' . htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
227 public function getAllowedParams() {
229 'xmldoublequote' => false,
231 'includexmlnamespace' => false,
235 public function getParamDescription() {
237 'xmldoublequote' => 'If specified, double quotes all attributes and content',
238 'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
239 . 'in the MediaWiki namespace whose page name ends with ".xsl"',
240 'includexmlnamespace' => 'If specified, adds an XML namespace'
244 public function getDescription() {
245 return 'Output data in XML format' . parent
::getDescription();