| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Mapper; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 7 |
use MediaWiki\User\UserIdentity; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class EventMapper extends AbstractMapper { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public function insert( Event $event ) { |
| 22 |
$dbw = $this->dbFactory->getEchoDb( DB_PRIMARY ); |
| 23 |
|
| 24 |
$row = $event->toDbArray(); |
| 25 |
|
| 26 |
$dbw->newInsertQueryBuilder() |
| 27 |
->insertInto( 'echo_event' ) |
| 28 |
->row( $row ) |
| 29 |
->caller( __METHOD__ ) |
| 30 |
->execute(); |
| 31 |
|
| 32 |
$id = $dbw->insertId(); |
| 33 |
|
| 34 |
$listeners = $this->getMethodListeners( __FUNCTION__ ); |
| 35 |
foreach ( $listeners as $listener ) { |
| 36 |
$dbw->onTransactionCommitOrIdle( $listener, __METHOD__ ); |
| 37 |
} |
| 38 |
|
| 39 |
return $id; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function fetchById( $id, $fromPrimary = false ) { |
| 50 |
$db = $fromPrimary ? $this->dbFactory->getEchoDb( DB_PRIMARY ) : $this->dbFactory->getEchoDb( DB_REPLICA ); |
| 51 |
|
| 52 |
$row = $db->newSelectQueryBuilder() |
| 53 |
->select( Event::selectFields() ) |
| 54 |
->from( 'echo_event' ) |
| 55 |
->where( [ 'event_id' => $id ] ) |
| 56 |
->caller( __METHOD__ ) |
| 57 |
->fetchRow(); |
| 58 |
|
| 59 |
|
| 60 |
if ( !$row && !$fromPrimary && $this->dbFactory->canRetryPrimary() ) { |
| 61 |
return $this->fetchById( $id, true ); |
| 62 |
} elseif ( !$row ) { |
| 63 |
throw new InvalidArgumentException( "No Event found with ID: $id" ); |
| 64 |
} |
| 65 |
|
| 66 |
return Event::newFromRow( $row ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function toggleDeleted( array $eventIds, $deleted ) { |
| 74 |
$dbw = $this->dbFactory->getEchoDb( DB_PRIMARY ); |
| 75 |
|
| 76 |
$selectDeleted = $deleted ? 0 : 1; |
| 77 |
$setDeleted = $deleted ? 1 : 0; |
| 78 |
$dbw->newUpdateQueryBuilder() |
| 79 |
->update( 'echo_event' ) |
| 80 |
->set( [ |
| 81 |
'event_deleted' => $setDeleted, |
| 82 |
] ) |
| 83 |
->where( [ |
| 84 |
'event_deleted' => $selectDeleted, |
| 85 |
'event_id' => $eventIds, |
| 86 |
] ) |
| 87 |
->caller( __METHOD__ ) |
| 88 |
->execute(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function fetchByPage( $pageId ) { |
| 98 |
$events = []; |
| 99 |
$seenEventIds = []; |
| 100 |
$dbr = $this->dbFactory->getEchoDb( DB_REPLICA ); |
| 101 |
|
| 102 |
|
| 103 |
$res = $dbr->newSelectQueryBuilder() |
| 104 |
->select( Event::selectFields() ) |
| 105 |
->from( 'echo_event' ) |
| 106 |
->where( [ 'event_page_id' => $pageId ] ) |
| 107 |
->caller( __METHOD__ ) |
| 108 |
->fetchResultSet(); |
| 109 |
foreach ( $res as $row ) { |
| 110 |
$event = Event::newFromRow( $row ); |
| 111 |
$events[] = $event; |
| 112 |
$seenEventIds[] = $event->getId(); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
$conds = [ 'etp_page' => $pageId ]; |
| 117 |
if ( $seenEventIds ) { |
| 118 |
|
| 119 |
|
| 120 |
$conds[] = $dbr->expr( 'event_id', '!=', $seenEventIds ); |
| 121 |
} |
| 122 |
$res = $dbr->newSelectQueryBuilder() |
| 123 |
->select( Event::selectFields() ) |
| 124 |
->distinct() |
| 125 |
->from( 'echo_event' ) |
| 126 |
->join( 'echo_target_page', null, 'event_id=etp_event' ) |
| 127 |
->where( $conds ) |
| 128 |
->caller( __METHOD__ ) |
| 129 |
->fetchResultSet(); |
| 130 |
foreach ( $res as $row ) { |
| 131 |
$events[] = Event::newFromRow( $row ); |
| 132 |
} |
| 133 |
|
| 134 |
return $events; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function fetchIdsByPage( $pageId ) { |
| 144 |
$events = $this->fetchByPage( $pageId ); |
| 145 |
$eventIds = array_map( |
| 146 |
static function ( Event $event ) { |
| 147 |
return $event->getId(); |
| 148 |
}, |
| 149 |
$events |
| 150 |
); |
| 151 |
return $eventIds; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function fetchUnreadByUserAndPage( UserIdentity $user, $pageId ) { |
| 162 |
$dbr = $this->dbFactory->getEchoDb( DB_REPLICA ); |
| 163 |
$fields = array_merge( Event::selectFields(), [ 'notification_timestamp' ] ); |
| 164 |
|
| 165 |
$res = $dbr->newSelectQueryBuilder() |
| 166 |
->select( $fields ) |
| 167 |
->from( 'echo_event' ) |
| 168 |
->join( 'echo_notification', null, 'notification_event=event_id' ) |
| 169 |
->join( 'echo_target_page', null, 'etp_event=event_id' ) |
| 170 |
->where( [ |
| 171 |
'event_deleted' => 0, |
| 172 |
'notification_user' => $user->getId(), |
| 173 |
'notification_read_timestamp' => null, |
| 174 |
'etp_page' => $pageId, |
| 175 |
] ) |
| 176 |
->caller( __METHOD__ ) |
| 177 |
->fetchResultSet(); |
| 178 |
|
| 179 |
$data = []; |
| 180 |
foreach ( $res as $row ) { |
| 181 |
$data[] = Event::newFromRow( $row ); |
| 182 |
} |
| 183 |
|
| 184 |
return $data; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public function deleteOrphanedEvents( array $eventIds, $ignoreUserId = null, $ignoreUserTable = null ) { |
| 204 |
$dbw = $this->dbFactory->getEchoDb( DB_PRIMARY ); |
| 205 |
$dbr = $this->dbFactory->getEchoDb( DB_REPLICA ); |
| 206 |
|
| 207 |
$notifJoinConds = []; |
| 208 |
$emailJoinConds = []; |
| 209 |
if ( $ignoreUserId !== null ) { |
| 210 |
if ( $ignoreUserTable === null || $ignoreUserTable === 'echo_notification' ) { |
| 211 |
$notifJoinConds[] = $dbr->expr( 'notification_user', '!=', $ignoreUserId ); |
| 212 |
} |
| 213 |
if ( $ignoreUserTable === null || $ignoreUserTable === 'echo_email_batch' ) { |
| 214 |
$emailJoinConds[] = $dbr->expr( 'eeb_user_id', '!=', $ignoreUserId ); |
| 215 |
} |
| 216 |
} |
| 217 |
$orphanedEventIds = $dbr->newSelectQueryBuilder() |
| 218 |
->select( 'event_id' ) |
| 219 |
->from( 'echo_event' ) |
| 220 |
->leftJoin( 'echo_notification', null, array_merge( |
| 221 |
[ 'notification_event=event_id' ], |
| 222 |
$notifJoinConds |
| 223 |
) ) |
| 224 |
->leftJoin( 'echo_email_batch', null, array_merge( |
| 225 |
[ 'eeb_event_id=event_id' ], |
| 226 |
$emailJoinConds |
| 227 |
) ) |
| 228 |
->where( [ |
| 229 |
'event_id' => $eventIds, |
| 230 |
'notification_timestamp' => null, |
| 231 |
'eeb_user_id' => null |
| 232 |
] ) |
| 233 |
->caller( __METHOD__ ) |
| 234 |
->fetchFieldValues(); |
| 235 |
if ( $orphanedEventIds ) { |
| 236 |
$dbw->newDeleteQueryBuilder() |
| 237 |
->deleteFrom( 'echo_event' ) |
| 238 |
->where( [ 'event_id' => $orphanedEventIds ] ) |
| 239 |
->caller( __METHOD__ ) |
| 240 |
->execute(); |
| 241 |
$dbw->newDeleteQueryBuilder() |
| 242 |
->deleteFrom( 'echo_target_page' ) |
| 243 |
->where( [ 'etp_event' => $orphanedEventIds ] ) |
| 244 |
->caller( __METHOD__ ) |
| 245 |
->execute(); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
} |