/
digest.php
82 lines (68 loc) · 2.36 KB
/
digest.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
<?php
/**
* Use this tool to massively send all the pending subscription digests without waiting
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Abilio Marques <https://github.com/abiliojr>
*/
if(!defined('DOKU_ROOT')) define('DOKU_ROOT', realpath(dirname(__FILE__).'/../../../').'/');
define('NOSESSION', 1);
require_once(DOKU_ROOT.'inc/init.php');
class DigestTriggerCLI extends DokuCLI {
/**
* Register options and arguments on the given $options object
*
* @param DokuCLI_Options $options
* @return void
*/
protected function setup(DokuCLI_Options $options) {
global $auth;
global $conf;
$auth = new auth_plugin_authplain();
$period = $conf['subscribe_time'];
$options->setHelp(
"Massively sends pending subscription digests.\n" .
"By default, sends every pending digest from the beginning of time. " .
"An event must be older than \$conf['subscribe_time'] = $period sec " .
"to be included in any digest."
);
$options->registerOption(
'cron',
"Send all the pending emails aged between $period and " .
2 * $period . " seconds. Useful to be run from a cron scheduler.",
'c'
);
}
protected function findFreshEdits($betweenPeriod) {
global $conf;
global $auth;
// use 0.95 and 1.1 to be sure it sends everything even with cron jitter
$timestampHigh = time() - 0.95 * $conf['subscribe_time'];
$timestampLow = $timestampHigh - 1.1 * $conf['subscribe_time'];
if (!$betweenPeriod) $timestampLow = 0;
$changesFile = $conf['changelog'];
return explode("\n", shell_exec("cat $changesFile | grep [[:blank:]][Ee][[:blank:]] | cut -f 1,4 | awk '\$1>=$timestampLow && \$1<=$timestampHigh' | cut -f 2 | sort | uniq"));
}
protected function sendAll($IDs) {
$sub = new Subscription();
foreach($IDs as $ID) {
if (!$ID) continue;
echo "$ID sent to " . $sub->send_bulk($ID) . " email(s)\n";
}
}
/**
* Your main program
*
* Arguments and options have been parsed when this is run
*
* @param DokuCLI_Options $options
* @return void
*/
protected function main(DokuCLI_Options $options) {
$cron = $options->getOpt('cron');
$this->sendAll($this->findFreshEdits($cron));
}
}
// Main
$cli = new DigestTriggerCLI();
$cli->run();