/admin/tool/xmldb/actions/reconcile_files/reconcile_files.class.php

https://github.com/sbourget/moodle · PHP · 124 lines · 57 code · 21 blank · 46 comment · 10 complexity · 401ae100de4eb6b97880f60aaba08638 MD5 · raw file

  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. * List all the files needing reconcile because the definitions don't match the XML contents.
  18. *
  19. * @package tool_xmldb
  20. * @copyright 2022 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
  21. * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. class reconcile_files extends XMLDBAction {
  24. /**
  25. * Init method, every subclass will have its own
  26. */
  27. public function init() {
  28. parent::init();
  29. // Set own custom attributes.
  30. $this->sesskey_protected = false; // This action doesn't need sesskey protection.
  31. // Get needed strings.
  32. $this->loadStrings([
  33. 'backtomainview' => 'tool_xmldb',
  34. 'reconcile_files_intro' => 'tool_xmldb',
  35. 'reconcile_files_no' => 'tool_xmldb',
  36. 'reconcile_files_yes' => 'tool_xmldb',
  37. 'searchresults' => 'tool_xmldb',
  38. ]);
  39. }
  40. /**
  41. * Invoke method, every class will have its own
  42. * returns true/false on completion, setting both
  43. * errormsg and output as necessary
  44. */
  45. public function invoke() {
  46. parent::invoke();
  47. $result = true;
  48. // Set own core attributes.
  49. $this->does_generate = ACTION_GENERATE_HTML;
  50. // These are always here.
  51. global $CFG, $XMLDB;
  52. // Do the job, setting $result as needed.
  53. // Add link back to home.
  54. $b = ' <p class="centerpara">';
  55. $b .= '&nbsp;<a href="index.php?action=main_view#lastused">[' . $this->str['backtomainview'] . ']</a>';
  56. $b .= '</p>';
  57. $this->output .= $b;
  58. $c = '<p class="centerpara">';
  59. $c .= $this->str['reconcile_files_intro'];
  60. $c .= '</p>';
  61. $this->output .= $c;
  62. // Get the list of DB directories.
  63. $result = $this->launch('get_db_directories');
  64. if ($result && !empty($XMLDB->dbdirs)) {
  65. $needfix = [];
  66. foreach ($XMLDB->dbdirs as $key => $dbdir) {
  67. // Verify it exists.
  68. if (!file_exists($key . '/install.xml') && !is_readable($key . '/install.xml')) {
  69. continue;
  70. }
  71. // Read the raw contents of the file.
  72. $rawcontents = file_get_contents($key . '/install.xml');
  73. // Load the XMLDB file and its structure.
  74. $xmldb = new xmldb_file($key . '/install.xml');
  75. $xmldb->loadXMLStructure();
  76. // Generate the XML contents from the loaded structure.
  77. $xmlcontents = $xmldb->getStructure()->xmlOutput();
  78. if ($rawcontents != $xmlcontents) {
  79. $relpath = str_replace($CFG->dirroot . '/', '', $key) . '/install.xml';
  80. $needfix[] = $relpath;
  81. // Left here on purpose, as a quick way to fix problems. To be
  82. // enabled and run by developers only, uncomment the next line:
  83. // file_put_contents($key . '/install.xml', $xmlcontents);
  84. // (this script won't ever do that officially).
  85. }
  86. }
  87. }
  88. $r = '<h3 class="main">' . $this->str['searchresults'] . '</h3>';
  89. if ($needfix) {
  90. $r .= '<h4 class="main">' . $this->str['reconcile_files_yes'] . count($needfix) . '</h4>';
  91. $r .= '<ul><li>' . implode('</li><li>', $needfix) . '</li></ul>';
  92. } else {
  93. $r .= '<h4 class="main">' . $this->str['reconcile_files_no'] . '</h4>';
  94. }
  95. // Set the output.
  96. $this->output .= $r;
  97. // Launch postaction if exists (leave this unmodified).
  98. if ($this->getPostAction() && $result) {
  99. return $this->launch($this->getPostAction());
  100. }
  101. return $result;
  102. }
  103. }