Consolidated web initialisation code into includes/WebStart.php. Moved profiling...
authorTim Starling <tstarling@users.mediawiki.org>
Fri, 14 Jul 2006 05:35:31 +0000 (05:35 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Fri, 14 Jul 2006 05:35:31 +0000 (05:35 +0000)
19 files changed:
StartProfiler.php [new file with mode: 0644]
img_auth.php
includes/AutoLoader.php
includes/DefaultSettings.php
includes/Exception.php
includes/GlobalFunctions.php
includes/Profiler.php [new file with mode: 0644]
includes/ProfilerSimple.php
includes/ProfilerSimpleUDP.php
includes/ProfilerStub.php
includes/Profiling.php [deleted file]
includes/Setup.php
includes/WebStart.php [new file with mode: 0644]
includes/Wiki.php
index.php
profileinfo.php
redirect.php
thumb.php
trackback.php

diff --git a/StartProfiler.php b/StartProfiler.php
new file mode 100644 (file)
index 0000000..ff51fbf
--- /dev/null
@@ -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
+ */
+
+?>
index fb58ba2..111fbdf 100644 (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;
 }
 
index 783570f..f2082f8 100644 (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',
index 7148555..9e89efd 100644 (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;
 
index eeb4559..9b04611 100644 (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 ) {}
        }
 
index c8a1a60..a50e3e1 100644 (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();
diff --git a/includes/Profiler.php b/includes/Profiler.php
new file mode 100644 (file)
index 0000000..9fa26e2
--- /dev/null
@@ -0,0 +1,355 @@
+<?php
+/**
+ * This file is only included if profiling is enabled
+ * @package MediaWiki
+ */
+
+$wgProfiling = true;
+
+/**
+ * @param $functioname name of the function we will profile
+ */
+function wfProfileIn($functionname) {
+       global $wgProfiler;
+       $wgProfiler->profileIn($functionname);
+}
+
+/**
+ * @param $functioname name of the function we have profiled
+ */
+function wfProfileOut($functionname = 'missing') {
+       global $wgProfiler;
+       $wgProfiler->profileOut($functionname);
+}
+
+function wfGetProfilingOutput($start, $elapsed) {
+       global $wgProfiler;
+       return $wgProfiler->getOutput($start, $elapsed);
+}
+
+function wfProfileClose() {
+       global $wgProfiler;
+       $wgProfiler->close();
+}
+
+if (!function_exists('memory_get_usage')) {
+       # Old PHP or --enable-memory-limit not compiled in
+       function memory_get_usage() {
+               return 0;
+       }
+}
+
+/**
+ * @todo document
+ * @package MediaWiki
+ */
+class Profiler {
+       var $mStack = array (), $mWorkStack = array (), $mCollated = array ();
+       var $mCalls = array (), $mTotals = array ();
+
+       function Profiler()
+       {
+               // Push an entry for the pre-profile setup time onto the stack
+               global $wgRequestTime;
+               if ( !empty( $wgRequestTime ) ) {
+                       $this->mWorkStack[] = array( '-total', 0, $wgRequestTime, 0 );
+                       $this->mStack[] = array( '-setup', 1, $wgRequestTime, 0, microtime(true), 0 );
+               } else {
+                       $this->profileIn( '-total' );
+               }
+
+       }
+
+       function profileIn($functionname) {
+               global $wgDebugFunctionEntry;
+               if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
+                       wfDebug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
+               }
+               $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), $this->getTime(), memory_get_usage());
+       }
+
+       function profileOut($functionname) {
+               $memory = memory_get_usage();
+               $time = $this->getTime();
+
+               global $wgDebugFunctionEntry;
+
+               if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
+                       wfDebug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
+               }
+
+               $bit = array_pop($this->mWorkStack);
+
+               if (!$bit) {
+                       wfDebug("Profiling error, !\$bit: $functionname\n");
+               } else {
+                       //if ($wgDebugProfiling) {
+                               if ($functionname == 'close') {
+                                       $message = "Profile section ended by close(): {$bit[0]}";
+                                       wfDebug( "$message\n" );
+                                       $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
+                               }
+                               elseif ($bit[0] != $functionname) {
+                                       $message = "Profiling error: in({$bit[0]}), out($functionname)";
+                                       wfDebug( "$message\n" );
+                                       $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
+                               }
+                       //}
+                       $bit[] = $time;
+                       $bit[] = $memory;
+                       $this->mStack[] = $bit;
+               }
+       }
+
+       function close() {
+               while (count($this->mWorkStack)) {
+                       $this->profileOut('close');
+               }
+       }
+
+       function getOutput() {
+               global $wgDebugFunctionEntry;
+               $wgDebugFunctionEntry = false;
+
+               if (!count($this->mStack) && !count($this->mCollated)) {
+                       return "No profiling output\n";
+               }
+               $this->close();
+
+               global $wgProfileCallTree;
+               if ($wgProfileCallTree) {
+                       return $this->getCallTree();
+               } else {
+                       return $this->getFunctionReport();
+               }
+       }
+
+       function getCallTree($start = 0) {
+               return implode('', array_map(array (& $this, 'getCallTreeLine'), $this->remapCallTree($this->mStack)));
+       }
+
+       function remapCallTree($stack) {
+               if (count($stack) < 2) {
+                       return $stack;
+               }
+               $outputs = array ();
+               for ($max = count($stack) - 1; $max > 0;) {
+                       /* Find all items under this entry */
+                       $level = $stack[$max][1];
+                       $working = array ();
+                       for ($i = $max -1; $i >= 0; $i --) {
+                               if ($stack[$i][1] > $level) {
+                                       $working[] = $stack[$i];
+                               } else {
+                                       break;
+                               }
+                       }
+                       $working = $this->remapCallTree(array_reverse($working));
+                       $output = array ();
+                       foreach ($working as $item) {
+                               array_push($output, $item);
+                       }
+                       array_unshift($output, $stack[$max]);
+                       $max = $i;
+
+                       array_unshift($outputs, $output);
+               }
+               $final = array ();
+               foreach ($outputs as $output) {
+                       foreach ($output as $item) {
+                               $final[] = $item;
+                       }
+               }
+               return $final;
+       }
+
+       function getCallTreeLine($entry) {
+               list ($fname, $level, $start, $x, $end) = $entry;
+               $delta = $end - $start;
+               $space = str_repeat(' ', $level);
+
+               # The ugly double sprintf is to work around a PHP bug,
+               # which has been fixed in recent releases.
+               return sprintf( "%10s %s %s\n",
+                       trim( sprintf( "%7.3f", $delta * 1000.0 ) ),
+                       $space, $fname );
+       }
+
+       function getTime() {
+               return microtime(true);
+               #return $this->getUserTime();
+       }
+
+       function getUserTime() {
+               $ru = getrusage();
+               return $ru['ru_utime.tv_sec'].' '.$ru['ru_utime.tv_usec'] / 1e6;
+       }
+
+       function getFunctionReport() {
+               $width = 140;
+               $nameWidth = $width - 65;
+               $format =      "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d  (%13.3f -%13.3f) [%d]\n";
+               $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s\n";
+               $prof = "\nProfiling data\n";
+               $prof .= sprintf($titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem');
+               $this->mCollated = array ();
+               $this->mCalls = array ();
+               $this->mMemory = array ();
+
+               # Estimate profiling overhead
+               $profileCount = count($this->mStack);
+               wfProfileIn('-overhead-total');
+               for ($i = 0; $i < $profileCount; $i ++) {
+                       wfProfileIn('-overhead-internal');
+                       wfProfileOut('-overhead-internal');
+               }
+               wfProfileOut('-overhead-total');
+
+               # First, subtract the overhead!
+               foreach ($this->mStack as $entry) {
+                       $fname = $entry[0];
+                       $thislevel = $entry[1];
+                       $start = $entry[2];
+                       $end = $entry[4];
+                       $elapsed = $end - $start;
+                       $memory = $entry[5] - $entry[3];
+
+                       if ($fname == '-overhead-total') {
+                               $overheadTotal[] = $elapsed;
+                               $overheadMemory[] = $memory;
+                       }
+                       elseif ($fname == '-overhead-internal') {
+                               $overheadInternal[] = $elapsed;
+                       }
+               }
+               $overheadTotal = array_sum($overheadTotal) / count($overheadInternal);
+               $overheadMemory = array_sum($overheadMemory) / count($overheadInternal);
+               $overheadInternal = array_sum($overheadInternal) / count($overheadInternal);
+
+               # Collate
+               foreach ($this->mStack as $index => $entry) {
+                       $fname = $entry[0];
+                       $thislevel = $entry[1];
+                       $start = $entry[2];
+                       $end = $entry[4];
+                       $elapsed = $end - $start;
+
+                       $memory = $entry[5] - $entry[3];
+                       $subcalls = $this->calltreeCount($this->mStack, $index);
+
+                       if (!preg_match('/^-overhead/', $fname)) {
+                               # Adjust for profiling overhead (except special values with elapsed=0
+                               if ( $elapsed ) {
+                                       $elapsed -= $overheadInternal;
+                                       $elapsed -= ($subcalls * $overheadTotal);
+                                       $memory -= ($subcalls * $overheadMemory);
+                               }
+                       }
+
+                       if (!array_key_exists($fname, $this->mCollated)) {
+                               $this->mCollated[$fname] = 0;
+                               $this->mCalls[$fname] = 0;
+                               $this->mMemory[$fname] = 0;
+                               $this->mMin[$fname] = 1 << 24;
+                               $this->mMax[$fname] = 0;
+                               $this->mOverhead[$fname] = 0;
+                       }
+
+                       $this->mCollated[$fname] += $elapsed;
+                       $this->mCalls[$fname]++;
+                       $this->mMemory[$fname] += $memory;
+                       $this->mMin[$fname] = min($this->mMin[$fname], $elapsed);
+                       $this->mMax[$fname] = max($this->mMax[$fname], $elapsed);
+                       $this->mOverhead[$fname] += $subcalls;
+               }
+
+               $total = @ $this->mCollated['-total'];
+               $this->mCalls['-overhead-total'] = $profileCount;
+
+               # Output
+               arsort($this->mCollated, SORT_NUMERIC);
+               foreach ($this->mCollated as $fname => $elapsed) {
+                       $calls = $this->mCalls[$fname];
+                       $percent = $total ? 100. * $elapsed / $total : 0;
+                       $memory = $this->mMemory[$fname];
+                       $prof .= sprintf($format, substr($fname, 0, $nameWidth), $calls, (float) ($elapsed * 1000), (float) ($elapsed * 1000) / $calls, $percent, $memory, ($this->mMin[$fname] * 1000.0), ($this->mMax[$fname] * 1000.0), $this->mOverhead[$fname]);
+
+                       global $wgProfileToDatabase;
+                       if ($wgProfileToDatabase) {
+                               Profiler :: logToDB($fname, (float) ($elapsed * 1000), $calls);
+                       }
+               }
+               $prof .= "\nTotal: $total\n\n";
+
+               return $prof;
+       }
+
+       /**
+        * Counts the number of profiled function calls sitting under
+        * the given point in the call graph. Not the most efficient algo.
+        *
+        * @param $stack Array:
+        * @param $start Integer:
+        * @return Integer
+        * @private
+        */
+       function calltreeCount(& $stack, $start) {
+               $level = $stack[$start][1];
+               $count = 0;
+               for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) {
+                       $count ++;
+               }
+               return $count;
+       }
+
+       /**
+        * @static
+        */
+       function logToDB($name, $timeSum, $eventCount) {
+               # Warning: $wguname is a live patch, it should be moved to Setup.php
+               global $wguname, $wgProfilePerHost;
+
+               $fname = 'Profiler::logToDB';
+               $dbw = & wfGetDB(DB_MASTER);
+               if (!is_object($dbw))
+                       return false;
+               $errorState = $dbw->ignoreErrors( true );
+               $profiling = $dbw->tableName('profiling');
+
+               $name = substr($name, 0, 255);
+               $encname = $dbw->strencode($name);
+               
+               if ($wgProfilePerHost) {
+                       $pfhost = $wguname['nodename'];
+               } else {
+                       $pfhost = '';
+               }
+
+               $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} ".
+                       "WHERE pf_name='{$encname}' AND pf_server='{$pfhost}'";
+               $dbw->query($sql);
+
+               $rc = $dbw->affectedRows();
+               if ($rc == 0) {
+                       $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount,
+                               'pf_time' => $timeSum, 'pf_server' => $pfhost ), $fname, array ('IGNORE'));
+               }
+               // When we upgrade to mysql 4.1, the insert+update
+               // can be merged into just a insert with this construct added:
+               //     "ON DUPLICATE KEY UPDATE ".
+               //     "pf_count=pf_count + VALUES(pf_count), ".
+               //     "pf_time=pf_time + VALUES(pf_time)";
+               $dbw->ignoreErrors( $errorState );
+       }
+
+       /**
+        * Get the function name of the current profiling section
+        */
+       function getCurrentSection() {
+               $elt = end($this->mWorkStack);
+               return $elt[0];
+       }
+
+}
+
+?>
index ed058c6..777a10f 100644 (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 */
index c395228..5c8da13 100644 (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 {
index 3bcdaab..4cf0aa4 100644 (file)
@@ -21,6 +21,6 @@ function wfProfileOut( $fn = '' ) {
 }
 function wfGetProfilingOutput( $s, $e ) {}
 function wfProfileClose() {}
-function wfLogProfilingData() {}
+$wgProfiling = false;
 
 ?>
diff --git a/includes/Profiling.php b/includes/Profiling.php
deleted file mode 100644 (file)
index edecc4f..0000000
+++ /dev/null
@@ -1,353 +0,0 @@
-<?php
-/**
- * This file is only included if profiling is enabled
- * @package MediaWiki
- */
-
-/**
- * @param $functioname name of the function we will profile
- */
-function wfProfileIn($functionname) {
-       global $wgProfiler;
-       $wgProfiler->profileIn($functionname);
-}
-
-/**
- * @param $functioname name of the function we have profiled
- */
-function wfProfileOut($functionname = 'missing') {
-       global $wgProfiler;
-       $wgProfiler->profileOut($functionname);
-}
-
-function wfGetProfilingOutput($start, $elapsed) {
-       global $wgProfiler;
-       return $wgProfiler->getOutput($start, $elapsed);
-}
-
-function wfProfileClose() {
-       global $wgProfiler;
-       $wgProfiler->close();
-}
-
-if (!function_exists('memory_get_usage')) {
-       # Old PHP or --enable-memory-limit not compiled in
-       function memory_get_usage() {
-               return 0;
-       }
-}
-
-/**
- * @todo document
- * @package MediaWiki
- */
-class Profiler {
-       var $mStack = array (), $mWorkStack = array (), $mCollated = array ();
-       var $mCalls = array (), $mTotals = array ();
-
-       function Profiler()
-       {
-               // Push an entry for the pre-profile setup time onto the stack
-               global $wgRequestTime;
-               if ( !empty( $wgRequestTime ) ) {
-                       $this->mWorkStack[] = array( '-total', 0, $wgRequestTime, 0 );
-                       $this->mStack[] = array( '-setup', 1, $wgRequestTime, 0, microtime(true), 0 );
-               } else {
-                       $this->profileIn( '-total' );
-               }
-
-       }
-
-       function profileIn($functionname) {
-               global $wgDebugFunctionEntry;
-               if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
-                       wfDebug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
-               }
-               $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), $this->getTime(), memory_get_usage());
-       }
-
-       function profileOut($functionname) {
-               $memory = memory_get_usage();
-               $time = $this->getTime();
-
-               global $wgDebugFunctionEntry;
-
-               if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
-                       wfDebug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
-               }
-
-               $bit = array_pop($this->mWorkStack);
-
-               if (!$bit) {
-                       wfDebug("Profiling error, !\$bit: $functionname\n");
-               } else {
-                       //if ($wgDebugProfiling) {
-                               if ($functionname == 'close') {
-                                       $message = "Profile section ended by close(): {$bit[0]}";
-                                       wfDebug( "$message\n" );
-                                       $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
-                               }
-                               elseif ($bit[0] != $functionname) {
-                                       $message = "Profiling error: in({$bit[0]}), out($functionname)";
-                                       wfDebug( "$message\n" );
-                                       $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
-                               }
-                       //}
-                       $bit[] = $time;
-                       $bit[] = $memory;
-                       $this->mStack[] = $bit;
-               }
-       }
-
-       function close() {
-               while (count($this->mWorkStack)) {
-                       $this->profileOut('close');
-               }
-       }
-
-       function getOutput() {
-               global $wgDebugFunctionEntry;
-               $wgDebugFunctionEntry = false;
-
-               if (!count($this->mStack) && !count($this->mCollated)) {
-                       return "No profiling output\n";
-               }
-               $this->close();
-
-               global $wgProfileCallTree;
-               if ($wgProfileCallTree) {
-                       return $this->getCallTree();
-               } else {
-                       return $this->getFunctionReport();
-               }
-       }
-
-       function getCallTree($start = 0) {
-               return implode('', array_map(array (& $this, 'getCallTreeLine'), $this->remapCallTree($this->mStack)));
-       }
-
-       function remapCallTree($stack) {
-               if (count($stack) < 2) {
-                       return $stack;
-               }
-               $outputs = array ();
-               for ($max = count($stack) - 1; $max > 0;) {
-                       /* Find all items under this entry */
-                       $level = $stack[$max][1];
-                       $working = array ();
-                       for ($i = $max -1; $i >= 0; $i --) {
-                               if ($stack[$i][1] > $level) {
-                                       $working[] = $stack[$i];
-                               } else {
-                                       break;
-                               }
-                       }
-                       $working = $this->remapCallTree(array_reverse($working));
-                       $output = array ();
-                       foreach ($working as $item) {
-                               array_push($output, $item);
-                       }
-                       array_unshift($output, $stack[$max]);
-                       $max = $i;
-
-                       array_unshift($outputs, $output);
-               }
-               $final = array ();
-               foreach ($outputs as $output) {
-                       foreach ($output as $item) {
-                               $final[] = $item;
-                       }
-               }
-               return $final;
-       }
-
-       function getCallTreeLine($entry) {
-               list ($fname, $level, $start, $x, $end) = $entry;
-               $delta = $end - $start;
-               $space = str_repeat(' ', $level);
-
-               # The ugly double sprintf is to work around a PHP bug,
-               # which has been fixed in recent releases.
-               return sprintf( "%10s %s %s\n",
-                       trim( sprintf( "%7.3f", $delta * 1000.0 ) ),
-                       $space, $fname );
-       }
-
-       function getTime() {
-               return microtime(true);
-               #return $this->getUserTime();
-       }
-
-       function getUserTime() {
-               $ru = getrusage();
-               return $ru['ru_utime.tv_sec'].' '.$ru['ru_utime.tv_usec'] / 1e6;
-       }
-
-       function getFunctionReport() {
-               $width = 140;
-               $nameWidth = $width - 65;
-               $format =      "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d  (%13.3f -%13.3f) [%d]\n";
-               $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s\n";
-               $prof = "\nProfiling data\n";
-               $prof .= sprintf($titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem');
-               $this->mCollated = array ();
-               $this->mCalls = array ();
-               $this->mMemory = array ();
-
-               # Estimate profiling overhead
-               $profileCount = count($this->mStack);
-               wfProfileIn('-overhead-total');
-               for ($i = 0; $i < $profileCount; $i ++) {
-                       wfProfileIn('-overhead-internal');
-                       wfProfileOut('-overhead-internal');
-               }
-               wfProfileOut('-overhead-total');
-
-               # First, subtract the overhead!
-               foreach ($this->mStack as $entry) {
-                       $fname = $entry[0];
-                       $thislevel = $entry[1];
-                       $start = $entry[2];
-                       $end = $entry[4];
-                       $elapsed = $end - $start;
-                       $memory = $entry[5] - $entry[3];
-
-                       if ($fname == '-overhead-total') {
-                               $overheadTotal[] = $elapsed;
-                               $overheadMemory[] = $memory;
-                       }
-                       elseif ($fname == '-overhead-internal') {
-                               $overheadInternal[] = $elapsed;
-                       }
-               }
-               $overheadTotal = array_sum($overheadTotal) / count($overheadInternal);
-               $overheadMemory = array_sum($overheadMemory) / count($overheadInternal);
-               $overheadInternal = array_sum($overheadInternal) / count($overheadInternal);
-
-               # Collate
-               foreach ($this->mStack as $index => $entry) {
-                       $fname = $entry[0];
-                       $thislevel = $entry[1];
-                       $start = $entry[2];
-                       $end = $entry[4];
-                       $elapsed = $end - $start;
-
-                       $memory = $entry[5] - $entry[3];
-                       $subcalls = $this->calltreeCount($this->mStack, $index);
-
-                       if (!preg_match('/^-overhead/', $fname)) {
-                               # Adjust for profiling overhead (except special values with elapsed=0
-                               if ( $elapsed ) {
-                                       $elapsed -= $overheadInternal;
-                                       $elapsed -= ($subcalls * $overheadTotal);
-                                       $memory -= ($subcalls * $overheadMemory);
-                               }
-                       }
-
-                       if (!array_key_exists($fname, $this->mCollated)) {
-                               $this->mCollated[$fname] = 0;
-                               $this->mCalls[$fname] = 0;
-                               $this->mMemory[$fname] = 0;
-                               $this->mMin[$fname] = 1 << 24;
-                               $this->mMax[$fname] = 0;
-                               $this->mOverhead[$fname] = 0;
-                       }
-
-                       $this->mCollated[$fname] += $elapsed;
-                       $this->mCalls[$fname]++;
-                       $this->mMemory[$fname] += $memory;
-                       $this->mMin[$fname] = min($this->mMin[$fname], $elapsed);
-                       $this->mMax[$fname] = max($this->mMax[$fname], $elapsed);
-                       $this->mOverhead[$fname] += $subcalls;
-               }
-
-               $total = @ $this->mCollated['-total'];
-               $this->mCalls['-overhead-total'] = $profileCount;
-
-               # Output
-               asort($this->mCollated, SORT_NUMERIC);
-               foreach ($this->mCollated as $fname => $elapsed) {
-                       $calls = $this->mCalls[$fname];
-                       $percent = $total ? 100. * $elapsed / $total : 0;
-                       $memory = $this->mMemory[$fname];
-                       $prof .= sprintf($format, substr($fname, 0, $nameWidth), $calls, (float) ($elapsed * 1000), (float) ($elapsed * 1000) / $calls, $percent, $memory, ($this->mMin[$fname] * 1000.0), ($this->mMax[$fname] * 1000.0), $this->mOverhead[$fname]);
-
-                       global $wgProfileToDatabase;
-                       if ($wgProfileToDatabase) {
-                               Profiler :: logToDB($fname, (float) ($elapsed * 1000), $calls);
-                       }
-               }
-               $prof .= "\nTotal: $total\n\n";
-
-               return $prof;
-       }
-
-       /**
-        * Counts the number of profiled function calls sitting under
-        * the given point in the call graph. Not the most efficient algo.
-        *
-        * @param $stack Array:
-        * @param $start Integer:
-        * @return Integer
-        * @private
-        */
-       function calltreeCount(& $stack, $start) {
-               $level = $stack[$start][1];
-               $count = 0;
-               for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) {
-                       $count ++;
-               }
-               return $count;
-       }
-
-       /**
-        * @static
-        */
-       function logToDB($name, $timeSum, $eventCount) {
-               # Warning: $wguname is a live patch, it should be moved to Setup.php
-               global $wguname, $wgProfilePerHost;
-
-               $fname = 'Profiler::logToDB';
-               $dbw = & wfGetDB(DB_MASTER);
-               if (!is_object($dbw))
-                       return false;
-               $errorState = $dbw->ignoreErrors( true );
-               $profiling = $dbw->tableName('profiling');
-
-               $name = substr($name, 0, 255);
-               $encname = $dbw->strencode($name);
-               
-               if ($wgProfilePerHost) {
-                       $pfhost = $wguname['nodename'];
-               } else {
-                       $pfhost = '';
-               }
-
-               $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} ".
-                       "WHERE pf_name='{$encname}' AND pf_server='{$pfhost}'";
-               $dbw->query($sql);
-
-               $rc = $dbw->affectedRows();
-               if ($rc == 0) {
-                       $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount,
-                               'pf_time' => $timeSum, 'pf_server' => $pfhost ), $fname, array ('IGNORE'));
-               }
-               // When we upgrade to mysql 4.1, the insert+update
-               // can be merged into just a insert with this construct added:
-               //     "ON DUPLICATE KEY UPDATE ".
-               //     "pf_count=pf_count + VALUES(pf_count), ".
-               //     "pf_time=pf_time + VALUES(pf_time)";
-               $dbw->ignoreErrors( $errorState );
-       }
-
-       /**
-        * Get the function name of the current profiling section
-        */
-       function getCurrentSection() {
-               $elt = end($this->mWorkStack);
-               return $elt[0];
-       }
-
-}
-
-?>
index 381ea1b..6b2f584 100644 (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 );
 
-}
 ?>
diff --git a/includes/WebStart.php b/includes/WebStart.php
new file mode 100644 (file)
index 0000000..fc6d969
--- /dev/null
@@ -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' );
+}
+?>
index 6f01000..d2f51f8 100644 (file)
@@ -302,8 +302,7 @@ class MediaWiki {
         * Ends this task peacefully
         */
        function restInPeace ( &$loadBalancer ) {
-               wfProfileClose();
-               logProfilingData();
+               wfLogProfilingData();
                $loadBalancer->closeAll();
                wfDebug( "Request ended normally\n" );
        }
index f18fa8a..00a29f2 100644 (file)
--- a/index.php
+++ b/index.php
@@ -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" );
index 52658fb..388e0cd 100644 (file)
 
 $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) {
index c0e8746..4718600 100644 (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' );
index 54ec64c..d4e561e 100644 (file)
--- 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 '<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();
 
 ?>
index 274a1c8..ae1e32b 100644 (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');