You are here

acl.install in ACL 5

Same filename and directory in other branches
  1. 8 acl.install
  2. 6 acl.install
  3. 7 acl.install

File

acl.install
View source
<?php

function acl_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("create table if not exists {acl} (\n        acl_id int(10) NOT NULL default 0,\n        module varchar(255),\n        name varchar(255),\n        PRIMARY KEY (acl_id)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("create table if not exists {acl_user} (\n        acl_id int(10) NOT NULL default 0,\n        uid int(10) NOT NULL default 0,\n        PRIMARY KEY (acl_id, uid),\n        KEY uid (uid)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("create table if not exists {acl_node} (\n        acl_id int(10) NOT NULL default 0,\n        nid int(10) NOT NULL default 0,\n        grant_view tinyint(1) unsigned NOT NULL default '0',\n        grant_update tinyint(1) unsigned NOT NULL default '0',\n        grant_delete tinyint(1) unsigned NOT NULL default '0',\n        priority smallint(2) NOT NULL default '0',\n        PRIMARY KEY (acl_id, nid)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {acl} (\n        acl_id integer NOT NULL DEFAULT 0,\n        module varchar(255),\n        name varchar(255),\n        PRIMARY KEY (acl_id)\n      );");
      db_query("CREATE SEQUENCE {acl}_acl_id_seq;");
      db_query("CREATE TABLE {acl_user} (\n        acl_id integer NOT NULL DEFAULT 0,\n        uid int NOT NULL DEFAULT 0,\n        PRIMARY KEY (acl_id, uid)\n      );");
      db_query("CREATE INDEX {acl_user}_uid_index ON {acl_user} (uid)");
      db_query("CREATE TABLE {acl_node} (\n        acl_id integer NOT NULL DEFAULT 0,\n        nid int NOT NULL DEFAULT 0,\n        grant_view smallint NOT NULL default 0,\n        grant_update smallint NOT NULL default 0,\n        grant_delete smallint NOT NULL default 0,\n        priority smallint NOT NULL default 0,\n        PRIMARY KEY (acl_id, nid)\n      );");
      break;
  }
}

/*
 * Implementation of hook_uninstall
 */
function acl_uninstall() {
  if ($GLOBALS['db_type'] == 'pgsql') {
    db_query('DROP INDEX {acl_user}_uid_index');
  }
  db_query('DROP TABLE {acl}');
  db_query('DROP TABLE {acl_user}');
  db_query('DROP TABLE {acl_node}');
}

/**
 * Fixes table prefix
 */
function acl_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      db_query('LOCK TABLES {sequences} WRITE');
      $ret[] = update_sql("UPDATE {sequences} SET name = '" . db_prefix_tables('{acl}_acl_id') . "' WHERE name = 'acl_id'");
      db_query('UNLOCK TABLES');
      break;
    case 'pgsql':
      db_query('START TRANSACTION;');
      $ret[] = update_sql("CREATE SEQUENCE {acl}_acl_id_seq START " . db_next_id('acl_id'));
      db_query('COMMIT;');
      break;
  }
  return $ret;
}

/**
 * Fixes primary keys
 */
function acl_update_2() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':

      // drop the previously created indexes (except for acl_user.uid)
      $ret[] = update_sql('ALTER TABLE {acl} DROP INDEX acl_id');
      $ret[] = update_sql('ALTER TABLE {acl_user} DROP INDEX acl_id');
      $ret[] = update_sql('ALTER TABLE {acl_node} DROP INDEX acl_id');
      $ret[] = update_sql('ALTER TABLE {acl_node} DROP INDEX nid');

      // create new indexes (as primary keys this time)
      $ret[] = update_sql('ALTER TABLE {acl} ADD PRIMARY KEY (acl_id)');
      $ret[] = update_sql('ALTER TABLE {acl_user} ADD PRIMARY KEY (acl_id, uid)');
      $ret[] = update_sql('ALTER TABLE {acl_node} ADD PRIMARY KEY (acl_id, nid)');
      break;
    case 'pgsql':
      $ret[] = update_sql('ALTER TABLE {acl} DROP PRIMARY KEY , ADD PRIMARY KEY (acl_id)');
      $ret[] = update_sql('ALTER TABLE {acl_user} DROP PRIMARY KEY , ADD PRIMARY KEY (acl_id, uid)');
      $ret[] = update_sql('ALTER TABLE {acl_node} DROP PRIMARY KEY , ADD PRIMARY KEY (acl_id, nid)');
      $ret[] = update_sql('CREATE INDEX {acl_user}_uid_index ON {acl_user} (uid)');
      break;
  }
  return $ret;
}

/*
 * Updates tables to use utf8 for mysql
 */
function acl_update_3() {
  $ret = array();

  // Only for MySQL 4.1+
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
      break;
    case 'mysql':
      if (version_compare(mysql_get_server_info($GLOBALS['active_db']), '4.1.0', '<')) {
        return array();
      }
      break;
    case 'pgsql':
      return array();
  }
  $ret = update_convert_table_utf8('acl');
  $ret = array_merge($ret, update_convert_table_utf8('acl_node'));
  $ret = array_merge($ret, update_convert_table_utf8('acl_user'));
  return $ret;
}

/**
 * Put back acl_node(nid) index for deleting nodes and clean up {acl_node}.
 */
function acl_update_4() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      $ret[] = update_sql("ALTER TABLE {acl_node} ADD INDEX (nid)");
      break;
    case 'pgsql':
      $ret[] = update_sql('CREATE INDEX {acl_node}_nid_index ON {acl_node} (nid)');
      break;
  }
  db_query("DELETE FROM {acl_node} WHERE nid NOT IN (SELECT nid FROM {node})");
  return $ret;
}

/**
 * Clean up {acl_user}.
 */
function acl_update_5() {
  $ret = array();
  db_query("DELETE FROM {acl_user} WHERE uid NOT IN (SELECT uid FROM {users})");
  return $ret;
}

/**
 * Add priority column.
 */
function acl_update_6() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      $ret[] = update_sql("ALTER TABLE {acl_node} ADD priority smallint(2) NOT NULL default 0");
      break;
    case 'pgsql':
      db_add_column($ret, 'acl_node', 'priority', 'smallint', array(
        'default' => 0,
        'not null' => TRUE,
      ));
      break;
  }
  return $ret;
}

Functions

Namesort descending Description
acl_install
acl_uninstall
acl_update_1 Fixes table prefix
acl_update_2 Fixes primary keys
acl_update_3
acl_update_4 Put back acl_node(nid) index for deleting nodes and clean up {acl_node}.
acl_update_5 Clean up {acl_user}.
acl_update_6 Add priority column.