* 1) Copy this file to thumb.config.php and modify the settings.
* 2) The webserver must be setup to have thumb-handler.php as a 404 handler.
* This can be done in apache by editing .htaccess in the /thumb directory by adding:
- * ErrorDocument 404 /path/to/thumb-handler.php
+ * ErrorDocument 404 /path/to/thumb_handler.php
*/
-# URL name of the server (e.g. "upload.wikipedia.org").
-$thgThumbServer = "http://localhost";
-# URL fragment after the server name to the thumb directory
-$thgThumbFragment = "MW_trunk/images/thumb";
-# URL regex fragment correspond to the directory hashing of thumbnails.
-# This must correspond to $wgLocalFileRepo['hashLevels'].
-$thgThumbHashFragment = '[0-9a-f]/[0-9a-f][0-9a-f]/'; // 2-level directory hashing
+$thgThumbUrlMatch = array(
+ # URL name of the server (e.g. "upload.wikipedia.org").
+ 'server' => 'http://localhost',
+ # URL fragment to the thumb/ directory
+ 'dirFragment' => 'MW_trunk/images/thumb',
+ # URL regex fragment correspond to the directory hashing of thumbnails.
+ # This must correspond to $wgLocalFileRepo['hashLevels'].
+ 'hashFragment' => '[0-9a-f]/[0-9a-f][0-9a-f]/' // 2-level directory hashing
+);
-# The URL to thumb.php, accessible from the web server.
-$thgThumbScriptPath = "http://localhost/MW_trunk/thumb.php";
-
-# Timeout to use for cURL request to thumb.php.
-# Leave it long enough to generate a ulimit timeout in ordinary
-# cases, but short enough to avoid a local PHP timeout.
-$thgThumbCurlTimeout = 53;
-# Optional proxy server to use to access thumb.php
-$thgThumbCurlProxy = null; // proxy to thumb.php
-
-# File path to a php file the gives a 404 error message
-$thgThumb404File = null;
+$thgThumbCurlConfig = array(
+ # Optionally cURL to thumb.php instead of using it directly
+ 'enabled' => false,
+ # The URL to thumb.php, accessible from the web server.
+ 'url' => 'http://localhost/MW_trunk/thumb.php',
+ # Optional proxy server to use to access thumb.php
+ 'proxy' => null,
+ # Timeout to use for cURL request to thumb.php.
+ # Leave it long enough to generate a ulimit timeout in ordinary
+ # cases, but short enough to avoid a local PHP timeout.
+ 'timeout' => 53
+);
# Custom functions for overriding aspects of thumb handling
$thgThumbCallbacks = array();
# Valid web server entry point
define( 'THUMB_HANDLER', true );
-# Load thumb-handler configuration. We don't want to use
-# WebStart.php or the like as it would kill performance.
-$configPath = dirname( __FILE__ ) . "/thumb.config.php";
-if ( !file_exists( $configPath ) ) {
- die( "Thumb-handler.php is not enabled for this wiki.\n" );
-} elseif ( !extension_loaded( 'curl' ) ) {
- die( "cURL is not enabled for PHP on this wiki.\n" ); // sanity
+# Load thumb-handler configuration. Avoids WebStart.php for performance.
+if ( !file_exists( dirname( __FILE__ ) . "/thumb.config.php" ) ) {
+ die( "thumb_handler.php is not enabled for this wiki.\n" );
}
-require( $configPath );
+require( dirname( __FILE__ ) . "/thumb.config.php" );
-wfHandleThumb404Main();
+# Execute thumb.php if not handled via cURL
+if ( wfHandleThumb404Main() === 'wfThumbMain' ) {
+ require( dirname( __FILE__ ) . '/thumb.php' );
+}
function wfHandleThumb404Main() {
- global $thgThumbCallbacks, $thgThumb404File;
+ global $thgThumbCallbacks, $thgThumbCurlConfig;
# lighttpd puts the original request in REQUEST_URI, while
# sjs sets that to the 404 handler, and puts the original
# Show 404 error if this is not a valid thumb request...
if ( !is_array( $params ) ) {
header( 'X-Debug: no regex match' ); // useful for debugging
- if ( $thgThumb404File ) { // overridden by configuration?
- require( $thgThumb404File );
+ if ( isset( $thgThumbCallbacks['error404'] )
+ && is_callable( $thgThumbCallbacks['error404'] ) ) // overridden by configuration?
+ {
+ call_user_func( $thgThumbCallbacks['error404'] );
} else {
wfDisplay404Error(); // standard 404 message
}
}
}
- wfStreamThumbViaCurl( $params, $uri );
+ # Obtain and stream the thumbnail or setup for wfThumbMain() call...
+ if ( $thgThumbCurlConfig['enabled'] ) {
+ wfStreamThumbViaCurl( $params, $uri );
+ return true; // done
+ } else {
+ $_REQUEST = $params; // pass params to thumb.php
+ return 'wfThumbMain';
+ }
}
/**
* @return Array|null associative params array or null
*/
function wfExtractThumbParams( $uri ) {
- global $thgThumbServer, $thgThumbFragment, $thgThumbHashFragment;
+ global $thgThumbUrlMatch;
- $thumbRegex = '!^(?:' . preg_quote( $thgThumbServer ) . ')?/' .
- preg_quote( $thgThumbFragment ) . '(/archive|/temp|)/' .
- $thgThumbHashFragment . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!';
+ $thumbRegex = '!^(?:' . preg_quote( $thgThumbUrlMatch['server'] ) . ')?/' .
+ preg_quote( $thgThumbUrlMatch['dirFragment'] ) . '(/archive|/temp|)/' .
+ $thgThumbUrlMatch['hashFragment'] . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!';
if ( preg_match( $thumbRegex, $uri, $matches ) ) {
list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size ) = $matches;
* @return void
*/
function wfStreamThumbViaCurl( array $params, $uri ) {
- global $thgThumbCallbacks, $thgThumbScriptPath, $thgThumbCurlProxy, $thgThumbCurlTimeout;
+ global $thgThumbCallbacks, $thgThumbCurlConfig;
+
+ if ( !extension_loaded( 'curl' ) ) {
+ die( "cURL is not enabled for PHP on this wiki.\n" ); // sanity
+ }
# Build up the request URL to use with CURL...
- $reqURL = "{$thgThumbScriptPath}?";
+ $reqURL = $thgThumbCurlConfig['url'] . '?';
$first = true;
foreach ( $params as $name => $value ) {
if ( $first ) {
}
$ch = curl_init( $reqURL );
- if ( $thgThumbCurlProxy ) {
- curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlProxy );
+ if ( $thgThumbCurlConfig['proxy'] ) {
+ curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlConfig['proxy'] );
}
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
- curl_setopt( $ch, CURLOPT_TIMEOUT, $thgThumbCurlTimeout );
+ curl_setopt( $ch, CURLOPT_TIMEOUT, $thgThumbCurlConfig['timeout'] );
# Actually make the request
$text = curl_exec( $ch );