Non-word characters don't terminate tag names.
[mediawiki.git] / includes / ScopedPHPTimeout.php
blobd1493c30b7ad0efee52cde73bd70f636393f978c
1 <?php
2 /**
3 * Expansion of the PHP execution time limit feature for a function call.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
23 /**
24 * Class to expand PHP execution time for a function call.
25 * Use this when performing changes that should not be interrupted.
27 * On construction, set_time_limit() is called and set to $seconds.
28 * If the client aborts the connection, PHP will continue to run.
29 * When the object goes out of scope, the timer is restarted, with
30 * the original time limit minus the time the object existed.
32 class ScopedPHPTimeout {
33 protected $startTime; // float; seconds
34 protected $oldTimeout; // integer; seconds
35 protected $oldIgnoreAbort; // boolean
37 protected static $stackDepth = 0; // integer
38 protected static $totalCalls = 0; // integer
39 protected static $totalElapsed = 0; // float; seconds
41 /* Prevent callers in infinite loops from running forever */
42 const MAX_TOTAL_CALLS = 1000000;
43 const MAX_TOTAL_TIME = 300; // seconds
45 /**
46 * @param $seconds integer
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 );
60 ++self::$stackDepth;
61 ++self::$totalCalls; // proof against < 1us scopes
66 /**
67 * Restore the original timeout.
68 * This does not account for the timer value on __construct().
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;
80 --self::$stackDepth;
81 ignore_user_abort( $this->oldIgnoreAbort );