Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / api / ApiFormatXml.php
blob4ec149c09ed6845afc981a730247e80854b173ef
1 <?php
2 /**
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
24 * @file
27 /**
28 * API XML output formatter
29 * @ingroup API
31 class ApiFormatXml extends ApiFormatBase {
33 private $mRootElemName = 'api';
34 public static $namespace = 'http://www.mediawiki.org/xml/api/';
35 private $mIncludeNamespace = false;
36 private $mXslt = null;
38 public function getMimeType() {
39 return 'text/xml';
42 public function getNeedsRawData() {
43 return true;
46 public function setRootElement( $rootElemName ) {
47 $this->mRootElemName = $rootElemName;
50 public function execute() {
51 $params = $this->extractRequestParams();
52 $this->mIncludeNamespace = $params['includexmlnamespace'];
53 $this->mXslt = $params['xslt'];
55 $this->printText( '<?xml version="1.0"?>' );
56 if ( !is_null( $this->mXslt ) ) {
57 $this->addXslt();
59 if ( $this->mIncludeNamespace ) {
60 // If the result data already contains an 'xmlns' namespace added
61 // for custom XML output types, it will override the one for the
62 // generic API results.
63 // This allows API output of other XML types like Atom, RSS, RSD.
64 $data = $this->getResultData() + array( 'xmlns' => self::$namespace );
65 } else {
66 $data = $this->getResultData();
69 $this->printText(
70 self::recXmlPrint( $this->mRootElemName,
71 $data,
72 $this->getIsHtml() ? - 2 : null
77 /**
78 * This method takes an array and converts it to XML.
80 * There are several noteworthy cases:
82 * If array contains a key '_element', then the code assumes that ALL
83 * other keys are not important and replaces them with the
84 * value['_element'].
86 * @par Example:
87 * @verbatim
88 * name='root', value = array( '_element'=>'page', 'x', 'y', 'z')
89 * @endverbatim
90 * creates:
91 * @verbatim
92 * <root> <page>x</page> <page>y</page> <page>z</page> </root>
93 * @endverbatim
95 * If any of the array's element key is '*', then the code treats all
96 * other key->value pairs as attributes, and the value['*'] as the
97 * element's content.
99 * @par Example:
100 * @verbatim
101 * name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10)
102 * @endverbatim
103 * creates:
104 * @verbatim
105 * <root lang='en' id='10'>text</root>
106 * @endverbatim
108 * Finally neither key is found, all keys become element names, and values
109 * become element content.
111 * @note The method is recursive, so the same rules apply to any
112 * sub-arrays.
114 * @param $elemName
115 * @param $elemValue
116 * @param $indent
118 * @return string
120 public static function recXmlPrint( $elemName, $elemValue, $indent ) {
121 $retval = '';
122 if ( !is_null( $indent ) ) {
123 $indent += 2;
124 $indstr = "\n" . str_repeat( ' ', $indent );
125 } else {
126 $indstr = '';
128 $elemName = str_replace( ' ', '_', $elemName );
130 if ( is_array( $elemValue ) ) {
131 if ( isset( $elemValue['*'] ) ) {
132 $subElemContent = $elemValue['*'];
133 unset( $elemValue['*'] );
135 // Add xml:space="preserve" to the
136 // element so XML parsers will leave
137 // whitespace in the content alone
138 $elemValue['xml:space'] = 'preserve';
139 } else {
140 $subElemContent = null;
143 if ( isset( $elemValue['_element'] ) ) {
144 $subElemIndName = $elemValue['_element'];
145 unset( $elemValue['_element'] );
146 } else {
147 $subElemIndName = null;
150 $indElements = array();
151 $subElements = array();
152 foreach ( $elemValue as $subElemId => & $subElemValue ) {
153 if ( is_int( $subElemId ) ) {
154 $indElements[] = $subElemValue;
155 unset( $elemValue[$subElemId] );
156 } elseif ( is_array( $subElemValue ) ) {
157 $subElements[$subElemId] = $subElemValue;
158 unset( $elemValue[$subElemId] );
162 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
163 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
166 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
167 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
170 if ( !is_null( $subElemContent ) ) {
171 $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
172 } elseif ( !count( $indElements ) && !count( $subElements ) ) {
173 $retval .= $indstr . Xml::element( $elemName, $elemValue );
174 } else {
175 $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
177 foreach ( $subElements as $subElemId => & $subElemValue ) {
178 $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
181 foreach ( $indElements as &$subElemValue ) {
182 $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
185 $retval .= $indstr . Xml::closeElement( $elemName );
187 } elseif ( !is_object( $elemValue ) ) {
188 // to make sure null value doesn't produce unclosed element,
189 // which is what Xml::element( $elemName, null, null ) returns
190 if ( $elemValue === null ) {
191 $retval .= $indstr . Xml::element( $elemName );
192 } else {
193 $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
196 return $retval;
199 function addXslt() {
200 $nt = Title::newFromText( $this->mXslt );
201 if ( is_null( $nt ) || !$nt->exists() ) {
202 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
203 return;
205 if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
206 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
207 return;
209 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
210 $this->setWarning( 'Stylesheet should have .xsl extension.' );
211 return;
213 $this->printText( '<?xml-stylesheet href="' . htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
216 public function getAllowedParams() {
217 return array(
218 'xslt' => null,
219 'includexmlnamespace' => false,
223 public function getParamDescription() {
224 return array(
225 'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
226 . 'in the MediaWiki namespace whose page name ends with ".xsl"',
227 'includexmlnamespace' => 'If specified, adds an XML namespace'
231 public function getDescription() {
232 return 'Output data in XML format' . parent::getDescription();