privatemsg_filter.install

  1. 1 privatemsg_filter/privatemsg_filter.install
  2. 7-1 privatemsg_filter/privatemsg_filter.install
  3. 7-2 privatemsg_filter/privatemsg_filter.install
  4. 6-2 privatemsg_filter/privatemsg_filter.install

install file for privatemsg_filter

File

privatemsg_filter/privatemsg_filter.install
View source
  1. <?php
  2. /**
  3. * @file
  4. * install file for privatemsg_filter
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function privatemsg_filter_schema() {
  10. $schema = array();
  11. $schema['pm_tags'] = array(
  12. 'description' => '{pm_tags} holds the names of tags and their id.',
  13. 'fields' => array(
  14. 'tag_id' => array(
  15. 'description' => 'Tag ID',
  16. 'type' => 'serial',
  17. 'not null' => TRUE,
  18. 'unsigned' => TRUE,
  19. ),
  20. 'tag' => array(
  21. 'description' => 'The name of the tag',
  22. 'type' => 'varchar',
  23. 'length' => 255,
  24. 'not null' => TRUE,
  25. ),
  26. 'public' => array(
  27. 'description' => 'Defines if a tag is public (visible for all users)',
  28. 'type' => 'int',
  29. 'unsigned' => TRUE,
  30. 'size' => 'tiny',
  31. 'default' => 0,
  32. ),
  33. 'hidden' => array(
  34. 'type' => 'int',
  35. 'description' => 'Defines if a tag should not be displayed and is usually automatically managed',
  36. 'unsigned' => TRUE,
  37. 'size' => 'tiny',
  38. 'default' => 0,
  39. )
  40. ),
  41. 'primary key' => array('tag_id'),
  42. 'indexes' => array(
  43. 'tag_list' => array('tag_id', 'tag', 'public'),
  44. ),
  45. );
  46. $schema['pm_tags_index'] = array(
  47. 'description' => '{pm_tags_index} holds mapping information between tags, threads the users.',
  48. 'fields' => array(
  49. 'tag_id' => array(
  50. 'description' => 'Tag ID',
  51. 'type' => 'int',
  52. 'not null' => TRUE,
  53. 'unsigned' => TRUE,
  54. ),
  55. 'uid' => array(
  56. 'description' => 'ID of the user',
  57. 'type' => 'int',
  58. 'not null' => TRUE,
  59. 'unsigned' => TRUE,
  60. ),
  61. 'thread_id' => array(
  62. 'description' => 'id of the thread',
  63. 'type' => 'int',
  64. 'not null' => TRUE,
  65. 'unsigned' => TRUE,
  66. ),
  67. ),
  68. 'primary key' => array('tag_id', 'uid', 'thread_id'),
  69. 'indexes' => array(
  70. 'thread_tags' => array('uid', 'thread_id'),
  71. ),
  72. );
  73. return $schema;
  74. }
  75. /**
  76. * Implements hook_uninstall().
  77. */
  78. function privatemsg_filter_uninstall() {
  79. variable_del('privatemsg_filter_searchbody');
  80. variable_del('privatemsg_filter_tagfield_weight');
  81. variable_del('privatemsg_filter_default_list');
  82. variable_del('privatemsg_filter_inbox_tag');
  83. }
  84. /**
  85. * Implements hook_enable().
  86. */
  87. function privatemsg_filter_enable() {
  88. 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()) {
  89. $tag_id = db_insert('pm_tags')
  90. ->fields(array(
  91. 'tag' => 'Inbox',
  92. 'hidden' => 1,
  93. ))
  94. ->execute();
  95. variable_set('privatemsg_filter_inbox_tag', $tag_id);
  96. }
  97. 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'))));
  98. }
  99. /**
  100. * Add hidden flag and create inbox tag.
  101. */
  102. function privatemsg_filter_update_7000() {
  103. if (!db_field_exists('pm_tags', 'hidden')) {
  104. db_add_field('pm_tags', 'hidden', array(
  105. 'description' => 'Defines if a tag should not be displayed and is usually automatically managed',
  106. 'type' => 'int',
  107. 'unsigned' => TRUE,
  108. 'size' => 'tiny',
  109. 'default' => 0,
  110. ));
  111. }
  112. 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())) {
  113. $tag_id = db_insert('pm_tags')
  114. ->fields(array(
  115. 'tag' => 'Inbox',
  116. 'hidden' => 1,
  117. ))
  118. ->execute();
  119. variable_set('privatemsg_filter_inbox_tag', $tag_id);
  120. }
  121. }
  122. /**
  123. * Add inbox tag to existing inbox messages.
  124. */
  125. function privatemsg_filter_update_7001(&$sandbox) {
  126. // First run, initialize sandbox.
  127. if (!isset($sandbox['current_thread_id'])) {
  128. // If the sandbox is not initialized, check if there are any threads tagged
  129. // with the inbox tag. If yes, the update did already run.
  130. 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()) {
  131. return;
  132. }
  133. $sandbox['current_thread_id'] = 0;
  134. // Assume that the thread ids are distributed more or less equally over the
  135. // whole data set. This allows us to calculate the approximate progress.
  136. $sandbox['max'] = db_query('SELECT MAX(thread_id) FROM {pm_index}')->fetchField();
  137. }
  138. // There is no way to know if this update is run before or after
  139. // privatemsg_update_update_6201() which renames uid to recipient.
  140. $uid_column = 'uid';
  141. if (db_field_exists('pm_index', 'recipient')) {
  142. $uid_column = 'recipient';
  143. }
  144. // Fetch the 10 next thread_ids.
  145. $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']));
  146. $threads = $result->fetchCol();
  147. $inbox_tag = variable_get('privatemsg_filter_inbox_tag', '');
  148. if (!empty($threads)) {
  149. // By limiting this slow query (having condition with 2 depending subqueries)
  150. // on a specific set of threads, this allows us to process the slow having
  151. // part on a relatively small subset of pm_index that can be selected based on
  152. // the thread_id index.
  153. $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';
  154. $result = db_query($sql, array(':threads' => $threads));
  155. foreach ($result as $row) {
  156. // Make sure that we don't add a tag to a thread twice,
  157. // only insert if there is no such tag yet.
  158. // We duplicate privatemsg_filter_add_tags() here in case
  159. // privatemsg_filter.module is disabled and therefore not loaded.
  160. $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();
  161. if (!$exists) {
  162. db_insert('pm_tags_index')
  163. ->fields(array(
  164. 'tag_id' => $inbox_tag,
  165. 'uid' => $row->uid,
  166. 'thread_id' => $row->thread_id,
  167. ))
  168. ->execute();
  169. }
  170. }
  171. $sandbox['current_thread_id'] = max($threads);
  172. }
  173. // Set #finished based on sandbox.
  174. $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current_thread_id'] / $sandbox['max']);
  175. }

Functions

Namesort descending Description
privatemsg_filter_enable Implements hook_enable().
privatemsg_filter_schema Implements hook_schema().
privatemsg_filter_uninstall Implements hook_uninstall().
privatemsg_filter_update_7000 Add hidden flag and create inbox tag.
privatemsg_filter_update_7001 Add inbox tag to existing inbox messages.