2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class EchoSubscription {
|
|
|
|
private $user = null;
|
|
|
|
private $event = null;
|
|
|
|
private $title = null;
|
|
|
|
private $notificationTypes = null;
|
|
|
|
private $dirty = false;
|
|
|
|
private $loaded = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main constructor
|
|
|
|
* Creates an EchoSubscription object representing the subscription by
|
|
|
|
* a user to an event (on a page if applicable).
|
|
|
|
*
|
|
|
|
* @param $user User object for the user whose subscription we're talking about.
|
2012-08-01 19:53:05 +00:00
|
|
|
* @param $event String identifier for the event type of interest. Max length is 63 chars.
|
2012-05-17 00:29:37 +00:00
|
|
|
* @param $title Title|null Optional Title of interest for events, if applicable.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
2012-08-31 21:50:46 +00:00
|
|
|
public function __construct( $user, $event, $title = null ) {
|
|
|
|
if ( !( $user instanceof User || $user instanceof StubObject ) ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
throw new MWException( "Invalid user parameter" );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $title && !$title instanceof Title ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
throw new MWException( "Invalid Title parameter" );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( !$event || !is_string( $event ) || strlen( $event ) > 63 ) {
|
|
|
|
throw new MWException( "Invalid event parameter" );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->user = $user;
|
|
|
|
$this->event = $event;
|
|
|
|
$this->title = $title;
|
|
|
|
$this->loaded = false;
|
|
|
|
$this->dirty = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load subscription data from pre-selected echo_subscription rows.
|
|
|
|
* Will throw an exception rather than reconfigure the object if
|
|
|
|
* the rows are not matched to this object's setup.
|
|
|
|
*
|
|
|
|
* @param $rows Array of row objects from echo_subscription table.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
public function loadFromRows( $rows ) {
|
|
|
|
global $wgEchoDefaultNotificationTypes;
|
|
|
|
$this->notificationTypes = $wgEchoDefaultNotificationTypes['all'];
|
|
|
|
if ( isset( $wgEchoDefaultNotificationTypes[$this->event] ) ) {
|
|
|
|
$this->notificationTypes = array_merge(
|
|
|
|
$this->notificationTypes,
|
|
|
|
$wgEchoDefaultNotificationTypes[$this->event]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $rows as $row ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
if ( $this->title ) {
|
|
|
|
$correctNS = $this->title->getNamespace();
|
|
|
|
$correctTitle = $this->title->getDBkey();
|
|
|
|
} else {
|
|
|
|
$correctNS = null;
|
|
|
|
$correctTitle = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
$row->sub_user != $this->user->getId() ||
|
|
|
|
$row->sub_event_type != $this->event ||
|
|
|
|
$row->sub_page_namespace != $correctNS ||
|
|
|
|
$row->sub_page_title != $correctTitle
|
|
|
|
) {
|
|
|
|
throw new MWException( "Invalid parameter: mismatched rows" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $row->sub_enabled ) {
|
|
|
|
$this->notificationTypes[$row->sub_notify_type] = true;
|
|
|
|
} else {
|
|
|
|
$this->notificationTypes[$row->sub_notify_type] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->loaded = true;
|
|
|
|
$this->dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an EchoSubscription object for an array of echo_notification rows.
|
|
|
|
* Obviously, the rows must be from a single subscription
|
|
|
|
* (their user/event/title must match)
|
|
|
|
*
|
|
|
|
* @param $rows Array of row objects from echo_subscription table.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return EchoSubscription
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
public static function newFromRows( $rows ) {
|
|
|
|
$firstRow = $rows[0];
|
|
|
|
|
|
|
|
$user = User::newFromId( $firstRow->sub_user );
|
|
|
|
$event = $firstRow->sub_event_type;
|
|
|
|
|
|
|
|
if (
|
2012-08-30 16:04:39 +00:00
|
|
|
!is_null( $firstRow->sub_page_namespace ) &&
|
|
|
|
!is_null( $firstRow->sub_page_title )
|
2012-04-27 15:14:24 +00:00
|
|
|
) {
|
|
|
|
$title = Title::makeTitleSafe(
|
|
|
|
$firstRow->sub_page_namespace,
|
|
|
|
$firstRow->sub_page_title
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$title = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$obj = new EchoSubscription( $user, $event, $title );
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
$obj->loadFromRows( $rows );
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getConds() {
|
|
|
|
$conds = array(
|
2012-08-31 21:50:46 +00:00
|
|
|
'sub_user' => $this->user->getId(),
|
|
|
|
'sub_event_type' => $this->event,
|
|
|
|
);
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
if ( $this->title ) {
|
|
|
|
$conds['sub_page_namespace'] = $this->title->getNamespace();
|
|
|
|
$conds['sub_page_title'] = $this->title->getDBkey();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $conds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads data from the database
|
|
|
|
*/
|
|
|
|
protected function load() {
|
|
|
|
if ( $this->loaded ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
|
|
|
|
$conds = $this->getConds();
|
|
|
|
$res = $dbr->select( 'echo_subscription', '*', $conds, __METHOD__ );
|
|
|
|
|
|
|
|
$this->loadFromRows( $res );
|
|
|
|
}
|
|
|
|
|
|
|
|
## ACCESSORS
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* @return User
|
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public function getUser() {
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEventType() {
|
|
|
|
return $this->event;
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* @return Title
|
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public function getTitle() {
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNotificationTypes() {
|
|
|
|
$this->load();
|
|
|
|
return $this->notificationTypes;
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @return bool
|
|
|
|
*/
|
2012-08-30 16:04:39 +00:00
|
|
|
public function isNotificationEnabled( $type ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$types = $this->getNotificationTypes();
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
return !empty( $types[$type] );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
public function enableNotification( $type ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$this->load();
|
|
|
|
$this->notificationTypes[$type] = true;
|
|
|
|
$this->dirty = true;
|
|
|
|
}
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
public function disableNotification( $type ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$this->load();
|
|
|
|
$this->notificationTypes[$type] = false;
|
|
|
|
$this->dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save() {
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$this->dirty ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$conds = $this->getConds();
|
|
|
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
$dbw->begin();
|
|
|
|
$dbw->delete( 'echo_subscription', $conds, __METHOD__ );
|
|
|
|
|
|
|
|
global $wgEchoDefaultNotificationTypes;
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( isset( $wgEchoDefaultNotificationTypes[$this->event] ) ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$defaultState = array_merge(
|
|
|
|
$wgEchoDefaultNotificationTypes['all'],
|
|
|
|
$wgEchoDefaultNotificationTypes[$this->event]
|
|
|
|
);
|
|
|
|
} else {
|
2012-08-31 21:50:46 +00:00
|
|
|
$defaultState = $wgEchoDefaultNotificationTypes['all'];
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$rows = array();
|
|
|
|
$allDefault = count( array_diff(
|
2012-08-30 16:04:39 +00:00
|
|
|
array_keys( array_filter( $this->getNotificationTypes() ) ),
|
|
|
|
array_keys( array_filter( $defaultState ) )
|
2012-04-27 15:14:24 +00:00
|
|
|
) ) === 0;
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $this->getNotificationTypes() as $type => $state ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( !isset( $defaultState[$type] ) || $state != $defaultState[$type] || $allDefault ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$rows[] = $conds + array(
|
|
|
|
'sub_enabled' => $state,
|
|
|
|
'sub_notify_type' => $type,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( count( $rows ) ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$dbw->insert( 'echo_subscription', $rows, __METHOD__ );
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbw->commit();
|
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|