* API: major refactoring
[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 Execute() {
43 $xmlindent = null;
44 extract($this->ExtractRequestParams());
45
46 if ($xmlindent || $this->GetIsHtml())
47 $xmlindent = -2;
48 else
49 $xmlindent = null;
50
51 $this->PrintText('<?xml version="1.0" encoding="utf-8"?>');
52 $this->recXmlPrint('api', $this->GetResult()->GetData(), $xmlindent);
53 }
54
55 /**
56 * This method takes an array and converts it into an xml.
57 * There are several noteworthy cases:
58 *
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>
61 *
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>
64 *
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.
67 */
68 function recXmlPrint($elemName, $elemValue, $indent) {
69 $indstr = "";
70 if (!is_null($indent)) {
71 $indent += 2;
72 $indstr = "\n" . str_repeat(" ", $indent);
73 }
74
75 switch (gettype($elemValue)) {
76 case 'array' :
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>");
84 } else {
85 $this->PrintText($indstr . wfElement($elemName, $elemValue, $subElemContent));
86 }
87 } else {
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);
94 }
95 }
96 } else {
97 foreach ($elemValue as $subElemName => & $subElemValue) {
98 $this->recXmlPrint($subElemName, $subElemValue, $indent);
99 }
100 }
101 $this->PrintText($indstr . "</$elemName>");
102 }
103 break;
104 case 'object' :
105 // ignore
106 break;
107 default :
108 $this->PrintText($indstr . wfElement($elemName, null, $elemValue));
109 break;
110 }
111 }
112 protected function GetDescription() {
113 return 'Output data in XML format';
114 }
115
116 protected function GetAllowedParams() {
117 return array (
118 'xmlindent' => false
119 );
120 }
121
122 protected function GetParamDescription() {
123 return array (
124 'xmlindent' => 'Enable XML indentation'
125 );
126 }
127 }
128 ?>