From: tjhunt Date: Thu, 23 Oct 2008 08:32:12 +0000 (+0000) Subject: xmldb checks: MDL-16976 write a check that finds rows that violate the foreign key... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=8c901fb816ede9a5a89c05196cfd8260265030c3;p=moodle.git xmldb checks: MDL-16976 write a check that finds rows that violate the foreign key constraints. --- diff --git a/admin/xmldb/actions/check_foreign_keys/check_foreign_keys.class.php b/admin/xmldb/actions/check_foreign_keys/check_foreign_keys.class.php new file mode 100644 index 0000000000..5e6de08a98 --- /dev/null +++ b/admin/xmldb/actions/check_foreign_keys/check_foreign_keys.class.php @@ -0,0 +1,165 @@ +introstr = 'confirmcheckforeignkeys'; + parent::init(); + + /// Set own core attributes + + /// Set own custom attributes + + /// Get needed strings + $this->loadStrings(array( + 'key' => 'xmldb', + 'violatedforeignkeys' => 'xmldb', + 'noviolatedforeignkeysfound' => 'xmldb', + 'violatedforeignkeysfound' => 'xmldb', + 'violations' => 'xmldb', + )); + } + + protected function check_table(xmldb_table $xmldb_table, array $metacolumns) { + global $DB; + $dbman = $DB->get_manager(); + + $strictchecks = optional_param('strict', false, PARAM_BOOL); + + $o = ''; + $violatedkeys = array(); + + /// Keys + if ($xmldb_keys = $xmldb_table->getKeys()) { + $o.=' '; + } + + return array($o, $violatedkeys); + } + + protected function display_results(array $violatedkeys) { + $r = ''; + $r.= ' '; + $r.= ' '; + $r.= ' '; + $r.= '
'; + $r.= '

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

'; + $r.= '

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

'; + $r.= '
'; + + /// If we have found wrong integers inform about them + if (count($violatedkeys)) { + $r.= '

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

'; + $r.= '
    '; + foreach ($violatedkeys as $violation) { + $violation->tablename = $violation->table->getName(); + $violation->keyname = $violation->key->getName(); + + $r.= '
  • ' .get_string('fkviolationdetails', 'xmldb', $violation) . + '
    ' . s($violation->sql) . '; ' . s($violation->sqlparams) . '
  • '; + } + $r.= '
'; + } else { + $r.= '

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

'; + } + $r.= '
'; + /// Add the complete log message + $r.= '

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

'; + $r.= '
'; + + return $r; + } +} +?> diff --git a/admin/xmldb/actions/main_view/main_view.class.php b/admin/xmldb/actions/main_view/main_view.class.php index 4f706a5c0a..14b7fbd563 100644 --- a/admin/xmldb/actions/main_view/main_view.class.php +++ b/admin/xmldb/actions/main_view/main_view.class.php @@ -50,6 +50,7 @@ class main_view extends XMLDBAction { 'gotolastused' => 'xmldb', 'checkindexes' => 'xmldb', 'checkdefaults' => 'xmldb', + 'checkforeignkeys' => 'xmldb', 'checkbigints' => 'xmldb' )); } @@ -95,6 +96,7 @@ class main_view extends XMLDBAction { if ($DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres') { $b .= ' [' . $this->str['checkbigints'] . ']'; } + $b .= ' [' . $this->str['checkforeignkeys'] . ']'; $b .= '

'; /// Send buttons to output $o .= $b;