MediaWiki  1.23.15
ApiFormatXml.php
Go to the documentation of this file.
1 <?php
31 class ApiFormatXml extends ApiFormatBase {
32 
33  private $mRootElemName = 'api';
34  public static $namespace = 'http://www.mediawiki.org/xml/api/';
35  private $mIncludeNamespace = false;
36  private $mXslt = null;
37 
38  public function getMimeType() {
39  return 'text/xml';
40  }
41 
42  public function getNeedsRawData() {
43  return true;
44  }
45 
46  public function setRootElement( $rootElemName ) {
47  $this->mRootElemName = $rootElemName;
48  }
49 
50  public function execute() {
51  $params = $this->extractRequestParams();
52  $this->mIncludeNamespace = $params['includexmlnamespace'];
53  $this->mXslt = $params['xslt'];
54 
55  $this->printText( '<?xml version="1.0"?>' );
56  if ( !is_null( $this->mXslt ) ) {
57  $this->addXslt();
58  }
59  if ( $this->mIncludeNamespace ) {
60  // If the result data already contains an 'xmlns' namespace added
61  // for custom XML output types, it will override the one for the
62  // generic API results.
63  // This allows API output of other XML types like Atom, RSS, RSD.
64  $data = $this->getResultData() + array( 'xmlns' => self::$namespace );
65  } else {
66  $data = $this->getResultData();
67  }
68 
69  $this->printText(
70  self::recXmlPrint( $this->mRootElemName,
71  $data,
72  $this->getIsHtml() ? -2 : null
73  )
74  );
75  }
76 
120  public static function recXmlPrint( $elemName, $elemValue, $indent ) {
121  $retval = '';
122  if ( !is_null( $indent ) ) {
123  $indent += 2;
124  $indstr = "\n" . str_repeat( ' ', $indent );
125  } else {
126  $indstr = '';
127  }
128  $elemName = str_replace( ' ', '_', $elemName );
129 
130  if ( is_array( $elemValue ) ) {
131  if ( isset( $elemValue['*'] ) ) {
132  $subElemContent = $elemValue['*'];
133  unset( $elemValue['*'] );
134 
135  // Add xml:space="preserve" to the
136  // element so XML parsers will leave
137  // whitespace in the content alone
138  $elemValue['xml:space'] = 'preserve';
139  } else {
140  $subElemContent = null;
141  }
142 
143  if ( isset( $elemValue['_element'] ) ) {
144  $subElemIndName = $elemValue['_element'];
145  unset( $elemValue['_element'] );
146  } else {
147  $subElemIndName = null;
148  }
149 
150  $indElements = array();
151  $subElements = array();
152  foreach ( $elemValue as $subElemId => & $subElemValue ) {
153  if ( is_int( $subElemId ) ) {
154  $indElements[] = $subElemValue;
155  unset( $elemValue[$subElemId] );
156  } elseif ( is_array( $subElemValue ) ) {
157  $subElements[$subElemId] = $subElemValue;
158  unset( $elemValue[$subElemId] );
159  } elseif ( is_bool( $subElemValue ) ) {
160  // treat true as empty string, skip false in xml format
161  if ( $subElemValue === true ) {
162  $subElemValue = '';
163  } else {
164  unset( $elemValue[$subElemId] );
165  }
166  }
167  }
168 
169  if ( is_null( $subElemIndName ) && count( $indElements ) ) {
170  ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys " .
171  "without _element value. Use ApiResult::setIndexedTagName()." );
172  }
173 
174  if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
175  ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
176  }
177 
178  if ( !is_null( $subElemContent ) ) {
179  $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
180  } elseif ( !count( $indElements ) && !count( $subElements ) ) {
181  $retval .= $indstr . Xml::element( $elemName, $elemValue );
182  } else {
183  $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
184 
185  foreach ( $subElements as $subElemId => & $subElemValue ) {
186  $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
187  }
188 
189  foreach ( $indElements as &$subElemValue ) {
190  $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
191  }
192 
193  $retval .= $indstr . Xml::closeElement( $elemName );
194  }
195  } elseif ( !is_object( $elemValue ) ) {
196  // to make sure null value doesn't produce unclosed element,
197  // which is what Xml::element( $elemName, null, null ) returns
198  if ( $elemValue === null ) {
199  $retval .= $indstr . Xml::element( $elemName );
200  } else {
201  $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
202  }
203  }
204 
205  return $retval;
206  }
207 
208  function addXslt() {
209  $nt = Title::newFromText( $this->mXslt );
210  if ( is_null( $nt ) || !$nt->exists() ) {
211  $this->setWarning( 'Invalid or non-existent stylesheet specified' );
212 
213  return;
214  }
215  if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
216  $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
217 
218  return;
219  }
220  if ( substr( $nt->getText(), -4 ) !== '.xsl' ) {
221  $this->setWarning( 'Stylesheet should have .xsl extension.' );
222 
223  return;
224  }
225  $this->printText( '<?xml-stylesheet href="' .
226  htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
227  }
228 
229  public function getAllowedParams() {
230  return array(
231  'xslt' => null,
232  'includexmlnamespace' => false,
233  );
234  }
235 
236  public function getParamDescription() {
237  return array(
238  'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
239  . 'in the MediaWiki namespace whose page name ends with ".xsl"',
240  'includexmlnamespace' => 'If specified, adds an XML namespace'
241  );
242  }
243 
244  public function getDescription() {
245  return 'Output data in XML format' . parent::getDescription();
246  }
247 }
ApiFormatBase\getNeedsRawData
getNeedsRawData()
Whether this formatter needs raw data such as _element tags.
Definition: ApiFormatBase.php:66
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ApiFormatBase
This is the abstract base class for API formatters.
Definition: ApiFormatBase.php:32
$params
$params
Definition: styleTest.css.php:40
ApiFormatBase\getMimeType
getMimeType()
Overriding class returns the mime type that should be sent to the client.
ApiBase\getParamDescription
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiBase.php:597
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:39
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
ApiFormatBase\getDescription
getDescription()
Returns the description string for this module.
Definition: ApiFormatBase.php:339
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:707
ApiBase\getResultData
getResultData()
Get the result data array (read-only)
Definition: ApiBase.php:219
ApiFormatBase\printText
printText( $text)
The main format printing function.
Definition: ApiFormatBase.php:229
ApiBase\setWarning
setWarning( $warning)
Set warning section for this module.
Definition: ApiBase.php:245
Xml\closeElement
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:118
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiBase\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiBase.php:585
ApiFormatBase\getIsHtml
getIsHtml()
Returns true when the HTML pretty-printer should be used.
Definition: ApiFormatBase.php:97
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:87
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:2030
ApiBase\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
$retval
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account incomplete not yet checked for validity & $retval
Definition: hooks.txt:237