11efcf948cc7c79956587a3893361031f396cede
5 * Created on Sep 19, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiFormatBase.php');
32 class ApiFormatXml
extends ApiFormatBase
{
34 public function __construct($main, $format) {
35 parent
:: __construct($main, $format);
38 public function getMimeType() {
42 public function getNeedsRawData() {
46 public function execute() {
48 extract($this->extractRequestParams());
50 if ($xmlindent ||
$this->getIsHtml())
55 $this->printText('<?xml version="1.0" encoding="utf-8"?>');
56 $this->recXmlPrint('api', $this->getResultData(), $xmlindent);
60 * This method takes an array and converts it into an xml.
61 * There are several noteworthy cases:
63 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
64 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
66 * 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.
67 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
69 * If neither key is found, all keys become element names, and values become element content.
70 * The method is recursive, so the same rules apply to any sub-arrays.
72 function recXmlPrint($elemName, $elemValue, $indent) {
73 if (!is_null($indent)) {
75 $indstr = "\n" . str_repeat(" ", $indent);
80 switch (gettype($elemValue)) {
83 if (isset ($elemValue['*'])) {
84 $subElemContent = $elemValue['*'];
85 unset ($elemValue['*']);
87 $subElemContent = null;
90 if (isset ($elemValue['_element'])) {
91 $subElemIndName = $elemValue['_element'];
92 unset ($elemValue['_element']);
94 $subElemIndName = null;
97 $indElements = array ();
98 $subElements = array ();
99 foreach ($elemValue as $subElemId => & $subElemValue) {
100 if (gettype($subElemId) === 'integer') {
101 if (!is_array($subElemValue))
102 ApiBase
:: dieDebug(__METHOD__
, "($elemName, ...) has a scalar indexed value.");
103 $indElements[] = $subElemValue;
104 unset ($elemValue[$subElemId]);
105 } elseif (is_array($subElemValue)) {
106 $subElements[$subElemId] = $subElemValue;
107 unset ($elemValue[$subElemId]);
111 if (is_null($subElemIndName) && !empty ($indElements))
112 ApiBase
:: dieDebug(__METHOD__
, "($elemName, ...) has integer keys without _element value");
114 if (!empty ($subElements) && !empty ($indElements) && !is_null($subElemContent))
115 ApiBase
:: dieDebug(__METHOD__
, "($elemName, ...) has content and subelements");
117 if (!is_null($subElemContent)) {
118 $this->printText($indstr . wfElement($elemName, $elemValue, $subElemContent));
119 } elseif (empty ($indElements) && empty ($subElements)) {
120 $this->printText($indstr . wfElement($elemName, $elemValue));
122 $this->printText($indstr . wfElement($elemName, $elemValue, null));
124 foreach ($subElements as $subElemId => & $subElemValue)
125 $this->recXmlPrint($subElemId, $subElemValue, $indent);
127 foreach ($indElements as $subElemId => & $subElemValue)
128 $this->recXmlPrint($subElemIndName, $subElemValue, $indent);
130 $this->printText($indstr . wfCloseElement($elemName));
137 $this->printText($indstr . wfElement($elemName, null, $elemValue));
141 protected function getDescription() {
142 return 'Output data in XML format';
145 protected function getAllowedParams() {
151 protected function getParamDescription() {
153 'xmlindent' => 'Enable XML indentation'
157 public function getVersion() {
158 return __CLASS__
. ': $Id$';