| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications; |
| 4 |
|
| 5 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 6 |
use MediaWiki\User\UserGroupManager; |
| 7 |
use MediaWiki\User\UserIdentity; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class AttributeManager { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
private $userGroupManager; |
| 18 |
|
| 19 |
|
| 20 |
private $userOptionsLookup; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
protected $notifications; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
protected $categories; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected $defaultNotifyTypeAvailability; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
protected $notifyTypeAvailabilityByCategory; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public const ALERT = 'alert'; |
| 46 |
public const MESSAGE = 'message'; |
| 47 |
public const ALL = 'all'; |
| 48 |
|
| 49 |
|
| 50 |
protected const DEFAULT_SECTION = self::ALERT; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static $sections = [ |
| 57 |
self::ALERT, |
| 58 |
self::MESSAGE |
| 59 |
]; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public const ATTR_LOCATORS = 'user-locators'; |
| 65 |
public const ATTR_FILTERS = 'user-filters'; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function __construct( |
| 79 |
array $notifications, |
| 80 |
array $categories, |
| 81 |
array $defaultNotifyTypeAvailability, |
| 82 |
array $notifyTypeAvailabilityByCategory, |
| 83 |
UserGroupManager $userGroupManager, |
| 84 |
UserOptionsLookup $userOptionsLookup |
| 85 |
) { |
| 86 |
|
| 87 |
$this->notifications = $notifications; |
| 88 |
$this->categories = $categories; |
| 89 |
|
| 90 |
$this->defaultNotifyTypeAvailability = $defaultNotifyTypeAvailability; |
| 91 |
$this->notifyTypeAvailabilityByCategory = $notifyTypeAvailabilityByCategory; |
| 92 |
$this->userGroupManager = $userGroupManager; |
| 93 |
$this->userOptionsLookup = $userOptionsLookup; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function getUserCallable( $type, $locator = self::ATTR_LOCATORS ) { |
| 104 |
if ( isset( $this->notifications[$type][$locator] ) ) { |
| 105 |
return (array)$this->notifications[$type][$locator]; |
| 106 |
} |
| 107 |
|
| 108 |
return []; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function getUserEnabledEvents( UserIdentity $userIdentity, $notifierTypes ) { |
| 120 |
if ( is_string( $notifierTypes ) ) { |
| 121 |
$notifierTypes = [ $notifierTypes ]; |
| 122 |
} |
| 123 |
return array_values( array_filter( |
| 124 |
array_keys( $this->notifications ), |
| 125 |
function ( $eventType ) use ( $userIdentity, $notifierTypes ) { |
| 126 |
$category = $this->getNotificationCategory( $eventType ); |
| 127 |
return $this->getCategoryEligibility( $userIdentity, $category ) && |
| 128 |
array_reduce( $notifierTypes, function ( $prev, $type ) use ( $userIdentity, $category ) { |
| 129 |
return $prev || |
| 130 |
( |
| 131 |
$this->isNotifyTypeAvailableForCategory( $category, $type ) && |
| 132 |
$this->userOptionsLookup->getOption( |
| 133 |
$userIdentity, |
| 134 |
"echo-subscriptions-$type-$category" |
| 135 |
) |
| 136 |
); |
| 137 |
}, false ); |
| 138 |
} |
| 139 |
) ); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function getUserEnabledEventsBySections( |
| 151 |
UserIdentity $userIdentity, |
| 152 |
$notifierTypes, |
| 153 |
array $sections |
| 154 |
) { |
| 155 |
$events = []; |
| 156 |
foreach ( $sections as $section ) { |
| 157 |
$events = array_merge( |
| 158 |
$events, |
| 159 |
$this->getEventsForSection( $section ) |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
return array_intersect( |
| 164 |
$this->getUserEnabledEvents( $userIdentity, $notifierTypes ), |
| 165 |
$events |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public function getEventsForSection( $section ) { |
| 177 |
$events = []; |
| 178 |
|
| 179 |
$isDefault = ( $section === self::DEFAULT_SECTION ); |
| 180 |
|
| 181 |
foreach ( $this->notifications as $event => $attribs ) { |
| 182 |
if ( |
| 183 |
( |
| 184 |
isset( $attribs['section'] ) && |
| 185 |
$attribs['section'] === $section |
| 186 |
) || |
| 187 |
( |
| 188 |
$isDefault && |
| 189 |
( |
| 190 |
!isset( $attribs['section'] ) || |
| 191 |
|
| 192 |
|
| 193 |
!in_array( $attribs['section'], self::$sections ) |
| 194 |
) |
| 195 |
) |
| 196 |
|
| 197 |
) { |
| 198 |
$events[] = $event; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
return $events; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function getInternalCategoryNames() { |
| 211 |
return array_keys( $this->categories ); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function getCategoryEligibility( UserIdentity $userIdentity, $category ) { |
| 223 |
$usersGroups = $this->userGroupManager->getUserGroups( $userIdentity ); |
| 224 |
if ( isset( $this->categories[$category]['usergroups'] ) ) { |
| 225 |
$allowedGroups = $this->categories[$category]['usergroups']; |
| 226 |
if ( !array_intersect( $usersGroups, $allowedGroups ) ) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function getNotificationPriority( $notificationType ) { |
| 241 |
$category = $this->getNotificationCategory( $notificationType ); |
| 242 |
|
| 243 |
return $this->getCategoryPriority( $category ); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public function getCategoryPriority( $category ) { |
| 253 |
if ( isset( $this->categories[$category]['priority'] ) ) { |
| 254 |
$priority = $this->categories[$category]['priority']; |
| 255 |
if ( $priority >= 1 && $priority <= 10 ) { |
| 256 |
return $priority; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
return 10; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function getNotificationCategory( $notificationType ) { |
| 271 |
if ( isset( $this->notifications[$notificationType]['category'] ) ) { |
| 272 |
$category = $this->notifications[$notificationType]['category']; |
| 273 |
if ( isset( $this->categories[$category] ) ) { |
| 274 |
return $category; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
return 'other'; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public function getEventsByCategory(): array { |
| 288 |
$eventsByCategory = []; |
| 289 |
|
| 290 |
foreach ( $this->categories as $category => $categoryDetails ) { |
| 291 |
$eventsByCategory[$category] = []; |
| 292 |
} |
| 293 |
|
| 294 |
foreach ( $this->notifications as $notificationType => $notificationDetails ) { |
| 295 |
$category = $notificationDetails['category']; |
| 296 |
if ( isset( $eventsByCategory[$category] ) ) { |
| 297 |
|
| 298 |
|
| 299 |
$eventsByCategory[$category][] = $notificationType; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return $eventsByCategory; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public function getNotifyTypeAvailabilityForCategory( $category ) { |
| 316 |
return array_merge( |
| 317 |
$this->defaultNotifyTypeAvailability, |
| 318 |
$this->notifyTypeAvailabilityByCategory[$category] ?? [] |
| 319 |
); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public function isNotifyTypeAvailableForCategory( $category, $notifyType ) { |
| 334 |
return $this->getNotifyTypeAvailabilityForCategory( $category )[$notifyType]; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public function isCategoryDisplayedInPreferences( $category ) { |
| 344 |
return !( |
| 345 |
isset( $this->categories[$category]['no-dismiss'] ) && |
| 346 |
in_array( 'all', $this->categories[$category]['no-dismiss'] ) |
| 347 |
); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public function isNotifyTypeDismissableForCategory( $category, $notifyType ) { |
| 362 |
return !( |
| 363 |
isset( $this->categories[$category]['no-dismiss'] ) && |
| 364 |
( |
| 365 |
in_array( 'all', $this->categories[$category]['no-dismiss'] ) || |
| 366 |
in_array( $notifyType, $this->categories[$category]['no-dismiss'] ) |
| 367 |
) |
| 368 |
); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public function getNotificationSection( $notificationType ) { |
| 377 |
return $this->notifications[$notificationType]['section'] ?? self::DEFAULT_SECTION; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function getNotifyAgentEvents() { |
| 386 |
$events = []; |
| 387 |
foreach ( $this->notifications as $event => $attribs ) { |
| 388 |
if ( $attribs['canNotifyAgent'] ?? false ) { |
| 389 |
$events[] = $event; |
| 390 |
} |
| 391 |
} |
| 392 |
return $events; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
public function isBundleExpandable( $type ) { |
| 400 |
return $this->notifications[$type]['bundle']['expandable'] ?? false; |
| 401 |
} |
| 402 |
|
| 403 |
} |
| 404 |
|
| 405 |
class_alias( AttributeManager::class, 'EchoAttributeManager' ); |