MediaWiki
REL1_21
|
00001 <?php 00031 class ApiFormatWddx extends ApiFormatBase { 00032 00033 public function getMimeType() { 00034 return 'text/xml'; 00035 } 00036 00037 public function execute() { 00038 // Some versions of PHP have a broken wddx_serialize_value, see 00039 // PHP bug 45314. Test encoding an affected character (U+00A0) 00040 // to avoid this. 00041 $expected = "<wddxPacket version='1.0'><header/><data><string>\xc2\xa0</string></data></wddxPacket>"; 00042 if ( function_exists( 'wddx_serialize_value' ) 00043 && !$this->getIsHtml() 00044 && wddx_serialize_value( "\xc2\xa0" ) == $expected ) { 00045 $this->printText( wddx_serialize_value( $this->getResultData() ) ); 00046 } else { 00047 // Don't do newlines and indentation if we weren't asked 00048 // for pretty output 00049 $nl = ( $this->getIsHtml() ? '' : "\n" ); 00050 $indstr = ' '; 00051 $this->printText( "<?xml version=\"1.0\"?>$nl" ); 00052 $this->printText( "<wddxPacket version=\"1.0\">$nl" ); 00053 $this->printText( "$indstr<header/>$nl" ); 00054 $this->printText( "$indstr<data>$nl" ); 00055 $this->slowWddxPrinter( $this->getResultData(), 4 ); 00056 $this->printText( "$indstr</data>$nl" ); 00057 $this->printText( "</wddxPacket>$nl" ); 00058 } 00059 } 00060 00066 function slowWddxPrinter( $elemValue, $indent = 0 ) { 00067 $indstr = ( $this->getIsHtml() ? '' : str_repeat( ' ', $indent ) ); 00068 $indstr2 = ( $this->getIsHtml() ? '' : str_repeat( ' ', $indent + 2 ) ); 00069 $nl = ( $this->getIsHtml() ? '' : "\n" ); 00070 switch ( gettype( $elemValue ) ) { 00071 case 'array': 00072 // Check whether we've got an associative array (<struct>) 00073 // or a regular array (<array>) 00074 $cnt = count( $elemValue ); 00075 if ( $cnt == 0 || array_keys( $elemValue ) === range( 0, $cnt - 1 ) ) { 00076 // Regular array 00077 $this->printText( $indstr . Xml::element( 'array', array( 00078 'length' => $cnt ), null ) . $nl ); 00079 foreach ( $elemValue as $subElemValue ) { 00080 $this->slowWddxPrinter( $subElemValue, $indent + 2 ); 00081 } 00082 $this->printText( "$indstr</array>$nl" ); 00083 } else { 00084 // Associative array (<struct>) 00085 $this->printText( "$indstr<struct>$nl" ); 00086 foreach ( $elemValue as $subElemName => $subElemValue ) { 00087 $this->printText( $indstr2 . Xml::element( 'var', array( 00088 'name' => $subElemName 00089 ), null ) . $nl ); 00090 $this->slowWddxPrinter( $subElemValue, $indent + 4 ); 00091 $this->printText( "$indstr2</var>$nl" ); 00092 } 00093 $this->printText( "$indstr</struct>$nl" ); 00094 } 00095 break; 00096 case 'integer': 00097 case 'double': 00098 $this->printText( $indstr . Xml::element( 'number', null, $elemValue ) . $nl ); 00099 break; 00100 case 'string': 00101 $this->printText( $indstr . Xml::element( 'string', null, $elemValue ) . $nl ); 00102 break; 00103 default: 00104 ApiBase::dieDebug( __METHOD__, 'Unknown type ' . gettype( $elemValue ) ); 00105 } 00106 } 00107 00108 public function getDescription() { 00109 return 'Output data in WDDX format' . parent::getDescription(); 00110 } 00111 }