1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
<?php
use Wikimedia\Rdbms\IResultWrapper;
/**
* Database mapper for EchoEvent model, which is an immutable class, there should
* not be any update to it
*/
class EchoEventMapper extends EchoAbstractMapper {
/**
* Insert an event record
*
* @param EchoEvent $event
* @return int|false
*/
public function insert( EchoEvent $event ) {
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
$row = $event->toDbArray();
$dbw->insert( 'echo_event', $row, __METHOD__ );
$id = $dbw->insertId();
$listeners = $this->getMethodListeners( __FUNCTION__ );
foreach ( $listeners as $listener ) {
$dbw->onTransactionCommitOrIdle( $listener, __METHOD__ );
}
return $id;
}
/**
* Create an EchoEvent by id
*
* @param int $id
* @param bool $fromMaster
* @return EchoEvent|false False if it wouldn't load/unserialize
* @throws MWException
*/
public function fetchById( $id, $fromMaster = false ) {
$db = $fromMaster ? $this->dbFactory->getEchoDb( DB_MASTER ) : $this->dbFactory->getEchoDb( DB_REPLICA );
$row = $db->selectRow( 'echo_event', EchoEvent::selectFields(), [ 'event_id' => $id ], __METHOD__ );
// If the row was not found, fall back on the master if it makes sense to do so
if ( !$row && !$fromMaster && $this->dbFactory->canRetryMaster() ) {
return $this->fetchById( $id, true );
} elseif ( !$row ) {
throw new MWException( "No EchoEvent found with ID: $id" );
}
return EchoEvent::newFromRow( $row );
}
/**
* @param int[] $eventIds
* @param bool $deleted
* @return bool|IResultWrapper
*/
public function toggleDeleted( array $eventIds, $deleted ) {
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
$selectDeleted = $deleted ? 0 : 1;
$setDeleted = $deleted ? 1 : 0;
$dbw->update(
'echo_event',
[
'event_deleted' => $setDeleted,
],
[
'event_deleted' => $selectDeleted,
'event_id' => $eventIds,
],
__METHOD__
);
return true;
}
/**
* Fetch events associated with a page
*
* @param int $pageId
* @return EchoEvent[] Events
*/
public function fetchByPage( $pageId ) {
$events = [];
$seenEventIds = [];
$dbr = $this->dbFactory->getEchoDb( DB_REPLICA );
// From echo_event
$res = $dbr->select(
[ 'echo_event' ],
EchoEvent::selectFields(),
[ 'event_page_id' => $pageId ],
__METHOD__
);
if ( $res ) {
foreach ( $res as $row ) {
$event = EchoEvent::newFromRow( $row );
$events[] = $event;
$seenEventIds[] = $event->getId();
}
}
// From echo_target_page
$conds = [ 'etp_page' => $pageId ];
if ( $seenEventIds ) {
// Some events have both a title and target page(s).
// Skip the events that were already found in the echo_event table (the query above).
$conds[] = 'event_id NOT IN ( ' . $dbr->makeList( $seenEventIds ) . ' )';
}
$res = $dbr->select(
[ 'echo_event', 'echo_target_page' ],
EchoEvent::selectFields(),
$conds,
__METHOD__,
[ 'GROUP BY' => 'etp_event' ],
[ 'echo_target_page' => [ 'INNER JOIN', 'event_id=etp_event' ] ]
);
if ( $res ) {
foreach ( $res as $row ) {
$events[] = EchoEvent::newFromRow( $row );
}
}
return $events;
}
/**
* Fetch event IDs associated with a page
*
* @param int $pageId
* @return int[] Event IDs
*/
public function fetchIdsByPage( $pageId ) {
$events = $this->fetchByPage( $pageId );
$eventIds = array_map(
function ( EchoEvent $event ) {
return $event->getId();
},
$events
);
return $eventIds;
}
/**
* Fetch events unread by a user and associated with a page
*
* @param User $user
* @param int $pageId
* @return EchoEvent[]
*/
public function fetchUnreadByUserAndPage( User $user, $pageId ) {
$dbr = $this->dbFactory->getEchoDb( DB_REPLICA );
$fields = array_merge( EchoEvent::selectFields(), [ 'notification_timestamp' ] );
$res = $dbr->select(
[ 'echo_event', 'echo_notification', 'echo_target_page' ],
$fields,
[
'event_deleted' => 0,
'notification_user' => $user->getId(),
'notification_read_timestamp' => null,
'etp_page' => $pageId,
],
__METHOD__,
[],
[
'echo_target_page' => [ 'INNER JOIN', 'etp_event=event_id' ],
'echo_notification' => [ 'INNER JOIN', [ 'notification_event=event_id' ] ],
]
);
$data = [];
foreach ( $res as $row ) {
$data[] = EchoEvent::newFromRow( $row );
}
return $data;
}
/**
* Find out which of the given event IDs are orphaned, and delete them.
*
* An event is orphaned if it is not referred to by any rows in the echo_notification or
* echo_email_batch tables. If $ignoreUserId is set, rows for that user are not considered when
* determining orphanhood; if $ignoreUserTable is set, this only applies to that table.
* Use this when you've just recently deleted rows related to this user on the master, so that
* this function won't refuse to delete recently-orphaned events because it still sees the
* recently-deleted rows on the replica.
*
* @param array $eventIds Event IDs to check to see if they have become orphaned
* @param int|null $ignoreUserId Allow events to be deleted if the only referring rows
* have this user ID
* @param string|null $ignoreUserTable Restrict $ignoreUserId to this table only
* ('echo_notification' or 'echo_email_batch')
*/
public function deleteOrphanedEvents( array $eventIds, $ignoreUserId = null, $ignoreUserTable = null ) {
$dbw = $this->dbFactory->getEchoDb( DB_MASTER );
$dbr = $this->dbFactory->getEchoDb( DB_REPLICA );
$notifJoinConds = [];
$emailJoinConds = [];
if ( $ignoreUserId !== null ) {
if ( $ignoreUserTable === null || $ignoreUserTable === 'echo_notification' ) {
$notifJoinConds[] = 'notification_user != ' . $dbr->addQuotes( $ignoreUserId );
}
if ( $ignoreUserTable === null || $ignoreUserTable === 'echo_email_batch' ) {
$emailJoinConds[] = 'eeb_user_id != ' . $dbr->addQuotes( $ignoreUserId );
}
}
$orphanedEventIds = $dbr->selectFieldValues(
[ 'echo_event', 'echo_notification', 'echo_email_batch' ],
'event_id',
[
'event_id' => $eventIds,
'notification_timestamp' => null,
'eeb_user_id' => null
],
__METHOD__,
[],
[
'echo_notification' => [ 'LEFT JOIN', array_merge( [
'notification_event=event_id'
], $notifJoinConds ) ],
'echo_email_batch' => [ 'LEFT JOIN', array_merge( [
'eeb_event_id=event_id'
], $emailJoinConds ) ]
]
);
if ( $orphanedEventIds ) {
$dbw->delete( 'echo_event', [ 'event_id' => $orphanedEventIds ], __METHOD__ );
$dbw->delete( 'echo_target_page', [ 'etp_event' => $orphanedEventIds ], __METHOD__ );
}
}
}
|