Consolidated web initialisation code into includes/WebStart.php. Moved profiling setup to a hook file "StartProfiler.php", following Brion's suggestion to merge Wikimedia's early profiling patch into subversion. Renamed Profiling.php and logProfilingData(), removed unnecessary wfProfileClose() calls.

This commit is contained in:
Tim Starling 2006-07-14 05:35:31 +00:00
parent 0256e2c648
commit 3ea576aa25
18 changed files with 125 additions and 185 deletions

22
StartProfiler.php Normal file
View file

@ -0,0 +1,22 @@
<?php
require_once( './includes/ProfilerStub.php' );
/**
* To use a profiler, delete the line above and add something like this:
*
* require_once( './includes/Profiler.php' );
* $wgProfiler = new Profiler;
*
* Or for a sampling profiler:
* if ( !mt_rand( 0, 100 ) ) {
* require_once( './includes/Profiler.php' );
* $wgProfiler = new Profiler;
* } else {
* require_once( './includes/ProfilerStub.php' );
* }
*
* Configuration of the profiler output can be done in LocalSettings.php
*/
?>

View file

@ -7,18 +7,9 @@
* to an array of pages you want everyone to be able to access. Your server must
* support PATH_INFO, CGI-based configurations generally don't.
*/
# Valid web server entry point, enable includes
define( 'MEDIAWIKI', true );
if ( isset( $_REQUEST['GLOBALS'] ) ) {
echo '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>';
die( -1 );
}
require_once( 'includes/Defines.php' );
require_once( './LocalSettings.php' );
require_once( 'includes/Setup.php' );
require_once( 'includes/StreamFile.php' );
require_once( './includes/WebStart.php' );
wfProfileIn( 'img_auth.php' );
require_once( './includes/StreamFile.php' );
if( !isset( $_SERVER['PATH_INFO'] ) ) {
wfForbidden();
@ -47,6 +38,7 @@ if( is_dir( $filename ) ) {
# Write file
wfStreamFile( $filename );
wfLogProfilingData();
function wfForbidden() {
header( 'HTTP/1.0 403 Forbidden' );
@ -55,6 +47,7 @@ function wfForbidden() {
<h1>Access denied</h1>
<p>You need to log in to access files on this server</p>
</body></html>";
wfLogProfilingData();
exit;
}

View file

@ -122,7 +122,7 @@ function __autoload($className) {
'ParserCache' => 'includes/ParserCache.php',
'ProfilerSimple' => 'includes/ProfilerSimple.php',
'ProfilerSimpleUDP' => 'includes/ProfilerSimpleUDP.php',
'Profiler' => 'includes/Profiling.php',
'Profiler' => 'includes/Profiler.php',
'ProxyTools' => 'includes/ProxyTools.php',
'ProtectionForm' => 'includes/ProtectionForm.php',
'QueryPage' => 'includes/QueryPage.php',

View file

@ -1153,21 +1153,17 @@ $wgTexvc = './math/texvc';
#
# You have to create a 'profiling' table in your database before using
# profiling see maintenance/archives/patch-profiling.sql .
#
# To enable profiling, edit StartProfiler.php
/** Enable for more detailed by-function times in debug log */
$wgProfiling = false;
/** Only record profiling info for pages that took longer than this */
$wgProfileLimit = 0.0;
/** Don't put non-profiling info into log file */
$wgProfileOnly = false;
/** Log sums from profiling into "profiling" table in db. */
$wgProfileToDatabase = false;
/** Only profile every n requests when profiling is turned on */
$wgProfileSampleRate = 1;
/** If true, print a raw call tree instead of per-function report */
$wgProfileCallTree = false;
/** If not empty, specifies profiler type to load */
$wgProfilerType = '';
/** Should application server host be put into profiling table */
$wgProfilePerHost = false;

View file

@ -181,8 +181,7 @@ function wfExceptionHandler( $e ) {
// Final cleanup, similar to wfErrorExit()
if ( $wgFullyInitialised ) {
try {
wfProfileClose();
logProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
} catch ( Exception $e ) {}
}

View file

@ -243,13 +243,12 @@ function wfLogDBError( $text ) {
/**
* @todo document
*/
function logProfilingData() {
function wfLogProfilingData() {
global $wgRequestTime, $wgDebugLogFile, $wgDebugRawPage, $wgRequest;
global $wgProfiling, $wgUser;
$now = wfTime();
$elapsed = $now - $wgRequestTime;
if ( $wgProfiling ) {
$now = wfTime();
$elapsed = $now - $wgRequestTime;
$prof = wfGetProfilingOutput( $wgRequestTime, $elapsed );
$forward = '';
if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) )
@ -621,8 +620,7 @@ function wfAbruptExit( $error = false ){
wfDebug('WARNING: Abrupt exit\n');
}
wfProfileClose();
logProfilingData();
wfLogProfilingData();
if ( !$error ) {
$wgLoadBalancer->closeAll();

View file

@ -4,6 +4,8 @@
* @package MediaWiki
*/
$wgProfiling = true;
/**
* @param $functioname name of the function we will profile
*/
@ -265,7 +267,7 @@ class Profiler {
$this->mCalls['-overhead-total'] = $profileCount;
# Output
asort($this->mCollated, SORT_NUMERIC);
arsort($this->mCollated, SORT_NUMERIC);
foreach ($this->mCollated as $fname => $elapsed) {
$calls = $this->mCalls[$fname];
$percent = $total ? 100. * $elapsed / $total : 0;

View file

@ -8,7 +8,7 @@
* @todo document
* @package MediaWiki
*/
require_once(dirname(__FILE__).'/Profiling.php');
require_once(dirname(__FILE__).'/Profiler.php');
class ProfilerSimple extends Profiler {
function ProfilerSimple() {
@ -86,9 +86,14 @@ class ProfilerSimple extends Profiler {
}
function getCpuTime($ru=null) {
if ($ru==null)
$ru=getrusage();
return ($ru['ru_utime.tv_sec']+$ru['ru_stime.tv_sec']+($ru['ru_utime.tv_usec']+$ru['ru_stime.tv_usec'])*1e-6);
if ( function_exists( getrusage() ) {
if ( $ru == null )
$ru = getrusage();
return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
$ru['ru_stime.tv_usec']) * 1e-6);
} else {
return 0;
}
}
/* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */

View file

@ -3,7 +3,7 @@
(the one from wikipedia/udpprofile CVS )
*/
require_once(dirname(__FILE__).'/Profiling.php');
require_once(dirname(__FILE__).'/Profiler.php');
require_once(dirname(__FILE__).'/ProfilerSimple.php');
class ProfilerSimpleUDP extends ProfilerSimple {

View file

@ -21,6 +21,6 @@ function wfProfileOut( $fn = '' ) {
}
function wfGetProfilingOutput( $s, $e ) {}
function wfProfileClose() {}
function wfLogProfilingData() {}
$wgProfiling = false;
?>

View file

@ -8,7 +8,10 @@
* This file is not a valid entry point, perform no further processing unless
* MEDIAWIKI is defined
*/
if( defined( 'MEDIAWIKI' ) ) {
if( !defined( 'MEDIAWIKI' ) ) {
echo "This file is part of MediaWiki, it is not a valid entry point.\n";
exit( 1 );
}
# The main wiki script and things like database
# conversion and maintenance scripts all share a
@ -16,35 +19,17 @@ if( defined( 'MEDIAWIKI' ) ) {
# setting up a few globals.
#
$fname = 'Setup.php';
wfProfileIn( $fname );
// Check to see if we are at the file scope
if ( !isset( $wgVersion ) ) {
echo "Error, Setup.php must be included from the file scope, after DefaultSettings.php\n";
die( 1 );
}
if( !isset( $wgProfiling ) )
$wgProfiling = false;
require_once( "$IP/includes/AutoLoader.php" );
if ( function_exists( 'wfProfileIn' ) ) {
/* nada, everything should be done already */
} elseif ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) {
$wgProfiling = true;
if ($wgProfilerType == "") {
$wgProfiler = new Profiler();
} else {
$prclass="Profiler{$wgProfilerType}";
require_once( $prclass.".php" );
$wgProfiler = new $prclass();
}
} else {
require_once( "$IP/includes/ProfilerStub.php" );
}
$fname = 'Setup.php';
wfProfileIn( $fname );
wfProfileIn( $fname.'-exception' );
require_once( "$IP/includes/Exception.php" );
wfInstallExceptionHandler();
@ -323,5 +308,4 @@ $wgFullyInitialised = true;
wfProfileOut( $fname.'-extensions' );
wfProfileOut( $fname );
}
?>

52
includes/WebStart.php Normal file
View file

@ -0,0 +1,52 @@
<?php
# This does the initial setup for a web request. It does some security checks,
# starts the profiler and loads the configuration, and optionally loads
# Setup.php depending on whether MW_NO_SETUP is defined.
$wgRequestTime = microtime(true);
# getrusage() does not exist on the Microsoft Windows platforms, catching this
if ( function_exists ( 'getrusage' ) ) {
$wgRUstart = getrusage();
} else {
$wgRUstart = array();
}
unset( $IP );
@ini_set( 'allow_url_fopen', 0 ); # For security
if ( isset( $_REQUEST['GLOBALS'] ) ) {
die( '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>');
}
# Valid web server entry point, enable includes.
# Please don't move this line to includes/Defines.php. This line essentially
# defines a valid entry point. If you put it in includes/Defines.php, then
# any script that includes it becomes an entry point, thereby defeating
# its purpose.
define( 'MEDIAWIKI', true );
# Start profiler
require_once( './StartProfiler.php' );
wfProfileIn( 'WebStart.php-conf' );
# Load up some global defines.
require_once( './includes/Defines.php' );
# LocalSettings.php is the per site customization file. If it does not exit
# the wiki installer need to be launched or the generated file moved from
# ./config/ to ./
if( !file_exists( './LocalSettings.php' ) ) {
$IP = '.';
require_once( './includes/DefaultSettings.php' ); # used for printing the version
require_once( './includes/templates/NoLocalSettings.php' );
die();
}
# Include this site setttings
require_once( './LocalSettings.php' );
wfProfileOut( 'WebStart.php-conf' );
if ( !defined( 'MW_NO_SETUP' ) ) {
require_once( './includes/Setup.php' );
}
?>

View file

@ -302,8 +302,7 @@ class MediaWiki {
* Ends this task peacefully
*/
function restInPeace ( &$loadBalancer ) {
wfProfileClose();
logProfilingData();
wfLogProfilingData();
$loadBalancer->closeAll();
wfDebug( "Request ended normally\n" );
}

View file

@ -1,83 +1,7 @@
<?php
/**
* Main wiki script; see docs/design.txt
* @package MediaWiki
*/
$wgRequestTime = microtime(true);
# getrusage() does not exist on the Microsoft Windows platforms, catching this
if ( function_exists ( 'getrusage' ) ) {
$wgRUstart = getrusage();
} else {
$wgRUstart = array();
}
unset( $IP );
@ini_set( 'allow_url_fopen', 0 ); # For security...
if ( isset( $_REQUEST['GLOBALS'] ) ) {
die( '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>');
}
# Valid web server entry point, enable includes.
# Please don't move this line to includes/Defines.php. This line essentially
# defines a valid entry point. If you put it in includes/Defines.php, then
# any script that includes it becomes an entry point, thereby defeating
# its purpose.
define( 'MEDIAWIKI', true );
# Load up some global defines.
require_once( './includes/Defines.php' );
# LocalSettings.php is the per site customization file. If it does not exit
# the wiki installer need to be launched or the generated file moved from
# ./config/ to ./
if( !file_exists( 'LocalSettings.php' ) ) {
$IP = '.';
require_once( 'includes/DefaultSettings.php' ); # used for printing the version
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<title>MediaWiki <?php echo $wgVersion ?></title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<style type='text/css' media='screen, projection'>
html, body {
color: #000;
background-color: #fff;
font-family: sans-serif;
text-align: center;
}
h1 {
font-size: 150%;
}
</style>
</head>
<body>
<img src='skins/common/images/mediawiki.png' alt='The MediaWiki logo' />
<h1>MediaWiki <?php echo $wgVersion ?></h1>
<div class='error'>
<?php
if ( file_exists( 'config/LocalSettings.php' ) ) {
echo( 'To complete the installation, move <tt>config/LocalSettings.php</tt> to the parent directory.' );
} else {
echo( 'Please <a href="config/index.php" title="setup">setup the wiki</a> first.' );
}
?>
</div>
</body>
</html>
<?php
die();
}
# Include this site setttings
require_once( './LocalSettings.php' );
# Prepare MediaWiki
require_once( 'includes/Setup.php' );
# Initialise common code
require_once( './includes/WebStart.php' );
# Initialize MediaWiki base class
require_once( "includes/Wiki.php" );

View file

@ -46,14 +46,8 @@
$wgDBadminuser = $wgDBadminpassword = $wgDBserver = $wgDBname = $wgEnableProfileInfo = false;
define("MEDIAWIKI", 1);
if ( isset( $_REQUEST['GLOBALS'] ) ) {
echo '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>';
die( -1 );
}
require_once("./includes/Defines.php");
require_once("./LocalSettings.php");
define( 'MW_NO_SETUP', 1 );
require_once( './includes/WebStart.php' );
require_once("./AdminSettings.php");
if (!$wgEnableProfileInfo) {

View file

@ -1,15 +1,6 @@
<?php
unset( $DP );
unset( $IP );
$wgCommandLineMode = false;
define( 'MEDIAWIKI', true );
if ( isset( $_REQUEST['GLOBALS'] ) ) {
echo '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>';
die( -1 );
}
require_once( './includes/Defines.php' );
require_once( './LocalSettings.php' );
define( 'MW_NO_SETUP', 1 );
require_once( './includes/WebStart.php' );
global $wgArticlePath;
require_once( 'includes/WebRequest.php' );

View file

@ -4,18 +4,10 @@
* PHP script to stream out an image thumbnail.
* If the file exists, we make do with abridged MediaWiki initialisation.
*/
define( 'MEDIAWIKI', true );
unset( $IP );
if ( isset( $_REQUEST['GLOBALS'] ) ) {
echo '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>';
die( -1 );
}
define( 'MW_NO_OUTPUT_BUFFER', true );
require_once( './includes/Defines.php' );
require_once( './LocalSettings.php' );
define( 'MW_NO_SETUP', 1 );
require_once( './includes/WebStart.php' );
wfProfileIn( 'thumb.php' );
wfProfileIn( 'thumb.php-start' );
require_once( 'GlobalFunctions.php' );
require_once( 'ImageFunctions.php' );
@ -52,12 +44,14 @@ $thumbPath = wfImageThumbDir( $fileName ) . '/' . $thumbName;
if ( is_file( $thumbPath ) && filemtime( $thumbPath ) >= filemtime( $imagePath ) ) {
wfStreamFile( $thumbPath );
// Can't log profiling data with no Setup.php
exit;
}
// OK, no valid thumbnail, time to get out the heavy machinery
wfProfileOut( 'thumb.php-start' );
require_once( 'Setup.php' );
wfProfileIn( 'thumb.php' );
wfProfileIn( 'thumb.php-render' );
$img = Image::newFromName( $fileName );
if ( $img ) {
@ -79,7 +73,8 @@ if ( $thumb && $thumb->path ) {
</body></html>";
}
wfProfileOut( 'thumb.php-render' );
wfProfileOut( 'thumb.php' );
wfLogProfilingData();
?>

View file

@ -4,21 +4,7 @@
* @package MediaWiki
* @subpackage SpecialPage
*/
unset($IP);
define('MEDIAWIKI', true);
if ( isset( $_REQUEST['GLOBALS'] ) ) {
echo '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>';
die( -1 );
}
require_once('./includes/Defines.php');
if (!file_exists('LocalSettings.php'))
exit;
require_once('./LocalSettings.php');
require_once('includes/Setup.php');
require_once( './includes/WebStart.php' );
require_once('DatabaseFunctions.php');