2014-08-06 00:16:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2016-09-07 17:38:12 +00:00
|
|
|
* Map a title to an echo event so that we can mark a notification as read
|
2014-08-06 00:16:10 +00:00
|
|
|
* when visiting the page. This only supports titles with ids because majority
|
|
|
|
* of notifications have page_id and searching by namespace and title is slow
|
|
|
|
*/
|
|
|
|
class EchoTargetPage extends EchoAbstractEntity {
|
|
|
|
|
|
|
|
/**
|
2018-08-13 07:32:22 +00:00
|
|
|
* @var Title|null|false False if not initialized yet
|
2014-08-06 00:16:10 +00:00
|
|
|
*/
|
2016-03-07 23:56:00 +00:00
|
|
|
protected $title = false;
|
2014-08-06 00:16:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $pageId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var EchoEvent|null
|
|
|
|
*/
|
|
|
|
protected $event;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $eventId;
|
|
|
|
|
2016-03-09 19:01:42 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $eventType;
|
|
|
|
|
2014-08-06 00:16:10 +00:00
|
|
|
/**
|
|
|
|
* Only allow creating instance internally
|
|
|
|
*/
|
2015-10-01 13:48:52 +00:00
|
|
|
protected function __construct() {
|
|
|
|
}
|
2014-08-06 00:16:10 +00:00
|
|
|
|
|
|
|
/**
|
2016-09-07 17:38:12 +00:00
|
|
|
* Create a EchoTargetPage instance from Title and EchoEvent
|
2014-08-06 00:16:10 +00:00
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
* @param EchoEvent $event
|
2016-03-09 19:01:42 +00:00
|
|
|
* @return EchoTargetPage|null
|
2014-08-06 00:16:10 +00:00
|
|
|
*/
|
2016-09-07 17:38:12 +00:00
|
|
|
public static function create( Title $title, EchoEvent $event ) {
|
2014-08-06 00:16:10 +00:00
|
|
|
// This only support title with a page_id
|
2016-09-07 17:38:12 +00:00
|
|
|
if ( !$title->getArticleID() ) {
|
2014-08-06 00:16:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$obj = new self();
|
|
|
|
$obj->event = $event;
|
|
|
|
$obj->eventId = $event->getId();
|
2016-03-09 19:01:42 +00:00
|
|
|
$obj->eventType = $event->getType();
|
2014-08-06 00:16:10 +00:00
|
|
|
$obj->title = $title;
|
|
|
|
$obj->pageId = $title->getArticleID();
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-06 00:16:10 +00:00
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a EchoTargetPage instance from stdClass object
|
|
|
|
*
|
|
|
|
* @param stdClass $row
|
|
|
|
* @return EchoTargetPage
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
|
|
|
public static function newFromRow( $row ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$requiredFields = [
|
2014-08-06 00:16:10 +00:00
|
|
|
'etp_page',
|
|
|
|
'etp_event'
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-08-06 00:16:10 +00:00
|
|
|
foreach ( $requiredFields as $field ) {
|
|
|
|
if ( !isset( $row->$field ) || !$row->$field ) {
|
|
|
|
throw new MWException( $field . ' is not set in the row!' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$obj = new self();
|
|
|
|
$obj->pageId = $row->etp_page;
|
|
|
|
$obj->eventId = $row->etp_event;
|
2016-03-09 19:01:42 +00:00
|
|
|
if ( isset( $row->event_type ) ) {
|
|
|
|
$obj->eventType = $row->event_type;
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-06 00:16:10 +00:00
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Title|null
|
|
|
|
*/
|
|
|
|
public function getTitle() {
|
2016-03-07 23:56:00 +00:00
|
|
|
if ( $this->title === false ) {
|
2019-06-11 21:43:05 +00:00
|
|
|
$this->title = Title::newFromID( $this->pageId );
|
2014-08-06 00:16:10 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-06 00:16:10 +00:00
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getPageId() {
|
|
|
|
return $this->pageId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return EchoEvent
|
|
|
|
*/
|
|
|
|
public function getEvent() {
|
|
|
|
if ( !$this->event ) {
|
|
|
|
$this->event = EchoEvent::newFromID( $this->eventId );
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-06 00:16:10 +00:00
|
|
|
return $this->event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getEventId() {
|
|
|
|
return $this->eventId;
|
|
|
|
}
|
|
|
|
|
2016-03-09 19:01:42 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getEventType() {
|
|
|
|
if ( !$this->eventType ) {
|
|
|
|
$this->eventType = $this->getEvent()->getType();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->eventType;
|
|
|
|
}
|
|
|
|
|
2014-08-06 00:16:10 +00:00
|
|
|
/**
|
|
|
|
* Convert the properties to a database row
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return int[]
|
2014-08-06 00:16:10 +00:00
|
|
|
*/
|
|
|
|
public function toDbArray() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2014-08-06 00:16:10 +00:00
|
|
|
'etp_page' => $this->pageId,
|
|
|
|
'etp_event' => $this->eventId
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-08-06 00:16:10 +00:00
|
|
|
}
|
|
|
|
}
|