2006-09-23 15:57:16 +00:00
|
|
|
<?php
|
2010-02-23 18:05:46 +00:00
|
|
|
/**
|
2010-12-22 20:52:06 +00:00
|
|
|
*
|
2006-09-23 15:57:16 +00:00
|
|
|
*
|
2010-08-07 19:59:42 +00:00
|
|
|
* Created on Sep 19, 2006
|
|
|
|
|
*
|
2012-07-15 20:13:02 +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-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
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';
|
2011-11-28 20:04:40 +00:00
|
|
|
public static $namespace = 'http://www.mediawiki.org/xml/api/';
|
2011-11-04 01:31:15 +00:00
|
|
|
private $mIncludeNamespace = false;
|
2009-07-13 21:37:49 +00:00
|
|
|
private $mXslt = null;
|
2006-10-16 00:08:03 +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();
|
2011-11-04 01:31:15 +00:00
|
|
|
$this->mIncludeNamespace = $params['includexmlnamespace'];
|
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
|
|
|
}
|
2011-11-04 01:31:15 +00:00
|
|
|
if ( $this->mIncludeNamespace ) {
|
|
|
|
|
// If the result data already contains an 'xmlns' namespace added
|
|
|
|
|
// for custom XML output types, it will override the one for the
|
|
|
|
|
// generic API results.
|
|
|
|
|
// This allows API output of other XML types like Atom, RSS, RSD.
|
|
|
|
|
$data = $this->getResultData() + array( 'xmlns' => self::$namespace );
|
|
|
|
|
} else {
|
|
|
|
|
$data = $this->getResultData();
|
|
|
|
|
}
|
2011-11-03 21:22:34 +00:00
|
|
|
|
2010-02-23 18:05:46 +00:00
|
|
|
$this->printText(
|
|
|
|
|
self::recXmlPrint( $this->mRootElemName,
|
2011-10-06 20:11:44 +00:00
|
|
|
$data,
|
2013-11-14 12:42:04 +00:00
|
|
|
$this->getIsHtml() ? -2 : null
|
2010-02-23 18:05:46 +00:00
|
|
|
)
|
|
|
|
|
);
|
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.
|
2012-07-10 15:18:20 +00:00
|
|
|
*
|
2010-02-23 18:05:46 +00:00
|
|
|
* There are several noteworthy cases:
|
|
|
|
|
*
|
2012-07-10 15:18:20 +00:00
|
|
|
* If array contains a key '_element', then the code assumes that ALL
|
|
|
|
|
* other keys are not important and replaces them with the
|
|
|
|
|
* value['_element'].
|
|
|
|
|
*
|
|
|
|
|
* @par Example:
|
|
|
|
|
* @verbatim
|
2013-02-03 18:47:42 +00:00
|
|
|
* name='root', value = array( '_element'=>'page', 'x', 'y', 'z')
|
2012-07-10 15:18:20 +00:00
|
|
|
* @endverbatim
|
|
|
|
|
* creates:
|
|
|
|
|
* @verbatim
|
|
|
|
|
* <root> <page>x</page> <page>y</page> <page>z</page> </root>
|
|
|
|
|
* @endverbatim
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @par Example:
|
|
|
|
|
* @verbatim
|
2013-02-03 18:47:42 +00:00
|
|
|
* name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10)
|
2012-07-10 15:18:20 +00:00
|
|
|
* @endverbatim
|
|
|
|
|
* creates:
|
|
|
|
|
* @verbatim
|
|
|
|
|
* <root lang='en' id='10'>text</root>
|
|
|
|
|
* @endverbatim
|
2010-02-23 18:05:46 +00:00
|
|
|
*
|
2012-07-10 15:18:20 +00:00
|
|
|
* Finally neither key is found, all keys become element names, and values
|
|
|
|
|
* become element content.
|
2010-02-23 18:05:46 +00:00
|
|
|
*
|
2012-07-10 15:18:20 +00:00
|
|
|
* @note The method is recursive, so the same rules apply to any
|
|
|
|
|
* sub-arrays.
|
2011-11-03 21:22:34 +00:00
|
|
|
*
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param string $elemName
|
|
|
|
|
* @param mixed $elemValue
|
|
|
|
|
* @param int $indent
|
2011-05-29 14:24:27 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2010-02-23 18:05:46 +00:00
|
|
|
*/
|
2013-04-09 00:25:47 +00:00
|
|
|
public static function recXmlPrint( $elemName, $elemValue, $indent ) {
|
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
|
|
|
|
2013-03-15 21:50:42 +00:00
|
|
|
if ( is_array( $elemValue ) ) {
|
|
|
|
|
if ( isset( $elemValue['*'] ) ) {
|
|
|
|
|
$subElemContent = $elemValue['*'];
|
|
|
|
|
unset( $elemValue['*'] );
|
|
|
|
|
|
|
|
|
|
// Add xml:space="preserve" to the
|
|
|
|
|
// element so XML parsers will leave
|
|
|
|
|
// whitespace in the content alone
|
|
|
|
|
$elemValue['xml:space'] = 'preserve';
|
|
|
|
|
} else {
|
|
|
|
|
$subElemContent = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $elemValue['_element'] ) ) {
|
|
|
|
|
$subElemIndName = $elemValue['_element'];
|
|
|
|
|
unset( $elemValue['_element'] );
|
|
|
|
|
} else {
|
|
|
|
|
$subElemIndName = null;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 18:53:38 +00:00
|
|
|
if ( isset( $elemValue['_subelements'] ) ) {
|
|
|
|
|
foreach ( $elemValue['_subelements'] as $subElemId ) {
|
|
|
|
|
if ( isset( $elemValue[$subElemId] ) && !is_array( $elemValue[$subElemId] ) ) {
|
|
|
|
|
$elemValue[$subElemId] = array( '*' => $elemValue[$subElemId] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unset( $elemValue['_subelements'] );
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-15 21:50:42 +00:00
|
|
|
$indElements = array();
|
|
|
|
|
$subElements = array();
|
|
|
|
|
foreach ( $elemValue as $subElemId => & $subElemValue ) {
|
|
|
|
|
if ( is_int( $subElemId ) ) {
|
|
|
|
|
$indElements[] = $subElemValue;
|
|
|
|
|
unset( $elemValue[$subElemId] );
|
|
|
|
|
} elseif ( is_array( $subElemValue ) ) {
|
|
|
|
|
$subElements[$subElemId] = $subElemValue;
|
2013-04-26 14:42:31 +00:00
|
|
|
unset( $elemValue[$subElemId] );
|
2014-01-19 09:42:47 +00:00
|
|
|
} elseif ( is_bool( $subElemValue ) ) {
|
|
|
|
|
// treat true as empty string, skip false in xml format
|
|
|
|
|
if ( $subElemValue === true ) {
|
|
|
|
|
$subElemValue = '';
|
|
|
|
|
} else {
|
|
|
|
|
unset( $elemValue[$subElemId] );
|
|
|
|
|
}
|
2010-02-23 18:05:46 +00:00
|
|
|
}
|
2013-03-15 21:50:42 +00:00
|
|
|
}
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2013-03-15 21:50:42 +00:00
|
|
|
if ( is_null( $subElemIndName ) && count( $indElements ) ) {
|
2013-11-16 19:09:17 +00:00
|
|
|
ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys " .
|
|
|
|
|
"without _element value. Use ApiResult::setIndexedTagName()." );
|
2013-03-15 21:50:42 +00:00
|
|
|
}
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2013-03-15 21:50:42 +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
|
|
|
|
2013-03-15 21:50:42 +00:00
|
|
|
if ( !is_null( $subElemContent ) ) {
|
|
|
|
|
$retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
|
|
|
|
|
} elseif ( !count( $indElements ) && !count( $subElements ) ) {
|
|
|
|
|
$retval .= $indstr . Xml::element( $elemName, $elemValue );
|
|
|
|
|
} else {
|
|
|
|
|
$retval .= $indstr . Xml::element( $elemName, $elemValue, null );
|
2006-10-01 02:02:13 +00:00
|
|
|
|
2013-03-15 21:50:42 +00:00
|
|
|
foreach ( $subElements as $subElemId => & $subElemValue ) {
|
|
|
|
|
$retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
|
2006-10-01 20:17:16 +00:00
|
|
|
}
|
2013-03-15 21:50:42 +00:00
|
|
|
|
|
|
|
|
foreach ( $indElements as &$subElemValue ) {
|
|
|
|
|
$retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
|
2013-01-23 20:55:39 +00:00
|
|
|
}
|
2013-03-15 21:50:42 +00:00
|
|
|
|
|
|
|
|
$retval .= $indstr . Xml::closeElement( $elemName );
|
|
|
|
|
}
|
|
|
|
|
} elseif ( !is_object( $elemValue ) ) {
|
|
|
|
|
// to make sure null value doesn't produce unclosed element,
|
|
|
|
|
// which is what Xml::element( $elemName, null, null ) returns
|
|
|
|
|
if ( $elemValue === null ) {
|
|
|
|
|
$retval .= $indstr . Xml::element( $elemName );
|
|
|
|
|
} else {
|
|
|
|
|
$retval .= $indstr . Xml::element( $elemName, null, $elemValue );
|
|
|
|
|
}
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|
2013-11-14 12:42:04 +00:00
|
|
|
|
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' );
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2009-07-13 21:37:49 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
|
|
|
|
|
$this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2009-07-13 21:37:49 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2013-11-14 12:42:04 +00:00
|
|
|
if ( substr( $nt->getText(), -4 ) !== '.xsl' ) {
|
2009-07-13 21:37:49 +00:00
|
|
|
$this->setWarning( 'Stylesheet should have .xsl extension.' );
|
2013-11-14 12:42:04 +00:00
|
|
|
|
2009-07-13 21:37:49 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2013-11-16 19:09:17 +00:00
|
|
|
$this->printText( '<?xml-stylesheet href="' .
|
|
|
|
|
htmlspecialchars( $nt->getLocalURL( '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(
|
2014-09-17 19:43:31 +00:00
|
|
|
'xslt' => array(
|
2014-10-15 20:52:40 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG => 'apihelp-xml-param-xslt',
|
2014-09-17 19:43:31 +00:00
|
|
|
),
|
|
|
|
|
'includexmlnamespace' => array(
|
|
|
|
|
ApiBase::PARAM_DFLT => false,
|
2014-10-15 20:52:40 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG => 'apihelp-xml-param-includexmlnamespace',
|
2014-09-17 19:43:31 +00:00
|
|
|
),
|
2008-03-03 22:19:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
2006-09-23 15:57:16 +00:00
|
|
|
}
|