r103742 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103741‎ | r103742 | r103743 >
Date:10:24, 20 November 2011
Author:aaron
Status:ok
Tags:
Comment:
StreamFile::StreamFile() tweaks:
* Replaced unused $request param with $sendErrors to suppress sending error messages
* Added boolean return value (success/failure)
Modified paths:
  • /trunk/phase3/includes/StreamFile.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/StreamFile.php
@@ -9,24 +9,30 @@
1010 * Stream a file to the browser, adding all the headings and fun stuff
1111 * @param $fname string Full name and path of the file to stream
1212 * @param $headers array Any additional headers to send
 13+ * @param $sendErrors bool Send error messages if errors occur (like 404)
 14+ * @return bool Success
1315 */
14 - public static function stream( $fname, $headers = array(), $request = null ) {
 16+ public static function stream( $fname, $headers = array(), $sendErrors = true ) {
 17+ global $wgLanguageCode;
 18+
1519 wfSuppressWarnings();
1620 $stat = stat( $fname );
1721 wfRestoreWarnings();
1822 if ( !$stat ) {
19 - header( 'HTTP/1.0 404 Not Found' );
20 - header( 'Cache-Control: no-cache' );
21 - header( 'Content-Type: text/html; charset=utf-8' );
22 - $encFile = htmlspecialchars( $fname );
23 - $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] );
24 - echo "<html><body>
25 - <h1>File not found</h1>
26 - <p>Although this PHP script ($encScript) exists, the file requested for output
27 - ($encFile) does not.</p>
28 - </body></html>
29 - ";
30 - return;
 23+ if ( $sendErrors ) {
 24+ header( 'HTTP/1.0 404 Not Found' );
 25+ header( 'Cache-Control: no-cache' );
 26+ header( 'Content-Type: text/html; charset=utf-8' );
 27+ $encFile = htmlspecialchars( $fname );
 28+ $encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] );
 29+ echo "<html><body>
 30+ <h1>File not found</h1>
 31+ <p>Although this PHP script ($encScript) exists, the file requested for output
 32+ ($encFile) does not.</p>
 33+ </body></html>
 34+ ";
 35+ }
 36+ return false;
3137 }
3238
3339 header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $stat['mtime'] ) . ' GMT' );
@@ -44,29 +50,31 @@
4551 // Don't stream it out as text/html if there was a PHP error
4652 if ( headers_sent() ) {
4753 echo "Headers already sent, terminating.\n";
48 - return;
 54+ return false;
4955 }
5056
51 - global $wgLanguageCode;
52 - header( "Content-Disposition: inline;filename*=utf-8'$wgLanguageCode'" . urlencode( basename( $fname ) ) );
 57+ header( "Content-Disposition: inline;filename*=utf-8'$wgLanguageCode'" .
 58+ urlencode( basename( $fname ) ) );
5359
 60+ // Send additional headers
5461 foreach ( $headers as $header ) {
5562 header( $header );
5663 }
5764
 65+ // Don't send if client has up to date cache
5866 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
5967 $modsince = preg_replace( '/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE'] );
6068 $sinceTime = strtotime( $modsince );
6169 if ( $stat['mtime'] <= $sinceTime ) {
6270 ini_set( 'zlib.output_compression', 0 );
6371 header( "HTTP/1.0 304 Not Modified" );
64 - return;
 72+ return true; // ok
6573 }
6674 }
6775
6876 header( 'Content-Length: ' . $stat['size'] );
6977
70 - readfile( $fname );
 78+ return readfile( $fname );
7179 }
7280
7381 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r103743MFT r103742aaron10:27, 20 November 2011

Status & tagging log