You are here

quotes.install in Quotes 5

Same filename and directory in other branches
  1. 6 quotes.install
  2. 7 quotes.install

Handles installation and updates for the quotes module.

@copyright Copyright (c) 2003-2007 Jim Riggs. All rights reserved. @author Jim Riggs <drupal at jim and lissa dot com>

File

quotes.install
View source
<?php

/**
 * @file
 *   Handles installation and updates for the quotes module.
 *
 * @copyright Copyright (c) 2003-2007 Jim Riggs.  All rights reserved.
 * @author Jim Riggs <drupal at jim and lissa dot com>
 */

//********************************************************************

//* Drupal Hooks

//********************************************************************

/**
 * Implementation of hook_install().
 */
function quotes_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query('CREATE TABLE {quotes} (
          nid INTEGER UNSIGNED NOT NULL,
          vid INTEGER UNSIGNED NOT NULL PRIMARY KEY,
          aid INTEGER UNSIGNED NOT NULL,
          citation TEXT,
          promote INTEGER UNSIGNED NOT NULL DEFAULT 0
        ) /*!40100 DEFAULT CHARACTER SET utf8 */');
      db_query('CREATE INDEX {quotes}_nid ON {quotes} (nid)');
      db_query('CREATE INDEX {quotes}_promote ON {quotes} (promote)');
      db_query('CREATE INDEX {quotes}_aid ON {quotes} (aid)');
      db_query('CREATE TABLE {quotes_blocks} (
          bid INTEGER NOT NULL PRIMARY KEY,
          block_type INTEGER NOT NULL,
          cron_interval INTEGER NOT NULL,
          cron_step INTEGER NOT NULL,
          cron_last INTEGER NOT NULL,
          vid INTEGER NOT NULL,
          count INTEGER NOT NULL DEFAULT 1,
          show_titles INTEGER NOT NULL DEFAULT 0,
          show_citation INTEGER NOT NULL DEFAULT 0,
          max_length INTEGER NOT NULL DEFAULT 0,
          view_weight INTEGER NOT NULL DEFAULT 1,
          rand_freq INTEGER NOT NULL DEFAULT 100,
          name VARCHAR(255) NOT NULL UNIQUE,
          nid_filter TEXT NOT NULL,
          aid_filter TEXT NOT NULL,
          rid_filter TEXT NOT NULL,
          uid_filter TEXT NOT NULL,
          tid_filter TEXT NOT NULL,
          view_text VARCHAR(64),
          more_text VARCHAR(64)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */');
      db_query('CREATE TABLE {quotes_authors} (
          aid INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
          name TEXT NOT NULL,
          bio TEXT NULL,
          INDEX (name (255))
        ) /*!40100 DEFAULT CHARACTER SET utf8 */');
      break;
    case 'pgsql':
      db_query('CREATE TABLE {quotes} (
          nid INTEGER NOT NULL,
          vid INTEGER NOT NULL,
          aid INTEGER NOT NULL,
          citation TEXT,
          promote INTEGER NOT NULL,
          PRIMARY KEY(vid)
      )');
      db_query('CREATE INDEX {quotes}_nid_idx ON {quotes} (nid)');
      db_query('CREATE INDEX {quotes}_promote_idx ON {quotes} (promote)');
      db_query('CREATE TABLE {quotes_blocks} (
          bid INTEGER NOT NULL,
          block_type INTEGER NOT NULL,
          vid INTEGER NOT NULL,
          cron_interval INTEGER NOT NULL,
          cron_step INTEGER NOT NULL,
          cron_last INTEGER NOT NULL,
          count INTEGER NOT NULL DEFAULT 1,
          show_titles INTEGER NOT NULL DEFAULT 0,
          show_citation INTEGER NOT NULL DEFAULT 0,
          max_length INTEGER NOT NULL DEFAULT 0,
          view_weight INTEGER NOT NULL DEFAULT 1,
          rand_freq INTEGER NOT NULL DEFAULT 100,
          name VARCHAR(255) NOT NULL,
          nid_filter TEXT NOT NULL,
          aid_filter TEXT NOT NULL,
          rid_filter TEXT NOT NULL,
          uid_filter TEXT NOT NULL,
          tid_filter TEXT NOT NULL,
          view_text VARCHAR(64),
          more_text VARCHAR(64),
          PRIMARY KEY(bid),
          UNIQUE(name))');
      db_query('CREATE TABLE {quotes_authors} (
          aid SERIAL,
          name TEXT NOT NULL,
          bio TEXT NULL,
          PRIMARY KEY(aid),
          UNIQUE(name))');
      db_query('CREATE INDEX {quotes_authors}_name_idx ON {quotes_authors} (name)');
  }
}

