diff --git a/includes/class-PDC.php b/includes/class-PDC.php index 17e796f..676a24e 100644 --- a/includes/class-PDC.php +++ b/includes/class-PDC.php @@ -1,169 +1,197 @@ . namespace itwikidelbot; use DateTime; /** * Handle a "Procedura di cancellazione" */ class PDC extends Page { /** * Prefix of every PDC */ const PREFIX = 'Wikipedia:Pagine da cancellare/'; /** * Prefix of every multiple PDC */ const PREFIX_MULTIPLE = self::PREFIX . 'multiple/'; /** * PDC type * * @var class */ private $type; /** * Page id * * @var int */ private $id; /** * Page title with prefix * * @var string */ private $title; /** * Page length in bytes * * @var int */ private $length; /** * Page date * * @var date */ private $date; + /** + * Is Protected? + * + * @var bool + */ + private $isProtected; + /** * Constructor * - * @param $id Page id - * @param $title string Page title - * @param $length Page length - * @param $date Page last update + * @param $id int Page id + * @param $title string string Page title + * @param $length int Page length + * @param $date Page DateTime last update + * @param $is_protected bool Is protected? * @see Page::__construct() */ - public function __construct( $type, $id, $title, $length, $date ) { - $this->type = $type; - $this->id = $id; - $this->title = $title; - $this->length = $length; - $this->date = $date; + public function __construct( $type, $id, $title, $length, DateTime $date, $is_protected ) { + $this->type = $type; + $this->id = $id; + $this->title = $title; + $this->length = $length; + $this->date = $date; + $this->isProtected = $is_protected; parent::__construct( $title ); } /** * Statical constructor * * @param $type string Specified PDC class name * @param $page mixed|null */ public static function createFromRaw( $type, $page ) { if( ! isset( $page->touched, $page->pageid, $page->title, $page->length ) ) { throw new \Exception( 'invalid PDC' ); } + + $is_protected = false; + foreach( $page->protection as $protection ) { + if( 'edit' === $protection->type && 'sysop' === $protection->level ) { + $is_protected = true; + break; + } + } + return new self( $type, $page->pageid, $page->title, $page->length, - DateTime::createFromFormat( DateTime::ISO8601, $page->touched ) // e.g. 2018-04-22T14:07:49Z + DateTime::createFromFormat( DateTime::ISO8601, $page->touched ), // e.g. 2018-04-22T14:07:49Z + $is_protected ); } /** * Get the page length * * @return int */ public function getLength() { return $this->length; } /** * Get the latest update date * * @return DateTime */ public function getDate() { return $this->date; } /** * Get the PDC type * * @return string e.g. 'consensuale' */ public function getType() { return $this->type::getPDCType(); } + /** + * Check if this page is protected + * + * @return bool + */ + public function isProtected() { + return $this->isProtected; + } + /** * Check if this is a valid PDC * * @return bool */ public function isValid() { return $this->titlehasPrefix( self::PREFIX ); } /** * Check if this PDC involves multiple PDCs * * @return bool */ public function isMultiple() { return $this->titleHasPrefix( self::PREFIX_MULTIPLE ); } /** * Check if the title has a certain prefix * * @param $prefix string * @return bool */ private function titleHasPrefix( $prefix ) { return 0 === strpos( $this->getTitle(), $prefix ); } }