From fad610045bb028801d7fddcf1019e3a64ddbfb4c Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 31 Mar 2020 21:08:07 +0200 Subject: [PATCH] MDL-67886 tool_xmldb: checks for extra indexes --- .../check_indexes/check_indexes.class.php | 86 +++++++++++++++---- admin/tool/xmldb/lang/en/tool_xmldb.php | 4 +- 2 files changed, 71 insertions(+), 19 deletions(-) diff --git a/admin/tool/xmldb/actions/check_indexes/check_indexes.class.php b/admin/tool/xmldb/actions/check_indexes/check_indexes.class.php index ba03bac82e3..b7c175ca229 100644 --- a/admin/tool/xmldb/actions/check_indexes/check_indexes.class.php +++ b/admin/tool/xmldb/actions/check_indexes/check_indexes.class.php @@ -44,11 +44,13 @@ class check_indexes extends XMLDBCheckAction { // Get needed strings $this->loadStrings(array( + 'extraindexesfound' => 'tool_xmldb', 'missing' => 'tool_xmldb', 'key' => 'tool_xmldb', 'index' => 'tool_xmldb', 'missingindexes' => 'tool_xmldb', - 'nomissingindexesfound' => 'tool_xmldb', + 'nomissingorextraindexesfound' => 'tool_xmldb', + 'yesextraindexesfound' => 'tool_xmldb', 'yesmissingindexesfound' => 'tool_xmldb', )); } @@ -58,6 +60,7 @@ class check_indexes extends XMLDBCheckAction { $dbman = $DB->get_manager(); $o = ''; + $dbindexes = $DB->get_indexes($xmldb_table->getName()); $missing_indexes = array(); // Keys @@ -88,6 +91,7 @@ class check_indexes extends XMLDBCheckAction { // Check if the index exists in DB if ($dbman->index_exists($xmldb_table, $xmldb_index)) { $o.='' . $this->str['ok'] . ''; + $this->remove_index_from_dbindex($dbindexes, $xmldb_index); } else { $o.='' . $this->str['missing'] . ''; // Add the missing index to the list @@ -109,6 +113,7 @@ class check_indexes extends XMLDBCheckAction { // Check if the index exists in DB if ($dbman->index_exists($xmldb_table, $xmldb_index)) { $o.='' . $this->str['ok'] . ''; + $this->remove_index_from_dbindex($dbindexes, $xmldb_index); } else { $o.='' . $this->str['missing'] . ''; // Add the missing index to the list @@ -122,6 +127,14 @@ class check_indexes extends XMLDBCheckAction { $o.=' '; } + // Hack - skip for table 'search_simpledb_index' as this plugin adds indexes dynamically on install + // which are not included in install.xml. See search/engine/simpledb/db/install.php. + if ($xmldb_table->getName() != 'search_simpledb_index') { + foreach ($dbindexes as $indexname => $index) { + $missing_indexes[] = $indexname; + } + } + return array($o, $missing_indexes); } @@ -129,33 +142,56 @@ class check_indexes extends XMLDBCheckAction { global $DB; $dbman = $DB->get_manager(); + $missingindexes = []; + $extraindexes = []; + + foreach ($missing_indexes as $missingindex) { + if (is_object($missingindex)) { + $missingindexes[] = $missingindex; + } else { + $extraindexes[] = $missingindex; + } + } + $s = ''; $r = ''; $r.= ' '; $r.= ' '; $r.= '
'; $r.= '

' . $this->str['searchresults'] . '

'; - $r.= '

' . $this->str['missingindexes'] . ': ' . count($missing_indexes) . '

'; + $r .= '

' . $this->str['missingindexes'] . ': ' . count($missingindexes) . '

'; + $r .= '

' . $this->str['extraindexesfound'] . ': ' . count($extraindexes) . '

'; $r.= '
'; - // If we have found missing indexes inform about them - if (count($missing_indexes)) { - $r.= '

' . $this->str['yesmissingindexesfound'] . '

'; - $r.= '
    '; - foreach ($missing_indexes as $obj) { - $xmldb_table = $obj->table; - $xmldb_index = $obj->index; - $sqlarr = $dbman->generator->getAddIndexSQL($xmldb_table, $xmldb_index); - $r.= '
  • ' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' . - $this->str['index'] . ': ' . $xmldb_index->readableInfo() . '
  • '; - $sqlarr = $dbman->generator->getEndedStatements($sqlarr); - $s.= '' . str_replace("\n", '
    ', implode('
    ', $sqlarr)) . '

    '; + // If we have found missing indexes or extra indexes inform the user about them. + if (!empty($missingindexes) || !empty($extraindexes)) { + if ($missingindexes) { + $r.= '

    ' . $this->str['yesmissingindexesfound'] . '

    '; + $r.= '
      '; + foreach ($missingindexes as $obj) { + $xmldb_table = $obj->table; + $xmldb_index = $obj->index; + $sqlarr = $dbman->generator->getAddIndexSQL($xmldb_table, $xmldb_index); + $r.= '
    • ' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' . + $this->str['index'] . ': ' . $xmldb_index->readableInfo() . '
    • '; + $sqlarr = $dbman->generator->getEndedStatements($sqlarr); + $s.= '' . str_replace("\n", '
      ', implode('
      ', $sqlarr)) . '

      '; + } + $r.= '
    '; + // Add the SQL statements (all together) + $r.= '
    ' . $s; + } + if ($extraindexes) { + $r .= '

    ' . $this->str['yesextraindexesfound'] . '

    '; + $r .= '
      '; + foreach ($extraindexes as $ei) { + $r .= '
    • ' . $ei . '
    • '; + } + $r .= '
    '; + $r .= '
    '; } - $r.= '
'; - // Add the SQL statements (all together) - $r.= '
' . $s; } else { - $r.= '

' . $this->str['nomissingindexesfound'] . '

'; + $r .= '

' . $this->str['nomissingorextraindexesfound'] . '

'; } $r.= '
'; @@ -166,4 +202,18 @@ class check_indexes extends XMLDBCheckAction { return $r; } + + /** + * Removes an index from the array $dbindexes if it is found. + * + * @param array $dbindexes + * @param xmldb_index $index + */ + private function remove_index_from_dbindex(array &$dbindexes, xmldb_index $index) { + foreach ($dbindexes as $key => $dbindex) { + if ($dbindex['columns'] == $index->getFields()) { + unset($dbindexes[$key]); + } + } + } } diff --git a/admin/tool/xmldb/lang/en/tool_xmldb.php b/admin/tool/xmldb/lang/en/tool_xmldb.php index c465ed16c11..77ababc6a13 100644 --- a/admin/tool/xmldb/lang/en/tool_xmldb.php +++ b/admin/tool/xmldb/lang/en/tool_xmldb.php @@ -99,6 +99,7 @@ $string['edit_xml_file'] = 'Edit XML file'; $string['enumvaluesincorrect'] = 'Incorrect values for enum field'; $string['expected'] = 'Expected'; $string['extensionrequired'] = 'Sorry - the PHP extension \'{$a}\' is required for this action. Please install the extension if you want to use this feature.'; +$string['extraindexesfound'] = 'Extra indexes found'; $string['field'] = 'Field'; $string['fieldnameempty'] = 'Name field empty'; $string['fields'] = 'Fields'; @@ -157,7 +158,7 @@ $string['newtablefrommysql'] = 'New table from MySQL'; $string['new_table_from_mysql'] = 'New table from MySQL'; $string['nofieldsspecified'] = 'No fields specified'; $string['nomasterprimaryuniquefound'] = 'The column(s) that your foreign key references must be included in a primary or unique KEY in the referenced table. Note that the column being in a UNIQUE INDEX is not good enough.'; -$string['nomissingindexesfound'] = 'No missing indexes have been found, your DB doesn\'t need further actions.'; +$string['nomissingorextraindexesfound'] = 'No missing or extra indexes have been found, your DB doesn\'t need further actions.'; $string['noreffieldsspecified'] = 'No reference fields specified'; $string['noreftablespecified'] = 'Specified reference table not found'; $string['noviolatedforeignkeysfound'] = 'No violated foreign keys found'; @@ -215,6 +216,7 @@ $string['wronglengthforenum'] = 'Incorrect length for enum field'; $string['wrongnumberofreffields'] = 'Wrong number of reference fields'; $string['wrongreservedwords'] = 'Currently used reserved words
(note that table names aren\'t important if using $CFG->prefix)'; $string['wrongoraclesemantics'] = 'Wrong Oracle BYTE semantics found'; +$string['yesextraindexesfound'] = 'The following additional indexes were found.'; $string['yesmissingindexesfound'] = '

Some missing indexes have been found in your DB. Here are their details and the needed SQL statements to be executed with your favourite SQL interface to create all of them. Remember to backup your data first!

After doing that, it\'s highly recommended to execute this utility again to check that no more missing indexes are found.

'; $string['yeswrongdefaultsfound'] = '

Some inconsistent defaults have been found in your DB. Here are their details and the needed SQL statements to be executed with your favourite SQL interface to fix them all. Remember to backup your data first!