mediawiki-extensions-Echo/includes/DeferredMarkAsDeletedUpdate.php
Stephane Bisson 6e2236db81 Moderate notifications
* When a page is deleted, moderate associated
  notifications. When it is undeleted, unmoderate.

* When a notification cannot be rendered
  (canRender() returns false), moderate it.

The biggest advantage of moderation over mark-read-and-ignore
is that those notifications are filtered out at the database
level from this point on. They are not re-processed every single
time and do not affect the number of notifications returned by
the API.

Bug: T140327
Bug: T140836
Bug: T141463
Change-Id: Idefe78408fd584c13aaa9174cee3055539d92848
2016-08-19 22:20:04 +00:00

73 lines
1.7 KiB
PHP

<?php
use MediaWiki\Logger\LoggerFactory;
/**
* Mark event notifications as deleted at the end of a request. Used to queue up
* individual events to mark due to formatting failures.
*/
class EchoDeferredMarkAsDeletedUpdate implements DeferrableUpdate {
/**
* @var array
*/
protected $events = array();
/**
* @param EchoEvent $event
*/
public static function add( EchoEvent $event ) {
static $update;
if ( $update === null ) {
$update = new self();
DeferredUpdates::addUpdate( $update );
}
$update->addInternal( $event );
}
/**
* @param EchoEvent $event
*/
private function addInternal( EchoEvent $event ) {
$this->events[] = $event;
}
private function filterEventsWithTitleDbLag() {
return array_filter(
$this->events,
function ( EchoEvent $event ) {
if ( !$event->getTitle() && $event->getTitle( true ) ) {
// It is very likely this event was found
// unreaderable because of slave lag.
// Do not moderate it at this time.
LoggerFactory::getInstance( 'Echo' )->debug(
'EchoDeferredMarkAsDeletedUpdate: Event {eventId} was found unrenderable but its associated title exists on Master. Skipping.',
array(
'eventId' => $event->getId(),
'title' => $event->getTitle()->getPrefixedText(),
)
);
return false;
}
return true;
}
);
}
/**
* Marks all queued notifications as read.
* Satisfies DeferrableUpdate interface
*/
public function doUpdate() {
$events = $this->filterEventsWithTitleDbLag();
$eventIds = array_map(
function ( EchoEvent $event ) {
return $event->getId();
},
$events
);
EchoModerationController::moderate( $eventIds, true );
$this->events = array();
}
}