diff --git a/includes/class-Category.php b/includes/class-Category.php index 1140f3f..a1bb6c9 100644 --- a/includes/class-Category.php +++ b/includes/class-Category.php @@ -1,163 +1,162 @@ . 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 ); + return $api->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(); } }