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;
/// And we nedd some ddl suff
require_once ($CFG->libdir . '/ddllib.php');
/// 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 ($CFG->dbfamily) {
case 'mysql':
$correct_type = 'bigint';
break;
case 'postgres':
$correct_type = 'int8';
break;
default:
$correct_type = NULL;
}
/// 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.= ' ' . $this->str['confirmcheckbigints'] . ' ';
if ($CFG->dbfamily == 'mysql') {
$o.= ' ' . $this->str['mysqlextracheckbigints'] . ' ';
}
$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='';
foreach ($dbdirs as $dbdir) {
/// Only if the directory exists
if (!$dbdir->path_exists) {
continue;
}
/// Load the XML file
$xmldb_file = new XMLDBFile($dbdir->path . '/install.xml');
/// Load the needed XMLDB generator
$classname = 'XMLDB' . $CFG->dbtype;
$generator = new $classname();
$generator->setPrefix($CFG->prefix);
/// 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.=' - ' . str_replace($CFG->dirroot . '/', '', $dbdir->path . '/install.xml');
/// Getting tables
if ($xmldb_tables = $structure->getTables()) {
$o.='
';
/// Foreach table, process its fields
foreach ($xmldb_tables as $xmldb_table) {
/// Skip table if not exists
if (!table_exists($xmldb_table)) {
continue;
}
/// Fetch metadata from phisical DB. All the columns info.
if ($metacolumns = $db->MetaColumns($CFG->prefix . $xmldb_table->getName())) {
$metacolumns = array_change_key_case($metacolumns, CASE_LOWER);
} else {
//// Skip table if no metacolumns is available for it
continue;
}
/// Table processing starts here
$o.=' - ' . $xmldb_table->getName();
/// Get and process XMLDB fields
if ($xmldb_fields = $xmldb_table->getFields()) {
$o.='
';
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.=' - ' . $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 || ($CFG->dbfamily == 'mysql' && $xmldb_field->getUnsigned() && !$metacolumn->unsigned)) {
$o.='' . $this->str['wrong'] . '';
/// Add the wrong field to the list
$obj = new object;
$obj->table = $xmldb_table;
$obj->field = $xmldb_field;
$wrong_fields[] = $obj;
} else {
$o.='' . $this->str['ok'] . '';
}
$o.='
';
}
$o.='
';
}
$o.=' ';
/// Give the script some more time (resetting to current if exists)
if ($currenttl = @ini_get('max_execution_time')) {
@ini_set('max_execution_time',$currenttl);
}
}
$o.='
';
}
$o.=' ';
}
$o.='
';
}
/// We have finished, let's show the results of the search
$s = '';
$r = '';
$r.= ' ';
$r.= ' ' . $this->str['searchresults'] . '';
$r.= ' ' . $this->str['wrongints'] . ': ' . count($wrong_fields) . ' ';
$r.= ' |
';
$r.= ' ';
/// If we have found wrong integers inform about them
if (count($wrong_fields)) {
$r.= ' ' . $this->str['yeswrongintsfound'] . ' ';
$r.= ' ';
foreach ($wrong_fields as $obj) {
$xmldb_table = $obj->table;
$xmldb_field = $obj->field;
/// MySQL directly supports this
if ($CFG->dbfamily == 'mysql') {
$sqlarr = $xmldb_table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $xmldb_field, true);
/// PostgreSQL (XMLDB implementation) is a bit, er... imperfect.
} else if ($CFG->dbfamily == 'postgres') {
$sqlarr = array('ALTER TABLE ' . $CFG->prefix . $xmldb_table->getName() .
' ALTER COLUMN ' . $xmldb_field->getName() . ' TYPE BIGINT;');
}
$r.= ' - ' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' .
$this->str['field'] . ': ' . $xmldb_field->getName() . '
';
/// 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.= ' |
';
$r.= ' ';
/// Add the complete log message
$r.= ' ' . $this->str['completelogbelow'] . ' ';
$r.= ' |
';
$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;
}
}
?>