Fixes admin/plugins. Shows tables - owned by plugin and just referenced

main
Yasen Pramatarov 2026-01-19 21:28:35 +02:00
parent 4c5136adf4
commit 80aaa0cab6
2 changed files with 70 additions and 22 deletions

View File

@ -603,25 +603,41 @@ if ($queryAction === 'plugin_check_page' && isset($_GET['plugin'])) {
// Check database tables // Check database tables
$db = \App\App::db(); $db = \App\App::db();
$pluginTables = []; $pluginOwnedTables = [];
if ($db instanceof PDO) { $pluginReferencedTables = [];
$stmt = $db->query("SHOW TABLES"); if ($db && method_exists($db, 'getConnection')) {
$pdo = $db->getConnection();
$stmt = $pdo->query("SHOW TABLES");
$allTables = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); $allTables = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
if ($hasMigration) { if ($hasMigration) {
// Check each migration file for table references
foreach ($migrationFiles as $migrationFile) { foreach ($migrationFiles as $migrationFile) {
$migrationContent = file_get_contents($migrationFile); $migrationContent = file_get_contents($migrationFile);
// Extract tables created by this migration (plugin-owned)
if (preg_match_all('/CREATE\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?\s+`?([a-zA-Z0-9_]+)`?/i', $migrationContent, $matches)) {
foreach ($matches[1] as $tableName) {
if (in_array($tableName, $allTables)) {
$pluginOwnedTables[] = $tableName;
}
}
}
// Find all referenced tables (dependencies)
foreach ($allTables as $table) { foreach ($allTables as $table) {
if (strpos($migrationContent, $table) !== false) { if (strpos($migrationContent, $table) !== false && !in_array($table, $pluginOwnedTables)) {
$pluginTables[] = $table; $pluginReferencedTables[] = $table;
} }
} }
} }
$pluginTables = array_unique($pluginTables); $pluginOwnedTables = array_unique($pluginOwnedTables);
$pluginReferencedTables = array_unique($pluginReferencedTables);
} }
} }
$checkResults['tables'] = $pluginTables; $checkResults['tables'] = [
'owned' => $pluginOwnedTables,
'referenced' => $pluginReferencedTables,
];
// Check plugin functions // Check plugin functions
$bootstrapPath = $pluginInfo['path'] . '/bootstrap.php'; $bootstrapPath = $pluginInfo['path'] . '/bootstrap.php';

View File

@ -628,25 +628,41 @@ endif; ?>
// Check database tables // Check database tables
$db = \App\App::db(); $db = \App\App::db();
$pluginTables = []; $pluginOwnedTables = [];
if ($db instanceof PDO) { $pluginReferencedTables = [];
$stmt = $db->query("SHOW TABLES"); if ($db && method_exists($db, 'getConnection')) {
$pdo = $db->getConnection();
$stmt = $pdo->query("SHOW TABLES");
$allTables = $stmt->fetchAll(PDO::FETCH_COLUMN, 0); $allTables = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
if ($hasMigration) { if ($hasMigration) {
// Check each migration file for table references
foreach ($migrationFiles as $migrationFile) { foreach ($migrationFiles as $migrationFile) {
$migrationContent = file_get_contents($migrationFile); $migrationContent = file_get_contents($migrationFile);
// Extract tables created by this migration (plugin-owned)
if (preg_match_all('/CREATE\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?\s+`?([a-zA-Z0-9_]+)`?/i', $migrationContent, $matches)) {
foreach ($matches[1] as $tableName) {
if (in_array($tableName, $allTables)) {
$pluginOwnedTables[] = $tableName;
}
}
}
// Find all referenced tables (dependencies)
foreach ($allTables as $table) { foreach ($allTables as $table) {
if (strpos($migrationContent, $table) !== false) { if (strpos($migrationContent, $table) !== false && !in_array($table, $pluginOwnedTables)) {
$pluginTables[] = $table; $pluginReferencedTables[] = $table;
} }
} }
} }
$pluginTables = array_unique($pluginTables); $pluginOwnedTables = array_unique($pluginOwnedTables);
$pluginReferencedTables = array_unique($pluginReferencedTables);
} }
} }
$checkResults['tables'] = $pluginTables; $checkResults['tables'] = [
'owned' => $pluginOwnedTables,
'referenced' => $pluginReferencedTables,
];
// Check plugin functions and integrations // Check plugin functions and integrations
$bootstrapPath = $plugin['path'] . '/bootstrap.php'; $bootstrapPath = $plugin['path'] . '/bootstrap.php';
@ -773,13 +789,29 @@ endif; ?>
<h6 class="card-title mb-0">Database Tables</h6> <h6 class="card-title mb-0">Database Tables</h6>
</div> </div>
<div class="card-body"> <div class="card-body">
<?php if (!empty($checkResults['tables'])): ?> <?php if (!empty($checkResults['tables']['owned']) || !empty($checkResults['tables']['referenced'])): ?>
<?php foreach ($checkResults['tables'] as $table): ?> <?php if (!empty($checkResults['tables']['owned'])): ?>
<div class="d-flex justify-content-between align-items-center mb-2"> <div class="mb-3">
<span><?= htmlspecialchars($table) ?></span> <strong class="text-danger">Plugin Tables (removed on purge):</strong>
<span class="badge bg-success">Present</span> <?php foreach ($checkResults['tables']['owned'] as $table): ?>
<div class="d-flex justify-content-between align-items-center mb-2 mt-2">
<span><i class="fas fa-database text-danger"></i> <?= htmlspecialchars($table) ?></span>
<span class="badge bg-danger">Owned</span>
</div>
<?php endforeach; ?>
</div> </div>
<?php endforeach; ?> <?php endif; ?>
<?php if (!empty($checkResults['tables']['referenced'])): ?>
<div>
<strong class="text-muted">Referenced Tables (dependencies):</strong>
<?php foreach ($checkResults['tables']['referenced'] as $table): ?>
<div class="d-flex justify-content-between align-items-center mb-2 mt-2">
<span><i class="fas fa-link text-muted"></i> <?= htmlspecialchars($table) ?></span>
<span class="badge bg-secondary">Referenced</span>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php else: ?> <?php else: ?>
<p class="text-muted mb-0"> <p class="text-muted mb-0">
<?php if ($checkResults['files']['migration']): ?> <?php if ($checkResults['files']['migration']): ?>