Properly deprecate srprop=score|hasrelated
[mediawiki.git] / includes / libs / XmlTypeCheck.php
blobeb98a4ad6841ee42cf4d46f63cb51582893d55cc
1 <?php
2 /**
3 * XML syntax and type checker.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 class XmlTypeCheck {
24 /**
25 * Will be set to true or false to indicate whether the file is
26 * well-formed XML. Note that this doesn't check schema validity.
28 public $wellFormed = false;
30 /**
31 * Will be set to true if the optional element filter returned
32 * a match at some point.
34 public $filterMatch = false;
36 /**
37 * Name of the document's root element, including any namespace
38 * as an expanded URL.
40 public $rootElement = '';
42 /**
43 * Additional parsing options
45 private $parserOptions = array(
46 'processing_instruction_handler' => '',
49 /**
50 * @param string $input a filename or string containing the XML element
51 * @param callable $filterCallback (optional)
52 * Function to call to do additional custom validity checks from the
53 * SAX element handler event. This gives you access to the element
54 * namespace, name, and attributes, but not to text contents.
55 * Filter should return 'true' to toggle on $this->filterMatch
56 * @param boolean $isFile (optional) indicates if the first parameter is a
57 * filename (default, true) or if it is a string (false)
58 * @param array $options list of additional parsing options:
59 * processing_instruction_handler: Callback for xml_set_processing_instruction_handler
61 function __construct( $input, $filterCallback = null, $isFile = true, $options = array() ) {
62 $this->filterCallback = $filterCallback;
63 $this->parserOptions = array_merge( $this->parserOptions, $options );
65 if ( $isFile ) {
66 $this->validateFromFile( $input );
67 } else {
68 $this->validateFromString( $input );
72 /**
73 * Alternative constructor: from filename
75 * @param string $fname the filename of an XML document
76 * @param callable $filterCallback (optional)
77 * Function to call to do additional custom validity checks from the
78 * SAX element handler event. This gives you access to the element
79 * namespace, name, and attributes, but not to text contents.
80 * Filter should return 'true' to toggle on $this->filterMatch
81 * @return XmlTypeCheck
83 public static function newFromFilename( $fname, $filterCallback = null ) {
84 return new self( $fname, $filterCallback, true );
87 /**
88 * Alternative constructor: from string
90 * @param string $string a string containing an XML element
91 * @param callable $filterCallback (optional)
92 * Function to call to do additional custom validity checks from the
93 * SAX element handler event. This gives you access to the element
94 * namespace, name, and attributes, but not to text contents.
95 * Filter should return 'true' to toggle on $this->filterMatch
96 * @return XmlTypeCheck
98 public static function newFromString( $string, $filterCallback = null ) {
99 return new self( $string, $filterCallback, false );
103 * Get the root element. Simple accessor to $rootElement
105 * @return string
107 public function getRootElement() {
108 return $this->rootElement;
112 * Get an XML parser with the root element handler.
113 * @see XmlTypeCheck::rootElementOpen()
114 * @return resource a resource handle for the XML parser
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' )
127 return $parser;
131 * @param string $fname the filename
133 private function validateFromFile( $fname ) {
134 $parser = $this->getParser();
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;
148 } while ( !feof( $file ) );
150 fclose( $file );
153 $this->wellFormed = true;
155 xml_parser_free( $parser );
160 * @param string $string the XML-input-string to be checked.
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;
170 $this->wellFormed = true;
174 * @param $parser
175 * @param $name
176 * @param $attribs
178 private function rootElementOpen( $parser, $name, $attribs ) {
179 $this->rootElement = $name;
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 );
191 * @param $parser
192 * @param $name
193 * @param $attribs
195 private function elementOpen( $parser, $name, $attribs ) {
196 if ( call_user_func( $this->filterCallback, $name, $attribs ) ) {
197 // Filter hit!
198 $this->filterMatch = true;
203 * @param $parser
204 * @param $target
205 * @param $data
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;