<?php
// $Id: path_redirect.install,v 1.2.2.6.2.26 2009/12/01 05:40:09 davereid Exp $

/**
 * @file
 * Install and uninstall schema and functions for the path_redirect module.
 */

/**
 * Implement hook_schema().
 */
function path_redirect_schema() {
  $schema['path_redirect'] = array(
    'description' => 'Stores information on redirects.',
    'fields' => array(
      'rid'  => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Unique path redirect ID.',
      ),
      'source' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'The source path to redirect from.',
      ),
      'redirect' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'The destination path to redirect to.',
      ),
      'query' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'description' => 'The query string to send to the destination.',
      ),
      'fragment' => array(
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
        'description' => 'An internal page anchor append to the destination.',
      ),
      'language' => array(
        'description' => 'The language this redirect is for; if blank, the alias will be used for unknown languages.',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
      'type' => array(
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'description' => 'The HTTP status code to use for the redirect.',
      ),
      'last_used' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The timestamp of when the redirect was last used.',
      ),
    ),
    'primary key' => array('rid'),
    'unique keys' => array('source_language' => array('source', 'language')),
  );

  return $schema;
}

/**
 * Implement hook_install().
 */
function path_redirect_install() {
  drupal_install_schema('path_redirect');
}

/**
 * Implement hook_uninstall().
 */
function path_redirect_uninstall() {
  // Remove tables.
  drupal_uninstall_schema('path_redirect');

  // Remove variables.
  variable_del('path_redirect_redirect_warning');
  variable_del('path_redirect_allow_bypass');
  variable_del('path_redirect_auto_redirect');
  variable_del('path_redirect_purge_inactive');
  variable_del('path_redirect_default_status');
  variable_del('path_redirect_nodeapi_enabled');
}

/**
 * Allow longer values for path and redirect. Add a unique key on rid.
 */
function path_redirect_update_1() {
  $ret = array();
  db_change_field($ret, 'path_redirect', 'path', 'path', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));
  db_change_field($ret, 'path_redirect', 'redirect', 'redirect', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE), array('unique keys' => array('rid')));
  return $ret;
}

/**
 * Convert the type column to an integer to only store the status code.
 */
function path_redirect_update_2() {
  $ret = array();

  // Convert string status messages to integer status codes.
  update_sql("UPDATE {path_redirect} SET type = '300' WHERE type = 'HTTP/1.0 300 Multiple Choices'");
  update_sql("UPDATE {path_redirect} SET type = '301' WHERE type = 'HTTP/1.0 301 Moved Permanently'");
  update_sql("UPDATE {path_redirect} SET type = '302' WHERE type = 'HTTP/1.0 302 Found'");
  update_sql("UPDATE {path_redirect} SET type = '303' WHERE type = 'HTTP/1.0 303 See Other'");
  update_sql("UPDATE {path_redirect} SET type = '304' WHERE type = 'HTTP/1.0 304 Not Modified'");
  update_sql("UPDATE {path_redirect} SET type = '305' WHERE type = 'HTTP/1.0 305 Use Proxy'");
  update_sql("UPDATE {path_redirect} SET type = '307' WHERE type = 'HTTP/1.0 307 Temporary Redirect'");

  // Convert the type column to store integers now that all values are converted.
  db_change_field($ret, 'path_redirect', 'type', 'type', array('type' => 'int', 'size' => 'small', 'not null' => TRUE));

  return $ret;
}

/**
 * Fix a typo that could have caused the code 306 to be used mistakenly.
 */
function path_redirect_update_3() {
  $ret = array();
  $ret[] = update_sql("UPDATE {path_redirect} SET type = 307 WHERE type = 306");
  return $ret;
}

/**
 * Allow longer values for query strings fix the primary and unique keys.
 */
function path_redirect_update_4() {
  $ret = array();

  // Allow longer query strings.
  db_change_field($ret, 'path_redirect', 'query', 'query', array('type' => 'varchar', 'length' => 255, 'not null' => FALSE));

  // Change the primary key from path to rid and add a unique key on path.
  db_drop_primary_key($ret, 'path_redirect');
  db_drop_unique_key($ret, 'path_redirect', 'rid');
  db_add_primary_key($ret, 'path_redirect', array('rid'));
  db_add_unique_key($ret, 'path_redirect', 'path', array('path'));

  return $ret;
}

/**
 * Backporting schema for successful upgrades to 6.x.
 */
function path_redirect_update_5100() {
  $ret = array();
  db_change_field($ret, 'path_redirect', 'rid', 'rid', array('type' => 'serial', 'not null' => TRUE));
  db_change_field($ret, 'path_redirect', 'query', 'query', array('type' => 'varchar', 'length' => 255, 'not null' => FALSE));
  db_change_field($ret, 'path_redirect', 'fragment', 'fragment', array('type' => 'varchar', 'length' => 50, 'not null' => FALSE));
  db_change_field($ret, 'path_redirect', 'type', 'type', array('type' => 'int', 'size' => 'small', 'not null' => TRUE));
  return $ret;
}

/**
 * @defgroup updates-6.x-extra Extra path_redirect updates for 6.x
 * @{
 */

/**
 * Add a last used timestamp field.
 */
function path_redirect_update_6100() {
  $ret = array();
  db_add_field($ret, 'path_redirect', 'last_used', array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
  $ret[] = update_sql("UPDATE {path_redirect} SET last_used = " . time());
  return $ret;
}

/**
 * Add a language field.
 */
function path_redirect_update_6101() {
  $ret = array();
  db_drop_unique_key($ret, 'path_redirect', 'path');
  db_add_field($ret, 'path_redirect', 'language', array('type' => 'varchar', 'length' => 12, 'not null' => TRUE, 'default' => ''));
  db_add_unique_key($ret, 'path_redirect', 'path_language', array('path', 'language'));
  return $ret;
}

/**
 * Replace frontpage paths with '<front>'.
 */
function path_redirect_update_6102() {
  $ret = array();
  $frontpage = variable_get('site_frontpage', 'node');
  $ret[] = update_sql("UPDATE {path_redirect} SET redirect = '<front>' WHERE redirect = '$frontpage'");
  return $ret;
}

/**
 * Rename the path field to source.
 */
function path_redirect_update_6103() {
  $ret = array();
  db_drop_unique_key($ret, 'path_redirect', 'path_language');
  db_change_field($ret, 'path_redirect', 'path', 'source', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));
  db_add_unique_key($ret, 'path_redirect', 'source_language', array('source', 'language'));
  return $ret;
}

/**
 * @} End of "defgroup updates-6.x-extra"
 * The next series of updates should start at 7000.
 */
