Special:Userrights didn't recognize user as self if person didn't capitalize
[lhc/web/wiklou.git] / includes / api / ApiFormatXml.php
1 <?php
2
3 /**
4 * Created on Sep 19, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiFormatBase.php' );
29 }
30
31 /**
32 * API XML output formatter
33 * @ingroup API
34 */
35 class ApiFormatXml extends ApiFormatBase {
36
37 private $mRootElemName = 'api';
38 private $mDoubleQuote = false;
39 private $mXslt = null;
40
41 public function __construct( $main, $format ) {
42 parent::__construct( $main, $format );
43 }
44
45 public function getMimeType() {
46 return 'text/xml';
47 }
48
49 public function getNeedsRawData() {
50 return true;
51 }
52
53 public function setRootElement( $rootElemName ) {
54 $this->mRootElemName = $rootElemName;
55 }
56
57 public function execute() {
58 $params = $this->extractRequestParams();
59 $this->mDoubleQuote = $params['xmldoublequote'];
60 $this->mXslt = $params['xslt'];
61
62 $this->printText( '<?xml version="1.0"?>' );
63 if ( !is_null( $this->mXslt ) ) {
64 $this->addXslt();
65 }
66 $this->printText(
67 self::recXmlPrint( $this->mRootElemName,
68 $this->getResultData(),
69 $this->getIsHtml() ? - 2 : null,
70 $this->mDoubleQuote
71 )
72 );
73 }
74
75 /**
76 * This method takes an array and converts it to XML.
77 * There are several noteworthy cases:
78 *
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>
81 *
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>
84 *
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.
87 */
88 public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
89 $retval = '';
90 if ( !is_null( $indent ) ) {
91 $indent += 2;
92 $indstr = "\n" . str_repeat( ' ', $indent );
93 } else {
94 $indstr = '';
95 }
96 $elemName = str_replace( ' ', '_', $elemName );
97
98 switch ( gettype( $elemValue ) ) {
99 case 'array':
100 if ( isset( $elemValue['*'] ) ) {
101 $subElemContent = $elemValue['*'];
102 if ( $doublequote ) {
103 $subElemContent = Sanitizer::encodeAttribute( $subElemContent );
104 }
105 unset( $elemValue['*'] );
106
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';
111 } else {
112 $subElemContent = null;
113 }
114
115 if ( isset( $elemValue['_element'] ) ) {
116 $subElemIndName = $elemValue['_element'];
117 unset( $elemValue['_element'] );
118 } else {
119 $subElemIndName = null;
120 }
121
122 $indElements = array();
123 $subElements = array();
124 foreach ( $elemValue as $subElemId => & $subElemValue ) {
125 if ( is_string( $subElemValue ) && $doublequote ) {
126 $subElemValue = Sanitizer::encodeAttribute( $subElemValue );
127 }
128
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] );
135 }
136 }
137
138 if ( is_null( $subElemIndName ) && count( $indElements ) ) {
139 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
140 }
141
142 if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
143 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
144 }
145
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 );
150 } else {
151 $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
152
153 foreach ( $subElements as $subElemId => & $subElemValue ) {
154 $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
155 }
156
157 foreach ( $indElements as $subElemId => & $subElemValue ) {
158 $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
159 }
160
161 $retval .= $indstr . Xml::closeElement( $elemName );
162 }
163 break;
164 case 'object':
165 // ignore
166 break;
167 default:
168 $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
169 break;
170 }
171 return $retval;
172 }
173
174 function addXslt() {
175 $nt = Title::newFromText( $this->mXslt );
176 if ( is_null( $nt ) || !$nt->exists() ) {
177 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
178 return;
179 }
180 if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
181 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
182 return;
183 }
184 if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
185 $this->setWarning( 'Stylesheet should have .xsl extension.' );
186 return;
187 }
188 $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
189 }
190
191 public function getAllowedParams() {
192 return array(
193 'xmldoublequote' => false,
194 'xslt' => null,
195 );
196 }
197
198 public function getParamDescription() {
199 return array(
200 'xmldoublequote' => 'If specified, double quotes all attributes and content',
201 'xslt' => 'If specified, adds <xslt> as stylesheet',
202 );
203 }
204
205 public function getDescription() {
206 return 'Output data in XML format' . parent::getDescription();
207 }
208
209 public function getVersion() {
210 return __CLASS__ . ': $Id$';
211 }
212 }