(bug 19528) Added XSLT parameter to API queries in format=xml
[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 (C) 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 * @ingroup API
33 */
34 class ApiFormatXml extends ApiFormatBase {
35
36 private $mRootElemName = 'api';
37 private $mDoubleQuote = false;
38 private $mXslt = null;
39
40 public function __construct($main, $format) {
41 parent :: __construct($main, $format);
42 }
43
44 public function getMimeType() {
45 return 'text/xml';
46 }
47
48 public function getNeedsRawData() {
49 return true;
50 }
51
52 public function setRootElement($rootElemName) {
53 $this->mRootElemName = $rootElemName;
54 }
55
56 public function execute() {
57 $params = $this->extractRequestParams();
58 $this->mDoubleQuote = $params['xmldoublequote'];
59 $this->mXslt = $params['xslt'];
60
61 $this->printText('<?xml version="1.0"?>');
62 if (!is_null($this->mXslt))
63 $this->addXslt();
64 $this->recXmlPrint($this->mRootElemName, $this->getResultData(), $this->getIsHtml() ? -2 : null);
65 }
66
67 /**
68 * This method takes an array and converts it to XML.
69 * There are several noteworthy cases:
70 *
71 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
72 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
73 *
74 * 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.
75 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
76 *
77 * If neither key is found, all keys become element names, and values become element content.
78 * The method is recursive, so the same rules apply to any sub-arrays.
79 */
80 function recXmlPrint($elemName, $elemValue, $indent) {
81 if (!is_null($indent)) {
82 $indent += 2;
83 $indstr = "\n" . str_repeat(" ", $indent);
84 } else {
85 $indstr = '';
86 }
87 $elemName = str_replace(' ', '_', $elemName);
88
89 switch (gettype($elemValue)) {
90 case 'array' :
91 if (isset ($elemValue['*'])) {
92 $subElemContent = $elemValue['*'];
93 if ($this->mDoubleQuote)
94 $subElemContent = $this->doubleQuote($subElemContent);
95 unset ($elemValue['*']);
96
97 // Add xml:space="preserve" to the
98 // element so XML parsers will leave
99 // whitespace in the content alone
100 $elemValue['xml:space'] = 'preserve';
101 } else {
102 $subElemContent = null;
103 }
104
105 if (isset ($elemValue['_element'])) {
106 $subElemIndName = $elemValue['_element'];
107 unset ($elemValue['_element']);
108 } else {
109 $subElemIndName = null;
110 }
111
112 $indElements = array ();
113 $subElements = array ();
114 foreach ($elemValue as $subElemId => & $subElemValue) {
115 if (is_string($subElemValue) && $this->mDoubleQuote)
116 $subElemValue = $this->doubleQuote($subElemValue);
117
118 if (gettype($subElemId) === 'integer') {
119 $indElements[] = $subElemValue;
120 unset ($elemValue[$subElemId]);
121 } elseif (is_array($subElemValue)) {
122 $subElements[$subElemId] = $subElemValue;
123 unset ($elemValue[$subElemId]);
124 }
125 }
126
127 if (is_null($subElemIndName) && count($indElements))
128 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().");
129
130 if (count($subElements) && count($indElements) && !is_null($subElemContent))
131 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
132
133 if (!is_null($subElemContent)) {
134 $this->printText($indstr . Xml::element($elemName, $elemValue, $subElemContent));
135 } elseif (!count($indElements) && !count($subElements)) {
136 $this->printText($indstr . Xml::element($elemName, $elemValue));
137 } else {
138 $this->printText($indstr . Xml::element($elemName, $elemValue, null));
139
140 foreach ($subElements as $subElemId => & $subElemValue)
141 $this->recXmlPrint($subElemId, $subElemValue, $indent);
142
143 foreach ($indElements as $subElemId => & $subElemValue)
144 $this->recXmlPrint($subElemIndName, $subElemValue, $indent);
145
146 $this->printText($indstr . Xml::closeElement($elemName));
147 }
148 break;
149 case 'object' :
150 // ignore
151 break;
152 default :
153 $this->printText($indstr . Xml::element($elemName, null, $elemValue));
154 break;
155 }
156 }
157 function addXslt() {
158 $nt = Title::newFromText( $this->mXslt );
159 if ( is_null( $nt ) || !$nt->exists() ) {
160 $this->setWarning( 'Invalid or non-existent stylesheet specified' );
161 return;
162 }
163 if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
164 $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
165 return;
166 }
167 if ( substr( $nt->getText(), -4 ) !== '.xsl' ) {
168 $this->setWarning( 'Stylesheet should have .xsl extension.' );
169 return;
170 }
171 $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
172 }
173
174 private function doubleQuote( $text ) {
175 return Sanitizer::encodeAttribute( $text );
176 }
177
178 public function getAllowedParams() {
179 return array (
180 'xmldoublequote' => false,
181 'xslt' => null,
182 );
183 }
184
185 public function getParamDescription() {
186 return array (
187 'xmldoublequote' => 'If specified, double quotes all attributes and content.',
188 'xslt' => 'If specified, adds <xslt> as stylesheet',
189 );
190 }
191
192
193 public function getDescription() {
194 return 'Output data in XML format' . parent :: getDescription();
195 }
196
197 public function getVersion() {
198 return __CLASS__ . ': $Id$';
199 }
200 }