MediaWiki  REL1_24
ApiFormatWddx.php
Go to the documentation of this file.
00001 <?php
00032 class ApiFormatWddx extends ApiFormatBase {
00033 
00034     public function getMimeType() {
00035         return 'text/xml';
00036     }
00037 
00038     public function execute() {
00039         $this->markDeprecated();
00040 
00041         // Some versions of PHP have a broken wddx_serialize_value, see
00042         // PHP bug 45314. Test encoding an affected character (U+00A0)
00043         // to avoid this.
00044         $expected =
00045             "<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         ) {
00050             $this->printText( wddx_serialize_value( $this->getResultData() ) );
00051         } else {
00052             // Don't do newlines and indentation if we weren't asked
00053             // for pretty output
00054             $nl = ( $this->getIsHtml() ? "\n" : '' );
00055             $indstr = ' ';
00056             $this->printText( "<?xml version=\"1.0\"?>$nl" );
00057             $this->printText( "<wddxPacket version=\"1.0\">$nl" );
00058             $this->printText( "$indstr<header/>$nl" );
00059             $this->printText( "$indstr<data>$nl" );
00060             $this->slowWddxPrinter( $this->getResultData(), 4 );
00061             $this->printText( "$indstr</data>$nl" );
00062             $this->printText( "</wddxPacket>$nl" );
00063         }
00064     }
00065 
00071     function slowWddxPrinter( $elemValue, $indent = 0 ) {
00072         $indstr = ( $this->getIsHtml() ? str_repeat( ' ', $indent ) : '' );
00073         $indstr2 = ( $this->getIsHtml() ? str_repeat( ' ', $indent + 2 ) : '' );
00074         $nl = ( $this->getIsHtml() ? "\n" : '' );
00075         if ( is_array( $elemValue ) ) {
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         } elseif ( is_int( $elemValue ) || is_float( $elemValue ) ) {
00100             $this->printText( $indstr . Xml::element( 'number', null, $elemValue ) . $nl );
00101         } elseif ( is_string( $elemValue ) ) {
00102             $this->printText( $indstr . Xml::element( 'string', null, $elemValue ) . $nl );
00103         } elseif ( is_bool( $elemValue ) ) {
00104             $this->printText( $indstr . Xml::element( 'boolean',
00105                 array( 'value' => $elemValue ? 'true' : 'false' ) ) . $nl
00106             );
00107         } else {
00108             ApiBase::dieDebug( __METHOD__, 'Unknown type ' . gettype( $elemValue ) );
00109         }
00110     }
00111 
00112     public function getDescription() {
00113         return 'DEPRECATED! Output data in WDDX format' . parent::getDescription();
00114     }
00115 }