30 public static $namespace =
'http://www.mediawiki.org/xml/api/';
39 $this->mRootElemName = $rootElemName;
44 $this->mIncludeNamespace = $params[
'includexmlnamespace'];
45 $this->mXslt = $params[
'xslt'];
47 $this->
printText(
'<?xml version="1.0"?>' );
48 if ( $this->mXslt !==
null ) {
53 if ( $this->mIncludeNamespace && $result->getResultData(
'xmlns' ) ===
null ) {
60 $data = $result->getResultData(
null, [
61 'Custom' =>
static 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 ) ) {
142 } elseif ( in_array( $k, $subelementKeys,
true ) || $name ===
null ) {
148 } elseif ( is_bool( $oldv ) ) {
152 } elseif ( $v !==
null ) {
158 if ( $subelements || $indexedSubelements ) {
165 } elseif ( is_scalar(
$content ) ) {
168 $attributes += [
'xml:space' =>
'preserve' ];
177 if ( $name !==
null ) {
178 $retval .= $indstr .
Xml::element( $name, $attributes,
null );
180 $retval .= static::recXmlPrint(
null,
$content, $indent );
181 if ( $name !==
null ) {
185 } elseif ( !$indexedSubelements && !$subelements ) {
186 if ( $name !==
null ) {
187 $retval .= $indstr .
Xml::element( $name, $attributes );
190 if ( $name !==
null ) {
191 $retval .= $indstr .
Xml::element( $name, $attributes,
null );
193 foreach ( $subelements as $k => $v ) {
194 $retval .= static::recXmlPrint( $k, $v, $indent );
196 foreach ( $indexedSubelements as $k => $v ) {
197 $retval .= static::recXmlPrint( $indexedTagName, $v, $indent,
198 $indexSubelements ? [
'_idx' => $k ] : []
201 if ( $name !==
null ) {
208 if ( $value ===
null ) {
210 $retval .= $indstr .
Xml::element( $name, $attributes );
213 $retval .= $indstr .
Xml::element( $name, $attributes, $value );
226 private static function mangleName( $name, $preserveKeys = [] ) {
227 static $nsc =
null, $nc =
null;
229 if ( in_array( $name, $preserveKeys,
true ) ) {
233 if ( $name ===
'' ) {
237 if ( $nsc ===
null ) {
241 $nsc =
'A-Za-z\x{C0}-\x{D6}\x{D8}-\x{F6}\x{F8}-\x{2FF}\x{370}-\x{37D}\x{37F}-\x{1FFF}' .
242 '\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}' .
243 '\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}-\x{EFFFF}';
244 $nc = $nsc .
'_\-.0-9\x{B7}\x{300}-\x{36F}\x{203F}-\x{2040}';
247 if ( preg_match(
"/^[$nsc][$nc]*$/uS", $name ) ) {
251 return '_' . preg_replace_callback(
253 static function ( $m ) {
254 return sprintf(
'.%X.', UtfNormal\Utils::utf8ToCodepoint( $m[0] ) );
256 str_replace(
'.',
'.2E.', $name )
262 if ( $nt ===
null || !$nt->exists() ) {
263 $this->
addWarning(
'apiwarn-invalidxmlstylesheet' );
268 $this->
addWarning(
'apiwarn-invalidxmlstylesheetns' );
272 if ( substr( $nt->getText(), -4 ) !==
'.xsl' ) {
273 $this->
addWarning(
'apiwarn-invalidxmlstylesheetext' );
277 $this->
printText(
'<?xml-stylesheet href="' .
278 htmlspecialchars( $nt->getLocalURL(
'action=raw' ) ) .
'" type="text/xsl" ?>' );
282 return parent::getAllowedParams() + [
286 'includexmlnamespace' => [
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
const META_TYPE
Key for the 'type' metadata item.
const META_SUBELEMENTS
Key for the 'subelements' metadata item.
const META_BC_BOOLS
Key for the 'BC bools' metadata item.
const META_PRESERVE_KEYS
Key for the 'preserve keys' metadata item.
const NO_SIZE_CHECK
For addValue() and similar functions, do not check size while adding a value Don't use this unless yo...
const META_CONTENT
Key for the 'content' metadata item.
const META_BC_SUBELEMENTS
Key for the 'BC subelements' metadata item.
const META_INDEXED_TAG_NAME
Key for the 'indexed tag name' metadata item.
static isMetadataKey( $key)
Test whether a key should be considered metadata.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
static closeElement( $element)
Shortcut to close an XML element.
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.