1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* PHP time limit management.
|
|
|
19 |
*
|
|
|
20 |
* @package core
|
|
|
21 |
* @copyright 2013 The Open University
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
defined('MOODLE_INTERNAL') || die();
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Utility class to manage PHP time limit.
|
|
|
29 |
*/
|
|
|
30 |
class core_php_time_limit {
|
|
|
31 |
/**
|
|
|
32 |
* @var int Current end time of time limit (-1 if not set)
|
|
|
33 |
*/
|
|
|
34 |
protected static $currentend = -1;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* @var array Data for unit testing
|
|
|
38 |
*/
|
|
|
39 |
protected static $unittestdata = array();
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Sets the PHP time limit to a number of seconds from now.
|
|
|
43 |
*
|
|
|
44 |
* This function will always extend the time limit (in other words, if the time
|
|
|
45 |
* limit has already been set further in the future, it will do nothing).
|
|
|
46 |
*
|
|
|
47 |
* In order to support front-end servers which may time out silently if no
|
|
|
48 |
* output is displayed, you should ideally only call this function if you expect
|
|
|
49 |
* some output to be displayed at the same time. (I.e. if you call this function
|
|
|
50 |
* each time around a loop, also display some output each time around the loop,
|
|
|
51 |
* such as a progress bar update.)
|
|
|
52 |
*
|
|
|
53 |
* @param int $newlimit Limit in seconds from now (0 = infinite)
|
|
|
54 |
*/
|
|
|
55 |
public static function raise($newlimit = 0) {
|
|
|
56 |
global $CFG;
|
|
|
57 |
|
|
|
58 |
// Special behaviour in unit tests so that we can check the value.
|
|
|
59 |
if (PHPUNIT_TEST) {
|
|
|
60 |
self::$unittestdata[] = $newlimit;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// If the time limit has already been set to 'infinite', ignore. Also do
|
|
|
64 |
// nothing in CLI scripts (including unit testing) which are set to
|
|
|
65 |
// infinite by default.
|
|
|
66 |
if (self::$currentend === 0 || CLI_SCRIPT) {
|
|
|
67 |
return;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
// Maximum time limit can be set in config. This can be useful for front-end
|
|
|
71 |
// server systems; if the front-end server has a timeout without receiving
|
|
|
72 |
// data, it's helpful to set this timeout lower to ensure that a suitable
|
|
|
73 |
// error gets logged.
|
|
|
74 |
if (!empty($CFG->maxtimelimit)) {
|
|
|
75 |
$realtimeout = max(1, $CFG->maxtimelimit);
|
|
|
76 |
if ($newlimit === 0) {
|
|
|
77 |
$newlimit = $realtimeout;
|
|
|
78 |
} else {
|
|
|
79 |
$newlimit = min($newlimit, $realtimeout);
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
// If new time limit is infinite, just set that.
|
|
|
84 |
if ($newlimit === 0) {
|
|
|
85 |
self::$currentend = 0;
|
|
|
86 |
@set_time_limit(0);
|
|
|
87 |
return;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
// Calculate time limits to make sure it's longer than previous.
|
|
|
91 |
$now = time();
|
|
|
92 |
$newend = $now + $newlimit;
|
|
|
93 |
if (self::$currentend !== -1 && self::$currentend > $newend) {
|
|
|
94 |
// Existing time limit is already longer, so do nothing.
|
|
|
95 |
return;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
// Set time limit and update current value.
|
|
|
99 |
@set_time_limit($newlimit);
|
|
|
100 |
self::$currentend = $newend;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* For unit testing, returns an array of the values set during test.
|
|
|
105 |
*
|
|
|
106 |
* @return array Array of values set
|
|
|
107 |
*/
|
|
|
108 |
public static function get_and_clear_unit_test_data() {
|
|
|
109 |
$data = self::$unittestdata;
|
|
|
110 |
self::$unittestdata = array();
|
|
|
111 |
return $data;
|
|
|
112 |
}
|
|
|
113 |
}
|