r100577 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100576‎ | r100577 | r100578 >
Date:03:08, 24 October 2011
Author:aaron
Status:resolved (Comments)
Tags:
Comment:
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 (modified) (history)
  • /trunk/phase3/thumb.config.sample (modified) (history)

Diff [purge]

Index: trunk/phase3/thumb.config.sample
@@ -37,3 +37,6 @@
3838
3939 # File path to a php file the gives a 404 error message
4040 $thgThumb404File = "404.php";
 41+
 42+# Custom functions for overriding aspects of thumb handling
 43+$thgThumbCallbacks = array();
Index: trunk/phase3/thumb-handler.php
@@ -12,7 +12,7 @@
1313 require( $configPath );
1414
1515 function wfHandleThumb404() {
16 - global $thgThumb404File;
 16+ global $thgThumbCallbacks, $thgThumb404File;
1717
1818 # lighttpd puts the original request in REQUEST_URI, while
1919 # sjs sets that to the 404 handler, and puts the original
@@ -24,9 +24,12 @@
2525 $uri = $_SERVER['REQUEST_URI'];
2626 }
2727
28 - # Extract thumb.php params from the URI.
29 - if ( function_exists( 'wfCustomExtractThumbParams' ) ) {
30 - $params = wfCustomExtractThumbParams( $uri ); // overridden by configuration
 28+ # Extract thumb.php params from the URI...
 29+ if ( isset( $thgThumbCallbacks['extractParams'] )
 30+ && is_callable( $thgThumbCallbacks['extractParams'] ) )
 31+ {
 32+ # Overridden by configuration
 33+ $params = call_user_func_array( $thgThumbCallbacks['extractParams'], array( $uri ) );
3134 } else {
3235 $params = wfExtractThumbParams( $uri ); // basic wiki URL param extracting
3336 }
@@ -40,7 +43,7 @@
4144 if ( preg_match( '/[\x80-\xff]/', $uri ) ) {
4245 header( 'HTTP/1.0 400 Bad request' );
4346 header( 'Content-Type: text/html' );
44 - echo "<html><head><title>Bad request</title></head><body>" .
 47+ print "<html><head><title>Bad request</title></head><body>" .
4548 "The URI contained bytes with the high bit set, this is not allowed." .
4649 "</body></html>";
4750 return;
@@ -48,12 +51,21 @@
4952 header( 'HTTP/1.0 404 Not found' );
5053 header( 'Content-Type: text/html' );
5154 header( 'X-Debug: filename contains a space' ); // useful for debugging
52 - echo "<html><head><title>Not found</title></head><body>" .
 55+ print "<html><head><title>Not found</title></head><body>" .
5356 "The URL contained spaces, we don't have any thumbnail files with spaces." .
5457 "</body></html>";
5558 return;
5659 }
5760
 61+ # Check any backend caches for the thumbnail...
 62+ if ( isset( $thgThumbCallbacks['checkCache'] )
 63+ && is_callable( $thgThumbCallbacks['checkCache'] ) )
 64+ {
 65+ if ( call_user_func_array( $thgThumbCallbacks['checkCache'], array( $uri, $params ) ) ) {
 66+ return; // file streamed from backend thumb cache
 67+ }
 68+ }
 69+
5870 wfStreamThumbViaCurl( $params, $uri );
5971 }
6072
@@ -69,11 +81,11 @@
7082
7183 $thumbRegex = '!^(?:' . preg_quote( $thgThumbServer ) . ')?/' .
7284 preg_quote( $thgThumbFragment ) . '(/archive|/temp|)/' .
73 - $thgThumbHashFragment . '([^/]*)/' . '(page(\d*)-)*(\d*)px-([^/]*)$!';
 85+ $thgThumbHashFragment . '([^/]*)/(page(\d*)-)*(\d*)px-[^/]*$!';
7486
7587 # Is this a thumbnail?
7688 if ( preg_match( $thumbRegex, $uri, $matches ) ) {
77 - list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size, $fn2 ) = $matches;
 89+ list( $all, $archOrTemp, $filename, $pagefull, $pagenum, $size ) = $matches;
