diff --git a/bot.php b/bot.php index 2b546dc..dcb22c0 100755 --- a/bot.php +++ b/bot.php @@ -1,27 +1,31 @@ #!/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 category year $year = date( 'Y' ); CategoryYear::createIfNotExists( $year ); + +// create the month category +$month = date( 'n' ); // 1-12 +CategoryYearMonth::createIfNotExists( $year, $month ); diff --git a/includes/class-CategoryYear.php b/includes/class-CategoryYear.php index 316cdde..06e5356 100644 --- a/includes/class-CategoryYear.php +++ b/includes/class-CategoryYear.php @@ -1,102 +1,97 @@ . namespace itwikidelbot; use \wm\WikipediaIt; use \mw\Tokens; /** * Handle a yearly category * * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2017 */ class CategoryYear { /** * Format of the generic title of a yearly category * * e.g. 'Categoria:Cancellazioni - 2018' */ const GENERIC_TITLE = 'Categoria:Cancellazioni - %d'; /** * Parent category title */ const MAIN_CATEGORY_TITLE = 'Categoria:Pagine in cancellazione per anno'; /** * Create the yearly category if it does not exist * - * e.g. 'https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2018' - * * @param $year int e.g. 2018 */ public static function createIfNotExists( $year ) { $categoryinfo = WikipediaIt::getInstance()->fetch( [ 'action' => 'query', 'prop' => 'categoryinfo', 'titles' => self::title( $year ), ] ); foreach( $categoryinfo->query->pages as $pageid => $page ) { if( $pageid < 0 && isset( $page->missing ) ) { self::create( $year ); } } } /** * Create the yearly category * - * e.g. 'https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2018' - * * @param $year int e.g. 2018 - * @param $summary string */ private static function create( $year ) { $template_args = [ self::MAIN_CATEGORY_TITLE, $year ]; $wit = WikipediaIt::getInstance(); $args = [ 'action' => 'edit', 'title' => self::title( $year ), 'text' => Template::get( 'CATEGORY_YEAR_CONTENT', $template_args ), 'summary' => Template::get( 'CATEGORY_YEAR_SUMMARY', $template_args ), 'token' => $wit->login()->getToken( Tokens::CSRF ), 'bot' => 1, ]; $wit->post( $args ); } /** * Title of the category in the specified year * * @param $year int e.g. 2018 * @return string Category name e.g. 'Categoria:Cancellazioni - 2018' */ - private static function title( $year ) { + public static function title( $year ) { return sprintf( self::GENERIC_TITLE, $year ); } } diff --git a/includes/class-CategoryYear.php b/includes/class-CategoryYearMonth.php similarity index 65% copy from includes/class-CategoryYear.php copy to includes/class-CategoryYearMonth.php index 316cdde..2d1139d 100644 --- a/includes/class-CategoryYear.php +++ b/includes/class-CategoryYearMonth.php @@ -1,102 +1,98 @@ . namespace itwikidelbot; use \wm\WikipediaIt; use \mw\Tokens; /** - * Handle a yearly category + * Handle a monthly category * - * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2017 + * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_aprile_2018 */ -class CategoryYear { +class CategoryYearMonth { /** * Format of the generic title of a yearly category * - * e.g. 'Categoria:Cancellazioni - 2018' + * e.g. 'Categoria:Cancellazioni - aprile 2017' */ - const GENERIC_TITLE = 'Categoria:Cancellazioni - %d'; + const GENERIC_TITLE = 'Categoria:Cancellazioni - %2$s %1$d'; /** - * Parent category title - */ - const MAIN_CATEGORY_TITLE = 'Categoria:Pagine in cancellazione per anno'; - - /** - * Create the yearly category if it does not exist - * - * e.g. 'https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2018' + * Create the monthly category if it does not exist * * @param $year int e.g. 2018 + * @param $month int e.g. 4 */ - public static function createIfNotExists( $year ) { + public static function createIfNotExists( $year, $month ) { $categoryinfo = WikipediaIt::getInstance()->fetch( [ 'action' => 'query', 'prop' => 'categoryinfo', - 'titles' => self::title( $year ), + 'titles' => self::title( $year, $month ), ] ); foreach( $categoryinfo->query->pages as $pageid => $page ) { if( $pageid < 0 && isset( $page->missing ) ) { - self::create( $year ); + self::create( $year, $month ); } } } /** - * Create the yearly category - * - * e.g. 'https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2018' + * Create the monthly category * * @param $year int e.g. 2018 - * @param $summary string + * @param $month int e.g. 4 */ - private static function create( $year ) { + private static function create( $year, $month ) { $template_args = [ - self::MAIN_CATEGORY_TITLE, - $year + CategoryYear::title( $year, $month ), + $year, + $month, + Months::number2name( $month - 1 ), ]; $wit = WikipediaIt::getInstance(); $args = [ 'action' => 'edit', - 'title' => self::title( $year ), - 'text' => Template::get( 'CATEGORY_YEAR_CONTENT', $template_args ), - 'summary' => Template::get( 'CATEGORY_YEAR_SUMMARY', $template_args ), + 'title' => self::title( $year, $month ), + 'text' => Template::get( 'CATEGORY_MONTH_CONTENT', $template_args ), + 'summary' => Template::get( 'CATEGORY_MONTH_SUMMARY', $template_args ), 'token' => $wit->login()->getToken( Tokens::CSRF ), 'bot' => 1, ]; + var_dump( $args ); exit; $wit->post( $args ); } /** - * Title of the category in the specified year + * Title of the category in the specified month * * @param $year int e.g. 2018 * @return string Category name e.g. 'Categoria:Cancellazioni - 2018' */ - private static function title( $year ) { - return sprintf( self::GENERIC_TITLE, $year ); + private static function title( $year, $month ) { + $human_month = Months::number2name( $month - 1 ); + return sprintf( self::GENERIC_TITLE, $year, $human_month ); } } diff --git a/templates/CATEGORY_MONTH_CONTENT.template b/templates/CATEGORY_MONTH_CONTENT.template new file mode 100644 index 0000000..98ac872 --- /dev/null +++ b/templates/CATEGORY_MONTH_CONTENT.template @@ -0,0 +1,15 @@ + +This is the template for the content of a montly category. + +Placeholdes: + %1$s: category title + %2$d: year + %3$d: month + %4$s: human month + + +[[Categoria:Cancellazioni - 2018| 04]] + + +[[Categoria:Cancellazioni - %2$d| %3$d]] + diff --git a/templates/CATEGORY_MONTH_SUMMARY.template b/templates/CATEGORY_MONTH_SUMMARY.template new file mode 100644 index 0000000..37a55c4 --- /dev/null +++ b/templates/CATEGORY_MONTH_SUMMARY.template @@ -0,0 +1,14 @@ + +This is the template for the content of a montly category. + +Placeholdes: + %1$s: category title + %2$d: year + %3$d: month + %4$s: human month + + +Bot: creazione categoria annuale di servizio per aprile 2018 + + +Bot: creazione categoria mensile di servizio per %4$s %2$d diff --git a/templates/CATEGORY_YEAR_SUMMARY.template b/templates/CATEGORY_YEAR_SUMMARY.template index 72bfef5..db451b4 100644 --- a/templates/CATEGORY_YEAR_SUMMARY.template +++ b/templates/CATEGORY_YEAR_SUMMARY.template @@ -1,12 +1,12 @@ This is the template for the summary of a yearly category. Placeholdes: %1$s: category title %2$d: year -Bot: creazione categoria annuale di servizio +Bot: creazione categoria annuale di servizio per il 2018 -Bot: creazione categoria annuale di servizio +Bot: creazione categoria annuale di servizio per il %2$d