2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Immutable class to represent an event.
|
|
|
|
* In Echo nomenclature, an event is a single occurrence.
|
|
|
|
* A user's subscriptions determine what Notifications they receive.
|
|
|
|
*/
|
|
|
|
class EchoEvent {
|
2013-01-14 23:52:46 +00:00
|
|
|
public $type = null;
|
2012-04-27 15:14:24 +00:00
|
|
|
protected $id = null;
|
|
|
|
protected $variant = null;
|
2012-09-02 09:30:38 +00:00
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
protected $agent = null;
|
2012-05-17 00:29:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Title
|
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
protected $title = null;
|
|
|
|
protected $extra = null;
|
|
|
|
protected $timestamp = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* You should not call the constructor.
|
|
|
|
* Instead use one of the factory functions:
|
2012-08-31 21:50:46 +00:00
|
|
|
* EchoEvent::create To create a new event
|
|
|
|
* EchoEvent::newFromRow To create an event object from a row object
|
|
|
|
* EchoEvent::newFromID To create an event object from the database given its ID
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
protected function __construct() {
|
|
|
|
}
|
|
|
|
|
2012-06-06 07:04:28 +00:00
|
|
|
## Save just as the ID
|
|
|
|
function __sleep() {
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$this->id ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
throw new MWException( "Unable to serialize an uninitialized EchoEvent" );
|
2012-06-06 07:04:28 +00:00
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
return array( 'id' );
|
2012-06-06 07:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function __wakeup() {
|
|
|
|
$this->loadFromID( $this->id );
|
|
|
|
}
|
|
|
|
|
|
|
|
function __toString() {
|
|
|
|
return "EchoEvent(id={$this->id}; type={$this->type})";
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
/**
|
|
|
|
* Creates an EchoEvent object
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $info array Named arguments:
|
2012-04-27 15:14:24 +00:00
|
|
|
* type (required): The event type;
|
|
|
|
* variant: A variant of the type;
|
|
|
|
* agent: The user who caused the event;
|
|
|
|
* title: The page on which the event was triggered;
|
|
|
|
* extra: Event-specific extra information (e.g. post content)
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
|
|
|
* @return EchoEvent
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
public static function create( $info = array() ) {
|
2013-02-16 02:20:34 +00:00
|
|
|
global $wgEchoNotifications;
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$obj = new EchoEvent;
|
|
|
|
static $validFields = array( 'type', 'variant', 'agent', 'title', 'extra' );
|
|
|
|
|
2012-08-01 19:53:05 +00:00
|
|
|
if ( empty( $info['type'] ) ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
throw new MWException( "'type' parameter is mandatory" );
|
|
|
|
}
|
|
|
|
|
2013-02-16 02:20:34 +00:00
|
|
|
if ( !isset( $wgEchoNotifications[$info['type']] ) ) {
|
2012-08-01 18:20:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$obj->id = false;
|
|
|
|
$obj->timestamp = wfTimestampNow();
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $validFields as $field ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( isset( $info[$field] ) ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$obj->$field = $info[$field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( $obj->title && !$obj->title instanceof Title ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
throw new MWException( "Invalid title parameter" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $obj->agent && !
|
2012-08-31 21:50:46 +00:00
|
|
|
( $obj->agent instanceof User ||
|
2012-08-30 16:04:39 +00:00
|
|
|
$obj->agent instanceof StubObject )
|
2012-04-27 15:14:24 +00:00
|
|
|
) {
|
|
|
|
throw new MWException( "Invalid user parameter" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$obj->insert();
|
2012-12-19 20:28:17 +00:00
|
|
|
|
|
|
|
global $wgEchoUseJobQueue;
|
|
|
|
|
|
|
|
EchoNotificationController::notify( $obj, $wgEchoUseJobQueue );
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
2012-11-13 23:06:11 +00:00
|
|
|
/**
|
|
|
|
* Check whether the echo event is an enabled event
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isEnabledEvent() {
|
2013-02-16 02:20:34 +00:00
|
|
|
global $wgEchoNotifications;
|
|
|
|
if ( isset( $wgEchoNotifications[$this->getType()] ) ) {
|
2012-11-13 23:06:11 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
/**
|
|
|
|
* Inserts the object into the database.
|
|
|
|
*/
|
|
|
|
protected function insert() {
|
2013-01-15 23:21:39 +00:00
|
|
|
global $wgEchoBackend;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
if ( $this->id ) {
|
|
|
|
throw new MWException( "Attempt to insert() an existing event" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$row = array(
|
|
|
|
'event_id' => $this->id,
|
2013-01-15 23:21:39 +00:00
|
|
|
'event_timestamp' => $this->timestamp,
|
2012-04-27 15:14:24 +00:00
|
|
|
'event_type' => $this->type,
|
|
|
|
'event_variant' => $this->variant,
|
|
|
|
);
|
|
|
|
|
2012-12-26 22:05:29 +00:00
|
|
|
$row['event_extra'] = $this->serializeExtra();
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
if ( $this->agent ) {
|
|
|
|
if ( $this->agent->isAnon() ) {
|
|
|
|
$row['event_agent_ip'] = $this->agent->getName();
|
|
|
|
} else {
|
|
|
|
$row['event_agent_id'] = $this->agent->getId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->title ) {
|
|
|
|
$row['event_page_namespace'] = $this->title->getNamespace();
|
|
|
|
$row['event_page_title'] = $this->title->getDBkey();
|
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
$this->id = $wgEchoBackend->createEvent( $row );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-06 07:04:28 +00:00
|
|
|
* Loads data from the provided $row into this object.
|
2012-04-27 15:14:24 +00:00
|
|
|
*
|
|
|
|
* @param $row Database row object from echo_event
|
|
|
|
*/
|
2012-08-30 16:04:39 +00:00
|
|
|
public function loadFromRow( $row ) {
|
2012-06-06 07:04:28 +00:00
|
|
|
$this->id = $row->event_id;
|
|
|
|
$this->timestamp = $row->event_timestamp;
|
|
|
|
$this->type = $row->event_type;
|
|
|
|
$this->variant = $row->event_variant;
|
2012-08-30 16:04:39 +00:00
|
|
|
$this->extra = $row->event_extra ? unserialize( $row->event_extra ) : null;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
if ( $row->event_agent_id ) {
|
2012-06-06 07:04:28 +00:00
|
|
|
$this->agent = User::newFromID( $row->event_agent_id );
|
2012-04-27 15:14:24 +00:00
|
|
|
} elseif ( $row->event_agent_ip ) {
|
2012-06-06 07:04:28 +00:00
|
|
|
$this->agent = User::newFromName( $row->event_agent_ip, false );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $row->event_page_title !== null ) {
|
2012-06-06 07:04:28 +00:00
|
|
|
$this->title = Title::makeTitleSafe(
|
2012-04-27 15:14:24 +00:00
|
|
|
$row->event_page_namespace,
|
|
|
|
$row->event_page_title
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-06 07:04:28 +00:00
|
|
|
* Loads data from the database into this object, given the event ID.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $id int Event ID
|
|
|
|
* @param $fromMaster bool
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
2012-08-02 19:50:46 +00:00
|
|
|
public function loadFromID( $id, $fromMaster = false ) {
|
2013-01-15 23:21:39 +00:00
|
|
|
global $wgEchoBackend;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
$this->loadFromRow( $wgEchoBackend->loadEvent( $id, $fromMaster ) );
|
2012-06-06 07:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an EchoEvent from a row object
|
|
|
|
*
|
|
|
|
* @param $row Database row object from echo_event
|
|
|
|
* @return EchoEvent object.
|
|
|
|
*/
|
2012-08-30 16:04:39 +00:00
|
|
|
public static function newFromRow( $row ) {
|
2012-06-06 07:04:28 +00:00
|
|
|
$obj = new EchoEvent();
|
|
|
|
$obj->loadFromRow( $row );
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an EchoEvent from the database by ID
|
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $id int Event ID
|
|
|
|
* @return EchoEvent
|
2012-06-06 07:04:28 +00:00
|
|
|
*/
|
|
|
|
public static function newFromID( $id ) {
|
|
|
|
$obj = new EchoEvent();
|
|
|
|
$obj->loadFromID( $id );
|
|
|
|
return $obj;
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-26 22:05:29 +00:00
|
|
|
/**
|
|
|
|
* Update extra data
|
|
|
|
*/
|
|
|
|
public function updateExtra( $extra ) {
|
2013-01-15 23:21:39 +00:00
|
|
|
global $wgEchoBackend;
|
2012-12-26 22:05:29 +00:00
|
|
|
|
|
|
|
$this->extra = $extra;
|
|
|
|
if ( $this->id && $this->extra ) {
|
2013-01-15 23:21:39 +00:00
|
|
|
$wgEchoBackend->updateEventExtra( $this );
|
2012-12-26 22:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize the extra data for event
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-01-15 23:21:39 +00:00
|
|
|
public function serializeExtra() {
|
2012-12-26 22:05:29 +00:00
|
|
|
if ( is_array( $this->extra ) || is_object( $this->extra ) ) {
|
|
|
|
$extra = serialize( $this->extra );
|
|
|
|
} elseif ( is_null( $this->extra ) ) {
|
|
|
|
$extra = null;
|
|
|
|
} else {
|
|
|
|
$extra = serialize( array( $this->extra ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $extra;
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
## Accessors
|
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTimestamp() {
|
|
|
|
return $this->timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getType() {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getVariant() {
|
|
|
|
return $this->variant;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getExtra() {
|
|
|
|
return $this->extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAgent() {
|
|
|
|
return $this->agent;
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
2012-11-14 21:52:17 +00:00
|
|
|
* @return Title|null
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public function getTitle() {
|
|
|
|
return $this->title;
|
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|