r103678 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103677‎ | r103678 | r103679 >
Date:14:14, 19 November 2011
Author:dantman
Status:ok (Comments)
Tags:
Comment:
Incorporate a router into our maintenance/dev/ code to better handle the variety of file types we support.
Modified paths:
  • /trunk/phase3/maintenance/dev/router.php (added) (history)
  • /trunk/phase3/maintenance/dev/start.sh (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/dev/router.php
@@ -0,0 +1,52 @@
 2+<?php
 3+
 4+if ( isset( $_SERVER["SCRIPT_FILENAME"] ) ) {
 5+ $file = $_SERVER["SCRIPT_FILENAME"];
 6+ if ( !is_readable( $file ) ) {
 7+ // Let the server throw the error
 8+ return false;
 9+ }
 10+ $ext = pathinfo( $file, PATHINFO_EXTENSION );
 11+ if ( $ext == 'php' ) {
 12+ # Let it execute php files
 13+ return false;
 14+ }
 15+ $mime = false;
 16+ $lines = explode( "\n", file_get_contents( "includes/mime.types" ) );
 17+ foreach ( $lines as $line ) {
 18+ $exts = explode( " ", $line );
 19+ $mime = array_shift( $exts );
 20+ if ( in_array( $ext, $exts ) ) {
 21+ break; # this is the right value for $mime
 22+ }
 23+ $mime = false;
 24+ }
 25+ if ( !$mime ) {
 26+ $basename = basename( $file );
 27+ if ( $basename == strtoupper( $basename ) ) {
 28+ # IF it's something like README serve it as text
 29+ $mime = "text/plain";
 30+ }
 31+ }
 32+ if ( $mime ) {
 33+ # Use custom handling to serve files with a known mime type
 34+ # This way we can serve things like .svg files that the built-in
 35+ # PHP webserver doesn't understand.
 36+ # ;) Nicely enough we just happen to bundle a mime.types file
 37+ $f = fopen($file, 'rb');
 38+ if ( preg_match( '^text/', $mime ) ) {
 39+ # Text should have a charset=UTF-8 (php's webserver does this too)
 40+ header("Content-Type: $mime; charset=UTF-8");
 41+ } else {
 42+ header("Content-Type: $mime");
 43+ }
 44+ header("Content-Length: " . filesize($file));
 45+ // Stream that out to the browser
 46+ fpassthru($f);
 47+ return true;
 48+ }
 49+}
 50+
 51+# Let the php server handle things on it's own otherwise
 52+return false;
 53+
Index: trunk/phase3/maintenance/dev/start.sh
@@ -18,4 +18,4 @@
1919 echo ""
2020
2121 cd "$DEV/../../"; # $IP
22 -"$PHP" -S "localhost:$PORT"
 22+"$PHP" -S "localhost:$PORT" "$DEV/router.php"

Sign-offs

UserFlagDate
Hasharinspected12:24, 25 November 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r105092Followup r103678; Add a comment.dantman23:19, 3 December 2011

Comments

#Comment by Hashar (talk | contribs)   12:24, 25 November 2011

Can you please add a comment about what this file is meant for? What are the expected inputs and what happens on return true or return false?

Thanks!

#Comment by Dantman (talk | contribs)   14:11, 25 November 2011

You want me to write php documentation in MediaWiki?

http://ca2.php.net/manual/en/features.commandline.webserver.php

#Comment by Bryan (talk | contribs)   09:46, 26 November 2011

There could at least be something like "# This is the entry point for the PHP command line webserver, see <link>" at the top of the file.

Status & tagging log