-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.php
executable file
·97 lines (81 loc) · 2.55 KB
/
bot.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
#!/usr/bin/php
<?php
# Copyright (C) 2018 Valerio Bozzolan
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
namespace itwikidelbot;
// autoload classes
require __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'autoload.php';
use cli\Log;
// load credentials
require __DIR__ . DIRECTORY_SEPARATOR . 'config.php';
// the bot must be in sync with the italian community
date_default_timezone_set( 'Europe/Rome' );
// allowed only from command line interface
if( ! isset( $argv[ 0 ] ) ) {
exit( 1 );
}
// command line arguments
$opts = getopt( 'h', [
'ask',
'days:',
'from:',
'help',
'minutes-ago:',
'verbose',
] );
// help message
if( isset( $opts[ 'help' ] ) || isset( $opts[ 'h' ] ) ) {
printf( "usage: %s [OPTIONS]\n", $argv[ 0 ] );
echo " --days=DAYS how many days to be processed (default: 1)\n";
echo " --from=YYYY-MM-DD starting date (default: today)\n";
echo " --minutes-ago=N quits if the last edit was below N minutes ago (default: 5)\n";
echo " --ask ask before saving\n";
echo " --verbose verbose mode\n";
echo " -h --help show this help and exit\n";
exit( 0 );
}
// days to be processed from now to the past
$DAYS = isset( $opts[ 'days' ] )
? (int) $opts[ 'days' ]
: 1;
// starting date formatted as 'YYYY-MM-DD'
$DATE = isset( $opts[ 'from' ] )
? $opts[ 'from' ]
: 'now';
// minimum last edit age
$MINUTES = isset( $opts[ 'minutes-ago' ] )
? (int) $opts[ 'minutes-ago' ]
: 5;
// ask for every edit
if( isset( $opts[ 'ask' ] ) ) {
Page::$ASK_BEFORE_SAVING = true;
}
// verbose mode
if( isset( $opts[ 'verbose' ] ) ) {
Log::$DEBUG = true;
}
Log::info( 'start' );
$bot = Bot::createFromString( $DATE );
if( $MINUTES < 1 || $bot->isLasteditOlderThanMinutes( $MINUTES ) ) {
for( $i = 0; $i < $DAYS; $i++ ) {
$bot->run()->previousDay();
}
} else {
// do not insist
Log::info( sprintf(
'skip: someone running less than %d minutes ago',
$MINUTES
) );
}
Log::info( 'end' );