xmldb checks: MDL-16975 refactor the various checks to eliminate duplicated code.

This commit is contained in:
tjhunt
2008-10-23 08:30:43 +00:00
parent 659054c521
commit 9f5e5dee97
5 changed files with 501 additions and 614 deletions
@@ -0,0 +1,206 @@
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //
// //
// This program 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 2 of the License, or //
// (at your option) any later version. //
// //
// This program 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: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/// This is a base class for the various actions that interate over all the
/// tables and check some aspect of their definition.
abstract class XMLDBCheckAction extends XMLDBAction {
/**
* This string is displayed with a yes/no choice before the report is run.
* You must set this to the name of a lang string in xmldb.php before calling init.
*/
protected $introstr = '';
/**
* Init method, every subclass will have its own
*/
function init() {
parent::init();
/// Set own core attributes
/// Set own custom attributes
/// Get needed strings
$this->loadStrings(array(
$this->introstr => 'xmldb',
'ok' => '',
'wrong' => 'xmldb',
'table' => 'xmldb',
'field' => 'xmldb',
'searchresults' => 'xmldb',
'completelogbelow' => 'xmldb',
'yes' => '',
'no' => '',
'error' => '',
'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
$dbman = $DB->get_manager();
/// Here we'll acummulate all the wrong fields found
$problemsfound = 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 = '<table class="generalbox" border="0" cellpadding="5" cellspacing="0" id="notice">';
$o.= ' <tr><td class="generalboxcontent">';
$o.= ' <p class="centerpara">' . $this->str[$this->introstr] . '</p>';
$o.= ' <table class="boxaligncenter" cellpadding="20"><tr><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=' . $this->title . '&amp;confirmed=yes" method="post"><fieldset class="invisiblefieldset">';
$o.= ' <input type="submit" value="'. $this->str['yes'] .'" /></fieldset></form></div>';
$o.= ' </td><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=main_view" method="post"><fieldset class="invisiblefieldset">';
$o.= ' <input type="submit" value="'. $this->str['no'] .'" /></fieldset></form></div>';
$o.= ' </td></tr>';
$o.= ' </table>';
$o.= ' </td></tr>';
$o.= '</table>';
$this->output = $o;
} else {
/// The back to edit table button
$b = ' <p class="centerpara buttons">';
$b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
$b .= '</p>';
/// Iterate over $XMLDB->dbdirs, loading their XML data to memory
if ($XMLDB->dbdirs) {
$dbdirs =& $XMLDB->dbdirs;
$o='<ul>';
foreach ($dbdirs as $dbdir) {
/// Only if the directory exists
if (!$dbdir->path_exists) {
continue;
}
/// Load the XML file
$xmldb_file = new xmldb_file($dbdir->path . '/install.xml');
/// Only if the file exists
if (!$xmldb_file->fileExists()) {
continue;
}
/// Load the XML contents to structure
$loaded = $xmldb_file->loadXMLStructure();
if (!$loaded || !$xmldb_file->isLoaded()) {
notify('Errors found in XMLDB file: '. $dbdir->path . '/install.xml');
continue;
}
/// Arriving here, everything is ok, get the XMLDB structure
$structure = $xmldb_file->getStructure();
$o.=' <li>' . str_replace($CFG->dirroot . '/', '', $dbdir->path . '/install.xml');
/// Getting tables
if ($xmldb_tables = $structure->getTables()) {
$o.=' <ul>';
/// Foreach table, process its fields
foreach ($xmldb_tables as $xmldb_table) {
/// Skip table if not exists
if (!$dbman->table_exists($xmldb_table)) {
continue;
}
/// Fetch metadata from phisical DB. All the columns info.
if (!$metacolumns = $DB->get_columns($xmldb_table->getName())) {
//// Skip table if no metacolumns is available for it
continue;
}
/// Table processing starts here
$o.=' <li>' . $xmldb_table->getName();
/// Do the specific check.
list($output, $newproblems) = $this->check_table($xmldb_table, $metacolumns);
$o.=$output;
$problemsfound = array_merge($problemsfound, $newproblems);
$o.=' </li>';
}
$o.=' </ul>';
}
$o.=' </li>';
}
$o.='</ul>';
}
/// Create a report of the problems found.
$r = $this->display_results($problemsfound);
/// Combine the various bits of output.
$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;
}
/**
* Do the checks necessary on one particular table.
*
* @param xmldb_table $xmldb_table the table definition from the install.xml file.
* @param array $metacolumns the column information read from the database.
* @return array an array with two elements: First, some additional progress output,
* for example a list (<ul>) of the things check each with an one work ok/not ok summary.
* Second, an array giving the details of any problems found. These arrays
* for all tables will be aggregated, and then passed to
*/
abstract protected function check_table(xmldb_table $xmldb_table, array $metacolumns);
/**
* Display a list of the problems found.
*
* @param array $problems_found an aggregation of all the problems found by
* all the check_table calls.
* @return string a display of all the problems found as HTML.
*/
abstract protected function display_results(array $problems_found);
}
?>
@@ -29,12 +29,17 @@
/// and providing one SQL script to fix all them. Also, under MySQL,
/// it performs one check of signed bigints. MDL-11038
class check_bigints extends XMLDBAction {
class check_bigints extends XMLDBCheckAction {
private $correct_type;
private $dbfamily;
/**
* Init method, every subclass will have its own
*/
function init() {
global $DB;
$this->introstr = 'confirmcheckbigints';
parent::init();
/// Set own core attributes
@@ -43,231 +48,117 @@ class check_bigints extends XMLDBAction {
/// Get needed strings
$this->loadStrings(array(
'confirmcheckbigints' => 'xmldb',
'ok' => '',
'wrong' => 'xmldb',
'table' => 'xmldb',
'field' => 'xmldb',
'searchresults' => 'xmldb',
'wrongints' => 'xmldb',
'completelogbelow' => 'xmldb',
'nowrongintsfound' => 'xmldb',
'yeswrongintsfound' => 'xmldb',
'mysqlextracheckbigints' => 'xmldb',
'yes' => '',
'no' => '',
'error' => '',
'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;
$dbman = $DB->get_manager();
$dbfamily = $DB->get_dbfamily();
/// Here we'll acummulate all the wrong fields found
$wrong_fields = array();
/// Correct fields must be type bigint for MySQL and int8 for PostgreSQL
switch ($dbfamily) {
$this->dbfamily = $DB->get_dbfamily();
switch ($this->dbfamily) {
case 'mysql':
$correct_type = 'bigint';
$this->correct_type = 'bigint';
break;
case 'postgres':
$correct_type = 'int8';
$this->correct_type = 'int8';
break;
default:
$correct_type = NULL;
$this->correct_type = NULL;
}
}
protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
$o = '';
$wrong_fields = array();
/// Get and process XMLDB fields
if ($xmldb_fields = $xmldb_table->getFields()) {
$o.=' <ul>';
foreach ($xmldb_fields as $xmldb_field) {
/// If the field isn't integer(10), skip
if ($xmldb_field->getType() != XMLDB_TYPE_INTEGER || $xmldb_field->getLength() != 10) {
continue;
}
/// If the metadata for that column doesn't exist, skip
if (!isset($metacolumns[$xmldb_field->getName()])) {
continue;
}
/// To variable for better handling
$metacolumn = $metacolumns[$xmldb_field->getName()];
/// Going to check this field in DB
$o.=' <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
/// Detect if the phisical field is wrong and, under mysql, check for incorrect signed fields too
if ($metacolumn->type != $this->correct_type || ($this->dbfamily == 'mysql' && $xmldb_field->getUnsigned() && !$metacolumn->unsigned)) {
$o.='<font color="red">' . $this->str['wrong'] . '</font>';
/// Add the wrong field to the list
$obj = new object;
$obj->table = $xmldb_table;
$obj->field = $xmldb_field;
$wrong_fields[] = $obj;
} else {
$o.='<font color="green">' . $this->str['ok'] . '</font>';
}
$o.='</li>';
}
$o.=' </ul>';
}
/// Do the job, setting $result as needed
return array($o, $wrong_fields);
}
/// Get the confirmed to decide what to do
$confirmed = optional_param('confirmed', false, PARAM_BOOL);
protected function display_results(array $wrong_fields) {
global $DB;
$dbman = $DB->get_manager();
/// If not confirmed, show confirmation box
if (!$confirmed) {
$o = '<table class="generalbox" border="0" cellpadding="5" cellspacing="0" id="notice">';
$o.= ' <tr><td class="generalboxcontent">';
$o.= ' <p class="centerpara">' . $this->str['confirmcheckbigints'] . '</p>';
if ($dbfamily == 'mysql') {
$o.= ' <p class="centerpara">' . $this->str['mysqlextracheckbigints'] . '</p>';
}
$o.= ' <table class="boxaligncenter" cellpadding="20"><tr><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=check_bigints&amp;confirmed=yes" method="post"><fieldset class="invisiblefieldset">';
$o.= ' <input type="submit" value="'. $this->str['yes'] .'" /></fieldset></form></div>';
$o.= ' </td><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=main_view" method="post"><fieldset class="invisiblefieldset">';
$o.= ' <input type="submit" value="'. $this->str['no'] .'" /></fieldset></form></div>';
$o.= ' </td></tr>';
$o.= ' </table>';
$o.= ' </td></tr>';
$o.= '</table>';
$s = '';
$r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
$r.= ' <tr><td class="generalboxcontent">';
$r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
$r.= ' <p class="centerpara">' . $this->str['wrongints'] . ': ' . count($wrong_fields) . '</p>';
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
$this->output = $o;
} else {
/// The back to edit table button
$b = ' <p class="centerpara buttons">';
$b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
$b .= '</p>';
/// Iterate over $XMLDB->dbdirs, loading their XML data to memory
if ($XMLDB->dbdirs) {
$dbdirs =& $XMLDB->dbdirs;
$o='<ul>';
foreach ($dbdirs as $dbdir) {
/// Only if the directory exists
if (!$dbdir->path_exists) {
continue;
}
/// Load the XML file
$xmldb_file = new xmldb_file($dbdir->path . '/install.xml');
/// Only if the file exists
if (!$xmldb_file->fileExists()) {
continue;
}
/// Load the XML contents to structure
$loaded = $xmldb_file->loadXMLStructure();
if (!$loaded || !$xmldb_file->isLoaded()) {
notify('Errors found in XMLDB file: '. $dbdir->path . '/install.xml');
continue;
}
/// Arriving here, everything is ok, get the XMLDB structure
$structure = $xmldb_file->getStructure();
$o.=' <li>' . str_replace($CFG->dirroot . '/', '', $dbdir->path . '/install.xml');
/// Getting tables
if ($xmldb_tables = $structure->getTables()) {
$o.=' <ul>';
/// Foreach table, process its fields
foreach ($xmldb_tables as $xmldb_table) {
/// Skip table if not exists
if (!$dbman->table_exists($xmldb_table)) {
continue;
}
/// Fetch metadata from phisical DB. All the columns info.
if (!$metacolumns = $DB->get_columns($xmldb_table->getName())) {
//// Skip table if no metacolumns is available for it
continue;
}
/// Table processing starts here
$o.=' <li>' . $xmldb_table->getName();
/// Get and process XMLDB fields
if ($xmldb_fields = $xmldb_table->getFields()) {
$o.=' <ul>';
foreach ($xmldb_fields as $xmldb_field) {
/// If the field isn't integer(10), skip
if ($xmldb_field->getType() != XMLDB_TYPE_INTEGER || $xmldb_field->getLength() != 10) {
continue;
}
/// If the metadata for that column doesn't exist, skip
if (!isset($metacolumns[$xmldb_field->getName()])) {
continue;
}
/// To variable for better handling
$metacolumn = $metacolumns[$xmldb_field->getName()];
/// Going to check this field in DB
$o.=' <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
/// Detect if the phisical field is wrong and, under mysql, check for incorrect signed fields too
if ($metacolumn->type != $correct_type || ($dbfamily == 'mysql' && $xmldb_field->getUnsigned() && !$metacolumn->unsigned)) {
$o.='<font color="red">' . $this->str['wrong'] . '</font>';
/// Add the wrong field to the list
$obj = new object;
$obj->table = $xmldb_table;
$obj->field = $xmldb_field;
$wrong_fields[] = $obj;
} else {
$o.='<font color="green">' . $this->str['ok'] . '</font>';
}
$o.='</li>';
}
$o.=' </ul>';
}
$o.=' </li>';
}
$o.=' </ul>';
}
$o.=' </li>';
}
$o.='</ul>';
}
/// We have finished, let's show the results of the search
$s = '';
$r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
$r.= ' <tr><td class="generalboxcontent">';
$r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
$r.= ' <p class="centerpara">' . $this->str['wrongints'] . ': ' . count($wrong_fields) . '</p>';
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
/// If we have found wrong integers inform about them
if (count($wrong_fields)) {
$r.= ' <p class="centerpara">' . $this->str['yeswrongintsfound'] . '</p>';
$r.= ' <ul>';
foreach ($wrong_fields as $obj) {
$xmldb_table = $obj->table;
$xmldb_field = $obj->field;
/// MySQL directly supports this
/// If we have found wrong integers inform about them
if (count($wrong_fields)) {
$r.= ' <p class="centerpara">' . $this->str['yeswrongintsfound'] . '</p>';
$r.= ' <ul>';
foreach ($wrong_fields as $obj) {
$xmldb_table = $obj->table;
$xmldb_field = $obj->field;
/// MySQL directly supports this
// TODO: move this hack to generators!!
if ($dbfamily == 'mysql') {
$sqlarr = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field);
/// PostgreSQL (XMLDB implementation) is a bit, er... imperfect.
} else if ($dbfamily == 'postgres') {
$sqlarr = array('ALTER TABLE ' . $DB->get_prefix() . $xmldb_table->getName() .
' ALTER COLUMN ' . $xmldb_field->getName() . ' TYPE BIGINT;');
}
$r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
$this->str['field'] . ': ' . $xmldb_field->getName() . '</li>';
/// Add to output if we have sentences
if ($sqlarr) {
$sqlarr = $dbman->generator->getEndedStatements($sqlarr);
$s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)). '</code><br />';
}
if ($this->dbfamily == 'mysql') {
$sqlarr = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field);
/// PostgreSQL (XMLDB implementation) is a bit, er... imperfect.
} else if ($this->dbfamily == 'postgres') {
$sqlarr = array('ALTER TABLE ' . $DB->get_prefix() . $xmldb_table->getName() .
' ALTER COLUMN ' . $xmldb_field->getName() . ' TYPE BIGINT;');
}
$r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
$this->str['field'] . ': ' . $xmldb_field->getName() . '</li>';
/// Add to output if we have sentences
if ($sqlarr) {
$sqlarr = $dbman->generator->getEndedStatements($sqlarr);
$s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)). '</code><br />';
}
$r.= ' </ul>';
/// Add the SQL statements (all together)
$r.= '<hr />' . $s;
} else {
$r.= ' <p class="centerpara">' . $this->str['nowrongintsfound'] . '</p>';
}
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
/// Add the complete log message
$r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
$r.= ' </td></tr>';
$r.= '</table>';
$this->output = $b . $r . $o;
$r.= ' </ul>';
/// Add the SQL statements (all together)
$r.= '<hr />' . $s;
} else {
$r.= ' <p class="centerpara">' . $this->str['nowrongintsfound'] . '</p>';
}
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
/// Add the complete log message
$r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
$r.= ' </td></tr>';
$r.= '</table>';
/// Launch postaction if exists (leave this here!)
if ($this->getPostAction() && $result) {
return $this->launch($this->getPostAction());
}
/// Return ok if arrived here
return $result;
return $r;
}
}
?>
@@ -28,12 +28,13 @@
/// match those specified in the xml specs
/// and providing one SQL script to fix all them.
class check_defaults extends XMLDBAction {
class check_defaults extends XMLDBCheckAction {
/**
* Init method, every subclass will have its own
*/
function init() {
$this->introstr = 'confirmcheckdefaults';
parent::init();
/// Set own core attributes
@@ -42,230 +43,117 @@ class check_defaults extends XMLDBAction {
/// Get needed strings
$this->loadStrings(array(
'confirmcheckdefaults' => 'xmldb',
'ok' => '',
'wrong' => 'xmldb',
'table' => 'xmldb',
'field' => 'xmldb',
'searchresults' => 'xmldb',
'wrongdefaults' => 'xmldb',
'completelogbelow' => 'xmldb',
'nowrongdefaultsfound' => 'xmldb',
'yeswrongdefaultsfound' => 'xmldb',
'yes' => '',
'no' => '',
'error' => '',
'expected' => 'xmldb',
'actual' => '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
$dbman = $DB->get_manager();
/// Here we'll acummulate all the wrong fields found
protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
$o = '';
$wrong_fields = array();
/// Do the job, setting $result as needed
/// Get and process XMLDB fields
if ($xmldb_fields = $xmldb_table->getFields()) {
$o.=' <ul>';
foreach ($xmldb_fields as $xmldb_field) {
/// Get the confirmed to decide what to do
$confirmed = optional_param('confirmed', false, PARAM_BOOL);
// Get the default value for the field
$xmldbdefault = $xmldb_field->getDefault();
/// If not confirmed, show confirmation box
if (!$confirmed) {
$o = '<table class="generalbox" border="0" cellpadding="5" cellspacing="0" id="notice">';
$o.= ' <tr><td class="generalboxcontent">';
$o.= ' <p class="centerpara">' . $this->str['confirmcheckdefaults'] . '</p>';
$o.= ' <table class="boxaligncenter" cellpadding="20"><tr><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=check_defaults&amp;confirmed=yes" method="post"><fieldset class="invisiblefieldset">';
$o.= ' <input type="submit" value="'. $this->str['yes'] .'" /></fieldset></form></div>';
$o.= ' </td><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=main_view" method="post"><fieldset class="invisiblefieldset">';
$o.= ' <input type="submit" value="'. $this->str['no'] .'" /></fieldset></form></div>';
$o.= ' </td></tr>';
$o.= ' </table>';
$o.= ' </td></tr>';
$o.= '</table>';
/// If the metadata for that column doesn't exist or 'id' field found, skip
if (!isset($metacolumns[$xmldb_field->getName()]) or $xmldb_field->getName() == 'id') {
continue;
}
$this->output = $o;
/// To variable for better handling
$metacolumn = $metacolumns[$xmldb_field->getName()];
/// Going to check this field in DB
$o.=' <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
// get the value of the physical default (or blank if there isn't one)
if ($metacolumn->has_default==1) {
$physicaldefault = $metacolumn->default_value;
}
else {
$physicaldefault = '';
}
// there *is* a default and it's wrong
if ($physicaldefault != $xmldbdefault) {
$info = '('.$this->str['expected']." '$xmldbdefault', ".$this->str['actual'].
" '$physicaldefault')";
$o.='<font color="red">' . $this->str['wrong'] . " $info</font>";
/// Add the wrong field to the list
$obj = new object;
$obj->table = $xmldb_table;
$obj->field = $xmldb_field;
$obj->physicaldefault = $physicaldefault;
$obj->xmldbdefault = $xmldbdefault;
$wrong_fields[] = $obj;
} else {
$o.='<font color="green">' . $this->str['ok'] . '</font>';
}
$o.='</li>';
}
$o.=' </ul>';
}
return array($o, $wrong_fields);
}
protected function display_results(array $wrong_fields) {
global $DB;
$dbman = $DB->get_manager();
$s = '';
$r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
$r.= ' <tr><td class="generalboxcontent">';
$r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
$r.= ' <p class="centerpara">' . $this->str['wrongdefaults'] . ': ' . count($wrong_fields) . '</p>';
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
/// If we have found wrong defaults inform about them
if (count($wrong_fields)) {
$r.= ' <p class="centerpara">' . $this->str['yeswrongdefaultsfound'] . '</p>';
$r.= ' <ul>';
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 = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field);
$r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
$this->str['field'] . ': ' . $xmldb_field->getName() . ', ' .
$this->str['expected'] . ' ' . "'$xmldbdefault'" . ' ' .
$this->str['actual'] . ' ' . "'$physicaldefault'" . '</li>';
/// Add to output if we have sentences
if ($sqlarr) {
$sqlarr = $dbman->generator->getEndedStatements($sqlarr);
$s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
}
}
$r.= ' </ul>';
/// Add the SQL statements (all together)
$r.= '<hr />' . $s;
} else {
/// The back to edit table button
$b = ' <p class="centerpara buttons">';
$b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
$b .= '</p>';
/// Iterate over $XMLDB->dbdirs, loading their XML data to memory
if ($XMLDB->dbdirs) {
$dbdirs =& $XMLDB->dbdirs;
$o='<ul>';
foreach ($dbdirs as $dbdir) {
/// Only if the directory exists
if (!$dbdir->path_exists) {
continue;
}
/// Load the XML file
$xmldb_file = new xmldb_file($dbdir->path . '/install.xml');
/// Only if the file exists
if (!$xmldb_file->fileExists()) {
continue;
}
/// Load the XML contents to structure
$loaded = $xmldb_file->loadXMLStructure();
if (!$loaded || !$xmldb_file->isLoaded()) {
notify('Errors found in XMLDB file: '. $dbdir->path . '/install.xml');
continue;
}
/// Arriving here, everything is ok, get the XMLDB structure
$structure = $xmldb_file->getStructure();
$o.=' <li>' . str_replace($CFG->dirroot . '/', '', $dbdir->path . '/install.xml');
/// Getting tables
if ($xmldb_tables = $structure->getTables()) {
$o.=' <ul>';
/// Foreach table, process its fields
foreach ($xmldb_tables as $xmldb_table) {
/// Skip table if not exists
if (!$dbman->table_exists($xmldb_table)) {
continue;
}
/// Fetch metadata from phisical DB. All the columns info.
if (!$metacolumns = $DB->get_columns($xmldb_table->getName())) {
//// Skip table if no metacolumns is available for it
continue;
}
/// Table processing starts here
$o.=' <li>' . $xmldb_table->getName();
/// Get and process XMLDB fields
if ($xmldb_fields = $xmldb_table->getFields()) {
$o.=' <ul>';
foreach ($xmldb_fields as $xmldb_field) {
// Get the default value for the field
$xmldbdefault = $xmldb_field->getDefault();
/// If the metadata for that column doesn't exist or 'id' field found, skip
if (!isset($metacolumns[$xmldb_field->getName()]) or $xmldb_field->getName() == 'id') {
continue;
}
/// To variable for better handling
$metacolumn = $metacolumns[$xmldb_field->getName()];
/// Going to check this field in DB
$o.=' <li>' . $this->str['field'] . ': ' . $xmldb_field->getName() . ' ';
// get the value of the physical default (or blank if there isn't one)
if ($metacolumn->has_default==1) {
$physicaldefault = $metacolumn->default_value;
}
else {
$physicaldefault = '';
}
// there *is* a default and it's wrong
if ($physicaldefault != $xmldbdefault) {
$info = '('.$this->str['expected']." '$xmldbdefault', ".$this->str['actual'].
" '$physicaldefault')";
$o.='<font color="red">' . $this->str['wrong'] . " $info</font>";
/// Add the wrong field to the list
$obj = new object;
$obj->table = $xmldb_table;
$obj->field = $xmldb_field;
$obj->physicaldefault = $physicaldefault;
$obj->xmldbdefault = $xmldbdefault;
$wrong_fields[] = $obj;
} else {
$o.='<font color="green">' . $this->str['ok'] . '</font>';
}
$o.='</li>';
}
$o.=' </ul>';
}
$o.=' </li>';
}
$o.=' </ul>';
}
$o.=' </li>';
}
$o.='</ul>';
}
/// We have finished, let's show the results of the search
$s = '';
$r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
$r.= ' <tr><td class="generalboxcontent">';
$r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
$r.= ' <p class="centerpara">' . $this->str['wrongdefaults'] . ': ' . count($wrong_fields) . '</p>';
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
/// If we have found wrong defaults inform about them
if (count($wrong_fields)) {
$r.= ' <p class="centerpara">' . $this->str['yeswrongdefaultsfound'] . '</p>';
$r.= ' <ul>';
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 = $dbman->generator->getAlterFieldSQL($xmldb_table, $xmldb_field);
$r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
$this->str['field'] . ': ' . $xmldb_field->getName() . ', ' .
$this->str['expected'] . ' ' . "'$xmldbdefault'" . ' ' .
$this->str['actual'] . ' ' . "'$physicaldefault'" . '</li>';
/// Add to output if we have sentences
if ($sqlarr) {
$sqlarr = $dbman->generator->getEndedStatements($sqlarr);
$s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
}
}
$r.= ' </ul>';
/// Add the SQL statements (all together)
$r.= '<hr />' . $s;
} else {
$r.= ' <p class="centerpara">' . $this->str['nowrongdefaultsfound'] . '</p>';
}
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
/// Add the complete log message
$r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
$r.= ' </td></tr>';
$r.= '</table>';
$this->output = $b . $r . $o;
$r.= ' <p class="centerpara">' . $this->str['nowrongdefaultsfound'] . '</p>';
}
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
/// Add the complete log message
$r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
$r.= ' </td></tr>';
$r.= '</table>';
/// Launch postaction if exists (leave this here!)
if ($this->getPostAction() && $result) {
return $this->launch($this->getPostAction());
}
/// Return ok if arrived here
return $result;
return $r;
}
}
?>
@@ -28,12 +28,13 @@
/// with the phisical DB implementation, reporting about all the missing
/// indexes to be created to be 100% ok.
class check_indexes extends XMLDBAction {
class check_indexes extends XMLDBCheckAction {
/**
* Init method, every subclass will have its own
*/
function init() {
$this->introstr = 'confirmcheckindexes';
parent::init();
/// Set own core attributes
@@ -42,226 +43,127 @@ class check_indexes extends XMLDBAction {
/// Get needed strings
$this->loadStrings(array(
'confirmcheckindexes' => 'xmldb',
'ok' => '',
'missing' => 'xmldb',
'table' => 'xmldb',
'key' => 'xmldb',
'index' => 'xmldb',
'searchresults' => 'xmldb',
'missingindexes' => 'xmldb',
'completelogbelow' => 'xmldb',
'nomissingindexesfound' => 'xmldb',
'yesmissingindexesfound' => 'xmldb',
'yes' => '',
'no' => '',
'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;
protected function check_table(xmldb_table $xmldb_table, array $metacolumns) {
global $DB;
$dbman = $DB->get_manager();
/// Here we'll acummulate all the missing indexes found
$o = '';
$missing_indexes = array();
/// Do the job, setting $result as needed
/// Keys
if ($xmldb_keys = $xmldb_table->getKeys()) {
$o.=' <ul>';
foreach ($xmldb_keys as $xmldb_key) {
$o.=' <li>' . $this->str['key'] . ': ' . $xmldb_key->readableInfo() . ' ';
/// Primaries are skipped
if ($xmldb_key->getType() == XMLDB_KEY_PRIMARY) {
$o.='<font color="green">' . $this->str['ok'] . '</font></li>';
continue;
}
/// If we aren't creating the keys or the key is a XMLDB_KEY_FOREIGN (not underlying index generated
/// automatically by the RDBMS) create the underlying (created by us) index (if doesn't exists)
if (!$dbman->generator->getKeySQL($xmldb_table, $xmldb_key) || $xmldb_key->getType() == XMLDB_KEY_FOREIGN) {
/// Create the interim index
$xmldb_index = new xmldb_index('anyname');
$xmldb_index->setFields($xmldb_key->getFields());
switch ($xmldb_key->getType()) {
case XMLDB_KEY_UNIQUE:
case XMLDB_KEY_FOREIGN_UNIQUE:
$xmldb_index->setUnique(true);
break;
case XMLDB_KEY_FOREIGN:
$xmldb_index->setUnique(false);
break;
}
/// Check if the index exists in DB
if ($dbman->index_exists($xmldb_table, $xmldb_index)) {
$o.='<font color="green">' . $this->str['ok'] . '</font>';
} else {
$o.='<font color="red">' . $this->str['missing'] . '</font>';
/// Add the missing index to the list
$obj = new object;
$obj->table = $xmldb_table;
$obj->index = $xmldb_index;
$missing_indexes[] = $obj;
}
}
$o.='</li>';
}
$o.=' </ul>';
}
/// Indexes
if ($xmldb_indexes = $xmldb_table->getIndexes()) {
$o.=' <ul>';
foreach ($xmldb_indexes as $xmldb_index) {
$o.=' <li>' . $this->str['index'] . ': ' . $xmldb_index->readableInfo() . ' ';
/// Check if the index exists in DB
if ($dbman->index_exists($xmldb_table, $xmldb_index)) {
$o.='<font color="green">' . $this->str['ok'] . '</font>';
} else {
$o.='<font color="red">' . $this->str['missing'] . '</font>';
/// Add the missing index to the list
$obj = new object;
$obj->table = $xmldb_table;
$obj->index = $xmldb_index;
$missing_indexes[] = $obj;
}
$o.='</li>';
}
$o.=' </ul>';
}
/// Get the confirmed to decide what to do
$confirmed = optional_param('confirmed', false, PARAM_BOOL);
return array($o, $missing_indexes);
}
/// If not confirmed, show confirmation box
if (!$confirmed) {
$o = '<table class="generalbox" border="0" cellpadding="5" cellspacing="0" id="notice">';
$o.= ' <tr><td class="generalboxcontent">';
$o.= ' <p class="centerpara">' . $this->str['confirmcheckindexes'] . '</p>';
$o.= ' <table class="boxaligncenter" cellpadding="20"><tr><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=check_indexes&amp;confirmed=yes" method="post"><fieldset class="invisiblefieldset">';
$o.= ' <input type="submit" value="'. $this->str['yes'] .'" /></fieldset></form></div>';
$o.= ' </td><td>';
$o.= ' <div class="singlebutton">';
$o.= ' <form action="index.php?action=main_view" method="post"><fieldset class="invisiblefieldset">';
$o.= ' <input type="submit" value="'. $this->str['no'] .'" /></fieldset></form></div>';
$o.= ' </td></tr>';
$o.= ' </table>';
$o.= ' </td></tr>';
$o.= '</table>';
protected function display_results(array $missing_indexes) {
global $DB;
$dbman = $DB->get_manager();
$this->output = $o;
$s = '';
$r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
$r.= ' <tr><td class="generalboxcontent">';
$r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
$r.= ' <p class="centerpara">' . $this->str['missingindexes'] . ': ' . count($missing_indexes) . '</p>';
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
/// If we have found missing indexes inform about them
if (count($missing_indexes)) {
$r.= ' <p class="centerpara">' . $this->str['yesmissingindexesfound'] . '</p>';
$r.= ' <ul>';
foreach ($missing_indexes as $obj) {
$xmldb_table = $obj->table;
$xmldb_index = $obj->index;
$sqlarr = $dbman->generator->getAddIndexSQL($xmldb_table, $xmldb_index);
$r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
$this->str['index'] . ': ' . $xmldb_index->readableInfo() . '</li>';
$sqlarr = $dbman->generator->getEndedStatements($sqlarr);
$s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
}
$r.= ' </ul>';
/// Add the SQL statements (all together)
$r.= '<hr />' . $s;
} else {
/// The back to edit table button
$b = ' <p class="centerpara buttons">';
$b .= '<a href="index.php">[' . $this->str['back'] . ']</a>';
$b .= '</p>';
/// Iterate over $XMLDB->dbdirs, loading their XML data to memory
if ($XMLDB->dbdirs) {
$dbdirs =& $XMLDB->dbdirs;
$o='<ul>';
foreach ($dbdirs as $dbdir) {
/// Only if the directory exists
if (!$dbdir->path_exists) {
continue;
}
/// Load the XML file
$xmldb_file = new xmldb_file($dbdir->path . '/install.xml');
/// Only if the file exists
if (!$xmldb_file->fileExists()) {
continue;
}
/// Load the XML contents to structure
$loaded = $xmldb_file->loadXMLStructure();
if (!$loaded || !$xmldb_file->isLoaded()) {
notify('Errors found in XMLDB file: '. $dbdir->path . '/install.xml');
continue;
}
/// Arriving here, everything is ok, get the XMLDB structure
$structure = $xmldb_file->getStructure();
$o.=' <li>' . str_replace($CFG->dirroot . '/', '', $dbdir->path . '/install.xml');
/// Getting tables
if ($xmldb_tables = $structure->getTables()) {
$o.=' <ul>';
/// Foreach table, process its indexes and keys
foreach ($xmldb_tables as $xmldb_table) {
/// Skip table if not exists
if (!$dbman->table_exists($xmldb_table)) {
continue;
}
$o.=' <li>' . $xmldb_table->getName();
/// Keys
if ($xmldb_keys = $xmldb_table->getKeys()) {
$o.=' <ul>';
foreach ($xmldb_keys as $xmldb_key) {
$o.=' <li>' . $this->str['key'] . ': ' . $xmldb_key->readableInfo() . ' ';
/// Primaries are skipped
if ($xmldb_key->getType() == XMLDB_KEY_PRIMARY) {
$o.='<font color="green">' . $this->str['ok'] . '</font></li>';
continue;
}
/// If we aren't creating the keys or the key is a XMLDB_KEY_FOREIGN (not underlying index generated
/// automatically by the RDBMS) create the underlying (created by us) index (if doesn't exists)
if (!$dbman->generator->getKeySQL($xmldb_table, $xmldb_key) || $xmldb_key->getType() == XMLDB_KEY_FOREIGN) {
/// Create the interim index
$xmldb_index = new xmldb_index('anyname');
$xmldb_index->setFields($xmldb_key->getFields());
switch ($xmldb_key->getType()) {
case XMLDB_KEY_UNIQUE:
case XMLDB_KEY_FOREIGN_UNIQUE:
$xmldb_index->setUnique(true);
break;
case XMLDB_KEY_FOREIGN:
$xmldb_index->setUnique(false);
break;
}
/// Check if the index exists in DB
if ($dbman->index_exists($xmldb_table, $xmldb_index)) {
$o.='<font color="green">' . $this->str['ok'] . '</font>';
} else {
$o.='<font color="red">' . $this->str['missing'] . '</font>';
/// Add the missing index to the list
$obj = new object;
$obj->table = $xmldb_table;
$obj->index = $xmldb_index;
$missing_indexes[] = $obj;
}
}
$o.='</li>';
}
$o.=' </ul>';
}
/// Indexes
if ($xmldb_indexes = $xmldb_table->getIndexes()) {
$o.=' <ul>';
foreach ($xmldb_indexes as $xmldb_index) {
$o.=' <li>' . $this->str['index'] . ': ' . $xmldb_index->readableInfo() . ' ';
/// Check if the index exists in DB
if ($dbman->index_exists($xmldb_table, $xmldb_index)) {
$o.='<font color="green">' . $this->str['ok'] . '</font>';
} else {
$o.='<font color="red">' . $this->str['missing'] . '</font>';
/// Add the missing index to the list
$obj = new object;
$obj->table = $xmldb_table;
$obj->index = $xmldb_index;
$missing_indexes[] = $obj;
}
$o.='</li>';
}
$o.=' </ul>';
}
$o.=' </li>';
}
$o.=' </ul>';
}
$o.=' </li>';
}
$o.='</ul>';
}
/// We have finished, let's show the results of the search
$s = '';
$r = '<table class="generalbox boxaligncenter boxwidthwide" border="0" cellpadding="5" cellspacing="0" id="results">';
$r.= ' <tr><td class="generalboxcontent">';
$r.= ' <h2 class="main">' . $this->str['searchresults'] . '</h2>';
$r.= ' <p class="centerpara">' . $this->str['missingindexes'] . ': ' . count($missing_indexes) . '</p>';
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
/// If we have found missing indexes inform about them
if (count($missing_indexes)) {
$r.= ' <p class="centerpara">' . $this->str['yesmissingindexesfound'] . '</p>';
$r.= ' <ul>';
foreach ($missing_indexes as $obj) {
$xmldb_table = $obj->table;
$xmldb_index = $obj->index;
$sqlarr = $dbman->generator->getAddIndexSQL($xmldb_table, $xmldb_index);
$r.= ' <li>' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
$this->str['index'] . ': ' . $xmldb_index->readableInfo() . '</li>';
$sqlarr = $dbman->generator->getEndedStatements($sqlarr);
$s.= '<code>' . str_replace("\n", '<br />', implode('<br />', $sqlarr)) . '</code><br />';
}
$r.= ' </ul>';
/// Add the SQL statements (all together)
$r.= '<hr />' . $s;
} else {
$r.= ' <p class="centerpara">' . $this->str['nomissingindexesfound'] . '</p>';
}
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
/// Add the complete log message
$r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
$r.= ' </td></tr>';
$r.= '</table>';
$this->output = $b . $r . $o;
$r.= ' <p class="centerpara">' . $this->str['nomissingindexesfound'] . '</p>';
}
$r.= ' </td></tr>';
$r.= ' <tr><td class="generalboxcontent">';
/// Add the complete log message
$r.= ' <p class="centerpara">' . $this->str['completelogbelow'] . '</p>';
$r.= ' </td></tr>';
$r.= '</table>';
/// Launch postaction if exists (leave this here!)
if ($this->getPostAction() && $result) {
return $this->launch($this->getPostAction());
}
/// Return ok if arrived here
return $result;
return $r;
}
}
?>
+1 -1
View File
@@ -32,7 +32,7 @@
/// Add required XMLDB action classes
require_once('actions/XMLDBAction.class.php');
require_once('actions/XMLDBCheckAction.class.php');
/// Add required XMLDB DB classes
require_once('../../lib/xmldb/xmldb_object.php');