Reverted r42528. Links with href="#" make firefox scroll to the top of the page,...
[mediawiki.git] / includes / XmlTypeCheck.php
blob10a08547fb059b40973b531d6cdfee893d43c521
1 <?php
3 class XmlTypeCheck {
4 /**
5 * Will be set to true or false to indicate whether the file is
6 * well-formed XML. Note that this doesn't check schema validity.
7 */
8 public $wellFormed = false;
10 /**
11 * Name of the document's root element, including any namespace
12 * as an expanded URL.
14 public $rootElement = '';
16 private $softNamespaces;
17 private $namespaces = array();
19 /**
20 * @param $file string filename
21 * @param $softNamespaces bool
22 * If set to true, use of undeclared XML namespaces will be ignored.
23 * This matches the behavior of rsvg, but more compliant consumers
24 * such as Firefox will reject such files.
25 * Leave off for the default, stricter checks.
27 function __construct( $file, $softNamespaces=false ) {
28 $this->softNamespaces = $softNamespaces;
29 $this->run( $file );
32 /**
33 * Get the root element. Simple accessor to $rootElement
35 public function getRootElement() {
36 return $this->rootElement;
39 private function run( $fname ) {
40 if( $this->softNamespaces ) {
41 $parser = xml_parser_create( 'UTF-8' );
42 } else {
43 $parser = xml_parser_create_ns( 'UTF-8' );
46 // case folding violates XML standard, turn it off
47 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
49 xml_set_element_handler( $parser, array( $this, 'elementOpen' ), false );
51 $file = fopen( $fname, "rb" );
52 do {
53 $chunk = fread( $file, 32768 );
54 $ret = xml_parse( $parser, $chunk, feof( $file ) );
55 if( $ret == 0 ) {
56 // XML isn't well-formed!
57 fclose( $file );
58 xml_parser_free( $parser );
59 return;
61 } while( !feof( $file ) );
63 $this->wellFormed = true;
65 fclose( $file );
66 xml_parser_free( $parser );
69 private function elementOpen( $parser, $name, $attribs ) {
70 if( $this->softNamespaces ) {
71 // Check namespaces manually, so expat doesn't throw
72 // errors on use of undeclared namespaces.
73 foreach( $attribs as $attrib => $val ) {
74 if( $attrib == 'xmlns' ) {
75 $this->namespaces[''] = $val;
76 } elseif( substr( $attrib, 0, strlen( 'xmlns:' ) ) == 'xmlns:' ) {
77 $this->namespaces[substr( $attrib, strlen( 'xmlns:' ) )] = $val;
81 if( strpos( $name, ':' ) === false ) {
82 $ns = '';
83 $subname = $name;
84 } else {
85 list( $ns, $subname ) = explode( ':', $name, 2 );
88 if( isset( $this->namespaces[$ns] ) ) {
89 $name = $this->namespaces[$ns] . ':' . $subname;
90 } else {
91 // Technically this is invalid for XML with Namespaces.
92 // But..... we'll just let it slide in soft mode.
96 // We only need the first open element
97 $this->rootElement = $name;
98 xml_set_element_handler( $parser, false, false );