-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackfill.php
250 lines (208 loc) · 9.43 KB
/
backfill.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
require_once(WWW_DIR."/lib/framework/db.php");
require_once(WWW_DIR."/lib/groups.php");
require_once(WWW_DIR."/lib/nntp.php");
require_once(WWW_DIR."/lib/binaries.php");
class Backfill
{
function backfillAllGroups($groupName='', $backfillDate=null, $regexOnly=false)
{
$groups = new Groups;
$res = false;
if ($groupName != '') {
$grp = $groups->getByName($groupName);
if ($grp)
$res = array($grp);
} else {
$res = $groups->getActive();
}
if ($res) {
foreach($res as $groupArr) {
$this->backfillGroup($groupArr, $backfillDate, $regexOnly);
}
} else {
echo "No groups specified. Ensure groups are added to newznab's database for updating.\n";
}
}
function backfillGroup($groupArr, $backfillDate=null, $regexOnly=false)
{
$db = new DB();
$binaries = new Binaries();
if ($regexOnly === true) {
echo "Only inserting binaries which match regex\n";
$binaries->onlyProcessRegexBinaries = true;
}
$this->startGroup = microtime(true);
$nntp = new Nntp();
$nntpc = new Nntp();
if ($nntp->doConnect(5, false, true)) {
echo sprintf("Processing %s\n", $groupArr['name']);
$data = $nntp->selectGroup($groupArr['name']);
if ($nntp->isError($data)) {
echo "Could not select group (bad name?): {$groupArr['name']}\n";
return;
}
if ($backfillDate) {
$targetpost = $this->daytopost($nntp, $groupArr['name'], $this->dateToDays($backfillDate), true);
} else {
$targetpost = $this->daytopost($nntp, $groupArr['name'], $groupArr['backfill_target'], true);
}
if ($groupArr['first_record'] == 0 || $groupArr['backfill_target'] == 0 && !$backfillDate) {
echo "Group {$groupArr['name']} has invalid numbers. Have you run update on it? Have you set the backfill days amount?\n";
return;
}
echo "Group {$data['group']}: server has {$data['first']} - {$data['last']}, or ~";
echo ((int)((($this->postdate($nntp, $data['last'], false) - (int)$this->postdate($nntp, $data['first'], false)) / 86400)));
echo " days.\nLocal first = {$groupArr['first_record']} (";
echo ((int)((date('U') - $this->postdate($nntp, $groupArr['first_record'], false)) / 86400));
echo " days). Backfill target of " . ($backfillDate ? date('Y-m-d', $backfillDate) : $groupArr['backfill_target'] . " days") . " is post $targetpost.\n";
if ($targetpost >= $groupArr['first_record']) {
echo "Nothing to do, we already have the target post.\n \n";
return "";
}
if ($targetpost < $data['first']) {
echo "WARNING: Backfill came back as before server's first. Setting targetpost to server first.\n";
echo "Skipping Group \n";
return "";
}
echo ($binaries->onlyProcessRegexBinaries === true) ? "Note: Discarding parts that do not match a regex\n" : "";
$nntp->doQuit();
$nntpc->doConnect();
$datac = $nntpc->selectGroup($groupArr['name']);
if ($nntpc->isError($datac)) {
echo "Could not select group (bad name?): {$groupArr['name']}\n";
return;
}
$total = $groupArr['first_record'] - $targetpost;
$done = false;
$last = $groupArr['first_record'] - 1;
$first = $last - $binaries->messagebuffer + 1;
if ($targetpost > $first) {
$first = $targetpost;
}
while ($done === false) {
$binaries->startLoop = microtime(true);
echo "Getting " . ($last - $first + 1) . " parts (" . number_format($first - $targetpost) . " in queue)\n";
flush();
$success = $binaries->scan($nntpc, $groupArr, $first, $last, 'backfill');
if (!$success) {
return "";
}
$db->exec(sprintf("update `groups` SET first_record = %s, last_updated = now() WHERE ID = %d", $db->escapeString($first), $groupArr['ID']));
if ($first == $targetpost) {
$done = true;
} else {
$last = $first - 1;
$first = $last - $binaries->messagebuffer + 1;
if ($targetpost > $first) {
$first = $targetpost;
}
}
}
$nntpc->doQuit();
$nntp->doConnect();
$first_record_postdate = $this->postdate($nntp, $first, false);
$nntp->doQuit();
if ($first_record_postdate != "") {
$db->exec(sprintf("update `groups` SET first_record_postdate = FROM_UNIXTIME(%s), last_updated = now() WHERE ID = %d", $first_record_postdate, $groupArr['ID']));
}
$timeGroup = number_format(microtime(true) - $this->startGroup, 2);
echo "Group processed in $timeGroup seconds \n";
} else {
echo "Failed to get NNTP connection.\n";
}
}
function postdate($nntp, $post, $debug=true)
{
$attempts=0;
$date="";
do {
$msgs = $nntp->getOverview($post."-".$post, true, false);
if ($nntp->isError($msgs)) {
echo "Error {$msgs->code}: {$msgs->message}\n";
echo "Returning from postdate\n";
return "";
}
if (!isset($msgs[0]['Date']) || $msgs[0]['Date'] == "" || is_null($msgs[0]['Date'])) {
$success = false;
} else {
$date = $msgs[0]['Date'];
$success = true;
}
if ($debug && $attempts > 0) echo "retried $attempts time(s)\n";
$attempts++;
} while ($attempts <= 3 && $success == false);
if (!$success) {
return "";
}
$date = strtotime($date);
return $date;
}
function daytopost($nntp, $group, $days, $debug=true)
{
$pddebug = false; // DEBUG every postdate call?!?!
if ($debug) {
echo "INFO: daytopost finding post for $group $days days back.\n";
}
$data = $nntp->selectGroup($group);
if ($nntp->isError($data)) {
echo "Error {$data->code}: {$data->message}\n";
echo "Returning from daytopost\n";
return "";
}
$goaldate = date('U') - (86400 * $days); // goal timestamp
$firstDate = $this->postdate($nntp, $data['first'], $pddebug);
$lastDate = $this->postdate($nntp, $data['last'], $pddebug);
// Check if firstDate or lastDate is numeric
if (!is_numeric($firstDate) || !is_numeric($lastDate)) {
echo "Error: Invalid first or last date.\n";
return "";
}
if ($goaldate < $firstDate) {
echo "WARNING: Backfill target of $days day(s) is older than the first article stored on your news server.\n";
echo "Starting from the first available article (" . date("r", $firstDate) . " or " . $this->daysOld($firstDate) . " days).\n";
return $data['first'];
} elseif ($goaldate > $lastDate) {
echo "ERROR: Backfill target of $days day(s) is newer than the last article stored on your news server.\n";
echo "To backfill this group you need to set Backfill Days to at least " . ceil($this->daysOld($lastDate) + 1) . " days (" . date("r", $lastDate - 86400) . ").\n";
return "";
}
$interval = floor(($data['last'] - $data['first']) * 0.5);
$dateofnextone = $lastDate;
// Begin while loop
while ($this->daysOld($dateofnextone) < $days) {
$nskip = 1;
while (($tmpDate = $this->postdate($nntp, ($data['last'] - $interval), $pddebug)) > $goaldate) {
$data['last'] = $data['last'] - $interval - ($nskip - 1);
if ($debug) {
echo "New upperbound ({$data['last']}) is " . $this->daysOld($tmpDate) . " days old.\n";
}
$nskip = $nskip * 2;
}
$interval = ceil($interval / 2);
if ($interval <= 0) {
break;
}
$dateofnextone = $this->postdate($nntp, ($data['last'] - 1), $pddebug);
}
echo "Determined to be article {$data['last']} which is " . $this->daysOld($dateofnextone) . " days old (" . date("r", $dateofnextone) . ").\n";
return $data['last'];
}
private function daysOld($timestamp)
{
if (!is_numeric($timestamp))
{
echo "Error: Invalid timestamp. Using current time as fallback.";
$timestamp = time();
}
else
{
$timestamp = (int)$timestamp;
}
return round((time() - $timestamp) / 86400, 1);
}
private function dateToDays($backfillDate)
{
return floor(-($backfillDate - time()) / (60*60*24));
}
}