Merge "prop=duplicatefiles does not show duplicates under same name"
[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 $file string filename
44 * @param $filterCallback callable (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 */
50 function __construct( $file, $filterCallback=null ) {
51 $this->filterCallback = $filterCallback;
52 $this->run( $file );
53 }
54
55 /**
56 * Get the root element. Simple accessor to $rootElement
57 *
58 * @return string
59 */
60 public function getRootElement() {
61 return $this->rootElement;
62 }
63
64 /**
65 * @param $fname
66 */
67 private function run( $fname ) {
68 $parser = xml_parser_create_ns( 'UTF-8' );
69
70 // case folding violates XML standard, turn it off
71 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
72
73 xml_set_element_handler( $parser, array( $this, 'rootElementOpen' ), false );
74
75 if ( file_exists( $fname ) ) {
76 $file = fopen( $fname, "rb" );
77 if ( $file ) {
78 do {
79 $chunk = fread( $file, 32768 );
80 $ret = xml_parse( $parser, $chunk, feof( $file ) );
81 if( $ret == 0 ) {
82 // XML isn't well-formed!
83 fclose( $file );
84 xml_parser_free( $parser );
85 return;
86 }
87 } while( !feof( $file ) );
88
89 fclose( $file );
90 }
91 }
92
93 $this->wellFormed = true;
94
95 xml_parser_free( $parser );
96 }
97
98 /**
99 * @param $parser
100 * @param $name
101 * @param $attribs
102 */
103 private function rootElementOpen( $parser, $name, $attribs ) {
104 $this->rootElement = $name;
105
106 if( is_callable( $this->filterCallback ) ) {
107 xml_set_element_handler( $parser, array( $this, 'elementOpen' ), false );
108 $this->elementOpen( $parser, $name, $attribs );
109 } else {
110 // We only need the first open element
111 xml_set_element_handler( $parser, false, false );
112 }
113 }
114
115 /**
116 * @param $parser
117 * @param $name
118 * @param $attribs
119 */
120 private function elementOpen( $parser, $name, $attribs ) {
121 if( call_user_func( $this->filterCallback, $name, $attribs ) ) {
122 // Filter hit!
123 $this->filterMatch = true;
124 }
125 }
126 }