| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Model; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Extension\Notifications\Bundleable; |
| 7 |
use MediaWiki\Extension\Notifications\Hooks\HookRunner; |
| 8 |
use MediaWiki\Extension\Notifications\Mapper\NotificationMapper; |
| 9 |
use MediaWiki\Extension\Notifications\Notifier; |
| 10 |
use MediaWiki\Extension\Notifications\NotifUser; |
| 11 |
use MediaWiki\MediaWikiServices; |
| 12 |
use MediaWiki\User\User; |
| 13 |
use stdClass; |
| 14 |
|
| 15 |
class Notification extends AbstractEntity implements Bundleable { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
protected $user; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
protected $event; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
protected $targetPages; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
protected $timestamp; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected $readTimestamp; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected $bundleHash = ''; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
protected $bundledNotifications; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
protected function __construct() { |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static function create( array $info ) { |
| 70 |
$obj = new Notification(); |
| 71 |
static $validFields = [ 'event', 'user' ]; |
| 72 |
|
| 73 |
foreach ( $validFields as $field ) { |
| 74 |
if ( isset( $info[$field] ) ) { |
| 75 |
$obj->$field = $info[$field]; |
| 76 |
} else { |
| 77 |
throw new InvalidArgumentException( "Field $field is required" ); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if ( !$obj->user instanceof User ) { |
| 82 |
throw new InvalidArgumentException( 'Invalid user parameter, expected: User object' ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( !$obj->event instanceof Event ) { |
| 86 |
throw new InvalidArgumentException( 'Invalid event parameter, expected: Event object' ); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
$obj->timestamp = $obj->event->getTimestamp(); |
| 91 |
|
| 92 |
if ( !$obj->timestamp ) { |
| 93 |
$obj->timestamp = wfTimestampNow(); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
$obj->insert(); |
| 98 |
|
| 99 |
return $obj; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
protected function insert() { |
| 106 |
global $wgEchoNotifications; |
| 107 |
|
| 108 |
$notifMapper = new NotificationMapper(); |
| 109 |
|
| 110 |
$services = MediaWikiServices::getInstance(); |
| 111 |
$hookRunner = new HookRunner( $services->getHookContainer() ); |
| 112 |
|
| 113 |
$bundleKey = ''; |
| 114 |
if ( !empty( $wgEchoNotifications[$this->event->getType()]['bundle']['web'] ) ) { |
| 115 |
Notifier::getBundleRules( $this->event, $bundleKey ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( $bundleKey ) { |
| 119 |
$hash = md5( $bundleKey ); |
| 120 |
$this->bundleHash = $hash; |
| 121 |
} |
| 122 |
|
| 123 |
$notifUser = NotifUser::newFromUser( $this->user ); |
| 124 |
|
| 125 |
|
| 126 |
$notifMapper->attachListener( 'insert', 'refresh-notif-count', |
| 127 |
static function () use ( $notifUser ) { |
| 128 |
$notifUser->resetNotificationCount(); |
| 129 |
} |
| 130 |
); |
| 131 |
|
| 132 |
$notifMapper->insert( $this ); |
| 133 |
|
| 134 |
if ( $this->event->getCategory() === 'edit-user-talk' ) { |
| 135 |
$services->getTalkPageNotificationManager() |
| 136 |
->setUserHasNewMessages( $this->user ); |
| 137 |
} |
| 138 |
$hookRunner->onEchoCreateNotificationComplete( $this ); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public static function newFromRow( $row, ?array $targetPages = null ) { |
| 148 |
$notification = new Notification(); |
| 149 |
|
| 150 |
if ( property_exists( $row, 'event_type' ) ) { |
| 151 |
$notification->event = Event::newFromRow( $row ); |
| 152 |
} else { |
| 153 |
$notification->event = Event::newFromID( $row->notification_event ); |
| 154 |
} |
| 155 |
|
| 156 |
if ( $notification->event === false ) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
$notification->targetPages = $targetPages; |
| 161 |
$notification->user = User::newFromId( $row->notification_user ); |
| 162 |
|
| 163 |
$notification->timestamp = wfTimestamp( TS_MW, $row->notification_timestamp ); |
| 164 |
$notification->readTimestamp = wfTimestampOrNull( TS_MW, $row->notification_read_timestamp ); |
| 165 |
$notification->bundleHash = $row->notification_bundle_hash; |
| 166 |
|
| 167 |
return $notification; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function toDbArray() { |
| 175 |
return [ |
| 176 |
'notification_event' => $this->event->getId(), |
| 177 |
'notification_user' => $this->user->getId(), |
| 178 |
'notification_timestamp' => $this->timestamp, |
| 179 |
'notification_read_timestamp' => $this->readTimestamp, |
| 180 |
'notification_bundle_hash' => $this->bundleHash, |
| 181 |
]; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function getEvent() { |
| 189 |
return $this->event; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function getUser() { |
| 197 |
return $this->user; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public function getTimestamp() { |
| 205 |
return $this->timestamp; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public function getReadTimestamp() { |
| 213 |
return $this->readTimestamp; |
| 214 |
} |
| 215 |
|
| 216 |
public function isRead() { |
| 217 |
return $this->getReadTimestamp() !== null; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public function getBundleHash() { |
| 225 |
return $this->bundleHash; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public function getTargetPages() { |
| 235 |
return $this->targetPages; |
| 236 |
} |
| 237 |
|
| 238 |
public function setBundledNotifications( array $notifications ) { |
| 239 |
$this->bundledNotifications = $notifications; |
| 240 |
} |
| 241 |
|
| 242 |
public function getBundledNotifications() { |
| 243 |
return $this->bundledNotifications; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public function canBeBundled() { |
| 250 |
return !$this->isRead(); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public function getBundlingKey() { |
| 257 |
return $this->getBundleHash(); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public function setBundledElements( array $bundleables ) { |
| 264 |
$this->setBundledNotifications( $bundleables ); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function getSortingKey() { |
| 271 |
return ( $this->isRead() ? '0' : '1' ) . '_' . $this->getTimestamp(); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public static function selectFields() { |
| 280 |
return array_merge( Event::selectFields(), [ |
| 281 |
'notification_event', |
| 282 |
'notification_user', |
| 283 |
'notification_timestamp', |
| 284 |
'notification_read_timestamp', |
| 285 |
'notification_bundle_hash', |
| 286 |
] ); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
class_alias( Notification::class, 'EchoNotification' ); |