From 3ea576aa259b9f990e18aa3c58c62036d80c3ce3 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Fri, 14 Jul 2006 05:35:31 +0000 Subject: [PATCH] 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. --- StartProfiler.php | 22 +++++++ img_auth.php | 17 ++--- includes/AutoLoader.php | 2 +- includes/DefaultSettings.php | 8 +-- includes/Exception.php | 3 +- includes/GlobalFunctions.php | 10 ++- includes/{Profiling.php => Profiler.php} | 4 +- includes/ProfilerSimple.php | 13 ++-- includes/ProfilerSimpleUDP.php | 2 +- includes/ProfilerStub.php | 2 +- includes/Setup.php | 30 +++------ includes/WebStart.php | 52 +++++++++++++++ includes/Wiki.php | 3 +- index.php | 80 +----------------------- profileinfo.php | 10 +-- redirect.php | 13 +--- thumb.php | 23 +++---- trackback.php | 16 +---- 18 files changed, 125 insertions(+), 185 deletions(-) create mode 100644 StartProfiler.php rename includes/{Profiling.php => Profiler.php} (99%) create mode 100644 includes/WebStart.php diff --git a/StartProfiler.php b/StartProfiler.php new file mode 100644 index 0000000000..ff51fbf13c --- /dev/null +++ b/StartProfiler.php @@ -0,0 +1,22 @@ + diff --git a/img_auth.php b/img_auth.php index fb58ba2823..111fbdf747 100644 --- a/img_auth.php +++ b/img_auth.php @@ -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 '$GLOBALS overwrite vulnerability'; - 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() {

Access denied

You need to log in to access files on this server

"; + wfLogProfilingData(); exit; } diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 783570f9d4..f2082f82cd 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -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', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 7148555ec9..9e89efdcb5 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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; diff --git a/includes/Exception.php b/includes/Exception.php index eeb45594a0..9b046117f9 100644 --- a/includes/Exception.php +++ b/includes/Exception.php @@ -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 ) {} } diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index c8a1a605a5..a50e3e1da5 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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(); diff --git a/includes/Profiling.php b/includes/Profiler.php similarity index 99% rename from includes/Profiling.php rename to includes/Profiler.php index edecc4f32f..9fa26e20b2 100644 --- a/includes/Profiling.php +++ b/includes/Profiler.php @@ -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; diff --git a/includes/ProfilerSimple.php b/includes/ProfilerSimple.php index ed058c65cd..777a10fd67 100644 --- a/includes/ProfilerSimple.php +++ b/includes/ProfilerSimple.php @@ -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 */ diff --git a/includes/ProfilerSimpleUDP.php b/includes/ProfilerSimpleUDP.php index c395228b08..5c8da1362b 100644 --- a/includes/ProfilerSimpleUDP.php +++ b/includes/ProfilerSimpleUDP.php @@ -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 { diff --git a/includes/ProfilerStub.php b/includes/ProfilerStub.php index 3bcdaab2af..4cf0aa4494 100644 --- a/includes/ProfilerStub.php +++ b/includes/ProfilerStub.php @@ -21,6 +21,6 @@ function wfProfileOut( $fn = '' ) { } function wfGetProfilingOutput( $s, $e ) {} function wfProfileClose() {} -function wfLogProfilingData() {} +$wgProfiling = false; ?> diff --git a/includes/Setup.php b/includes/Setup.php index 381ea1b61b..6b2f5841c1 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -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 ); -} ?> diff --git a/includes/WebStart.php b/includes/WebStart.php new file mode 100644 index 0000000000..fc6d9695c3 --- /dev/null +++ b/includes/WebStart.php @@ -0,0 +1,52 @@ +$GLOBALS overwrite vulnerability'); +} + +# 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' ); +} +?> diff --git a/includes/Wiki.php b/includes/Wiki.php index 6f01000382..d2f51f851d 100644 --- a/includes/Wiki.php +++ b/includes/Wiki.php @@ -302,8 +302,7 @@ class MediaWiki { * Ends this task peacefully */ function restInPeace ( &$loadBalancer ) { - wfProfileClose(); - logProfilingData(); + wfLogProfilingData(); $loadBalancer->closeAll(); wfDebug( "Request ended normally\n" ); } diff --git a/index.php b/index.php index f18fa8a6ed..00a29f2151 100644 --- a/index.php +++ b/index.php @@ -1,83 +1,7 @@ $GLOBALS overwrite vulnerability'); -} - -# 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 -?> - - - - MediaWiki <?php echo $wgVersion ?> - - - - - The MediaWiki logo - -

MediaWiki

-
- config/LocalSettings.php to the parent directory.' ); - } else { - echo( 'Please setup the wiki first.' ); - } - ?> - -
- - -$GLOBALS overwrite vulnerability'; - 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) { diff --git a/redirect.php b/redirect.php index c0e8746ef6..4718600d6c 100644 --- a/redirect.php +++ b/redirect.php @@ -1,15 +1,6 @@ $GLOBALS overwrite vulnerability'; - 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' ); diff --git a/thumb.php b/thumb.php index 54ec64cf00..d4e561e69e 100644 --- a/thumb.php +++ b/thumb.php @@ -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 '$GLOBALS overwrite vulnerability'; - 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 ) { "; } +wfProfileOut( 'thumb.php-render' ); wfProfileOut( 'thumb.php' ); - +wfLogProfilingData(); ?> diff --git a/trackback.php b/trackback.php index 274a1c82d7..ae1e32b25a 100644 --- a/trackback.php +++ b/trackback.php @@ -4,21 +4,7 @@ * @package MediaWiki * @subpackage SpecialPage */ - -unset($IP); -define('MEDIAWIKI', true); -if ( isset( $_REQUEST['GLOBALS'] ) ) { - echo '$GLOBALS overwrite vulnerability'; - 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'); -- 2.20.1