Skip to content

Commit

Permalink
Issue #4501 - Improve verification and cron table engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
CaMer0n committed Dec 6, 2023
1 parent 84fe29c commit 3d000c8
Show file tree
Hide file tree
Showing 3 changed files with 775 additions and 671 deletions.
2 changes: 1 addition & 1 deletion e107_core/sql/core_sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
cron_active INT(1) UNSIGNED NOT NULL,
PRIMARY KEY (cron_id),
UNIQUE KEY cron_function (cron_function)
) ENGINE = MYISAM;
) ENGINE=InnoDB;


# Table structure for table `dblog` - db/debug/rolling
Expand Down
72 changes: 50 additions & 22 deletions e107_handlers/db_verify_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public function compare($selection, $language='')

$maybeCharset = isset($sqlDataArr['charset'][0]) ? $sqlDataArr['charset'][0] : 'INTERNAL_ERROR:CHARSET';
$fileData['charset'] = $this->getIntendedCharset($this->sqlFileTables[$selection]['charset'][$key]);
$sqlData['charset'] = $this->getCanonicalCharset($maybeCharset);
$sqlData['charset'] = $sqlDataArr['charset'][0]; // check the actual charset. $this->getCanonicalCharset($maybeCharset);

/*
$debugA = print_r($fileFieldData,TRUE); // Extracted Field Arrays
Expand All @@ -399,10 +399,15 @@ public function compare($selection, $language='')

if(isset($debugA) && (e_PAGE === 'db.php'))
{

$engineA = !empty($this->sqlFileTables[$selection]['engine'][0]) ? $this->sqlFileTables[$selection]['engine'][0] : 'unknown';
$engineB = !empty($sqlDataArr['engine'][0]) ? $sqlDataArr['engine'][0] : 'unknown';

$charsetA = !empty($this->sqlFileTables[$selection]['charset'][0]) ? $this->sqlFileTables[$selection]['charset'][0] : 'not specified';
$charsetB = !empty($sqlDataArr['charset'][0]) ? $sqlDataArr['charset'][0] : 'unknown';

$debug = "<table class='table table-bordered table-condensed'>
<tr><td style='padding:5px;font-weight:bold'>FILE: ".$tbl." (key=".$key.")</td>
<td style='padding:5px;font-weight:bold'>SQL: ".$tbl."</td>
<tr><td style='padding:5px;font-weight:bold'>FILE: $tbl (key=$key) <span class='badge'>$engineA</span> $charsetA</td>
<td style='padding:5px;font-weight:bold'>SQL: $tbl <span class='badge'>$engineB</span> $charsetB</td>
</tr>
<tr><td style='width:50%'><pre>".$debugA."</pre></td>
<td style='width:50%'><pre>".$debugB."</pre></td></tr></table>";
Expand Down Expand Up @@ -489,13 +494,13 @@ public function prepareResults($tbl, $selection, $sqlData, $fileData)

}

if ($fileData['engine'] != $sqlData['engine'])
if ($fileData['engine'] !== $sqlData['engine'])
{
$this->errors[$tbl]['_status'] |= self::STATUS_TABLE_MISMATCH_STORAGE_ENGINE;
$this->errors[$tbl]['_valid_' . self::STATUS_TABLE_MISMATCH_STORAGE_ENGINE] = $fileData['engine'];
$this->errors[$tbl]['_invalid_' . self::STATUS_TABLE_MISMATCH_STORAGE_ENGINE] = $sqlData['engine'];
}
if ($fileData['charset'] != $sqlData['charset'])
if ($fileData['charset'] !== $sqlData['charset'])
{
$this->errors[$tbl]['_status'] |= self::STATUS_TABLE_MISMATCH_DEFAULT_CHARSET;
$this->errors[$tbl]['_valid_' . self::STATUS_TABLE_MISMATCH_DEFAULT_CHARSET] = $fileData['charset'];
Expand All @@ -520,7 +525,7 @@ function compileResults()
{
foreach($this->results as $tabs => $field)
{
$file = varset($this->results[$tabs]['_file']);
$file = varset($this->results[$tabs]['_file'],$tabs);
$errorStatus = is_int($this->errors[$tabs]['_status']) ?
$this->errors[$tabs]['_status'] : self::STATUS_TABLE_OK;

Expand Down Expand Up @@ -599,6 +604,10 @@ public function errors()
return $badTableCount;
}

public function getErrors()
{
return $this->errors;
}

/**
* @param $fileArray
Expand Down Expand Up @@ -642,7 +651,7 @@ function renderResults($fileArray=array())
self::STATUS_TABLE_MISMATCH_DEFAULT_CHARSET => DBVLAN_18,
'mismatch' => DBVLAN_8,
'missing_field' => DBVLAN_11,
'ok' => ADMIN_TRUE_ICON,
'ok' => defset('ADMIN_TRUE_ICON','true'),
'missing_index' => DBVLAN_25,
);

Expand Down Expand Up @@ -931,7 +940,7 @@ function getFixQuery(
$fdata = $this->getIndex($sqlFileData);
$newval = $this->toMysql($fdata[$field],'index');
}
else
elseif($mode == 'alter' || $mode === 'insert' || $mode === 'index')
{
$fdata = $this->getFields($sqlFileData);
$newval = $this->toMysql($fdata[$field]);
Expand Down Expand Up @@ -971,10 +980,14 @@ function getFixQuery(
case 'convert':
$showCreateTable = $this->getSqlData($table);
$currentSchema = $this->getSqlFileTables($showCreateTable);
if ($engine != $currentSchema['engine'][0])
$query .= "ALTER TABLE `".MPREFIX.$table."` ENGINE=".$engine.";";
if ($charset != $currentSchema['charset'][0])
$query .= "ALTER TABLE `".MPREFIX.$table."` CONVERT TO CHARACTER SET ".$charset.";";
if($engine != $currentSchema['engine'][0])
{
$query .= "ALTER TABLE `" . MPREFIX . $table . "` ENGINE=" . $engine . ";";
}
if($charset != $currentSchema['charset'][0])
{
$query .= "ALTER TABLE `" . MPREFIX . $table . "` CONVERT TO CHARACTER SET " . $charset . ";";
}
break;
}

Expand All @@ -998,10 +1011,10 @@ function runFix($fixArray='')
$fixArray = $this->fixList; // Fix All
}


foreach($fixArray as $j=>$file)
{

foreach($file as $table=>$val)
{

Expand Down Expand Up @@ -1074,7 +1087,9 @@ function getSqlFileTables($sql_data)

$sql_data = preg_replace("#\/\*.*?\*\/#mis", '', $sql_data); // remove comments

$regex = "/CREATE TABLE (?:IF NOT EXISTS )?`?([\w]*)`?\s*?\(([^;]*)\)\s*((?:[\w\s]+=[^\s]+)+\s*)*;/i";
// $regex = "/CREATE TABLE (?:IF NOT EXISTS )?`?([\w]*)`?\s*?\(([^;]*)\)\s*((?:[\w\s]+=[^\s]+)+\s*)*;/i";
// $regex = "/CREATE TABLE (?:IF NOT EXISTS )?`?(\w*)`?\s*?\(([^;]*)\)\s*((?:[\w\s]+=\S+)+\s*)*;/i";
$regex = "/CREATE TABLE (?:IF NOT EXISTS )?`?(\w*)`?\s*?\(([^;]*)\)\s*((?:[\w\s]+=[^;]+)+\s*)*;/i";

preg_match_all($regex,$sql_data,$match);

Expand Down Expand Up @@ -1116,7 +1131,8 @@ function getSqlFileTables($sql_data)
$engine = null;
$charset = null;

$tableOptionsRegex = "/([\w\s]+=[\w]+)+?\s*/";
// $tableOptionsRegex = "/([\w\s]+=[\w]+)+?\s*/";
$tableOptionsRegex = "/([\w\s]+=\s?\w+)+?\s*/";
preg_match_all($tableOptionsRegex, $rawTableOptions, $tableOptionsSplit);
$tableOptionsSplit = current($tableOptionsSplit);
foreach ($tableOptionsSplit as $rawTableOption)
Expand All @@ -1139,7 +1155,7 @@ function getSqlFileTables($sql_data)
}
}

