Merge "mediawiki.skinning: Merge toccolours into css rule for toc and mw-warning"
[lhc/web/wiklou.git] / includes / libs / XmlTypeCheck.php
index 92ca7d8..0d6c3a6 100644 (file)
@@ -39,18 +39,46 @@ class XmlTypeCheck {
         */
        public $rootElement = '';
 
+       /**
+        * A stack of strings containing the data of each xml element as it's processed. Append
+        * data to the top string of the stack, then pop off the string and process it when the
+        * element is closed.
+        */
+       protected $elementData = array();
+
+       /**
+        * A stack of element names and attributes, as we process them.
+        */
+       protected $elementDataContext = array();
+
+       /**
+        * Current depth of the data stack.
+        */
+       protected $stackDepth = 0;
+
+       /**
+        * Additional parsing options
+        */
+       private $parserOptions = array(
+               'processing_instruction_handler' => '',
+       );
+
        /**
         * @param string $input a filename or string containing the XML element
         * @param callable $filterCallback (optional)
         *        Function to call to do additional custom validity checks from the
         *        SAX element handler event. This gives you access to the element
-        *        namespace, name, and attributes, but not to text contents.
+        *        namespace, name, attributes, and text contents.
         *        Filter should return 'true' to toggle on $this->filterMatch
-        * @param boolean $isFile (optional) indicates if the first parameter is a
+        * @param bool $isFile (optional) indicates if the first parameter is a
         *        filename (default, true) or if it is a string (false)
+        * @param array $options list of additional parsing options:
+        *        processing_instruction_handler: Callback for xml_set_processing_instruction_handler
         */
-       function __construct( $input, $filterCallback = null, $isFile = true ) {
+       function __construct( $input, $filterCallback = null, $isFile = true, $options = array() ) {
                $this->filterCallback = $filterCallback;
+               $this->parserOptions = array_merge( $this->parserOptions, $options );
+
                if ( $isFile ) {
                        $this->validateFromFile( $input );
                } else {
@@ -107,6 +135,12 @@ class XmlTypeCheck {
                // case folding violates XML standard, turn it off
                xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
                xml_set_element_handler( $parser, array( $this, 'rootElementOpen' ), false );
+               if ( $this->parserOptions['processing_instruction_handler'] ) {
+                       xml_set_processing_instruction_handler(
+                               $parser,
+                               array( $this, 'processingInstructionHandler' )
+                       );
+               }
                return $parser;
        }
 
@@ -162,7 +196,12 @@ class XmlTypeCheck {
                $this->rootElement = $name;
 
                if ( is_callable( $this->filterCallback ) ) {
-                       xml_set_element_handler( $parser, array( $this, 'elementOpen' ), false );
+                       xml_set_element_handler(
+                               $parser,
+                               array( $this, 'elementOpen' ),
+                               array( $this, 'elementClose' )
+                       );
+                       xml_set_character_data_handler( $parser, array( $this, 'elementData' ) );
                        $this->elementOpen( $parser, $name, $attribs );
                } else {
                        // We only need the first open element
@@ -176,7 +215,48 @@ class XmlTypeCheck {
         * @param $attribs
         */
        private function elementOpen( $parser, $name, $attribs ) {
-               if ( call_user_func( $this->filterCallback, $name, $attribs ) ) {
+               $this->elementDataContext[] = array( $name, $attribs );
+               $this->elementData[] = '';
+               $this->stackDepth++;
+       }
+
+       /**
+        * @param $parser
+        * @param $name
+        */
+       private function elementClose( $parser, $name ) {
+               list( $name, $attribs ) = array_pop( $this->elementDataContext );
+               $data = array_pop( $this->elementData );
+               $this->stackDepth--;
+
+               if ( call_user_func(
+                       $this->filterCallback,
+                       $name,
+                       $attribs,
+                       $data
+               ) ) {
+                       // Filter hit!
+                       $this->filterMatch = true;
+               }
+       }
+
+       /**
+        * @param $parser
+        * @param $data
+        */
+       private function elementData( $parser, $data ) {
+               // xml_set_character_data_handler breaks the data on & characters, so
+               // we collect any data here, and we'll run the callback in elementClose
+               $this->elementData[ $this->stackDepth - 1 ] .= trim( $data );
+       }
+
+       /**
+        * @param $parser
+        * @param $target
+        * @param $data
+        */
+       private function processingInstructionHandler( $parser, $target, $data ) {
+               if ( call_user_func( $this->parserOptions['processing_instruction_handler'], $target, $data ) ) {
                        // Filter hit!
                        $this->filterMatch = true;
                }