0c81c2fc9c68dc2a9fdc1e2131f6386c10f22ed2
[lhc/web/wiklou.git] / includes / api / ApiFormatXml.php
1 <?php
2
3 /*
4 * Created on Sep 19, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiFormatBase.php');
29 }
30
31 /**
32 * @addtogroup API
33 */
34 class ApiFormatXml extends ApiFormatBase {
35
36 private $mRootElemName = 'api';
37
38 public function __construct($main, $format) {
39 parent :: __construct($main, $format);
40 }
41
42 public function getMimeType() {
43 return 'text/xml';
44 }
45
46 public function getNeedsRawData() {
47 return true;
48 }
49
50 public function setRootElement($rootElemName) {
51 $this->mRootElemName = $rootElemName;
52 }
53
54 public function execute() {
55 $this->printText('<?xml version="1.0" encoding="utf-8"?>');
56 $this->recXmlPrint($this->mRootElemName, $this->getResultData(), $this->getIsHtml() ? -2 : null);
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 if (!is_null($indent)) {
74 $indent += 2;
75 $indstr = "\n" . str_repeat(" ", $indent);
76 } else {
77 $indstr = '';
78 }
79
80 switch (gettype($elemValue)) {
81 case 'array' :
82
83 if (isset ($elemValue['*'])) {
84 $subElemContent = $elemValue['*'];
85 unset ($elemValue['*']);
86 } else {
87 $subElemContent = null;
88 }
89
90 if (isset ($elemValue['_element'])) {
91 $subElemIndName = $elemValue['_element'];
92 unset ($elemValue['_element']);
93 } else {
94 $subElemIndName = null;
95 }
96
97 $indElements = array ();
98 $subElements = array ();
99 foreach ($elemValue as $subElemId => & $subElemValue) {
100 if (gettype($subElemId) === 'integer') {
101 $indElements[] = $subElemValue;
102 unset ($elemValue[$subElemId]);
103 } elseif (is_array($subElemValue)) {
104 $subElements[$subElemId] = $subElemValue;
105 unset ($elemValue[$subElemId]);
106 }
107 }
108
109 if (is_null($subElemIndName) && !empty ($indElements))
110 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().");
111
112 if (!empty ($subElements) && !empty ($indElements) && !is_null($subElemContent))
113 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
114
115 if (!is_null($subElemContent)) {
116 $this->printText($indstr . wfElement($elemName, $elemValue, $subElemContent));
117 } elseif (empty ($indElements) && empty ($subElements)) {
118 $this->printText($indstr . wfElement($elemName, $elemValue));
119 } else {
120 $this->printText($indstr . wfElement($elemName, $elemValue, null));
121
122 foreach ($subElements as $subElemId => & $subElemValue)
123 $this->recXmlPrint($subElemId, $subElemValue, $indent);
124
125 foreach ($indElements as $subElemId => & $subElemValue)
126 $this->recXmlPrint($subElemIndName, $subElemValue, $indent);
127
128 $this->printText($indstr . wfCloseElement($elemName));
129 }
130 break;
131 case 'object' :
132 // ignore
133 break;
134 default :
135 $this->printText($indstr . wfElement($elemName, null, $elemValue));
136 break;
137 }
138 }
139 protected function getDescription() {
140 return 'Output data in XML format' . parent :: getDescription();
141 }
142
143 public function getVersion() {
144 return __CLASS__ . ': $Id$';
145 }
146 }
147 ?>