ApiFormatXml.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * API XML output formatter
  24. * @ingroup API
  25. */
  26. class ApiFormatXml extends ApiFormatBase {
  27. private $mRootElemName = 'api';
  28. public static $namespace = 'http://www.mediawiki.org/xml/api/';
  29. private $mIncludeNamespace = false;
  30. private $mXslt = null;
  31. public function getMimeType() {
  32. return 'text/xml';
  33. }
  34. public function setRootElement( $rootElemName ) {
  35. $this->mRootElemName = $rootElemName;
  36. }
  37. public function execute() {
  38. $params = $this->extractRequestParams();
  39. $this->mIncludeNamespace = $params['includexmlnamespace'];
  40. $this->mXslt = $params['xslt'];
  41. $this->printText( '<?xml version="1.0"?>' );
  42. if ( !is_null( $this->mXslt ) ) {
  43. $this->addXslt();
  44. }
  45. $result = $this->getResult();
  46. if ( $this->mIncludeNamespace && $result->getResultData( 'xmlns' ) === null ) {
  47. // If the result data already contains an 'xmlns' namespace added
  48. // for custom XML output types, it will override the one for the
  49. // generic API results.
  50. // This allows API output of other XML types like Atom, RSS, RSD.
  51. $result->addValue( null, 'xmlns', self::$namespace, ApiResult::NO_SIZE_CHECK );
  52. }
  53. $data = $result->getResultData( null, [
  54. 'Custom' => function ( &$data, &$metadata ) {
  55. if ( isset( $metadata[ApiResult::META_TYPE] ) ) {
  56. // We want to use non-BC for BCassoc to force outputting of _idx.
  57. switch ( $metadata[ApiResult::META_TYPE] ) {
  58. case 'BCassoc':
  59. $metadata[ApiResult::META_TYPE] = 'assoc';
  60. break;
  61. }
  62. }
  63. },
  64. 'BC' => [ 'nobool', 'no*', 'nosub' ],
  65. 'Types' => [ 'ArmorKVP' => '_name' ],
  66. ] );
  67. $this->printText(
  68. static::recXmlPrint( $this->mRootElemName,
  69. $data,
  70. $this->getIsHtml() ? -2 : null
  71. )
  72. );
  73. }
  74. /**
  75. * This method takes an array and converts it to XML.
  76. *
  77. * @param string|null $name Tag name
  78. * @param mixed $value Tag value (attributes/content/subelements)
  79. * @param int|null $indent Indentation
  80. * @param array $attributes Additional attributes
  81. * @return string
  82. */
  83. public static function recXmlPrint( $name, $value, $indent, $attributes = [] ) {
  84. $retval = '';
  85. if ( $indent !== null ) {
  86. if ( $name !== null ) {
  87. $indent += 2;
  88. }
  89. $indstr = "\n" . str_repeat( ' ', $indent );
  90. } else {
  91. $indstr = '';
  92. }
  93. if ( is_object( $value ) ) {
  94. $value = (array)$value;
  95. }
  96. if ( is_array( $value ) ) {
  97. $contentKey = $value[ApiResult::META_CONTENT] ?? '*';
  98. $subelementKeys = $value[ApiResult::META_SUBELEMENTS] ?? [];
  99. if ( isset( $value[ApiResult::META_BC_SUBELEMENTS] ) ) {
  100. $subelementKeys = array_merge(
  101. $subelementKeys, $value[ApiResult::META_BC_SUBELEMENTS]
  102. );
  103. }
  104. $preserveKeys = $value[ApiResult::META_PRESERVE_KEYS] ?? [];
  105. $indexedTagName = isset( $value[ApiResult::META_INDEXED_TAG_NAME] )
  106. ? self::mangleName( $value[ApiResult::META_INDEXED_TAG_NAME], $preserveKeys )
  107. : '_v';
  108. $bcBools = $value[ApiResult::META_BC_BOOLS] ?? [];
  109. $indexSubelements = isset( $value[ApiResult::META_TYPE] )
  110. ? $value[ApiResult::META_TYPE] !== 'array'
  111. : false;
  112. $content = null;
  113. $subelements = [];
  114. $indexedSubelements = [];
  115. foreach ( $value as $k => $v ) {
  116. if ( ApiResult::isMetadataKey( $k ) && !in_array( $k, $preserveKeys, true ) ) {
  117. continue;
  118. }
  119. $oldv = $v;
  120. if ( is_bool( $v ) && !in_array( $k, $bcBools, true ) ) {
  121. $v = $v ? 'true' : 'false';
  122. }
  123. if ( $name !== null && $k === $contentKey ) {
  124. $content = $v;
  125. } elseif ( is_int( $k ) ) {
  126. $indexedSubelements[$k] = $v;
  127. } elseif ( is_array( $v ) || is_object( $v ) ) {
  128. $subelements[self::mangleName( $k, $preserveKeys )] = $v;
  129. } elseif ( in_array( $k, $subelementKeys, true ) || $name === null ) {
  130. $subelements[self::mangleName( $k, $preserveKeys )] = [
  131. 'content' => $v,
  132. ApiResult::META_CONTENT => 'content',
  133. ApiResult::META_TYPE => 'assoc',
  134. ];
  135. } elseif ( is_bool( $oldv ) ) {
  136. if ( $oldv ) {
  137. $attributes[self::mangleName( $k, $preserveKeys )] = '';
  138. }
  139. } elseif ( $v !== null ) {
  140. $attributes[self::mangleName( $k, $preserveKeys )] = $v;
  141. }
  142. }
  143. if ( $content !== null ) {
  144. if ( $subelements || $indexedSubelements ) {
  145. $subelements[self::mangleName( $contentKey, $preserveKeys )] = [
  146. 'content' => $content,
  147. ApiResult::META_CONTENT => 'content',
  148. ApiResult::META_TYPE => 'assoc',
  149. ];
  150. $content = null;
  151. } elseif ( is_scalar( $content ) ) {
  152. // Add xml:space="preserve" to the element so XML parsers
  153. // will leave whitespace in the content alone
  154. $attributes += [ 'xml:space' => 'preserve' ];
  155. }
  156. }
  157. if ( $content !== null ) {
  158. if ( is_scalar( $content ) ) {
  159. $retval .= $indstr . Xml::element( $name, $attributes, $content );
  160. } else {
  161. if ( $name !== null ) {
  162. $retval .= $indstr . Xml::element( $name, $attributes, null );
  163. }
  164. $retval .= static::recXmlPrint( null, $content, $indent );
  165. if ( $name !== null ) {
  166. $retval .= $indstr . Xml::closeElement( $name );
  167. }
  168. }
  169. } elseif ( !$indexedSubelements && !$subelements ) {
  170. if ( $name !== null ) {
  171. $retval .= $indstr . Xml::element( $name, $attributes );
  172. }
  173. } else {
  174. if ( $name !== null ) {
  175. $retval .= $indstr . Xml::element( $name, $attributes, null );
  176. }
  177. foreach ( $subelements as $k => $v ) {
  178. $retval .= static::recXmlPrint( $k, $v, $indent );
  179. }
  180. foreach ( $indexedSubelements as $k => $v ) {
  181. $retval .= static::recXmlPrint( $indexedTagName, $v, $indent,
  182. $indexSubelements ? [ '_idx' => $k ] : []
  183. );
  184. }
  185. if ( $name !== null ) {
  186. $retval .= $indstr . Xml::closeElement( $name );
  187. }
  188. }
  189. } else {
  190. // to make sure null value doesn't produce unclosed element,
  191. // which is what Xml::element( $name, null, null ) returns
  192. if ( $value === null ) {
  193. $retval .= $indstr . Xml::element( $name, $attributes );
  194. } else {
  195. $retval .= $indstr . Xml::element( $name, $attributes, $value );
  196. }
  197. }
  198. return $retval;
  199. }
  200. /**
  201. * Mangle XML-invalid names to be valid in XML
  202. * @param string $name
  203. * @param array $preserveKeys Names to not mangle
  204. * @return string Mangled name
  205. */
  206. private static function mangleName( $name, $preserveKeys = [] ) {
  207. static $nsc = null, $nc = null;
  208. if ( in_array( $name, $preserveKeys, true ) ) {
  209. return $name;
  210. }
  211. if ( $name === '' ) {
  212. return '_';
  213. }
  214. if ( $nsc === null ) {
  215. // Note we omit ':' from $nsc and $nc because it's reserved for XML
  216. // namespacing, and we omit '_' from $nsc (but not $nc) because we
  217. // reserve it.
  218. $nsc = 'A-Za-z\x{C0}-\x{D6}\x{D8}-\x{F6}\x{F8}-\x{2FF}\x{370}-\x{37D}\x{37F}-\x{1FFF}' .
  219. '\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}' .
  220. '\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}-\x{EFFFF}';
  221. $nc = $nsc . '_\-.0-9\x{B7}\x{300}-\x{36F}\x{203F}-\x{2040}';
  222. }
  223. if ( preg_match( "/^[$nsc][$nc]*$/uS", $name ) ) {
  224. return $name;
  225. }
  226. return '_' . preg_replace_callback(
  227. "/[^$nc]/uS",
  228. function ( $m ) {
  229. return sprintf( '.%X.', UtfNormal\Utils::utf8ToCodepoint( $m[0] ) );
  230. },
  231. str_replace( '.', '.2E.', $name )
  232. );
  233. }
  234. protected function addXslt() {
  235. $nt = Title::newFromText( $this->mXslt );
  236. if ( is_null( $nt ) || !$nt->exists() ) {
  237. $this->addWarning( 'apiwarn-invalidxmlstylesheet' );
  238. return;
  239. }
  240. if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
  241. $this->addWarning( 'apiwarn-invalidxmlstylesheetns' );
  242. return;
  243. }
  244. if ( substr( $nt->getText(), -4 ) !== '.xsl' ) {
  245. $this->addWarning( 'apiwarn-invalidxmlstylesheetext' );
  246. return;
  247. }
  248. $this->printText( '<?xml-stylesheet href="' .
  249. htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
  250. }
  251. public function getAllowedParams() {
  252. return parent::getAllowedParams() + [
  253. 'xslt' => [
  254. ApiBase::PARAM_HELP_MSG => 'apihelp-xml-param-xslt',
  255. ],
  256. 'includexmlnamespace' => [
  257. ApiBase::PARAM_DFLT => false,
  258. ApiBase::PARAM_HELP_MSG => 'apihelp-xml-param-includexmlnamespace',
  259. ],
  260. ];
  261. }
  262. }