You are here

messaging.install in Messaging 6.4

File

messaging.install
View source
<?php

/**
* Implementation of hook_schema().
*/
function messaging_schema() {
  $schema['messaging_store'] = array(
    'description' => 'Stores queued messages to be sent or sent messages as logs.',
    'fields' => array(
      'mqid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Unique message id.',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {user}.uid for destination if it is a unique destination.',
      ),
      'sender' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {user}.uid who sent the message if any.',
      ),
      'method' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Messaging send method key.',
      ),
      'language' => array(
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Language code.',
      ),
      'destination' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Destination identifier, it may be an email address.',
      ),
      'subject' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Message subject, single text line.',
      ),
      'body' => array(
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
        'description' => 'Message body, multiple text line.',
      ),
      'params' => array(
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'Additional serialized parameters.',
      ),
      'created' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Unix timestamp, when the message was created/stored.',
      ),
      'sent' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Unix timestamp, when the message was sent.',
      ),
      'cron' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Will be 1 if row marked for processing on cron.',
      ),
      'queue' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Will be 1 if this is a queued message.',
      ),
      'log' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Will be 1 if this is a message log.',
      ),
      'error' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Error code.',
      ),
      'data' => array(
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'Additional serialized parameters.',
      ),
    ),
    'primary key' => array(
      'mqid',
    ),
    'indexes' => array(
      'cron' => array(
        'cron',
      ),
      'queue' => array(
        'queue',
      ),
      'log' => array(
        'log',
      ),
    ),
  );
  $schema['messaging_destination'] = array(
    'description' => 'Stores message destination for users and non users.',
    'fields' => array(
      'mdid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Unique destination id.',
      ),
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {user}.uid for destination if it belongs to a registered user.',
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 60,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Destination type: mail, xmpp, etc.',
      ),
      'address' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Destination identifier, it may be an email address.',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
        'size' => 'tiny',
        'description' => 'Destination status: 1= active, 0 = inactive.',
      ),
      'sent' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Unix timestamp, when the last message was sent to this address.',
      ),
    ),
    'primary key' => array(
      'mdid',
    ),
    'indexes' => array(
      'uid' => array(
        'uid',
      ),
      'address' => array(
        'address',
      ),
    ),
  );
  return $schema;
}

/**
* Implementation of hook_install().
*/
function messaging_install() {

  // Create tables.
  drupal_install_schema('messaging');
}

/**
* Implementation of hook_uninstall().
*/
function messaging_uninstall() {

  // Remove tables.
  drupal_uninstall_schema('messaging');

  // Remove the default filter, plain text added in install
  if ($format = variable_get('messaging_default_filter', 0)) {
    db_query('DELETE FROM {filters} WHERE format = %d', $format);
    db_query('DELETE FROM {filter_formats} WHERE format = %d', $format);
  }

  // Remove variables
  variable_del('messaging_debug');
  variable_del('messaging_default_filter');
  variable_del('messaging_default_method');
  variable_del('messaging_log');
  variable_del('messaging_process_limit');
  db_query("DELETE FROM {variable} WHERE name LIKE 'messaging_method_%'");
}

/**
 * Disable a sending method and return an alternative one
 */
function messaging_update_method_disable($method, $replace) {

  // Check that we are not getting rid of the default method
  if ($method == messaging_method_default()) {
    variable_set('messaging_default_method', $replace);
  }
  messaging_update_method_update($method, $replace);
  return $replace;
}

/**
 * Find a suitable replacement for a sending method
 * 
 * @param $method
 *   Method key, the one to find a replacement for
 * @param $default
 *   Whether to try default remaining method if we cannot find one of the same type
 */
function messaging_update_method_replace($method, $default = TRUE) {
  $replace = NULL;

  // Find an alternative one within the same group, i.e. 'mail'
  if ($method_group = messaging_method_info($method, 'group')) {
    foreach (messaging_method_info(NULL, 'group') as $index => $group) {
      if ($group == $method_group && $method != $index) {
        $replace = $index;
        break;
      }
    }
  }

  // If still not replacement, go for the default
  if (empty($replace) && $default) {
    if ($method == messaging_method_default()) {
      $info = messaging_method_info();
      unset($info[$method]);
      $replace = key($info);
    }
    else {
      $replace = messaging_method_default();
    }
  }
  return $replace;
}

/**
 * Udate sending method, change for a new one
 */
