(bug 8652) Catch exceptions generated by malformed XML in multipage media. Note...
authorAryeh Gregor <simetrical@users.mediawiki.org>
Thu, 25 Jan 2007 03:07:39 +0000 (03:07 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Thu, 25 Jan 2007 03:07:39 +0000 (03:07 +0000)
Incidentally, should we maybe use set_exception_handler()?

RELEASE-NOTES
includes/ImagePage.php
languages/messages/MessagesEn.php

index 1024128..075873f 100644 (file)
@@ -146,6 +146,7 @@ lighter making things easier to read.
   alias to that which should be used to make it more self documentating.
 * (bug 8749) Bring MySQL 5 table defs back into sync
 * (bug 8751) Set session cookies to HTTPS-only to match other cookies
+* (bug 8652) Catch exceptions generated by malformed XML in multipage media
 
 
 == Languages updated ==
index 2e6e94e..f57e9d7 100644 (file)
@@ -248,39 +248,53 @@ class ImagePage extends Article {
                                     htmlspecialchars( $this->img->getTitle()->getPrefixedText() ).'" />' . $anchorclose . '</div>' );
 
                                if ( $this->img->isMultipage() ) {
-                                       $count = $this->img->pageCount();
+                                       try {
+                                               // Apparently SimpleXmlElement->__construct() can throw
+                                               // exceptions for malformed XML, although this isn't documented . . .
+                                               // http://us2.php.net/manual/en/function.simplexml-element-construct.php
 
-                                       if ( $page > 1 ) {
-                                               $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
-                                               $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
-                                               $this->img->selectPage( $page - 1 );
-                                               $thumb1 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none' );
-                                       } else {
-                                               $thumb1 = '';
-                                       }
-
-                                       if ( $page < $count ) {
-                                               $label = wfMsg( 'imgmultipagenext' );
-                                               $this->img->selectPage( $page + 1 );
-                                               $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
-                                               $thumb2 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none' );
-                                       } else {
-                                               $thumb2 = '';
-                                       }
-
-                                       $select = '<form name="pageselector" action="' . $this->img->getEscapeLocalUrl( '' ) . '" method="GET" onchange="document.pageselector.submit();">' ;
-                                       $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
-                                               ' <select id="pageselector" name="page">';
-                                       for ( $i=1; $i <= $count; $i++ ) {
-                                               $select .= Xml::option( $wgLang->formatNum( $i ), $i,
-                                                       $i == $page );
+                                               $count = $this->img->pageCount();
+       
+                                               if ( $page > 1 ) {
+                                                       $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
+                                                       $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page-1) );
+                                                       $this->img->selectPage( $page - 1 );
+                                                       $thumb1 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none' );
+                                               } else {
+                                                       $thumb1 = '';
+                                               }
+       
+                                               if ( $page < $count ) {
+                                                       $label = wfMsg( 'imgmultipagenext' );
+                                                       $this->img->selectPage( $page + 1 );
+                                                       $link = $sk->makeLinkObj( $this->mTitle, $label, 'page='. ($page+1) );
+                                                       $thumb2 = $sk->makeThumbLinkObj( $this->img, $link, $label, 'none' );
+                                               } else {
+                                                       $thumb2 = '';
+                                               }
+       
+                                               $select = '<form name="pageselector" action="' . $this->img->getEscapeLocalUrl( '' ) . '" method="GET" onchange="document.pageselector.submit();">' ;
+                                               $select .= $wgOut->parse( wfMsg( 'imgmultigotopre' ), false ) .
+                                                       ' <select id="pageselector" name="page">';
+                                               for ( $i=1; $i <= $count; $i++ ) {
+                                                       $select .= Xml::option( $wgLang->formatNum( $i ), $i,
+                                                               $i == $page );
+                                               }
+                                               $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
+                                                       '<input type="submit" value="' .
+                                                       htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
+       
+                                               $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
+                                                  "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
+                                       } catch( Exception $e ) {
+                                               if ( $e->getText() == "String could not be parsed as XML" ) {
+                                                       // Hacky and fragile string test, yay
+                                                       $errorMsg = wfMsg( 'imgmultiparseerror' );
+                                                       $wgOut->addHtml( "</td><td>$errorMsg</td></tr></table>" );
+                                               } else {
+                                                       throw $e;
+                                               }
                                        }
-                                       $select .= '</select>' . $wgOut->parse( wfMsg( 'imgmultigotopost' ), false ) .
-                                               '<input type="submit" value="' .
-                                               htmlspecialchars( wfMsg( 'imgmultigo' ) ) . '"></form>';
-
-                                       $wgOut->addHTML( '</td><td><div class="multipageimagenavbox">' .
-                                          "$select<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>" );
                                }
                        } else {
                                #if direct link is allowed but it's not a renderable image, show an icon.
index 96dead0..45e759f 100644 (file)
@@ -2722,6 +2722,7 @@ Please confirm that really want to recreate this page.',
 'imgmultigo' => 'Go!',
 'imgmultigotopre' => 'Go to page',
 'imgmultigotopost' => '',
+'imgmultiparseerror' => 'The image file appears to be corrupted or incorrect, so {{SITENAME}} cannot retrieve a list of pages.',
 
 # Table pager
 'ascending_abbrev' => 'asc',