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 * Fix orphaned calendar events that were broken by MDL-67494. 19 * 20 * This script will look for all the calendar events which userids 21 * where broken by a wrong upgrade step, affecting to Moodle 3.9.5 22 * and up. 23 * 24 * It performs checks to both: 25 * a) Detect if the site was affected (ran the wrong upgrade step). 26 * b) Look for orphaned calendar events, categorising them as: 27 * - standard: site / category / course / group / user events 28 * - subscription: events created via subscriptions. 29 * - action: normal action events, created to show common important dates. 30 * - override: user and group override events, particular, that some activities support. 31 * - custom: other events, not being any of the above, common or particular. 32 * By specifying it (--fix) try to recover as many broken events (missing userid) as 33 * possible. Standard, subscription, action, override events in core are fully supported but 34 * override or custom events should be fixed by each plugin as far as there isn't any standard 35 * API (plugin-wise) to launch a rebuild of the calendar events. 36 * 37 * @package core 38 * @copyright 2021 onwards Simey Lameze <simey@moodle.com> 39 * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41 42 define('CLI_SCRIPT', true); 43 44 require_once(__DIR__ . '/../../config.php'); 45 require_once($CFG->libdir . "/clilib.php"); 46 require_once($CFG->libdir . '/db/upgradelib.php'); 47 48 // Supported options. 49 $long = ['fix' => false, 'help' => false]; 50 $short = ['f' => 'fix', 'h' => 'help']; 51 52 // CLI options. 53 [$options, $unrecognized] = cli_get_params($long, $short); 54 55 if ($unrecognized) { 56 $unrecognized = implode("\n ", $unrecognized); 57 cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); 58 } 59 60 if ($options['help']) { 61 $help = <<<EOT 62 Fix orphaned calendar events. 63 64 This script detects calendar events that have had their 65 userid lost. By default it will perform various checks 66 and report them, showing the site status in an easy way. 67 68 Also, optionally (--fix), it wil try to recover as many 69 lost userids as possible from different sources. Note that 70 this script aims to process well-know events in core, 71 leaving custom events in 3rd part plugins mostly unmodified 72 because there isn't any consistent way to regenerate them. 73 74 For more details: https://tracker.moodle.org/browse/MDL-71156 75 76 Options: 77 -h, --help Print out this help. 78 -f, --fix Fix the orphaned calendar events in the DB. 79 If not specified only check and report problems to STDERR. 80 81 Usage: 82 - Only report: \$ sudo -u www-data /usr/bin/php admin/cli/fix_orphaned_calendar_events.php 83 - Report and fix: \$ sudo -u www-data /usr/bin/php admin/cli/fix_orphaned_calendar_events.php -f 84 EOT; 85 86 cli_writeln($help); 87 die; 88 } 89 90 // Check various usual pre-requisites. 91 if (empty($CFG->version)) { 92 cli_error('Database is not yet installed.'); 93 } 94 95 $admin = get_admin(); 96 if (!$admin) { 97 cli_error('Error: No admin account was found.'); 98 } 99 100 if (moodle_needs_upgrading()) { 101 cli_error('Moodle upgrade pending, script execution suspended.'); 102 } 103 104 // Do everything as admin by default. 105 \core\session\manager::set_user($admin); 106 107 // Report current site status. 108 cli_heading('Checking the site status'); 109 $needsfix = upgrade_calendar_site_status(); 110 111 // Report current calendar events status. 112 cli_heading('Checking the calendar events status'); 113 $info = upgrade_calendar_events_status(); 114 $hasbadevents = $info['total']->bad > 0 || $info['total']->bad != $info['other']->bad; 115 $needsfix = $needsfix || $hasbadevents; 116 117 // If, selected, fix as many calendar events as possible. 118 if ($options['fix']) { 119 120 // If the report has told us that the fix was not needed... ask for confirmation! 121 if (!$needsfix) { 122 cli_writeln("This site DOES NOT NEED to run the calendar events fix."); 123 $input = cli_input('Are you completely sure that you want to run the fix? (y/N)', 'N', ['y', 'Y', 'n', 'N']); 124 if (strtolower($input) != 'y') { 125 exit(0); 126 } 127 cli_writeln(""); 128 } 129 cli_heading('Fixing as many as possible calendar events'); 130 upgrade_calendar_events_fix_remaining($info); 131 // Report current (after fix) calendar events status. 132 cli_heading('Checking the calendar events status (after fix)'); 133 upgrade_calendar_events_status(); 134 } else if ($needsfix) { 135 // Fix option was not provided but problem events have been found. Notify the user and provide info how to fix these events. 136 cli_writeln("This site NEEDS to run the calendar events fix!"); 137 cli_writeln("To fix the calendar events, re-run this script with the --fix option."); 138 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body