MediaWiki  REL1_22
ApiFormatXml.php
Go to the documentation of this file.
00001 <?php
00031 class ApiFormatXml extends ApiFormatBase {
00032 
00033     private $mRootElemName = 'api';
00034     public static $namespace = 'http://www.mediawiki.org/xml/api/';
00035     private $mIncludeNamespace = false;
00036     private $mXslt = null;
00037 
00038     public function getMimeType() {
00039         return 'text/xml';
00040     }
00041 
00042     public function getNeedsRawData() {
00043         return true;
00044     }
00045 
00046     public function setRootElement( $rootElemName ) {
00047         $this->mRootElemName = $rootElemName;
00048     }
00049 
00050     public function execute() {
00051         $params = $this->extractRequestParams();
00052         $this->mIncludeNamespace = $params['includexmlnamespace'];
00053         $this->mXslt = $params['xslt'];
00054 
00055         $this->printText( '<?xml version="1.0"?>' );
00056         if ( !is_null( $this->mXslt ) ) {
00057             $this->addXslt();
00058         }
00059         if ( $this->mIncludeNamespace ) {
00060             // If the result data already contains an 'xmlns' namespace added
00061             // for custom XML output types, it will override the one for the
00062             // generic API results.
00063             // This allows API output of other XML types like Atom, RSS, RSD.
00064             $data = $this->getResultData() + array( 'xmlns' => self::$namespace );
00065         } else {
00066             $data = $this->getResultData();
00067         }
00068 
00069         $this->printText(
00070             self::recXmlPrint( $this->mRootElemName,
00071                 $data,
00072                 $this->getIsHtml() ? - 2 : null
00073             )
00074         );
00075     }
00076 
00120     public static function recXmlPrint( $elemName, $elemValue, $indent ) {
00121         $retval = '';
00122         if ( !is_null( $indent ) ) {
00123             $indent += 2;
00124             $indstr = "\n" . str_repeat( ' ', $indent );
00125         } else {
00126             $indstr = '';
00127         }
00128         $elemName = str_replace( ' ', '_', $elemName );
00129 
00130         if ( is_array( $elemValue ) ) {
00131             if ( isset( $elemValue['*'] ) ) {
00132                 $subElemContent = $elemValue['*'];
00133                 unset( $elemValue['*'] );
00134 
00135                 // Add xml:space="preserve" to the
00136                 // element so XML parsers will leave
00137                 // whitespace in the content alone
00138                 $elemValue['xml:space'] = 'preserve';
00139             } else {
00140                 $subElemContent = null;
00141             }
00142 
00143             if ( isset( $elemValue['_element'] ) ) {
00144                 $subElemIndName = $elemValue['_element'];
00145                 unset( $elemValue['_element'] );
00146             } else {
00147                 $subElemIndName = null;
00148             }
00149 
00150             $indElements = array();
00151             $subElements = array();
00152             foreach ( $elemValue as $subElemId => & $subElemValue ) {
00153                 if ( is_int( $subElemId ) ) {
00154                     $indElements[] = $subElemValue;
00155                     unset( $elemValue[$subElemId] );
00156                 } elseif ( is_array( $subElemValue ) ) {
00157                     $subElements[$subElemId] = $subElemValue;
00158                     unset( $elemValue[$subElemId] );
00159                 }
00160             }
00161 
00162             if ( is_null( $subElemIndName ) && count( $indElements ) ) {
00163                 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
00164             }
00165 
00166             if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
00167                 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
00168             }
00169 
00170             if ( !is_null( $subElemContent ) ) {
00171                 $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
00172             } elseif ( !count( $indElements ) && !count( $subElements ) ) {
00173                 $retval .= $indstr . Xml::element( $elemName, $elemValue );
00174             } else {
00175                 $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
00176 
00177                 foreach ( $subElements as $subElemId => & $subElemValue ) {
00178                     $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
00179                 }
00180 
00181                 foreach ( $indElements as &$subElemValue ) {
00182                     $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
00183                 }
00184 
00185                 $retval .= $indstr . Xml::closeElement( $elemName );
00186             }
00187         } elseif ( !is_object( $elemValue ) ) {
00188             // to make sure null value doesn't produce unclosed element,
00189             // which is what Xml::element( $elemName, null, null ) returns
00190             if ( $elemValue === null ) {
00191                 $retval .= $indstr . Xml::element( $elemName );
00192             } else {
00193                 $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
00194             }
00195         }
00196         return $retval;
00197     }
00198 
00199     function addXslt() {
00200         $nt = Title::newFromText( $this->mXslt );
00201         if ( is_null( $nt ) || !$nt->exists() ) {
00202             $this->setWarning( 'Invalid or non-existent stylesheet specified' );
00203             return;
00204         }
00205         if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
00206             $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
00207             return;
00208         }
00209         if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
00210             $this->setWarning( 'Stylesheet should have .xsl extension.' );
00211             return;
00212         }
00213         $this->printText( '<?xml-stylesheet href="' . htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
00214     }
00215 
00216     public function getAllowedParams() {
00217         return array(
00218             'xslt' => null,
00219             'includexmlnamespace' => false,
00220         );
00221     }
00222 
00223     public function getParamDescription() {
00224         return array(
00225             'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
00226                 . 'in the MediaWiki namespace whose page name ends with ".xsl"',
00227             'includexmlnamespace' => 'If specified, adds an XML namespace'
00228         );
00229     }
00230 
00231     public function getDescription() {
00232         return 'Output data in XML format' . parent::getDescription();
00233     }
00234 }