Skip to content

Commit 5af18c3

Browse files
author
David Monllaó
committed
Merge branch 'MDL-63706_35' of git://github.com/markn86/moodle into MOODLE_35_STABLE
2 parents fb911d3 + 6649b4f commit 5af18c3

File tree

10 files changed

+112
-40
lines changed

10 files changed

+112
-40
lines changed

admin/settings/server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
// "systempaths" settingpage
99
$temp = new admin_settingpage('systempaths', new lang_string('systempaths','admin'));
10-
10+
$temp->add(new admin_setting_configexecutable('pathtophp', new lang_string('pathtophp', 'admin'),
11+
new lang_string('configpathtophp', 'admin'), ''));
1112
$temp->add(new admin_setting_configexecutable('pathtodu', new lang_string('pathtodu', 'admin'), new lang_string('configpathtodu', 'admin'), ''));
1213
$temp->add(new admin_setting_configexecutable('aspellpath', new lang_string('aspellpath', 'admin'), new lang_string('edhelpaspellpath'), ''));
1314
$temp->add(new admin_setting_configexecutable('pathtodot', new lang_string('pathtodot', 'admin'), new lang_string('pathtodot_help', 'admin'), ''));
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
* Form for scheduled tasks admin pages.
19+
*
20+
* @package tool_task
21+
* @copyright 2018 Toni Barbera <toni@moodle.com>
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace tool_task;
26+
27+
defined('MOODLE_INTERNAL') || die();
28+
29+
/**
30+
* Running tasks from CLI.
31+
*
32+
* @copyright 2018 Toni Barbera <toni@moodle.com>
33+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34+
*/
35+
class run_from_cli {
36+
37+
/**
38+
* Find the path of PHP CLI binary.
39+
*
40+
* @return string|false The PHP CLI executable PATH
41+
*/
42+
protected static function find_php_cli_path() {
43+
global $CFG;
44+
45+
if (!empty($CFG->pathtophp) && is_executable(trim($CFG->pathtophp))) {
46+
return $CFG->pathtophp;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Returns if Moodle have access to PHP CLI binary or not.
54+
*
55+
* @return bool
56+
*/
57+
public static function is_runnable():bool {
58+
return self::find_php_cli_path() !== false;
59+
}
60+
61+
/**
62+
* Executes a cron from web invocation using PHP CLI.
63+
*
64+
* @param \core\task\task_base $task Task that be executed via CLI.
65+
* @return bool
66+
* @throws \moodle_exception
67+
*/
68+
public static function execute(\core\task\task_base $task):bool {
69+
global $CFG;
70+
71+
if (!self::is_runnable()) {
72+
$redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'systempaths']);
73+
throw new \moodle_exception('cannotfindthepathtothecli', 'tool_task', $redirecturl->out());
74+
} else {
75+
// Shell-escaped path to the PHP binary.
76+
$phpbinary = escapeshellarg(self::find_php_cli_path());
77+
78+
// Shell-escaped path CLI script.
79+
$pathcomponents = [$CFG->dirroot, $CFG->admin, 'tool', 'task', 'cli', 'schedule_task.php'];
80+
$scriptpath = escapeshellarg(implode(DIRECTORY_SEPARATOR, $pathcomponents));
81+
82+
// Shell-escaped task name.
83+
$classname = get_class($task);
84+
$taskarg = escapeshellarg("--execute={$classname}");
85+
86+
// Build the CLI command.
87+
$command = "{$phpbinary} {$scriptpath} {$taskarg}";
88+
89+
// Execute it.
90+
passthru($command);
91+
}
92+
93+
return true;
94+
}
95+
}

admin/tool/task/lang/en/tool_task.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
$string['asap'] = 'ASAP';
2626
$string['backtoscheduledtasks'] = 'Back to scheduled tasks';
2727
$string['blocking'] = 'Blocking';
28+
$string['cannotfindthepathtothecli'] = 'Cannot find the path to the PHP CLI executable so task execution aborted. Set the "Path to PHP CLI" setting in "Site administration / Server / System paths"';
2829
$string['clearfaildelay_confirm'] = 'Are you sure you want to clear the fail delay for task \'{$a}\'? After clearing the delay, the task will run according to its normal schedule.';
2930
$string['component'] = 'Component';
3031
$string['corecomponent'] = 'Core';
@@ -58,3 +59,4 @@
5859
$string['taskschedulemonth'] = 'Month';
5960
$string['taskschedulemonth_help'] = 'Month field for task schedule. The field uses the same format as unix cron. Some examples are:<br/><ul><li><strong>*</strong> Every month</li><li><strong>*/2</strong> Every second month</li><li><strong>1</strong> Every January</li><li><strong>1,5</strong> Every January and May</li></ul>';
6061
$string['privacy:metadata'] = 'The Scheduled task configuration plugin does not store any personal data.';
62+

