MediaWiki  1.23.15
XmlTypeCheck.php
Go to the documentation of this file.
1 <?php
28 class XmlTypeCheck {
33  public $wellFormed = null;
34 
39  public $filterMatch = false;
40 
45  public $rootElement = '';
46 
52  protected $elementData = array();
53 
57  protected $elementDataContext = array();
58 
62  protected $stackDepth = 0;
63 
67  private $parserOptions = array(
68  'processing_instruction_handler' => '',
69  );
70 
83  function __construct( $input, $filterCallback = null, $isFile = true, $options = array() ) {
84  $this->filterCallback = $filterCallback;
85  $this->parserOptions = array_merge( $this->parserOptions, $options );
86  $this->validateFromInput( $input, $isFile );
87  }
88 
100  public static function newFromFilename( $fname, $filterCallback = null ) {
101  return new self( $fname, $filterCallback, true );
102  }
103 
115  public static function newFromString( $string, $filterCallback = null ) {
116  return new self( $string, $filterCallback, false );
117  }
118 
124  public function getRootElement() {
125  return $this->rootElement;
126  }
127 
128 
132  private function validateFromInput( $xml, $isFile ) {
133  $reader = new XMLReader();
134  if ( $isFile ) {
135  $s = $reader->open( $xml, null, LIBXML_NOERROR | LIBXML_NOWARNING );
136  } else {
137  $s = $reader->XML( $xml, null, LIBXML_NOERROR | LIBXML_NOWARNING );
138  }
139  if ( $s !== true ) {
140  // Couldn't open the XML
141  $this->wellFormed = false;
142  } else {
143  $oldDisable = libxml_disable_entity_loader( true );
144  $reader->setParserProperty( XMLReader::SUBST_ENTITIES, true );
145  try {
146  $this->validate( $reader );
147  } catch ( Exception $e ) {
148  // Calling this malformed, because we didn't parse the whole
149  // thing. Maybe just an external entity refernce.
150  $this->wellFormed = false;
151  $reader->close();
152  libxml_disable_entity_loader( $oldDisable );
153  throw $e;
154  }
155  $reader->close();
156  libxml_disable_entity_loader( $oldDisable );
157  }
158  }
159 
160  private function readNext( XMLReader $reader ) {
161  set_error_handler( array( $this, 'XmlErrorHandler' ) );
162  $ret = $reader->read();
163  restore_error_handler();
164  return $ret;
165  }
166 
167  public function XmlErrorHandler( $errno, $errstr ) {
168  $this->wellFormed = false;
169  }
170 
171  private function validate( $reader ) {
172 
173  // First, move through anything that isn't an element, and
174  // handle any processing instructions with the callback
175  do {
176  if( !$this->readNext( $reader ) ) {
177  // Hit the end of the document before any elements
178  $this->wellFormed = false;
179  return;
180  }
181  if ( $reader->nodeType === XMLReader::PI ) {
182  $this->processingInstructionHandler( $reader->name, $reader->value );
183  }
184  } while ( $reader->nodeType != XMLReader::ELEMENT );
185 
186  // Process the rest of the document
187  do {
188  switch ( $reader->nodeType ) {
189  case XMLReader::ELEMENT:
190  $name = $this->expandNS(
191  $reader->name,
192  $reader->namespaceURI
193  );
194  if ( $this->rootElement === '' ) {
195  $this->rootElement = $name;
196  }
197  $empty = $reader->isEmptyElement;
198  $attrs = $this->getAttributesArray( $reader );
199  $this->elementOpen( $name, $attrs );
200  if ( $empty ) {
201  $this->elementClose();
202  }
203  break;
204 
205  case XMLReader::END_ELEMENT:
206  $this->elementClose();
207  break;
208 
210  case XMLReader::SIGNIFICANT_WHITESPACE:
211  case XMLReader::CDATA:
212  case XMLReader::TEXT:
213  $this->elementData( $reader->value );
214  break;
215 
216  case XMLReader::ENTITY_REF:
217  // Unexpanded entity (maybe external?),
218  // don't send to the filter (xml_parse didn't)
219  break;
220 
221  case XMLReader::COMMENT:
222  // Don't send to the filter (xml_parse didn't)
223  break;
224 
225  case XMLReader::PI:
226  // Processing instructions can happen after the header too
228  $reader->name,
229  $reader->value
230  );
231  break;
232  default:
233  // One of DOC, DOC_TYPE, ENTITY, END_ENTITY,
234  // NOTATION, or XML_DECLARATION
235  // xml_parse didn't send these to the filter, so we won't.
236  }
237 
238  } while ( $this->readNext( $reader ) );
239 
240  if ( $this->stackDepth !== 0 ) {
241  $this->wellFormed = false;
242  } elseif ( $this->wellFormed === null ) {
243  $this->wellFormed = true;
244  }
245 
246  }
247 
253  private function getAttributesArray( XMLReader $r ) {
254  $attrs = array();
255  while ( $r->moveToNextAttribute() ) {
256  if ( $r->namespaceURI === 'http://www.w3.org/2000/xmlns/' ) {
257  // XMLReader treats xmlns attributes as normal
258  // attributes, while xml_parse doesn't
259  continue;
260  }
261  $name = $this->expandNS( $r->name, $r->namespaceURI );
262  $attrs[$name] = $r->value;
263  }
264  return $attrs;
265  }
266 
272  private function expandNS( $name, $namespaceURI ) {
273  if ( $namespaceURI ) {
274  $parts = explode( ':', $name );
275  $localname = array_pop( $parts );
276  return "$namespaceURI:$localname";
277  }
278  return $name;
279  }
280 
285  private function elementOpen( $name, $attribs ) {
286  $this->elementDataContext[] = array( $name, $attribs );
287  $this->elementData[] = '';
288  $this->stackDepth++;
289  }
290 
293  private function elementClose() {
294  list( $name, $attribs ) = array_pop( $this->elementDataContext );
295  $data = array_pop( $this->elementData );
296  $this->stackDepth--;
297 
298  if ( is_callable( $this->filterCallback )
299  && call_user_func(
300  $this->filterCallback,
301  $name,
302  $attribs,
303  $data
304  )
305  ) {
306  // Filter hit
307  $this->filterMatch = true;
308  }
309  }
310 
314  private function elementData( $data ) {
315  // Collect any data here, and we'll run the callback in elementClose
316  $this->elementData[ $this->stackDepth - 1 ] .= trim( $data );
317  }
318 
323  private function processingInstructionHandler( $target, $data ) {
324  if ( $this->parserOptions['processing_instruction_handler'] ) {
325  if ( call_user_func(
326  $this->parserOptions['processing_instruction_handler'],
327  $target,
328  $data
329  ) ) {
330  // Filter hit!
331  $this->filterMatch = true;
332  }
333  }
334  }
335 }
XmlTypeCheck\$filterMatch
$filterMatch
Will be set to true if the optional element filter returned a match at some point.
Definition: XmlTypeCheck.php:39
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
XmlTypeCheck\newFromString
static newFromString( $string, $filterCallback=null)
Alternative constructor: from string.
Definition: XmlTypeCheck.php:115
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1530
XmlTypeCheck\__construct
__construct( $input, $filterCallback=null, $isFile=true, $options=array())
Definition: XmlTypeCheck.php:83
$fname
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition: Setup.php:35
XmlTypeCheck\newFromFilename
static newFromFilename( $fname, $filterCallback=null)
Alternative constructor: from filename.
Definition: XmlTypeCheck.php:100
XmlTypeCheck\$rootElement
$rootElement
Name of the document's root element, including any namespace as an expanded URL.
Definition: XmlTypeCheck.php:45
$s
$s
Definition: mergeMessageFileList.php:156
XmlTypeCheck\expandNS
expandNS( $name, $namespaceURI)
Definition: XmlTypeCheck.php:272
XmlTypeCheck\getAttributesArray
getAttributesArray(XMLReader $r)
Get all of the attributes for an XMLReader's current node.
Definition: XmlTypeCheck.php:253
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1530
XmlTypeCheck\$elementData
$elementData
A stack of strings containing the data of each xml element as it's processed.
Definition: XmlTypeCheck.php:52
csslex.WHITESPACE
string WHITESPACE
Definition: csslex.py:70
XmlTypeCheck\$elementDataContext
$elementDataContext
A stack of element names and attributes, as we process them.
Definition: XmlTypeCheck.php:57
XmlTypeCheck\XmlErrorHandler
XmlErrorHandler( $errno, $errstr)
Definition: XmlTypeCheck.php:167
XmlTypeCheck\$wellFormed
$wellFormed
Will be set to true or false to indicate whether the file is well-formed XML.
Definition: XmlTypeCheck.php:33
XmlTypeCheck\readNext
readNext(XMLReader $reader)
Definition: XmlTypeCheck.php:160
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:188
XmlTypeCheck\$parserOptions
$parserOptions
Additional parsing options.
Definition: XmlTypeCheck.php:67
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1530
XmlTypeCheck\$stackDepth
$stackDepth
Current depth of the data stack.
Definition: XmlTypeCheck.php:62
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
XmlTypeCheck\elementData
elementData( $data)
Definition: XmlTypeCheck.php:314
XmlTypeCheck\getRootElement
getRootElement()
Get the root element.
Definition: XmlTypeCheck.php:124
XmlTypeCheck
Definition: XmlTypeCheck.php:28
COMMENT
const COMMENT
Definition: UtfNormalTest2.php:33
XmlTypeCheck\elementOpen
elementOpen( $name, $attribs)
Definition: XmlTypeCheck.php:285
XmlTypeCheck\processingInstructionHandler
processingInstructionHandler( $target, $data)
Definition: XmlTypeCheck.php:323
XmlTypeCheck\validateFromInput
validateFromInput( $xml, $isFile)
Definition: XmlTypeCheck.php:132
XmlTypeCheck\validate
validate( $reader)
Definition: XmlTypeCheck.php:171
XmlTypeCheck\elementClose
elementClose()
Definition: XmlTypeCheck.php:293
$attribs
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition: hooks.txt:1530
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:1632