diff --git a/bot.php b/bot.php index dcb22c0..e688924 100755 --- a/bot.php +++ b/bot.php @@ -1,31 +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 ); +( new CategoryYear( $year ) )->saveIfNotExists(); // create the month category $month = date( 'n' ); // 1-12 -CategoryYearMonth::createIfNotExists( $year, $month ); +( new CategoryYearMonth( $year, $month ) )->saveIfNotExists(); diff --git a/includes/class-Category.php b/includes/class-Category.php new file mode 100644 index 0000000..6cefc53 --- /dev/null +++ b/includes/class-Category.php @@ -0,0 +1,135 @@ +. + +namespace itwikidelbot; + +use \wm\WikipediaIt; +use \mw\Tokens; + +/** + * Handle a category + */ +class Category { + + /** + * @var string Category title with prefix + */ + private $title; + + /** + * Construct a Category + * + * @param $title Category title with its prefix + */ + public function __construct( $title ) { + $this->title = $title; + } + + /** + * Get the category title with its prefix + * + * @return string + */ + public function getTitle() { + return $this->title; + } + + /** + * Save this category + * + * @param $content string Category 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 category if it does not exist + * + * @param $content string Category 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 category exists + * + * @return bool + */ + public function exists() { + return static::existsByTitle( $this->getTitle() ); + } + + /** + * Save a category + * + * @param $title string Category name with prefix + * @param $content string Category content + * @param $summary string Edit summary + * @return mixed Response + */ + public static function saveByTitleContentSummary( $title, $content, $summary ) { + $wit = WikipediaIt::getInstance(); + $args = [ + 'action' => 'edit', + 'title' => $title, + 'text' => $content, + 'summary' => $summary, + 'token' => $wit->login()->getToken( Tokens::CSRF ), + 'bot' => 1, + ]; + var_dump( $args );exit; + return $wit->post( $args ); + } + + /** + * Save a category if it does not exist + * + * @param $title string Category name with prefix + * @param $content string Category 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 category title exists + * + * @param $title string Category name with prefix + * @return bool + */ + public static function existsByTitle( $title ) { + $categoryinfo = WikipediaIt::getInstance()->fetch( [ + 'action' => 'query', + 'prop' => 'categoryinfo', + 'titles' => $title, + ] ); + foreach( $categoryinfo->query->pages as $pageid => $page ) { + return $pageid < 0 && isset( $page->missing ); + } + return false; + } +} diff --git a/includes/class-CategoryTemplated.php b/includes/class-CategoryTemplated.php new file mode 100644 index 0000000..6476a1a --- /dev/null +++ b/includes/class-CategoryTemplated.php @@ -0,0 +1,85 @@ +. + +namespace itwikidelbot; + +/** + * Handle a generic category associated to a template + */ +abstract class CategoryTemplated extends Category { + + /** + * Template name of this category + */ + const TEMPLATE_NAME = 'TEMPLATE_NAME'; + + /** + * Get the template arguments + * + * To be overrided + * + * @return array + */ + abstract protected function getTemplateArguments(); + + /** + * Get the edit summary for this category from its template + * + * @return string + */ + public function getTemplateSummary() { + return Template::get( static::TEMPLATE_NAME . '_SUMMARY', $this->getTemplateArguments() ); + } + + /** + * Get the page content for this category from its template + * + * @return string + */ + public function getTemplateContent() { + return Template::get( static::TEMPLATE_NAME . '_CONTENT', $this->getTemplateArguments() ); + } + + /** + * Save this category from the content of its template + * + * @return mixed + */ + public function save() { + return $this->saveByTitleContentSummary( $this->getTitle(), $this->getTemplateContent(), $this->getTemplateSummary() ); + } + + /** + * Save this category from the content of its template if not exists + * + * @return bool|mixed + */ + public function saveIfNotExists() { + if( $this->exists() ) { + return $this->save(); + } + return false; + } + + /** + * Check if this category exists + * + * @return bool + */ + public function exists() { + return $this->existsByTitle( $this->getTitle() ); + } +} diff --git a/includes/class-CategoryYear.php b/includes/class-CategoryYear.php index 06e5356..69fe6f0 100644 --- a/includes/class-CategoryYear.php +++ b/includes/class-CategoryYear.php @@ -1,97 +1,91 @@ . namespace itwikidelbot; -use \wm\WikipediaIt; -use \mw\Tokens; - /** * Handle a yearly category * * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2017 */ -class CategoryYear { +class CategoryYear extends CategoryTemplated { + + /** + * Year + * + * @var int + */ + private $year; + + /** + * The template name for a yearly category + * + * @override CategoryTemplated::TEMPLATE_NAME + */ + const TEMPLATE_NAME = 'CATEGORY_YEAR'; /** * 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'; + const PARENT_CATEGORY_TITLE = 'Categoria:Pagine in cancellazione per anno'; /** - * Create the yearly category if it does not exist + * Constructor * - * @param $year int e.g. 2018 + * @param $year int */ - 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 ); - } - } + public function __construct( $year ) { + parent::__construct( self::title( $year ) ) ; + $this->year = $year; } /** - * Create the yearly category + * Get the template arguments * - * @param $year int e.g. 2018 + * @override CategoryTemplated::getTemplateArguments() + * @return array */ - 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, + protected function getTemplateArguments() { + return [ + self::PARENT_CATEGORY_TITLE, + $this->year, ]; - $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' */ public static function title( $year ) { return sprintf( self::GENERIC_TITLE, $year ); } } diff --git a/includes/class-CategoryYearMonth.php b/includes/class-CategoryYearMonth.php index 2d1139d..b5fd817 100644 --- a/includes/class-CategoryYearMonth.php +++ b/includes/class-CategoryYearMonth.php @@ -1,98 +1,98 @@ . namespace itwikidelbot; -use \wm\WikipediaIt; -use \mw\Tokens; - /** * Handle a monthly category * * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_aprile_2018 */ -class CategoryYearMonth { +class CategoryYearMonth extends CategoryTemplated { + + /** + * Year + * + * @var int + */ + private $year; + + /** + * Month + * + * @var int 1-12 + */ + private $month; + + /** + * The template name for a monthly category + * + * @override CategoryTemplated::TEMPLATE_NAME + */ + const TEMPLATE_NAME = 'CATEGORY_MONTH'; /** - * Format of the generic title of a yearly category + * Format of the generic title of a monthly category * - * e.g. 'Categoria:Cancellazioni - aprile 2017' + * e.g. 'Categoria:Cancellazioni - aprile 2018' */ const GENERIC_TITLE = 'Categoria:Cancellazioni - %2$s %1$d'; /** - * Create the monthly category if it does not exist + * Constructor * - * @param $year int e.g. 2018 - * @param $month int e.g. 4 + * @param $year int + * @param $month int 1-12 */ - public static function createIfNotExists( $year, $month ) { - $categoryinfo = WikipediaIt::getInstance()->fetch( [ - 'action' => 'query', - 'prop' => 'categoryinfo', - 'titles' => self::title( $year, $month ), - ] ); - foreach( $categoryinfo->query->pages as $pageid => $page ) { - if( $pageid < 0 && isset( $page->missing ) ) { - self::create( $year, $month ); - } - } + public function __construct( $year, $month ) { + parent::__construct( self::title( $year, $month ) ); + $this->year = $year; + $this->month = $month; } /** - * Create the monthly category + * Get the template arguments * - * @param $year int e.g. 2018 - * @param $month int e.g. 4 + * @override CategoryTemplated::getTemplateArguments() + * @return array */ - private static function create( $year, $month ) { - $template_args = [ - CategoryYear::title( $year, $month ), - $year, - $month, - Months::number2name( $month - 1 ), - ]; - $wit = WikipediaIt::getInstance(); - $args = [ - 'action' => 'edit', - '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, + protected function getTemplateArguments() { + return [ + CategoryYear::title( $this->year ), + $this->year, + $this->month, + Months::number2name( $this->month - 1 ), ]; - var_dump( $args ); exit; - $wit->post( $args ); } /** - * Title of the category in the specified month + * Title of a monthly category * * @param $year int e.g. 2018 * @return string Category name e.g. 'Categoria:Cancellazioni - 2018' */ - private static function title( $year, $month ) { + public static function title( $year, $month ) { $human_month = Months::number2name( $month - 1 ); return sprintf( self::GENERIC_TITLE, $year, $human_month ); } }