4 * Created on Sep 19, 2006
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiFormatBase.php' );
32 * API XML output formatter
35 class ApiFormatXml
extends ApiFormatBase
{
37 private $mRootElemName = 'api';
38 private $mDoubleQuote = false;
39 private $mXslt = null;
41 public function __construct( $main, $format ) {
42 parent
::__construct( $main, $format );
45 public function getMimeType() {
49 public function getNeedsRawData() {
53 public function setRootElement( $rootElemName ) {
54 $this->mRootElemName
= $rootElemName;
57 public function execute() {
58 $params = $this->extractRequestParams();
59 $this->mDoubleQuote
= $params['xmldoublequote'];
60 $this->mXslt
= $params['xslt'];
62 $this->printText( '<?xml version="1.0"?>' );
63 if ( !is_null( $this->mXslt
) ) {
67 self
::recXmlPrint( $this->mRootElemName
,
68 $this->getResultData(),
69 $this->getIsHtml() ?
- 2 : null,
76 * This method takes an array and converts it to XML.
77 * There are several noteworthy cases:
79 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
80 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
82 * 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.
83 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
85 * If neither key is found, all keys become element names, and values become element content.
86 * The method is recursive, so the same rules apply to any sub-arrays.
88 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
90 if ( !is_null( $indent ) ) {
92 $indstr = "\n" . str_repeat( ' ', $indent );
96 $elemName = str_replace( ' ', '_', $elemName );
98 switch ( gettype( $elemValue ) ) {
100 if ( isset( $elemValue['*'] ) ) {
101 $subElemContent = $elemValue['*'];
102 if ( $doublequote ) {
103 $subElemContent = Sanitizer
::encodeAttribute( $subElemContent );
105 unset( $elemValue['*'] );
107 // Add xml:space="preserve" to the
108 // element so XML parsers will leave
109 // whitespace in the content alone
110 $elemValue['xml:space'] = 'preserve';
112 $subElemContent = null;
115 if ( isset( $elemValue['_element'] ) ) {
116 $subElemIndName = $elemValue['_element'];
117 unset( $elemValue['_element'] );
119 $subElemIndName = null;
122 $indElements = array();
123 $subElements = array();
124 foreach ( $elemValue as $subElemId => & $subElemValue ) {
125 if ( is_string( $subElemValue ) && $doublequote ) {
126 $subElemValue = Sanitizer
::encodeAttribute( $subElemValue );
129 if ( gettype( $subElemId ) === 'integer' ) {
130 $indElements[] = $subElemValue;
131 unset( $elemValue[$subElemId] );
132 } elseif ( is_array( $subElemValue ) ) {
133 $subElements[$subElemId] = $subElemValue;
134 unset ( $elemValue[$subElemId] );
138 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
139 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
142 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
143 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has content and subelements" );
146 if ( !is_null( $subElemContent ) ) {
147 $retval .= $indstr . Xml
::element( $elemName, $elemValue, $subElemContent );
148 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
149 $retval .= $indstr . Xml
::element( $elemName, $elemValue );
151 $retval .= $indstr . Xml
::element( $elemName, $elemValue, null );
153 foreach ( $subElements as $subElemId => & $subElemValue ) {
154 $retval .= self
::recXmlPrint( $subElemId, $subElemValue, $indent );
157 foreach ( $indElements as $subElemId => & $subElemValue ) {
158 $retval .= self
::recXmlPrint( $subElemIndName, $subElemValue, $indent );
161 $retval .= $indstr . Xml
::closeElement( $elemName );
168 $retval .= $indstr . Xml
::element( $elemName, null, $elemValue );
175 $nt = Title
::newFromText( $this->mXslt
);
176 if ( is_null( $nt ) ||
!$nt->exists() ) {
177 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
180 if ( $nt->getNamespace() != NS_MEDIAWIKI
) {
181 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
184 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
185 $this->setWarning( 'Stylesheet should have .xsl extension.' );
188 $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
191 public function getAllowedParams() {
193 'xmldoublequote' => false,
198 public function getParamDescription() {
200 'xmldoublequote' => 'If specified, double quotes all attributes and content',
201 'xslt' => 'If specified, adds <xslt> as stylesheet',
205 public function getDescription() {
206 return 'Output data in XML format' . parent
::getDescription();
209 public function getVersion() {
210 return __CLASS__
. ': $Id$';