| Commit | Line | Data |
|---|---|---|
| 67189554 | 1 | <?php |
| 67189554 TGGM |
2 | |
| 3 | /** | |
| 4 | * @file | |
| 5 | * install file for privatemsg_filter | |
| 6 | */ | |
| 7 | ||
| 42daa60e OT |
8 | /** |
| 9 | * Implements hook_schema(). | |
| 10 | */ | |
| 67189554 TGGM |
11 | function privatemsg_filter_schema() { |
| 12 | $schema = array(); | |
| 13 | ||
| 14 | $schema['pm_tags'] = array( | |
| e4b16a95 | 15 | 'description' => '{pm_tags} holds the names of tags and their id.', |
| 67189554 TGGM |
16 | 'fields' => array( |
| 17 | 'tag_id' => array( | |
| e4b16a95 | 18 | 'description' => 'Tag ID', |
| 67189554 TGGM |
19 | 'type' => 'serial', |
| 20 | 'not null' => TRUE, | |
| 21 | 'unsigned' => TRUE, | |
| 22 | ), | |
| 23 | 'tag' => array( | |
| e4b16a95 | 24 | 'description' => 'The name of the tag', |
| 67189554 TGGM |
25 | 'type' => 'varchar', |
| 26 | 'length' => 255, | |
| 27 | 'not null' => TRUE, | |
| 28 | ), | |
| e832aa65 SG |
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 | ), | |
| c7b4c4d1 SG |
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 | ) | |
| 67189554 | 43 | ), |
| 31984367 SG |
44 | 'primary key' => array('tag_id'), |
| 45 | 'indexes' => array( | |
| 46 | 'tag_list' => array('tag_id', 'tag', 'public'), | |
| 47 | ), | |
| 67189554 TGGM |
48 | ); |
| 49 | ||
| 50 | $schema['pm_tags_index'] = array( | |
| e4b16a95 | 51 | 'description' => '{pm_tags_index} holds mapping information between tags, threads the users.', |
| 67189554 TGGM |
52 | 'fields' => array( |
| 53 | 'tag_id' => array( | |
| e4b16a95 | 54 | 'description' => 'Tag ID', |
| 67189554 TGGM |
55 | 'type' => 'int', |
| 56 | 'not null' => TRUE, | |
| 57 | 'unsigned' => TRUE, | |
| 58 | ), | |
| 59 | 'uid' => array( | |
| e4b16a95 | 60 | 'description' => 'ID of the user', |
| 67189554 TGGM |
61 | 'type' => 'int', |
| 62 | 'not null' => TRUE, | |
| 63 | 'unsigned' => TRUE, | |
| 64 | ), | |
| 65 | 'thread_id' => array( | |
| e4b16a95 | 66 | 'description' => 'id of the thread', |
| 67189554 TGGM |
67 | 'type' => 'int', |
| 68 | 'not null' => TRUE, | |
| 69 | 'unsigned' => TRUE, | |
| 70 | ), | |
| 71 | ), | |
| 72 | 'primary key' => array('tag_id', 'uid', 'thread_id'), | |
| 73 | 'indexes' => array( | |
| 31984367 | 74 | 'thread_tags' => array('uid', 'thread_id'), |
| 67189554 TGGM |
75 | ), |
| 76 | ); | |
| 77 | ||
| 78 | return $schema; | |
| 79 | } | |
| 67189554 | 80 | |
| 42daa60e OT |
81 | /** |
| 82 | * Implements hook_uninstall(). | |
| 83 | */ | |
| 67189554 | 84 | function privatemsg_filter_uninstall() { |
| b24e2440 | 85 | variable_del('privatemsg_filter_searchbody'); |
| f3c05a33 | 86 | variable_del('privatemsg_filter_tagfield_weight'); |
| b9d5a14f | 87 | variable_del('privatemsg_filter_default_list'); |
| c7b4c4d1 SG |
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')))); | |
| 31984367 SG |
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 | ||
| fbe8e358 SG |
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'; | |
| c5db5b4a | 154 | if (db_field_exists('pm_index', 'recipient')) { |
| fbe8e358 SG |
155 | $uid_column = 'recipient'; |
| 156 | } | |
| 157 | ||
| 31984367 SG |
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. | |
| fbe8e358 | 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'; |
| 31984367 SG |
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 | } |