http://www.mediawiki.org/wiki/Special:Code/MediaWiki/100577

Revision: 100577
Author:   aaron
Date:     2011-10-24 03:08:45 +0000 (Mon, 24 Oct 2011)
Log Message:
-----------
FU r100535:
* Changed custom functions to work via a $thgThumbCallbacks variable
* Added 'checkCache' and 'fillCache' hooks to $thgThumbCallbacks
* Minor regex cleanup to wfExtractThumbParams()
* Re-organized a bit of the wfStreamThumbViaCurl() code

Modified Paths:
--------------
    trunk/phase3/thumb-handler.php
    trunk/phase3/thumb.config.sample

Modified: trunk/phase3/thumb-handler.php
===================================================================
--- trunk/phase3/thumb-handler.php      2011-10-24 02:47:22 UTC (rev 100576)
+++ trunk/phase3/thumb-handler.php      2011-10-24 03:08:45 UTC (rev 100577)
@@ -12,7 +12,7 @@
 require( $configPath );
 
 function wfHandleThumb404() {
-       global $thgThumb404File;
+       global $thgThumbCallbacks, $thgThumb404File;
 
        # lighttpd puts the original request in REQUEST_URI, while
        # sjs sets that to the 404 handler, and puts the original
@@ -24,9 +24,12 @@
                $uri = $_SERVER['REQUEST_URI'];
        }
 
-       # Extract thumb.php params from the URI.
-       if ( function_exists( 'wfCustomExtractThumbParams' ) ) {
-               $params = wfCustomExtractThumbParams( $uri ); // overridden by 
configuration
+       # Extract thumb.php params from the URI...
+       if ( isset( $thgThumbCallbacks['extractParams'] )
+               && is_callable( $thgThumbCallbacks['extractParams'] ) )
+       {
+               # Overridden by configuration
+               $params = call_user_func_array( 
$thgThumbCallbacks['extractParams'], array( $uri ) );
        } else {
                $params = wfExtractThumbParams( $uri ); // basic wiki URL param 
extracting
        }
@@ -40,7 +43,7 @@
        if ( preg_match( '/[\x80-\xff]/', $uri ) ) {
                header( 'HTTP/1.0 400 Bad request' );
                header( 'Content-Type: text/html' );
-               echo "<html><head><title>Bad request</title></head><body>" . 
+               print "<html><head><title>Bad request</title></head><body>" . 
                        "The URI contained bytes with the high bit set, this is 
not allowed." . 
                        "</body></html>";
                return;
@@ -48,12 +51,21 @@
                header( 'HTTP/1.0 404 Not found' );
                header( 'Content-Type: text/html' );
                header( 'X-Debug: filename contains a space' ); // useful for 
debugging
-               echo "<html><head><title>Not found</title></head><body>" . 
+               print "<html><head><title>Not found</title></head><body>" . 
                        "The URL contained spaces, we don't have any thumbnail 
files with spaces." . 
                        "</body></html>";
                return;
        }
 
+       # Check any backend caches for the thumbnail...
+       if ( isset( $thgThumbCallbacks['checkCache'] )
+               && is_callable( $thgThumbCallbacks['checkCache'] ) )
+       {
+               if ( call_user_func_array( $thgThumbCallbacks['checkCache'], 
array( $uri, $params ) ) ) {
+                       return; // file streamed from backend thumb cache
+               }
+       }
+
        wfStreamThumbViaCurl( $params, $uri );
 }
 
@@ -69,11 +81,11 @@
 
        $thumbRegex = '!^(?:' . preg_quote( $thgThumbServer ) . ')?/' .
                preg_quote( $thgThumbFragment ) . '(/archive|/temp|)/' .
