-
Notifications
You must be signed in to change notification settings - Fork 3
/
purge.php
60 lines (46 loc) · 1.45 KB
/
purge.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
<?php
// CLI only
if (php_sapi_name() !== 'cli')
die('Access Denied');
define('DEFAULT_DAYS', 90);
// Class load
require __DIR__. '/src/GetOpt.php';
// Config file
$config = require __DIR__ . '/config.inc.php';
$dbConfig = & $config['database'];
$mtrConfig = & $config['mtr'];
$config = $config['general'];
// Timezone setting
date_default_timezone_set($config['timezone']);
/**
* Options definition
*/
$shortopts = "";
$shortopts .= "d::";
// Long options
$longopts = array(
"days::",
);
// GetOpt
$getOpt = new GetOpt($shortopts, $longopts);
// var_dump($getOpt->getOptions());exit;
// Days
$optTmp = $getOpt->get(['days', 'd']);
$days = ($optTmp) ? (integer) $optTmp : DEFAULT_DAYS;
try {
// Database connection
$conn = new PDO("mysql:host={$dbConfig['host']};dbname={$dbConfig['database']}", $dbConfig['username'], $dbConfig['password']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
// Purge before-date calculation
$beforeDatetime = date("Y-m-d H:i:s", time() - ($days * 24 * 3600));
echo $beforeDatetime;exit;
// Delete records
$result = $conn->exec("DELETE FROM {$dbConfig['table']} WHERE `start_datetime` < \"{$beforeDatetime}\";");
// var_dump($result);exit;
if ($result === false) {
die("Error!: " . $conn->errorInfo()[2] . "\n");
}
echo "Purge completed - Deleted rows: {$result}\n";
} catch (PDOException $e) {
die("Error!: " . $e->getMessage() . "\n");
}