/**
 * Implementation of hook_uninstall().
 */
function quotes_uninstall() {
  variable_del('quotes_author_bio');
  variable_del('quotes_author_link');
  variable_del('quotes_block_citation');
  variable_del('quotes_block_view_text');
  variable_del('quotes_block_view_weight');
  variable_del('quotes_leader');
  variable_del('quotes_node_view_link');
  variable_del('quotes_node_view_weight');
  variable_del('quotes_per_page');
  variable_del('quotes_show_myquotes');
  variable_del('quotes_showlink');
  variable_del('quotes_user_recent');
  db_query("DELETE FROM {variable} WHERE name LIKE ('quotes_more_%')");

  // Remove all Quotes nodes.
  $result = db_query("SELECT nid FROM {node} WHERE type = 'quotes'");
  while ($obj = db_fetch_object($result)) {
    node_delete($obj->nid);
  }
  db_query('DROP TABLE {quotes}');
  db_query('DROP TABLE {quotes_blocks}');
  db_query('DROP TABLE {quotes_authors}');
  db_query("DELETE FROM {blocks} WHERE module='quotes'");
  if ($_GLOBALS['db_type'] == 'pgsql') {
    db_query('DROP SEQUENCE {quotes}_bid_seq');
  }
}

/**
 * Implementation of hook_update_N().
 * Updates existing tables to UTF-8.
 */
function quotes_update_1() {
  return _system_update_utf8(array(
    'quotes',
    'quotes_blocks',
  ));
}

/**
 * Implementation of hook_update_N().
 * Adds vid column to quotes table.
 */
function quotes_update_2() {
  global $db_type;
  $items = array();
  $items[] = update_sql('ALTER TABLE {quotes} ADD vid INTEGER NOT NULL');
  $items[] = update_sql('UPDATE {quotes} SET vid = nid');
  if ($db_type == 'pgsql') {
    $items[] = update_sql('ALTER TABLE {quotes} DROP CONSTRAINT {quotes}_pkey');
  }
  else {
    $items[] = update_sql('ALTER TABLE {quotes} DROP PRIMARY KEY');
  }
  $items[] = update_sql('ALTER TABLE {quotes} ADD PRIMARY KEY (nid, vid)');
  return $items;
}

/**
 * Implementation of hook_update_N().
 * Fixes the primary key on the quotes table, adds indexes on the nid
 * and promote columns, and deletes the quotes_version variable.
 */
function quotes_update_3() {
  global $db_type;
  $items = array();
  if ($db_type == 'pgsql') {
    $items[] = update_sql('ALTER TABLE {quotes} DROP CONSTRAINT {quotes}_pkey');
  }
  else {
    $items[] = update_sql('ALTER TABLE {quotes} DROP PRIMARY KEY');
  }
  $items[] = update_sql('ALTER TABLE {quotes} ADD PRIMARY KEY (vid)');
  $items[] = update_sql('CREATE INDEX {quotes}_nid ON {quotes} (nid)');
  $items[] = update_sql('CREATE INDEX {quotes}_promote ON {quotes} (promote)');
  variable_del('quotes_version');
  return $items;
}

/**
 * Implementation of hook_update_N().
 * Removes the title column and adds the additional fields to
 * quotes_blocks to support cron.
 */
function quotes_update_4() {
  global $db_type;
  $items = array();
  $items[] = update_sql('ALTER TABLE {quotes} ADD COLUMN title');
  return $items;
}

/**
 * Implementation of hook_update_N().
 * Add citation column to quotes table
 */
function quotes_update_5() {
  global $db_type;
  $items = array();
  $items[] = update_sql('ALTER TABLE {quotes} ADD citation VARCHAR(255)');
  return $items;
}

/**
 * Implementation of hook_update_N().
 * Add count column to quotes_block table
 */
