db check working

This commit is contained in:
Paul Spenke
2023-06-30 16:37:44 +08:00
parent 77c642984c
commit 400d960f81
10 changed files with 193 additions and 99 deletions
+56 -71
View File
@@ -1,75 +1,60 @@
<?php <?php
return [ return [
'settings' => "CREATE TABLE `settings` ( 'settings' => [
`id` int NOT NULL, 'columns' => [
`setting` varchar(50) NOT NULL, 'setting' => 'varchar(50) NOT NULL',
`value` mediumtext NOT NULL, 'value' => 'mediumtext NOT NULL',
`is_array` tinyint(1) NOT NULL 'is_array' => 'tinyint(1) NOT NULL',
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; ],
ALTER TABLE `settings` 'uniqueKeys' => [
ADD PRIMARY KEY (`id`), 'setting' => ['setting']
ADD UNIQUE KEY `setting` (`setting`); ],
ALTER TABLE `settings` ],
MODIFY `id` int NOT NULL AUTO_INCREMENT;", 'pages' => [
'pages' => "CREATE TABLE `pages` ( 'columns' => [
`id` int NOT NULL, 'name' => 'varchar(255)',
`name` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci NOT NULL, 'layout' => 'varchar(255)',
`layout` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci NOT NULL, 'data' => 'longtext',
`data` longtext CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci, 'updated_at' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 'created_at' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ],
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; 'uniqueKeys' => [],
INSERT INTO `pages` (`id`, `name`, `layout`, `data`, `updated_at`, `created_at`) VALUES ],
(1, 'Home', 'master', NULL, '2023-06-05 07:10:09', '2023-06-05 07:10:09'), 'menus' => [
(2, 'About', 'master', NULL, '2023-06-05 07:10:57', '2023-06-05 07:10:57'); 'columns' => [
ALTER TABLE `pages` 'name' => 'varchar(255)',
ADD PRIMARY KEY (`id`); 'pages' => 'mediumtext',
ALTER TABLE `pages` ],
MODIFY `id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;", 'uniqueKeys' => [
'menus' => "CREATE TABLE `menus` ( 'name' => ['name']
`id` int NOT NULL, ],
`name` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL, ],
`pages` mediumtext CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NOT NULL 'page_translations' => [
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; 'columns' => [
INSERT INTO `menus` (`id`, `name`, `pages`) VALUES 'page_id' => 'int NOT NULL',
(1, 'Main', '1,2'), 'locale' => 'varchar(50) NOT NULL',
(2, 'Footer', '1'); 'title' => 'varchar(255)',
ALTER TABLE `menus` 'meta_title' => 'varchar(255)',
ADD PRIMARY KEY (`id`), 'meta_description' => 'varchar(255)',
ADD UNIQUE KEY `menu` (`name`); 'route' => 'varchar(255)',
ALTER TABLE `menus` 'updated_at' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
MODIFY `id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;", 'created_at' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP',
'page_translations' => "CREATE TABLE `page_translations` ( ],
`id` int NOT NULL, 'uniqueKeys' => [
`page_id` int NOT NULL, 'page_id' => ['page_id','locale']
`locale` varchar(50) NOT NULL, ],
`title` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci NOT NULL, ],
`meta_title` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci NOT NULL, 'uploads' => [
`meta_description` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci NOT NULL, 'columns' => [
`route` varchar(255) CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci NOT NULL, 'public_id' => 'varchar(50)',
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 'original_file' => 'varchar(512)',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP 'mime_type' => 'varchar(50)',
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; 'server_file' => 'varchar(512)',
INSERT INTO `page_translations` (`id`, `page_id`, `locale`, `title`, `meta_title`, `meta_description`, `route`, `updated_at`, `created_at`) VALUES ],
(1, 1, 'de', 'Home', 'Home', 'Home', '/', '2023-06-05 07:10:09', '2023-06-05 07:10:09'), 'uniqueKeys' => [
(2, 2, 'de', 'About', 'About', 'Description', '/about', '2023-06-05 07:10:57', '2023-06-05 07:10:57'); 'public_id' => ['public_id'],
ALTER TABLE `page_translations` 'server_file' => ['server_file']
ADD PRIMARY KEY (`id`), ],
ADD UNIQUE KEY `page_id` (`page_id`,`locale`); ],
ALTER TABLE `page_translations`
MODIFY `id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;",
'uploads' => "CREATE TABLE `uploads` (
`id` int NOT NULL,
`public_id` varchar(50) NOT NULL,
`original_file` varchar(512) NOT NULL,
`mime_type` varchar(50) NOT NULL,
`server_file` varchar(512) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
ALTER TABLE `uploads`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `public_id` (`public_id`),
ADD UNIQUE KEY `server_file` (`server_file`);
ALTER TABLE `uploads`
MODIFY `id` int NOT NULL AUTO_INCREMENT;",
]; ];
+1 -1
View File
File diff suppressed because one or more lines are too long
+1
View File
@@ -45,6 +45,7 @@ return [
'settings-updated' => 'Einstellungen aktualisiert', 'settings-updated' => 'Einstellungen aktualisiert',
'save-menus' => 'Menus speichern', 'save-menus' => 'Menus speichern',
'menus-updated' => 'Menus aktualisiert', 'menus-updated' => 'Menus aktualisiert',
'db-updated' => 'Datenbank aktualisiert',
'pagebuilder-block-images' => 'Seiteneditor Vorschaubilder', 'pagebuilder-block-images' => 'Seiteneditor Vorschaubilder',
'render-thumbs' => 'Vorschaubilder generieren', 'render-thumbs' => 'Vorschaubilder generieren',
'check-database' => 'Datenbank überprüfen im Info-Tab' 'check-database' => 'Datenbank überprüfen im Info-Tab'
+1
View File
@@ -45,6 +45,7 @@ return [
'settings-updated' => 'Settings successfully updated', 'settings-updated' => 'Settings successfully updated',
'save-menus' => 'Save menus', 'save-menus' => 'Save menus',
'menus-updated' => 'Menus updated', 'menus-updated' => 'Menus updated',
'db-updated' => 'Database updated',
'pagebuilder-block-images' => 'Pagebuilder block thumbnails', 'pagebuilder-block-images' => 'Pagebuilder block thumbnails',
'render-thumbs' => 'Render Thumbnails', 'render-thumbs' => 'Render Thumbnails',
'check-database' => 'Check database' 'check-database' => 'Check database'
+1
View File
@@ -45,6 +45,7 @@ return [
'selected-theme' => 'Selected theme', 'selected-theme' => 'Selected theme',
'save-menus' => 'Save menus', 'save-menus' => 'Save menus',
'menus-updated' => 'Menus updated', 'menus-updated' => 'Menus updated',
'db-updated' => 'Database updated',
'pagebuilder-block-images' => 'Listado de bloques', 'pagebuilder-block-images' => 'Listado de bloques',
'render-thumbs' => 'Mostrar Miniaturas', 'render-thumbs' => 'Mostrar Miniaturas',
'check-database' => 'Check database' 'check-database' => 'Check database'
+1
View File
@@ -45,6 +45,7 @@ return [
'settings-updated' => 'Instellingen opgeslagen', 'settings-updated' => 'Instellingen opgeslagen',
'save-menus' => 'Save menus', 'save-menus' => 'Save menus',
'menus-updated' => 'Menus updated', 'menus-updated' => 'Menus updated',
'db-updated' => 'Database updated',
'pagebuilder-block-images' => 'Pagebuilder blok afbeeldingen', 'pagebuilder-block-images' => 'Pagebuilder blok afbeeldingen',
'render-thumbs' => 'Blok afbeeldingen genereren', 'render-thumbs' => 'Blok afbeeldingen genereren',
'check-database' => 'Check database' 'check-database' => 'Check database'
+68 -14
View File
@@ -119,28 +119,82 @@ class DB
function tableExists($table) { function tableExists($table) {
try { try {
$this->pdo->query("SELECT 1 FROM {$table} LIMIT 1"); $this->pdo->query("SELECT 1 FROM {$table} LIMIT 1");
return TRUE; return true;
} catch (Exception $e) { } catch (Exception $e) {
return FALSE; return false;
} }
} }
/** public function columnExists(string $table, string $column)
* Perform a custom query with user input data passed as $parameters.
*
* @param string $table
* @param string $sql
* @return bool
*/
public function prepare(string $table, string $sql)
{ {
// check if table with the name $table exists. if not, execute $query.' // check if table with the name $table exists. if not, execute $query.'
try { try {
$this->pdo->query("SELECT 1 FROM {$table} LIMIT 1"); $rs = $this->pdo->query("SELECT * FROM {$table} LIMIT 0");
return TRUE; for ($i = 0; $i < $rs->columnCount(); $i++) {
$col = $rs->getColumnMeta($i);
$columns[] = $col['name'];
}
if (in_array($column, $columns)) return true;
return false;
} catch (Exception $e) { } catch (Exception $e) {
$stmt = $this->pdo->prepare($sql); return false;
return $stmt->execute(); }
}
public function uniqueKeyExists(string $table, string $key)
{
// check if table with the name $table exists. if not, execute $query.'
try {
$keys = $this->select(
"SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME = ? AND TABLE_SCHEMA = 'plugi' AND TABLE_NAME = ? AND CONSTRAINT_TYPE = 'UNIQUE'",
[$key, $table]
);
if (count($keys) === 1) return true;
return false;
} catch (Exception $e) {
print_r($e->getMessage());
return false;
}
}
public function diffDB() {
$tables = include __DIR__.'/../../config/tables.php';
$diff = [];
foreach ($tables as $table => $tableDefinition) {
if ($this->tableExists($table) === false) {
$diff[$table] = false;
} else {
$diff[$table]['columns'] = [];
foreach ($tableDefinition['columns'] as $column => $columnDefinition) {
if(!$this->columnExists($table, $column)) $diff[$table]['columns'][$column] = $columnDefinition;
}
$diff[$table]['uniqueKeys'] = [];
foreach ($tableDefinition['uniqueKeys'] as $key => $keyDefinition) {
if(!$this->uniqueKeyExists($table, $key)) $diff[$table]['uniqueKeys'][$key] = '`'.implode('`,`', $keyDefinition).'`';
}
}
}
return $diff;
}
public function resolveDiffDB() {
$diff = $this->diffDB();
foreach ($diff as $table => $tableDiff) {
if($tableDiff === false){
$this->query("CREATE TABLE {$table} (`id` int NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3");
$this->query("ALTER TABLE {$table} ADD PRIMARY KEY (`id`)");
$this->query("ALTER TABLE {$table} MODIFY `id` int NOT NULL AUTO_INCREMENT");
} elseif (is_array($tableDiff)) {
foreach ($tableDiff['columns'] as $column => $columnDefinition) {
$this->query("ALTER TABLE {$table} ADD {$column} {$columnDefinition}");
}
foreach ($tableDiff['uniqueKeys'] as $key => $keyDefinition) {
$this->query("ALTER TABLE {$table} ADD UNIQUE KEY {$key} ($keyDefinition)");
}
}
} }
} }
} }
@@ -44,6 +44,13 @@ class WebsiteManager implements WebsiteManagerContract
} }
} }
if ($route === 'database') {
if ($action === 'update') {
$this->handleUpdateDB();
exit();
}
}
if ($route === 'page_settings') { if ($route === 'page_settings') {
if ($action === 'create') { if ($action === 'create') {
$this->handleCreate(); $this->handleCreate();
@@ -155,6 +162,21 @@ class WebsiteManager implements WebsiteManagerContract
} }
} }
/**
* Handle requests for updating the website DB.
*/
public function handleUpdateDB()
{
global $phpb_db;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$phpb_db->resolveDiffDB();
phpb_redirect(phpb_url('website_manager', ['tab' => 'info']), [
'message-type' => 'success',
'message' => phpb_trans('website-manager.db-updated')
]);
}
}
/** /**
* Render the website manager overview page. * Render the website manager overview page.
*/ */
@@ -8,20 +8,51 @@
<tr> <tr>
<th class="w-60">Database</th> <th class="w-60">Database</th>
<td> <td>
<form method="post" action="<?= phpb_url('website_manager', ['route' => 'database', 'action' => 'update', 'tab' => 'info']) ?>">
<?php <?php
global $phpb_db; global $phpb_db;
$tables = include __DIR__.'/../../../../../../config/tables.php'; $diff = $phpb_db->diffDB();
foreach ($tables as $table => $structure) : $actionNeeded = false;
?> foreach ($diff as $table => $tableDiff) :
<li> if(!is_array($tableDiff)) $actionNeeded = true;
<?= phpb_e($table) ?> (<?= phpb_e($phpb_db->tableExists($table) ? 'yes' : 'no') ?>)
<ul>
<li></li>
</ul>
</li>
<?php
endforeach;
?> ?>
<div>
table <strong><?= phpb_e($table) ?></strong> (<?= phpb_e(is_array($tableDiff) ? (empty($tableDiff['columns']) && empty($tableDiff['uniqueKeys']) ? 'is fine' : 'has diff') : 'is not existent') ?>)
</div>
<?php
if (is_array($tableDiff)) :
foreach ($tableDiff['columns'] as $column => $def) :
$actionNeeded = true;
?>
<div class="ml-4">
<i class="ph ph-arrow-elbow-down-right"></i> column <strong><?= phpb_e($column) ?></strong> (not existent)
</div>
<?php
endforeach;
endif;
?>
<?php
if (is_array($tableDiff)) :
foreach ($tableDiff['uniqueKeys'] as $key => $def) :
$actionNeeded = true;
?>
<div class="ml-4">
<i class="ph ph-arrow-elbow-down-right"></i> unique key <strong><?= phpb_e($key) ?></strong> (not existent)
</div>
<?php
endforeach;
endif;
?>
<?php
endforeach;
if ($actionNeeded) :
?>
<button class="btn" type="submit">Update</button>
<?php
endif;
?>
</form>
</td> </td>
</tr> </tr>
<tr> <tr>
@@ -16,7 +16,6 @@ $settingIns = phpb_instance('setting');
<label class="label cursor-pointer"> <label class="label cursor-pointer">
<span class="label-text"><?= phpb_e($localeText) ?></span> <span class="label-text"><?= phpb_e($localeText) ?></span>
<input type="checkbox" name="languages[]" value="<?= phpb_e($locale) ?>" <?= phpb_e($settingIns::has('languages', $locale)) ? 'checked' : '' ?> class="checkbox checkbox-sm" /> <input type="checkbox" name="languages[]" value="<?= phpb_e($locale) ?>" <?= phpb_e($settingIns::has('languages', $locale)) ? 'checked' : '' ?> class="checkbox checkbox-sm" />
</label> </label>
</div> </div>
<?php <?php
@@ -116,6 +115,5 @@ $settingIns = phpb_instance('setting');
<!-- <a href="--><?php //= phpb_url('website_manager', ['route' => 'settings', 'action' => 'renderBlockThumbs']) ?><!--" class="btn btn-secondary btn-sm mr-1">--> <!-- <a href="--><?php //= phpb_url('website_manager', ['route' => 'settings', 'action' => 'renderBlockThumbs']) ?><!--" class="btn btn-secondary btn-sm mr-1">-->
<!-- --><?php //= phpb_trans('website-manager.render-thumbs') ?> <!-- --><?php //= phpb_trans('website-manager.render-thumbs') ?>
<!-- </a>--> <!-- </a>-->
</div>
</form> </form>