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 $mXslt = null;
43 public function __construct( $main, $format ) {
44 parent
::__construct( $main, $format );
47 public function getMimeType() {
51 public function getNeedsRawData() {
55 public function setRootElement( $rootElemName ) {
56 $this->mRootElemName
= $rootElemName;
59 public function execute() {
60 $params = $this->extractRequestParams();
61 $this->mDoubleQuote
= $params['xmldoublequote'];
62 $this->mXslt
= $params['xslt'];
64 $this->printText( '<?xml version="1.0"?>' );
65 if ( !is_null( $this->mXslt
) ) {
69 self
::recXmlPrint( $this->mRootElemName
,
70 array( 'xmlns' => self
::$namespace ) +
$this->getResultData(),
71 $this->getIsHtml() ?
- 2 : null,
78 * This method takes an array and converts it to XML.
79 * There are several noteworthy cases:
81 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
82 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
84 * 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.
85 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
87 * If neither key is found, all keys become element names, and values become element content.
88 * The method is recursive, so the same rules apply to any sub-arrays.
93 * @param $doublequote bool
97 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
99 if ( !is_null( $indent ) ) {
101 $indstr = "\n" . str_repeat( ' ', $indent );
105 $elemName = str_replace( ' ', '_', $elemName );
107 switch ( gettype( $elemValue ) ) {
109 if ( isset( $elemValue['*'] ) ) {
110 $subElemContent = $elemValue['*'];
111 if ( $doublequote ) {
112 $subElemContent = Sanitizer
::encodeAttribute( $subElemContent );
114 unset( $elemValue['*'] );
116 // Add xml:space="preserve" to the
117 // element so XML parsers will leave
118 // whitespace in the content alone
119 $elemValue['xml:space'] = 'preserve';
121 $subElemContent = null;
124 if ( isset( $elemValue['_element'] ) ) {
125 $subElemIndName = $elemValue['_element'];
126 unset( $elemValue['_element'] );
128 $subElemIndName = null;
131 $indElements = array();
132 $subElements = array();
133 foreach ( $elemValue as $subElemId => & $subElemValue ) {
134 if ( is_string( $subElemValue ) && $doublequote ) {
135 $subElemValue = Sanitizer
::encodeAttribute( $subElemValue );
138 if ( gettype( $subElemId ) === 'integer' ) {
139 $indElements[] = $subElemValue;
140 unset( $elemValue[$subElemId] );
141 } elseif ( is_array( $subElemValue ) ) {
142 $subElements[$subElemId] = $subElemValue;
143 unset ( $elemValue[$subElemId] );
147 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
148 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
151 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
152 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has content and subelements" );
155 if ( !is_null( $subElemContent ) ) {
156 $retval .= $indstr . Xml
::element( $elemName, $elemValue, $subElemContent );
157 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
158 $retval .= $indstr . Xml
::element( $elemName, $elemValue );
160 $retval .= $indstr . Xml
::element( $elemName, $elemValue, null );
162 foreach ( $subElements as $subElemId => & $subElemValue ) {
163 $retval .= self
::recXmlPrint( $subElemId, $subElemValue, $indent );
166 foreach ( $indElements as &$subElemValue ) {
167 $retval .= self
::recXmlPrint( $subElemIndName, $subElemValue, $indent );
170 $retval .= $indstr . Xml
::closeElement( $elemName );
177 $retval .= $indstr . Xml
::element( $elemName, null, $elemValue );
184 $nt = Title
::newFromText( $this->mXslt
);
185 if ( is_null( $nt ) ||
!$nt->exists() ) {
186 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
189 if ( $nt->getNamespace() != NS_MEDIAWIKI
) {
190 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
193 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
194 $this->setWarning( 'Stylesheet should have .xsl extension.' );
197 $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
200 public function getAllowedParams() {
202 'xmldoublequote' => false,
207 public function getParamDescription() {
209 'xmldoublequote' => 'If specified, double quotes all attributes and content',
210 'xslt' => 'If specified, adds <xslt> as stylesheet',
214 public function getDescription() {
215 return 'Output data in XML format' . parent
::getDescription();
218 public function getVersion() {
219 return __CLASS__
. ': $Id$';