06bd9f33623cb489411d72e988c0000afb80079f
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 $data = array( 'xmlns' => self
::$namespace ) +
$this->getResultData();
73 $data = $this->getResultData();
77 self
::recXmlPrint( $this->mRootElemName
,
79 $this->getIsHtml() ?
- 2 : null,
86 * 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 other keys are not important and replaces them with the value['_element'].
90 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
92 * 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.
93 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
95 * If neither key is found, all keys become element names, and values become element content.
96 * The method is recursive, so the same rules apply to any sub-arrays.
101 * @param $doublequote bool
105 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
107 if ( !is_null( $indent ) ) {
109 $indstr = "\n" . str_repeat( ' ', $indent );
113 $elemName = str_replace( ' ', '_', $elemName );
115 switch ( gettype( $elemValue ) ) {
117 if ( isset( $elemValue['*'] ) ) {
118 $subElemContent = $elemValue['*'];
119 if ( $doublequote ) {
120 $subElemContent = Sanitizer
::encodeAttribute( $subElemContent );
122 unset( $elemValue['*'] );
124 // Add xml:space="preserve" to the
125 // element so XML parsers will leave
126 // whitespace in the content alone
127 $elemValue['xml:space'] = 'preserve';
129 $subElemContent = null;
132 if ( isset( $elemValue['_element'] ) ) {
133 $subElemIndName = $elemValue['_element'];
134 unset( $elemValue['_element'] );
136 $subElemIndName = null;
139 $indElements = array();
140 $subElements = array();
141 foreach ( $elemValue as $subElemId => & $subElemValue ) {
142 if ( is_string( $subElemValue ) && $doublequote ) {
143 $subElemValue = Sanitizer
::encodeAttribute( $subElemValue );
146 if ( gettype( $subElemId ) === 'integer' ) {
147 $indElements[] = $subElemValue;
148 unset( $elemValue[$subElemId] );
149 } elseif ( is_array( $subElemValue ) ) {
150 $subElements[$subElemId] = $subElemValue;
151 unset ( $elemValue[$subElemId] );
155 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
156 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
159 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
160 ApiBase
::dieDebug( __METHOD__
, "($elemName, ...) has content and subelements" );
163 if ( !is_null( $subElemContent ) ) {
164 $retval .= $indstr . Xml
::element( $elemName, $elemValue, $subElemContent );
165 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
166 $retval .= $indstr . Xml
::element( $elemName, $elemValue );
168 $retval .= $indstr . Xml
::element( $elemName, $elemValue, null );
170 foreach ( $subElements as $subElemId => & $subElemValue ) {
171 $retval .= self
::recXmlPrint( $subElemId, $subElemValue, $indent );
174 foreach ( $indElements as &$subElemValue ) {
175 $retval .= self
::recXmlPrint( $subElemIndName, $subElemValue, $indent );
178 $retval .= $indstr . Xml
::closeElement( $elemName );
185 $retval .= $indstr . Xml
::element( $elemName, null, $elemValue );
192 $nt = Title
::newFromText( $this->mXslt
);
193 if ( is_null( $nt ) ||
!$nt->exists() ) {
194 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
197 if ( $nt->getNamespace() != NS_MEDIAWIKI
) {
198 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
201 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
202 $this->setWarning( 'Stylesheet should have .xsl extension.' );
205 $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
208 public function getAllowedParams() {
210 'xmldoublequote' => false,
212 'includexmlnamespace' => false,
216 public function getParamDescription() {
218 'xmldoublequote' => 'If specified, double quotes all attributes and content',
219 'xslt' => 'If specified, adds <xslt> as stylesheet',
220 'includexmlnamespace' => 'If specified, adds an XML namespace'
224 public function getDescription() {
225 return 'Output data in XML format' . parent
::getDescription();
228 public function getVersion() {
229 return __CLASS__
. ': $Id$';