Skip to content

Commit

Permalink
MDL-69764 tool_task: unify crontab verification
Browse files Browse the repository at this point in the history
Use scheduled_task crontab field verification in
admin/tool/task to unify how Moodle deals with
crontab definition and its verification.
This helps remove duplicated code and fix
crontab definition not allowed in the web form,
but actually was valid.

Updated crontab fields precision on task_scheduled
table to have enough room for the worst case:
all possible different values separated by comma.
  • Loading branch information
jpahullo committed Feb 17, 2022
1 parent d24a4ab commit 21f73b2
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 261 deletions.
69 changes: 25 additions & 44 deletions admin/tool/task/classes/edit_scheduled_task_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,52 +121,33 @@ public function definition() {
*/
public function validation($data, $files) {
$error = parent::validation($data, $files);
$fields = array('minute', 'hour', 'day', 'month', 'dayofweek');
foreach ($fields as $field) {
if (!self::validate_fields($field, $data[$field])) {
$error[$field . 'group'] = get_string('invaliddata', 'core_error');
}
// Use a checker class.
$checker = new \tool_task\scheduled_checker_task();
$checker->set_minute($data['minute']);
$checker->set_hour($data['hour']);
$checker->set_month($data['month']);
$checker->set_day_of_week($data['dayofweek']);
$checker->set_day($data['day']);
$checker->set_disabled(false);
$checker->set_customised(false);

if (!$checker->is_valid($checker::FIELD_MINUTE)) {
$error['minutegroup'] = get_string('invaliddata', 'core_error');
}
return $error;
}

/**
* Helper function that validates the submitted data.
*
* Explanation of the regex:-
*
* \A\*\z - matches *
* \A[0-5]?[0-9]\z - matches entries like 23
* \A\*\/[0-5]?[0-9]\z - matches entries like * / 5
* \A[0-5]?[0-9](,[0-5]?[0-9])*\z - matches entries like 1,2,3
* \A[0-5]?[0-9]-[0-5]?[0-9]\z - matches entries like 2-10
*
* @param string $field field to validate
* @param string $value value
*
* @return bool true if validation passes, false other wise.
*/
public static function validate_fields($field, $value) {
switch ($field) {
case 'minute' :
case 'hour' :
$regex = "/\A\*\z|\A[0-5]?[0-9]\z|\A\*\/[0-5]?[0-9]\z|\A[0-5]?[0-9](,[0-5]?[0-9])*\z|\A[0-5]?[0-9]-[0-5]?[0-9]\z/";
break;
case 'day':
$regex = "/\A\*\z|\A([1-2]?[0-9]|3[0-1])\z|\A\*\/([1-2]?[0-9]|3[0-1])\z|";
$regex .= "\A([1-2]?[0-9]|3[0-1])(,([1-2]?[0-9]|3[0-1]))*\z|\A([1-2]?[0-9]|3[0-1])-([1-2]?[0-9]|3[0-1])\z/";
break;
case 'month':
$regex = "/\A\*\z|\A([0-9]|1[0-2])\z|\A\*\/([0-9]|1[0-2])\z|\A([0-9]|1[0-2])(,([0-9]|1[0-2]))*\z|";
$regex .= "\A([0-9]|1[0-2])-([0-9]|1[0-2])\z/";
break;
case 'dayofweek':
$regex = "/\A\*\z|\A[0-6]\z|\A\*\/[0-6]\z|\A[0-6](,[0-6])*\z|\A[0-6]-[0-6]\z/";
break;
default:
return false;
if (!$checker->is_valid($checker::FIELD_HOUR)) {
$error['hourgroup'] = get_string('invaliddata', 'core_error');
}
if (!$checker->is_valid($checker::FIELD_DAY)) {
$error['daygroup'] = get_string('invaliddata', 'core_error');
}
if (!$checker->is_valid($checker::FIELD_MONTH)) {
$error['monthgroup'] = get_string('invaliddata', 'core_error');
}
return (bool)preg_match($regex, $value);
if (!$checker->is_valid($checker::FIELD_DAYOFWEEK)) {
$error['dayofweekgroup'] = get_string('invaliddata', 'core_error');
}

return $error;
}
}

40 changes: 40 additions & 0 deletions admin/tool/task/classes/scheduled_checker_task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace tool_task;

/**
* Checker class. Fake scheduled task used only to check that crontab settings are valid.
*
* @package tool_task
* @copyright 2021 Jordi Pujol-Ahulló <jpahullo@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class scheduled_checker_task extends \core\task\scheduled_task {

/**
* Gets the checker task name.
*/
public function get_name() {
return "Checker task";
}

/**
* Does nothing.
*/
public function execute() {
}
}
Loading

0 comments on commit 21f73b2

Please sign in to comment.