From: thepurpleblob Date: Thu, 15 May 2008 14:47:08 +0000 (+0000) Subject: MDL-14854 X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=dfac8649c7d51163bc1721c66c210e15b2756f03;p=moodle.git MDL-14854 Add facility to XMLDB editor to check for inconsistent field defaults. --- diff --git a/admin/xmldb/actions/check_defaults/check_defaults.class.php b/admin/xmldb/actions/check_defaults/check_defaults.class.php new file mode 100644 index 0000000000..2c7a90209a --- /dev/null +++ b/admin/xmldb/actions/check_defaults/check_defaults.class.php @@ -0,0 +1,278 @@ +loadStrings(array( + 'confirmcheckdefaults' => 'xmldb', + 'ok' => '', + 'wrong' => 'xmldb', + 'table' => 'xmldb', + 'field' => 'xmldb', + 'searchresults' => 'xmldb', + 'wrongdefaults' => 'xmldb', + 'completelogbelow' => 'xmldb', + 'nowrongdefaultsfound' => 'xmldb', + 'yeswrongdefaultsfound' => 'xmldb', + 'yes' => '', + 'no' => '', + 'error' => '', + 'shouldbe' => 'xmldb', + 'butis' => 'xmldb', + 'back' => 'xmldb' + + )); + } + + /** + * Invoke method, every class will have its own + * returns true/false on completion, setting both + * errormsg and output as necessary + */ + function invoke() { + parent::invoke(); + + $result = true; + + /// Set own core attributes + $this->does_generate = ACTION_GENERATE_HTML; + + /// These are always here + global $CFG, $XMLDB, $db; + + /// And we nedd some ddl suff + require_once ($CFG->libdir . '/ddllib.php'); + + /// Here we'll acummulate all the wrong fields found + $wrong_fields = array(); + + /// Do the job, setting $result as needed + + /// Get the confirmed to decide what to do + $confirmed = optional_param('confirmed', false, PARAM_BOOL); + + /// If not confirmed, show confirmation box + if (!$confirmed) { + $o = ''; + $o.= ' '; + $o.= '
'; + $o.= '

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

'; + $o.= ' '; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + + $this->output = $o; + } else { + /// The back to edit table button + $b = '

'; + $b .= '[' . $this->str['back'] . ']'; + $b .= '

'; + + /// Iterate over $XMLDB->dbdirs, loading their XML data to memory + if ($XMLDB->dbdirs) { + $dbdirs =& $XMLDB->dbdirs; + $o=''; + } + + /// We have finished, let's show the results of the search + $s = ''; + $r = ''; + $r.= ' '; + $r.= ' '; + $r.= ' '; + $r.= '
'; + $r.= '

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

'; + $r.= '

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

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

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

'; + $r.= '
    '; + foreach ($wrong_fields as $obj) { + $xmldb_table = $obj->table; + $xmldb_field = $obj->field; + $physicaldefault = $obj->physicaldefault; + $xmldbdefault = $obj->xmldbdefault; + + // get the alter table command + $sqlarr = $xmldb_table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $xmldb_field, true); + + $r.= '
  • ' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' . + $this->str['field'] . ': ' . $xmldb_field->getName() . ', ' . + $this->str['shouldbe'] . ' ' . "'$xmldbdefault'" . ' ' . + $this->str['butis'] . ' ' . "'$physicaldefault'" . '
  • '; + /// Add to output if we have sentences + if ($sqlarr) { + $s.= '' . str_replace("\n", '
    ', implode('
    ', $sqlarr)) . '

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

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

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

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

'; + $r.= '
'; + + $this->output = $b . $r . $o; + } + + /// Launch postaction if exists (leave this here!) + if ($this->getPostAction() && $result) { + return $this->launch($this->getPostAction()); + } + + /// Return ok if arrived here + return $result; + } +} +?> diff --git a/admin/xmldb/actions/main_view/main_view.class.php b/admin/xmldb/actions/main_view/main_view.class.php index a9cc67e097..8428343e08 100644 --- a/admin/xmldb/actions/main_view/main_view.class.php +++ b/admin/xmldb/actions/main_view/main_view.class.php @@ -49,6 +49,7 @@ class main_view extends XMLDBAction { 'test' => 'xmldb', 'gotolastused' => 'xmldb', 'checkindexes' => 'xmldb', + 'checkdefaults' => 'xmldb', 'checkbigints' => 'xmldb' )); } @@ -88,6 +89,8 @@ class main_view extends XMLDBAction { $b .= ' [' . $this->str['test'] . ']'; /// The check indexes button $b .= ' [' . $this->str['checkindexes'] . ']'; + /// The check defaults button + $b .= ' [' . $this->str['checkdefaults'] . ']'; /// The check bigints button (only for MySQL and PostgreSQL) MDL-11038a if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') { $b .= ' [' . $this->str['checkbigints'] . ']'; diff --git a/lang/en_utf8/xmldb.php b/lang/en_utf8/xmldb.php index b9afe43662..f42df8078d 100644 --- a/lang/en_utf8/xmldb.php +++ b/lang/en_utf8/xmldb.php @@ -6,11 +6,14 @@ $string['aftertable'] = 'After Table:'; $string['back'] = 'Back'; $string['backtomainview'] = 'Back To Main'; $string['binaryincorrectlength'] = 'Incorrect length for binary field'; +$string['butis'] = 'but is'; $string['cannotuseidfield'] = 'Cannot insert the \"id\" field. It is an autonumeric column'; $string['change'] = 'Change'; $string['charincorrectlength'] = 'Incorrect length for char field'; $string['checkbigints'] = 'Check Bigints'; +$string['checkdefaults'] = 'Check Defaults'; $string['checkindexes'] = 'Check Indexes'; +$string['check_defaults'] = 'Look for inconsistent default values'; $string['check_bigints'] = 'Look for incorrect DB integers'; $string['check_indexes'] = 'Look for missing DB indexes'; $string['completelogbelow'] = '(see the complete log of the search below)'; @@ -18,6 +21,10 @@ $string['confirmcheckbigints'] = 'This functionality will search for
It\'s highly recommended to be running the latest (+ version) available of your Moodle release (1.8, 1.9, 2.x ...) before executing the search of wrong integers.

This functionality doesn\'t perform any action against the DB (just reads from it), so can be safely executed at any moment.'; +$string['confirmcheckdefaults'] = 'This functionality will search for inconsistent default values in your Moodle server, generating (but not executing!) the needed SQL statements to have all the default values properly defined.

+Once generated you can copy such statements and execute them safely with your favourite SQL interface (don\'t forget to backup your data before doing that).

+It\'s highly recommended to be running the latest (+ version) available of your Moodle release (1.8, 1.9, 2.x ...) before executing the search of wrong integers.

+This functionality doesn\'t perform any action against the DB (just reads from it), so can be safely executed at any moment.'; $string['confirmcheckindexes'] = 'This functionality will search for potential missing indexes in your Moodle server, generating (but not executing!) automatically the needed SQL statements to keep everything updated.

Once generated you can copy such statements and execute them safely with your favourite SQL interface (don\'t forget to backup your data before doing that).

It\'s highly recommended to be running the latest (+ version) available of your Moodle release (1.8, 1.9, 2.x ...) before executing the search of missing indexes.

@@ -105,6 +112,7 @@ $string['selectonecommand'] = 'Please, select one Action from the list to view P $string['selectonefieldkeyindex'] = 'Please, select one Field/Key/Index from the list to view the PHP code'; $string['selecttable'] = 'Select Table:'; $string['sentences'] = 'Sentences'; +$string['shouldbe'] = 'should be'; $string['statements'] = 'Statements'; $string['statementtable'] = 'Statement Table:'; $string['statementtype'] = 'Statement Type:'; @@ -125,10 +133,12 @@ $string['vieworiginal'] = 'View Original'; $string['viewphpcode'] = 'View PHP Code'; $string['viewsqlcode'] = 'View SQL Code'; $string['wrong'] = 'Wrong'; +$string['wrongdefaults'] = 'Wrong Defaults Found'; $string['wrongints'] = 'Wrong Integers Found'; $string['wronglengthforenum'] = 'Incorrect length for enum field'; $string['wrongnumberoffieldsorvalues'] = 'Incorrect number of fields or values in sentence'; $string['wrongreservedwords'] = 'Currently Used Reserved Words
(note that table names aren\'t important if using $CFG->prefix)'; +$string['yeswrongdefaultsfound'] = 'Some inconsistent defaults have been found in your DB. Here there are their details and the needed SQL statements to be executed with your favourite SQL interface to fix them all (don\'t forget to backup your data before doing that).

After doing that, it\'s highly recommended to execute this utility again to check that no more iconsistent defaults are found.'; $string['yesmissingindexesfound'] = 'Some missing indexes have been found in your DB. Here there are their details and the needed SQL statements to be executed with your favourite SQL interface to create all them (don\'t forget to backup your data before doing that).

After doing that, it\'s highly recommended to execute this utility again to check that no more missing indexes are found.'; $string['yeswrongintsfound'] = 'Some wrong integers have been found in your DB. Here there are their details and the needed SQL statements to be executed with your favourite SQL interface to create all them (don\'t forget to backup your data before doing that).

After doing that, it\'s highly recommended to execute this utility again to check that no more wrong integers are found.'; ?>