MediaWiki  1.23.8
XmlTypeCheck.php
Go to the documentation of this file.
1 <?php
23 class XmlTypeCheck {
28  public $wellFormed = false;
29 
34  public $filterMatch = false;
35 
40  public $rootElement = '';
41 
47  protected $elementData = array();
48 
52  protected $elementDataContext = array();
53 
57  protected $stackDepth = 0;
58 
62  private $parserOptions = array(
63  'processing_instruction_handler' => '',
64  );
65 
78  function __construct( $input, $filterCallback = null, $isFile = true, $options = array() ) {
79  $this->filterCallback = $filterCallback;
80  $this->parserOptions = array_merge( $this->parserOptions, $options );
81 
82  if ( $isFile ) {
83  $this->validateFromFile( $input );
84  } else {
85  $this->validateFromString( $input );
86  }
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 
133  private function getParser() {
134  $parser = xml_parser_create_ns( 'UTF-8' );
135  // case folding violates XML standard, turn it off
136  xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
137  xml_set_element_handler( $parser, array( $this, 'rootElementOpen' ), false );
138  if ( $this->parserOptions['processing_instruction_handler'] ) {
139  xml_set_processing_instruction_handler(
140  $parser,
141  array( $this, 'processingInstructionHandler' )
142  );
143  }
144  return $parser;
145  }
146 
150  private function validateFromFile( $fname ) {
151  $parser = $this->getParser();
152 
153  if ( file_exists( $fname ) ) {
154  $file = fopen( $fname, "rb" );
155  if ( $file ) {
156  do {
157  $chunk = fread( $file, 32768 );
158  $ret = xml_parse( $parser, $chunk, feof( $file ) );
159  if ( $ret == 0 ) {
160  $this->wellFormed = false;
161  fclose( $file );
162  xml_parser_free( $parser );
163  return;
164  }
165  } while ( !feof( $file ) );
166 
167  fclose( $file );
168  }
169  }
170  $this->wellFormed = true;
171 
172  xml_parser_free( $parser );
173  }
174 
179  private function validateFromString( $string ) {
180  $parser = $this->getParser();
181  $ret = xml_parse( $parser, $string, true );
182  xml_parser_free( $parser );
183  if ( $ret == 0 ) {
184  $this->wellFormed = false;
185  return;
186  }
187  $this->wellFormed = true;
188  }
189 
195  private function rootElementOpen( $parser, $name, $attribs ) {
196  $this->rootElement = $name;
197 
198  if ( is_callable( $this->filterCallback ) ) {
199  xml_set_element_handler(
200  $parser,
201  array( $this, 'elementOpen' ),
202  array( $this, 'elementClose' )
203  );
204  xml_set_character_data_handler( $parser, array( $this, 'elementData' ) );
205  $this->elementOpen( $parser, $name, $attribs );
206  } else {
207  // We only need the first open element
208  xml_set_element_handler( $parser, false, false );
209  }
210  }
211 
217  private function elementOpen( $parser, $name, $attribs ) {
218  $this->elementDataContext[] = array( $name, $attribs );
219  $this->elementData[] = '';
220  $this->stackDepth++;
221  }
222 
227  private function elementClose( $parser, $name ) {
228  list( $name, $attribs ) = array_pop( $this->elementDataContext );
229  $data = array_pop( $this->elementData );
230  $this->stackDepth--;
231 
232  if ( call_user_func(
233  $this->filterCallback,
234  $name,
235  $attribs,
236  $data
237  ) ) {
238  // Filter hit!
239  $this->filterMatch = true;
240  }
241  }
242 
247  private function elementData( $parser, $data ) {
248  // xml_set_character_data_handler breaks the data on & characters, so
249  // we collect any data here, and we'll run the callback in elementClose
250  $this->elementData[ $this->stackDepth - 1 ] .= trim( $data );
251  }
252 
258  private function processingInstructionHandler( $parser, $target, $data ) {
259  if ( call_user_func( $this->parserOptions['processing_instruction_handler'], $target, $data ) ) {
260  // Filter hit!
261  $this->filterMatch = true;
262  }
263  }
264 }
XmlTypeCheck\getParser
getParser()
Get an XML parser with the root element handler.
Definition: XmlTypeCheck.php:133
XmlTypeCheck\$filterMatch
$filterMatch
Will be set to true if the optional element filter returned a match at some point.
Definition: XmlTypeCheck.php:34
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:78
$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:40
XmlTypeCheck\elementOpen
elementOpen( $parser, $name, $attribs)
Definition: XmlTypeCheck.php:217
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:47
XmlTypeCheck\$elementDataContext
$elementDataContext
A stack of element names and attributes, as we process them.
Definition: XmlTypeCheck.php:52
XmlTypeCheck\$wellFormed
$wellFormed
Will be set to true or false to indicate whether the file is well-formed XML.
Definition: XmlTypeCheck.php:28
$parser
do that in ParserLimitReportFormat instead $parser
Definition: hooks.txt:1956
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:62
$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:57
XmlTypeCheck\processingInstructionHandler
processingInstructionHandler( $parser, $target, $data)
Definition: XmlTypeCheck.php:258
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
XmlTypeCheck\validateFromString
validateFromString( $string)
Definition: XmlTypeCheck.php:179
XmlTypeCheck\elementData
elementData( $parser, $data)
Definition: XmlTypeCheck.php:247
XmlTypeCheck\getRootElement
getRootElement()
Get the root element.
Definition: XmlTypeCheck.php:124
XmlTypeCheck
Definition: XmlTypeCheck.php:23
XmlTypeCheck\validateFromFile
validateFromFile( $fname)
Definition: XmlTypeCheck.php:150
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
XmlTypeCheck\elementClose
elementClose( $parser, $name)
Definition: XmlTypeCheck.php:227
$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
XmlTypeCheck\rootElementOpen
rootElementOpen( $parser, $name, $attribs)
Definition: XmlTypeCheck.php:195