function quotes_update_6() {
  global $db_type;
  $items = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $items[] = update_sql("ALTER TABLE {quotes_blocks} ADD COLUMN count INTEGER NOT NULL DEFAULT 1");
      $items[] = update_sql("ALTER TABLE {quotes_blocks} ADD COLUMN show_titles INTEGER NOT NULL DEFAULT 0");
      break;
    case 'pgsql':
      $items[] = update_sql("ALTER TABLE {quotes_blocks} ADD COLUMN count INTEGER");
      $items[] = update_sql("UPDATE {quotes_blocks} SET count=0 WHERE count IS NULL");
      $items[] = update_sql("ALTER TABLE {quotes_blocks} ALTER COLUMN count SET NOT NULL");
      $items[] = update_sql("ALTER TABLE {quotes_blocks} ALTER COLUMN count SET DEFAULT 1");
      $items[] = update_sql("ALTER TABLE {quotes_blocks} ADD COLUMN show_titles INTEGER");
      $items[] = update_sql("UPDATE {quotes_blocks} SET show_titles=0 WHERE show_titles IS NULL");
      $items[] = update_sql("ALTER TABLE {quotes_blocks} ALTER COLUMN show_titles SET NOT NULL");
      $items[] = update_sql("ALTER TABLE {quotes_blocks} ALTER COLUMN show_titles SET DEFAULT 1");
      break;
  }
  return $items;
}

/**
 * Implementation of hook_update_N().
 * Convert authors to separate table.
 */
function quotes_update_7() {
  global $db_type;
  $items = $create = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $create[] = 'CREATE TABLE {quotes_authors} (
          aid INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
          name VARCHAR(255) NOT NULL,
          bio TEXT NULL,
          UNIQUE (`name`)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */';
      break;
    case 'pgsql':
      $create[] = 'CREATE TABLE {quotes_authors} (
          aid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
          name VARCHAR(255) NOT NULL,
          bio TEXT NULL,
          UNIQUE (name)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */';
      db_query('CREATE INDEX {quotes_authors}_name ON {quotes_authors} (name)');
  }
  foreach ($create as $query) {
    $result = db_query($query);
    $items[] = array(
      'success' => $result !== FALSE,
      'query' => check_plain($query),
    );
  }
  $items = array();

  // Add the aid column after the vid column.
  db_add_column($items, 'quotes', 'aid', 'INT UNSIGNED NOT NULL AFTER vid');
  $add = $items[count($items) - 1]['success'];
  if (!$add) {
    drupal_set_message(t('Add column "aid" failed.'), 'error');
    return;
  }

  // Add an index for the aid.
  $items[] = update_sql("ALTER TABLE {quotes} ADD INDEX quotes_aid (aid) ");
  $add = $items[count($items) - 1]['success'];
  if (!$add) {
    drupal_set_message(t('Add index for "aid" failed.'), 'error');
    return;
  }

  // Get all the authors.
  $rows = array();
  $result = db_query("SELECT DISTINCT(author) FROM {quotes} ORDER BY author");
  while ($q = db_fetch_array($result)) {
    $author = $q['author'];

    // If the current author field has a mini-bio, split it off.
    $paren = strpos($author, '(');
    $comma = strpos($author, ',');
    $sub = min($paren === FALSE ? drupal_strlen($author) : $paren, $comma === FALSE ? drupal_strlen($author) : $comma);
    if ($sub === FALSE) {
      $bio = NULL;
    }
    else {
      $bio = trim(drupal_substr($author, $sub));
      $author = trim(drupal_substr($author, 0, $sub));
    }

    // Add the row to the new table.
    $items[] = update_sql("INSERT INTO {quotes_authors} (name, bio) VALUES ('" . db_escape_string($author) . "', '" . db_escape_string($bio) . "')");
    $add = $items[count($items) - 1]['success'];
    if ($add === FALSE) {
      $aid = 'failed';
      $upd = 'bypassed';
    }
    else {
      $aid = db_result(db_query("SELECT LAST_INSERT_ID()"));
      if ($aid < 1) {
        drupal_set_message(t('Invalid aid returned: ') . $aid, 'error');
      }
      $query = 'UPDATE {quotes} SET aid=' . $aid . " WHERE author='" . db_escape_string($q['author']) . "'";
      $items[] = update_sql($query);
    }
  }
  $items[] = update_sql("ALTER TABLE {quotes} DROP author");
  $del = $items[count($items) - 1]['success'];
  if (!$del) {
    drupal_set_message(t('Drop column "author" failed.'), 'error');
  }
  drupal_set_message(t('Update 7 for Quotes complete.'), 'status');
  return $items;
}

/**
 * Implementation of hook_update_N.
 * Change citation, author name to TEXT.
 */
function quotes_update_8() {
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {quotes} CHANGE citation citation TEXT");
  $ret[] = update_sql("ALTER TABLE {quotes_authors} DROP INDEX name");
  $ret[] = update_sql("ALTER TABLE {quotes_authors} CHANGE name name TEXT");
  $ret[] = update_sql("ALTER TABLE {quotes_authors} ADD INDEX name (name (255))");
  return $ret;
}

