privatemsg_filter.install in Privatemsg 6.2
Same filename and directory in other branches
install file for privatemsg_filter
File
privatemsg_filter/privatemsg_filter.installView source
<?php
/**
* @file
* install file for privatemsg_filter
*/
/**
* Implements hook_schema().
*/
function privatemsg_filter_schema() {
$schema = array();
$schema['pm_tags'] = array(
'description' => '{pm_tags} holds the names of tags and their id.',
'fields' => array(
'tag_id' => array(
'description' => 'Tag ID',
'type' => 'serial',
'not null' => TRUE,
'unsigned' => TRUE,
),
'tag' => array(
'description' => 'The name of the tag',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'public' => array(
'description' => 'Defines if a tag is public (visible for all users)',
'type' => 'int',
'unsigned' => TRUE,
'size' => 'tiny',
'default' => 0,
),
'hidden' => array(
'type' => 'int',
'description' => 'Defines if a tag should not be displayed and is usually automatically managed',
'unsigned' => TRUE,
'size' => 'tiny',
'default' => 0,
),
),
'primary key' => array(
'tag_id',
),
'indexes' => array(
'tag_list' => array(
'tag_id',
'tag',
'public',
),
),
);
$schema['pm_tags_index'] = array(
'description' => '{pm_tags_index} holds mapping information between tags, threads the users.',
'fields' => array(
'tag_id' => array(
'description' => 'Tag ID',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
'uid' => array(
'description' => 'ID of the user',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
'thread_id' => array(
'description' => 'id of the thread',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
),
'primary key' => array(
'tag_id',
'uid',
'thread_id',
),
'indexes' => array(
'thread_tags' => array(
'uid',
'thread_id',
),
),
);
return $schema;
}
/**
* Implements hook_install().
*/
function privatemsg_filter_install() {
drupal_install_schema('privatemsg_filter');
}
/**
* Implements hook_enable().
*/
function privatemsg_filter_enable() {
if (!($tag_id = variable_get('privatemsg_filter_inbox_tag', '')) || !db_result(db_query('SELECT 1 FROM {pm_tags} WHERE tag_id = %d', $tag_id))) {
db_query("INSERT INTO {pm_tags} (tag, hidden) VALUES('Inbox', 1)");
variable_set('privatemsg_filter_inbox_tag', db_last_insert_id('pm_tags', 'tag_id'));
}
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/settings/messages/tags/rebuild'),
)));
}
/**
* Implements hook_uninstall().
*/
function privatemsg_filter_uninstall() {
variable_del('privatemsg_filter_searchbody');
variable_del('privatemsg_filter_tagfield_weight');
variable_del('privatemsg_filter_default_list');
variable_del('privatemsg_filter_inbox_tag');
drupal_uninstall_schema('privatemsg_filter');
}
function privatemsg_filter_update_6001() {
$ret = array();
$ret[] = update_sql("UPDATE {permission} SET perm = REPLACE(perm, 'use privatemsg_filter', 'filter private messages') WHERE perm LIKE '%use privatemsg_filter%'");
$ret[] = update_sql("UPDATE {permission} SET perm = REPLACE(perm, 'create privatemsg_filter', 'create private message tags') WHERE perm LIKE '%create privatemsg_filter%'");
return $ret;
}
function privatemsg_filter_update_6002() {
$ret = array();
db_add_column($ret, 'pm_tags', 'public', 'int', array(
'description' => 'Defines if a tag is public (visible for all users)',
'unsigned' => TRUE,
'size' => 'tiny',
'default' => 0,
));
return $ret;
}
/**
* Add hidden flag and create inbox tag.
*/
function privatemsg_filter_update_6200() {
$ret = array();
if (!db_column_exists('pm_tags', 'hidden')) {
db_add_column($ret, 'pm_tags', 'hidden', 'int', array(
'description' => 'Defines if a tag should not be displayed and is usually automatically managed',
'unsigned' => TRUE,
'size' => 'tiny',
'default' => 0,
));
}
if (!($tag_id = variable_get('privatemsg_filter_inbox_tag', '')) || !db_result(db_query('SELECT 1 FROM {pm_tags} WHERE tag_id = %d', $tag_id))) {
$ret[] = update_sql("INSERT INTO {pm_tags} (tag, hidden) VALUES('Inbox', 1)");
variable_set('privatemsg_filter_inbox_tag', db_last_insert_id('pm_tags', 'tag_id'));
}
return $ret;
}
/**
* Add inbox tag to existing inbox messages.
*/
function privatemsg_filter_update_6201(&$sandbox) {
// First run, initialize sandbox.
if (!isset($sandbox['current_thread_id'])) {
// If the sandbox is not initialized, check if there are any threads tagged
// with the inbox tag. If yes, the update did already run.
if (db_result(db_query_range('SELECT 1 FROM {pm_tags_index} WHERE tag_id = %d', variable_get('privatemsg_filter_inbox_tag', 0), 0, 1))) {
return array();
}
$sandbox['current_thread_id'] = 0;
// Assume that the thread ids are distributed more or less equally over the
// whole data set. This allows us to calculate the approximate progress.
$sandbox['max'] = db_result(db_query('SELECT MAX(thread_id) FROM {pm_index}'));
}
// There is no way to know if this update is run before or after
// privatemsg_update_update_6201() which renames uid to recipient.
$uid_column = 'uid';
if (db_column_exists('pm_index', 'recipient')) {
$uid_column = 'recipient';
}
// Fetch the 10 next thread_ids.
$result = db_query_range('SELECT DISTINCT thread_id FROM {pm_index} WHERE thread_id > %d ORDER BY thread_id ASC', $sandbox['current_thread_id'], 0, 20);
$threads = array();
while ($row = db_fetch_object($result)) {
$threads[] = $row->thread_id;
}
$inbox_tag = variable_get('privatemsg_filter_inbox_tag', '');
if (!empty($threads)) {
// By limiting this slow query (having condition with 2 depending subqueries)
// on a specific set of threads, this allows us to process the slow having
// part on a relatively small subset of pm_index that can be selected based on
// the thread_id index.
$sql = 'SELECT pmi.thread_id, pmi.' . $uid_column . ' AS uid FROM {pm_index} pmi WHERE thread_id IN (' . db_placeholders($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';
$result = db_query($sql, $threads);
while ($row = db_fetch_object($result)) {
// Make sure that we don't add a tag to a thread twice,
// only insert if there is no such tag yet.
// We duplicate privatemsg_filter_add_tags() here in case
// privatemsg_filter.module is disabled and therefore not loaded.
if (!db_result(db_query('SELECT 1 FROM {pm_tags_index} WHERE tag_id = %d AND (uid = %d AND thread_id = %d)', $inbox_tag, $row->uid, $row->thread_id))) {
db_query('INSERT INTO {pm_tags_index} (tag_id, uid, thread_id) VALUES (%d, %d, %d)', $inbox_tag, $row->uid, $row->thread_id);
}
}
$sandbox['current_thread_id'] = max($threads);
}
// Set #finished based on sandbox.
$ret['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['current_thread_id'] / $sandbox['max'];
return $ret;
}
/**
* Add new indexes to existing 6.x-2.x installations.
*/
function privatemsg_filter_update_6202() {
$ret = array();
if (!privatemsg_db_index_exists('pm_tags', 'tag_list')) {
db_add_index($ret, 'pm_tags', 'tag_list', array(
'tag_id',
'tag',
'public',
));
}
if (privatemsg_db_index_exists('pm_tags_index', 'uid')) {
db_drop_index($ret, 'pm_tags_index', 'uid');
}
if (!privatemsg_db_index_exists('pm_tags_index', 'thread_tags')) {
db_add_index($ret, 'pm_tags_index', 'thread_tags', array(
'uid',
'thread_id',
));
}
return $ret;
}
Functions
Name | Description |
---|---|
privatemsg_filter_enable | Implements hook_enable(). |
privatemsg_filter_install | Implements hook_install(). |
privatemsg_filter_schema | Implements hook_schema(). |
privatemsg_filter_uninstall | Implements hook_uninstall(). |
privatemsg_filter_update_6001 | |
privatemsg_filter_update_6002 | |
privatemsg_filter_update_6200 | Add hidden flag and create inbox tag. |
privatemsg_filter_update_6201 | Add inbox tag to existing inbox messages. |
privatemsg_filter_update_6202 | Add new indexes to existing 6.x-2.x installations. |