Skip to content

Commit c69099c

Browse files
authored
Merge pull request #40 from FriendsOfREDAXO/snapshot_left
Keep at least 2 Snapshots closes #39
2 parents f376b5b + f30dd0a commit c69099c

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

lib/Index.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,31 @@ public static function cleanUpSnapshots(): void
153153

154154
$cleanup_interval = (int) $cleanup_interval;
155155

156-
$sql = rex_sql::factory();
157-
$sql->setTable(rex::getTable('diff_detect_index'));
158-
$sql->setWhere('createdate < DATE_SUB(:datetime, INTERVAL :interval SECOND)', [
159-
'datetime' => date(rex_sql::FORMAT_DATETIME),
160-
'interval' => $cleanup_interval,
161-
]);
162-
$sql->delete();
163-
if (null !== $sql->getError()) {
164-
throw new rex_exception($sql->getError());
156+
foreach (Url::getAll() as $URL) {
157+
$indeces = rex_sql::factory()->getArray('
158+
SELECT *
159+
FROM ' . rex::getTable('diff_detect_index') . ' as t1
160+
JOIN (
161+
SELECT id
162+
FROM ' . rex::getTable('diff_detect_index') . '
163+
WHERE url_id = :url_id
164+
ORDER BY createdate DESC
165+
LIMIT 2,100000
166+
) as t2 ON t1.id = t2.id
167+
WHERE
168+
url_id = :url_id
169+
AND createdate < DATE_SUB(:datetime, INTERVAL :interval SECOND)
170+
', [
171+
'url_id' => $URL->getId(),
172+
'datetime' => date(rex_sql::FORMAT_DATETIME),
173+
'interval' => $cleanup_interval,
174+
]);
175+
176+
foreach ($indeces as $Index) {
177+
$Index = self::fromSqlData($Index);
178+
$Index->delete();
179+
}
165180
}
166-
167181
}
168182

169183
public function setUrl(Url $url): self
@@ -189,4 +203,21 @@ public function getContent(): string
189203
$content = (new Html2Text($content))->getText();
190204
return $content;
191205
}
206+
207+
public function delete(): void
208+
{
209+
$sql = rex_sql::factory()->setQuery(
210+
'
211+
DELETE from ' . rex::getTable('diff_detect_index') . '
212+
WHERE id = :id
213+
',
214+
[
215+
'id' => $this->getId(),
216+
],
217+
);
218+
219+
if (null !== $sql->getError()) {
220+
throw new rex_exception($sql->getError());
221+
}
222+
}
192223
}

lib/Url.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ public static function get(int $id): ?self
5151
return self::fromSqlData($data);
5252
}
5353

54+
/**
55+
* @throws rex_sql_exception
56+
* @return array<Url>
57+
*/
58+
public static function getAll(): array
59+
{
60+
$sql = rex_sql::factory();
61+
$sql->setTable(rex::getTable('diff_detect_url'));
62+
$sql->select();
63+
64+
$urls = [];
65+
foreach ($sql->getArray() as $data) {
66+
$urls[] = self::fromSqlData($data);
67+
}
68+
69+
return $urls;
70+
}
71+
5472
public function getId(): ?int
5573
{
5674
return $this->id;

pages/url.list.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
$urlParams = [
135135
'func' => 'status',
136136
'id' => $list->getValue('id'),
137-
'status' => ('0' == $list->getValue('status')) ? '1' : '0',
137+
'status' => ('0' === $list->getValue('status')) ? '1' : '0',
138138
];
139139

140140
$start = rex_request($startKey = $list->getName() . '_start', 'string', '');

0 commit comments

Comments
 (0)