4 * Created on Sep 19, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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' );
34 class ApiFormatXml
extends ApiFormatBase
{
36 private $mRootElemName = 'api';
37 private $mDoubleQuote = false;
38 private $mXslt = null;
40 public function __construct( $main, $format ) {
41 parent
:: __construct( $main, $format );
44 public function getMimeType() {
48 public function getNeedsRawData() {
52 public function setRootElement( $rootElemName ) {
53 $this->mRootElemName
= $rootElemName;
56 public function execute() {
57 $params = $this->extractRequestParams();
58 $this->mDoubleQuote
= $params['xmldoublequote'];
59 $this->mXslt
= $params['xslt'];
61 $this->printText( '<?xml version="1.0"?>' );
62 if ( !is_null( $this->mXslt
) )
64 $this->printText( self
::recXmlPrint( $this->mRootElemName
,
65 $this->getResultData(),
66 $this->getIsHtml() ?
- 2 : null,
67 $this->mDoubleQuote
) );
71 * This method takes an array and converts it to XML.
72 * There are several noteworthy cases:
74 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
75 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
77 * 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.
78 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
80 * If neither key is found, all keys become element names, and values become element content.
81 * The method is recursive, so the same rules apply to any sub-arrays.
83 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
85 if ( !is_null( $indent ) ) {
87 $indstr = "\n" . str_repeat( " ", $indent );
91 $elemName = str_replace( ' ', '_', $elemName );
93 switch ( gettype( $elemValue ) ) {
95 if ( isset ( $elemValue['*'] ) ) {
96 $subElemContent = $elemValue['*'];
98 $subElemContent = Sanitizer
::encodeAttribute( $subElemContent );
99 unset ( $elemValue['*'] );
101 // Add xml:space="preserve" to the
102 // element so XML parsers will leave
103 // whitespace in the content alone
104 $elemValue['xml:space'] = 'preserve';
106 $subElemContent = null;
109 if ( isset ( $elemValue['_element'] ) ) {
110 $subElemIndName = $elemValue['_element'];
111 unset ( $elemValue['_element'] );
113 $subElemIndName = null;
116 $indElements = array ();
117 $subElements = array ();
118 foreach ( $elemValue as $subElemId => & $subElemValue ) {
119 if ( is_string( $subElemValue ) && $doublequote )
120 $subElemValue = Sanitizer
::encodeAttribute( $subElemValue );
122 if ( gettype( $subElemId ) === 'integer' ) {
123 $indElements[] = $subElemValue;
124 unset ( $elemValue[$subElemId] );
125 } elseif ( is_array( $subElemValue ) ) {
126 $subElements[$subElemId] = $subElemValue;
127 unset ( $elemValue[$subElemId] );
131 if ( is_null( $subElemIndName ) && count( $indElements ) )
132 ApiBase
:: dieDebug( __METHOD__
, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
134 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) )
135 ApiBase
:: dieDebug( __METHOD__
, "($elemName, ...) has content and subelements" );
137 if ( !is_null( $subElemContent ) ) {
138 $retval .= $indstr . Xml
::element( $elemName, $elemValue, $subElemContent );
139 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
140 $retval .= $indstr . Xml
::element( $elemName, $elemValue );
142 $retval .= $indstr . Xml
::element( $elemName, $elemValue, null );
144 foreach ( $subElements as $subElemId => & $subElemValue )
145 $retval .= self
::recXmlPrint( $subElemId, $subElemValue, $indent );
147 foreach ( $indElements as $subElemId => & $subElemValue )
148 $retval .= self
::recXmlPrint( $subElemIndName, $subElemValue, $indent );
150 $retval .= $indstr . Xml
::closeElement( $elemName );
157 $retval .= $indstr . Xml
::element( $elemName, null, $elemValue );
163 $nt = Title
::newFromText( $this->mXslt
);
164 if ( is_null( $nt ) ||
!$nt->exists() ) {
165 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
168 if ( $nt->getNamespace() != NS_MEDIAWIKI
) {
169 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
172 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
173 $this->setWarning( 'Stylesheet should have .xsl extension.' );
176 $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
179 public function getAllowedParams() {
181 'xmldoublequote' => false,
186 public function getParamDescription() {
188 'xmldoublequote' => 'If specified, double quotes all attributes and content.',
189 'xslt' => 'If specified, adds <xslt> as stylesheet',
193 public function getDescription() {
194 return 'Output data in XML format' . parent
:: getDescription();
197 public function getVersion() {
198 return __CLASS__
. ': $Id$';