/
Mongo.php
194 lines (169 loc) · 4.71 KB
/
Mongo.php
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
<?php
/**
* Copyright 2013-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2013-2014 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
/**
* Sentmail driver implementation for MongoDB databases.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013-2014 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Sentmail_Mongo extends IMP_Sentmail implements Horde_Mongo_Collection_Index
{
/* Field names. */
const ACTION = 'action';
const MESSAGEID = 'msgid';
const RECIPIENT = 'recip';
const SUCCESS = 'success';
const TS = 'ts';
const WHO = 'who';
/**
* Handle for the current database connection.
*
* @var MongoCollection
*/
protected $_db;
/**
* Indices list.
*
* @var array
*/
protected $_indices = array(
'index_ts' => array(
self::TS => 1
),
'index_who' => array(
self::WHO => 1
),
'index_success' => array(
self::SUCCESS => 1
)
);
/**
* @param array $params Parameters:
* - collection: (string) The name of the sentmail collection.
* - mongo_db: (Horde_Mongo_Client) [REQUIRED] The DB instance.
*/
public function __construct(array $params = array())
{
if (!isset($params['mongo_db'])) {
throw new InvalidArgumentException('Missing mongo_db parameter.');
}
parent::__construct(array_merge(array(
'collection' => 'imp_sentmail'
), $params));
$this->_db = $this->_params['mongo_db']->selectCollection(null, $this->_params['collection']);
}
/**
*/
protected function _log($action, $message_id, $recipient, $success)
{
try {
$this->_db->insert(array(
self::ACTION => $action,
self::MESSAGEID => $message_id,
self::RECIPIENT => $recipient,
self::SUCCESS => intval($success),
self::TS => time(),
self::WHO => $GLOBALS['registry']->getAuth()
));
} catch (MongoException $e) {}
}
/**
*/
public function favouriteRecipients($limit, $filter = null)
{
$query = array(
self::SUCCESS => 1,
self::WHO => $GLOBALS['registry']->getAuth()
);
if (!empty($filter)) {
$query[self::ACTION] = array('$in' => $filter);
}
$out = array();
try {
$res = $this->_db->aggregate(array(
/* Match the query. */
array('$match' => $query),
/* Group by recipient. */
array(
'$group' => array(
'_id' => '$' . self::RECIPIENT,
'count' => array(
'$sum' => 1
)
)
),
/* Sort by recipient. */
array(
'$sort' => array('count' => -1)
),
/* Limit the return. */
array(
'$limit' => $limit
)
));
if (isset($res['result'])) {
foreach ($res['result'] as $val) {
$out[] = $val['_id'];
}
}
} catch (MongoException $e) {}
return $out;
}
/**
*/
public function numberOfRecipients($hours, $user = false)
{
$query = array(
self::SUCCESS => 1,
self::TS => array(
'$gt' => (time() - ($hours * 3600))
)
);
if ($user) {
$query[self::WHO] = $GLOBALS['registry']->getAuth();
}
try {
return $this->_db->count($query);
} catch (MongoException $e) {
return 0;
}
}
/**
*/
protected function _deleteOldEntries($before)
{
try {
$this->_db->remove(array(
self::TS => array(
'$lt' => $before
)
));
} catch (MongoException $e) {}
}
/* Horde_Mongo_Collection_Index methods. */
/**
*/
public function checkMongoIndices()
{
return $this->_params['mongo_db']->checkIndices($this->_db, $this->_indices);
}
/**
*/
public function createMongoIndices()
{
$this->_params['mongo_db']->createIndices($this->_db, $this->_indices);
}
}