* API: Overall query-related cleanup.
[lhc/web/wiklou.git] / includes / api / ApiFormatXml.php
1 <?php
2
3
4 /*
5 * Created on Sep 19, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
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.
15 *
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.
20 *
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
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ("ApiFormatBase.php");
30 }
31
32 class ApiFormatXml extends ApiFormatBase {
33
34 public function __construct($main, $format) {
35 parent :: __construct($main, $format);
36 }
37
38 public function GetMimeType() {
39 return 'text/xml';
40 }
41
42 public function GetNeedsRawData() {
43 return true;
44 }
45
46 public function Execute() {
47 $xmlindent = null;
48 extract($this->ExtractRequestParams());
49
50 if ($xmlindent || $this->GetIsHtml())
51 $xmlindent = -2;
52 else
53 $xmlindent = null;
54
55 $this->PrintText('<?xml version="1.0" encoding="utf-8"?>');
56 $this->recXmlPrint('api', $this->GetResult()->GetData(), $xmlindent);
57 }
58
59 /**
60 * This method takes an array and converts it into an xml.
61 * There are several noteworthy cases:
62 *
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>
65 *
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>
68 *
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.
71 */
72 function recXmlPrint($elemName, $elemValue, $indent) {
73 $indstr = "";
74 if (!is_null($indent)) {
75 $indent += 2;
76 $indstr = "\n" . str_repeat(" ", $indent);
77 }
78
79 switch (gettype($elemValue)) {
80 case 'array' :
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>");
88 } else {
89 $this->PrintText($indstr . wfElement($elemName, $elemValue, $subElemContent));
90 }
91 } else {
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);
98 }
99 }
100 } else {
101 foreach ($elemValue as $subElemName => & $subElemValue) {
102 $this->recXmlPrint($subElemName, $subElemValue, $indent);
103 }
104 }
105 $this->PrintText($indstr . "</$elemName>");
106 }
107 break;
108 case 'object' :
109 // ignore
110 break;
111 default :
112 $this->PrintText($indstr . wfElement($elemName, null, $elemValue));
113 break;
114 }
115 }
116 protected function GetDescription() {
117 return 'Output data in XML format';
118 }
119
120 protected function GetAllowedParams() {
121 return array (
122 'xmlindent' => false
123 );
124 }
125
126 protected function GetParamDescription() {
127 return array (
128 'xmlindent' => 'Enable XML indentation'
129 );
130 }
131 }
132 ?>