2006-09-23 15:57:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Created on Sep 19, 2006
|
|
|
|
|
*
|
|
|
|
|
* API for MediaWiki 1.8+
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (!defined('MEDIAWIKI')) {
|
|
|
|
|
// Eclipse helper - will be ignored in production
|
2006-10-01 02:02:13 +00:00
|
|
|
require_once ('ApiFormatBase.php');
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ApiFormatXml extends ApiFormatBase {
|
|
|
|
|
|
|
|
|
|
public function __construct($main, $format) {
|
|
|
|
|
parent :: __construct($main, $format);
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
public function getMimeType() {
|
2006-09-23 15:57:16 +00:00
|
|
|
return 'text/xml';
|
|
|
|
|
}
|
2006-09-26 06:37:26 +00:00
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
public function getNeedsRawData() {
|
2006-09-25 04:12:07 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2006-09-23 15:57:16 +00:00
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
public function execute() {
|
2006-09-23 15:57:16 +00:00
|
|
|
$xmlindent = null;
|
2006-09-27 05:13:48 +00:00
|
|
|
extract($this->extractRequestParams());
|
2006-09-23 15:57:16 +00:00
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
if ($xmlindent || $this->getIsHtml())
|
2006-09-23 15:57:16 +00:00
|
|
|
$xmlindent = -2;
|
|
|
|
|
else
|
|
|
|
|
$xmlindent = null;
|
|
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
$this->printText('<?xml version="1.0" encoding="utf-8"?>');
|
2006-10-01 02:02:13 +00:00
|
|
|
$this->recXmlPrint('api', $this->getResultData(), $xmlindent);
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method takes an array and converts it into an xml.
|
|
|
|
|
* There are several noteworthy cases:
|
|
|
|
|
*
|
2006-10-01 02:02:13 +00:00
|
|
|
* If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
|
|
|
|
|
* Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
|
2006-09-23 15:57:16 +00:00
|
|
|
*
|
2006-10-01 02:02:13 +00:00
|
|
|
* 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.
|
|
|
|
|
* Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
|
2006-09-23 15:57:16 +00:00
|
|
|
*
|
|
|
|
|
* If neither key is found, all keys become element names, and values become element content.
|
|
|
|
|
* The method is recursive, so the same rules apply to any sub-arrays.
|
|
|
|
|
*/
|
|
|
|
|
function recXmlPrint($elemName, $elemValue, $indent) {
|
|
|
|
|
if (!is_null($indent)) {
|
|
|
|
|
$indent += 2;
|
|
|
|
|
$indstr = "\n" . str_repeat(" ", $indent);
|
2006-10-01 02:02:13 +00:00
|
|
|
} else {
|
|
|
|
|
$indstr = '';
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (gettype($elemValue)) {
|
|
|
|
|
case 'array' :
|
2006-10-01 02:02:13 +00:00
|
|
|
|
|
|
|
|
if (isset ($elemValue['*'])) {
|
2006-09-23 15:57:16 +00:00
|
|
|
$subElemContent = $elemValue['*'];
|
|
|
|
|
unset ($elemValue['*']);
|
|
|
|
|
} else {
|
2006-10-01 02:02:13 +00:00
|
|
|
$subElemContent = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset ($elemValue['_element'])) {
|
|
|
|
|
$subElemIndName = $elemValue['_element'];
|
|
|
|
|
unset ($elemValue['_element']);
|
|
|
|
|
} else {
|
|
|
|
|
$subElemIndName = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$indElements = array ();
|
|
|
|
|
$subElements = array ();
|
|
|
|
|
foreach ($elemValue as $subElemId => & $subElemValue) {
|
|
|
|
|
if (gettype($subElemId) === 'integer') {
|
|
|
|
|
if (!is_array($subElemValue))
|
2006-10-01 20:17:16 +00:00
|
|
|
ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has a scalar indexed value.");
|
2006-10-01 02:02:13 +00:00
|
|
|
$indElements[] = $subElemValue;
|
|
|
|
|
unset ($elemValue[$subElemId]);
|
2006-10-01 20:17:16 +00:00
|
|
|
} elseif (is_array($subElemValue)) {
|
|
|
|
|
$subElements[$subElemId] = $subElemValue;
|
|
|
|
|
unset ($elemValue[$subElemId]);
|
|
|
|
|
}
|
2006-10-01 02:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_null($subElemIndName) && !empty ($indElements))
|
2006-10-01 20:17:16 +00:00
|
|
|
ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value");
|
2006-10-01 02:02:13 +00:00
|
|
|
|
|
|
|
|
if (!empty ($subElements) && !empty ($indElements) && !is_null($subElemContent))
|
2006-10-01 20:17:16 +00:00
|
|
|
ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
|
2006-10-01 02:02:13 +00:00
|
|
|
|
|
|
|
|
if (!is_null($subElemContent)) {
|
|
|
|
|
$this->printText($indstr . wfElement($elemName, $elemValue, $subElemContent));
|
2006-10-01 20:17:16 +00:00
|
|
|
} elseif (empty ($indElements) && empty ($subElements)) {
|
2006-10-01 02:02:13 +00:00
|
|
|
$this->printText($indstr . wfElement($elemName, $elemValue));
|
2006-10-01 20:17:16 +00:00
|
|
|
} else {
|
|
|
|
|
$this->printText($indstr . wfElement($elemName, $elemValue, null));
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2006-10-01 20:17:16 +00:00
|
|
|
foreach ($subElements as $subElemId => & $subElemValue)
|
|
|
|
|
$this->recXmlPrint($subElemId, $subElemValue, $indent);
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2006-10-01 20:17:16 +00:00
|
|
|
foreach ($indElements as $subElemId => & $subElemValue)
|
|
|
|
|
$this->recXmlPrint($subElemIndName, $subElemValue, $indent);
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2006-10-01 20:17:16 +00:00
|
|
|
$this->printText($indstr . wfCloseElement($elemName));
|
|
|
|
|
}
|
2006-09-23 15:57:16 +00:00
|
|
|
break;
|
|
|
|
|
case 'object' :
|
|
|
|
|
// ignore
|
|
|
|
|
break;
|
|
|
|
|
default :
|
2006-09-27 05:13:48 +00:00
|
|
|
$this->printText($indstr . wfElement($elemName, null, $elemValue));
|
2006-09-23 15:57:16 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-09-27 05:13:48 +00:00
|
|
|
protected function getDescription() {
|
2006-09-23 15:57:16 +00:00
|
|
|
return 'Output data in XML format';
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
protected function getAllowedParams() {
|
2006-09-23 15:57:16 +00:00
|
|
|
return array (
|
|
|
|
|
'xmlindent' => false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
protected function getParamDescription() {
|
2006-09-23 15:57:16 +00:00
|
|
|
return array (
|
|
|
|
|
'xmlindent' => 'Enable XML indentation'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
?>
|