1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class IMP_Sentmail_Mongo extends IMP_Sentmail implements Horde_Mongo_Collection_Index
24: {
25:
26: const ACTION = 'action';
27: const MESSAGEID = 'msgid';
28: const RECIPIENT = 'recip';
29: const SUCCESS = 'success';
30: const TS = 'ts';
31: const WHO = 'who';
32:
33: 34: 35: 36: 37:
38: protected $_db;
39:
40: 41: 42: 43: 44:
45: protected $_indices = array(
46: 'index_ts' => array(
47: self::TS => 1
48: ),
49: 'index_who' => array(
50: self::WHO => 1
51: ),
52: 'index_success' => array(
53: self::SUCCESS => 1
54: )
55: );
56:
57: 58: 59: 60: 61:
62: public function __construct(array $params = array())
63: {
64: if (!isset($params['mongo_db'])) {
65: throw new InvalidArgumentException('Missing mongo_db parameter.');
66: }
67:
68: parent::__construct(array_merge(array(
69: 'collection' => 'imp_sentmail'
70: ), $params));
71:
72: $this->_db = $this->_params['mongo_db']->selectCollection(null, $this->_params['collection']);
73: }
74:
75: 76:
77: protected function _log($action, $message_id, $recipient, $success)
78: {
79: try {
80: $this->_db->insert(array(
81: self::ACTION => $action,
82: self::MESSAGEID => $message_id,
83: self::RECIPIENT => $recipient,
84: self::SUCCESS => intval($success),
85: self::TS => time(),
86: self::WHO => $GLOBALS['registry']->getAuth()
87: ));
88: } catch (MongoException $e) {}
89: }
90:
91: 92:
93: public function favouriteRecipients($limit, $filter = null)
94: {
95: $query = array(
96: self::SUCCESS => 1,
97: self::WHO => $GLOBALS['registry']->getAuth()
98: );
99:
100: if (!empty($filter)) {
101: $query[self::ACTION] = array('$in' => $filter);
102: }
103:
104: $out = array();
105:
106: try {
107:
108: $res = $this->_db->aggregate(array(
109:
110: array('$match' => $query),
111:
112:
113: array(
114: '$group' => array(
115: '_id' => '$' . self::RECIPIENT,
116: 'count' => array(
117: '$sum' => 1
118: )
119: )
120: ),
121:
122:
123: array(
124: '$sort' => array('count' => -1)
125: ),
126:
127:
128: array(
129: '$limit' => $limit
130: )
131: ));
132:
133: if (isset($res['result'])) {
134: foreach ($res['result'] as $val) {
135: $out[] = $val['_id'];
136: }
137: }
138: } catch (MongoException $e) {}
139:
140: return $out;
141: }
142:
143: 144:
145: public function numberOfRecipients($hours, $user = false)
146: {
147: $query = array(
148: self::TS => array(
149: '$gt' => (time() - ($hours * 3600))
150: )
151: );
152:
153: if ($user) {
154: $query[self::WHO] = $GLOBALS['registry']->getAuth();
155: }
156:
157: try {
158: return $this->_db->count($query);
159: } catch (MongoException $e) {
160: return 0;
161: }
162: }
163:
164: 165:
166: protected function _deleteOldEntries($before)
167: {
168: try {
169: $this->_db->remove(array(
170: self::TS => array(
171: '$lt' => $before
172: )
173: ));
174: } catch (MongoException $e) {}
175: }
176:
177:
178:
179: 180:
181: public function checkMongoIndices()
182: {
183: return $this->_params['mongo_db']->checkIndices($this->_db, $this->_indices);
184: }
185:
186: 187:
188: public function createMongoIndices()
189: {
190: $this->_params['mongo_db']->createIndices($this->_db, $this->_indices);
191: }
192:
193: }
194: