Don't check namespace in SpecialWantedtemplates
[lhc/web/wiklou.git] / includes / libs / ScopedPHPTimeout.php
1 <?php
2 /**
3 * Expansion of the PHP execution time limit feature for a function call.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * Class to expand PHP execution time for a function call.
25 * Use this when performing changes that should not be interrupted.
26 *
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.
31 */
32 class ScopedPHPTimeout {
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
45 /**
46 * @param $seconds integer
47 */
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
62 }
63 }
64 }
65
66 /**
67 * Restore the original timeout.
68 * This does not account for the timer value on __construct().
69 */
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 );
82 }
83 }
84 }