diff --git a/bot.php b/bot.php index 8120195..02883c5 100755 --- a/bot.php +++ b/bot.php @@ -1,98 +1,105 @@ #!/usr/bin/php . namespace itwikidelbot; // autoload classes require __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'autoload.php'; use cli\Log; +use \Throwable; // Load credentials. // If the file does not exist, a wizard starts. config_wizard( __DIR__ . DIRECTORY_SEPARATOR . 'config.php' ); // the bot must be in sync with the italian community date_default_timezone_set( Page::COMMUNITY_TIMEZONE ); // 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(); +try { + $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 + ) ); } -} else { - // do not insist - Log::info( sprintf( - 'skip: someone running less than %d minutes ago', - $MINUTES - ) ); +} catch( Throwable $e ) { + // Log the exact time of this crash and throw it as-is + Log::error( 'unexpected crash:' ); + throw $e; } Log::info( 'end' );