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 __construct( $main, $format ) {
40 parent
::__construct( $main, $format );
43 public function getMimeType() {
47 public function getNeedsRawData() {
51 public function setRootElement( $rootElemName ) {
52 $this->mRootElemName
= $rootElemName;
55 public function execute() {
56 $params = $this->extractRequestParams();
57 $this->mDoubleQuote
= $params['xmldoublequote'];
58 $this->mIncludeNamespace
= $params['includexmlnamespace'];
59 $this->mXslt
= $params['xslt'];
61 $this->printText( '<?xml version="1.0"?>' );
62 if ( !is_null( $this->mXslt
) ) {
65 if ( $this->mIncludeNamespace
) {
66 // If the result data already contains an 'xmlns' namespace added
67 // for custom XML output types, it will override the one for the
68 // generic API results.
69 // This allows API output of other XML types like Atom, RSS, RSD.
70 $data = $this->getResultData() +
array( 'xmlns' => self
::$namespace );
72 $data = $this->getResultData();
76 self
::recXmlPrint( $this->mRootElemName
,
78 $this->getIsHtml() ?
- 2 : null,
85 * This method takes an array and converts it to XML.
87 * There are several noteworthy cases:
89 * If array contains a key '_element', then the code assumes that ALL
90 * other keys are not important and replaces them with the
95 * name='root', value = array( '_element'=>'page', 'x', 'y', 'z')
99 * <root> <page>x</page> <page>y</page> <page>z</page> </root>
102 * If any of the array's element key is '*', then the code treats all
103 * other key->value pairs as attributes, and the value['*'] as the
108 * name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10)
112 * <root lang='en' id='10'>text</root>
115 * Finally neither key is found, all keys become element names, and values
116 * become element content.
118 * @note The method is recursive, so the same rules apply to any
124 * @param $doublequote bool
128 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
130 if ( !is_null( $indent ) ) {
132 $indstr = "\n" . str_repeat( ' ', $indent );
136 $elemName = str_replace( ' ', '_', $elemName );
138 switch ( gettype( $elemValue ) ) {
140 if ( isset( $elemValue['*'] ) ) {
141 $subElemContent = $elemValue['*'];
142 if ( $doublequote ) {
143 $subElemContent = Sanitizer
::encodeAttribute( $subElemContent );
145 unset( $elemValue['*'] );
147 // Add xml:space="preserve" to the
148 // element so XML parsers will leave
149 // whitespace in the content alone
150 $elemValue['xml:space'] = 'preserve';
152 $subElemContent = null;
155 if ( isset( $elemValue['_element'] ) ) {
156 $subElemIndName = $elemValue['_element'];
157 unset( $elemValue['_element'] );
159 $subElemIndName = null;
162 $indElements = array();
163 $subElements = array();
164 foreach ( $elemValue as $subElemId => & $subElemValue ) {
165 if ( is_string( $subElemValue ) && $doublequote ) {
166 $subElemValue = Sanitizer
::encodeAttribute( $subElemValue );
169 if ( gettype( $subElemId ) === 'integer' ) {
170 $indElements[] = $subElemValue;
171 unset( $elemValue[$subElemId] );
172 } elseif ( is_array( $subElemValue ) ) {
173 $subElements[$subElemId] = $subElemValue;
174 unset ( $elemValue[$subElemId] );
178 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
179 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
182 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
183 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has content and subelements" );
186 if ( !is_null( $subElemContent ) ) {
187 $retval .= $indstr . Xml
::element( $elemName, $elemValue, $subElemContent );
188 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
189 $retval .= $indstr . Xml
::element( $elemName, $elemValue );
191 $retval .= $indstr . Xml
::element( $elemName, $elemValue, null );
193 foreach ( $subElements as $subElemId => & $subElemValue ) {
194 $retval .= self
::recXmlPrint( $subElemId, $subElemValue, $indent );
197 foreach ( $indElements as &$subElemValue ) {
198 $retval .= self
::recXmlPrint( $subElemIndName, $subElemValue, $indent );
201 $retval .= $indstr . Xml
::closeElement( $elemName );
208 $retval .= $indstr . Xml
::element( $elemName, null, $elemValue );
215 $nt = Title
::newFromText( $this->mXslt
);
216 if ( is_null( $nt ) ||
!$nt->exists() ) {
217 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
220 if ( $nt->getNamespace() != NS_MEDIAWIKI
) {
221 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
224 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
225 $this->setWarning( 'Stylesheet should have .xsl extension.' );
228 $this->printText( '<?xml-stylesheet href="' . htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
231 public function getAllowedParams() {
233 'xmldoublequote' => false,
235 'includexmlnamespace' => false,
239 public function getParamDescription() {
241 'xmldoublequote' => 'If specified, double quotes all attributes and content',
242 'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
243 . 'in the MediaWiki namespace whose page name ends with ".xsl"',
244 'includexmlnamespace' => 'If specified, adds an XML namespace'
248 public function getDescription() {
249 return 'Output data in XML format' . parent
::getDescription();