diff --git a/includes/class-Category.php b/includes/class-Category.php index b84572b..edeee18 100644 --- a/includes/class-Category.php +++ b/includes/class-Category.php @@ -1,153 +1,153 @@ . 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 ) { $wit = WikipediaIt::getInstance(); $args = [ 'action' => 'edit', 'title' => $title, 'text' => $content, 'summary' => $summary, 'token' => $wit->login()->getToken( Tokens::CSRF ), 'bot' => 1, ]; if( self::$INSPECT ) { print_r( $args ); } 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 = WikipediaIt::getInstance()->fetch( [ 'action' => 'query', 'prop' => 'categoryinfo', 'titles' => $title, ] ); foreach( $categoryinfo->query->pages as $pageid => $page ) { - return $pageid < 0 && isset( $page->missing ); + return ! isset( $page->missing ); } return false; } } diff --git a/includes/class-CategoryTemplated.php b/includes/class-CategoryTemplated.php index 53b63d7..a462cd6 100644 --- a/includes/class-CategoryTemplated.php +++ b/includes/class-CategoryTemplated.php @@ -1,76 +1,76 @@ . 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 getTemplatedSummary() { return Template::get( static::TEMPLATE_NAME . '_SUMMARY', $this->getTemplateArguments() ); } /** * Get the page content for this category from its template * * @return string */ public function getTemplatedContent() { 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->saveByContentSummary( $this->getTemplatedContent(), $this->getTemplatedSummary() ); } /** * Save this category from the content of its template if not exists * * @return bool|mixed */ public function saveIfNotExists() { - if( $this->exists() ) { + if( ! $this->exists() ) { return $this->save(); } return false; } }