-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.php
130 lines (104 loc) · 3.57 KB
/
extension.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
<?php
require_once __DIR__.'/stats.php';
class AutoTTLExtension extends Minz_Extension
{
// Defaults
private const MAX_TTL = 24 * 60 * 60; // 1 day
private const STATS_COUNT = 100;
public int $defaultTTL;
public int $maxTTL;
public int $statsCount;
/**
* @var AutoTTLStats
*/
private $stats;
public function init()
{
parent::init();
$this->registerHook('feed_before_actualize', [
$this,
'feedBeforeActualizeHook',
]);
$this->registerTranslates();
$this->initConfig();
}
private function initConfig()
{
if (!FreshRSS_Context::userConf()->hasParam('auto_ttl_max_ttl')) {
FreshRSS_Context::userConf()->_attribute('auto_ttl_max_ttl', self::MAX_TTL);
}
if (!FreshRSS_Context::userConf()->hasParam('auto_ttl_stats_count')) {
FreshRSS_Context::userConf()->_attribute('auto_ttl_stats_count', self::STATS_COUNT);
}
FreshRSS_Context::userConf()->save();
$this->defaultTTL = FreshRSS_Context::userConf()->attributeInt('ttl_default');
$this->maxTTL = FreshRSS_Context::userConf()->attributeInt('auto_ttl_max_ttl');
$this->statsCount = FreshRSS_Context::userConf()->attributeInt('auto_ttl_stats_count');
}
/*
* Called by FreshRSS when the configuration page is loaded or saved.
*/
public function handleConfigureAction()
{
$this->registerTranslates();
if (Minz_Request::isPost()) {
FreshRSS_Context::userConf()->_attribute('auto_ttl_max_ttl', Minz_Request::paramInt('auto_ttl_max_ttl'));
FreshRSS_Context::userConf()->_attribute('auto_ttl_stats_count', Minz_Request::paramInt('auto_ttl_stats_count'));
FreshRSS_Context::userConf()->save();
}
}
public function getStats(): AutoTTLStats
{
if ($this->stats === null) {
$this->stats = new AutoTTLStats($this->defaultTTL, $this->maxTTL, $this->statsCount);
}
return $this->stats;
}
public function feedBeforeActualizeHook(FreshRSS_Feed $feed)
{
if ($feed->lastUpdate() === 0) {
Minz_Log::debug(
sprintf(
'AutoTTL: feed %d (%s) never updated, updating now',
$feed->id(),
$feed->name(),
)
);
return $feed;
}
if ($feed->ttl() !== FreshRSS_Feed::TTL_DEFAULT) {
Minz_Log::debug(
sprintf(
'AutoTTL: feed %d (%s) not using default TTL, updating now',
$feed->id(),
$feed->name(),
)
);
return $feed;
}
$timeSinceLastUpdate = time() - $feed->lastUpdate();
$ttl = $this->getStats()->getAdjustedTTL($feed->id());
if ($timeSinceLastUpdate < $ttl) {
Minz_Log::debug(
sprintf(
'AutoTTL: skip feed %d (%s, last update %s): adjusted TTL (%ds) not exceeded yet',
$feed->id(),
$feed->name(),
date('r', $feed->lastUpdate()),
$ttl,
)
);
return null;
}
Minz_Log::debug(
sprintf(
'AutoTTL: updating feed %d (%s, last update %s, adjusted TTL %ds)',
$feed->id(),
$feed->name(),
date('r', $feed->lastUpdate()),
$ttl,
)
);
return $feed;
}
}