7890 $params = array( 'f' => $filename, 'width' => $size );
7991 if ( $pagenum ) {
8092 $params['page'] = $pagenum;
@@ -98,13 +110,13 @@
99111 * @return void
100112 */
101113 function wfStreamThumbViaCurl( array $params, $uri ) {
102 - global $thgThumbScriptPath, $thgThumbCurlProxy, $thgThumbCurlTimeout;
 114+ global $thgThumbCallbacks, $thgThumbScriptPath, $thgThumbCurlProxy, $thgThumbCurlTimeout;
103115
104116 if ( !function_exists( 'curl_init' ) ) {
105117 header( 'HTTP/1.0 404 Not found' );
106118 header( 'Content-Type: text/html' );
107119 header( 'X-Debug: cURL is not enabled' ); // useful for debugging
108 - echo "<html><head><title>Not found</title></head><body>" .
 120+ print "<html><head><title>Not found</title></head><body>" .
109121 "cURL is not enabled for PHP on this wiki. Unable to send request thumb.php." .
110122 "</body></html>";
111123 return;
@@ -119,24 +131,22 @@
120132 } else {
121133 $reqURL .= '&';
122134 }
123 - // Note: value is already urlencoded
124 - $reqURL .= "$name=$value";
 135+ $reqURL .= "$name=$value"; // Note: value is already urlencoded
125136 }
126137
127 - $ch = curl_init( $reqURL );
128 - if ( $thgThumbCurlProxy ) {
129 - curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlProxy );
 138+ # Set relevant HTTP headers...
 139+ $headers = array();
 140+ $headers[] = "X-Original-URI: " . str_replace( "\n", '', $uri );
 141+ if ( isset( $thgThumbCallbacks['curlHeaders'] )
 142+ && is_callable( $thgThumbCallbacks['curlHeaders'] ) )
 143+ {
 144+ # Add on any custom headers (like XFF)
 145+ call_user_func_array( $thgThumbCallbacks['curlHeaders'], array( &$headers ) );
130146 }
131147
132 - $headers = array(); // HTTP headers
133 - # Set certain headers...
134 - $headers[] = "X-Original-URI: " . str_replace( "\n", '', $uri );
135 - if ( function_exists( 'wfCustomThumbRequestHeaders' ) ) {
136 - wfCustomThumbRequestHeaders( $headers ); // add on any custom headers (like XFF)
137 - }
138148 # Pass through some other headers...
139 - $passthrough = array( 'If-Modified-Since', 'Referer', 'User-Agent' );
140 - foreach ( $passthrough as $headerName ) {
 149+ $passThrough = array( 'If-Modified-Since', 'Referer', 'User-Agent' );
 150+ foreach ( $passThrough as $headerName ) {
141151 $serverVarName = 'HTTP_' . str_replace( '-', '_', strtoupper( $headerName ) );
142152 if ( !empty( $_SERVER[$serverVarName] ) ) {
143153 $headers[] = $headerName . ': ' .
@@ -144,6 +154,11 @@
145155 }
146156 }
147157
 158+ $ch = curl_init( $reqURL );
 159+ if ( $thgThumbCurlProxy ) {
 160+ curl_setopt( $ch, CURLOPT_PROXY, $thgThumbCurlProxy );
 161+ }
 162+
148163 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
149164 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
150165 curl_setopt( $ch, CURLOPT_TIMEOUT, $thgThumbCurlTimeout );
@@ -177,6 +192,13 @@
178193 # Error message, suppress cache
179194 header( 'HTTP/1.1 500 Internal server error' );
180195 header( 'Cache-Control: no-cache' );
 196+ } else {
 197+ # OK thumbnail; save to any backend caches...
 198+ if ( isset( $thgThumbCallbacks['fillCache'] )
 199+ && is_callable( $thgThumbCallbacks['fillCache'] ) )
 200+ {
 201+ call_user_func_array( $thgThumbCallbacks['fillCache'], array( $uri, $text ) );
 202+ }
181203 }
182204
183205 if ( !$contentType ) {

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)   09:13, 22 December 2011

This was basically totally redone in r105512.

Status & tagging log