Merge "Add language Doteli (dty)"
[lhc/web/wiklou.git] / includes / media / XMP.php
index 042f749..a838355 100644 (file)
@@ -155,16 +155,27 @@ class XMPReader implements LoggerAwareInterface {
                $this->logger = $logger;
        }
 
+       /**
+        * free the XML parser.
+        *
+        * @note It is unclear to me if we really need to do this ourselves
+        *  or if php garbage collection will automatically free the xmlParser
+        *  when it is no longer needed.
+        */
+       private function destroyXMLParser() {
+               if ( $this->xmlParser ) {
+                       xml_parser_free( $this->xmlParser );
+                       $this->xmlParser = null;
+               }
+       }
+
        /**
         * Main use is if a single item has multiple xmp documents describing it.
         * For example in jpeg's with extendedXMP
         */
        private function resetXMLParser() {
 
-               if ( $this->xmlParser ) {
-                       //is this needed?
-                       xml_parser_free( $this->xmlParser );
-               }
+               $this->destroyXMLParser();
 
                $this->xmlParser = xml_parser_create_ns( 'UTF-8', ' ' );
                xml_parser_set_option( $this->xmlParser, XML_OPTION_CASE_FOLDING, 0 );
@@ -180,15 +191,6 @@ class XMPReader implements LoggerAwareInterface {
                $this->xmlParsableBuffer = '';
        }
 
-       /** Destroy the xml parser
-        *
-        * Not sure if this is actually needed.
-        */
-       function __destruct() {
-               // not sure if this is needed.
-               xml_parser_free( $this->xmlParser );
-       }
-
        /**
         * Check if this instance supports using this class
         */
@@ -294,12 +296,11 @@ class XMPReader implements LoggerAwareInterface {
         *
         * @param string $content XMP data
         * @param bool $allOfIt If this is all the data (true) or if its split up (false). Default true
-        * @param bool $reset Does xml parser need to be reset. Default false
         * @throws RuntimeException
         * @return bool Success.
         */
-       public function parse( $content, $allOfIt = true, $reset = false ) {
-               if ( $reset ) {
+       public function parse( $content, $allOfIt = true ) {
+               if ( !$this->xmlParser ) {
                        $this->resetXMLParser();
                }
                try {
@@ -338,9 +339,9 @@ class XMPReader implements LoggerAwareInterface {
                        }
                        if ( $this->charset !== 'UTF-8' ) {
                                //don't convert if already utf-8
-                               wfSuppressWarnings();
+                               MediaWiki\suppressWarnings();
                                $content = iconv( $this->charset, 'UTF-8//IGNORE', $content );
-                               wfRestoreWarnings();
+                               MediaWiki\restoreWarnings();
                        }
 
                        // Ensure the XMP block does not have an xml doctype declaration, which
@@ -373,14 +374,21 @@ class XMPReader implements LoggerAwareInterface {
 
                                $this->logger->info( "XMPReader::parse : Error reading XMP content: $error ($where)" );
                                $this->results = array(); // blank if error.
+                               $this->destroyXMLParser();
                                return false;
                        }
                } catch ( Exception $e ) {
                        $this->logger->info( 'XMP parse error: ' . $e );
                        $this->results = array();
 
+                       if ( $allOfIt ) {
+                               $this->destroyXMLParser();
+                       }
                        return false;
                }
+               if ( $allOfIt ) {
+                       $this->destroyXMLParser();
+               }
 
                return true;
        }
@@ -407,7 +415,7 @@ class XMPReader implements LoggerAwareInterface {
                $len = unpack( 'Nlength/Noffset', substr( $content, 32, 8 ) );
 
                if ( !$len || $len['length'] < 4 || $len['offset'] < 0 || $len['offset'] > $len['length'] ) {
-                       $this->logger->info(  __METHOD__ . 'Error reading extended XMP block, invalid length or offset.' );
+                       $this->logger->info( __METHOD__ . 'Error reading extended XMP block, invalid length or offset.' );
 
                        return false;
                }
@@ -525,7 +533,7 @@ class XMPReader implements LoggerAwareInterface {
 
                // Even with LIBXML_NOWARNING set, XMLReader::read gives a warning
                // when parsing truncated XML, which causes unit tests to fail.
-               wfSuppressWarnings();
+               MediaWiki\suppressWarnings();
                while ( $reader->read() ) {
                        if ( $reader->nodeType === XMLReader::ELEMENT ) {
                                // Reached the first element without hitting a doctype declaration
@@ -539,7 +547,7 @@ class XMPReader implements LoggerAwareInterface {
                                break;
                        }
                }
-               wfRestoreWarnings();
+               MediaWiki\restoreWarnings();
 
                if ( !is_null( $result ) ) {
                        return $result;
@@ -630,7 +638,6 @@ class XMPReader implements LoggerAwareInterface {
                // Validate structures.
                list( $ns, $tag ) = explode( ' ', $elm, 2 );
                if ( isset( $this->items[$ns][$tag]['validate'] ) ) {
-
                        $info =& $this->items[$ns][$tag];
                        $finalName = isset( $info['map_name'] )
                                ? $info['map_name'] : $tag;