diff --git a/classes/clean.php b/classes/clean.php index d6fc2b1..a927c59 100644 --- a/classes/clean.php +++ b/classes/clean.php @@ -398,4 +398,43 @@ protected static function execute_sql($sql) { public static function get_settings_section_url($sectionname) { return new \moodle_url('/admin/settings.php', array('section' => $sectionname)); } + + /** + * Log some context of where and why this was run. + */ + public static function debug_info() { + global $CFG; + + $context = "Time: " . \userdate(time()) . "\n"; + $context .= "TZ: " . $CFG->timezone . "\n"; + $context .= "Host: " . gethostname() . "\n"; + $context .= "Moodle User: " . $USER->username . "\n"; + $context .= "\$CFG->dbhost: " . $CFG->dbhost . "\n"; + $context .= "\$CFG->dbuser: " . $CFG->dbuser . "\n"; + $context .= "\$CFG->dataroot: " . $CFG->dataroot . "\n"; + $context .= "\$CFG->wwwroot: " . $CFG->wwwroot . "\n"; + $context .= "\$CFG->original_wwwroot: " . $CFG->original_wwwroot . "\n"; + + self::log($context); + + // Also set this in the DB. + set_config('lastwash', $context, 'local_datacleaner'); + + } + + /** + * Log details in a variety of places + * + * @param string + */ + public static function log($string) { + global $CFG; + + // Send it to stderr. + error_log($string); + + // Stash a copy into sitedir log file. + mkdir("$CFG->dataroot/datacleaner"); + file_put_contents("$CFG->dataroot/datacleaner/clean.log", $string, FILE_APPEND); + } } diff --git a/cli/clean.php b/cli/clean.php index 12feba9..c2abd9f 100644 --- a/cli/clean.php +++ b/cli/clean.php @@ -26,6 +26,8 @@ require_once($CFG->libdir.'/adminlib.php'); require_once(dirname(__FILE__) . '/lib.php'); +use local_datacleaner\clean; + // Now get cli options. list($options, $unrecognized) = cli_get_params( array( @@ -102,16 +104,18 @@ } if ($options['dryrun']) { - echo "=== DRY RUN ===\n"; + clean::log("=== DRY RUN ===\n"); } $filter = $options['filter']; if ($filter) { - echo "Filtering to ONLY run: $filter \n"; + clean::log("Filtering to ONLY run: $filter \n"); } $cascade = null; +local_datacleaner\clean::debug_info(); + foreach ($plugins as $plugin) { // Get the class that does the work. $classname = 'cleaner_' . $plugin->name . '\clean'; @@ -127,7 +131,7 @@ // Skip subplugins that have a sort order that is greater or equal to 200. if ($options['run-pre-wash']) { if ($plugin->sortorder >= 200) { - echo "NOTICE: Pre washing only. Skipping {$plugin->name} ({$plugin->sortorder}) cleaner.\n"; + clean::log("NOTICE: Pre washing only. Skipping {$plugin->name} ({$plugin->sortorder}) cleaner.\n"); continue; } } @@ -136,18 +140,18 @@ // Skip subplugins that have a sort order that is less than 200. if ($options['run-post-wash']) { if ($plugin->sortorder < 200) { - echo "NOTICE: Post washing only. Skipping {$plugin->name} ({$plugin->sortorder}) cleaner.\n"; + clean::log("NOTICE: Post washing only. Skipping {$plugin->name} ({$plugin->sortorder}) cleaner.\n"); continue; } } - echo "== Running {$plugin->name} cleaner ==\n"; if (!class_exists($classname)) { - echo "ERROR: Unable to locate local/datacleaner/cleaner/{$plugin->name}/classes/clean.php class. Skipping.\n"; + clean::log("ERROR: Unable to locate local/datacleaner/cleaner/{$plugin->name}/classes/clean.php class. Skipping.\n"); continue; } $class = new $classname($options); + clean::log("== Running {$plugin->name} cleaner ==\n"); if (is_null($cascade) && $class->needs_cascade_delete()) { $cascade = new \local_datacleaner\schema_add_cascade_delete($options); @@ -160,4 +164,4 @@ $class->execute(); } -echo "Done.\n"; +clean::log("Done.\n"); diff --git a/cli/lib.php b/cli/lib.php index 45b5044..7d2614c 100644 --- a/cli/lib.php +++ b/cli/lib.php @@ -23,6 +23,8 @@ defined('MOODLE_INTERNAL') || die(); +use local_datacleaner\clean; + /** * Print a message to the terminal. * @@ -38,6 +40,7 @@ function print_message($text, $highlight = false) { } else { echo $text; } + clean::log($text); } /**