$ret['engine'][] = $engine;
$ret['engine'][] = str_replace('MYISAM', 'MyISAM', $engine);
$ret['charset'][] = $charset;
}

Expand Down Expand Up @@ -1383,7 +1399,7 @@ function renderTableSelect()
</colgroup>
<thead>
<tr>
<th class='first form-inline' colspan='3'><label for='check-all-verify-jstarget-verify-table'>".$frm->checkbox_toggle('check-all-verify', 'verify_table', false )." ".LAN_CHECKALL.' | '.LAN_UNCHECKALL."</label></th>
<th class='first form-inline' colspan='3'><label for='check-all-verify-jstarget-verify-table'>".$frm->checkbox_toggle('check-all-verify', 'verify_table' )." ".LAN_CHECKALL.' | '.LAN_UNCHECKALL."</label></th>
</tr>
</thead>
<tbody>
Expand All @@ -1408,7 +1424,7 @@ function renderTableSelect()
}
else
{
$icon = E_16_E107;
$icon = defset('E_16_E107');
$name = LAN_CORE;
}
$text .= ($c === 0) ? "<tr>\n" : '';
Expand Down Expand Up @@ -1461,7 +1477,7 @@ function renderTableSelect()
*
* @return string[] An unordered list of the storage engines supported by the current MySQL server
*/
private static function getAvailableStorageEngines()
private function getAvailableStorageEngines()
{
$db = e107::getDb();
$db->gen("SHOW ENGINES;");
Expand All @@ -1481,8 +1497,20 @@ private static function getAvailableStorageEngines()
*/
public function getIntendedStorageEngine($maybeStorageEngine = null)
{
if ($maybeStorageEngine === null)

if($maybeStorageEngine === null)
{
return $this->getIntendedStorageEngine(self::MOST_PREFERRED_STORAGE_ENGINE);
}

if(strtoupper($maybeStorageEngine) === 'MYISAM')
{
$maybeStorageEngine = 'MyISAM';
}
elseif(strtoupper($maybeStorageEngine) === 'INNODB')
{
$maybeStorageEngine = 'InnoDB';
}

if (!array_key_exists($maybeStorageEngine, $this->storageEnginePreferenceMap))
{
Expand Down

0 comments on commit 3d000c8

Please sign in to comment.