00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiFormatBase.php');
00029 }
00030
00034 class ApiFormatXml extends ApiFormatBase {
00035
00036 private $mRootElemName = 'api';
00037 private $mDoubleQuote = false;
00038
00039 public function __construct($main, $format) {
00040 parent :: __construct($main, $format);
00041 }
00042
00043 public function getMimeType() {
00044 return 'text/xml';
00045 }
00046
00047 public function getNeedsRawData() {
00048 return true;
00049 }
00050
00051 public function setRootElement($rootElemName) {
00052 $this->mRootElemName = $rootElemName;
00053 }
00054
00055 public function execute() {
00056 $params = $this->extractRequestParams();
00057 $this->mDoubleQuote = $params['xmldoublequote'];
00058
00059 $this->printText('<?xml version="1.0"?>');
00060 $this->recXmlPrint($this->mRootElemName, $this->getResultData(), $this->getIsHtml() ? -2 : null);
00061 }
00062
00076 function recXmlPrint($elemName, $elemValue, $indent) {
00077 if (!is_null($indent)) {
00078 $indent += 2;
00079 $indstr = "\n" . str_repeat(" ", $indent);
00080 } else {
00081 $indstr = '';
00082 }
00083 $elemName = str_replace(' ', '_', $elemName);
00084
00085 switch (gettype($elemValue)) {
00086 case 'array' :
00087 if (isset ($elemValue['*'])) {
00088 $subElemContent = $elemValue['*'];
00089 if ($this->mDoubleQuote)
00090 $subElemContent = $this->doubleQuote($subElemContent);
00091 unset ($elemValue['*']);
00092
00093
00094
00095
00096 $elemValue['xml:space'] = 'preserve';
00097 } else {
00098 $subElemContent = null;
00099 }
00100
00101 if (isset ($elemValue['_element'])) {
00102 $subElemIndName = $elemValue['_element'];
00103 unset ($elemValue['_element']);
00104 } else {
00105 $subElemIndName = null;
00106 }
00107
00108 $indElements = array ();
00109 $subElements = array ();
00110 foreach ($elemValue as $subElemId => & $subElemValue) {
00111 if (is_string($subElemValue) && $this->mDoubleQuote)
00112 $subElemValue = $this->doubleQuote($subElemValue);
00113
00114 if (gettype($subElemId) === 'integer') {
00115 $indElements[] = $subElemValue;
00116 unset ($elemValue[$subElemId]);
00117 } elseif (is_array($subElemValue)) {
00118 $subElements[$subElemId] = $subElemValue;
00119 unset ($elemValue[$subElemId]);
00120 }
00121 }
00122
00123 if (is_null($subElemIndName) && count($indElements))
00124 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().");
00125
00126 if (count($subElements) && count($indElements) && !is_null($subElemContent))
00127 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
00128
00129 if (!is_null($subElemContent)) {
00130 $this->printText($indstr . Xml::element($elemName, $elemValue, $subElemContent));
00131 } elseif (!count($indElements) && !count($subElements)) {
00132 $this->printText($indstr . Xml::element($elemName, $elemValue));
00133 } else {
00134 $this->printText($indstr . Xml::element($elemName, $elemValue, null));
00135
00136 foreach ($subElements as $subElemId => & $subElemValue)
00137 $this->recXmlPrint($subElemId, $subElemValue, $indent);
00138
00139 foreach ($indElements as $subElemId => & $subElemValue)
00140 $this->recXmlPrint($subElemIndName, $subElemValue, $indent);
00141
00142 $this->printText($indstr . Xml::closeElement($elemName));
00143 }
00144 break;
00145 case 'object' :
00146
00147 break;
00148 default :
00149 $this->printText($indstr . Xml::element($elemName, null, $elemValue));
00150 break;
00151 }
00152 }
00153 private function doubleQuote( $text ) {
00154 return Sanitizer::encodeAttribute( $text );
00155 }
00156
00157 public function getAllowedParams() {
00158 return array (
00159 'xmldoublequote' => false
00160 );
00161 }
00162
00163 public function getParamDescription() {
00164 return array (
00165 'xmldoublequote' => 'If specified, double quotes all attributes and content.',
00166 );
00167 }
00168
00169
00170 public function getDescription() {
00171 return 'Output data in XML format' . parent :: getDescription();
00172 }
00173
00174 public function getVersion() {
00175 return __CLASS__ . ': $Id: ApiFormatXml.php 50217 2009-05-05 13:12:16Z tstarling $';
00176 }
00177 }