Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiFormatWddx.php
blob8662a64b8d3f45455db1e903697639a8a5e889c6
1 <?php
2 /**
5 * Created on Oct 22, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 /**
28 * API WDDX output formatter
29 * @deprecated since 1.24
30 * @ingroup API
32 class ApiFormatWddx extends ApiFormatBase {
34 public function getMimeType() {
35 return 'text/xml';
38 public function execute() {
39 $this->markDeprecated();
41 if ( !$this->getIsHtml() && !static::useSlowPrinter() ) {
42 $this->printText( wddx_serialize_value( $this->getResultData() ) );
43 } else {
44 // Don't do newlines and indentation if we weren't asked
45 // for pretty output
46 $nl = ( $this->getIsHtml() ? "\n" : '' );
47 $indstr = ( $this->getIsHtml() ? ' ' : '' );
48 $this->printText( "<?xml version=\"1.0\"?>$nl" );
49 $this->printText( "<wddxPacket version=\"1.0\">$nl" );
50 $this->printText( "$indstr<header />$nl" );
51 $this->printText( "$indstr<data>$nl" );
52 $this->slowWddxPrinter( $this->getResultData(), 4 );
53 $this->printText( "$indstr</data>$nl" );
54 $this->printText( "</wddxPacket>$nl" );
58 public static function useSlowPrinter() {
59 if ( !function_exists( 'wddx_serialize_value' ) ) {
60 return true;
63 // Some versions of PHP have a broken wddx_serialize_value, see
64 // PHP bug 45314. Test encoding an affected character (U+00A0)
65 // to avoid this.
66 $expected =
67 "<wddxPacket version='1.0'><header/><data><string>\xc2\xa0</string></data></wddxPacket>";
68 if ( wddx_serialize_value( "\xc2\xa0" ) !== $expected ) {
69 return true;
72 // Some versions of HHVM don't correctly encode ampersands.
73 $expected =
74 "<wddxPacket version='1.0'><header/><data><string>&amp;</string></data></wddxPacket>";
75 if ( wddx_serialize_value( '&' ) !== $expected ) {
76 return true;
79 // Some versions of HHVM don't correctly encode empty arrays as subvalues.
80 $expected =
81 "<wddxPacket version='1.0'><header/><data><array length='1'><array length='0'></array></array></data></wddxPacket>";
82 if ( wddx_serialize_value( array( array() ) ) !== $expected ) {
83 return true;
86 // Some versions of HHVM don't correctly encode associative arrays with numeric keys.
87 $expected =
88 "<wddxPacket version='1.0'><header/><data><struct><var name='2'><number>1</number></var></struct></data></wddxPacket>";
89 if ( wddx_serialize_value( array( 2 => 1 ) ) !== $expected ) {
90 return true;
93 return false;
96 /**
97 * Recursively go through the object and output its data in WDDX format.
98 * @param mixed $elemValue
99 * @param int $indent
101 function slowWddxPrinter( $elemValue, $indent = 0 ) {
102 $indstr = ( $this->getIsHtml() ? str_repeat( ' ', $indent ) : '' );
103 $indstr2 = ( $this->getIsHtml() ? str_repeat( ' ', $indent + 2 ) : '' );
104 $nl = ( $this->getIsHtml() ? "\n" : '' );
105 if ( is_array( $elemValue ) ) {
106 // Check whether we've got an associative array (<struct>)
107 // or a regular array (<array>)
108 $cnt = count( $elemValue );
109 if ( $cnt == 0 || array_keys( $elemValue ) === range( 0, $cnt - 1 ) ) {
110 // Regular array
111 $this->printText( $indstr . Xml::element( 'array', array(
112 'length' => $cnt ), null ) . $nl );
113 foreach ( $elemValue as $subElemValue ) {
114 $this->slowWddxPrinter( $subElemValue, $indent + 2 );
116 $this->printText( "$indstr</array>$nl" );
117 } else {
118 // Associative array (<struct>)
119 $this->printText( "$indstr<struct>$nl" );
120 foreach ( $elemValue as $subElemName => $subElemValue ) {
121 $this->printText( $indstr2 . Xml::element( 'var', array(
122 'name' => $subElemName
123 ), null ) . $nl );
124 $this->slowWddxPrinter( $subElemValue, $indent + 4 );
125 $this->printText( "$indstr2</var>$nl" );
127 $this->printText( "$indstr</struct>$nl" );
129 } elseif ( is_int( $elemValue ) || is_float( $elemValue ) ) {
130 $this->printText( $indstr . Xml::element( 'number', null, $elemValue ) . $nl );
131 } elseif ( is_string( $elemValue ) ) {
132 $this->printText( $indstr . Xml::element( 'string', null, $elemValue ) . $nl );
133 } elseif ( is_bool( $elemValue ) ) {
134 $this->printText( $indstr . Xml::element( 'boolean',
135 array( 'value' => $elemValue ? 'true' : 'false' ) ) . $nl
137 } elseif ( $elemValue === null ) {
138 $this->printText( $indstr . Xml::element( 'null', array() ) . $nl );
139 } else {
140 ApiBase::dieDebug( __METHOD__, 'Unknown type ' . gettype( $elemValue ) );
144 public function isDeprecated() {
145 return true;