MediaWiki REL1_35
router.php
Go to the documentation of this file.
1<?php
24if ( PHP_SAPI != 'cli-server' ) {
25 die( "This script can only be run by php's cli-server sapi." );
26}
27
28if ( !isset( $_SERVER['SCRIPT_FILENAME'] ) ) {
29 // Let built-in server handle error.
30 return false;
31}
32
33// The SCRIPT_FILENAME can be one of three things:
34// 1. Absolute path to a file in the docroot associated with the
35// path of the current request URL. PHP does this for any file path
36// where it finds a matching file on disk. For both PHP files, and for
37// static files.
38// 2. Relative path to router.php (this file), for any unknown URL path
39// that ends in ".php" or another extension that PHP would execute.
40// 3. Absolute path to {docroot}/index.php, for any other unknown path.
41// Effectively treating it as a 404 handler.
42$file = $_SERVER['SCRIPT_FILENAME'];
43if ( !is_readable( $file ) ) {
44 // Let built-in server handle error.
45 return false;
46}
47
48$ext = pathinfo( $file, PATHINFO_EXTENSION );
49if ( $ext == 'php' ) {
50 // Let built-in server handle script inclusion.
51 return false;
52} else {
53 // Serve static file with appropiate Content-Type headers.
54 // The built-in server for PHP 7.0+ supports most files already
55 // (contrary to PHP 5.2, which was supported when router.php was created).
56 // But it still doesn't support as many MIME types as MediaWiki (e.g. ".json")
57 require_once __DIR__ . "/../../../includes/libs/mime/MimeMap.php";
58
59 // Fallback
60 $mime = 'text/plain';
61 // Borrow from MimeAnalyzer
62 foreach ( \Wikimedia\Mime\MimeMap::MIME_EXTENSIONS as $type => $exts ) {
63 if ( in_array( $ext, $exts ) ) {
64 $mime = $type;
65 break;
66 }
67 }
68
69 if ( preg_match( '#^text/#', $mime ) ) {
70 // Text should have a charset=UTF-8 (PHP's webserver does this too)
71 header( "Content-Type: $mime; charset=UTF-8" );
72 } else {
73 header( "Content-Type: $mime" );
74 }
75
76 $content = file_get_contents( $file );
77
78 header( 'Vary: Accept-Encoding' );
79 $acceptGzip = preg_match( '/\bgzip\b/', $_SERVER['HTTP_ACCEPT_ENCODING'] ?? '' );
80 if ( $acceptGzip &&
81 // Don't compress binary static files (e.g. png)
82 preg_match( '/text|javascript|json|css|xml|svg/', $mime ) &&
83 // Tiny files tend to grow instead of shrink. – <https://gerrit.wikimedia.org/r/537974>
84 strlen( $content ) > 150
85 ) {
86 $content = gzencode( $content, 9 );
87 header( 'Content-Encoding: gzip' );
88 }
89 header( "Content-Length: " . strlen( $content ) );
90 echo $content;
91
92 return true;
93}
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
$acceptGzip
Definition router.php:79
$content
Definition router.php:76
$mime
Definition router.php:60
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42
if(!is_readable( $file)) $ext
Definition router.php:48