mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-06-25 16:19:19 +02:00
Fix: CSV importer and imported data
- pad rows, distinguish empty year, better error diagnostics - derive year from identifier when year column is empty - fix remaining 18 theses: Installation/Performance (slash→dash) orientation alias - csv importer: use column-name-based header detection instead of hardcoded positions
This commit is contained in:
64
app/migrations/applied/013_fix_remarks_keywords.php
Normal file
64
app/migrations/applied/013_fix_remarks_keywords.php
Normal file
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
/**
|
||||
* Migrate keywords from theses.remarks → tags + thesis_tags.
|
||||
*
|
||||
* The CSV importer (before the column-shift fix) stored comma-separated
|
||||
* keywords in the `remarks` column. This script extracts them and creates
|
||||
* proper tag rows, then clears the remarks column.
|
||||
*
|
||||
* Run: php migrations/pending/013_fix_remarks_keywords.php
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../../src/Database.php';
|
||||
|
||||
$db = Database::getInstance();
|
||||
$pdo = $db->getPDO();
|
||||
|
||||
// Fetch theses with non-empty remarks
|
||||
$rows = $pdo->query(
|
||||
"SELECT id, remarks FROM theses WHERE remarks IS NOT NULL AND remarks != ''"
|
||||
)->fetchAll();
|
||||
|
||||
$insertTag = $pdo->prepare('INSERT OR IGNORE INTO tags (name) VALUES (?)');
|
||||
$getTagId = $pdo->prepare('SELECT id FROM tags WHERE name = ?');
|
||||
$insertLink = $pdo->prepare('INSERT OR IGNORE INTO thesis_tags (thesis_id, tag_id) VALUES (?, ?)');
|
||||
$clearRemarks = $pdo->prepare('UPDATE theses SET remarks = NULL WHERE id = ?');
|
||||
|
||||
$pdo->beginTransaction();
|
||||
|
||||
try {
|
||||
$migrated = 0;
|
||||
foreach ($rows as $row) {
|
||||
$thesisId = (int)$row['id'];
|
||||
$raw = trim($row['remarks']);
|
||||
if ($raw === '') {
|
||||
$clearRemarks->execute([$thesisId]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$keywords = array_map('trim', explode(',', $raw));
|
||||
foreach ($keywords as $kw) {
|
||||
$kw = trim($kw);
|
||||
if ($kw === '' || mb_strlen($kw) > 100) continue;
|
||||
|
||||
// Create tag if needed
|
||||
$insertTag->execute([$kw]);
|
||||
$getTagId->execute([$kw]);
|
||||
$tagId = $getTagId->fetchColumn();
|
||||
if ($tagId) {
|
||||
$insertLink->execute([$thesisId, (int)$tagId]);
|
||||
}
|
||||
}
|
||||
|
||||
$clearRemarks->execute([$thesisId]);
|
||||
$migrated++;
|
||||
}
|
||||
|
||||
$pdo->commit();
|
||||
echo "Done. Migrated keywords for $migrated theses.\n";
|
||||
} catch (Throwable $e) {
|
||||
$pdo->rollBack();
|
||||
echo "Error: " . $e->getMessage() . "\n";
|
||||
exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user