Have been playing with custom API modules and been a bit frustrated with the XML...
authorBrion Vibber <brion@users.mediawiki.org>
Thu, 3 Jul 2008 21:25:20 +0000 (21:25 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Thu, 3 Jul 2008 21:25:20 +0000 (21:25 +0000)
Adding pseudo-element _attribs alongside _element for XML output. There doesn't seem to be a good way currently to specify both attributes *and* subelements -- '*' lets you have text and attributes, but not sub-elements and attributes. Contents of _attribs will be added as attributes without disturbing the sub-elements.

includes/api/ApiFormatXml.php

index b119417..928e4b9 100644 (file)
@@ -83,6 +83,13 @@ class ApiFormatXml extends ApiFormatBase {
 
                switch (gettype($elemValue)) {
                        case 'array' :
+                               if (isset ($elemValue['_attribs'])) {
+                                       $attribValues = $elemValue['_attribs'];
+                                       unset( $elemValue['_attribs'] );
+                               } else {
+                                       $attribValues = null;
+                               }
+                               
                                if (isset ($elemValue['*'])) {
                                        $subElemContent = $elemValue['*'];
                                        if ($this->mDoubleQuote)
@@ -113,6 +120,8 @@ class ApiFormatXml extends ApiFormatBase {
                                                unset ($elemValue[$subElemId]);
                                        }
                                }
+                               
+                               $elemValue = wfArrayMerge( $attribValues, $elemValue ); // wtf
 
                                if (is_null($subElemIndName) && !empty ($indElements))
                                        ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().");
@@ -123,7 +132,7 @@ class ApiFormatXml extends ApiFormatBase {
                                if (!is_null($subElemContent)) {
                                        $this->printText($indstr . wfElement($elemName, $elemValue, $subElemContent));
                                } elseif (empty ($indElements) && empty ($subElements)) {
-                                               $this->printText($indstr . wfElement($elemName, $elemValue));
+                                       $this->printText($indstr . wfElement($elemName, $elemValue));
                                } else {
                                        $this->printText($indstr . wfElement($elemName, $elemValue, null));