Skip to content

Commit 4053c92

Browse files
author
dustinhuynh
committed
Concerns about the handling of the original wwwroot #57
1 parent cd744fc commit 4053c92

File tree

9 files changed

+235
-6
lines changed

9 files changed

+235
-6
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
* The action button in config page.
20+
*
21+
* @package local_datacleaner
22+
* @author Dustin Huynh <[email protected]>
23+
* @copyright 2025, Catalyst IT
24+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25+
*/
26+
class admin_setting_configbutton extends \admin_setting {
27+
28+
/** @var string Button label */
29+
protected $label;
30+
/** @var string Button href */
31+
protected $href;
32+
33+
/**
34+
* A button element
35+
*
36+
* @param string $name unique ascii name.
37+
* @param string $visiblename heading
38+
* @param string $description description of what the button does
39+
* @param string $label what is written on the button
40+
* @param string $href the URL directed to on click
41+
*/
42+
public function __construct($name, $visiblename, $description, $label, $href) {
43+
$this->nosave = true;
44+
$this->label = $label;
45+
$this->href = $href;
46+
parent::__construct($name, $visiblename, $description, '');
47+
}
48+
49+
/**
50+
* Always returns true
51+
* @return bool Always returns true
52+
*/
53+
public function get_setting() {
54+
return true;
55+
}
56+
57+
/**
58+
* Never write settings
59+
* @return string Always returns an empty string
60+
*/
61+
public function write_setting($data) {
62+
return '';
63+
}
64+
65+
/**
66+
* Returns an HTML string
67+
* @param mixed $data
68+
* @param string $query
69+
* @return string Returns an HTML string
70+
*/
71+
public function output_html($data, $query = '') {
72+
$button = \html_writer::tag('a',
73+
$this->label,
74+
[
75+
'href' => $this->href,
76+
'style' => 'margin-top: 10px; display: inline-block;',
77+
]
78+
);
79+
return format_admin_setting($this, $this->visiblename, $button, $this->description);
80+
}
81+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,11 @@
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 the original site URL';
55+
$string['resetbuttondesc'] = 'Mark this website as the production site URL.';
56+
$string['resetbuttonlabel'] = 'Reset URL';
57+
$string['resetsuccess'] = 'Production Site URL successfully reset.';

reset.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
require_once('../../config.php');
18+
19+
require_login();
20+
require_capability('moodle/site:config', context_system::instance());
21+
require_sesskey();
22+
global $CFG;
23+
$originalwwwroot = base64_encode($CFG->wwwroot);
24+
set_config('original_wwwroot', $originalwwwroot);
25+
26+
redirect(
27+
new moodle_url('/admin/settings.php', ['section' => 'generalsettings']),
28+
get_string('resetsuccess', 'local_datacleaner'),
29+
null,
30+
\core\output\notification::NOTIFY_SUCCESS
31+
);

settings.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@
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+
44+
$general->add(new \local_datacleaner\admin_setting_configbutton( 'local_datacleaner/resetbutton',
45+
get_string('resetbutton', 'local_datacleaner'),
46+
get_string('resetbuttondesc', 'local_datacleaner'),
47+
get_string('resetbuttonlabel', 'local_datacleaner'),
48+
new moodle_url('/local/datacleaner/reset.php', ['sesskey' => sesskey()])));
49+
$ADMIN->add('datacleaner', $general);
50+
3751
$temp = new admin_settingpage('cascadedeletesettings', new lang_string('cascadedeletesettings', 'local_datacleaner'));
3852

3953
$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 = '2.3.11';
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)