Projet

Général

Profil

Paste
Télécharger (10,5 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / privatemsg / privatemsg.install @ a5572547

1
<?php
2

    
3
/**
4
 * @file
5
 * Install file for privatemsg.module
6
 */
7

    
8
/**
9
 * Implements hook_schema().
10
 */
11
function privatemsg_schema() {
12
  $schema = array();
13
  $schema['pm_index'] = array(
14
    'description'       => '{pm_index} holds indexing information about messages and recipients for fast retrieval',
15
    'fields'        => array(
16
      'mid'    => array(
17
        'description'   => 'Private Message ID',
18
        'type'          => 'int',
19
        'not null'      => TRUE,
20
        'unsigned'      => TRUE,
21
      ),
22
      'thread_id'     => array(
23
        'description'   => 'Messages thread ID',
24
        'type'          => 'int',
25
        'not null'      => TRUE,
26
        'unsigned'      => TRUE,
27
      ),
28
      'recipient'     => array(
29
        'description'   => 'ID of the recipient object, typically user',
30
        'type'          => 'int',
31
        'not null'      => TRUE,
32
        'unsigned'      => TRUE,
33
      ),
34
      'is_new'     => array(
35
        'description'   => 'Whether the user has read this message',
36
        'type'          => 'int',
37
        'default'       => 1,
38
        'not null'      => TRUE,
39
        'unsigned'      => TRUE,
40
      ),
41
      'deleted' => array(
42
        'description'   => 'Whether the user has deleted this message',
43
        'type' => 'int',
44
        'unsigned' => TRUE,
45
        'not null' => TRUE,
46
        'default' => 0
47
      ),
48
      'type' => array(
49
        'description'   => 'Type of recipient object',
50
        'type' => 'varchar',
51
        'length' => 255,
52
        'not null' => TRUE,
53
        'default' => 'user'
54
      ),
55
    ),
56
    'primary key'     => array('mid', 'recipient', 'type'),
57
    'indexes'         => array(
58
      'list'         => array('recipient', 'type', 'deleted', 'is_new'),
59
      'messages'     => array('mid', 'recipient', 'type'),
60
      'participants' => array('thread_id', 'recipient', 'type', 'deleted'),
61
    ),
62
  );
63

    
64
  $schema['pm_message'] = array(
65
    'description'       => '{pm_messages} holds the message information',
66
    'fields'        => array(
67
      'mid'    => array(
68
        'description'   => 'Private Message ID',
69
        'type'          => 'serial',
70
        'not null'      => TRUE,
71
        'unsigned'      => TRUE,
72
      ),
73
      'author'     => array(
74
        'description'   => 'UID of the author',
75
        'type'          => 'int',
76
        'not null'      => TRUE,
77
        'unsigned'      => TRUE,
78
      ),
79
      'subject'     => array(
80
        'description'   => 'Subject text of the message',
81
        'type'          => 'varchar',
82
        'length'        => 255,
83
        'not null'      => TRUE,
84
      ),
85
      'body'     => array(
86
        'description'   => 'Body of the message',
87
        'type'          => 'text',
88
        'not null'      => TRUE,
89
        'size'          => 'big',
90
      ),
91
      'format'        => array(
92
        'type'          => 'varchar',
93
        'length'        => 255,
94
        'not null'      => FALSE,
95
        'description'   => 'The {filter_formats}.format of the message text.',
96
      ),
97
      'timestamp'     => array(
98
        'description'   => 'Time when the message was sent',
99
        'type'          => 'int',
100
        'not null'      => TRUE,
101
        'unsigned'      => TRUE,
102
      ),
103
      'has_tokens'     => array(
104
        'description'   => 'Indicates if the message has tokens',
105
        'type'          => 'int',
106
        'size'          => 'small',
107
        'not null'      => TRUE,
108
        'unsigned'      => TRUE,
109
        'default'       => 0,
110
      ),
111
    ),
112
    'primary key'     => array('mid'),
113
  );
114

    
115
  $schema['pm_disable'] = array(
116
    'description'       => '{pm_disable} holds the list of users that have disabled private messaging',
117
    'fields' => array(
118
      'uid'    => array(
119
        'description'   => 'ID of the user',
120
        'type'          => 'int',
121
        'not null'      => TRUE,
122
        'unsigned'      => TRUE,
123
      ),
124
    ),
125
    'primary key'       => array('uid'),
126
  );
127

    
128
  return $schema;
129
}
130

    
131
/**
132
 * Implements hook_install().
133
 */
134
function privatemsg_install() {
135
  // Define default formats for date format types.
136
  variable_set("date_format_privatemsg_current_day", 'g:i a');
137
  variable_set("date_format_privatemsg_current_year", 'M j');
138
  variable_set("date_format_privatemsg_years", 'n/j/y');
139
}
140

    
141
/**
142
 * Implements hook_uninstall().
143
 */
144
function privatemsg_uninstall() {
145
  variable_del('private_message_view_template');
146
  variable_del('privatemsg_per_page');
147
  variable_del('privatemsg_display_loginmessage');
148
  variable_del('privatemsg_display_fields');
149
  variable_del('privatemsg_display_link_self');
150
  variable_del('privatemsg_view_default_amount');
151
  variable_del('privatemsg_view_max_amount');
152
  variable_del('privatemsg_view_use_max_as_default');
153
  variable_del('privatemsg_display_profile_links');
154
  variable_del('privatemsg_link_node_types');
155
  variable_del('privatemsg_display_on_teaser');
156
  variable_del('privatemsg_no_messages_notification');
157
  variable_del('privatemsg_display_on_comments');
158
}
159

    
160
/**
161
 * Implements hook_update_dependencies().
162
 */
163
function privatemsg_update_dependencies() {
164
  if (module_exists('privatemsg_filter')) {
165
    $dependencies['privatemsg']['7004']['privatemsg_filter'] = '7001';
166
    return $dependencies;
167
  }
168
}
169

    
170
/**
171
 * Implements hook_update_last_removed().
172
 */
173
function privatemsg_update_last_removed() {
174
  // Only upgrading from 6.x-1.3 or later is supported.
175
  return 6007;
176
}
177

    
178

    
179
/**
180
 * Add has_tokens field to indicate if a message is using tokens.
181
 */
182
function privatemsg_update_7000() {
183
  db_add_field('pm_message', 'has_tokens', array(
184
    'description'   => 'Indicates if the message has tokens',
185
    'type'          => 'int',
186
    'size'          => 'small',
187
    'not null'      => TRUE,
188
    'unsigned'      => TRUE,
189
    'default'       => 0,
190
  ));
191
}
192

    
193
/**
194
 * Update format column to varchar.
195
 */
196
function privatemsg_update_7001() {
197
  db_change_field('pm_message', 'format', 'format', array(
198
    'type'          => 'varchar',
199
    'length'        => 255,
200
    'not null'      => FALSE,
201
    'description'   => 'The {filter_formats}.format of the message text.',
202
  ));
203
}
204

    
205

    
206
/**
207
 * Add {pm_disable} table.
208
 */
209
function privatemsg_update_7002() {
210

    
211
  // Make sure to not run this update twice.
212
  if (db_table_exists('pm_disable')) {
213
    return;
214
  }
215

    
216
  $schema['pm_disable'] = array(
217
    'description'       => '{pm_disable} holds the list of users that have disabled private messaging',
218
    'fields' => array(
219
      'uid'    => array(
220
        'description'   => 'ID of the user',
221
        'type'          => 'int',
222
        'not null'      => TRUE,
223
        'unsigned'      => TRUE,
224
      ),
225
    ),
226
    'primary key'       => array('uid'),
227
  );
228

    
229
  if (!(db_table_exists('pm_disable'))) {
230
    db_create_table('pm_disable', $schema['pm_disable']);
231
  }
232
}
233

    
234
/**
235
 * Change schema to allow other recipients than single users.
236
 */
237
function privatemsg_update_7003() {
238
  // Make sure to not run this update twice.
239
  if (db_field_exists('pm_index', 'recipient')) {
240
    return;
241
  }
242
  db_drop_index('pm_index', 'list');
243
  db_drop_index('pm_index', 'messages');
244
  db_drop_index('pm_index', 'participants');
245
  db_change_field('pm_index', 'uid', 'recipient', array(
246
    'description'   => 'ID of the recipient object, typically user',
247
    'type'          => 'int',
248
    'not null'      => TRUE,
249
    'unsigned'      => TRUE,
250
  ));
251
  db_add_field('pm_index', 'type', array(
252
    'description'   => 'Type of recipient object',
253
    'type' => 'varchar',
254
    'not null' => TRUE,
255
    'length'  => '255',
256
    'default' => 'user',
257
  ), array(
258
    'indexes' => array(
259
      'list'         => array('recipient', 'type', 'deleted', 'is_new'),
260
      'messages'     => array('mid', 'recipient', 'type'),
261
      'participants' => array('thread_id', 'recipient', 'type', 'deleted'),
262
    ),
263
  ));
264
}
265

    
266
/**
267
 * Remove duplicate entries in {pm_index}.
268
 */
269
function privatemsg_update_7004(&$sandbox) {
270
  // If the primary key already exists, this doesn't need to run.
271
  if (db_index_exists('pm_index', 'PRIMARY')) {
272
    return;
273
  }
274

    
275
  // First run, initialize sandbox and check if we are ready to run.
276
  if (!isset($sandbox['current_thread_id'])) {
277
    $sandbox['current_thread_id'] = 0;
278
    // Assume that the thread ids are distributed more or less equally over the
279
    // whole data set. This allows us to calculate the approximate progress.
280
    $sandbox['max'] = db_query('SELECT MAX(thread_id) FROM {pm_index}')->fetchField();
281
  }
282

    
283
  // Fetch the next 10 thread_ids.
284
  $result = db_query_range('SELECT DISTINCT thread_id FROM {pm_index} WHERE thread_id > :thread_id ORDER BY thread_id ASC', 0, 20, array(':thread_id' => $sandbox['current_thread_id']));
285
  $threads = $result->fetchCol();
286

    
287
  if (!empty($threads)) {
288
    // By limiting this slow query (having condition) on a specific set of
289
    // threads, this allows us to process the slow having part on a relatively
290
    // small subset of pm_index that can be selected based on the thread_id
291
    // index. There might also be cases where is_new and/or deleted are not
292
    // equal, use MAX(is_new) and MIN(deleted) for these cases.
293
    $sql = 'SELECT COUNT(*) AS count, pmi.recipient, pmi.type, pmi.mid, pmi.thread_id, MAX(pmi.is_new) As is_new, MIN(pmi.deleted) AS deleted FROM {pm_index} pmi WHERE thread_id IN (:threads) GROUP BY pmi.recipient, pmi.type, pmi.mid, pmi.thread_id HAVING COUNT(*) > 1';
294
    $result = db_query($sql, array(':threads' => $threads));
295
    foreach ($result as $row) {
296
      // Delete all occurrences of these entries.
297
      db_delete('pm_index')
298
        ->condition('mid', $row->mid)
299
        ->condition('recipient', $row->recipient)
300
        ->condition('type', $row->type)
301
        ->execute();
302
      // Re-insert a single entry. We do this to explicitly keep messages
303
      // unread and undeleted if there are conflicting entries.
304
      db_insert('pm_index')
305
        ->fields(array(
306
          'mid' => $row->mid,
307
          'thread_id' => $row->thread_id,
308
          'recipient' => $row->recipient,
309
          'type' => $row->type,
310
          'is_new' => $row->is_new,
311
          'deleted' => $row->deleted,
312
        ))
313
        ->execute();
314
    }
315
    $sandbox['current_thread_id'] = max($threads);
316
  }
317
  // Set #finished based on sandbox.
318
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current_thread_id'] / $sandbox['max']);
319
}
320

    
321
/**
322
 * Add primary key to {pm_index}.
323
 */
324
function privatemsg_update_7005() {
325
  if (!db_index_exists('pm_index', 'PRIMARY')) {
326
    db_add_primary_key('pm_index', array('mid', 'recipient', 'type'));
327
  }
328
}
329

    
330
/**
331
 * Define default formats for date format types.
332
 */
333
function privatemsg_update_7006() {
334
  variable_set("date_format_privatemsg_current_day", 'g:i a');
335
  variable_set("date_format_privatemsg_current_year", 'M j');
336
  variable_set("date_format_privatemsg_years", 'n/j/y');
337
}