Non-word characters don't terminate tag names.
[mediawiki.git] / includes / XmlTypeCheck.php
blob92ca7d80106b6445146ebf9f8f8e7ea61a9eea9e
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 * @param string $input a filename or string containing the XML element
44 * @param callable $filterCallback (optional)
45 * Function to call to do additional custom validity checks from the
46 * SAX element handler event. This gives you access to the element
47 * namespace, name, and attributes, but not to text contents.
48 * Filter should return 'true' to toggle on $this->filterMatch
49 * @param boolean $isFile (optional) indicates if the first parameter is a
50 * filename (default, true) or if it is a string (false)
52 function __construct( $input, $filterCallback = null, $isFile = true ) {
53 $this->filterCallback = $filterCallback;
54 if ( $isFile ) {
55 $this->validateFromFile( $input );
56 } else {
57 $this->validateFromString( $input );
61 /**
62 * Alternative constructor: from filename
64 * @param string $fname the filename of an XML document
65 * @param callable $filterCallback (optional)
66 * Function to call to do additional custom validity checks from the
67 * SAX element handler event. This gives you access to the element
68 * namespace, name, and attributes, but not to text contents.
69 * Filter should return 'true' to toggle on $this->filterMatch
70 * @return XmlTypeCheck
72 public static function newFromFilename( $fname, $filterCallback = null ) {
73 return new self( $fname, $filterCallback, true );
76 /**
77 * Alternative constructor: from string
79 * @param string $string a string containing an XML element
80 * @param callable $filterCallback (optional)
81 * Function to call to do additional custom validity checks from the
82 * SAX element handler event. This gives you access to the element
83 * namespace, name, and attributes, but not to text contents.
84 * Filter should return 'true' to toggle on $this->filterMatch
85 * @return XmlTypeCheck
87 public static function newFromString( $string, $filterCallback = null ) {
88 return new self( $string, $filterCallback, false );
91 /**
92 * Get the root element. Simple accessor to $rootElement
94 * @return string
96 public function getRootElement() {
97 return $this->rootElement;
101 * Get an XML parser with the root element handler.
102 * @see XmlTypeCheck::rootElementOpen()
103 * @return resource a resource handle for the XML parser
105 private function getParser() {
106 $parser = xml_parser_create_ns( 'UTF-8' );
107 // case folding violates XML standard, turn it off
108 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
109 xml_set_element_handler( $parser, array( $this, 'rootElementOpen' ), false );
110 return $parser;
114 * @param string $fname the filename
116 private function validateFromFile( $fname ) {
117 $parser = $this->getParser();
119 if ( file_exists( $fname ) ) {
120 $file = fopen( $fname, "rb" );
121 if ( $file ) {
122 do {
123 $chunk = fread( $file, 32768 );
124 $ret = xml_parse( $parser, $chunk, feof( $file ) );
125 if ( $ret == 0 ) {
126 $this->wellFormed = false;
127 fclose( $file );
128 xml_parser_free( $parser );
129 return;
131 } while ( !feof( $file ) );
133 fclose( $file );
136 $this->wellFormed = true;
138 xml_parser_free( $parser );
143 * @param string $string the XML-input-string to be checked.
145 private function validateFromString( $string ) {
146 $parser = $this->getParser();
147 $ret = xml_parse( $parser, $string, true );
148 xml_parser_free( $parser );
149 if ( $ret == 0 ) {
150 $this->wellFormed = false;
151 return;
153 $this->wellFormed = true;
157 * @param $parser
158 * @param $name
159 * @param $attribs
161 private function rootElementOpen( $parser, $name, $attribs ) {
162 $this->rootElement = $name;
164 if ( is_callable( $this->filterCallback ) ) {
165 xml_set_element_handler( $parser, array( $this, 'elementOpen' ), false );
166 $this->elementOpen( $parser, $name, $attribs );
167 } else {
168 // We only need the first open element
169 xml_set_element_handler( $parser, false, false );
174 * @param $parser
175 * @param $name
176 * @param $attribs
178 private function elementOpen( $parser, $name, $attribs ) {
179 if ( call_user_func( $this->filterCallback, $name, $attribs ) ) {
180 // Filter hit!
181 $this->filterMatch = true;