MDL-73764 tool_xmldb: New report to show all xml files needing reconcile
People uses to edit the install.xml files manually, here and there. That uses to come with small mistakes, like wrong white-space indentation, some attributes out of order... While none of them are critical, they add a lot of noise when somebody is correctly editing a file with the XMLDB Editor because it, correctly, overwrites the whole file and then diffs show a lot of unrelated changes. So, this report will inform us about any file needing to be regenerated because it has been manually edited (in a different way than the way the editor has). Note that the report is very basic, with minimal ouput, manually generating the HTML, like the rest of the XMLDB Editor actions do. We are not using renderers neither templates here. Also note that it includes a commented line of code that, once uncommented, enables the report to, also, fix the wrong files. Useful for developers.
This commit is contained in:
@@ -57,6 +57,7 @@ class main_view extends XMLDBAction {
|
||||
'checkforeignkeys' => 'tool_xmldb',
|
||||
'checkbigints' => 'tool_xmldb',
|
||||
'checkoraclesemantics' => 'tool_xmldb',
|
||||
'reconcilefiles' => 'tool_xmldb',
|
||||
'doc' => 'tool_xmldb',
|
||||
'filemodifiedoutfromeditor' => 'tool_xmldb',
|
||||
'viewxml' => 'tool_xmldb',
|
||||
@@ -97,6 +98,8 @@ class main_view extends XMLDBAction {
|
||||
$b .= ' <a href="index.php?action=view_reserved_words">[' . $this->str['reservedwords'] . ']</a>';
|
||||
// The docs button
|
||||
$b .= ' <a href="index.php?action=generate_all_documentation">[' . $this->str['doc'] . ']</a>';
|
||||
// The reconcile XMLDB files button.
|
||||
$b .= ' <a href="index.php?action=reconcile_files">[' . $this->str['reconcilefiles'] . ']</a>';
|
||||
// The check indexes button
|
||||
$b .= ' <a href="index.php?action=check_indexes&sesskey=' . sesskey() . '">[' . $this->str['checkindexes'] . ']</a>';
|
||||
// The check defaults button
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
// This file is part of Moodle - https://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* List all the files needing reconcile because the definitions don't match the XML contents.
|
||||
*
|
||||
* @package tool_xmldb
|
||||
* @copyright 2022 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
|
||||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class reconcile_files extends XMLDBAction {
|
||||
|
||||
/**
|
||||
* Init method, every subclass will have its own
|
||||
*/
|
||||
public function init() {
|
||||
parent::init();
|
||||
|
||||
// Set own custom attributes.
|
||||
$this->sesskey_protected = false; // This action doesn't need sesskey protection.
|
||||
|
||||
// Get needed strings.
|
||||
$this->loadStrings([
|
||||
'backtomainview' => 'tool_xmldb',
|
||||
'reconcile_files_intro' => 'tool_xmldb',
|
||||
'reconcile_files_no' => 'tool_xmldb',
|
||||
'reconcile_files_yes' => 'tool_xmldb',
|
||||
'searchresults' => 'tool_xmldb',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke method, every class will have its own
|
||||
* returns true/false on completion, setting both
|
||||
* errormsg and output as necessary
|
||||
*/
|
||||
public function invoke() {
|
||||
parent::invoke();
|
||||
|
||||
$result = true;
|
||||
|
||||
// Set own core attributes.
|
||||
$this->does_generate = ACTION_GENERATE_HTML;
|
||||
|
||||
// These are always here.
|
||||
global $CFG, $XMLDB;
|
||||
|
||||
// Do the job, setting $result as needed.
|
||||
|
||||
// Add link back to home.
|
||||
$b = ' <p class="centerpara">';
|
||||
$b .= ' <a href="index.php?action=main_view#lastused">[' . $this->str['backtomainview'] . ']</a>';
|
||||
$b .= '</p>';
|
||||
$this->output .= $b;
|
||||
|
||||
$c = '<p class="centerpara">';
|
||||
$c .= $this->str['reconcile_files_intro'];
|
||||
$c .= '</p>';
|
||||
$this->output .= $c;
|
||||
|
||||
// Get the list of DB directories.
|
||||
$result = $this->launch('get_db_directories');
|
||||
if ($result && !empty($XMLDB->dbdirs)) {
|
||||
$needfix = [];
|
||||
foreach ($XMLDB->dbdirs as $key => $dbdir) {
|
||||
// Verify it exists.
|
||||
if (!file_exists($key . '/install.xml') && !is_readable($key . '/install.xml')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Read the raw contents of the file.
|
||||
$rawcontents = file_get_contents($key . '/install.xml');
|
||||
|
||||
// Load the XMLDB file and its structure.
|
||||
$xmldb = new xmldb_file($key . '/install.xml');
|
||||
$xmldb->loadXMLStructure();
|
||||
|
||||
// Generate the XML contents from the loaded structure.
|
||||
$xmlcontents = $xmldb->getStructure()->xmlOutput();
|
||||
|
||||
if ($rawcontents != $xmlcontents) {
|
||||
$relpath = str_replace($CFG->dirroot . '/', '', $key) . '/install.xml';
|
||||
$needfix[] = $relpath;
|
||||
// Left here on purpose, as a quick way to fix problems. To be
|
||||
// enabled and run by developers only, uncomment the next line:
|
||||
// file_put_contents($key . '/install.xml', $xmlcontents);
|
||||
// (this script won't ever do that officially).
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$r = '<h3 class="main">' . $this->str['searchresults'] . '</h3>';
|
||||
if ($needfix) {
|
||||
$r .= '<h4 class="main">' . $this->str['reconcile_files_yes'] . count($needfix) . '</h4>';
|
||||
$r .= '<ul><li>' . implode('</li><li>', $needfix) . '</li></ul>';
|
||||
|
||||
} else {
|
||||
$r .= '<h4 class="main">' . $this->str['reconcile_files_no'] . '</h4>';
|
||||
}
|
||||
|
||||
// Set the output.
|
||||
$this->output .= $r;
|
||||
|
||||
// Launch postaction if exists (leave this unmodified).
|
||||
if ($this->getPostAction() && $result) {
|
||||
return $this->launch($this->getPostAction());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -176,6 +176,13 @@ $string['persistentfieldscomplete'] = 'The following fields have been added: ';
|
||||
$string['persistentfieldsexist'] = 'The following fields already exist: ';
|
||||
$string['pluginname'] = 'XMLDB editor';
|
||||
$string['primarykeyonlyallownotnullfields'] = 'Primary keys cannot be null';
|
||||
$string['reconcilefiles'] = 'Reconcile XMLDB files';
|
||||
$string['reconcile_files'] = 'Look for XMLDB files needing reconcile';
|
||||
$string['reconcile_files_intro'] = 'This functionality looks for the current contents of all the XMLDB files, verifying that they match the results of generating them from the XMLDB Editor.
|
||||
|
||||
A list of files needing to be reconciled (regenerated) will be displayed and the XMLDB Editor should be used to fix them.';
|
||||
$string['reconcile_files_no'] = 'All files are ok, no reconcile action needed';
|
||||
$string['reconcile_files_yes'] = 'Found files to reconcile: ';
|
||||
$string['reserved'] = 'Reserved';
|
||||
$string['reservedwords'] = 'Reserved words';
|
||||
$string['revert'] = 'Revert';
|
||||
|
||||
Reference in New Issue
Block a user