From 0ce9d901538588225c009d9596c985c80ae90b4a Mon Sep 17 00:00:00 2001 From: dustinhuynh Date: Mon, 4 Aug 2025 09:55:36 +1000 Subject: [PATCH] Concerns about the handling of the original wwwroot #57 --- classes/admin_setting_configencodedtext.php | 78 +++++++++++++++++++++ db/install.php | 29 ++++++++ db/upgrade.php | 12 +++- index.php | 6 +- lang/en/local_datacleaner.php | 5 ++ settings.php | 8 +++ version.php | 4 +- 7 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 classes/admin_setting_configencodedtext.php create mode 100644 db/install.php diff --git a/classes/admin_setting_configencodedtext.php b/classes/admin_setting_configencodedtext.php new file mode 100644 index 0000000..485b658 --- /dev/null +++ b/classes/admin_setting_configencodedtext.php @@ -0,0 +1,78 @@ +. + +namespace local_datacleaner; + + +/** + * The text setting where input is encoded before save. + * + * @package local_datacleaner + * @author Dustin Huynh + * @copyright 2025, Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class admin_setting_configencodedtext extends \admin_setting_configtext { + + /** + * Return the setting + * + * @return mixed returns config if successful else null + */ + public function get_setting() { + global $CFG; + return base64_decode($CFG->original_wwwroot) ?? null; + } + + /** + * Write the setting + */ + public function write_setting($data) { + global $CFG; + if ($this->paramtype === PARAM_INT && $data === '') { + $data = 0; + } + $validated = $this->validate($data); + if ($validated !== true) { + return $validated; + } + $originalwwwroot = base64_encode($data); + return set_config('original_wwwroot', $originalwwwroot) ? '' : get_string('errorsetting', 'admin'); + } + + /** + * Return an XHTML string for the setting + * @return string Returns an XHTML string + */ + public function output_html($data, $query = '') { + global $CFG; + $elementid = $this->get_id(); + $textbox = parent::output_html($data, $query); + $resetbutton = \html_writer::tag('button', + get_string('resetbutton', 'local_datacleaner'), + [ + 'type' => 'button', + 'onclick' => "document.getElementById('{$elementid}').value = '{$CFG->wwwroot}'", + 'class' => 'btn btn-secondary', + ], + ); + $html = \html_writer::div( + $textbox . $resetbutton, + 'mb-3' + ); + return $html; + } +} diff --git a/db/install.php b/db/install.php new file mode 100644 index 0000000..a8b203c --- /dev/null +++ b/db/install.php @@ -0,0 +1,29 @@ +. + +/** + * Install script for databases. + * + * @package local_datacleaner + * @author Dustin Huynh + * @copyright 2025, Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +function xmldb_local_datacleaner_install() { + global $CFG; + $originalwwwroot = base64_encode($CFG->wwwroot); + set_config('original_wwwroot', $originalwwwroot); +} diff --git a/db/upgrade.php b/db/upgrade.php index 462f8de..0df772f 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -30,8 +30,8 @@ * @param int $oldversion * @return bool */ -function xmldb_local_datacleaner_upgrade($oldversion = 0): bool{ - global $DB; +function xmldb_local_datacleaner_upgrade($oldversion = 0): bool { + global $DB, $CFG; $dbman = $DB->get_manager(); if ($oldversion < 2022020301) { // Clean up table. @@ -43,5 +43,13 @@ function xmldb_local_datacleaner_upgrade($oldversion = 0): bool{ upgrade_plugin_savepoint(true, 2022020301, 'local', 'datacleaner'); } + if ($oldversion < 2022020302) { + if (!isset($CFG->original_wwwroot)) { + $originalwwwroot = base64_encode($CFG->wwwroot); + set_config('original_wwwroot', $originalwwwroot); + } + upgrade_plugin_savepoint(true, 2022020302, 'local', 'datacleaner'); + } + return true; } diff --git a/index.php b/index.php index 84331ee..2053306 100644 --- a/index.php +++ b/index.php @@ -27,8 +27,10 @@ admin_externalpage_setup('local_datacleaner'); // Save the wwwroot for checking from the CLI that we're not in prod. -$originalwwwroot = base64_encode($CFG->wwwroot); -set_config('original_wwwroot', $originalwwwroot); +if (!isset($CFG->original_wwwroot)) { + $originalwwwroot = base64_encode($CFG->wwwroot); + set_config('original_wwwroot', $originalwwwroot); +} // Allows the admin to configure subplugins (enable/disable, configure). diff --git a/lang/en/local_datacleaner.php b/lang/en/local_datacleaner.php index 3acb5dc..82b184e 100644 --- a/lang/en/local_datacleaner.php +++ b/lang/en/local_datacleaner.php @@ -47,3 +47,8 @@ controls the threshold above which the relationship will not be created (which also means records in the target table will not be deleted). The threshold is expressed as a percentage of the total number of records involved. If the total number of records in a table is less than 100, this value is ignored and any conflicts cause the rule not to be created.'; +$string['generalsettings'] = 'General settings'; +$string['original_wwwroot'] = 'Production Site URL'; +$string['original_wwwrootdesc'] = 'We store the hostname in the cleaning configuration data. If the hostname matches production, + DataCleaner will not run. If this data is missing then it will not run.'; +$string['resetbutton'] = 'Reset URL'; diff --git a/settings.php b/settings.php index dec372e..a41e838 100644 --- a/settings.php +++ b/settings.php @@ -34,6 +34,14 @@ get_string('manage', 'local_datacleaner'), new moodle_url('/local/datacleaner/index.php'))); + +$general = new admin_settingpage('generalsettings', new lang_string('generalsettings', 'local_datacleaner')); + +$general->add(new \local_datacleaner\admin_setting_configencodedtext('local_datacleaner/original_wwwroot', + new lang_string('original_wwwroot', 'local_datacleaner'), + new lang_string('original_wwwrootdesc', 'local_datacleaner'), $CFG->wwwroot, PARAM_URL)); +$ADMIN->add('datacleaner', $general); + $temp = new admin_settingpage('cascadedeletesettings', new lang_string('cascadedeletesettings', 'local_datacleaner')); $temp->add(new admin_setting_configtext('local_datacleaner/mismatch_threshold', diff --git a/version.php b/version.php index 7926062..537aa41 100644 --- a/version.php +++ b/version.php @@ -24,8 +24,8 @@ defined('MOODLE_INTERNAL') || die; -$plugin->version = 2022020301; -$plugin->release = '2.3.10'; +$plugin->version = 2022020302; +$plugin->release = 2022020302; $plugin->maturity = MATURITY_STABLE; $plugin->requires = 2021051700; // Moodle 3.11 release and upwards. $plugin->supports = [311, 405];