summaryrefslogtreecommitdiff
path: root/includes/refresh.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/refresh.php')
-rw-r--r--includes/refresh.php56
1 files changed, 53 insertions, 3 deletions
diff --git a/includes/refresh.php b/includes/refresh.php
index 320af54..8f9da79 100644
--- a/includes/refresh.php
+++ b/includes/refresh.php
@@ -3,12 +3,43 @@
$app = json_decode(file_get_contents("./app.json"), true);
$start = microtime(true);
@mkdir("./data");
+@mkdir("./data/backup");
$files = [];
$restored = [];
$times = [];
$currentOpStart = microtime(true);
+file_put_contents("./data/backup/actions_" . date('Y-m-d_H-i-s') . ".json", @file_get_contents("./data/actions.json"));
+file_put_contents("./data/backup/toys_" . date('Y-m-d_H-i-s') . ".json", @file_get_contents("./data/toys.json"));
+
+exec('bash -c "cd /nas/web/ponies.equestria.horse/includes/data/backup; ls -tp toys_* | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"');
+exec('bash -c "cd /nas/web/ponies.equestria.horse/includes/data/backup; ls -tp actions_* | grep -v \'/$\' | tail -n +20 | xargs -I {} rm -- {}"');
+
+$times["important_backup"] = microtime(true) - $currentOpStart;
+
+$currentOpStart = microtime(true);
+$documents = array_map(function ($i) {
+ return [
+ "id" => substr($i, 0, -5),
+ ...(json_decode(file_get_contents("./data/docs/" . $i), true) ?? [])
+ ];
+}, array_filter(scandir("./data/docs"), function ($i) {
+ return !str_starts_with($i, ".") && str_ends_with($i, ".json");
+}));
+
+$deletable = array_values(array_filter($documents, function ($i) {
+ return strip_tags($i["contents"]) === "/delete";
+}));
+
+foreach ($deletable as $item) {
+ if (time() - $item["last"]["date"] > 86400) {
+ unlink("./data/docs/" . $item["id"] . ".json");
+ }
+}
+$times["docs_cleanup"] = microtime(true) - $currentOpStart;
+
+$currentOpStart = microtime(true);
foreach (array_filter(scandir("./data"), function ($i) {
return !str_starts_with($i, ".") && $i !== "backup" && is_file("./data/" . $i);
}) as $file) {
@@ -41,7 +72,19 @@ function getSystem(string $id) {
echo(" Switches\n");
$currentOpStart = microtime(true);
- file_put_contents("./data/$id-switches.json", file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/switches"));
+
+ echo(" Part 1/3\n");
+ $switches1 = json_decode(file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/switches"), true);
+ $oldest = $switches1[count($switches1) - 1]["timestamp"];
+
+ echo(" Part 2/3\n");
+ $switches2 = json_decode(file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/switches?before=$oldest"), true);
+ $oldest = $switches2[count($switches2) - 1]["timestamp"];
+
+ echo(" Part 3/3\n");
+ $switches3 = json_decode(file_get_contents("https://pluralkit.equestria.dev/v2/systems/$id/switches?before=$oldest"), true);
+
+ file_put_contents("./data/$id-switches.json", json_encode([...$switches1, ...$switches2, ...$switches3], JSON_PRETTY_PRINT));
$times["system-switches-$id"] = microtime(true) - $currentOpStart;
}
@@ -99,14 +142,21 @@ function isJson($string): bool {
}
foreach ($files as $file) {
- if (trim(file_get_contents("./data/" . $file)) === "" || (isJson(trim(file_get_contents("./data/" . $file))) && isset(json_decode(trim(file_get_contents("./data/" . $file)), true)["message"]))) {
+ if (trim(file_get_contents("./data/" . $file)) === "" || (isJson(trim(file_get_contents("./data/" . $file))) && isset(json_decode(trim(file_get_contents("./data/" . $file)), true)["message"])) && !str_ends_with($file, ".html")) {
$restored[] = $file;
copy("./data/backup/" . $file, "./data/" . $file);
}
}
$times["restore"] = microtime(true) - $currentOpStart;
-require_once "./backup.php";
+echo("Backing up...\n");
+
+if (date('i') === "00") {
+ require_once "./backup.php";
+ echo("Backup completed\n");
+} else {
+ echo("Backup skipped\n");
+}
$time = array_sum($times);
echo("Completed in " . $time . " seconds.\n");