MediaWiki  1.23.0
ScopedPHPTimeout.php
Go to the documentation of this file.
1 <?php
33  protected $startTime; // float; seconds
34  protected $oldTimeout; // integer; seconds
35  protected $oldIgnoreAbort; // boolean
36 
37  protected static $stackDepth = 0; // integer
38  protected static $totalCalls = 0; // integer
39  protected static $totalElapsed = 0; // float; seconds
40 
41  /* Prevent callers in infinite loops from running forever */
42  const MAX_TOTAL_CALLS = 1000000;
43  const MAX_TOTAL_TIME = 300; // seconds
44 
48  public function __construct( $seconds ) {
49  if ( ini_get( 'max_execution_time' ) > 0 ) { // CLI uses 0
50  if ( self::$totalCalls >= self::MAX_TOTAL_CALLS ) {
51  trigger_error( "Maximum invocations of " . __CLASS__ . " exceeded." );
52  } elseif ( self::$totalElapsed >= self::MAX_TOTAL_TIME ) {
53  trigger_error( "Time limit within invocations of " . __CLASS__ . " exceeded." );
54  } elseif ( self::$stackDepth > 0 ) { // recursion guard
55  trigger_error( "Resursive invocation of " . __CLASS__ . " attempted." );
56  } else {
57  $this->oldIgnoreAbort = ignore_user_abort( true );
58  $this->oldTimeout = ini_set( 'max_execution_time', $seconds );
59  $this->startTime = microtime( true );
61  ++self::$totalCalls; // proof against < 1us scopes
62  }
63  }
64  }
65 
70  public function __destruct() {
71  if ( $this->oldTimeout ) {
72  $elapsed = microtime( true ) - $this->startTime;
73  // Note: a limit of 0 is treated as "forever"
74  set_time_limit( max( 1, $this->oldTimeout - (int)$elapsed ) );
75  // If each scoped timeout is for less than one second, we end up
76  // restoring the original timeout without any decrease in value.
77  // Thus web scripts in an infinite loop can run forever unless we
78  // take some measures to prevent this. Track total time and calls.
79  self::$totalElapsed += $elapsed;
81  ignore_user_abort( $this->oldIgnoreAbort );
82  }
83  }
84 }
ScopedPHPTimeout\$startTime
$startTime
Definition: ScopedPHPTimeout.php:33
ScopedPHPTimeout\MAX_TOTAL_CALLS
const MAX_TOTAL_CALLS
Definition: ScopedPHPTimeout.php:42
ScopedPHPTimeout\__destruct
__destruct()
Restore the original timeout.
Definition: ScopedPHPTimeout.php:70
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
ScopedPHPTimeout\MAX_TOTAL_TIME
const MAX_TOTAL_TIME
Definition: ScopedPHPTimeout.php:43
ScopedPHPTimeout\$totalCalls
static $totalCalls
Definition: ScopedPHPTimeout.php:38
ScopedPHPTimeout
Class to expand PHP execution time for a function call.
Definition: ScopedPHPTimeout.php:32
ScopedPHPTimeout\$oldTimeout
$oldTimeout
Definition: ScopedPHPTimeout.php:34
ScopedPHPTimeout\__construct
__construct( $seconds)
Definition: ScopedPHPTimeout.php:48
ScopedPHPTimeout\$oldIgnoreAbort
$oldIgnoreAbort
Definition: ScopedPHPTimeout.php:35
ScopedPHPTimeout\$totalElapsed
static $totalElapsed
Definition: ScopedPHPTimeout.php:39
ScopedPHPTimeout\$stackDepth
static $stackDepth
Definition: ScopedPHPTimeout.php:37