/**
 * Implementation of hook_update_N.
 * 
 */
function quotes_update_9() {
  global $db_type;
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {quotes_blocks} ADD COLUMN show_citation INTEGER NOT NULL DEFAULT 0");
      $ret[] = update_sql("ALTER TABLE {quotes_blocks} ADD COLUMN max_length INTEGER NOT NULL DEFAULT 0");
      $ret[] = update_sql("ALTER TABLE {quotes_blocks} ADD COLUMN view_weight INTEGER NOT NULL DEFAULT 0");
      $ret[] = update_sql("ALTER TABLE {quotes_blocks} ADD COLUMN view_text VARCHAR(64)");
      $ret[] = update_sql("ALTER TABLE {quotes_blocks} ADD COLUMN more_text VARCHAR(64)");
      break;
    case 'pgsql':
      db_add_column($ret, 'quotes_blocks', 'show_citation', 'INTEGER', array(
        'not null' => TRUE,
        'default' => '0',
      ));
      db_add_column($ret, 'quotes_blocks', 'max_length', 'INTEGER', array(
        'not null' => TRUE,
        'default' => '0',
      ));
      db_add_column($ret, 'quotes_blocks', 'view_weight', 'INTEGER', array(
        'not null' => TRUE,
        'default' => '1',
      ));
      db_add_column($ret, 'quotes_blocks', 'view_text', 'VARCHAR(64)');
      db_add_column($ret, 'quotes_blocks', 'more_text', 'VARCHAR(64)');
  }

  // Incorrectly set per block.
  $max_len = variable_get('quotes_block_max_length', 0);

  // Set globally in admin page.
  $view_text = variable_get('quotes_block_view_text', t('View'));
  $view_weight = variable_get('quotes_block_view_weight', 1);
  $show_citation = variable_get('quotes_block_citation', TRUE);
  $ret[] = update_sql("UPDATE {quotes_blocks} SET view_text='" . $view_text . "', view_weight=" . $view_weight . ", show_citation=" . $show_citation . ", max_length=" . $max_len . " WHERE 1=1");

  // "more" text is per block already, but in a variable.
  $result = db_query('SELECT bid FROM {quotes_blocks}');
  while ($block = db_fetch_array($result)) {
    $more_text = variable_get('quotes_more_' . $block['bid'], NULL);
    $ret[] = update_sql("UPDATE {quotes_blocks} SET more_text='" . $more_text . "' WHERE bid=" . $block['bid']);
  }

  // Don't delete variables in case the update fails.
  return $ret;
}

/**
 * Implementation of hook_update_N.
 * Add aid_filter.
 */
function quotes_update_10() {
  global $db_type;
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {quotes_blocks} ADD COLUMN aid_filter TEXT NOT NULL");
      break;
    case 'pgsql':
      db_add_column($ret, 'quotes_blocks', 'aid_filter', 'TEXT', array(
        'not null' => TRUE,
      ));
  }
  return $ret;
}

/**
 * Implementation of hook_update_N.
 * Add rand_freq.
 */
function quotes_update_11() {
  global $db_type;
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {quotes_blocks} ADD COLUMN rand_freq INTEGER NOT NULL DEFAULT 100");
      break;
    case 'pgsql':
      db_add_column($ret, 'quotes_blocks', 'rand_freq', 'INTEGER', array(
        'not null' => TRUE,
        'default' => '100',
      ));
  }
  return $ret;
}

Functions

Namesort descending Description
quotes_install Implementation of hook_install().
quotes_uninstall Implementation of hook_uninstall().
quotes_update_1 Implementation of hook_update_N(). Updates existing tables to UTF-8.
quotes_update_10 Implementation of hook_update_N. Add aid_filter.
quotes_update_11 Implementation of hook_update_N. Add rand_freq.
quotes_update_2 Implementation of hook_update_N(). Adds vid column to quotes table.
quotes_update_3 Implementation of hook_update_N(). Fixes the primary key on the quotes table, adds indexes on the nid and promote columns, and deletes the quotes_version variable.
quotes_update_4 Implementation of hook_update_N(). Removes the title column and adds the additional fields to quotes_blocks to support cron.
quotes_update_5 Implementation of hook_update_N(). Add citation column to quotes table
quotes_update_6 Implementation of hook_update_N(). Add count column to quotes_block table
quotes_update_7 Implementation of hook_update_N(). Convert authors to separate table.
quotes_update_8 Implementation of hook_update_N. Change citation, author name to TEXT.
quotes_update_9 Implementation of hook_update_N.