diff --git a/bot.php b/bot.php index 1fac5f2..2070c28 100755 --- a/bot.php +++ b/bot.php @@ -1,39 +1,34 @@ #!/usr/bin/php . namespace itwikidelbot; // autoload classes require __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'autoload.php'; // load credentials require __DIR__ . DIRECTORY_SEPARATOR . 'config.php'; -// create the yearly category -$year = date( 'Y' ); -( new CategoryYear( $year ) ) - ->saveIfNotExists(); +// the first argument is the days to go backward +$DAYS = isset( $argv[ 1 ] ) ? (int) $argv[ 1 ] : 1; -// create the monthly category -$month = date( 'n' ); // 1-12 -( new CategoryYearMonth( $year, $month ) ) - ->saveIfNotExists(); +// the second argument is a 'YYYY-MM-DD' date +$DATE = isset( $argv[ 2 ] ) ? $argv[ 2 ] : 'now'; -// create the daily category -$day = date( 'j' ); -( new CategoryYearMonthDayTypes( $year, $month, $day ) ) - ->saveIfNotExists() - ->run(); +$bot = Bot::createFromString( $DATE ); +for( $i = 0; $i < $DAYS; $i++ ) { + $bot->run()->previousDay(); +} diff --git a/includes/class-Bot.php b/includes/class-Bot.php new file mode 100644 index 0000000..1f21529 --- /dev/null +++ b/includes/class-Bot.php @@ -0,0 +1,173 @@ +. + +namespace itwikidelbot; + +use DateTime; +use DateInterval; + +/** + * The Bot class helps in running the bot + */ +class Bot { + + /** + * The date + * + * @var DateTime + */ + private $dateTime; + + /** + * A cache of already done dates. + * It's an array of booleans like [year][month] = true. + * + * @var array + */ + private $cache = []; + + /** + * Construct + * + * @param $date DateTime + */ + public function __construct( DateTime $date = null ) { + if( ! $date ) { + $date = new DateTime(); + } + $this->setDate( $date ); + } + + /** + * Static construct + * + * @param $date string + */ + public static function createFromString( $date = 'now' ) { + return new self( new DateTime( $date ) ); + } + + /** + * Static construct + * + * @param $y int Year + * @param $m int Month 1-12 + * @param $d int Day 1-31 + */ + public static function createFromYearMonthDay( $y, $m, $d ) { + return new self( DateTime::createFromFormat( "Y m d", "$y $m $d") ); + } + + /** + * Get the date + * + * @return DateTime + */ + public function getDate() { + return $this->dateTime; + } + + /** + * Set the date + * + * @param $date DateTime + * @return self + */ + public function setDate( DateTime $date ) { + $this->dateTime = $date; + } + + /** + * Add a day + * + * @return self + */ + public function nextDay() { + return $this->addDays( 1 ); + } + + /** + * Add a day + * + * @return self + */ + public function previousDay() { + return $this->subDays( 1 ); + } + + /** + * Add a certain number of days + * + * @param $days int + * @return self + */ + public function addDays( $days ) { + $this->getDate()->add( new DateInterval( sprintf( + 'P%dD', + $days + ) ) ); + return $this; + } + + /** + * Subtract a certain number of days + * + * @param $days int + * @return self + */ + public function subDays( $days ) { + $this->getDate()->sub( new DateInterval( sprintf( + 'P%dD', + $days + ) ) ); + return $this; + } + + /** + * Run the bot at the internal date + * + * @TODO: do not repeat twice the same yearly and montly categories + * @return self + */ + public function run() { + $date = $this->getDate(); + $year = $date->format( 'Y' ); + $month = $date->format( 'n' ); // 1-12 + $day = $date->format( 'j' ); + + // create the yearly category (once) + $cache = & $this->cache; + if( ! isset( $cache[ $year ] ) ) { + $cache[ $year ] = []; + ( new CategoryYear( $year ) ) + ->saveIfNotExists(); + } + + // create the monthly category (once) + $cache = & $cache[ $year ]; + if( ! isset( $cache[ $month ] ) ) { + $cache[ $month ] = true; + ( new CategoryYearMonth( $year, $month ) ) + ->saveIfNotExists(); + } + + // create the daily category + ( new CategoryYearMonthDayTypes( $year, $month, $day ) ) + ->saveIfNotExists() + ->run(); + + return $this; + } +}