diff --git a/includes/class-Category.php b/includes/class-Category.php index 345bb18..1140f3f 100644 --- a/includes/class-Category.php +++ b/includes/class-Category.php @@ -1,162 +1,163 @@ . namespace itwikidelbot; use \wm\WikipediaIt; use \mw\Tokens; /** * Handle a category */ class Category { /** * Do nothing * * @var bool */ public static $PORCELAIN = false; /** * Debug mode * * @var bool */ public static $INSPECT = false; /** * @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 ) { $api = self::api(); $args = [ 'action' => 'edit', 'title' => $title, 'text' => $content, 'summary' => $summary, 'token' => $api->login()->getToken( Tokens::CSRF ), 'bot' => 1, ]; if( self::$INSPECT ) { print_r( $args ); + exit; } if( ! self::$PORCELAIN ) { 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 = self::api()->fetch( [ 'action' => 'query', 'prop' => 'categoryinfo', 'titles' => $title, ] ); foreach( $categoryinfo->query->pages as $pageid => $page ) { return ! isset( $page->missing ); } return false; } /** * Get the API related to this category * * @return mw\API */ protected static function api() { return WikipediaIt::getInstance(); } } diff --git a/includes/class-CategoryTemplated.php b/includes/class-CategoryTemplated.php index 2d49dae..990e33a 100644 --- a/includes/class-CategoryTemplated.php +++ b/includes/class-CategoryTemplated.php @@ -1,140 +1,128 @@ . namespace itwikidelbot; /** * Handle a generic category associated to a template */ -class CategoryTemplated extends Category { +abstract class CategoryTemplated extends Category { /** * Template name of this category * - * @param $name + * To be overrided */ - private $name; - - /** - * Arguments to be passed to the template - * - * @var array - */ - private $args; + const TEMPLATE_NAME = 'EXAMPLE_NAME'; /** * Constructor * - * @param $name Template name * @param $args Template arguments */ - public function __construct( $name, $args ) { - $this->name = $name; - $this->args = $args; + public function __construct() { parent::__construct( $this->getTemplatedTitle() ); } /** * Get the name of the template * * @return string */ public function getTemplateName() { - return $this->name; + return static::TEMPLATE_NAME; } /** * Get the name of the template for the page title * * @return string */ public function getTemplateTitleName() { return $this->getTemplateName() . '_TITLE'; } /** * Get the name of the template for the summary * * @return string */ public function getTemplateSummaryName() { return $this->getTemplateName() . '_SUMMARY'; } /** * Get the name of the template for the content */ public function getTemplateContentName() { return $this->getTemplateName() . '_CONTENT'; } /** * Get the template arguments * * @return array */ - public function getTemplateArguments() { - return $this->args; - } + abstract public function getTemplateArguments(); /** * Get the edit summary for this category from its template * * @return string */ public function getTemplatedSummary() { return Template::get( $this->getTemplateSummaryName(), $this->getTemplateArguments() ); } /** * Get the page title from its template * * @return string */ public function getTemplatedTitle() { return Template::get( $this->getTemplateTitleName(), $this->getTemplateArguments() ); } /** * Get the page content for this category from its template * * @return string */ public function getTemplatedContent() { return Template::get( $this->getTemplateContentName(), $this->getTemplateArguments() ); } /** * Save this category from the content of its template * * @return mixed */ public function save() { return $this->saveByContentSummary( $this->getTemplatedContent(), $this->getTemplatedSummary() ); } /** * Save this category from the content of its template if it does not exist * * @return self */ public function saveIfNotExists() { if( ! $this->exists() ) { $this->save(); } return $this; } } diff --git a/includes/class-CategoryYear.php b/includes/class-CategoryYear.php index e96e422..e509e2f 100644 --- a/includes/class-CategoryYear.php +++ b/includes/class-CategoryYear.php @@ -1,54 +1,66 @@ . namespace itwikidelbot; /** * Handle a yearly category * * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2017 */ class CategoryYear extends CategoryTemplated { + /** + * Template name of this category + * + * @override CategoryTemplated::TEMPLATE_NAME + */ + const TEMPLATE_NAME = 'CATEGORY_YEAR'; + /** * Year * * @var int */ private $year; /** * Constructor * * @param $year int * @see CategoryTemplated::__construct() */ public function __construct( $year ) { - - parent::__construct( 'CATEGORY_YEAR', [ - $year - ] ); - $this->year = $year; + parent::__construct(); + } + + /** + * Template arguments + * + * @override CategoryTemplated::getTemplateArguments() + */ + public function getTemplateArguments() { + return [ $this->year ]; } } diff --git a/includes/class-CategoryYearMonth.php b/includes/class-CategoryYearMonth.php index e39cfc1..8d5a40a 100644 --- a/includes/class-CategoryYearMonth.php +++ b/includes/class-CategoryYearMonth.php @@ -1,67 +1,81 @@ . namespace itwikidelbot; /** * Handle a monthly category * * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_aprile_2018 */ class CategoryYearMonth extends CategoryTemplated { + /** + * Template name of this category + * + * @override CategoryTemplated::TEMPLATE_NAME + */ + const TEMPLATE_NAME = 'CATEGORY_MONTH'; + /** * Year * * @var int */ private $year; /** * Month * * @var int 1-12 */ private $month; /** * Constructor * * @param $year int * @param $month int 1-12 * @see CategoryTemplated::__construct() */ public function __construct( $year, $month ) { + $this->year = $year; + $this->month = $month; + parent::__construct(); + } - parent::__construct( 'CATEGORY_MONTH', [ - $parent_title = ( new CategoryYear( $year ) ) + /** + * Template arguments + * + * @override CategoryTemplated::getTemplateArguments() + */ + public function getTemplateArguments() { + return [ + ( new CategoryYear( $this->year ) ) ->getTemplatedTitle(), - $year, - $month, - Months::number2name( $month - 1 ), - ] ); - - $this->year = $year; - $this->month = $month; + $this->year, + $this->month, + Months::number2name( $this->month - 1 ), + ]; } } diff --git a/includes/class-CategoryYearMonthDay.php b/includes/class-CategoryYearMonthDay.php index e09b5dc..3b0f4ee 100644 --- a/includes/class-CategoryYearMonthDay.php +++ b/includes/class-CategoryYearMonthDay.php @@ -1,77 +1,91 @@ . namespace itwikidelbot; /** * Handle a daily category * * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_del_19_febbraio_2018 */ class CategoryYearMonthDay extends CategoryTemplated { + /** + * Template name + * + * @override CategoryTemplated::TEMPLATE_NAME + */ + const TEMPLATE_NAME = 'CATEGORY_DAY'; + /** * Year * * @var int */ private $year; /** * Month * * @var int 1-12 */ private $month; /** * Day * * @var int 1-31 */ private $day; /** * Constructor * * @param $year int * @param $month int 1-12 * @param $day int 1-31 * @see CategoryTemplated::__construct() */ public function __construct( $year, $month, $day ) { + $this->year = $year; + $this->month = $month; + $this->day = $day; + parent::__construct(); + } - parent::__construct( 'CATEGORY_DAY', [ - ( new CategoryYearMonth( $year, $month ) ) + /** + * Template arguments + * + * @override CategoryTemplated::getTemplateArguments() + */ + public function getTemplateArguments() { + return [ + ( new CategoryYearMonth( $this->year, $this->month ) ) ->getTemplatedTitle(), - $year, - $month, - Months::number2name( $month - 1 ), - $day, - ] ); - - $this->year = $year; - $this->month = $month; - $this->day = $day; + $this->year, + $this->month, + Months::number2name( $this->month - 1 ), + $this->day, + ]; } }