r55641 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55640‎ | r55641 | r55642 >
Date:21:15, 27 August 2009
Author:catrope
Status:resolved (Comments)
Tags:
Comment:
API: Refactor the XML formatter a bit so that ApiFormatXml::recXmlPrint() is now actually usable as a generic XML formatter
Modified paths:
  • /trunk/phase3/includes/api/ApiFormatXml.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiFormatXml.php
@@ -61,7 +61,10 @@
6262 $this->printText('<?xml version="1.0"?>');
6363 if (!is_null($this->mXslt))
6464 $this->addXslt();
65 - $this->recXmlPrint($this->mRootElemName, $this->getResultData(), $this->getIsHtml() ? -2 : null);
 65+ $this->printText($this->recXmlPrint($this->mRootElemName,
 66+ $this->getResultData(),
 67+ $this->getIsHtml() ? -2 : null,
 68+ $this->mDoubleQuote));
6669 }
6770
6871 /**
@@ -77,7 +80,8 @@
7881 * If neither key is found, all keys become element names, and values become element content.
7982 * The method is recursive, so the same rules apply to any sub-arrays.
8083 */
81 - function recXmlPrint($elemName, $elemValue, $indent) {
 84+ public static function recXmlPrint($elemName, $elemValue, $indent, $doublequote = false) {
 85+ $retval = '';
8286 if (!is_null($indent)) {
8387 $indent += 2;
8488 $indstr = "\n" . str_repeat(" ", $indent);
@@ -90,8 +94,8 @@
9195 case 'array' :
9296 if (isset ($elemValue['*'])) {
9397 $subElemContent = $elemValue['*'];
94 - if ($this->mDoubleQuote)
95 - $subElemContent = $this->doubleQuote($subElemContent);
 98+ if ($doublequote)
 99+ $subElemContent = Sanitizer::encodeAttribute($subElemContent);
96100 unset ($elemValue['*']);
97101
98102 // Add xml:space="preserve" to the
@@ -112,8 +116,8 @@
113117 $indElements = array ();
114118 $subElements = array ();
115119 foreach ($elemValue as $subElemId => & $subElemValue) {
116 - if (is_string($subElemValue) && $this->mDoubleQuote)
117 - $subElemValue = $this->doubleQuote($subElemValue);
 120+ if (is_string($subElemValue) && $doublequote)
 121+ $subElemValue = Sanitizer::encodeAttribute($subElemValue);
118122
119123 if (gettype($subElemId) === 'integer') {
120124 $indElements[] = $subElemValue;
@@ -131,28 +135,29 @@
132136 ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
133137
134138 if (!is_null($subElemContent)) {
135 - $this->printText($indstr . Xml::element($elemName, $elemValue, $subElemContent));
 139+ $retval .= $indstr . Xml::element($elemName, $elemValue, $subElemContent);
136140 } elseif (!count($indElements) && !count($subElements)) {
137 - $this->printText($indstr . Xml::element($elemName, $elemValue));
 141+ $retval .= $indstr . Xml::element($elemName, $elemValue);
138142 } else {
139 - $this->printText($indstr . Xml::element($elemName, $elemValue, null));
 143+ $retval .= $indstr . Xml::element($elemName, $elemValue, null);
140144
141145 foreach ($subElements as $subElemId => & $subElemValue)
142 - $this->recXmlPrint($subElemId, $subElemValue, $indent);
 146+ $retval .= self::recXmlPrint($subElemId, $subElemValue, $indent);
143147
144148 foreach ($indElements as $subElemId => & $subElemValue)
145 - $this->recXmlPrint($subElemIndName, $subElemValue, $indent);
 149+ $retval .= self::recXmlPrint($subElemIndName, $subElemValue, $indent);
146150
147 - $this->printText($indstr . Xml::closeElement($elemName));
 151+ $retval .= $indstr . Xml::closeElement($elemName);
148152 }
149153 break;
150154 case 'object' :
151155 // ignore
152156 break;
153157 default :
154 - $this->printText($indstr . Xml::element($elemName, null, $elemValue));
 158+ $retval .= $indstr . Xml::element($elemName, null, $elemValue);
155159 break;
156160 }
 161+ return $retval;
157162 }
158163 function addXslt() {
159164 $nt = Title::newFromText( $this->mXslt );
@@ -171,10 +176,6 @@
172177 $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
173178 }
174179
175 - private function doubleQuote( $text ) {
176 - return Sanitizer::encodeAttribute( $text );
177 - }
178 -
179180 public function getAllowedParams() {
180181 return array (
181182 'xmldoublequote' => false,

Follow-up revisions

RevisionCommit summaryAuthorDate
r56080Cleanup for r55641: call static function staticallybrion23:54, 8 September 2009

Comments

#Comment by Brion VIBBER (talk | contribs)   23:51, 8 September 2009

+ $this->printText($this->recXmlPrint($this->mRootElemName,

We seem to be non-statically calling this static function, which will at least throw warnings under some error reporting modes and doesn't look too good. :)

#Comment by Brion VIBBER (talk | contribs)   23:54, 8 September 2009

Cleaned up in r56080

Status & tagging log