summaryrefslogtreecommitdiff
path: root/includes/api/ApiFormatXml.php
blob: 4ec149c09ed6845afc981a730247e80854b173ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
 *
 *
 * Created on Sep 19, 2006
 *
 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

/**
 * API XML output formatter
 * @ingroup API
 */
class ApiFormatXml extends ApiFormatBase {

	private $mRootElemName = 'api';
	public static $namespace = 'http://www.mediawiki.org/xml/api/';
	private $mIncludeNamespace = false;
	private $mXslt = null;

	public function getMimeType() {
		return 'text/xml';
	}

	public function getNeedsRawData() {
		return true;
	}

	public function setRootElement( $rootElemName ) {
		$this->mRootElemName = $rootElemName;
	}

	public function execute() {
		$params = $this->extractRequestParams();
		$this->mIncludeNamespace = $params['includexmlnamespace'];
		$this->mXslt = $params['xslt'];

		$this->printText( '<?xml version="1.0"?>' );
		if ( !is_null( $this->mXslt ) ) {
			$this->addXslt();
		}
		if ( $this->mIncludeNamespace ) {
			// If the result data already contains an 'xmlns' namespace added
			// for custom XML output types, it will override the one for the
			// generic API results.
			// This allows API output of other XML types like Atom, RSS, RSD.
			$data = $this->getResultData() + array( 'xmlns' => self::$namespace );
		} else {
			$data = $this->getResultData();
		}

		$this->printText(
			self::recXmlPrint( $this->mRootElemName,
				$data,
				$this->getIsHtml() ? - 2 : null
			)
		);
	}

	/**
	 * This method takes an array and converts it to XML.
	 *
	 * There are several noteworthy cases:
	 *
	 * If array contains a key '_element', then the code assumes that ALL
	 * other keys are not important and replaces them with the
	 * value['_element'].
	 *
	 * @par Example:
	 * @verbatim
	 * name='root', value = array( '_element'=>'page', 'x', 'y', 'z')
	 * @endverbatim
	 * creates:
	 * @verbatim
	 * <root>  <page>x</page>  <page>y</page>  <page>z</page> </root>
	 * @endverbatim
	 *
	 * 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.
	 *
	 * @par Example:
	 * @verbatim
	 * name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10)
	 * @endverbatim
	 * creates:
	 * @verbatim
	 * <root lang='en' id='10'>text</root>
	 * @endverbatim
	 *
	 * Finally neither key is found, all keys become element names, and values
	 * become element content.
	 *
	 * @note The method is recursive, so the same rules apply to any
	 * sub-arrays.
	 *
	 * @param $elemName
	 * @param $elemValue
	 * @param $indent
	 *
	 * @return string
	 */
	public static function recXmlPrint( $elemName, $elemValue, $indent ) {
		$retval = '';
		if ( !is_null( $indent ) ) {
			$indent += 2;
			$indstr = "\n" . str_repeat( ' ', $indent );
		} else {
			$indstr = '';
		}
		$elemName = str_replace( ' ', '_', $elemName );

		if ( is_array( $elemValue ) ) {
			if ( isset( $elemValue['*'] ) ) {
				$subElemContent = $elemValue['*'];
				unset( $elemValue['*'] );

				// Add xml:space="preserve" to the
				// element so XML parsers will leave
				// whitespace in the content alone
				$elemValue['xml:space'] = 'preserve';
			} else {
				$subElemContent = null;
			}

			if ( isset( $elemValue['_element'] ) ) {
				$subElemIndName = $elemValue['_element'];
				unset( $elemValue['_element'] );
			} else {
				$subElemIndName = null;
			}

			$indElements = array();
			$subElements = array();
			foreach ( $elemValue as $subElemId => & $subElemValue ) {
				if ( is_int( $subElemId ) ) {
					$indElements[] = $subElemValue;
					unset( $elemValue[$subElemId] );
				} elseif ( is_array( $subElemValue ) ) {
					$subElements[$subElemId] = $subElemValue;
					unset( $elemValue[$subElemId] );
				}
			}

			if ( is_null( $subElemIndName ) && count( $indElements ) ) {
				ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
			}

			if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
				ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
			}

			if ( !is_null( $subElemContent ) ) {
				$retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
			} elseif ( !count( $indElements ) && !count( $subElements ) ) {
				$retval .= $indstr . Xml::element( $elemName, $elemValue );
			} else {
				$retval .= $indstr . Xml::element( $elemName, $elemValue, null );

				foreach ( $subElements as $subElemId => & $subElemValue ) {
					$retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
				}

				foreach ( $indElements as &$subElemValue ) {
					$retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
				}

				$retval .= $indstr . Xml::closeElement( $elemName );
			}
		} elseif ( !is_object( $elemValue ) ) {
			// to make sure null value doesn't produce unclosed element,
			// which is what Xml::element( $elemName, null, null ) returns
			if ( $elemValue === null ) {
				$retval .= $indstr . Xml::element( $elemName );
			} else {
				$retval .= $indstr . Xml::element( $elemName, null, $elemValue );
			}
		}
		return $retval;
	}

	function addXslt() {
		$nt = Title::newFromText( $this->mXslt );
		if ( is_null( $nt ) || !$nt->exists() ) {
			$this->setWarning( 'Invalid or non-existent stylesheet specified' );
			return;
		}
		if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
			$this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
			return;
		}
		if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
			$this->setWarning( 'Stylesheet should have .xsl extension.' );
			return;
		}
		$this->printText( '<?xml-stylesheet href="' . htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
	}

	public function getAllowedParams() {
		return array(
			'xslt' => null,
			'includexmlnamespace' => false,
		);
	}

	public function getParamDescription() {
		return array(
			'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
				. 'in the MediaWiki namespace whose page name ends with ".xsl"',
			'includexmlnamespace' => 'If specified, adds an XML namespace'
		);
	}

	public function getDescription() {
		return 'Output data in XML format' . parent::getDescription();
	}
}