-               $thgThumbHashFragment . '([^/]*)/' . 
'(page(\d*)-)*(\d*)px-([^/]*)$!';
+               $thgThumbHashFragment . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!';
 
        # Is this a thumbnail?
        if ( preg_match( $thumbRegex, $uri, $matches ) ) {
-               list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size, 
$fn2 ) = $matches;
+               list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size 
) = $matches;
                $params = array( 'f' => $filename, 'width' => $size );
                if ( $pagenum ) {
                        $params['page'] = $pagenum;
@@ -98,13 +110,13 @@
  * @return void
  */
 function wfStreamThumbViaCurl( array $params, $uri ) {
-       global $thgThumbScriptPath, $thgThumbCurlProxy, $thgThumbCurlTimeout;
+       global $thgThumbCallbacks, $thgThumbScriptPath, $thgThumbCurlProxy, 
$thgThumbCurlTimeout;
 
        if ( !function_exists( 'curl_init' ) ) {
                header( 'HTTP/1.0 404 Not found' );
                header( 'Content-Type: text/html' );
                header( 'X-Debug: cURL is not enabled' ); // useful for 
debugging
-               echo "<html><head><title>Not found</title></head><body>" . 
+               print "<html><head><title>Not found</title></head><body>" . 
                        "cURL is not enabled for PHP on this wiki. Unable to 
send request thumb.php." . 
                        "</body></html>";
                return;
@@ -119,24 +131,22 @@
                } else {
                        $reqURL .= '&';
                }
-               // Note: value is already urlencoded
-               $reqURL .= "$name=$value";
+               $reqURL .= "$name=$value"; // Note: value is already urlencoded
        }
 
-       $ch = curl_init( $reqURL );
-       if ( $thgThumbCurlProxy ) {
-               curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlProxy );
+       # Set relevant HTTP headers...
+       $headers = array();
+       $headers[] = "X-Original-URI: " . str_replace( "\n", '', $uri );
+       if ( isset( $thgThumbCallbacks['curlHeaders'] )
+               && is_callable( $thgThumbCallbacks['curlHeaders'] ) )
+       {
+               # Add on any custom headers (like XFF)
+               call_user_func_array( $thgThumbCallbacks['curlHeaders'], array( 
&$headers ) );
        }
 
-       $headers = array(); // HTTP headers
-       # Set certain headers...
-       $headers[] = "X-Original-URI: " . str_replace( "\n", '', $uri );
-       if ( function_exists( 'wfCustomThumbRequestHeaders' ) ) {
-               wfCustomThumbRequestHeaders( $headers ); // add on any custom 
headers (like XFF)
-       }
        # Pass through some other headers...
-       $passthrough = array( 'If-Modified-Since', 'Referer', 'User-Agent' );
-       foreach ( $passthrough as $headerName ) {
+       $passThrough = array( 'If-Modified-Since', 'Referer', 'User-Agent' );
+       foreach ( $passThrough as $headerName ) {
                $serverVarName = 'HTTP_' . str_replace( '-', '_', strtoupper( 
$headerName ) );
                if ( !empty( $_SERVER[$serverVarName] ) ) {
                        $headers[] = $headerName . ': ' . 
@@ -144,6 +154,11 @@
                }
        }
 
+       $ch = curl_init( $reqURL );
+       if ( $thgThumbCurlProxy ) {
+               curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlProxy );
+       }
+
        curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch, CURLOPT_TIMEOUT, $thgThumbCurlTimeout );
@@ -177,6 +192,13 @@
                # Error message, suppress cache
                header( 'HTTP/1.1 500 Internal server error' );
                header( 'Cache-Control: no-cache' );
+       } else {
+               # OK thumbnail; save to any backend caches...
+               if ( isset( $thgThumbCallbacks['fillCache'] )
+                       && is_callable( $thgThumbCallbacks['fillCache'] ) )
+               {
+                       call_user_func_array( $thgThumbCallbacks['fillCache'], 
array( $uri, $text ) );
+               }
        }
 
        if ( !$contentType ) {

Modified: trunk/phase3/thumb.config.sample
===================================================================
--- trunk/phase3/thumb.config.sample    2011-10-24 02:47:22 UTC (rev 100576)
+++ trunk/phase3/thumb.config.sample    2011-10-24 03:08:45 UTC (rev 100577)
@@ -37,3 +37,6 @@
 
 # File path to a php file the gives a 404 error message
 $thgThumb404File = "404.php";
+
+# Custom functions for overriding aspects of thumb handling
+$thgThumbCallbacks = array();


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to