api: remove duplicate __contruct calls
[lhc/web/wiklou.git] / includes / api / ApiFormatWddx.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 22, 2006
6 *
7 * Copyright © 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * API WDDX output formatter
29 * @ingroup API
30 */
31 class ApiFormatWddx extends ApiFormatBase {
32
33 public function getMimeType() {
34 return 'text/xml';
35 }
36
37 public function execute() {
38 // Some versions of PHP have a broken wddx_serialize_value, see
39 // PHP bug 45314. Test encoding an affected character (U+00A0)
40 // to avoid this.
41 $expected = "<wddxPacket version='1.0'><header/><data><string>\xc2\xa0</string></data></wddxPacket>";
42 if ( function_exists( 'wddx_serialize_value' )
43 && !$this->getIsHtml()
44 && wddx_serialize_value( "\xc2\xa0" ) == $expected ) {
45 $this->printText( wddx_serialize_value( $this->getResultData() ) );
46 } else {
47 // Don't do newlines and indentation if we weren't asked
48 // for pretty output
49 $nl = ( $this->getIsHtml() ? '' : "\n" );
50 $indstr = ' ';
51 $this->printText( "<?xml version=\"1.0\"?>$nl" );
52 $this->printText( "<wddxPacket version=\"1.0\">$nl" );
53 $this->printText( "$indstr<header/>$nl" );
54 $this->printText( "$indstr<data>$nl" );
55 $this->slowWddxPrinter( $this->getResultData(), 4 );
56 $this->printText( "$indstr</data>$nl" );
57 $this->printText( "</wddxPacket>$nl" );
58 }
59 }
60
61 /**
62 * Recursively go through the object and output its data in WDDX format.
63 * @param $elemValue
64 * @param $indent int
65 */
66 function slowWddxPrinter( $elemValue, $indent = 0 ) {
67 $indstr = ( $this->getIsHtml() ? '' : str_repeat( ' ', $indent ) );
68 $indstr2 = ( $this->getIsHtml() ? '' : str_repeat( ' ', $indent + 2 ) );
69 $nl = ( $this->getIsHtml() ? '' : "\n" );
70 switch ( gettype( $elemValue ) ) {
71 case 'array':
72 // Check whether we've got an associative array (<struct>)
73 // or a regular array (<array>)
74 $cnt = count( $elemValue );
75 if ( $cnt == 0 || array_keys( $elemValue ) === range( 0, $cnt - 1 ) ) {
76 // Regular array
77 $this->printText( $indstr . Xml::element( 'array', array(
78 'length' => $cnt ), null ) . $nl );
79 foreach ( $elemValue as $subElemValue ) {
80 $this->slowWddxPrinter( $subElemValue, $indent + 2 );
81 }
82 $this->printText( "$indstr</array>$nl" );
83 } else {
84 // Associative array (<struct>)
85 $this->printText( "$indstr<struct>$nl" );
86 foreach ( $elemValue as $subElemName => $subElemValue ) {
87 $this->printText( $indstr2 . Xml::element( 'var', array(
88 'name' => $subElemName
89 ), null ) . $nl );
90 $this->slowWddxPrinter( $subElemValue, $indent + 4 );
91 $this->printText( "$indstr2</var>$nl" );
92 }
93 $this->printText( "$indstr</struct>$nl" );
94 }
95 break;
96 case 'integer':
97 case 'double':
98 $this->printText( $indstr . Xml::element( 'number', null, $elemValue ) . $nl );
99 break;
100 case 'string':
101 $this->printText( $indstr . Xml::element( 'string', null, $elemValue ) . $nl );
102 break;
103 default:
104 ApiBase::dieDebug( __METHOD__, 'Unknown type ' . gettype( $elemValue ) );
105 }
106 }
107
108 public function getDescription() {
109 return 'Output data in WDDX format' . parent::getDescription();
110 }
111 }