ApiFormatWddx.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * Created on Oct 22, 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 ApiFormatWddx extends ApiFormatBase {
  32. public function __construct($main, $format) {
  33. parent :: __construct($main, $format);
  34. }
  35. public function getMimeType() {
  36. return 'text/xml';
  37. }
  38. public function execute() {
  39. // Some versions of PHP have a broken wddx_serialize_value, see
  40. // PHP bug 45314. Test encoding an affected character (U+00A0)
  41. // to avoid this.
  42. $expected = "<wddxPacket version='1.0'><header/><data><string>\xc2\xa0</string></data></wddxPacket>";
  43. if (function_exists('wddx_serialize_value')
  44. && !$this->getIsHtml()
  45. && wddx_serialize_value("\xc2\xa0") == $expected) {
  46. $this->printText(wddx_serialize_value($this->getResultData()));
  47. } else {
  48. // Don't do newlines and indentation if we weren't asked
  49. // for pretty output
  50. $nl = ($this->getIsHtml() ? "" : "\n");
  51. $indstr = " ";
  52. $this->printText("<?xml version=\"1.0\"?>$nl");
  53. $this->printText("<wddxPacket version=\"1.0\">$nl");
  54. $this->printText("$indstr<header/>$nl");
  55. $this->printText("$indstr<data>$nl");
  56. $this->slowWddxPrinter($this->getResultData(), 4);
  57. $this->printText("$indstr</data>$nl");
  58. $this->printText("</wddxPacket>$nl");
  59. }
  60. }
  61. /**
  62. * Recursively go through the object and output its data in WDDX format.
  63. */
  64. function slowWddxPrinter($elemValue, $indent = 0) {
  65. $indstr = ($this->getIsHtml() ? "" : str_repeat(' ', $indent));
  66. $indstr2 = ($this->getIsHtml() ? "" : str_repeat(' ', $indent + 2));
  67. $nl = ($this->getIsHtml() ? "" : "\n");
  68. switch (gettype($elemValue)) {
  69. case 'array' :
  70. // Check whether we've got an associative array (<struct>)
  71. // or a regular array (<array>)
  72. $cnt = count($elemValue);
  73. if($cnt == 0 || array_keys($elemValue) === range(0, $cnt - 1)) {
  74. // Regular array
  75. $this->printText($indstr . Xml::element('array', array(
  76. 'length' => $cnt
  77. ), null) . $nl);
  78. foreach($elemValue as $subElemValue)
  79. $this->slowWddxPrinter($subElemValue, $indent + 2);
  80. $this->printText("$indstr</array>$nl");
  81. } else {
  82. // Associative array (<struct>)
  83. $this->printText("$indstr<struct>$nl");
  84. foreach($elemValue as $subElemName => $subElemValue) {
  85. $this->printText($indstr2 . Xml::element('var', array(
  86. 'name' => $subElemName
  87. ), null) . $nl);
  88. $this->slowWddxPrinter($subElemValue, $indent + 4);
  89. $this->printText("$indstr2</var>$nl");
  90. }
  91. $this->printText("$indstr</struct>$nl");
  92. }
  93. break;
  94. case 'integer' :
  95. case 'double' :
  96. $this->printText($indstr . Xml::element('number', null, $elemValue) . $nl);
  97. break;
  98. case 'string' :
  99. $this->printText($indstr . Xml::element('string', null, $elemValue) . $nl);
  100. break;
  101. default :
  102. ApiBase :: dieDebug(__METHOD__, 'Unknown type ' . gettype($elemValue));
  103. }
  104. }
  105. public function getDescription() {
  106. return 'Output data in WDDX format' . parent :: getDescription();
  107. }
  108. public function getVersion() {
  109. return __CLASS__ . ': $Id: ApiFormatWddx.php 48716 2009-03-23 20:06:16Z catrope $';
  110. }
  111. }