function messaging_update_method_update($old, $new) {

  // Replace some variables
  if ($old == variable_get('messaging_default_method', NULL)) {
    if ($new) {
      variable_set('messaging_default_method', $new);
    }
    else {
      variable_del('messaging_default_method');
    }
  }

  // Create new destinations if needed for users that had an old one, but only if the address type is different
  // It is possible to create new bulk destinations when we know the field and the table it comes from
  if ($new) {
    $old_type = messaging_method_info($old, 'address_type');
    $new_type = messaging_method_info($new, 'address_type');
    if ($old_type && $new_type && $old_type != $new_type && ($field = messaging_address_info($new_type, 'field_name')) && ($table = messaging_address_info($new_type, 'field_table'))) {
      $sql = "INSERT INTO {messaging_destination} (uid, type, address)";
      if ($new_type == 'user') {

        // Special case, destination is uid, it is much more straight forward
        $sql .= " SELECT d1.uid, '%s', d1.uid FROM {messaging_destination} d1";
      }
      else {
        $sql .= " SELECT d1.uid, '%s', t.{$field} FROM {messaging_destination} d1";
        $sql .= " INNER JOIN {" . $table . "} t ON d1.uid = t.uid";

        // Have destination data on the other table
      }

      // Don't have already a destination of this type ($new_type)
      $sql .= " LEFT JOIN {messaging_destination} d2 ON d1.uid = d2.uid AND d2.type = '%s'";
      $sql .= " WHERE d1.uid > 0 AND d1.type = '%s' AND d2.uid IS NULL";
      db_query($sql, $new_type, $new_type, $old_type);
      if ($created = db_affected_rows()) {
        drupal_set_message(t("Created @count user destinations for new sending method.", array(
          '@count' => $created,
        )));
      }
    }
  }

  // Inform all modules this method is being replaced
  module_invoke_all('messaging', 'method update', $old, $new);

  // Delete all stored messages for this method
  messaging_store()
    ->delete_multiple(array(
    'method' => $old,
  ));
}

/**
 * Update method settings
 */
function messaging_update_1() {
  $ret = array();
  if ($settings = variable_get('messaging_methods', array())) {
    foreach ($settings as $key => $info) {
      $info['subject_filter'] = $info['filter'];
      variable_set('messaging_method_' . $key, $info);
      $ret[] = array();
    }
    drupal_set_message('Your messaging methods have been updated. Please review the messaging settings.');
  }
  return $ret;
}

/**
 * Create queue storage
 */
function messaging_update_2() {
  $ret = array();
  drupal_install_schema('messaging_store');
  return $ret;
}

/**
 * Updates for Drupal 6 version. Not needed anymore
 */
function messaging_update_6001() {
  $ret = array();
  return $ret;
}

/**
 * Update sending methods names
 */
function messaging_update_6002() {
  $ret = array();
  module_load_all();
  if (module_exists('messaging_phpmailer')) {
    $replace['html_mail'] = 'phpmailer';
  }
  if (module_exists('messaging_mime_mail') && !module_exists('messaging_mail')) {
    $replace['mail'] = 'mimemail';
  }
  if (!empty($replace)) {
    foreach ($replace as $old => $new) {
      if ($settings = variable_get('messaging_method_' . $old, NULL)) {
        variable_set('messaging_method_' . $new, $settings);
        variable_del('messaging_method_' . $old);
      }
      messaging_update_method_update($old, $new);
      $ret[] = array(
        'success' => TRUE,
        'query' => "Replaced sending method {$old} by {$new}",
      );
    }
    drupal_set_message('Please, check all your messaging settings for sending methods.');
  }
  return $ret;
}

/**
 * Update schema field
 */
function messaging_update_6003() {
  $ret = array();
  db_change_field($ret, 'messaging_store', 'params', 'params', array(
    'type' => 'text',
    'not null' => TRUE,
    'size' => 'big',
    'serialize' => TRUE,
  ));
  return $ret;
}

/**
 * update messaging store to add in indexes
 */
function messaging_update_6004() {
  $ret = array();
  db_add_index($ret, 'messaging_store', 'cron', array(
    'cron',
  ));
  db_add_index($ret, 'messaging_store', 'queue', array(
    'queue',
  ));
  db_add_index($ret, 'messaging_store', 'log', array(
    'log',
  ));
  return $ret;
}

/**
 * Clean up message logs that were not properly cleaned before
 */
function messaging_update_6005() {
  $ret = array();
  $ret[] = update_sql('UPDATE {messaging_store} SET log = 1 WHERE log > 1');
  $ret[] = update_sql('DELETE FROM {messaging_store} WHERE log = 1 AND queue = 0 AND sent < %d', time() - variable_get('messaging_log', 0));
  return $ret;
}

/**
 * Create destination table
 */
function messaging_update_6006() {
  $ret = array();
  $table = drupal_get_schema('messaging_destination', TRUE);
  db_create_table($ret, 'messaging_destination', $table);

  // Mark this update so we know we don't have to update this table
  variable_set('messaging_update_6006', 1);
  return $ret;
}

/**
 * Add fields to messaging_store table
 */
