Introduce 'R' flag for -{}- tags. The flagged text will not be parsed
[mediawiki.git] / includes / api / ApiFormatXml.php
blob5d4e776f4a347cf7732bb23952fc9da0c860b809
1 <?php
4 /*
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 private $mRootElemName = 'api';
36 public function __construct($main, $format) {
37 parent :: __construct($main, $format);
40 public function getMimeType() {
41 return 'text/xml';
44 public function getNeedsRawData() {
45 return true;
48 public function setRootElement($rootElemName) {
49 $this->mRootElemName = $rootElemName;
52 public function execute() {
53 $this->printText('<?xml version="1.0" encoding="utf-8"?>');
54 $this->recXmlPrint($this->mRootElemName, $this->getResultData(), $this->getIsHtml() ? -2 : null);
57 /**
58 * This method takes an array and converts it into an xml.
59 * There are several noteworthy cases:
61 * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
62 * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
64 * 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.
65 * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
67 * If neither key is found, all keys become element names, and values become element content.
68 * The method is recursive, so the same rules apply to any sub-arrays.
70 function recXmlPrint($elemName, $elemValue, $indent) {
71 if (!is_null($indent)) {
72 $indent += 2;
73 $indstr = "\n" . str_repeat(" ", $indent);
74 } else {
75 $indstr = '';
78 switch (gettype($elemValue)) {
79 case 'array' :
81 if (isset ($elemValue['*'])) {
82 $subElemContent = $elemValue['*'];
83 unset ($elemValue['*']);
84 } else {
85 $subElemContent = null;
88 if (isset ($elemValue['_element'])) {
89 $subElemIndName = $elemValue['_element'];
90 unset ($elemValue['_element']);
91 } else {
92 $subElemIndName = null;
95 $indElements = array ();
96 $subElements = array ();
97 foreach ($elemValue as $subElemId => & $subElemValue) {
98 if (gettype($subElemId) === 'integer') {
99 $indElements[] = $subElemValue;
100 unset ($elemValue[$subElemId]);
101 } elseif (is_array($subElemValue)) {
102 $subElements[$subElemId] = $subElemValue;
103 unset ($elemValue[$subElemId]);
107 if (is_null($subElemIndName) && !empty ($indElements))
108 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().");
110 if (!empty ($subElements) && !empty ($indElements) && !is_null($subElemContent))
111 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
113 if (!is_null($subElemContent)) {
114 $this->printText($indstr . wfElement($elemName, $elemValue, $subElemContent));
115 } elseif (empty ($indElements) && empty ($subElements)) {
116 $this->printText($indstr . wfElement($elemName, $elemValue));
117 } else {
118 $this->printText($indstr . wfElement($elemName, $elemValue, null));
120 foreach ($subElements as $subElemId => & $subElemValue)
121 $this->recXmlPrint($subElemId, $subElemValue, $indent);
123 foreach ($indElements as $subElemId => & $subElemValue)
124 $this->recXmlPrint($subElemIndName, $subElemValue, $indent);
126 $this->printText($indstr . wfCloseElement($elemName));
128 break;
129 case 'object' :
130 // ignore
131 break;
132 default :
133 $this->printText($indstr . wfElement($elemName, null, $elemValue));
134 break;
137 protected function getDescription() {
138 return 'Output data in XML format' . parent :: getDescription();
141 public function getVersion() {
142 return __CLASS__ . ': $Id$';