mirror of
https://codeberg.org/PostERG/xamxam.git
synced 2026-09-25 09:53:08 +02:00
74 lines
2.3 KiB
PHP
Executable File
74 lines
2.3 KiB
PHP
Executable File
#!/usr/bin/env php
|
|
<?php
|
|
/**
|
|
* cleanup-drafts.php — Delete orphaned draft theses older than a threshold.
|
|
*
|
|
* Draft theses are created with status='draft' during the two-phase commit
|
|
* in ThesisCreateController. If the file phase throws after COMMIT, the
|
|
* draft remains orphaned — no files attached, but blocks the identifier.
|
|
*
|
|
* The eligibility age defaults to 7 days (168 hours) and can be overridden
|
|
* with the OLDER_THAN_HOURS environment variable:
|
|
* OLDER_THAN_HOURS=24 php scripts/cleanup-drafts.php
|
|
*
|
|
* Usage:
|
|
* php /tmp/cleanup-drafts.php # dry-run (list candidates)
|
|
* php /tmp/cleanup-drafts.php --no-dry-run # actually delete
|
|
*
|
|
* Exit codes: 0 on success, 1 on error.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
// Resolve APP_ROOT robustly. In production the app code is deployed flat under
|
|
// /var/www/xamxam/ (src/, storage/, templates/ at the root, no app/ subdir), and
|
|
// this script may itself live in /tmp. Always point at /var/www/xamxam in
|
|
// non-CLI-SAPI contexts; local dev (cli-server) keeps the app/ subdir layout.
|
|
$prodRoot = '/var/www/xamxam';
|
|
if (is_dir($prodRoot . '/src') && is_file($prodRoot . '/src/Database.php')) {
|
|
define('APP_ROOT', $prodRoot);
|
|
} else {
|
|
define('APP_ROOT', dirname(__DIR__) . '/app');
|
|
}
|
|
|
|
require_once APP_ROOT . '/src/Database.php';
|
|
|
|
$dryRun = !in_array('--no-dry-run', $argv, true);
|
|
|
|
// Eligibility threshold: default 7 days (168h); overridable via env.
|
|
$olderThanHours = (int) (getenv('OLDER_THAN_HOURS') ?: 168);
|
|
if ($olderThanHours < 1) {
|
|
$olderThanHours = 168;
|
|
}
|
|
|
|
try {
|
|
$db = new Database();
|
|
$result = $db->cleanupOrphanedDrafts($olderThanHours, $dryRun);
|
|
} catch (Exception $e) {
|
|
error_log('[cleanup-drafts] Error: ' . $e->getMessage());
|
|
exit(1);
|
|
}
|
|
|
|
$count = count($result['candidates']);
|
|
|
|
if ($count === 0) {
|
|
exit(0); // nothing to do — quiet exit
|
|
}
|
|
|
|
if ($dryRun) {
|
|
foreach ($result['candidates'] as $row) {
|
|
printf(
|
|
"DRY-RUN → #%d %s \"%s\" (submitted %s)\n",
|
|
$row['id'],
|
|
$row['identifier'],
|
|
$row['title'],
|
|
$row['submitted_at']
|
|
);
|
|
}
|
|
echo "Found {$count} orphaned draft(s). Re-run with --no-dry-run to delete.\n";
|
|
exit(0);
|
|
}
|
|
|
|
printf("Deleted %d orphaned draft(s).\n", $result['deleted']);
|
|
exit(0);
|