Projet

Général

Profil

Paste
Télécharger (6,97 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / privatemsg / privatemsg_filter / privatemsg_filter.install @ e3063c4a

1
<?php
2

    
3
/**
4
 * @file
5
 * install file for privatemsg_filter
6
 */
7

    
8
/**
9
 * Implements hook_schema().
10
 */
11
function privatemsg_filter_schema() {
12
  $schema = array();
13

    
14
  $schema['pm_tags'] = array(
15
    'description'       => '{pm_tags} holds the names of tags and their id.',
16
    'fields'        => array(
17
      'tag_id'    => array(
18
        'description'   => 'Tag ID',
19
        'type'          => 'serial',
20
        'not null'      => TRUE,
21
        'unsigned'      => TRUE,
22
      ),
23
      'tag'     => array(
24
        'description'   => 'The name of the tag',
25
        'type'          => 'varchar',
26
        'length'        => 255,
27
        'not null'      => TRUE,
28
      ),
29
      'public'    => array(
30
        'description'   => 'Defines if a tag is public (visible for all users)',
31
        'type'          => 'int',
32
        'unsigned'      => TRUE,
33
        'size'          => 'tiny',
34
        'default'       => 0,
35
      ),
36
      'hidden' => array(
37
        'type'          => 'int',
38
        'description'   => 'Defines if a tag should not be displayed and is usually automatically managed',
39
        'unsigned'      => TRUE,
40
        'size'          => 'tiny',
41
        'default'       => 0,
42
      )
43
    ),
44
    'primary key'     => array('tag_id'),
45
    'indexes'     => array(
46
      'tag_list'    => array('tag_id', 'tag', 'public'),
47
    ),
48
  );
49

    
50
  $schema['pm_tags_index'] = array(
51
    'description'       => '{pm_tags_index} holds mapping information between tags, threads the users.',
52
    'fields'        => array(
53
      'tag_id'    => array(
54
        'description'   => 'Tag ID',
55
        'type'          => 'int',
56
        'not null'      => TRUE,
57
        'unsigned'      => TRUE,
58
      ),
59
      'uid'    => array(
60
        'description'   => 'ID of the user',
61
        'type'          => 'int',
62
        'not null'      => TRUE,
63
        'unsigned'      => TRUE,
64
      ),
65
      'thread_id'    => array(
66
        'description'   => 'id of the thread',
67
        'type'          => 'int',
68
        'not null'      => TRUE,
69
        'unsigned'      => TRUE,
70
      ),
71
    ),
72
    'primary key'     => array('tag_id', 'uid', 'thread_id'),
73
    'indexes'         => array(
74
     'thread_tags'      => array('uid', 'thread_id'),
75
    ),
76
  );
77

    
78
  return $schema;
79
}
80

    
81
/**
82
 * Implements hook_uninstall().
83
 */
84
function privatemsg_filter_uninstall() {
85
  variable_del('privatemsg_filter_searchbody');
86
  variable_del('privatemsg_filter_tagfield_weight');
87
  variable_del('privatemsg_filter_default_list');
88
  variable_del('privatemsg_filter_inbox_tag');
89
}
90

    
91
/**
92
 * Implements hook_enable().
93
 */
94
function privatemsg_filter_enable() {
95
  if (!($tag_id = variable_get('privatemsg_filter_inbox_tag', '')) || db_query('SELECT 1 FROM {pm_tags} WHERE tag_id = :tag_id', array(':tag_id' => $tag_id))->fetchField()) {
96
    $tag_id = db_insert('pm_tags')
97
      ->fields(array(
98
        'tag' => 'Inbox',
99
        'hidden' => 1,
100
      ))
101
      ->execute();
102
    variable_set('privatemsg_filter_inbox_tag', $tag_id);
103
  }
104
  drupal_set_message(t('Visit <a href="!rebuild_url">Rebuild Inbox</a> to tag existing messages to show up in the inbox.', array('!rebuild_url' => url('admin/config/messaging/privatemsg/tags/rebuild'))));
105
}
106

    
107

    
108
/**
109
 * Add hidden flag and create inbox tag.
110
 */
111
function privatemsg_filter_update_7000() {
112
  if (!db_field_exists('pm_tags', 'hidden')) {
113
    db_add_field('pm_tags', 'hidden', array(
114
      'description'   => 'Defines if a tag should not be displayed and is usually automatically managed',
115
      'type'          => 'int',
116
      'unsigned'      => TRUE,
117
      'size'          => 'tiny',
118
      'default'       => 0,
119
    ));
120
  }
121

    
122
  if (!($tag_id = variable_get('privatemsg_filter_inbox_tag', '')) || (!db_query('SELECT 1 FROM {pm_tags} WHERE tag_id = :tag_id', array(':tag_id' => $tag_id))->fetchField())) {
123
    $tag_id = db_insert('pm_tags')
124
      ->fields(array(
125
        'tag' => 'Inbox',
126
        'hidden' => 1,
127
      ))
128
      ->execute();
129
    variable_set('privatemsg_filter_inbox_tag', $tag_id);
130
  }
131
}
132

    
133
/**
134
 * Add inbox tag to existing inbox messages.
135
 */
136
function privatemsg_filter_update_7001(&$sandbox) {
137
  // First run, initialize sandbox.
138
  if (!isset($sandbox['current_thread_id'])) {
139
    // If the sandbox is not initialized, check if there are any threads tagged
140
    // with the inbox tag. If yes, the update did already run.
141
    if (db_query_range('SELECT 1 FROM {pm_tags_index} WHERE tag_id = :tag_id', 0, 1, array(':tag_id' => variable_get('privatemsg_filter_inbox_tag', 0)))->fetchField()) {
142
      return;
143
    }
144

    
145
    $sandbox['current_thread_id'] = 0;
146
    // Assume that the thread ids are distributed more or less equally over the
147
    // whole data set. This allows us to calculate the approximate progress.
148
    $sandbox['max'] = db_query('SELECT MAX(thread_id) FROM {pm_index}')->fetchField();
149
  }
150

    
151
  // There is no way to know if this update is run before or after
152
  // privatemsg_update_update_6201() which renames uid to recipient.
153
  $uid_column = 'uid';
154
  if (db_field_exists('pm_index', 'recipient')) {
155
    $uid_column = 'recipient';
156
  }
157

    
158
  // Fetch the 10 next thread_ids.
159
  $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']));
160
  $threads = $result->fetchCol();
161
  $inbox_tag = variable_get('privatemsg_filter_inbox_tag', '');
162
  if (!empty($threads)) {
163
    // By limiting this slow query (having condition with 2 depending subqueries)
164
    // on a specific set of threads, this allows us to process the slow having
165
    // part on a relatively small subset of pm_index that can be selected based on
166
    // the thread_id index.
167
    $sql = 'SELECT pmi.thread_id, pmi.' . $uid_column . ' AS uid FROM {pm_index} pmi WHERE thread_id IN (:threads)  GROUP BY pmi.thread_id, pmi.' . $uid_column . ' HAVING ((SELECT pmf.author FROM {pm_message} pmf WHERE pmf.mid = pmi.thread_id) = pmi.' . $uid_column . ' AND COUNT(pmi.thread_id) > 1) OR (SELECT COUNT(*) FROM {pm_message} pmf INNER JOIN {pm_index} pmif ON (pmf.mid = pmif.mid) WHERE pmif.thread_id = pmi.thread_id AND pmf.author <> pmi.' . $uid_column . ') > 0';
168
    $result = db_query($sql, array(':threads' => $threads));
169
    foreach ($result as $row) {
170
      // Make sure that we don't add a tag to a thread twice,
171
      // only insert if there is no such tag yet.
172
      // We duplicate privatemsg_filter_add_tags() here in case
173
      // privatemsg_filter.module is disabled and therefore not loaded.
174
      $exists = db_query('SELECT 1 FROM {pm_tags_index} WHERE tag_id = :tag_id AND (uid = :uid AND thread_id = :thread_id)', array(':tag_id' => $inbox_tag, ':uid' => $row->uid, ':thread_id' => $row->thread_id))->fetchField();
175
      if (!$exists) {
176
        db_insert('pm_tags_index')
177
          ->fields(array(
178
            'tag_id' => $inbox_tag,
179
            'uid' => $row->uid,
180
            'thread_id' => $row->thread_id,
181
          ))
182
          ->execute();
183
      }
184
    }
185
    $sandbox['current_thread_id'] = max($threads);
186
  }
187
  // Set #finished based on sandbox.
188
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current_thread_id'] / $sandbox['max']);
189
}