Add DROP INDEX support to DatabaseSqlite::replaceVars method
[lhc/web/wiklou.git] / includes / XmlTypeCheck.php
1 <?php
2 /**
3 * XML syntax and type checker.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
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.
27 */
28 public $wellFormed = false;
29
30 /**
31 * Will be set to true if the optional element filter returned
32 * a match at some point.
33 */
34 public $filterMatch = false;
35
36 /**
37 * Name of the document's root element, including any namespace
38 * as an expanded URL.
39 */
40 public $rootElement = '';
41
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)
51 */
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 );
58 }
59 }
60
61 /**
62 * Alternative constructor: from filename
63 *
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
71 */
72 public static function newFromFilename( $fname, $filterCallback = null ) {
73 return new self( $fname, $filterCallback, true );
74 }
75
76 /**
77 * Alternative constructor: from string
78 *
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
86 */
87 public static function newFromString( $string, $filterCallback = null ) {
88 return new self( $string, $filterCallback, false );
89 }
90
91 /**
92 * Get the root element. Simple accessor to $rootElement
93 *
94 * @return string
95 */
96 public function getRootElement() {
97 return $this->rootElement;
98 }
99
100 /**
101 * Get an XML parser with the root element handler.
102 * @see XmlTypeCheck::rootElementOpen()
103 * @return resource a resource handle for the XML parser
104 */
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;
111 }
112
113 /**
114 * @param string $fname the filename
115 */
116 private function validateFromFile( $fname ) {
117 $parser = $this->getParser();
118
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;
130 }
131 } while ( !feof( $file ) );
132
133 fclose( $file );
134 }
135 }
136 $this->wellFormed = true;
137
138 xml_parser_free( $parser );
139 }
140
141 /**
142 *
143 * @param string $string the XML-input-string to be checked.
144 */
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;
152 }
153 $this->wellFormed = true;
154 }
155
156 /**
157 * @param $parser
158 * @param $name
159 * @param $attribs
160 */
161 private function rootElementOpen( $parser, $name, $attribs ) {
162 $this->rootElement = $name;
163
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 );
170 }
171 }
172
173 /**
174 * @param $parser
175 * @param $name
176 * @param $attribs
177 */
178 private function elementOpen( $parser, $name, $attribs ) {
179 if ( call_user_func( $this->filterCallback, $name, $attribs ) ) {
180 // Filter hit!
181 $this->filterMatch = true;
182 }
183 }
184 }