XMP: Throw generic exceptions instead of MWException
authorKunal Mehta <legoktm@gmail.com>
Sat, 23 May 2015 21:26:16 +0000 (23:26 +0200)
committerKunal Mehta <legoktm@gmail.com>
Sat, 23 May 2015 21:26:16 +0000 (23:26 +0200)
Change-Id: I8bd81cfccedaa6f474e541b71c93c66150b6f7b3

includes/media/XMP.php

index 5af31fa..957ddd2 100644 (file)
@@ -129,7 +129,7 @@ class XMPReader {
 
                if ( !function_exists( 'xml_parser_create_ns' ) ) {
                        // this should already be checked by this point
-                       throw new MWException( 'XMP support requires XML Parser' );
+                       throw new RuntimeException( 'XMP support requires XML Parser' );
                }
 
                $this->items = XMPInfo::getItems();
@@ -277,7 +277,7 @@ class XMPReader {
         * @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 MWException
+        * @throws RuntimeException
         * @return bool Success.
         */
        public function parse( $content, $allOfIt = true, $reset = false ) {
@@ -311,7 +311,7 @@ class XMPReader {
                                                        break;
                                                default:
                                                        //this should be impossible to get to
-                                                       throw new MWException( "Invalid BOM" );
+                                                       throw new RuntimeException( "Invalid BOM" );
                                        }
                                } else {
                                        // standard specifically says, if no bom assume utf-8
@@ -447,7 +447,7 @@ class XMPReader {
         *
         * @param XMLParser $parser XMLParser reference to the xml parser
         * @param string $data Character data
-        * @throws MWException On invalid data
+        * @throws RuntimeException On invalid data
         */
        function char( $parser, $data ) {
 
@@ -457,7 +457,7 @@ class XMPReader {
                }
 
                if ( !isset( $this->mode[0] ) ) {
-                       throw new MWException( 'Unexpected character data before first rdf:Description element' );
+                       throw new RuntimeException( 'Unexpected character data before first rdf:Description element' );
                }
 
                if ( $this->mode[0] === self::MODE_IGNORE ) {
@@ -467,7 +467,7 @@ class XMPReader {
                if ( $this->mode[0] !== self::MODE_SIMPLE
                        && $this->mode[0] !== self::MODE_QDESC
                ) {
-                       throw new MWException( 'character data where not expected. (mode ' . $this->mode[0] . ')' );
+                       throw new RuntimeException( 'character data where not expected. (mode ' . $this->mode[0] . ')' );
                }
 
                // to check, how does this handle w.s.
@@ -595,7 +595,7 @@ class XMPReader {
         * This method is called when we hit the "</exif:ISOSpeedRatings>" tag.
         *
         * @param string $elm Namespace . space . tag name.
-        * @throws MWException
+        * @throws RuntimeException
         */
        private function endElementNested( $elm ) {
 
@@ -605,7 +605,7 @@ class XMPReader {
                        && !( $elm === self::NS_RDF . ' Description'
                                && $this->mode[0] === self::MODE_STRUCT )
                ) {
-                       throw new MWException( "nesting mismatch. got a </$elm> but expected a </" .
+                       throw new RuntimeException( "nesting mismatch. got a </$elm> but expected a </" .
                                $this->curItem[0] . '>' );
                }
 
@@ -662,7 +662,7 @@ class XMPReader {
         * hit the "</rdf:li>")
         *
         * @param string $elm Namespace . ' ' . element name
-        * @throws MWException
+        * @throws RuntimeException
         */
        private function endElementModeLi( $elm ) {
 
@@ -689,7 +689,7 @@ class XMPReader {
                                $this->results['xmp-' . $info['map_group']][$finalName]['_type'] = 'lang';
                        }
                } else {
-                       throw new MWException( __METHOD__ . " expected </rdf:seq> or </rdf:bag> but instead got $elm." );
+                       throw new RuntimeException( __METHOD__ . " expected </rdf:seq> or </rdf:bag> but instead got $elm." );
                }
        }
 
@@ -727,7 +727,7 @@ class XMPReader {
         *
         * @param XMLParser $parser
         * @param string $elm Namespace . ' ' . element name
-        * @throws MWException
+        * @throws RuntimeException
         */
        function endElement( $parser, $elm ) {
                if ( $elm === ( self::NS_RDF . ' RDF' )
@@ -757,13 +757,13 @@ class XMPReader {
                if ( count( $this->mode[0] ) === 0 ) {
                        // This should never ever happen and means
                        // there is a pretty major bug in this class.
-                       throw new MWException( 'Encountered end element with no mode' );
+                       throw new RuntimeException( 'Encountered end element with no mode' );
                }
 
                if ( count( $this->curItem ) == 0 && $this->mode[0] !== self::MODE_INITIAL ) {
                        // just to be paranoid. Should always have a curItem, except for initially
                        // (aka during MODE_INITAL).
-                       throw new MWException( "Hit end element </$elm> but no curItem" );
+                       throw new RuntimeException( "Hit end element </$elm> but no curItem" );
                }
 
                switch ( $this->mode[0] ) {
@@ -784,7 +784,7 @@ class XMPReader {
                                if ( $elm === self::NS_RDF . ' Description' ) {
                                        array_shift( $this->mode );
                                } else {
-                                       throw new MWException( 'Element ended unexpectedly while in MODE_INITIAL' );
+                                       throw new RuntimeException( 'Element ended unexpectedly while in MODE_INITIAL' );
                                }
                                break;
                        case self::MODE_LI:
@@ -823,13 +823,13 @@ class XMPReader {
         * this should always be <rdf:Bag>
         *
         * @param string $elm Namespace . ' ' . tag
-        * @throws MWException If we have an element that's not <rdf:Bag>
+        * @throws RuntimeException If we have an element that's not <rdf:Bag>
         */
        private function startElementModeBag( $elm ) {
                if ( $elm === self::NS_RDF . ' Bag' ) {
                        array_unshift( $this->mode, self::MODE_LI );
                } else {
-                       throw new MWException( "Expected <rdf:Bag> but got $elm." );
+                       throw new RuntimeException( "Expected <rdf:Bag> but got $elm." );
                }
        }
 
@@ -838,7 +838,7 @@ class XMPReader {
         * this should always be <rdf:Seq>
         *
         * @param string $elm Namespace . ' ' . tag
-        * @throws MWException If we have an element that's not <rdf:Seq>
+        * @throws RuntimeException If we have an element that's not <rdf:Seq>
         */
        private function startElementModeSeq( $elm ) {
                if ( $elm === self::NS_RDF . ' Seq' ) {
@@ -849,7 +849,7 @@ class XMPReader {
                                . ' it is a Seq, since some buggy software is known to screw this up.' );
                        array_unshift( $this->mode, self::MODE_LI );
                } else {
-                       throw new MWException( "Expected <rdf:Seq> but got $elm." );
+                       throw new RuntimeException( "Expected <rdf:Seq> but got $elm." );
                }
        }
 
@@ -865,13 +865,13 @@ class XMPReader {
         * we don't care about.
         *
         * @param string $elm Namespace . ' ' . tag
-        * @throws MWException If we have an element that's not <rdf:Alt>
+        * @throws RuntimeException If we have an element that's not <rdf:Alt>
         */
        private function startElementModeLang( $elm ) {
                if ( $elm === self::NS_RDF . ' Alt' ) {
                        array_unshift( $this->mode, self::MODE_LI_LANG );
                } else {
-                       throw new MWException( "Expected <rdf:Seq> but got $elm." );
+                       throw new RuntimeException( "Expected <rdf:Seq> but got $elm." );
                }
        }
 
@@ -891,7 +891,7 @@ class XMPReader {
         *
         * @param string $elm Namespace and tag names separated by space.
         * @param array $attribs Attributes of the element.
-        * @throws MWException
+        * @throws RuntimeException
         */
        private function startElementModeSimple( $elm, $attribs ) {
                if ( $elm === self::NS_RDF . ' Description' ) {
@@ -905,7 +905,7 @@ class XMPReader {
                        }
                } elseif ( $elm === self::NS_RDF . ' value' ) {
                        // This should not be here.
-                       throw new MWException( __METHOD__ . ' Encountered <rdf:value> where it was unexpected.' );
+                       throw new RuntimeException( __METHOD__ . ' Encountered <rdf:value> where it was unexpected.' );
                } else {
                        // something else we don't recognize, like a qualifier maybe.
                        wfDebugLog( 'XMP', __METHOD__ .
@@ -950,7 +950,7 @@ class XMPReader {
         * @param string $ns Namespace
         * @param string $tag Tag name (without namespace prefix)
         * @param array $attribs Array of attributes
-        * @throws MWException
+        * @throws RuntimeException
         */
        private function startElementModeInitial( $ns, $tag, $attribs ) {
                if ( $ns !== self::NS_RDF ) {
@@ -980,7 +980,7 @@ class XMPReader {
                                if ( $this->charContent !== false ) {
                                        // Something weird.
                                        // Should not happen in valid XMP.
-                                       throw new MWException( 'tag nested in non-whitespace characters.' );
+                                       throw new RuntimeException( 'tag nested in non-whitespace characters.' );
                                }
                        } else {
                                // This element is not on our list of allowed elements so ignore.
@@ -1012,7 +1012,7 @@ class XMPReader {
         * @param string $ns Namespace
         * @param string $tag Tag name (no ns)
         * @param array $attribs Array of attribs w/ values.
-        * @throws MWException
+        * @throws RuntimeException
         */
        private function startElementModeStruct( $ns, $tag, $attribs ) {
                if ( $ns !== self::NS_RDF ) {
@@ -1023,7 +1023,7 @@ class XMPReader {
                                ) {
                                        // This assumes that we don't have inter-namespace nesting
                                        // which we don't in all the properties we're interested in.
-                                       throw new MWException( " <$tag> appeared nested in <" . $this->ancestorStruct
+                                       throw new RuntimeException( " <$tag> appeared nested in <" . $this->ancestorStruct
                                                . "> where it is not allowed." );
                                }
                                array_unshift( $this->mode, $this->items[$ns][$tag]['mode'] );
@@ -1031,7 +1031,7 @@ class XMPReader {
                                if ( $this->charContent !== false ) {
                                        // Something weird.
                                        // Should not happen in valid XMP.
-                                       throw new MWException( "tag <$tag> nested in non-whitespace characters (" .
+                                       throw new RuntimeException( "tag <$tag> nested in non-whitespace characters (" .
                                                $this->charContent . ")." );
                                }
                        } else {
@@ -1060,17 +1060,17 @@ class XMPReader {
         *
         * @param string $elm Namespace . ' ' . tagname
         * @param array $attribs Attributes. (needed for BAGSTRUCTS)
-        * @throws MWException If gets a tag other than <rdf:li>
+        * @throws RuntimeException If gets a tag other than <rdf:li>
         */
        private function startElementModeLi( $elm, $attribs ) {
                if ( ( $elm ) !== self::NS_RDF . ' li' ) {
-                       throw new MWException( "<rdf:li> expected but got $elm." );
+                       throw new RuntimeException( "<rdf:li> expected but got $elm." );
                }
 
                if ( !isset( $this->mode[1] ) ) {
                        // This should never ever ever happen. Checking for it
                        // to be paranoid.
-                       throw new MWException( 'In mode Li, but no 2xPrevious mode!' );
+                       throw new RuntimeException( 'In mode Li, but no 2xPrevious mode!' );
                }
 
                if ( $this->mode[1] === self::MODE_BAGSTRUCT ) {
@@ -1081,7 +1081,7 @@ class XMPReader {
 
                        if ( !isset( $this->curItem[1] ) ) {
                                // be paranoid.
-                               throw new MWException( 'Can not find parent of BAGSTRUCT.' );
+                               throw new RuntimeException( 'Can not find parent of BAGSTRUCT.' );
                        }
                        list( $curNS, $curTag ) = explode( ' ', $this->curItem[1] );
                        $this->ancestorStruct = isset( $this->items[$curNS][$curTag]['map_name'] )
@@ -1110,16 +1110,16 @@ class XMPReader {
         *
         * @param string $elm Namespace . ' ' . tag
         * @param array $attribs Array of elements (most importantly xml:lang)
-        * @throws MWException If gets a tag other than <rdf:li> or if no xml:lang
+        * @throws RuntimeException If gets a tag other than <rdf:li> or if no xml:lang
         */
        private function startElementModeLiLang( $elm, $attribs ) {
                if ( $elm !== self::NS_RDF . ' li' ) {
-                       throw new MWException( __METHOD__ . " <rdf:li> expected but got $elm." );
+                       throw new RuntimeException( __METHOD__ . " <rdf:li> expected but got $elm." );
                }
                if ( !isset( $attribs[self::NS_XML . ' lang'] )
                        || !preg_match( '/^[-A-Za-z0-9]{2,}$/D', $attribs[self::NS_XML . ' lang'] )
                ) {
-                       throw new MWException( __METHOD__
+                       throw new RuntimeException( __METHOD__
                                . " <rdf:li> did not contain, or has invalid xml:lang attribute in lang alternative" );
                }
 
@@ -1141,7 +1141,7 @@ class XMPReader {
         * @param XMLParser $parser
         * @param string $elm Namespace "<space>" element
         * @param array $attribs Attribute name => value
-        * @throws MWException
+        * @throws RuntimeException
         */
        function startElement( $parser, $elm, $attribs ) {
 
@@ -1178,7 +1178,7 @@ class XMPReader {
 
                if ( count( $this->mode ) === 0 ) {
                        // This should not happen.
-                       throw new MWException( 'Error extracting XMP, '
+                       throw new RuntimeException( 'Error extracting XMP, '
                                . "encountered <$elm> with no mode" );
                }
 
@@ -1215,7 +1215,7 @@ class XMPReader {
                                $this->startElementModeQDesc( $elm );
                                break;
                        default:
-                               throw new MWException( 'StartElement in unknown mode: ' . $this->mode[0] );
+                               throw new RuntimeException( 'StartElement in unknown mode: ' . $this->mode[0] );
                }
        }
 
@@ -1234,7 +1234,7 @@ class XMPReader {
         * @codingStandardsIgnoreEnd
         *
         * @param array $attribs Array attribute=>value
-        * @throws MWException
+        * @throws RuntimeException
         */
        private function doAttribs( $attribs ) {
                // first check for rdf:parseType attribute, as that can change
@@ -1264,7 +1264,7 @@ class XMPReader {
                                }
                        } elseif ( isset( $this->items[$ns][$tag] ) ) {
                                if ( $this->mode[0] === self::MODE_SIMPLE ) {
-                                       throw new MWException( __METHOD__
+                                       throw new RuntimeException( __METHOD__
                                                . " $ns:$tag found as attribute where not allowed" );
                                }
                                $this->saveValue( $ns, $tag, $val );