diff --git a/includes/class-CategoryYear.php b/includes/class-CategoryYear.php new file mode 100644 index 0000000..b1e46b9 --- /dev/null +++ b/includes/class-CategoryYear.php @@ -0,0 +1,108 @@ +. + +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 ) { + $summary = sprintf( + Template::get( 'CATEGORY_YEAR_SUMMARY' ), + self::MAIN_CATEGORY_TITLE, + $year + ); + $text = sprintf( + Template::get( 'CATEGORY_YEAR_CONTENT' ), + self::MAIN_CATEGORY_TITLE, + $year + ); + $wit = WikipediaIt::getInstance(); + $args = [ + 'action' => 'edit', + 'title' => self::title( $year ), + 'text' => $text, + 'summary' => $summary, + 'bot' => 1, + 'token' => $wit->login()->getToken( Tokens::CSRF ), + ]; + $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 ) { + return sprintf( self::GENERIC_TITLE, $year ); + } +}