MediaWiki  1.23.1
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 
45  private $parserOptions = array(
46  'processing_instruction_handler' => '',
47  );
48 
61  function __construct( $input, $filterCallback = null, $isFile = true, $options = array() ) {
62  $this->filterCallback = $filterCallback;
63  $this->parserOptions = array_merge( $this->parserOptions, $options );
64 
65  if ( $isFile ) {
66  $this->validateFromFile( $input );
67  } else {
68  $this->validateFromString( $input );
69  }
70  }
71 
83  public static function newFromFilename( $fname, $filterCallback = null ) {
84  return new self( $fname, $filterCallback, true );
85  }
86 
98  public static function newFromString( $string, $filterCallback = null ) {
99  return new self( $string, $filterCallback, false );
100  }
101 
107  public function getRootElement() {
108  return $this->rootElement;
109  }
110 
116  private function getParser() {
117  $parser = xml_parser_create_ns( 'UTF-8' );
118  // case folding violates XML standard, turn it off
119  xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
120  xml_set_element_handler( $parser, array( $this, 'rootElementOpen' ), false );
121  if ( $this->parserOptions['processing_instruction_handler'] ) {
122  xml_set_processing_instruction_handler(
123  $parser,
124  array( $this, 'processingInstructionHandler' )
125  );
126  }
127  return $parser;
128  }
129 
133  private function validateFromFile( $fname ) {
134  $parser = $this->getParser();
135 
136  if ( file_exists( $fname ) ) {
137  $file = fopen( $fname, "rb" );
138  if ( $file ) {
139  do {
140  $chunk = fread( $file, 32768 );
141  $ret = xml_parse( $parser, $chunk, feof( $file ) );
142  if ( $ret == 0 ) {
143  $this->wellFormed = false;
144  fclose( $file );
145  xml_parser_free( $parser );
146  return;
147  }
148  } while ( !feof( $file ) );
149 
150  fclose( $file );
151  }
152  }
153  $this->wellFormed = true;
154 
155  xml_parser_free( $parser );
156  }
157 
162  private function validateFromString( $string ) {
163  $parser = $this->getParser();
164  $ret = xml_parse( $parser, $string, true );
165  xml_parser_free( $parser );
166  if ( $ret == 0 ) {
167  $this->wellFormed = false;
168  return;
169  }
170  $this->wellFormed = true;
171  }
172 
178  private function rootElementOpen( $parser, $name, $attribs ) {
179  $this->rootElement = $name;
180 
181  if ( is_callable( $this->filterCallback ) ) {
182  xml_set_element_handler( $parser, array( $this, 'elementOpen' ), false );
183  $this->elementOpen( $parser, $name, $attribs );
184  } else {
185  // We only need the first open element
186  xml_set_element_handler( $parser, false, false );
187  }
188  }
189 
195  private function elementOpen( $parser, $name, $attribs ) {
196  if ( call_user_func( $this->filterCallback, $name, $attribs ) ) {
197  // Filter hit!
198  $this->filterMatch = true;
199  }
200  }
201 
207  private function processingInstructionHandler( $parser, $target, $data ) {
208  if ( call_user_func( $this->parserOptions['processing_instruction_handler'], $target, $data ) ) {
209  // Filter hit!
210  $this->filterMatch = true;
211  }
212  }
213 }
XmlTypeCheck\getParser
getParser()
Get an XML parser with the root element handler.
Definition: XmlTypeCheck.php:116
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:98
$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:61
$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:83
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:195
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\$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.
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:45
$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\processingInstructionHandler
processingInstructionHandler( $parser, $target, $data)
Definition: XmlTypeCheck.php:207
$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:162
XmlTypeCheck\getRootElement
getRootElement()
Get the root element.
Definition: XmlTypeCheck.php:107
XmlTypeCheck
Definition: XmlTypeCheck.php:23
XmlTypeCheck\validateFromFile
validateFromFile( $fname)
Definition: XmlTypeCheck.php:133
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
$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:178