diff --git a/config-example.php b/config-example.php index b8a93cc..03bc0ac 100644 --- a/config-example.php +++ b/config-example.php @@ -1,12 +1,12 @@ . namespace itwikidelbot; +use cli\Log; 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 initialization $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(); } + Log::info( "work on $year/$month/$day" ); + // PDCs indexed by page ID $pdcs = []; // handle every PDC type foreach( CategoryYearMonthDayTypes::all() as $CategoryType ) { // fetch PDCs from this type $category_type = new $CategoryType( $year, $month, $day ); $category_type_pdcs = $category_type->fetchPDCs(); // save the specific daily category type only if it's not empty if( $category_type_pdcs ) { $category_type->saveIfNotExists(); } // merge the same PDCs into one foreach( $category_type_pdcs as $category_type_pdc ) { $id = $category_type_pdc->getID(); if( isset( $pdcs[ $id ] ) ) { $pdcs[ $id ]->merge( $category_type_pdc ); } else { $pdcs[ $id ] = $category_type_pdc; } } } // discard PDCs that do not belong to this date $pdcs = PDCs::filterNotInDate( $pdcs, $date ); + Log::info( sprintf( sprintf( + "found %d PDCs", + count( $pdcs ) + ) ) ); + // sort by creation date PDCs::sortByCreationDate( $pdcs ); // index then by their PDC_TYPE $pdcs_by_type = PDCs::indexByType( $pdcs ); // save the counting page PageYearMonthDayPDCsCount::createFromDateTimePDCs( $this->getDate(), $pdcs_by_type ) ->save(); // save the log page PageYearMonthDayPDCsLog::createFromDateTimePDCs( $this->getDate(), $pdcs_by_type ) ->save(); return $this; } } diff --git a/includes/class-Page.php b/includes/class-Page.php index ad7a9f1..f0caf48 100644 --- a/includes/class-Page.php +++ b/includes/class-Page.php @@ -1,160 +1,162 @@ . namespace itwikidelbot; -use \cli\Input; -use \wm\WikipediaIt; -use \mw\Tokens; +use cli\Input; +use cli\Log; +use wm\WikipediaIt; +use mw\Tokens; /** * Handle a page */ class Page { /** * Enable this flag to ask for every changes * * @var bool */ public static $ASK_BEFORE_SAVING = false; /** * @var string Page title with prefix */ private $title; /** * Construct a Page * * @param $title Page title with its prefix */ public function __construct( $title ) { $this->title = $title; } /** * Get the page title with its prefix * * @return string */ public function getTitle() { return $this->title; } /** * Save this page * * @param $content string Page content * @param $summary string Edit summary * @return bool|mixed False if not created */ public function saveByContentSummary( $content, $summary ) { return static::saveByTitleContentSummary( $this->getTitle(), $content, $summary ); } /** * Save this page if it does not exist * * @param $content string Page content * @param $summary string Edit summary * @return bool|mixed False if not created */ public function saveByContentSummaryIfNotExists( $content, $summary ) { return self::saveByTitleContentSummaryIfNotExists( $this->getTitle(), $content, $summary ); } /** * Check if this page exists * * @return bool */ public function exists() { return static::existsByTitle( $this->getTitle() ); } /** * Save a page * * @param $title string Page name with prefix * @param $content string Page content * @param $summary string Edit summary * @return mixed Response */ public static function saveByTitleContentSummary( $title, $content, $summary ) { $api = self::api(); $args = [ 'action' => 'edit', 'title' => $title, 'text' => $content, 'summary' => $summary, 'token' => $api->login()->getToken( Tokens::CSRF ), 'bot' => 1, ]; - $save = true; + if( self::$ASK_BEFORE_SAVING ) { print_r( $args ); if( 'y' !== Input::yesNoQuestion( "Save?" ) ) { $save = false; } } - if( $save ) { - return $api->post( $args ); - } + + Log::info( "writing [[$title]]" ); + + return $api->post( $args ); } /** * Save a page if it does not exist * * @param $title string Page name with prefix * @param $content string Page content * @param $summary string Edit summary * @return bool|mixed False if not created */ public static function saveByTitleContentSummaryIfNotExists( $title, $content, $summary ) { if( ! static::existsByTitle( $title ) ) { return static::saveByTitleContentSummary( $title, $content, $summary ); } return false; } /** * Check if a page title exists * * @param $title string Page name with prefix * @return bool */ public static function existsByTitle( $title ) { $result = self::api()->fetch( [ 'action' => 'query', 'prop' => 'info', 'titles' => $title, ] ); foreach( $result->query->pages as $pageid => $page ) { return ! isset( $page->missing ); } return false; } /** * Get the API related to this page * * @return mw\API */ protected static function api() { return WikipediaIt::getInstance(); } }