ApiFormatXml.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /*
  3. * Created on Sep 19, 2006
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiFormatBase.php');
  27. }
  28. /**
  29. * @ingroup API
  30. */
  31. class ApiFormatXml extends ApiFormatBase {
  32. private $mRootElemName = 'api';
  33. private $mDoubleQuote = false;
  34. public function __construct($main, $format) {
  35. parent :: __construct($main, $format);
  36. }
  37. public function getMimeType() {
  38. return 'text/xml';
  39. }
  40. public function getNeedsRawData() {
  41. return true;
  42. }
  43. public function setRootElement($rootElemName) {
  44. $this->mRootElemName = $rootElemName;
  45. }
  46. public function execute() {
  47. $params = $this->extractRequestParams();
  48. $this->mDoubleQuote = $params['xmldoublequote'];
  49. $this->printText('<?xml version="1.0"?>');
  50. $this->recXmlPrint($this->mRootElemName, $this->getResultData(), $this->getIsHtml() ? -2 : null);
  51. }
  52. /**
  53. * This method takes an array and converts it to XML.
  54. * There are several noteworthy cases:
  55. *
  56. * If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
  57. * Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
  58. *
  59. * 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.
  60. * Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
  61. *
  62. * If neither key is found, all keys become element names, and values become element content.
  63. * The method is recursive, so the same rules apply to any sub-arrays.
  64. */
  65. function recXmlPrint($elemName, $elemValue, $indent) {
  66. if (!is_null($indent)) {
  67. $indent += 2;
  68. $indstr = "\n" . str_repeat(" ", $indent);
  69. } else {
  70. $indstr = '';
  71. }
  72. $elemName = str_replace(' ', '_', $elemName);
  73. switch (gettype($elemValue)) {
  74. case 'array' :
  75. if (isset ($elemValue['*'])) {
  76. $subElemContent = $elemValue['*'];
  77. if ($this->mDoubleQuote)
  78. $subElemContent = $this->doubleQuote($subElemContent);
  79. unset ($elemValue['*']);
  80. // Add xml:space="preserve" to the
  81. // element so XML parsers will leave
  82. // whitespace in the content alone
  83. $elemValue['xml:space'] = 'preserve';
  84. } else {
  85. $subElemContent = null;
  86. }
  87. if (isset ($elemValue['_element'])) {
  88. $subElemIndName = $elemValue['_element'];
  89. unset ($elemValue['_element']);
  90. } else {
  91. $subElemIndName = null;
  92. }
  93. $indElements = array ();
  94. $subElements = array ();
  95. foreach ($elemValue as $subElemId => & $subElemValue) {
  96. if (is_string($subElemValue) && $this->mDoubleQuote)
  97. $subElemValue = $this->doubleQuote($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]);
  104. }
  105. }
  106. if (is_null($subElemIndName) && count($indElements))
  107. ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().");
  108. if (count($subElements) && count($indElements) && !is_null($subElemContent))
  109. ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
  110. if (!is_null($subElemContent)) {
  111. $this->printText($indstr . Xml::element($elemName, $elemValue, $subElemContent));
  112. } elseif (!count($indElements) && !count($subElements)) {
  113. $this->printText($indstr . Xml::element($elemName, $elemValue));
  114. } else {
  115. $this->printText($indstr . Xml::element($elemName, $elemValue, null));
  116. foreach ($subElements as $subElemId => & $subElemValue)
  117. $this->recXmlPrint($subElemId, $subElemValue, $indent);
  118. foreach ($indElements as $subElemId => & $subElemValue)
  119. $this->recXmlPrint($subElemIndName, $subElemValue, $indent);
  120. $this->printText($indstr . Xml::closeElement($elemName));
  121. }
  122. break;
  123. case 'object' :
  124. // ignore
  125. break;
  126. default :
  127. $this->printText($indstr . Xml::element($elemName, null, $elemValue));
  128. break;
  129. }
  130. }
  131. private function doubleQuote( $text ) {
  132. return Sanitizer::encodeAttribute( $text );
  133. }
  134. public function getAllowedParams() {
  135. return array (
  136. 'xmldoublequote' => false
  137. );
  138. }
  139. public function getParamDescription() {
  140. return array (
  141. 'xmldoublequote' => 'If specified, double quotes all attributes and content.',
  142. );
  143. }
  144. public function getDescription() {
  145. return 'Output data in XML format' . parent :: getDescription();
  146. }
  147. public function getVersion() {
  148. return __CLASS__ . ': $Id: ApiFormatXml.php 50217 2009-05-05 13:12:16Z tstarling $';
  149. }
  150. }