r101117 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101116‎ | r101117 | r101118 >
Date:05:12, 28 October 2011
Author:aaron
Status:resolved (Comments)
Tags:
Comment:
FU r100535:
* Thumb handler can now also work without cURL
* Combined related config vars into array config vars
* Folded $thgThumb404File into $thgThumbCallbacks
* Avoided some global pollution
Modified paths:
  • /trunk/phase3/thumb.config.sample (modified) (history)
  • /trunk/phase3/thumb.php (modified) (history)
  • /trunk/phase3/thumb_handler.php (modified) (history)

Diff [purge]

Index: trunk/phase3/thumb_handler.php
@@ -3,20 +3,19 @@
44 # Valid web server entry point
55 define( 'THUMB_HANDLER', true );
66
7 -# Load thumb-handler configuration. We don't want to use
8 -# WebStart.php or the like as it would kill performance.
9 -$configPath = dirname( __FILE__ ) . "/thumb.config.php";
10 -if ( !file_exists( $configPath ) ) {
11 - die( "Thumb-handler.php is not enabled for this wiki.\n" );
12 -} elseif ( !extension_loaded( 'curl' ) ) {
13 - die( "cURL is not enabled for PHP on this wiki.\n" ); // sanity
 7+# Load thumb-handler configuration. Avoids WebStart.php for performance.
 8+if ( !file_exists( dirname( __FILE__ ) . "/thumb.config.php" ) ) {
 9+ die( "thumb_handler.php is not enabled for this wiki.\n" );
1410 }
15 -require( $configPath );
 11+require( dirname( __FILE__ ) . "/thumb.config.php" );
1612
17 -wfHandleThumb404Main();
 13+# Execute thumb.php if not handled via cURL
 14+if ( wfHandleThumb404Main() === 'wfThumbMain' ) {
 15+ require( dirname( __FILE__ ) . '/thumb.php' );
 16+}
1817
1918 function wfHandleThumb404Main() {
20 - global $thgThumbCallbacks, $thgThumb404File;
 19+ global $thgThumbCallbacks, $thgThumbCurlConfig;
2120
2221 # lighttpd puts the original request in REQUEST_URI, while
2322 # sjs sets that to the 404 handler, and puts the original
@@ -40,8 +39,10 @@
4140 # Show 404 error if this is not a valid thumb request...
4241 if ( !is_array( $params ) ) {
4342 header( 'X-Debug: no regex match' ); // useful for debugging
44 - if ( $thgThumb404File ) { // overridden by configuration?
45 - require( $thgThumb404File );
 43+ if ( isset( $thgThumbCallbacks['error404'] )
 44+ && is_callable( $thgThumbCallbacks['error404'] ) ) // overridden by configuration?
 45+ {
 46+ call_user_func( $thgThumbCallbacks['error404'] );
4647 } else {
4748 wfDisplay404Error(); // standard 404 message
4849 }
@@ -57,7 +58,14 @@
5859 }
5960 }
6061
61 - wfStreamThumbViaCurl( $params, $uri );
 62+ # Obtain and stream the thumbnail or setup for wfThumbMain() call...
 63+ if ( $thgThumbCurlConfig['enabled'] ) {
 64+ wfStreamThumbViaCurl( $params, $uri );
 65+ return true; // done
 66+ } else {
 67+ $_REQUEST = $params; // pass params to thumb.php
 68+ return 'wfThumbMain';
 69+ }
6270 }
6371
6472 /**
@@ -68,11 +76,11 @@
6977 * @return Array|null associative params array or null
7078 */
7179 function wfExtractThumbParams( $uri ) {
72 - global $thgThumbServer, $thgThumbFragment, $thgThumbHashFragment;
 80+ global $thgThumbUrlMatch;
7381
74 - $thumbRegex = '!^(?:' . preg_quote( $thgThumbServer ) . ')?/' .
75 - preg_quote( $thgThumbFragment ) . '(/archive|/temp|)/' .
76 - $thgThumbHashFragment . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!';
 82+ $thumbRegex = '!^(?:' . preg_quote( $thgThumbUrlMatch['server'] ) . ')?/' .
 83+ preg_quote( $thgThumbUrlMatch['dirFragment'] ) . '(/archive|/temp|)/' .
 84+ $thgThumbUrlMatch['hashFragment'] . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!';
7785
7886 if ( preg_match( $thumbRegex, $uri, $matches ) ) {
7987 list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size ) = $matches;
@@ -100,10 +108,14 @@
101109 * @return void
102110 */
103111 function wfStreamThumbViaCurl( array $params, $uri ) {
104 - global $thgThumbCallbacks, $thgThumbScriptPath, $thgThumbCurlProxy, $thgThumbCurlTimeout;
 112+ global $thgThumbCallbacks, $thgThumbCurlConfig;
105113
 114+ if ( !extension_loaded( 'curl' ) ) {
 115+ die( "cURL is not enabled for PHP on this wiki.\n" ); // sanity
 116+ }
 117+
106118 # Build up the request URL to use with CURL...
107 - $reqURL = "{$thgThumbScriptPath}?";
 119+ $reqURL = $thgThumbCurlConfig['url'] . '?';
108120 $first = true;
109121 foreach ( $params as $name => $value ) {
110122 if ( $first ) {
@@ -135,13 +147,13 @@
136148 }
137149
138150 $ch = curl_init( $reqURL );
139 - if ( $thgThumbCurlProxy ) {
140 - curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlProxy );
 151+ if ( $thgThumbCurlConfig['proxy'] ) {
 152+ curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlConfig['proxy'] );
141153 }
142154
143155 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
144156 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
145 - curl_setopt( $ch, CURLOPT_TIMEOUT, $thgThumbCurlTimeout );
 157+ curl_setopt( $ch, CURLOPT_TIMEOUT, $thgThumbCurlConfig['timeout'] );
146158
147159 # Actually make the request
148160 $text = curl_exec( $ch );
Index: trunk/phase3/thumb.config.sample
@@ -14,29 +14,31 @@
1515 * 1) Copy this file to thumb.config.php and modify the settings.
1616 * 2) The webserver must be setup to have thumb-handler.php as a 404 handler.
1717 * This can be done in apache by editing .htaccess in the /thumb directory by adding:
18 - * ErrorDocument 404 /path/to/thumb-handler.php
 18+ * ErrorDocument 404 /path/to/thumb_handler.php
1919 */
2020
21 -# URL name of the server (e.g. "upload.wikipedia.org").
22 -$thgThumbServer = "http://localhost";
23 -# URL fragment after the server name to the thumb directory
24 -$thgThumbFragment = "MW_trunk/images/thumb";
25 -# URL regex fragment correspond to the directory hashing of thumbnails.
26 -# This must correspond to $wgLocalFileRepo['hashLevels'].
27 -$thgThumbHashFragment = '[0-9a-f]/[0-9a-f][0-9a-f]/'; // 2-level directory hashing
 21+$thgThumbUrlMatch = array(
 22+ # URL name of the server (e.g. "upload.wikipedia.org").
 23+ 'server' => 'http://localhost',
 24+ # URL fragment to the thumb/ directory
 25+ 'dirFragment' => 'MW_trunk/images/thumb',
 26+ # URL regex fragment correspond to the directory hashing of thumbnails.
 27+ # This must correspond to $wgLocalFileRepo['hashLevels'].
 28+ 'hashFragment' => '[0-9a-f]/[0-9a-f][0-9a-f]/' // 2-level directory hashing
 29+);
2830
29 -# The URL to thumb.php, accessible from the web server.
30 -$thgThumbScriptPath = "http://localhost/MW_trunk/thumb.php";
 31+$thgThumbCurlConfig = array(
 32+ # Optionally cURL to thumb.php instead of using it directly
 33+ 'enabled' => false,
 34+ # The URL to thumb.php, accessible from the web server.
 35+ 'url' => 'http://localhost/MW_trunk/thumb.php',
 36+ # Optional proxy server to use to access thumb.php
 37+ 'proxy' => null,
 38+ # Timeout to use for cURL request to thumb.php.
 39+ # Leave it long enough to generate a ulimit timeout in ordinary
 40+ # cases, but short enough to avoid a local PHP timeout.
 41+ 'timeout' => 53
 42+);
3143
32 -# Timeout to use for cURL request to thumb.php.
33 -# Leave it long enough to generate a ulimit timeout in ordinary
34 -# cases, but short enough to avoid a local PHP timeout.
35 -$thgThumbCurlTimeout = 53;
36 -# Optional proxy server to use to access thumb.php
37 -$thgThumbCurlProxy = null; // proxy to thumb.php
38 -
39 -# File path to a php file the gives a 404 error message
40 -$thgThumb404File = null;
41 -
4244 # Custom functions for overriding aspects of thumb handling
4345 $thgThumbCallbacks = array();
Index: trunk/phase3/thumb.php
@@ -26,10 +26,12 @@
2727 $headers = array();
2828
2929 // Get input parameters
30 - if ( get_magic_quotes_gpc() ) {
31 - $params = array_map( 'stripslashes', $_REQUEST );
 30+ if ( defined( 'THUMB_HANDLER' ) ) {
 31+ $params = $_REQUEST; // called from thumb_handler.php
3232 } else {
33 - $params = $_REQUEST;
 33+ $params = get_magic_quotes_gpc()
 34+ ? array_map( 'stripslashes', $_REQUEST )
 35+ : $_REQUEST;
3436 }
3537
3638 $fileName = isset( $params['f'] ) ? $params['f'] : '';

Follow-up revisions

RevisionCommit summaryAuthorDate
r101181FU r101117:...aaron19:50, 28 October 2011
r105512FU r101117: removed cURL thumb handler code and made thumb_handler.php a thin...aaron03:43, 8 December 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r100535Added a basic thumb-handler.php file, configured via thumb.config.php. The co...aaron09:36, 23 October 2011

Comments

#Comment by Aaron Schulz (talk | contribs)   05:01, 8 December 2011

This was basically totally redone in r105512.

Status & tagging log