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 Execute() {
44 extract($this->ExtractRequestParams());
46 if ($xmlindent ||
$this->GetIsHtml())
51 $this->PrintText('<?xml version="1.0" encoding="utf-8"?>');
52 $this->recXmlPrint('api', $this->GetResult()->GetData(), $xmlindent);
56 * This method takes an array and converts it into an xml.
57 * There are several noteworthy cases:
59 * If array contains a key "_element", then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
60 * Example: name="root", value = array( "_element"=>"page", "x", "y", "z") creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
62 * 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.
63 * Example: name="root", value = array( "*"=>"text", "lang"=>"en", "id"=>10) creates <root lang="en" id="10">text</root>
65 * If neither key is found, all keys become element names, and values become element content.
66 * The method is recursive, so the same rules apply to any sub-arrays.
68 function recXmlPrint($elemName, $elemValue, $indent) {
70 if (!is_null($indent)) {
72 $indstr = "\n" . str_repeat(" ", $indent);
75 switch (gettype($elemValue)) {
77 if (array_key_exists('*', $elemValue)) {
78 $subElemContent = $elemValue['*'];
79 unset ($elemValue['*']);
80 if (gettype($subElemContent) === 'array') {
81 $this->PrintText($indstr . wfElement($elemName, $elemValue, null));
82 $this->recXmlPrint($elemName, $subElemContent, $indent);
83 $this->PrintText($indstr . "</$elemName>");
85 $this->PrintText($indstr . wfElement($elemName, $elemValue, $subElemContent));
88 $this->PrintText($indstr . wfElement($elemName, null, null));
89 if (array_key_exists('_element', $elemValue)) {
90 $subElemName = $elemValue['_element'];
91 foreach ($elemValue as $subElemId => & $subElemValue) {
92 if ($subElemId !== '_element') {
93 $this->recXmlPrint($subElemName, $subElemValue, $indent);
97 foreach ($elemValue as $subElemName => & $subElemValue) {
98 $this->recXmlPrint($subElemName, $subElemValue, $indent);
101 $this->PrintText($indstr . "</$elemName>");
108 $this->PrintText($indstr . wfElement($elemName, null, $elemValue));
112 protected function GetDescription() {
113 return 'Output data in XML format';
116 protected function GetAllowedParams() {
122 protected function GetParamDescription() {
124 'xmlindent' => 'Enable XML indentation'