You are here

twitter.install in Twitter 6.2

File

twitter.install
View source
<?php

/**
 * Implementation of hook_schema().
 */
function twitter_schema() {
  $schema['twitter'] = array(
    'description' => t("Stores individual Twitter posts."),
    'fields' => array(
      'twitter_id' => array(
        'description' => t("Unique identifier for each {twitter} post."),
        'type' => 'numeric',
        'unsigned' => TRUE,
        'precision' => 20,
        'scale' => 0,
        'not null' => TRUE,
        'default' => 0,
      ),
      'screen_name' => array(
        'description' => t("Screen name of the {twitter} user."),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created_at' => array(
        'description' => t("Date and time the {twitter} post was created."),
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      'created_time' => array(
        'description' => t("A duplicate of {twitter}.created_at in UNIX timestamp format."),
        'type' => 'int',
        'not null' => TRUE,
      ),
      'text' => array(
        'description' => t("The text of the {twitter} post."),
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
      'source' => array(
        'description' => t("The application that created the {twitter} post."),
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
      'in_reply_to_status_id' => array(
        'description' => t("Unique identifier of a status this {twitter} post was replying to."),
        'type' => 'numeric',
        'unsigned' => TRUE,
        'precision' => 20,
        'scale' => 0,
        'not null' => FALSE,
      ),
      'in_reply_to_user_id' => array(
        'description' => t("Unique identifier for the {twitter_account} this post was replying to."),
        'type' => 'numeric',
        'unsigned' => TRUE,
        'precision' => 20,
        'scale' => 0,
        'not null' => FALSE,
      ),
      'in_reply_to_screen_name' => array(
        'description' => t("Screen name of the {twitter} user this post was replying to."),
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
      'truncated' => array(
        'description' => t("Boolean flag indicating whether the {twitter} status was cut off to fit in the 140 character limit."),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'screen_name' => array(
        'screen_name',
      ),
    ),
    'primary key' => array(
      'twitter_id',
    ),
  );
  $schema['twitter_account'] = array(
    'description' => t("Stores information on specific Twitter user accounts."),
    'fields' => array(
      'twitter_uid' => array(
        'description' => t("The unique identifier of the {twitter_account}."),
        'type' => 'numeric',
        'unsigned' => TRUE,
        'precision' => 20,
        'scale' => 0,
        'not null' => TRUE,
        'default' => 0,
      ),
      'screen_name' => array(
        'description' => t("The unique login name of the {twitter_account} user."),
        'type' => 'varchar',
        'length' => 255,
      ),
      'name' => array(
        'description' => t("The full name of the {twitter_account} user."),
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => t("The description/biography associated with the {twitter_account}."),
        'type' => 'varchar',
        'length' => 255,
      ),
      'location' => array(
        'description' => t("The location of the {twitter_account}'s owner."),
        'type' => 'varchar',
        'length' => 255,
      ),
      'followers_count' => array(
        'description' => t("The number of users following this {twitter_account}."),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'profile_image_url' => array(
        'description' => t("The url of the {twitter_account}'s profile image."),
        'type' => 'varchar',
        'length' => 255,
      ),
      'url' => array(
        'description' => t("The url of the {twitter_account}'s home page."),
        'type' => 'varchar',
        'length' => 255,
      ),
      'protected' => array(
        'description' => t("Boolean flag indicating whether the {twitter_account}'s posts are publicly accessible."),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'last_refresh' => array(
        'description' => t("A UNIX timestamp marking the date Twitter statuses were last fetched on."),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'screen_name' => array(
        'screen_name',
      ),
    ),
    'primary key' => array(
      'twitter_uid',
    ),
  );
  $schema['twitter_user'] = array(
    'fields' => array(
      'uid' => array(
        'description' => t("The Drupal ID of the user account associated with the Twitter account."),
        'type' => 'int',
        'not null' => TRUE,
      ),
      'screen_name' => array(
        'description' => t("The unique login name for the Twitter account."),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'password' => array(
        'description' => t("The password for the Twitter account."),
        'type' => 'varchar',
        'length' => 64,
      ),
      'import' => array(
        'description' => t("Boolean flag indicating whether the {twitter_user}'s posts should be pulled in by the site."),
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 1,
      ),
    ),
    'primary key' => array(
      'uid',
      'screen_name',
    ),
    'indexes' => array(
      'screen_name' => array(
        'screen_name',
      ),
      'uid' => array(
        'uid',
      ),
      'import' => array(
        'import',
      ),
    ),
  );
  return $schema;
}

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

  // Create tables.
  drupal_install_schema('twitter');

  // Set the weight to 3, making it heavier than Pathauto.
  db_query("UPDATE {system} SET weight = 3 WHERE name = 'twitter'");
}

/**
 * Previous versions of the Twitter module had no database schema.
 * We're safe just running the basic install for update_1.
 */
function twitter_update_6000() {
  twitter_install();
}

/**
 * Adding a handful of additional flags on accounts, and saving more metadata
 * when Twitter sends it to us.
 */
function twitter_update_6001() {
  $ret = array();
  $attributes = array(
    'description' => t("Boolean flag indicating whether the {twitter_user}'s posts should be pulled in by the site."),
    'unsigned' => TRUE,
    'default' => 1,
    'not null' => TRUE,
  );
  db_add_column($ret, 'twitter_user', 'import', 'int', $attributes);
  $attributes = array(
    'description' => t("The location of the {twitter_account}'s owner."),
    'length' => 255,
  );
  db_add_column($ret, 'twitter_account', 'location', 'varchar(255)', $attributes);
  $attributes = array(
    'description' => t("The number of users following this {twitter_account}."),
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  db_add_column($ret, 'twitter_account', 'followers_count', 'int', $attributes);
  return $ret;
}

/**
 * Set the weight a little heavier to allow Pathauto and other modules to do
 * their work on the title, path alias, etc. before the twitter post is sent.
 */
function twitter_update_6002() {
  $ret = array();
  $ret[] = update_sql("UPDATE {system} SET weight = 3 WHERE name = 'twitter'");
  return $ret;
}

/**
 * Twitter status IDs are hitting the rollover point for signed ints. Let's
 * be sure we're ready. See http://bit.ly/kokvi for details.
 */
function twitter_update_6003() {
  $ret = array();
  db_drop_primary_key($ret, 'twitter');
  db_change_field($ret, 'twitter', 'twitter_id', 'twitter_id', array(
    'description' => t("Unique identifier for each {twitter} post."),
    'type' => 'int',
    'size' => 'big',
    'unsigned' => 'true',
    'not null' => TRUE,
  ), array(
    'primary key' => array(
      'twitter_id',
    ),
  ));
  return $ret;
}

/**
 * Add NOT NULL constraint and DEFAULT value to the screen_name field of the 
 * twitter and twitter_user tables per updated schema definition.
 *
 * See http://drupal.org/node/336048 and http://drupal.org/node/430442
 */
function twitter_update_6004() {
  $ret = array();

  // Have to drop PRIMARY KEY and indexes that use the field being changed.
  // twitter
  db_drop_index($ret, 'twitter', 'screen_name');
  db_change_field($ret, 'twitter', 'screen_name', 'screen_name', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ), array(
    'indexes' => array(
      'screen_name' => array(
        'screen_name',
      ),
    ),
  ));

  // twitter_user
  db_drop_index($ret, 'twitter_user', 'screen_name');
  db_drop_primary_key($ret, 'twitter_user');
  db_change_field($ret, 'twitter_user', 'screen_name', 'screen_name', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ), array(
    'primary key' => array(
      'uid',
      'screen_name',
    ),
    'indexes' => array(
      'screen_name' => array(
        'screen_name',
      ),
    ),
  ));
  return $ret;
}

/**
 * Dear PHP, I hate you so bad. Love, Jeff.
 *
 * Workaround for drupal_write_record(), which treats a DB bigint as a signed 32
 * bit int on 32 bit PHP builds. We can STORE bigints, and PHP automatically
 * converts them to floats in memory while we work with them, but db_placeholders()
 * always treats them as %d and casts them back to dumb signed ints.
 *
 * See http://drupal.org/node/333788 for more info.
 *
 * Instead we'll set the column type to 'string' which is a little like jumping
 * off the Sears Tower because the elevator's broken. But that's life.
 */
function twitter_update_6005() {
  $ret = array();

  // First clear out any borked statuses.
  $ret[] = update_sql("DELETE FROM {twitter} WHERE twitter_id = 2147483647");
  db_drop_primary_key($ret, 'twitter');
  db_change_field($ret, 'twitter', 'twitter_id', 'twitter_id', array(
    'description' => t("Unique identifier for each {twitter} post."),
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ), array(
    'primary key' => array(
      'twitter_id',
    ),
  ));
  return $ret;
}

/**
 * Dear PHP, I'm sorry baby. I know I said a lotta things last night, but I was
 * angry. I didn't mean it, you know that. I'll use numbers from now on -- no!
 * No, I'll use *decimals*. You'll like that, won't you, PHP? We're cool, right?
 *
 * Yeah. Yeah, we're cool.
 *
 * Also, while we're at it we'll add support for some of the additional Twitter
 * API data for replies, and convert the twitter_uid field to a numeric type, too.
 */
function twitter_update_6006() {
  $ret = array();
  db_drop_primary_key($ret, 'twitter');
  db_change_field($ret, 'twitter', 'twitter_id', 'twitter_id', array(
    'description' => t("Unique identifier for each {twitter} post."),
    'type' => 'numeric',
    'unsigned' => TRUE,
    'precision' => 20,
    'scale' => 0,
    'not null' => TRUE,
    'default' => 0,
  ), array(
    'primary key' => array(
      'twitter_id',
    ),
  ));
  db_add_field($ret, 'twitter', 'in_reply_to_status_id', array(
    'description' => t("Unique identifier of a status this {twitter} post was replying to."),
    'type' => 'numeric',
    'precision' => 20,
    'scale' => 0,
    'unsigned' => TRUE,
    'not null' => FALSE,
  ));
  db_add_field($ret, 'twitter', 'in_reply_to_user_id', array(
    'description' => t("Unique identifier for the {twitter_account} this post was replying to."),
    'type' => 'numeric',
    'precision' => 20,
    'scale' => 0,
    'unsigned' => TRUE,
    'not null' => FALSE,
  ));
  db_add_field($ret, 'twitter', 'in_reply_to_screen_name', array(
    'description' => t("Screen name of the {twitter} user this post was replying to."),
    'type' => 'varchar',
    'length' => 255,
    'not null' => FALSE,
  ));
  db_add_field($ret, 'twitter', 'truncated', array(
    'description' => t("Boolean flag indicating whether the {twitter} status was cut off to fit in the 140 character limit."),
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  ));
  db_drop_primary_key($ret, 'twitter_account');
  db_change_field($ret, 'twitter_account', 'twitter_uid', 'twitter_uid', array(
    'description' => t("The unique identifier of the {twitter_account}."),
    'type' => 'numeric',
    'unsigned' => TRUE,
    'precision' => 20,
    'scale' => 0,
    'not null' => TRUE,
    'default' => 0,
  ), array(
    'primary key' => array(
      'twitter_uid',
    ),
  ));
  return $ret;
}

/*
 * Update the last_refresh column to supply a default value. http://drupal.org/node/301317
 */
function twitter_update_6007() {
  $ret = array();
  db_change_field($ret, 'twitter_account', 'last_refresh', 'last_refresh', array(
    'description' => t("A UNIX timestamp marking the date Twitter statuses were last fetched on."),
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  ));
  return $ret;
}

/*
 * Replace default Tweet settings with actual tokens, since we're all tokeny and stuff now.
 * Also update any old actions.
 */
function twitter_update_6208() {
  $ret = array();
  $replacements = array(
    '!title' => '[title]',
    '!url' => '[url]',
    '!url-alias' => '[url-alias]',
    '!user' => '[author-name]',
    '!tinyurl' => '[shorturl]',
  );
  $var = variable_get('twitter_default_format', 'New post: !title !tinyurl');
  variable_set('twitter_default_format', strtr($var, $replacements));
  $ret[] = array(
    'success' => TRUE,
    'query' => t('Updated Twitter default text to use Tokens.'),
  );
  $replacements = array(
    '%site_name' => '[site-name]',
    '%username' => '[author-name]',
    '%node_url' => '[url]',
    '%node_type' => '[type]',
    '%title' => '[title]',
  );
  $results = db_query("SELECT aid, parameters FROM {actions} WHERE callback = 'twitter_actions_set_status_action'");
  while ($action = db_fetch_object($results)) {
    $params = unserialize($action->parameters);
    foreach ($params as $key => $value) {
      $params[$key] = strtr($value, $replacements);
    }
    $action->parameters = serialize($params);
    db_query("UPDATE {actions} SET parameters = '%s' WHERE aid = '%s'", $action->aid, $action->parameters);
    $ret[] = array(
      'success' => TRUE,
      'query' => t('Updated action %aid to use Tokens.', array(
        '%aid' => $action->aid,
      )),
    );
  }
  return $ret;
}
function twitter_uninstall() {

  // Remove tables.
  drupal_uninstall_schema('twitter');
}

Functions

Namesort descending Description
twitter_install Implementation of hook_install().
twitter_schema Implementation of hook_schema().
twitter_uninstall
twitter_update_6000 Previous versions of the Twitter module had no database schema. We're safe just running the basic install for update_1.
twitter_update_6001 Adding a handful of additional flags on accounts, and saving more metadata when Twitter sends it to us.
twitter_update_6002 Set the weight a little heavier to allow Pathauto and other modules to do their work on the title, path alias, etc. before the twitter post is sent.
twitter_update_6003 Twitter status IDs are hitting the rollover point for signed ints. Let's be sure we're ready. See http://bit.ly/kokvi for details.
twitter_update_6004 Add NOT NULL constraint and DEFAULT value to the screen_name field of the twitter and twitter_user tables per updated schema definition.
twitter_update_6005 Dear PHP, I hate you so bad. Love, Jeff.
twitter_update_6006 Dear PHP, I'm sorry baby. I know I said a lotta things last night, but I was angry. I didn't mean it, you know that. I'll use numbers from now on -- no! No, I'll use *decimals*. You'll like that, won't you, PHP?…
twitter_update_6007
twitter_update_6208