admin/tool/task/renderer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function scheduled_tasks_table($tasks) {
6262
$asap = get_string('asap', 'tool_task');
6363
$disabledstr = get_string('taskdisabled', 'tool_task');
6464
$plugindisabledstr = get_string('plugindisabled', 'tool_task');
65+
$runnabletasks = tool_task\run_from_cli::is_runnable();
6566
foreach ($tasks as $task) {
6667
$customised = $task->is_customised() ? $no : $yes;
6768
if (empty($CFG->preventscheduledtaskchanges)) {
@@ -105,7 +106,7 @@ public function scheduled_tasks_table($tasks) {
105106
}
106107

107108
$runnow = '';
108-
if (!$disabled && get_config('tool_task', 'enablerunnow')) {
109+
if ( ! $disabled && get_config('tool_task', 'enablerunnow') && $runnabletasks ) {
109110
$runnow = html_writer::div(html_writer::link(
110111
new moodle_url('/admin/tool/task/schedule_task.php',
111112
array('task' => get_class($task))),

admin/tool/task/schedule_task.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ function tool_task_mtrace_wrapper($message, $eol) {
8888
$CFG->mtrace_wrapper = 'tool_task_mtrace_wrapper';
8989

9090
// Run the specified task (this will output an error if it doesn't exist).
91-
cron_run_single_task($task);
91+
\tool_task\run_from_cli::execute($task);
92+
9293
echo html_writer::end_tag('pre');
9394

9495
$output = $PAGE->get_renderer('tool_task');

admin/tool/task/tests/behat/run_task_now.feature

Lines changed: 0 additions & 35 deletions
This file was deleted.

config-dist.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,11 @@
874874
// and 'gsdll32.dll' to a new folder without a space in the path)
875875
// $CFG->pathtogs = '/usr/bin/gs';
876876
//
877+
// Path to PHP CLI.
878+
// Probably something like /usr/bin/php. If you enter this, cron scripts can be
879+
// executed from admin web interface.
880+
// $CFG->pathtophp = '';
881+
//
877882
// Path to du.
878883
// Probably something like /usr/bin/du. If you enter this, pages that display
879884
// directory contents will run much faster for directories with a lot of files.

lang/en/admin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@
297297
$string['configpasswordpolicy'] = 'Turning this on will make Moodle check user passwords against a valid password policy. Use the settings below to specify your policy (they will be ignored if you set this to \'No\').';
298298
$string['configpasswordresettime'] = 'This specifies the amount of time people have to validate a password reset request before it expires. Usually 30 minutes is a good value.';
299299
$string['configpathtodu'] = 'Path to du. Probably something like /usr/bin/du. If you enter this, pages that display directory contents will run much faster for directories with a lot of files.';
300+
$string['configpathtophp'] = 'Path to PHP CLI. Probably something like /usr/bin/php. If you enter this, cron scripts can be executed from admin web interface.';
300301
$string['configperfdebug'] = 'If you turn this on, performance info will be printed in the footer of the standard theme';
301302
$string['configprofileroles'] = 'List of roles that are visible on user profiles and participation page.';
302303
$string['configprofilesforenrolledusersonly'] = 'To prevent misuse by spammers, profile descriptions of users who are not yet enrolled in any course are hidden. New users must enrol in at least one course before they can add a profile description.';
@@ -837,6 +838,7 @@
837838
$string['passwordreuselimit_desc'] = 'Number of times a user must change their password before they are allowed to reuse a password. Hashes of previously used passwords are stored in local database table. This feature might not be compatible with some external authentication plugins.';
838839
$string['pathtodot'] = 'Path to dot';
839840
$string['pathtodot_help'] = 'Path to dot. On Linux it is something like /usr/bin/dot. On Windows it is something like C:\Program Files (x86)\Graphviz2.38\bin\dot.exe. On Mac it is something like /opt/local/bin/dot. To be able to generate graphics from DOT files, you must have installed the dot executable and point to it here.';
841+
$string['pathtophp'] = 'Path to PHP CLI';
840842
$string['pathtodu'] = 'Path to du';
841843
$string['pathtogs'] = 'Path to ghostscript';
842844
$string['pathtogs_help'] = 'On most Linux installs, this can be left as \'/usr/bin/gs\'. On Windows it will be something like \'c:\\gs\\bin\\gswin32c.exe\' (make sure there are no spaces in the path - if necessary copy the files \'gswin32c.exe\' and \'gsdll32.dll\' to a new folder without a space in the path)';

lib/behat/lib.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ function behat_clean_init_config() {
219219
'wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
220220
'umaskpermissions', 'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix',
221221
'dboptions', 'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword',
222-
'proxybypass', 'pathtogs', 'pathtodu', 'aspellpath', 'pathtodot', 'skiplangupgrade',
222+
'proxybypass', 'pathtogs', 'pathtophp', 'pathtodu', 'aspellpath', 'pathtodot', 'skiplangupgrade',
223223
'altcacheconfigpath', 'pathtounoconv', 'alternative_file_system_class', 'pathtopython'
224224
));
225225

lib/phpunit/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181
$allowed = array('wwwroot', 'dataroot', 'dirroot', 'admin', 'directorypermissions', 'filepermissions',
182182
'dbtype', 'dblibrary', 'dbhost', 'dbname', 'dbuser', 'dbpass', 'prefix', 'dboptions',
183183
'proxyhost', 'proxyport', 'proxytype', 'proxyuser', 'proxypassword', 'proxybypass', // keep proxy settings from config.php
184-
'altcacheconfigpath', 'pathtogs', 'pathtodu', 'aspellpath', 'pathtodot',
184+
'altcacheconfigpath', 'pathtogs', 'pathtphp', 'pathtodu', 'aspellpath', 'pathtodot',
185185
'pathtounoconv', 'alternative_file_system_class', 'pathtopython'
186186
);
187187
$productioncfg = (array)$CFG;

0 commit comments

Comments
 (0)