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