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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiFormatBase.php' );
33 * API XML output formatter
36 class ApiFormatXml
extends ApiFormatBase
{
38 private $mRootElemName = 'api';
39 public static $namespace = 'http://www.mediawiki.org/xml/api/';
40 private $mDoubleQuote = false;
41 private $mIncludeNamespace = false;
42 private $mXslt = null;
44 public function __construct( $main, $format ) {
45 parent
::__construct( $main, $format );
48 public function getMimeType() {
52 public function getNeedsRawData() {
56 public function setRootElement( $rootElemName ) {
57 $this->mRootElemName
= $rootElemName;
60 public function execute() {
61 $params = $this->extractRequestParams();
62 $this->mDoubleQuote
= $params['xmldoublequote'];
63 $this->mIncludeNamespace
= $params['includexmlnamespace'];
64 $this->mXslt
= $params['xslt'];
66 $this->printText( '<?xml version="1.0"?>' );
67 if ( !is_null( $this->mXslt
) ) {
70 if ( $this->mIncludeNamespace
) {
71 // If the result data already contains an 'xmlns' namespace added
72 // for custom XML output types, it will override the one for the
73 // generic API results.
74 // This allows API output of other XML types like Atom, RSS, RSD.
75 $data = $this->getResultData() +
array( 'xmlns' => self
::$namespace );
77 $data = $this->getResultData();
81 self
::recXmlPrint( $this->mRootElemName
,
83 $this->getIsHtml() ?
- 2 : null,
90 * This method takes an array and converts it to XML.
91 * There are several noteworthy cases:
93 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
94 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
96 * 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.
97 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
99 * If neither key is found, all keys become element names, and values become element content.
100 * The method is recursive, so the same rules apply to any sub-arrays.
105 * @param $doublequote bool
109 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
111 if ( !is_null( $indent ) ) {
113 $indstr = "\n" . str_repeat( ' ', $indent );
117 $elemName = str_replace( ' ', '_', $elemName );
119 switch ( gettype( $elemValue ) ) {
121 if ( isset( $elemValue['*'] ) ) {
122 $subElemContent = $elemValue['*'];
123 if ( $doublequote ) {
124 $subElemContent = Sanitizer
::encodeAttribute( $subElemContent );
126 unset( $elemValue['*'] );
128 // Add xml:space="preserve" to the
129 // element so XML parsers will leave
130 // whitespace in the content alone
131 $elemValue['xml:space'] = 'preserve';
133 $subElemContent = null;
136 if ( isset( $elemValue['_element'] ) ) {
137 $subElemIndName = $elemValue['_element'];
138 unset( $elemValue['_element'] );
140 $subElemIndName = null;
143 $indElements = array();
144 $subElements = array();
145 foreach ( $elemValue as $subElemId => & $subElemValue ) {
146 if ( is_string( $subElemValue ) && $doublequote ) {
147 $subElemValue = Sanitizer
::encodeAttribute( $subElemValue );
150 if ( gettype( $subElemId ) === 'integer' ) {
151 $indElements[] = $subElemValue;
152 unset( $elemValue[$subElemId] );
153 } elseif ( is_array( $subElemValue ) ) {
154 $subElements[$subElemId] = $subElemValue;
155 unset ( $elemValue[$subElemId] );
159 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
160 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
163 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
164 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has content and subelements" );
167 if ( !is_null( $subElemContent ) ) {
168 $retval .= $indstr . Xml
::element( $elemName, $elemValue, $subElemContent );
169 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
170 $retval .= $indstr . Xml
::element( $elemName, $elemValue );
172 $retval .= $indstr . Xml
::element( $elemName, $elemValue, null );
174 foreach ( $subElements as $subElemId => & $subElemValue ) {
175 $retval .= self
::recXmlPrint( $subElemId, $subElemValue, $indent );
178 foreach ( $indElements as &$subElemValue ) {
179 $retval .= self
::recXmlPrint( $subElemIndName, $subElemValue, $indent );
182 $retval .= $indstr . Xml
::closeElement( $elemName );
189 $retval .= $indstr . Xml
::element( $elemName, null, $elemValue );
196 $nt = Title
::newFromText( $this->mXslt
);
197 if ( is_null( $nt ) ||
!$nt->exists() ) {
198 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
201 if ( $nt->getNamespace() != NS_MEDIAWIKI
) {
202 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
205 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
206 $this->setWarning( 'Stylesheet should have .xsl extension.' );
209 $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
212 public function getAllowedParams() {
214 'xmldoublequote' => false,
216 'includexmlnamespace' => false,
220 public function getParamDescription() {
222 'xmldoublequote' => 'If specified, double quotes all attributes and content',
223 'xslt' => 'If specified, adds <xslt> as stylesheet',
224 'includexmlnamespace' => 'If specified, adds an XML namespace'
228 public function getDescription() {
229 return 'Output data in XML format' . parent
::getDescription();
232 public function getVersion() {
233 return __CLASS__
. ': $Id$';