API: documentation and cleanup.
[lhc/web/wiklou.git] / thumb.php
index c67468b..b92a6f7 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -41,19 +41,26 @@ unset( $params['r'] );
 $fileName = strtr( $fileName, '\\/', '__' );
 
 // Work out paths, carefully avoiding constructing an Image object because that won't work yet
-$handler = thumbGetHandler( $fileName );
-if ( $handler ) {
-       $imagePath = wfImageDir( $fileName ) . '/' . $fileName;
-       $thumbName = $handler->makeParamString( $params ) . "-$fileName";
-       $thumbPath = wfImageThumbDir( $fileName ) . '/' . $thumbName;
-
-       if ( is_file( $thumbPath ) && filemtime( $thumbPath ) >= filemtime( $imagePath ) ) {
-               wfStreamFile( $thumbPath );
-               // Can't log profiling data with no Setup.php
-               exit;
+try {
+       $handler = thumbGetHandler( $fileName );
+       if ( $handler ) {
+               $imagePath = wfImageDir( $fileName ) . '/' . $fileName;
+               $thumbName = $handler->makeParamString( $params ) . "-$fileName";
+               $thumbPath = wfImageThumbDir( $fileName ) . '/' . $thumbName;
+
+               if ( is_file( $thumbPath ) && filemtime( $thumbPath ) >= filemtime( $imagePath ) ) {
+                       wfStreamFile( $thumbPath );
+                       // Can't log profiling data with no Setup.php
+                       exit;
+               }
        }
+} catch ( MWException $e ) {
+       require_once( './includes/Setup.php' );
+       thumbInternalError( $e->getHTML() );
+       exit;
 }
 
+
 // OK, no valid thumbnail, time to get out the heavy machinery
 wfProfileOut( 'thumb.php-start' );
 require_once( './includes/Setup.php' );
@@ -71,27 +78,19 @@ try {
        $thumb = false;
 }
 
-if ( $thumb && $thumb->getPath() ) {
+if ( $thumb && $thumb->getPath() && file_exists( $thumb->getPath() ) ) {
        wfStreamFile( $thumb->getPath() );
 } elseif ( $img ) {
-       header( 'Cache-Control: no-cache' );
-       header( 'Content-Type: text/html; charset=utf-8' );
-       header( 'HTTP/1.1 500 Internal server error' );
        if ( !$thumb ) {
                $msg = wfMsgHtml( 'thumbnail_error', 'Image::transform() returned false' );
        } elseif ( $thumb->isError() ) {
-               $msg = $thumb->toHtml();
-       } else {
+               $msg = $thumb->getHtmlMsg();
+       } elseif ( !$thumb->getPath() ) {
                $msg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
+       } else {
+               $msg = wfMsgHtml( 'thumbnail_error', 'Output file missing' );
        }
-       echo <<<EOT
-<html><head><title>Error generating thumbnail</title></head>
-<body>
-$msg
-</body>
-</html>
-
-EOT;
+       thumbInternalError( $msg );
 } else {
        $badtitle = wfMsg( 'badtitle' );
        $badtitletext = wfMsg( 'badtitletext' );
@@ -124,4 +123,21 @@ function thumbGetHandler( $fileName ) {
        return MediaHandler::getHandler( $mime );
 }
 
+function thumbInternalError( $msg ) {
+       header( 'Cache-Control: no-cache' );
+       header( 'Content-Type: text/html; charset=utf-8' );
+       header( 'HTTP/1.1 500 Internal server error' );
+       echo <<<EOT
+<html><head><title>Error generating thumbnail</title></head>
+<body>
+<h1>Error generating thumbnail</h1>
+<p>
+$msg
+</p>
+</body>
+</html>
+
+EOT;
+}
+
 ?>