29 private $mRootElemName =
'api';
30 public static $namespace =
'http://www.mediawiki.org/xml/api/';
31 private $mIncludeNamespace =
false;
32 private $mXslt =
null;
38 public function setRootElement( $rootElemName ) {
39 $this->mRootElemName = $rootElemName;
44 $this->mIncludeNamespace = $params[
'includexmlnamespace'];
45 $this->mXslt = $params[
'xslt'];
47 $this->
printText(
'<?xml version="1.0"?>' );
48 if ( !is_null( $this->mXslt ) ) {
53 if ( $this->mIncludeNamespace && $result->getResultData(
'xmlns' ) === null ) {
60 $data = $result->getResultData(
null, [
61 'Custom' =>
function ( &$data, &$metadata ) {
71 'BC' => [
'nobool',
'no*',
'nosub' ],
72 'Types' => [
'ArmorKVP' =>
'_name' ],
76 static::recXmlPrint( $this->mRootElemName,
92 public static function recXmlPrint( $name, $value, $indent, $attributes = [] ) {
94 if ( $indent !==
null ) {
95 if ( $name !==
null ) {
98 $indstr =
"\n" . str_repeat(
' ', $indent );
103 if ( is_object( $value ) ) {
104 $value = (array)$value;
106 if ( is_array( $value ) ) {
110 $subelementKeys = array_merge(
125 $indexedSubelements = [];
126 foreach ( $value as $k => $v ) {
132 if ( is_bool( $v ) && !in_array( $k, $bcBools,
true ) ) {
133 $v = $v ?
'true' :
'false';
136 if ( $name !==
null && $k === $contentKey ) {
138 } elseif ( is_int( $k ) ) {
139 $indexedSubelements[$k] = $v;
140 } elseif ( is_array( $v ) || is_object( $v ) ) {
141 $subelements[self::mangleName( $k, $preserveKeys )] = $v;
142 } elseif ( in_array( $k, $subelementKeys,
true ) || $name ===
null ) {
143 $subelements[self::mangleName( $k, $preserveKeys )] = [
148 } elseif ( is_bool( $oldv ) ) {
150 $attributes[self::mangleName( $k, $preserveKeys )] =
'';
152 } elseif ( $v !==
null ) {
153 $attributes[self::mangleName( $k, $preserveKeys )] = $v;
158 if ( $subelements || $indexedSubelements ) {
159 $subelements[self::mangleName( $contentKey, $preserveKeys )] = [
165 } elseif ( is_scalar(
$content ) ) {
168 $attributes += [
'xml:space' =>
'preserve' ];
176 if ( $name !==
null ) {
177 $retval .= $indstr .
Xml::element( $name, $attributes,
null );
179 $retval .= static::recXmlPrint(
null,
$content, $indent );
180 if ( $name !==
null ) {
184 } elseif ( !$indexedSubelements && !$subelements ) {
185 if ( $name !==
null ) {
186 $retval .= $indstr .
Xml::element( $name, $attributes );
189 if ( $name !==
null ) {
190 $retval .= $indstr .
Xml::element( $name, $attributes,
null );
192 foreach ( $subelements as $k => $v ) {
193 $retval .= static::recXmlPrint( $k, $v, $indent );
195 foreach ( $indexedSubelements as $k => $v ) {
196 $retval .= static::recXmlPrint( $indexedTagName, $v, $indent,
197 $indexSubelements ? [
'_idx' => $k ] : []
200 if ( $name !==
null ) {
207 if ( $value ===
null ) {
208 $retval .= $indstr .
Xml::element( $name, $attributes );
210 $retval .= $indstr .
Xml::element( $name, $attributes, $value );
223 private static function mangleName( $name, $preserveKeys = [] ) {
224 static $nsc =
null, $nc =
null;
226 if ( in_array( $name, $preserveKeys,
true ) ) {
230 if ( $name ===
'' ) {
234 if ( $nsc ===
null ) {
238 $nsc =
'A-Za-z\x{C0}-\x{D6}\x{D8}-\x{F6}\x{F8}-\x{2FF}\x{370}-\x{37D}\x{37F}-\x{1FFF}' .
239 '\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}' .
240 '\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}-\x{EFFFF}';
241 $nc = $nsc .
'_\-.0-9\x{B7}\x{300}-\x{36F}\x{203F}-\x{2040}';
244 if ( preg_match(
"/^[$nsc][$nc]*$/uS", $name ) ) {
248 return '_' . preg_replace_callback(
251 return sprintf(
'.%X.', UtfNormal\Utils::utf8ToCodepoint( $m[0] ) );
253 str_replace(
'.',
'.2E.', $name )
257 protected function addXslt() {
259 if ( is_null( $nt ) || !$nt->exists() ) {
260 $this->
addWarning(
'apiwarn-invalidxmlstylesheet' );
265 $this->
addWarning(
'apiwarn-invalidxmlstylesheetns' );
269 if ( substr( $nt->getText(), -4 ) !==
'.xsl' ) {
270 $this->
addWarning(
'apiwarn-invalidxmlstylesheetext' );
274 $this->
printText(
'<?xml-stylesheet href="' .
275 htmlspecialchars( $nt->getLocalURL(
'action=raw' ) ) .
'" type="text/xsl" ?>' );
279 return parent::getAllowedParams() + [
283 'includexmlnamespace' => [