function messaging_update_6007() {
  $ret = array();
  db_add_field($ret, 'messaging_store', 'data', array(
    'type' => 'text',
    'not null' => TRUE,
    'size' => 'big',
    'serialize' => TRUE,
  ));
  db_add_field($ret, 'messaging_store', 'error', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
  ));
  db_add_field($ret, 'messaging_store', 'language', array(
    'type' => 'varchar',
    'length' => 12,
    'not null' => TRUE,
    'default' => '',
  ));
  return $ret;
}

/**
 * Update method filter settings
 */
function messaging_update_6008() {
  messaging_include('text.inc');
  foreach (module_invoke_all('messaging', 'send methods') as $method => $info) {
    $properties = variable_get('messaging_method_' . $method, array()) + array(
      'name' => $info['name'],
    );
    $properties['filter'] = isset($info['filter']) ? $info['filter'] : 'messaging_plaintext';
    variable_set('messaging_filters_' . $method, $properties);
  }
  variable_del('messaging_default_filter');
  return array();
}

/**
 * Drop unique key for method, address and update table
 */
function messaging_update_6009() {
  $ret = array();
  if (!variable_get('messaging_update_6006', 0)) {

    // Depending on the upgrade path, we may have 'destination' key created or not. Skip error if not
    @db_drop_unique_key($ret, 'messaging_destination', 'destination');
  }
  return $ret;
}

/**
 * Update destination type
 */
function messaging_update_6010() {
  $ret = array();
  if (!variable_get('messaging_update_6006', 0) && db_column_exists('messaging_destination', 'method')) {
    foreach (messaging_method_info() as $method => $info) {
      $type = isset($info['address_type']) ? $info['address_type'] : $info['group'];

      // Best guess, will work for most cases
      $ret[] = update_sql("UPDATE {messaging_destination} SET type = '{$type}' WHERE method = '{$method}'");
    }
  }
  return $ret;
}

/**
 * Delete duplicated destinations
 */
function messaging_update_6011() {
  return _messaging_update_duplicate_destinations();
}

/**
 * Fix template_parts table, may be missing new field depending on update path
 */
function messaging_update_6012() {
  $ret = array();
  if (db_table_exists('messaging_message_parts') && !db_column_exists('messaging_message_parts', 'language')) {
    module_load_install('messaging_template');
    $ret = messaging_template_update_6001();
  }
  return $ret;
}

/**
 * Fix template_parts table, may be missing new field depending on update path
 */
function messaging_update_6013() {
  $ret = array();
  if (db_table_exists('messaging_message_parts') && !db_column_exists('messaging_message_parts', 'format')) {
    module_load_install('messaging_template');
    $ret = messaging_template_update_6002();
  }
  return $ret;
}

/**
 * Flush caches once more, new classes added after Beta 4
 */
function messaging_update_6014() {
  $ret = array();

  // Clear all caches for the new classes to work with autoload, new theme functions, etc..
  drupal_flush_all_caches();
  return $ret;
}

/**
 * Reusable update, delete duplicate destinations
 */
function _messaging_update_duplicate_destinations() {
  $ret = array();

  // Mark all rows that have a previous with the same type, address
  $ret[] = update_sql("UPDATE {messaging_destination} d INNER JOIN {messaging_destination} d2 ON d.uid = d2.uid AND d.address = d2.address AND d.type = d2.type AND d.mdid > d2.mdid SET d.type = ''");

  // Now delete marked rows
  $ret[] = update_sql("DELETE FROM messaging_destination WHERE type = ''");
  return $ret;
}

// Pending cache refresh (new classes for autoload)
// Pending, preserve data for now: db_drop_field($ret, 'messaging_destination', 'method');

Functions

Namesort descending Description
messaging_install Implementation of hook_install().
messaging_schema Implementation of hook_schema().
messaging_uninstall Implementation of hook_uninstall().
messaging_update_1 Update method settings
messaging_update_2 Create queue storage
messaging_update_6001 Updates for Drupal 6 version. Not needed anymore
messaging_update_6002 Update sending methods names
messaging_update_6003 Update schema field
messaging_update_6004 update messaging store to add in indexes
messaging_update_6005 Clean up message logs that were not properly cleaned before
messaging_update_6006 Create destination table
messaging_update_6007 Add fields to messaging_store table
messaging_update_6008 Update method filter settings
messaging_update_6009 Drop unique key for method, address and update table
messaging_update_6010 Update destination type
messaging_update_6011 Delete duplicated destinations
messaging_update_6012 Fix template_parts table, may be missing new field depending on update path
messaging_update_6013 Fix template_parts table, may be missing new field depending on update path
messaging_update_6014 Flush caches once more, new classes added after Beta 4
messaging_update_method_disable Disable a sending method and return an alternative one
messaging_update_method_replace Find a suitable replacement for a sending method
messaging_update_method_update Udate sending method, change for a new one
_messaging_update_duplicate_destinations Reusable update, delete duplicate destinations