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->GetResult()->GetData(), $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) {
74 if (!is_null($indent)) {
76 $indstr = "\n" . str_repeat(" ", $indent);
79 switch (gettype($elemValue)) {
81 if (array_key_exists('*', $elemValue)) {
82 $subElemContent = $elemValue['*'];
83 unset ($elemValue['*']);
84 if (gettype($subElemContent) === 'array') {
85 $this->PrintText($indstr . wfElement($elemName, $elemValue, null));
86 $this->recXmlPrint($elemName, $subElemContent, $indent);
87 $this->PrintText($indstr . "</$elemName>");
89 $this->PrintText($indstr . wfElement($elemName, $elemValue, $subElemContent));
92 $this->PrintText($indstr . wfElement($elemName, null, null));
93 if (array_key_exists('_element', $elemValue)) {
94 $subElemName = $elemValue['_element'];
95 foreach ($elemValue as $subElemId => & $subElemValue) {
96 if ($subElemId !== '_element') {
97 $this->recXmlPrint($subElemName, $subElemValue, $indent);
101 foreach ($elemValue as $subElemName => & $subElemValue) {
102 $this->recXmlPrint($subElemName, $subElemValue, $indent);
105 $this->PrintText($indstr . "</$elemName>");
112 $this->PrintText($indstr . wfElement($elemName, null, $elemValue));
116 protected function GetDescription() {
117 return 'Output data in XML format';
120 protected function GetAllowedParams() {
126 protected function GetParamDescription() {
128 'xmlindent' => 'Enable XML indentation'