Skip to content

Commit 4916dd2

Browse files
dustinhuynhbrendanheywood
authored andcommitted
Concerns about the handling of the original wwwroot #57
1 parent cd744fc commit 4916dd2

File tree

7 files changed

+136
-6
lines changed

7 files changed

+136
-6
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16+
17+
namespace local_datacleaner;
18+
19+
20+
/**
21+
* The text setting where input is encoded before save.
22+
*
23+
* @package local_datacleaner
24+
* @author Dustin Huynh <[email protected]>
25+
* @copyright 2025, Catalyst IT
26+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27+
*/
28+
class admin_setting_configencodedtext extends \admin_setting_configtext {
29+
30+
/**
31+
* Return the setting
32+
*
33+
* @return mixed returns config if successful else null
34+
*/
35+
public function get_setting() {
36+
global $CFG;
37+
return base64_decode($CFG->original_wwwroot) ?? null;
38+
}
39+
40+
/**
41+
* Write the setting
42+
*/
43+
public function write_setting($data) {
44+
global $CFG;
45+
if ($this->paramtype === PARAM_INT && $data === '') {
46+
$data = 0;
47+
}
48+
$validated = $this->validate($data);
49+
if ($validated !== true) {
50+
return $validated;
51+
}
52+
$originalwwwroot = base64_encode($data);
53+
return set_config('original_wwwroot', $originalwwwroot) ? '' : get_string('errorsetting', 'admin');
54+
}
55+
56+
/**
57+
* Return an XHTML string for the setting
58+
* @return string Returns an XHTML string
59+
*/
60+
public function output_html($data, $query = '') {
61+
global $CFG;
62+
$elementid = $this->get_id();
63+
$textbox = parent::output_html($data, $query);
64+
$resetbutton = \html_writer::tag('button',
65+
get_string('resetbutton', 'local_datacleaner'),
66+
[
67+
'type' => 'button',
68+
'onclick' => "document.getElementById('{$elementid}').value = '{$CFG->wwwroot}'",
69+
'class' => 'btn btn-secondary',
70+
],
71+
);
72+
$html = \html_writer::div(
73+
$textbox . $resetbutton,
74+
'mb-3'
75+
);
76+
return $html;
77+
}
78+
}

db/install.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Install script for databases.
19+
*
20+
* @package local_datacleaner
21+
* @author Dustin Huynh <[email protected]>
22+
* @copyright 2025, Catalyst IT
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
function xmldb_local_datacleaner_install() {
26+
global $CFG;
27+
$originalwwwroot = base64_encode($CFG->wwwroot);
28+
set_config('original_wwwroot', $originalwwwroot);
29+
}

db/upgrade.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
* @param int $oldversion
3131
* @return bool
3232
*/
33-
function xmldb_local_datacleaner_upgrade($oldversion = 0): bool{
34-
global $DB;
33+
function xmldb_local_datacleaner_upgrade($oldversion = 0): bool {
34+
global $DB, $CFG;
3535
$dbman = $DB->get_manager();
3636
if ($oldversion < 2022020301) {
3737
// Clean up table.
@@ -43,5 +43,13 @@ function xmldb_local_datacleaner_upgrade($oldversion = 0): bool{
4343
upgrade_plugin_savepoint(true, 2022020301, 'local', 'datacleaner');
4444
}
4545

46+
if ($oldversion < 2022020302) {
47+
if (!isset($CFG->original_wwwroot)) {
48+
$originalwwwroot = base64_encode($CFG->wwwroot);
49+
set_config('original_wwwroot', $originalwwwroot);
50+
}
51+
upgrade_plugin_savepoint(true, 2022020302, 'local', 'datacleaner');
52+
}
53+
4654
return true;
4755
}

index.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
admin_externalpage_setup('local_datacleaner');
2828

2929
// Save the wwwroot for checking from the CLI that we're not in prod.
30-
$originalwwwroot = base64_encode($CFG->wwwroot);
31-
set_config('original_wwwroot', $originalwwwroot);
30+
if (!isset($CFG->original_wwwroot)) {
31+
$originalwwwroot = base64_encode($CFG->wwwroot);
32+
set_config('original_wwwroot', $originalwwwroot);
33+
}
3234

3335
// Allows the admin to configure subplugins (enable/disable, configure).
3436

lang/en/local_datacleaner.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@
4747
controls the threshold above which the relationship will not be created (which also means records in the target table will not be
4848
deleted). The threshold is expressed as a percentage of the total number of records involved. If the total number of records in a
4949
table is less than 100, this value is ignored and any conflicts cause the rule not to be created.';
50+
$string['generalsettings'] = 'General settings';
51+
$string['original_wwwroot'] = 'Production Site URL';
52+
$string['original_wwwrootdesc'] = 'We store the hostname in the cleaning configuration data. If the hostname matches production,
53+
DataCleaner will not run. If this data is missing then it will not run.';
54+
$string['resetbutton'] = 'Reset URL';

settings.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
3434
get_string('manage', 'local_datacleaner'),
3535
new moodle_url('/local/datacleaner/index.php')));
3636

37+
38+
$general = new admin_settingpage('generalsettings', new lang_string('generalsettings', 'local_datacleaner'));
39+
40+
$general->add(new \local_datacleaner\admin_setting_configencodedtext('local_datacleaner/original_wwwroot',
41+
new lang_string('original_wwwroot', 'local_datacleaner'),
42+
new lang_string('original_wwwrootdesc', 'local_datacleaner'), $CFG->wwwroot, PARAM_URL));
43+
$ADMIN->add('datacleaner', $general);
44+
3745
$temp = new admin_settingpage('cascadedeletesettings', new lang_string('cascadedeletesettings', 'local_datacleaner'));
3846

3947
$temp->add(new admin_setting_configtext('local_datacleaner/mismatch_threshold',

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
defined('MOODLE_INTERNAL') || die;
2626

27-
$plugin->version = 2022020301;
28-
$plugin->release = '2.3.10';
27+
$plugin->version = 2022020302;
28+
$plugin->release = 2022020302;
2929
$plugin->maturity = MATURITY_STABLE;
3030
$plugin->requires = 2021051700; // Moodle 3.11 release and upwards.
3131
$plugin->supports = [311, 405];

0 commit comments

Comments
 (0)