2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
2017-03-30 19:35:32 +00:00
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
2019-04-17 15:46:06 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-01-14 05:09:42 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2021-02-18 21:45:19 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2017-03-30 19:35:32 +00:00
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
/**
|
|
|
|
* Immutable class to represent an event.
|
|
|
|
* In Echo nomenclature, an event is a single occurrence.
|
|
|
|
*/
|
2016-03-04 19:23:02 +00:00
|
|
|
class EchoEvent extends EchoAbstractEntity implements Bundleable {
|
2014-08-14 18:46:26 +00:00
|
|
|
|
2019-02-15 20:23:02 +00:00
|
|
|
/** @var string|null */
|
2013-03-04 20:56:42 +00:00
|
|
|
protected $type = null;
|
2019-12-21 05:45:14 +00:00
|
|
|
/** @var int|null|false */
|
2012-04-27 15:14:24 +00:00
|
|
|
protected $id = null;
|
2019-02-15 20:23:02 +00:00
|
|
|
/** @var string|null */
|
2012-04-27 15:14:24 +00:00
|
|
|
protected $variant = null;
|
2012-09-02 09:30:38 +00:00
|
|
|
/**
|
2019-02-15 20:23:02 +00:00
|
|
|
* @var User|null
|
2012-09-02 09:30:38 +00:00
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
protected $agent = null;
|
2012-05-17 00:29:37 +00:00
|
|
|
|
|
|
|
/**
|
2013-05-08 18:11:44 +00:00
|
|
|
* Loaded dynamically on request
|
|
|
|
*
|
2019-02-15 20:23:02 +00:00
|
|
|
* @var Title|null
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
protected $title = null;
|
2019-02-15 20:23:02 +00:00
|
|
|
/** @var int|null */
|
2013-05-08 18:11:44 +00:00
|
|
|
protected $pageId = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loaded dynamically on request
|
|
|
|
*
|
2019-04-17 15:46:06 +00:00
|
|
|
* @var RevisionRecord|null
|
2013-05-08 18:11:44 +00:00
|
|
|
*/
|
|
|
|
protected $revision = null;
|
|
|
|
|
2019-02-15 20:23:02 +00:00
|
|
|
/** @var array */
|
2016-12-05 18:51:07 +00:00
|
|
|
protected $extra = [];
|
2013-01-15 23:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification timestamp
|
2019-02-15 20:23:02 +00:00
|
|
|
* @var string|null
|
2013-01-15 23:21:39 +00:00
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
protected $timestamp = null;
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
/**
|
|
|
|
* A hash used to bundle a set of events, events that can be
|
|
|
|
* grouped for a user has the same bundle hash
|
2019-12-21 05:45:14 +00:00
|
|
|
* @var string|null
|
2013-01-15 23:21:39 +00:00
|
|
|
*/
|
|
|
|
protected $bundleHash;
|
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
/**
|
|
|
|
* Other events bundled with this one
|
|
|
|
*
|
|
|
|
* @var EchoEvent[]
|
|
|
|
*/
|
|
|
|
protected $bundledEvents;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletion flag
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $deleted = 0;
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2015-10-01 13:48:52 +00:00
|
|
|
protected function __construct() {
|
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
## Save the id and timestamp
|
2018-11-02 17:47:39 +00:00
|
|
|
public 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
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
return [ 'id', 'timestamp' ];
|
2012-06-06 07:04:28 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:47:39 +00:00
|
|
|
public function __wakeup() {
|
2012-06-06 07:04:28 +00:00
|
|
|
$this->loadFromID( $this->id );
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:47:39 +00:00
|
|
|
public function __toString() {
|
2012-06-06 07:04:28 +00:00
|
|
|
return "EchoEvent(id={$this->id}; type={$this->type})";
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
/**
|
|
|
|
* Creates an EchoEvent object
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param array $info 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;
|
2020-10-09 14:09:21 +00:00
|
|
|
* extra: Event-specific extra information (e.g. post content, delay time, root job params).
|
|
|
|
*
|
|
|
|
* Delayed jobs extra params:
|
|
|
|
* delay: Amount of time in seconds for the notification to be delayed
|
|
|
|
*
|
|
|
|
* Job deduplication extra params:
|
|
|
|
* rootJobSignature: The sha1 signature of the job
|
|
|
|
* rootJobTimestamp: The timestamp when the job gets submitted
|
|
|
|
*
|
|
|
|
* For example to enqueue a new `example` root job or make a parent job
|
|
|
|
* no-op when submitting a new notification you need to pass this extra params:
|
|
|
|
*
|
|
|
|
* [ 'extra' => Job::newRootJobParams('example') ]
|
2012-04-27 15:14:24 +00:00
|
|
|
*
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return EchoEvent|false False if aborted via hook or Echo DB is read-only
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
public static function create( $info = [] ) {
|
2013-02-16 02:20:34 +00:00
|
|
|
global $wgEchoNotifications;
|
|
|
|
|
2013-05-08 21:24:47 +00:00
|
|
|
// Do not create event and notifications if write access is locked
|
2015-11-01 10:50:44 +00:00
|
|
|
if ( wfReadOnly()
|
2021-05-03 07:28:02 +00:00
|
|
|
|| MWEchoDbFactory::newFromDefault()->getEchoDb( DB_PRIMARY )->isReadOnly()
|
2015-11-01 10:50:44 +00:00
|
|
|
) {
|
|
|
|
return false;
|
2013-05-08 21:24:47 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
$obj = new EchoEvent;
|
2016-12-05 18:51:07 +00:00
|
|
|
static $validFields = [ 'type', 'variant', 'agent', 'title', 'extra' ];
|
2012-04-27 15:14:24 +00:00
|
|
|
|
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;
|
2016-08-19 18:55:05 +00:00
|
|
|
if ( isset( $info['timestamp'] ) && $info[ 'timestamp' ] !== null ) {
|
2015-03-26 21:52:41 +00:00
|
|
|
$obj->timestamp = $info['timestamp'];
|
|
|
|
} else {
|
|
|
|
$obj->timestamp = wfTimestampNow();
|
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-05 18:56:11 +00:00
|
|
|
// If the extra size is more than 50000 bytes, that means there is
|
2014-02-05 22:21:48 +00:00
|
|
|
// probably a problem with the design of this notification type.
|
|
|
|
// There might be data loss if the size exceeds the DB column size of
|
|
|
|
// event_extra.
|
2014-02-05 18:56:11 +00:00
|
|
|
if ( strlen( $obj->serializeExtra() ) > 50000 ) {
|
|
|
|
wfDebugLog( __CLASS__, __FUNCTION__ . ': event extra data is too huge for ' . $info['type'] );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-02-05 18:56:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-08 18:11:44 +00:00
|
|
|
if ( $obj->title ) {
|
|
|
|
if ( !$obj->title instanceof Title ) {
|
2017-10-03 22:39:23 +00:00
|
|
|
throw new InvalidArgumentException( 'Invalid title parameter' );
|
2013-05-08 18:11:44 +00:00
|
|
|
}
|
2013-11-30 06:20:03 +00:00
|
|
|
$obj->setTitle( $obj->title );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 21:45:19 +00:00
|
|
|
if ( $obj->agent ) {
|
|
|
|
if ( !$obj->agent instanceof UserIdentity ) {
|
|
|
|
throw new InvalidArgumentException( "Invalid user parameter" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevisionStore returns UserIdentityValue now, convert to User for passing to hooks.
|
|
|
|
if ( !$obj->agent instanceof User ) {
|
|
|
|
$obj->agent = MediaWikiServices::getInstance()->getUserFactory()->newFromUserIdentity( $obj->agent );
|
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
if ( !Hooks::run( 'BeforeEchoEventInsert', [ $obj ] ) ) {
|
2013-09-28 02:19:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-29 11:23:31 +00:00
|
|
|
// @Todo - Database insert logic should not be inside the model
|
2012-04-27 15:14:24 +00:00
|
|
|
$obj->insert();
|
2012-12-19 20:28:17 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
Hooks::run( 'EchoEventInsertComplete', [ $obj ] );
|
2013-09-28 02:19:08 +00:00
|
|
|
|
2012-12-19 20:28:17 +00:00
|
|
|
global $wgEchoUseJobQueue;
|
|
|
|
|
2015-10-01 13:48:52 +00:00
|
|
|
EchoNotificationController::notify( $obj, $wgEchoUseJobQueue );
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2020-08-20 17:46:39 +00:00
|
|
|
$stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
|
|
|
|
$type = $info['type'];
|
|
|
|
$stats->increment( 'echo.event.all' );
|
|
|
|
$stats->increment( "echo.event.$type" );
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Convert the object's database property to array
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function toDbArray() {
|
2016-12-05 18:51:07 +00:00
|
|
|
$data = [
|
2014-07-18 03:58:21 +00:00
|
|
|
'event_type' => $this->type,
|
|
|
|
'event_variant' => $this->variant,
|
2016-03-04 19:23:02 +00:00
|
|
|
'event_deleted' => $this->deleted,
|
2014-07-18 03:58:21 +00:00
|
|
|
'event_extra' => $this->serializeExtra()
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-18 03:58:21 +00:00
|
|
|
if ( $this->id ) {
|
|
|
|
$data['event_id'] = $this->id;
|
|
|
|
}
|
|
|
|
if ( $this->agent ) {
|
2020-12-22 15:36:58 +00:00
|
|
|
if ( !$this->agent->isRegistered() ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$data['event_agent_ip'] = $this->agent->getName();
|
|
|
|
} else {
|
|
|
|
$data['event_agent_id'] = $this->agent->getId();
|
|
|
|
}
|
|
|
|
}
|
2014-08-16 00:24:11 +00:00
|
|
|
|
|
|
|
if ( $this->pageId ) {
|
|
|
|
$data['event_page_id'] = $this->pageId;
|
|
|
|
} elseif ( $this->title ) {
|
2019-04-17 15:46:06 +00:00
|
|
|
$pageId = $this->title->getArticleID();
|
2014-07-18 03:58:21 +00:00
|
|
|
// Don't need any special handling for title with no id
|
|
|
|
// as they are already stored in extra data array
|
|
|
|
if ( $pageId ) {
|
|
|
|
$data['event_page_id'] = $pageId;
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
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() {
|
2014-08-14 18:46:26 +00:00
|
|
|
$eventMapper = new EchoEventMapper();
|
2014-07-18 03:58:21 +00:00
|
|
|
$this->id = $eventMapper->insert( $this );
|
2016-09-07 17:38:12 +00:00
|
|
|
|
|
|
|
$targetPages = self::resolveTargetPages( $this->getExtraParam( 'target-page' ) );
|
|
|
|
if ( $targetPages ) {
|
2016-09-12 18:36:23 +00:00
|
|
|
$targetMapper = new EchoTargetPageMapper();
|
|
|
|
foreach ( $targetPages as $title ) {
|
|
|
|
$targetPage = EchoTargetPage::create( $title, $this );
|
|
|
|
if ( $targetPage ) {
|
|
|
|
$targetMapper->insert( $targetPage );
|
2016-09-07 17:38:12 +00:00
|
|
|
}
|
2016-09-12 18:36:23 +00:00
|
|
|
}
|
2016-09-07 17:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int[]|int|false $targetPageIds
|
|
|
|
* @return Title[]
|
|
|
|
*/
|
|
|
|
protected static function resolveTargetPages( $targetPageIds ) {
|
|
|
|
if ( !$targetPageIds ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [];
|
2016-09-07 17:38:12 +00:00
|
|
|
}
|
|
|
|
if ( !is_array( $targetPageIds ) ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$targetPageIds = [ $targetPageIds ];
|
2016-09-07 17:38:12 +00:00
|
|
|
}
|
2016-12-05 18:51:07 +00:00
|
|
|
$result = [];
|
2016-09-07 17:38:12 +00:00
|
|
|
foreach ( $targetPageIds as $targetPageId ) {
|
|
|
|
// Make sure the target-page id is a valid id
|
|
|
|
$title = Title::newFromID( $targetPageId );
|
2021-05-13 00:04:54 +00:00
|
|
|
// Try primary database if there is no match
|
2016-09-07 17:38:12 +00:00
|
|
|
if ( !$title ) {
|
|
|
|
$title = Title::newFromID( $targetPageId, Title::GAID_FOR_UPDATE );
|
|
|
|
}
|
|
|
|
if ( $title ) {
|
|
|
|
$result[] = $title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
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
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param stdClass $row row object from echo_event
|
2016-05-13 23:39:17 +00:00
|
|
|
* @return bool Whether loading was successful
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
2012-08-30 16:04:39 +00:00
|
|
|
public function loadFromRow( $row ) {
|
2019-04-10 20:09:40 +00:00
|
|
|
$this->id = (int)$row->event_id;
|
2012-06-06 07:04:28 +00:00
|
|
|
$this->type = $row->event_type;
|
2013-01-15 23:21:39 +00:00
|
|
|
|
|
|
|
// If the object is loaded from __sleep(), timestamp should be already set
|
|
|
|
if ( !$this->timestamp ) {
|
|
|
|
if ( isset( $row->notification_timestamp ) ) {
|
2013-08-22 21:51:01 +00:00
|
|
|
$this->timestamp = wfTimestamp( TS_MW, $row->notification_timestamp );
|
2013-01-15 23:21:39 +00:00
|
|
|
} else {
|
|
|
|
$this->timestamp = wfTimestampNow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 07:04:28 +00:00
|
|
|
$this->variant = $row->event_variant;
|
2016-05-13 23:39:17 +00:00
|
|
|
try {
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->extra = $row->event_extra ? unserialize( $row->event_extra ) : [];
|
2016-05-13 23:39:17 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
// T73489: unserializing can fail for old notifications
|
2017-03-01 21:02:59 +00:00
|
|
|
LoggerFactory::getInstance( 'Echo' )->warning(
|
|
|
|
'Failed to unserialize event {id}',
|
|
|
|
[
|
|
|
|
'id' => $row->event_id
|
|
|
|
]
|
|
|
|
);
|
2016-05-13 23:39:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-05-08 18:11:44 +00:00
|
|
|
$this->pageId = $row->event_page_id;
|
2016-03-04 19:23:02 +00:00
|
|
|
$this->deleted = $row->event_deleted;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
if ( $row->event_agent_id ) {
|
2020-08-11 09:33:33 +00:00
|
|
|
$this->agent = User::newFromId( (int)$row->event_agent_id );
|
2012-04-27 15:14:24 +00:00
|
|
|
} elseif ( $row->event_agent_ip ) {
|
2020-08-11 09:33:33 +00:00
|
|
|
$this->agent = User::newFromName( (string)$row->event_agent_ip, false );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2014-08-16 00:24:11 +00:00
|
|
|
// Lazy load the title from getTitle() so that we can do a batch-load
|
|
|
|
if (
|
2020-02-09 10:58:49 +00:00
|
|
|
isset( $this->extra['page_title'] ) && isset( $this->extra['page_namespace'] )
|
2014-08-16 00:24:11 +00:00
|
|
|
&& !$row->event_page_id
|
|
|
|
) {
|
2013-05-09 18:50:05 +00:00
|
|
|
$this->title = Title::makeTitleSafe(
|
|
|
|
$this->extra['page_namespace'],
|
|
|
|
$this->extra['page_title']
|
|
|
|
);
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2014-08-16 03:10:09 +00:00
|
|
|
if ( $row->event_page_id ) {
|
|
|
|
$titleCache = EchoTitleLocalCache::create();
|
2020-08-11 09:33:33 +00:00
|
|
|
$titleCache->add( (int)$row->event_page_id );
|
2014-08-16 03:10:09 +00:00
|
|
|
}
|
2014-08-16 07:00:08 +00:00
|
|
|
if ( isset( $this->extra['revid'] ) && $this->extra['revid'] ) {
|
|
|
|
$revisionCache = EchoRevisionLocalCache::create();
|
|
|
|
$revisionCache->add( $this->extra['revid'] );
|
|
|
|
}
|
2016-05-13 23:39:17 +00:00
|
|
|
|
|
|
|
return true;
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-06 07:04:28 +00:00
|
|
|
* Loads data from the database into this object, given the event ID.
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param int $id Event ID
|
2021-05-13 00:04:54 +00:00
|
|
|
* @param bool $fromPrimary
|
2016-05-13 23:39:17 +00:00
|
|
|
* @return bool Whether it loaded successfully
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
2021-05-13 00:04:54 +00:00
|
|
|
public function loadFromID( $id, $fromPrimary = false ) {
|
2014-08-14 18:46:26 +00:00
|
|
|
$eventMapper = new EchoEventMapper();
|
2021-05-13 00:04:54 +00:00
|
|
|
$event = $eventMapper->fetchById( $id, $fromPrimary );
|
2016-05-13 23:39:17 +00:00
|
|
|
if ( !$event ) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
// Copy over the attribute
|
|
|
|
$this->id = $event->id;
|
|
|
|
$this->type = $event->type;
|
|
|
|
$this->variant = $event->variant;
|
|
|
|
$this->extra = $event->extra;
|
|
|
|
$this->pageId = $event->pageId;
|
|
|
|
$this->agent = $event->agent;
|
|
|
|
$this->title = $event->title;
|
2016-03-04 19:23:02 +00:00
|
|
|
$this->deleted = $event->deleted;
|
2014-07-18 03:58:21 +00:00
|
|
|
// Don't overwrite timestamp if it exists already
|
|
|
|
if ( !$this->timestamp ) {
|
|
|
|
$this->timestamp = $event->timestamp;
|
|
|
|
}
|
2016-05-13 23:39:17 +00:00
|
|
|
|
|
|
|
return true;
|
2012-06-06 07:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an EchoEvent from a row object
|
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param stdClass $row row object from echo_event
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return EchoEvent|false
|
2012-06-06 07:04:28 +00:00
|
|
|
*/
|
2012-08-30 16:04:39 +00:00
|
|
|
public static function newFromRow( $row ) {
|
2012-06-06 07:04:28 +00:00
|
|
|
$obj = new EchoEvent();
|
2016-05-13 23:39:17 +00:00
|
|
|
return $obj->loadFromRow( $row )
|
|
|
|
? $obj
|
|
|
|
: false;
|
2012-06-06 07:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an EchoEvent from the database by ID
|
|
|
|
*
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param int $id Event ID
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return EchoEvent|false
|
2012-06-06 07:04:28 +00:00
|
|
|
*/
|
|
|
|
public static function newFromID( $id ) {
|
|
|
|
$obj = new EchoEvent();
|
2016-05-13 23:39:17 +00:00
|
|
|
return $obj->loadFromID( $id )
|
|
|
|
? $obj
|
|
|
|
: false;
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-26 22:05:29 +00:00
|
|
|
/**
|
|
|
|
* Serialize the extra data for event
|
2019-12-21 05:45:14 +00:00
|
|
|
* @return string|null
|
2012-12-26 22:05:29 +00:00
|
|
|
*/
|
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 );
|
2020-01-14 05:09:42 +00:00
|
|
|
} elseif ( $this->extra === null ) {
|
2012-12-26 22:05:29 +00:00
|
|
|
$extra = null;
|
|
|
|
} else {
|
2016-12-05 18:51:07 +00:00
|
|
|
$extra = serialize( [ $this->extra ] );
|
2012-12-26 22:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $extra;
|
|
|
|
}
|
|
|
|
|
2013-05-08 18:11:44 +00:00
|
|
|
/**
|
|
|
|
* Determine if the current user is allowed to view a particular
|
|
|
|
* field of this revision, if it's marked as deleted. When no
|
|
|
|
* revision is attached always returns true.
|
|
|
|
*
|
2019-04-17 15:46:06 +00:00
|
|
|
* @param int $field One of RevisionRecord::DELETED_TEXT,
|
|
|
|
* RevisionRecord::DELETED_COMMENT,
|
|
|
|
* RevisionRecord::DELETED_USER
|
2017-08-09 15:20:55 +00:00
|
|
|
* @param User $user User object to check
|
|
|
|
* @return bool
|
2013-05-08 18:11:44 +00:00
|
|
|
*/
|
2015-11-24 00:08:17 +00:00
|
|
|
public function userCan( $field, User $user ) {
|
2013-05-08 18:11:44 +00:00
|
|
|
$revision = $this->getRevision();
|
2015-08-28 23:15:06 +00:00
|
|
|
// User is handled specially
|
2019-04-17 15:46:06 +00:00
|
|
|
if ( $field === RevisionRecord::DELETED_USER ) {
|
2015-08-28 23:15:06 +00:00
|
|
|
$agent = $this->getAgent();
|
|
|
|
if ( !$agent ) {
|
|
|
|
// No user associated, so they can see it.
|
|
|
|
return true;
|
2019-04-17 15:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
$revision
|
|
|
|
&& $agent->getName() === $revision->getUser( RevisionRecord::RAW )->getName()
|
2015-08-28 23:15:06 +00:00
|
|
|
) {
|
|
|
|
// If the agent and the revision user are the same, use rev_deleted
|
2019-04-17 15:46:06 +00:00
|
|
|
return $revision->audienceCan( $field, RevisionRecord::FOR_THIS_USER, $user );
|
2015-08-28 23:15:06 +00:00
|
|
|
} else {
|
|
|
|
// Use User::isHidden()
|
2020-03-05 19:55:54 +00:00
|
|
|
$permManager = MediaWikiServices::getInstance()->getPermissionManager();
|
2021-03-09 18:29:11 +00:00
|
|
|
return $permManager->userHasAnyRight( $user, 'viewsuppressed', 'hideuser' )
|
|
|
|
|| !$agent->isHidden();
|
2015-08-28 23:15:06 +00:00
|
|
|
}
|
|
|
|
} elseif ( $revision ) {
|
|
|
|
// A revision is set, use rev_deleted
|
2019-04-17 15:46:06 +00:00
|
|
|
return $revision->audienceCan( $field, RevisionRecord::FOR_THIS_USER, $user );
|
2013-05-08 18:11:44 +00:00
|
|
|
} else {
|
2015-08-28 23:15:06 +00:00
|
|
|
// Not a user, and there is no associated revision, so the user can see it
|
2013-05-08 18:11:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
## Accessors
|
2019-07-14 10:57:45 +00:00
|
|
|
|
2013-03-04 20:56:42 +00:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public function getId() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2013-03-04 20:56:42 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public function getTimestamp() {
|
|
|
|
return $this->timestamp;
|
|
|
|
}
|
|
|
|
|
2013-03-04 20:56:42 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public function getType() {
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2013-03-04 20:56:42 +00:00
|
|
|
/**
|
2015-08-19 19:18:27 +00:00
|
|
|
* @return string|null
|
2013-03-04 20:56:42 +00:00
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public function getVariant() {
|
|
|
|
return $this->variant;
|
|
|
|
}
|
|
|
|
|
2013-03-04 20:56:42 +00:00
|
|
|
/**
|
2018-08-13 07:33:57 +00:00
|
|
|
* @return array
|
2013-03-04 20:56:42 +00:00
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public function getExtra() {
|
|
|
|
return $this->extra;
|
|
|
|
}
|
|
|
|
|
2013-05-08 18:11:44 +00:00
|
|
|
public function getExtraParam( $key, $default = null ) {
|
2019-03-12 20:04:00 +00:00
|
|
|
return $this->extra[$key] ?? $default;
|
2013-05-08 18:11:44 +00:00
|
|
|
}
|
|
|
|
|
2013-03-04 20:56:42 +00:00
|
|
|
/**
|
2015-08-19 19:18:27 +00:00
|
|
|
* @return User|null
|
2013-03-04 20:56:42 +00:00
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public function getAgent() {
|
|
|
|
return $this->agent;
|
|
|
|
}
|
|
|
|
|
2018-10-26 23:46:16 +00:00
|
|
|
/**
|
|
|
|
* Check whether this event allows its agent to be notified.
|
|
|
|
*
|
|
|
|
* Notifying the agent is only allowed if the event's type allows it, or if the event extra
|
|
|
|
* explicity specifies 'notifyAgent' => true.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canNotifyAgent() {
|
|
|
|
global $wgEchoNotifications;
|
|
|
|
$allowedInConfig = $wgEchoNotifications[$this->getType()]['canNotifyAgent'] ?? false;
|
|
|
|
$allowedInExtra = $this->getExtraParam( 'notifyAgent', false );
|
|
|
|
return $allowedInConfig || $allowedInExtra;
|
|
|
|
}
|
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
/**
|
2021-05-13 00:04:54 +00:00
|
|
|
* @param bool $fromPrimary
|
2016-07-27 14:52:18 +00:00
|
|
|
* @return null|Title
|
2012-05-17 00:29:37 +00:00
|
|
|
*/
|
2021-05-13 00:04:54 +00:00
|
|
|
public function getTitle( $fromPrimary = false ) {
|
2013-05-08 18:11:44 +00:00
|
|
|
if ( $this->title ) {
|
|
|
|
return $this->title;
|
2021-11-03 14:47:50 +00:00
|
|
|
}
|
|
|
|
if ( $this->pageId ) {
|
2014-08-16 03:10:09 +00:00
|
|
|
$titleCache = EchoTitleLocalCache::create();
|
|
|
|
$title = $titleCache->get( $this->pageId );
|
|
|
|
if ( $title ) {
|
2018-02-05 19:56:54 +00:00
|
|
|
$this->title = $title;
|
|
|
|
return $this->title;
|
2014-08-16 03:10:09 +00:00
|
|
|
}
|
2021-05-13 00:04:54 +00:00
|
|
|
$this->title = Title::newFromID( $this->pageId, $fromPrimary ? Title::GAID_FOR_UPDATE : 0 );
|
2021-11-03 14:47:50 +00:00
|
|
|
if ( $this->title ) {
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( isset( $this->extra['page_title'] ) && isset( $this->extra['page_namespace'] ) ) {
|
2018-02-05 19:56:54 +00:00
|
|
|
$this->title = Title::makeTitleSafe(
|
2014-03-02 08:03:38 +00:00
|
|
|
$this->extra['page_namespace'],
|
|
|
|
$this->extra['page_title']
|
2013-05-08 18:11:44 +00:00
|
|
|
);
|
2018-02-05 19:56:54 +00:00
|
|
|
return $this->title;
|
2013-05-08 18:11:44 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-17 15:46:06 +00:00
|
|
|
* @return RevisionRecord|null
|
2013-05-08 18:11:44 +00:00
|
|
|
*/
|
|
|
|
public function getRevision() {
|
|
|
|
if ( $this->revision ) {
|
|
|
|
return $this->revision;
|
2019-04-17 15:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $this->extra['revid'] ) ) {
|
2014-08-16 07:00:08 +00:00
|
|
|
$revisionCache = EchoRevisionLocalCache::create();
|
|
|
|
$revision = $revisionCache->get( $this->extra['revid'] );
|
|
|
|
if ( $revision ) {
|
2018-02-05 19:56:54 +00:00
|
|
|
$this->revision = $revision;
|
|
|
|
return $this->revision;
|
2014-08-16 07:00:08 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2019-04-17 15:46:06 +00:00
|
|
|
$store = MediaWikiServices::getInstance()->getRevisionStore();
|
|
|
|
$this->revision = $store->getRevisionById( $this->extra['revid'] );
|
2018-02-05 19:56:54 +00:00
|
|
|
return $this->revision;
|
2013-05-08 18:11:44 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2013-05-08 18:11:44 +00:00
|
|
|
return null;
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2013-03-09 00:32:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the category of the event type
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCategory() {
|
2021-02-25 01:55:40 +00:00
|
|
|
$attributeManager = EchoServices::getInstance()->getAttributeManager();
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-22 21:33:22 +00:00
|
|
|
return $attributeManager->getNotificationCategory( $this->type );
|
2013-03-09 00:32:28 +00:00
|
|
|
}
|
2013-01-15 23:21:39 +00:00
|
|
|
|
2014-08-05 21:50:54 +00:00
|
|
|
/**
|
|
|
|
* Get the section of the event type
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSection() {
|
2021-02-25 01:55:40 +00:00
|
|
|
$attributeManager = EchoServices::getInstance()->getAttributeManager();
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-05 21:50:54 +00:00
|
|
|
return $attributeManager->getNotificationSection( $this->type );
|
|
|
|
}
|
|
|
|
|
2014-08-27 18:39:53 +00:00
|
|
|
/**
|
2015-07-02 03:36:47 +00:00
|
|
|
* Determine whether an event can use the job queue, or should be immediate
|
2017-07-26 19:34:44 +00:00
|
|
|
* @return bool
|
2014-08-27 18:39:53 +00:00
|
|
|
*/
|
|
|
|
public function getUseJobQueue() {
|
|
|
|
global $wgEchoNotifications;
|
2015-07-02 03:36:47 +00:00
|
|
|
if ( isset( $wgEchoNotifications[$this->type]['immediate'] ) ) {
|
|
|
|
return !(bool)$wgEchoNotifications[$this->type]['immediate'];
|
2014-08-27 18:39:53 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2015-07-02 03:36:47 +00:00
|
|
|
return true;
|
2014-08-27 18:39:53 +00:00
|
|
|
}
|
|
|
|
|
2013-11-30 06:20:03 +00:00
|
|
|
public function setType( $type ) {
|
|
|
|
$this->type = $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setVariant( $variant ) {
|
|
|
|
$this->variant = $variant;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAgent( User $agent ) {
|
|
|
|
$this->agent = $agent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTitle( Title $title ) {
|
|
|
|
$this->title = $title;
|
2016-03-04 19:23:02 +00:00
|
|
|
$pageId = $title->getArticleID();
|
2013-11-30 06:20:03 +00:00
|
|
|
if ( $pageId ) {
|
|
|
|
$this->pageId = $pageId;
|
|
|
|
} else {
|
2016-03-04 19:23:02 +00:00
|
|
|
$this->extra['page_title'] = $title->getDBkey();
|
2013-11-30 06:20:03 +00:00
|
|
|
$this->extra['page_namespace'] = $title->getNamespace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setExtra( $name, $value ) {
|
|
|
|
$this->extra[$name] = $value;
|
|
|
|
}
|
|
|
|
|
2013-06-12 23:18:26 +00:00
|
|
|
/**
|
|
|
|
* Get the message key of the primary or secondary link for a notification type.
|
|
|
|
*
|
2020-03-20 15:37:25 +00:00
|
|
|
* @param string $rank 'primary' or 'secondary'
|
|
|
|
* @return string i18n message key
|
2013-06-12 23:18:26 +00:00
|
|
|
*/
|
|
|
|
public function getLinkMessage( $rank ) {
|
|
|
|
global $wgEchoNotifications;
|
2014-08-16 06:41:39 +00:00
|
|
|
$type = $this->getType();
|
2019-05-07 16:52:01 +00:00
|
|
|
return $wgEchoNotifications[$type][$rank . '-link']['message'] ?? '';
|
2013-06-12 23:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the link destination of the primary or secondary link for a notification type.
|
|
|
|
*
|
2020-03-20 15:37:25 +00:00
|
|
|
* @param string $rank 'primary' or 'secondary'
|
|
|
|
* @return string The link destination, e.g. 'agent'
|
2013-06-12 23:18:26 +00:00
|
|
|
*/
|
|
|
|
public function getLinkDestination( $rank ) {
|
|
|
|
global $wgEchoNotifications;
|
2014-08-16 06:41:39 +00:00
|
|
|
$type = $this->getType();
|
2019-05-07 16:52:01 +00:00
|
|
|
return $wgEchoNotifications[$type][$rank . '-link']['destination'] ?? '';
|
2013-06-12 23:18:26 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
/**
|
2019-12-21 05:45:14 +00:00
|
|
|
* @return string|null
|
2013-01-15 23:21:39 +00:00
|
|
|
*/
|
|
|
|
public function getBundleHash() {
|
|
|
|
return $this->bundleHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-21 05:45:14 +00:00
|
|
|
* @param string|null $hash
|
2013-01-15 23:21:39 +00:00
|
|
|
*/
|
|
|
|
public function setBundleHash( $hash ) {
|
|
|
|
$this->bundleHash = $hash;
|
|
|
|
}
|
2016-03-04 19:23:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isDeleted() {
|
|
|
|
return $this->deleted === 1;
|
|
|
|
}
|
|
|
|
|
2019-10-23 10:44:35 +00:00
|
|
|
public function setBundledEvents( array $events ) {
|
2016-03-04 19:23:02 +00:00
|
|
|
$this->bundledEvents = $events;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBundledEvents() {
|
|
|
|
return $this->bundledEvents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
|
|
|
public function canBeBundled() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
|
|
|
public function getBundlingKey() {
|
|
|
|
return $this->getBundleHash();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
2019-10-23 10:44:35 +00:00
|
|
|
public function setBundledElements( array $bundleables ) {
|
2016-03-04 19:23:02 +00:00
|
|
|
$this->setBundledEvents( $bundleables );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 15:20:55 +00:00
|
|
|
* @inheritDoc
|
2016-03-04 19:23:02 +00:00
|
|
|
*/
|
|
|
|
public function getSortingKey() {
|
|
|
|
return $this->getTimestamp();
|
|
|
|
}
|
2019-03-02 20:25:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the list of fields that should be selected to create
|
|
|
|
* a new event with EchoEvent::newFromRow
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public static function selectFields() {
|
|
|
|
return [
|
|
|
|
'event_id',
|
|
|
|
'event_type',
|
|
|
|
'event_variant',
|
|
|
|
'event_agent_id',
|
|
|
|
'event_agent_ip',
|
|
|
|
'event_extra',
|
|
|
|
'event_page_id',
|
|
|
|
'event_deleted',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
}
|