Repository navigation
Expand file tree
/
Copy pathUserWatchesQuery.php
More file actions
184 lines (159 loc) · 4.47 KB
/
Copy pathUserWatchesQuery.php
File metadata and controls
184 lines (159 loc) · 4.47 KB
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
<?php
class UserWatchesQuery extends WatchesQuery {
public $sqlUserName = 'u.user_name AS user_name';
public $sqlNumWatches = 'COUNT(*) AS num_watches';
public $sqlNumPending = 'SUM( IF(w.wl_notificationtimestamp IS NULL, 0, 1) ) AS num_pending';
public $sqlPercentPending = 'SUM( IF(w.wl_notificationtimestamp IS NULL, 0, 1) ) * 100 / COUNT(*) AS percent_pending';
public $sqlEngagementScore =
'ROUND( IFNULL(
EXP(
-0.01 * SUM(
IF(w.wl_notificationtimestamp IS NULL, 0, 1)
)
)
*
EXP(
-0.01 * FLOOR(
AVG(
TIMESTAMPDIFF( DAY, w.wl_notificationtimestamp, UTC_TIMESTAMP() )
)
)
),
1), 3) AS engagement_score';
protected $fieldNames = [
'user_name' => 'watchanalytics-special-header-user',
'num_watches' => 'watchanalytics-special-header-watches',
'num_pending' => 'watchanalytics-special-header-pending-watches',
'percent_pending' => 'watchanalytics-special-header-pending-percent',
'max_pending_minutes' => 'watchanalytics-special-header-pending-maxtime',
'avg_pending_minutes' => 'watchanalytics-special-header-pending-averagetime',
'engagement_score' => 'watchanalytics-special-header-engagement-score',
];
public function getQueryInfo( $conds = null ) {
$this->tables = [
'w' => 'watchlist',
'u' => 'user',
'p' => 'page',
'log' => 'logging',
];
$this->fields = [
$this->sqlUserName,
$this->sqlNumWatches,
$this->sqlNumPending,
$this->sqlPercentPending,
$this->sqlMaxPendingMins,
$this->sqlAvgPendingMins,
$this->sqlEngagementScore,
];
$this->conds = $conds ? $conds : [];
$this->join_conds = [
'u' => [
'LEFT JOIN', 'u.user_id=w.wl_user'
],
'p' => [
'LEFT JOIN', 'p.page_namespace=w.wl_namespace AND p.page_title=w.wl_title'
],
'log' => [
'LEFT JOIN',
'log.log_namespace = w.wl_namespace '
. ' AND log.log_title = w.wl_title'
. ' AND p.page_namespace IS NULL'
. ' AND p.page_title IS NULL'
. ' AND log.log_action = "delete"'
],
];
// optionally join the 'user_groups' table to filter by user group
if ( $this->userGroupFilter ) {
$this->tables['ug'] = 'user_groups';
$this->join_conds['ug'] = [
'RIGHT JOIN', "w.wl_user = ug.ug_user AND ug.ug_group = \"{$this->userGroupFilter}\""
];
$noNullUsers = 'w.wl_user IS NOT NULL';
if ( is_array( $this->conds ) ) {
$this->conds[] = $noNullUsers;
} elseif ( is_string( $this->conds ) ) {
$this->conds .= ' AND ' . $noNullUsers;
}
}
// optionally join the 'categorylinks' table to filter by page category
if ( $this->categoryFilter ) {
$this->setCategoryFilterQueryInfo();
}
$this->options = [
'GROUP BY' => 'w.wl_user'
];
return parent::getQueryInfo();
}
/**
* Gets watch statistics for a particular user.
*
* @param User $user the user to get watch-info on.
*
* @return array returns user watch info in an array with keys the same as
* $this->fieldNames.
*/
public function getUserWatchStats( User $user ) {
$qInfo = $this->getQueryInfo();
$dbr = wfGetDB( DB_REPLICA );
$res = $dbr->select(
$qInfo['tables'],
$qInfo['fields'],
'w.wl_user=' . $user->getId(),
__METHOD__,
$qInfo['options'],
$qInfo['join_conds']
);
$row = $dbr->fetchRow( $res );
// if user doesn't have any pages in watchlist, then no data will be
// returned by this query. Create a "blank" row instead.
if ( $row === false ) {
$row = [];
foreach ( $this->fieldNames as $name => $msg ) {
$row[ $name ] = 0;
}
$row[ 'user_name' ] = $user->getName();
}
return $row;
}
/**
* Gets watch statistics for a list of users.
*
* @param Array $userIds array of integer user IDs.
* @return Array returns user watch info in an array with user IDs as keys
* and values being objects with params num_watches and num_pending.
*/
public function getMultiUserWatchStats( array $userIds ) {
if ( ! count( $userIds ) ) {
return [];
}
$fields = [
'w.wl_user',
$this->sqlNumWatches,
$this->sqlNumPending,
];
$dbr = wfGetDB( DB_REPLICA );
$res = $dbr->select(
[
'w' => 'watchlist'
],
$fields,
[
'w.wl_user' => $userIds
],
__METHOD__,
[
'GROUP BY' => 'w.wl_user'
], // no options
null // no joins
);
$return = [];
while ( $row = $res->fetchObject() ) {
$return[] = (object)[
'wl_user' => $row->wl_user,
'num_watches' => $row->num_watches,
'num_pending' => $row->num_pending,
];
}
return $return;
}
}