mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-05-07 03:29:19 +02:00
refactor(Database): remove dead CRUD helpers and alias proliferation
Remove 5 unused ID-lookup helpers (getOrientationId, getAPProgramId,
getFinalityId, getLanguageId, getFormatId) — forms have always passed
FK ids directly from <select> elements; these methods were never called
outside import.php, which now uses inline PDO queries instead.
Collapse 13 alias methods down to the single canonical name for each:
getAllOrientations, getAllAPPrograms, getAllFinalityTypes,
getAllFormatTypes, getAllLanguages, getAllLicenseTypes,
getUsedTags, findOrCreateTag
The short-name variants (getOrientations, getApPrograms, etc.) and
compat aliases (getUsedKeywords, findOrCreateKeyword, getAllLicenseTypes
delegating to getLicenseTypes) are deleted. All call-sites updated:
- public/search.php: getOrientations→getAllOrientations, etc.
- public/admin/import.php: findOrCreateKeyword→findOrCreateTag,
thesis_keywords→thesis_tags, keyword_id→tag_id (fixes stale table
reference from pre-migration-001 that bypassed the M2M rename)
- tests/Unit/DatabaseTest.php: remove alias smoke-test (test 7)
Database.php: 948 → 848 lines (-100).
This commit is contained in:
16
TODO.md
16
TODO.md
@@ -378,16 +378,14 @@ Goal: rename the tables and column to the canonical M2M pattern (`tags`, `thesis
|
||||
|
||||
### B — PHP / Database.php
|
||||
|
||||
- [ ] **Dead CRUD helpers** — `getOrientationId()`, `getAPProgramId()`, `getFinalityId()`,
|
||||
`getLanguageId()`, `getFormatId()` are defined in `Database.php` but **never called** anywhere
|
||||
(forms now pass IDs directly from selects). Remove them to reduce surface area.
|
||||
- [x] **Dead CRUD helpers** — `getOrientationId()`, `getAPProgramId()`, `getFinalityId()`,
|
||||
`getLanguageId()`, `getFormatId()` removed from `Database.php`; `import.php` updated to
|
||||
use inline PDO queries in their place.
|
||||
|
||||
- [ ] **Alias proliferation** — `getOrientations()`/`getAllOrientations()`, `getApPrograms()`/`getAllAPPrograms()`,
|
||||
`getFinalityTypes()`/`getAllFinalityTypes()`, `getLanguages()`/`getAllLanguages()`,
|
||||
`getFormatTypes()`/`getAllFormatTypes()`, `getLicenseTypes()`/`getAllLicenseTypes()`,
|
||||
`findOrCreateKeyword()`, `getUsedKeywords()` — **13 alias methods** pointing at 6 real ones.
|
||||
Pick the canonical name for each pair, update all call-sites (there are few), and delete aliases.
|
||||
Reduces Database.php from ~945 lines significantly.
|
||||
- [x] **Alias proliferation** — collapsed 13 alias methods: canonical names are the `getAllXxx`
|
||||
variants (`getAllOrientations`, `getAllAPPrograms`, `getAllFinalityTypes`, `getAllFormatTypes`,
|
||||
`getAllLanguages`, `getAllLicenseTypes`) plus `getUsedTags` and `findOrCreateTag`; all
|
||||
call-sites updated (`search.php`, `import.php`); Database.php reduced from 948 → 848 lines.
|
||||
|
||||
- [ ] **`getPDO()` / `getConnection()` leaking to callers** — `edit.php`, `formulaire.php`,
|
||||
`thanks.php`, `import.php`, `tfe.php`, `index.php`, `media.php`, `system.php` all call
|
||||
|
||||
@@ -142,7 +142,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
|
||||
$orientationName = isset($orientationMap[$orientationCode]) ? $orientationMap[$orientationCode] : null;
|
||||
$orientationId = null;
|
||||
if ($orientationName) {
|
||||
$orientationId = $db->getOrientationId($orientationName);
|
||||
$stmtOr = $pdo->prepare("SELECT id FROM orientations WHERE name = ?");
|
||||
$stmtOr->execute([$orientationName]);
|
||||
$rowOr = $stmtOr->fetch();
|
||||
$orientationId = $rowOr ? $rowOr['id'] : null;
|
||||
}
|
||||
|
||||
// Map AP program
|
||||
@@ -159,7 +162,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
|
||||
// Map finality
|
||||
$finalityId = null;
|
||||
if (!empty($finalityName)) {
|
||||
$finalityId = $db->getFinalityId($finalityName);
|
||||
$stmtFin = $pdo->prepare("SELECT id FROM finality_types WHERE name = ?");
|
||||
$stmtFin->execute([$finalityName]);
|
||||
$rowFin = $stmtFin->fetch();
|
||||
$finalityId = $rowFin ? $rowFin['id'] : null;
|
||||
}
|
||||
|
||||
// Insert thesis
|
||||
@@ -221,10 +227,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
|
||||
$keywords = array_slice($keywords, 0, 10); // Max 10
|
||||
foreach ($keywords as $keyword) {
|
||||
if (!empty($keyword)) {
|
||||
$keywordId = $db->findOrCreateKeyword($keyword);
|
||||
if ($keywordId) {
|
||||
$stmt = $pdo->prepare("INSERT INTO thesis_keywords (thesis_id, keyword_id) VALUES (?, ?)");
|
||||
$stmt->execute([$thesisId, $keywordId]);
|
||||
$tagId = $db->findOrCreateTag($keyword);
|
||||
if ($tagId) {
|
||||
$stmtTag = $pdo->prepare("INSERT INTO thesis_tags (thesis_id, tag_id) VALUES (?, ?)");
|
||||
$stmtTag->execute([$thesisId, $tagId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -232,7 +238,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
|
||||
|
||||
// Add language
|
||||
if (!empty($languageRaw)) {
|
||||
$languageId = $db->getLanguageId(ucfirst(strtolower($languageRaw)));
|
||||
$stmtLang = $pdo->prepare("SELECT id FROM languages WHERE name = ?");
|
||||
$stmtLang->execute([ucfirst(strtolower($languageRaw))]);
|
||||
$rowLang = $stmtLang->fetch();
|
||||
$languageId = $rowLang ? $rowLang['id'] : null;
|
||||
if ($languageId) {
|
||||
$stmt = $pdo->prepare("INSERT INTO thesis_languages (thesis_id, language_id) VALUES (?, ?)");
|
||||
$stmt->execute([$thesisId, $languageId]);
|
||||
@@ -244,7 +253,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
|
||||
$formats = array_map('trim', explode(',', $formatsRaw));
|
||||
foreach ($formats as $formatName) {
|
||||
if (!empty($formatName)) {
|
||||
$formatId = $db->getFormatId(ucfirst(strtolower($formatName)));
|
||||
$stmtFmt = $pdo->prepare("SELECT id FROM format_types WHERE name = ?");
|
||||
$stmtFmt->execute([ucfirst(strtolower($formatName))]);
|
||||
$rowFmt = $stmtFmt->fetch();
|
||||
$formatId = $rowFmt ? $rowFmt['id'] : null;
|
||||
if ($formatId) {
|
||||
$stmt = $pdo->prepare("INSERT INTO thesis_formats (thesis_id, format_id) VALUES (?, ?)");
|
||||
$stmt->execute([$thesisId, $formatId]);
|
||||
|
||||
@@ -43,9 +43,9 @@ try {
|
||||
}
|
||||
|
||||
$years = $db->getAvailableYears();
|
||||
$orientations = $db->getOrientations();
|
||||
$apPrograms = $db->getApPrograms();
|
||||
$keywords = $db->getUsedKeywords();
|
||||
$orientations = $db->getAllOrientations();
|
||||
$apPrograms = $db->getAllAPPrograms();
|
||||
$keywords = $db->getUsedTags();
|
||||
// Fetch all published theses for the student index (no artificial cap)
|
||||
$students = $db->getAllPublishedTheses();
|
||||
} catch (InvalidArgumentException $e) {
|
||||
|
||||
124
src/Database.php
124
src/Database.php
@@ -425,51 +425,27 @@ class Database {
|
||||
/**
|
||||
* Get all orientations
|
||||
*/
|
||||
public function getOrientations() {
|
||||
$sql = "SELECT * FROM orientations ORDER BY name";
|
||||
$stmt = $this->pdo->query($sql);
|
||||
public function getAllOrientations(): array {
|
||||
$stmt = $this->pdo->query("SELECT * FROM orientations ORDER BY name");
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for formulaire compatibility
|
||||
*/
|
||||
public function getAllOrientations() {
|
||||
return $this->getOrientations();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all AP programs
|
||||
*/
|
||||
public function getApPrograms() {
|
||||
$sql = "SELECT * FROM ap_programs ORDER BY name";
|
||||
$stmt = $this->pdo->query($sql);
|
||||
public function getAllAPPrograms(): array {
|
||||
$stmt = $this->pdo->query("SELECT * FROM ap_programs ORDER BY name");
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for formulaire compatibility
|
||||
*/
|
||||
public function getAllAPPrograms() {
|
||||
return $this->getApPrograms();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all finality types
|
||||
*/
|
||||
public function getFinalityTypes() {
|
||||
$sql = "SELECT * FROM finality_types ORDER BY name";
|
||||
$stmt = $this->pdo->query($sql);
|
||||
public function getAllFinalityTypes(): array {
|
||||
$stmt = $this->pdo->query("SELECT * FROM finality_types ORDER BY name");
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for formulaire compatibility
|
||||
*/
|
||||
public function getAllFinalityTypes() {
|
||||
return $this->getFinalityTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all keywords used in published theses
|
||||
*/
|
||||
@@ -483,43 +459,22 @@ class Database {
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
/** Backwards-compat alias */
|
||||
public function getUsedKeywords(): array {
|
||||
return $this->getUsedTags();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all format types
|
||||
*/
|
||||
public function getFormatTypes() {
|
||||
$sql = "SELECT * FROM format_types ORDER BY name";
|
||||
$stmt = $this->pdo->query($sql);
|
||||
public function getAllFormatTypes(): array {
|
||||
$stmt = $this->pdo->query("SELECT * FROM format_types ORDER BY name");
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for formulaire compatibility
|
||||
*/
|
||||
public function getAllFormatTypes() {
|
||||
return $this->getFormatTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all languages
|
||||
*/
|
||||
public function getLanguages() {
|
||||
$sql = "SELECT * FROM languages ORDER BY name";
|
||||
$stmt = $this->pdo->query($sql);
|
||||
public function getAllLanguages(): array {
|
||||
$stmt = $this->pdo->query("SELECT * FROM languages ORDER BY name");
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for formulaire compatibility
|
||||
*/
|
||||
public function getAllLanguages() {
|
||||
return $this->getLanguages();
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// ADMIN LIST METHOD
|
||||
// ========================================================================
|
||||
@@ -649,10 +604,7 @@ class Database {
|
||||
return (int)$this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
/** Backwards-compat alias */
|
||||
public function findOrCreateKeyword($keyword): ?int {
|
||||
return $this->findOrCreateTag((string)$keyword);
|
||||
}
|
||||
|
||||
|
||||
// ========================================================================
|
||||
// TAG MANAGEMENT (admin)
|
||||
@@ -720,52 +672,7 @@ class Database {
|
||||
/**
|
||||
* Get orientation ID by name
|
||||
*/
|
||||
public function getOrientationId($name) {
|
||||
$stmt = $this->pdo->prepare("SELECT id FROM orientations WHERE name = ?");
|
||||
$stmt->execute([$name]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ? $result['id'] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get AP program ID by name
|
||||
*/
|
||||
public function getAPProgramId($name) {
|
||||
$stmt = $this->pdo->prepare("SELECT id FROM ap_programs WHERE name = ?");
|
||||
$stmt->execute([$name]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ? $result['id'] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get finality type ID by name
|
||||
*/
|
||||
public function getFinalityId($name) {
|
||||
$stmt = $this->pdo->prepare("SELECT id FROM finality_types WHERE name = ?");
|
||||
$stmt->execute([$name]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ? $result['id'] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get language ID by name
|
||||
*/
|
||||
public function getLanguageId($name) {
|
||||
$stmt = $this->pdo->prepare("SELECT id FROM languages WHERE name = ?");
|
||||
$stmt->execute([$name]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ? $result['id'] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get format type ID by name
|
||||
*/
|
||||
public function getFormatId($name) {
|
||||
$stmt = $this->pdo->prepare("SELECT id FROM format_types WHERE name = ?");
|
||||
$stmt->execute([$name]);
|
||||
$result = $stmt->fetch();
|
||||
return $result ? $result['id'] : null;
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// STATIC PAGES METHODS
|
||||
@@ -814,18 +721,11 @@ class Database {
|
||||
/**
|
||||
* Get all license types ordered by name
|
||||
*/
|
||||
public function getLicenseTypes(): array {
|
||||
public function getAllLicenseTypes(): array {
|
||||
$stmt = $this->pdo->query("SELECT * FROM license_types ORDER BY name");
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for form-loading consistency
|
||||
*/
|
||||
public function getAllLicenseTypes(): array {
|
||||
return $this->getLicenseTypes();
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// VISIBILITY METHODS
|
||||
// ========================================================================
|
||||
|
||||
@@ -71,17 +71,6 @@ try {
|
||||
throw new Exception("getUsedTags did not return expected structure: " . json_encode($tags[0] ?? []));
|
||||
}
|
||||
|
||||
// Test 7: backwards-compat alias findOrCreateKeyword
|
||||
echo "Test 7: findOrCreateKeyword alias\n";
|
||||
$testTag2 = '__kw_alias_' . bin2hex(random_bytes(4));
|
||||
$kwId = $db->findOrCreateKeyword($testTag2);
|
||||
if ($kwId > 0) {
|
||||
echo "✓ PASS: findOrCreateKeyword alias works, id=$kwId\n\n";
|
||||
$db->deleteTag($kwId);
|
||||
} else {
|
||||
throw new Exception("findOrCreateKeyword alias failed");
|
||||
}
|
||||
|
||||
echo "✅ All database tests passed!\n";
|
||||
return true;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user