2006-09-23 15:57:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
/**
|
2006-09-23 15:57:16 +00:00
|
|
|
* Created on Sep 19, 2006
|
|
|
|
|
*
|
|
|
|
|
* API for MediaWiki 1.8+
|
|
|
|
|
*
|
2010-02-23 18:05:46 +00:00
|
|
|
* Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
|
2006-09-23 15:57:16 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-09-23 15:57:16 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*/
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
2006-09-23 15:57:16 +00:00
|
|
|
// Eclipse helper - will be ignored in production
|
2010-02-23 18:05:46 +00:00
|
|
|
require_once( 'ApiFormatBase.php' );
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
2010-05-11 22:30:18 +00:00
|
|
|
* API XML output formatter
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2007-04-20 08:55:14 +00:00
|
|
|
*/
|
2006-09-23 15:57:16 +00:00
|
|
|
class ApiFormatXml extends ApiFormatBase {
|
|
|
|
|
|
2006-10-16 00:08:03 +00:00
|
|
|
private $mRootElemName = 'api';
|
2008-03-03 22:19:03 +00:00
|
|
|
private $mDoubleQuote = false;
|
2009-07-13 21:37:49 +00:00
|
|
|
private $mXslt = null;
|
2006-10-16 00:08:03 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
public function __construct( $main, $format ) {
|
2010-02-23 18:05:46 +00:00
|
|
|
parent::__construct( $main, $format );
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
public function getMimeType() {
|
2006-09-23 15:57:16 +00:00
|
|
|
return 'text/xml';
|
|
|
|
|
}
|
2006-09-26 06:37:26 +00:00
|
|
|
|
2006-09-27 05:13:48 +00:00
|
|
|
public function getNeedsRawData() {
|
2006-09-25 04:12:07 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
public function setRootElement( $rootElemName ) {
|
2006-10-16 00:08:03 +00:00
|
|
|
$this->mRootElemName = $rootElemName;
|
|
|
|
|
}
|
2006-09-23 15:57:16 +00:00
|
|
|
|
2006-10-15 07:43:52 +00:00
|
|
|
public function execute() {
|
2008-03-03 22:19:03 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
$this->mDoubleQuote = $params['xmldoublequote'];
|
2009-07-13 21:37:49 +00:00
|
|
|
$this->mXslt = $params['xslt'];
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->printText( '<?xml version="1.0"?>' );
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( !is_null( $this->mXslt ) ) {
|
2009-07-13 21:37:49 +00:00
|
|
|
$this->addXslt();
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
$this->printText(
|
|
|
|
|
self::recXmlPrint( $this->mRootElemName,
|
2009-08-27 21:15:20 +00:00
|
|
|
$this->getResultData(),
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->getIsHtml() ? - 2 : null,
|
2010-02-23 18:05:46 +00:00
|
|
|
$this->mDoubleQuote
|
|
|
|
|
)
|
|
|
|
|
);
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-02-23 18:05:46 +00:00
|
|
|
* This method takes an array and converts it to XML.
|
|
|
|
|
* There are several noteworthy cases:
|
|
|
|
|
*
|
|
|
|
|
* If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
|
|
|
|
|
* Example: name='root', value = array( '_element'=>'page', 'x', 'y', 'z') creates <root> <page>x</page> <page>y</page> <page>z</page> </root>
|
|
|
|
|
*
|
|
|
|
|
* If any of the array's element key is '*', then the code treats all other key->value pairs as attributes, and the value['*'] as the element's content.
|
|
|
|
|
* Example: name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) creates <root lang='en' id='10'>text</root>
|
|
|
|
|
*
|
|
|
|
|
* If neither key is found, all keys become element names, and values become element content.
|
|
|
|
|
* The method is recursive, so the same rules apply to any sub-arrays.
|
|
|
|
|
*/
|
2010-01-11 15:55:52 +00:00
|
|
|
public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
|
2009-08-27 21:15:20 +00:00
|
|
|
$retval = '';
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( !is_null( $indent ) ) {
|
2006-09-23 15:57:16 +00:00
|
|
|
$indent += 2;
|
2010-02-23 18:05:46 +00:00
|
|
|
$indstr = "\n" . str_repeat( ' ', $indent );
|
2006-10-01 02:02:13 +00:00
|
|
|
} else {
|
|
|
|
|
$indstr = '';
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
2010-01-11 15:55:52 +00:00
|
|
|
$elemName = str_replace( ' ', '_', $elemName );
|
2006-09-23 15:57:16 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
switch ( gettype( $elemValue ) ) {
|
2010-02-23 18:05:46 +00:00
|
|
|
case 'array':
|
|
|
|
|
if ( isset( $elemValue['*'] ) ) {
|
2008-04-14 07:45:50 +00:00
|
|
|
$subElemContent = $elemValue['*'];
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( $doublequote ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$subElemContent = Sanitizer::encodeAttribute( $subElemContent );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
unset( $elemValue['*'] );
|
|
|
|
|
|
2009-04-29 13:12:27 +00:00
|
|
|
// Add xml:space="preserve" to the
|
|
|
|
|
// element so XML parsers will leave
|
|
|
|
|
// whitespace in the content alone
|
|
|
|
|
$elemValue['xml:space'] = 'preserve';
|
2006-09-23 15:57:16 +00:00
|
|
|
} else {
|
2006-10-01 02:02:13 +00:00
|
|
|
$subElemContent = null;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( isset( $elemValue['_element'] ) ) {
|
2006-10-01 02:02:13 +00:00
|
|
|
$subElemIndName = $elemValue['_element'];
|
2010-02-23 18:05:46 +00:00
|
|
|
unset( $elemValue['_element'] );
|
2006-10-01 02:02:13 +00:00
|
|
|
} else {
|
|
|
|
|
$subElemIndName = null;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
$indElements = array();
|
|
|
|
|
$subElements = array();
|
2010-01-11 15:55:52 +00:00
|
|
|
foreach ( $elemValue as $subElemId => & $subElemValue ) {
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( is_string( $subElemValue ) && $doublequote ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$subElemValue = Sanitizer::encodeAttribute( $subElemValue );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( gettype( $subElemId ) === 'integer' ) {
|
2006-10-01 02:02:13 +00:00
|
|
|
$indElements[] = $subElemValue;
|
2010-02-23 18:05:46 +00:00
|
|
|
unset( $elemValue[$subElemId] );
|
2010-01-11 15:55:52 +00:00
|
|
|
} elseif ( is_array( $subElemValue ) ) {
|
2006-10-01 20:17:16 +00:00
|
|
|
$subElements[$subElemId] = $subElemValue;
|
2010-01-11 15:55:52 +00:00
|
|
|
unset ( $elemValue[$subElemId] );
|
2006-10-01 20:17:16 +00:00
|
|
|
}
|
2006-10-01 02:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( is_null( $subElemIndName ) && count( $indElements ) ) {
|
|
|
|
|
ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." );
|
|
|
|
|
}
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
|
|
|
|
|
ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
|
|
|
|
|
}
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( !is_null( $subElemContent ) ) {
|
|
|
|
|
$retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
|
|
|
|
|
} elseif ( !count( $indElements ) && !count( $subElements ) ) {
|
2010-02-23 18:05:46 +00:00
|
|
|
$retval .= $indstr . Xml::element( $elemName, $elemValue );
|
2006-10-01 20:17:16 +00:00
|
|
|
} else {
|
2010-01-11 15:55:52 +00:00
|
|
|
$retval .= $indstr . Xml::element( $elemName, $elemValue, null );
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
foreach ( $subElements as $subElemId => & $subElemValue ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
foreach ( $indElements as $subElemId => & $subElemValue ) {
|
2010-01-11 15:55:52 +00:00
|
|
|
$retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$retval .= $indstr . Xml::closeElement( $elemName );
|
2006-10-01 20:17:16 +00:00
|
|
|
}
|
2006-09-23 15:57:16 +00:00
|
|
|
break;
|
2010-02-23 18:05:46 +00:00
|
|
|
case 'object':
|
2006-09-23 15:57:16 +00:00
|
|
|
// ignore
|
|
|
|
|
break;
|
2010-02-23 18:05:46 +00:00
|
|
|
default:
|
2010-01-11 15:55:52 +00:00
|
|
|
$retval .= $indstr . Xml::element( $elemName, null, $elemValue );
|
2006-09-23 15:57:16 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2009-08-27 21:15:20 +00:00
|
|
|
return $retval;
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2009-07-13 21:37:49 +00:00
|
|
|
function addXslt() {
|
|
|
|
|
$nt = Title::newFromText( $this->mXslt );
|
|
|
|
|
if ( is_null( $nt ) || !$nt->exists() ) {
|
|
|
|
|
$this->setWarning( 'Invalid or non-existent stylesheet specified' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
|
|
|
|
|
$this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( substr( $nt->getText(), - 4 ) !== '.xsl' ) {
|
2009-07-13 21:37:49 +00:00
|
|
|
$this->setWarning( 'Stylesheet should have .xsl extension.' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-01-23 15:27:53 +00:00
|
|
|
$this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
|
2009-07-13 21:37:49 +00:00
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
|
2008-03-03 22:19:03 +00:00
|
|
|
public function getAllowedParams() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2009-07-13 21:37:49 +00:00
|
|
|
'xmldoublequote' => false,
|
|
|
|
|
'xslt' => null,
|
2008-03-03 22:19:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return array(
|
2010-05-11 22:30:18 +00:00
|
|
|
'xmldoublequote' => 'If specified, double quotes all attributes and content',
|
2009-07-13 21:37:49 +00:00
|
|
|
'xslt' => 'If specified, adds <xslt> as stylesheet',
|
2008-03-03 22:19:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2010-02-23 18:05:46 +00:00
|
|
|
return 'Output data in XML format' . parent::getDescription();
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
2006-10-01 21:20:55 +00:00
|
|
|
|
|
|
|
|
public function getVersion() {
|
|
|
|
|
return __CLASS__ . ': $Id$';
|
|
|
|
|
}
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|