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
|
<?php
/**
* Handle user email batch ( daily/weekly ) for database storage
*/
class MWDbEchoEmailBatch extends MWEchoEmailBatch {
/**
* Set the last event of this batch, this is a cutoff point for clearing
* processed/invalid events
*
* @return bool true if event exists false otherwise
*/
protected function setLastEvent() {
$dbr = MWEchoDbFactory::getDB( DB_SLAVE );
$res = $dbr->selectField(
array( 'echo_email_batch' ),
array( 'MAX( eeb_event_id )' ),
array( 'eeb_user_id' => $this->mUser->getId() ),
__METHOD__
);
if ( $res ) {
$this->lastEvent = $res;
return true;
} else {
return false;
}
}
/**
* Get the events queued for the current user
* @return array
*/
protected function getEvents() {
global $wgEchoNotifications;
$events = array();
$validEvents = array_keys( $wgEchoNotifications );
// Per the tech discussion in the design meeting (03/22/2013), since this is
// processed by a cron job, it's okay to use GROUP BY over more complex
// composite index, favor insert performance, storage space over read
// performance in this case
if ( $validEvents ) {
$dbr = MWEchoDbFactory::getDB( DB_SLAVE );
$conds = array(
'eeb_user_id' => $this->mUser->getId(),
'event_id = eeb_event_id',
'event_type' => $validEvents
);
// See setLastEvent() for more detail for this variable
if ( $this->lastEvent ) {
$conds[] = 'eeb_event_id <= ' . intval( $this->lastEvent );
}
$res = $dbr->select(
array( 'echo_email_batch', 'echo_event' ),
array( '*' ),
$conds,
__METHOD__,
array(
'ORDER BY' => 'eeb_event_priority',
'LIMIT' => self::$displaySize + 1,
'GROUP BY' => 'eeb_event_hash'
)
);
foreach ( $res as $row ) {
// records in the queue inserted before email bundling code
// have no hash, in this case, we just ignore them
if ( $row->eeb_event_hash ) {
$events[$row->eeb_id] = $row;
}
}
}
return $events;
}
/**
* Clear "processed" events in the queue, processed could be: email sent, invalid, users do not want to receive emails
*/
public function clearProcessedEvent() {
$conds = array( 'eeb_user_id' => $this->mUser->getId() );
// there is a processed cutoff point
if ( $this->lastEvent ) {
$conds[] = 'eeb_event_id <= ' . intval( $this->lastEvent );
}
$dbw = MWEchoDbFactory::getDB( DB_MASTER );
$dbw->delete(
'echo_email_batch',
$conds,
__METHOD__,
array()
);
}
/**
* Insert notification event into email queue
* @param $userId int
* @param $eventId int
* @param $priority int
* @param $hash string
*/
public static function actuallyAddToQueue( $userId, $eventId, $priority, $hash ) {
if ( !$userId || !$eventId ) {
return;
}
$dbw = MWEchoDbFactory::getDB( DB_MASTER );
$row = array(
'eeb_user_id' => $userId,
'eeb_event_id' => $eventId,
'eeb_event_priority' => $priority,
'eeb_event_hash' => $hash
);
$id = $dbw->nextSequenceValue( 'echo_email_batch_eeb_id' );
if ( $id ) {
$row['eeb_id'] = $id;
}
$dbw->insert(
'echo_email_batch',
$row,
__METHOD__,
array( 'IGNORE' )
);
}
/**
* Get a list of users to be notified for the batch
*
* @param $startUserId int
* @param $batchSize int
*
* @return ResultWrapper|bool
*/
public static function actuallyGetUsersToNotify( $startUserId, $batchSize ) {
$dbr = MWEchoDbFactory::getDB( DB_SLAVE );
$res = $dbr->select(
array( 'echo_email_batch' ),
array( 'eeb_user_id' ),
array( 'eeb_user_id > ' . $startUserId ),
__METHOD__,
array( 'ORDER BY' => 'eeb_user_id', 'LIMIT' => $batchSize )
);
return $res;
}
}
|