-
Notifications
You must be signed in to change notification settings - Fork 9
/
og_mailinglist.install
410 lines (366 loc) · 13.5 KB
/
og_mailinglist.install
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
// $Id$
include_once 'og_mailinglist_utilities.inc'; //Load our utilities so the hook_requirements can call those functions.
/**
* Implementation of hook_install().
*/
function og_mailinglist_install() {
$success = drupal_install_schema('og_mailinglist');
// Add any previously created groups to og_mailinglist.
add_groups_to_og_mailinglist();
if ($success) {
drupal_set_message(st('OG Mailinglist module installed tables successfully.'));
}
else {
drupal_set_message(st('The installation of the OG Mailinglist tables failed.'), 'error');
}
// Set OGM's system weight to be after Pathauto so our email footers always use the correct path.
$weight = db_result(db_query("SELECT weight FROM {system} WHERE type = 'module' AND name = 'pathauto'"));
if (!empty($weight)) {
db_query("UPDATE {system} SET weight = %d WHERE type = 'module' AND name = 'og_mailinglist'", $weight + 1);
}
}
/**
* Implementation of hook_uninstall().
*/
function og_mailinglist_uninstall() {
drupal_uninstall_schema('og_mailinglist');
$result = db_query("SELECT name FROM {variable} WHERE name LIKE 'og_mailinglist_%'");
while ($row = db_fetch_object($result)) {
variable_del($row->name);
}
drupal_set_message(t('OG Mailinglist has been uninstalled.'));
}
/**
* Implmentation of hook_requirements().
*/
function og_mailinglist_requirements($phase) {
if ($phase == "runtime") {
og_mailinglist_phpmailer_load_library();
$requirements['og_mailinglist_phpmailer']['title'] = t('PHPMailer');
if (class_exists('PHPMailer')) {
$requirements['og_mailinglist_phpmailer']['value'] = 'PHPMailer Library is installed correctly';
$requirements['og_mailinglist_phpmailer']['severity'] = REQUIREMENT_OK;
}
else {
// Required library wasn't found.
$requirements['og_mailinglist_phpmailer']['value'] = t('Not found');
// Provide a download link to the PHPMailer library.
$requirements['og_mailinglist_phpmailer']['description'] = t('The <a href="@phpmailer">PHPMailer</a> library is missing or not installed correctly. <a href="@download">Download</a> and extract it to your <em>og_mailinglist</em> module directory. See the INSTALL.txt file for more information at path/to/drupal@install.', array('@phpmailer' => 'http://phpmailer.worxware.com/index.php?pg=phpmailer', '@download' => 'http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/', '@install' => url(drupal_get_path('module', 'og_mailinglist') . '/INSTALL.txt')));
$requirements['og_mailinglist_phpmailer']['severity'] = REQUIREMENT_ERROR;
}
//Check for the Mail_mimeDecode library.
og_mailinglist_mimeDecode_load_library();
$requirements['og_mailinglist_mimeDecode']['title'] = t('Mail_mimeDecode PEAR extension');
if (class_exists('Mail_mimeDecode')) {
$requirements['og_mailinglist_mimeDecode']['value'] = 'Mail_mimeDecode PEAR Extension found';
$requirements['og_mailinglist_mimeDecode']['severity'] = REQUIREMENT_OK;
}
else {
// Required library wasn't found.
$requirements['og_mailinglist_mimeDecode']['value'] = t('Not found');
$requirements['og_mailinglist_mimeDecode']['description'] = t('The Mail_mimeDecode PEAR extension is missing or not installed correctly. Check that the extension is installed and that your PHP include_path is correct. See the INSTALL.txt file for more information at path/to/drupal@install.', array('@install' => url(drupal_get_path('module', 'og_mailinglist') . '/INSTALL.txt')));
$requirements['og_mailinglist_mimeDecode']['severity'] = REQUIREMENT_ERROR;
}
}
return $requirements;
}
/**
* Implementation of hook_schema().
*/
function og_mailinglist_schema() {
$schema = array();
$schema['og_mailinglist'] = array(
'fields' => array(
'nid' => array(
'description' => 'The nid for the new posting.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'group_email' => array(
'description' => 'The machine/email readable version of the group. Uses purl shortcode if available.',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => ''),
),
'primary key' => array('nid'),
);
$schema['og_mailinglist_source'] = array(
'fields' => array(
'nid' => array(
'description' => 'The nid for the new posting.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'cid' => array(
'description' => 'The cid for the new posting. If 0, means posting was a new node not comment.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'source' => array(
'type' => 'varchar',
'description' => 'If the new posting was via email or the web. Should be either "email" or "web"',
'length' => '64',
'not null' => TRUE,
'default' => ''),
'message_id' => array(
'type' => 'varchar',
'description' => 'The message-id field of this post, either
from the email or auto-generated',
'non null' => TRUE,
'default' => 0,
'length' => 255),
'in_reply_to' => array(
'type' => 'varchar',
'description' => 'The in-reply-to header of this email, either
from the email or implied from the comment.',
'non null' => TRUE,
'default' => 0,
'length' => 255),
'references_header' => array(
'type' => 'varchar',
'description' => 'The reference header of this post.
If it originated from the web, this stores the references + message-id
of its parent. We use the ungainly title of references_header as references appears
to be a reserved word for MySQL',
'non null' => TRUE,
'default' => 0,
'length' => 512),
'parent_message_id' => array(
'type' => 'varchar',
'description' => 'The message-id of the parent of this post.',
'non null' => TRUE,
'default' => 0,
'length' => 255),
),
'primary key' => array('nid', 'cid'),
'indexes' => array(
'source' => array('source'),
'message_id' => array('message_id'),
),
);
$schema['og_mailinglist_thread'] = array(
'fields' => array(
'nid' => array(
'description' => 'The nid of the thread.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'uid' => array(
'description' => 'The uid of the user who\'s subscribed to this thead.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
),
'primary key' => array('nid', 'uid'),
'indexes' => array(
'nid' => array('nid'),
'uid' => array('uid'),
),
);
$schema['og_mailinglist_subscription'] = array(
'fields' => array(
'nid' => array(
'description' => 'The nid of the Group.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'uid' => array(
'description' => 'The uid of the User.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'subscription_type' => array(
'type' => 'varchar',
'description' => 'Subscription type, e.g. "no email", "digest email", or "email"',
'length' => '128',
'not null' => TRUE,
'default' => ''
),
),
'primary key' => array('nid', 'uid'),
'indexes' => array(
'uid' => array('uid'),
'subscription_type' => array('subscription_type'),
),
);
return $schema;
}
/**
* Create the og_mailinglist table.
*/
function og_mailinglist_update_6000() {
// Create table.
$ret = array();
$schema['og_mailinglist'] = array(
'fields' => array(
'nid' => array(
'description' => 'The nid for the new posting.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0),
'group_email' => array(
'description' => 'The machine/email readable version of the group. Uses purl shortcode if available.',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => ''),
),
'primary key' => array('nid'),
);
db_create_table($ret, 'og_mailinglist', $schema['og_mailinglist']);
return $ret;
}
/**
* Add entries to og_mailinglist for previously created groups.
*/
function og_mailinglist_update_6001() {
add_groups_to_og_mailinglist();
}
/**
* Modify og_mailinglist_subscriptions to remove space integration
*/
function og_mailinglist_update_6002() {
$ret = array();
db_drop_field($ret, 'og_mailinglist_subscription', 'space_type');
db_change_field($ret, 'og_mailinglist_subscription', 'sid', 'nid', array(
'type' => 'int',
'not null' => TRUE,
'default' => 0));
return $ret;
}
/**
* Add columns to og_mailinglist_source to store
* more information about emails to improve threading on site and email clients.
*/
function og_mailinglist_update_6003() {
$ret = array();
db_add_field($ret, 'og_mailinglist_source', 'message_id', array(
'type' => 'varchar',
'description' => 'The message-id field of this post, either
from the email or auto-generated',
'non null' => TRUE,
'default' => 0,
'length' => 255));
db_add_field($ret, 'og_mailinglist_source', 'in_reply_to', array(
'type' => 'varchar',
'description' => 'The in-reply-to header of this email, either
from the email or implied from the comment.',
'non null' => TRUE,
'default' => 0,
'length' => 255));
db_add_field($ret, 'og_mailinglist_source', 'references', array(
'type' => 'varchar',
'description' => 'The reference header of this post.
If it originated from the web, this stores the references + message-id
of its parent.',
'non null' => TRUE,
'default' => 0,
'length' => 512));
db_add_field($ret, 'og_mailinglist_source', 'parent_message_id', array(
'type' => 'varchar',
'description' => 'The message-id of the parent of this post.',
'non null' => TRUE,
'default' => 0,
'length' => 255));
return $ret;
}
/**
* Change name of field to references_header from references.
*/
function og_mailinglist_update_6004() {
$ret = array();
db_change_field($ret, 'og_mailinglist_source', 'references', 'references_header', array(
'type' => 'varchar',
'non null' => TRUE,
'default' => 0,
'length' => 512)
);
return $ret;
}
/**
* Change module weight to ensure we fire after pathauto if its present.
*/
function og_mailinglist_update_6005() {
$ret = array();
$weight = db_result(db_query("SELECT weight FROM {system} WHERE type = 'module' AND name = 'pathauto'"));
if (!empty($weight)) {
db_query("UPDATE {system} SET weight = %d WHERE type = 'module' AND name = 'og_mailinglist'", $weight + 1);
}
return $ret;
}
function add_groups_to_og_mailinglist() {
if (!module_exists('purl') && !module_exists('spaces_og')) {
$results = db_query("SELECT nid, title FROM {node} WHERE type = 'group'");
$groups = array();
while ($data = db_fetch_array($results)) {
$groups[$data['nid']] = $data['title'];
}
foreach ($groups as $nid => $title) {
$title = string_to_machine_readable($title);
db_query("INSERT INTO {og_mailinglist}
VALUES (%d, '%s')", $nid, $title);
// Subscribe group members to active threads.
og_mailinglist_create_subscriptions_for_last_month($nid);
}
}
// Else, just use the purl entries
else {
$results = db_query("SELECT id, value
FROM {purl}
WHERE provider = 'spaces_og'");
$groups = array();
while ($data = db_fetch_array($results)) {
$groups[$data['id']] = $data['value'];
}
foreach ($groups as $nid => $title) {
db_query("INSERT INTO {og_mailinglist}
VALUES (%d, '%s')", $nid, $title);
// Subscribe group members to active threads.
og_mailinglist_create_subscriptions_for_last_month($nid);
}
}
}
/**
* Creates group thread subscriptions for past 30 days so og_mailinglist
* works out of the box for previously created threads.
*/
function og_mailinglist_create_subscriptions_for_last_month($gid) {
module_load_include('inc', 'og_mailinglist', 'og_mailinglist_api');
// Create email subscriptions to group for all group members.
$results = db_query("SELECT uid
FROM {og_uid}
WHERE nid = %d", $gid);
$uids = array();
while ($data = db_fetch_array($results)) {
$uids[] = $data['uid'];
}
og_mailinglist_save_group_subscriptions($gid, $uids);
$sql = "SELECT n.nid
FROM {og_ancestry} o
JOIN {node} n
ON o.nid = n.nid
WHERE o.group_nid = %d
AND n.created > (unix_timestamp() - 2592000)";
$results = db_query($sql, $gid);
while ($data = db_fetch_array($results)) {
og_mailinglist_save_group_thread_subscriptions($gid, $data['nid']);
}
}
function string_to_machine_readable($str) {
$str = strtolower($str);
$str = str_replace("-", "", $str);
$str = preg_replace('/[^a-z0-9]/', "-", $str);
$str = str_replace("--", "-", $str);
$str = trim($str, " -");
return $str;
}