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:
Pontoporeia
2026-03-28 11:35:23 +01:00
parent b0632b4772
commit 2e277b104e
5 changed files with 42 additions and 143 deletions

16
TODO.md
View File

@@ -378,16 +378,14 @@ Goal: rename the tables and column to the canonical M2M pattern (`tags`, `thesis
### B — PHP / Database.php ### B — PHP / Database.php
- [ ] **Dead CRUD helpers**`getOrientationId()`, `getAPProgramId()`, `getFinalityId()`, - [x] **Dead CRUD helpers**`getOrientationId()`, `getAPProgramId()`, `getFinalityId()`,
`getLanguageId()`, `getFormatId()` are defined in `Database.php` but **never called** anywhere `getLanguageId()`, `getFormatId()` removed from `Database.php`; `import.php` updated to
(forms now pass IDs directly from selects). Remove them to reduce surface area. use inline PDO queries in their place.
- [ ] **Alias proliferation**`getOrientations()`/`getAllOrientations()`, `getApPrograms()`/`getAllAPPrograms()`, - [x] **Alias proliferation**collapsed 13 alias methods: canonical names are the `getAllXxx`
`getFinalityTypes()`/`getAllFinalityTypes()`, `getLanguages()`/`getAllLanguages()`, variants (`getAllOrientations`, `getAllAPPrograms`, `getAllFinalityTypes`, `getAllFormatTypes`,
`getFormatTypes()`/`getAllFormatTypes()`, `getLicenseTypes()`/`getAllLicenseTypes()`, `getAllLanguages`, `getAllLicenseTypes`) plus `getUsedTags` and `findOrCreateTag`; all
`findOrCreateKeyword()`, `getUsedKeywords()`**13 alias methods** pointing at 6 real ones. call-sites updated (`search.php`, `import.php`); Database.php reduced from 948 → 848 lines.
Pick the canonical name for each pair, update all call-sites (there are few), and delete aliases.
Reduces Database.php from ~945 lines significantly.
- [ ] **`getPDO()` / `getConnection()` leaking to callers** — `edit.php`, `formulaire.php`, - [ ] **`getPDO()` / `getConnection()` leaking to callers** — `edit.php`, `formulaire.php`,
`thanks.php`, `import.php`, `tfe.php`, `index.php`, `media.php`, `system.php` all call `thanks.php`, `import.php`, `tfe.php`, `index.php`, `media.php`, `system.php` all call

View File

@@ -142,7 +142,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
$orientationName = isset($orientationMap[$orientationCode]) ? $orientationMap[$orientationCode] : null; $orientationName = isset($orientationMap[$orientationCode]) ? $orientationMap[$orientationCode] : null;
$orientationId = null; $orientationId = null;
if ($orientationName) { 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 // Map AP program
@@ -159,7 +162,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
// Map finality // Map finality
$finalityId = null; $finalityId = null;
if (!empty($finalityName)) { 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 // Insert thesis
@@ -221,10 +227,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
$keywords = array_slice($keywords, 0, 10); // Max 10 $keywords = array_slice($keywords, 0, 10); // Max 10
foreach ($keywords as $keyword) { foreach ($keywords as $keyword) {
if (!empty($keyword)) { if (!empty($keyword)) {
$keywordId = $db->findOrCreateKeyword($keyword); $tagId = $db->findOrCreateTag($keyword);
if ($keywordId) { if ($tagId) {
$stmt = $pdo->prepare("INSERT INTO thesis_keywords (thesis_id, keyword_id) VALUES (?, ?)"); $stmtTag = $pdo->prepare("INSERT INTO thesis_tags (thesis_id, tag_id) VALUES (?, ?)");
$stmt->execute([$thesisId, $keywordId]); $stmtTag->execute([$thesisId, $tagId]);
} }
} }
} }
@@ -232,7 +238,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
// Add language // Add language
if (!empty($languageRaw)) { 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) { if ($languageId) {
$stmt = $pdo->prepare("INSERT INTO thesis_languages (thesis_id, language_id) VALUES (?, ?)"); $stmt = $pdo->prepare("INSERT INTO thesis_languages (thesis_id, language_id) VALUES (?, ?)");
$stmt->execute([$thesisId, $languageId]); $stmt->execute([$thesisId, $languageId]);
@@ -244,7 +253,10 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
$formats = array_map('trim', explode(',', $formatsRaw)); $formats = array_map('trim', explode(',', $formatsRaw));
foreach ($formats as $formatName) { foreach ($formats as $formatName) {
if (!empty($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) { if ($formatId) {
$stmt = $pdo->prepare("INSERT INTO thesis_formats (thesis_id, format_id) VALUES (?, ?)"); $stmt = $pdo->prepare("INSERT INTO thesis_formats (thesis_id, format_id) VALUES (?, ?)");
$stmt->execute([$thesisId, $formatId]); $stmt->execute([$thesisId, $formatId]);

View File

@@ -43,9 +43,9 @@ try {
} }
$years = $db->getAvailableYears(); $years = $db->getAvailableYears();
$orientations = $db->getOrientations(); $orientations = $db->getAllOrientations();
$apPrograms = $db->getApPrograms(); $apPrograms = $db->getAllAPPrograms();
$keywords = $db->getUsedKeywords(); $keywords = $db->getUsedTags();
// Fetch all published theses for the student index (no artificial cap) // Fetch all published theses for the student index (no artificial cap)
$students = $db->getAllPublishedTheses(); $students = $db->getAllPublishedTheses();
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {

View File

@@ -425,51 +425,27 @@ class Database {
/** /**
* Get all orientations * Get all orientations
*/ */
public function getOrientations() { public function getAllOrientations(): array {
$sql = "SELECT * FROM orientations ORDER BY name"; $stmt = $this->pdo->query("SELECT * FROM orientations ORDER BY name");
$stmt = $this->pdo->query($sql);
return $stmt->fetchAll(); return $stmt->fetchAll();
} }
/**
* Alias for formulaire compatibility
*/
public function getAllOrientations() {
return $this->getOrientations();
}
/** /**
* Get all AP programs * Get all AP programs
*/ */
public function getApPrograms() { public function getAllAPPrograms(): array {
$sql = "SELECT * FROM ap_programs ORDER BY name"; $stmt = $this->pdo->query("SELECT * FROM ap_programs ORDER BY name");
$stmt = $this->pdo->query($sql);
return $stmt->fetchAll(); return $stmt->fetchAll();
} }
/**
* Alias for formulaire compatibility
*/
public function getAllAPPrograms() {
return $this->getApPrograms();
}
/** /**
* Get all finality types * Get all finality types
*/ */
public function getFinalityTypes() { public function getAllFinalityTypes(): array {
$sql = "SELECT * FROM finality_types ORDER BY name"; $stmt = $this->pdo->query("SELECT * FROM finality_types ORDER BY name");
$stmt = $this->pdo->query($sql);
return $stmt->fetchAll(); return $stmt->fetchAll();
} }
/**
* Alias for formulaire compatibility
*/
public function getAllFinalityTypes() {
return $this->getFinalityTypes();
}
/** /**
* Get all keywords used in published theses * Get all keywords used in published theses
*/ */
@@ -483,43 +459,22 @@ class Database {
return $stmt->fetchAll(); return $stmt->fetchAll();
} }
/** Backwards-compat alias */
public function getUsedKeywords(): array {
return $this->getUsedTags();
}
/** /**
* Get all format types * Get all format types
*/ */
public function getFormatTypes() { public function getAllFormatTypes(): array {
$sql = "SELECT * FROM format_types ORDER BY name"; $stmt = $this->pdo->query("SELECT * FROM format_types ORDER BY name");
$stmt = $this->pdo->query($sql);
return $stmt->fetchAll(); return $stmt->fetchAll();
} }
/**
* Alias for formulaire compatibility
*/
public function getAllFormatTypes() {
return $this->getFormatTypes();
}
/** /**
* Get all languages * Get all languages
*/ */
public function getLanguages() { public function getAllLanguages(): array {
$sql = "SELECT * FROM languages ORDER BY name"; $stmt = $this->pdo->query("SELECT * FROM languages ORDER BY name");
$stmt = $this->pdo->query($sql);
return $stmt->fetchAll(); return $stmt->fetchAll();
} }
/**
* Alias for formulaire compatibility
*/
public function getAllLanguages() {
return $this->getLanguages();
}
// ======================================================================== // ========================================================================
// ADMIN LIST METHOD // ADMIN LIST METHOD
// ======================================================================== // ========================================================================
@@ -649,10 +604,7 @@ class Database {
return (int)$this->pdo->lastInsertId(); return (int)$this->pdo->lastInsertId();
} }
/** Backwards-compat alias */
public function findOrCreateKeyword($keyword): ?int {
return $this->findOrCreateTag((string)$keyword);
}
// ======================================================================== // ========================================================================
// TAG MANAGEMENT (admin) // TAG MANAGEMENT (admin)
@@ -720,52 +672,7 @@ class Database {
/** /**
* Get orientation ID by name * 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 // STATIC PAGES METHODS
@@ -814,18 +721,11 @@ class Database {
/** /**
* Get all license types ordered by name * 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"); $stmt = $this->pdo->query("SELECT * FROM license_types ORDER BY name");
return $stmt->fetchAll(); return $stmt->fetchAll();
} }
/**
* Alias for form-loading consistency
*/
public function getAllLicenseTypes(): array {
return $this->getLicenseTypes();
}
// ======================================================================== // ========================================================================
// VISIBILITY METHODS // VISIBILITY METHODS
// ======================================================================== // ========================================================================

View File

@@ -71,17 +71,6 @@ try {
throw new Exception("getUsedTags did not return expected structure: " . json_encode($tags[0] ?? [])); 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"; echo "✅ All database tests passed!\n";
return true; return true;