/
killinactivedoc.php
70 lines (63 loc) · 2.52 KB
/
killinactivedoc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
// killinactivedoc.php -- HotCRP paper export script
// Copyright (c) 2006-2022 Eddie Kohler; see LICENSE.
if (realpath($_SERVER["PHP_SELF"]) === __FILE__) {
require_once(dirname(__DIR__) . "/src/init.php");
exit(KillInactiveDoc_Batch::make_args($argv)->run());
}
class KillInactiveDoc_Batch {
/** @var Conf */
public $conf;
/** @var bool */
public $force;
function __construct(Conf $conf, $arg) {
$this->conf = $conf;
$this->force = isset($arg["force"]);
}
/** @return int */
function run() {
$didmap = DocumentInfo::active_document_map($this->conf);
$result = $this->conf->qe_raw("select paperStorageId, paperId, timestamp, mimetype,
compression, sha1, documentType, filename, infoJson
from PaperStorage where paperStorageId not in (" . join(",", array_keys($didmap)) . ")
and paper is not null and paperStorageId>1 order by timestamp");
$killable = [];
while (($doc = DocumentInfo::fetch($result, $this->conf))) {
$killable[$doc->paperStorageId] = "[" . $this->conf->unparse_time_log($doc->timestamp)
. "] " . $doc->export_filename() . " ({$doc->paperStorageId})";
}
if (count($killable)) {
fwrite(STDOUT, join("\n", $killable) . "\n");
if (!$this->force) {
fwrite(STDERR, "\nKill " . plural(count($killable), "document") . "? (y/n) ");
$x = fread(STDIN, 100);
if (!preg_match('/\A[yY]/', $x)) {
fwrite(STDERR, "Exiting\n");
return 1;
}
}
$this->conf->qe_raw("update PaperStorage set paper=NULL where paperStorageId in ("
. join(",", array_keys($killable)) . ")");
fwrite(STDOUT, plural(count($killable), "document") . " killed.\n");
} else {
fwrite(STDOUT, "Nothing to do\n");
}
return 0;
}
/** @param list<string> $argv
* @return KillInactiveDoc_Batch */
static function make_args($argv) {
$arg = (new Getopt)->long(
"name:,n: !",
"config: !",
"help,h !",
"force,f"
)->description("Remove inactive documents from HotCRP database.
Usage: php batch/killinactivedoc.php [-f]")
->helpopt("help")
->maxarg(0)
->parse($argv);
$conf = initialize_conf($arg["config"] ?? null, $arg["name"] ?? null);
return new KillInactiveDoc_Batch($conf, $arg);
}
}