You are here

views.install in Views (for Drupal 7) 5

Same filename and directory in other branches
  1. 8.3 views.install
  2. 6.3 views.install
  3. 6.2 views.install
  4. 7.3 views.install

File

views.install
View source
<?php

function views_install() {
  drupal_set_message('Installing views');
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      db_query("CREATE TABLE if not exists {view_view} (\n        vid int(10) unsigned NOT NULL default '0',\n        name varchar(32) NOT NULL UNIQUE,\n        description varchar(255),\n        access varchar(255),\n        -- page fields\n        page int(1),\n        page_title varchar(255),\n        page_header longtext,\n        page_header_format int(4) NOT NULL,\n        page_empty longtext,\n        page_empty_format int(4) NOT NULL,\n        page_footer longtext,\n        page_footer_format int(4) NOT NULL,\n        page_type varchar(20),\n        use_pager int(1),\n        nodes_per_page int(5),\n        url varchar(255),\n        -- menu fields\n        menu int(1),\n        menu_tab int(1),\n        menu_tab_weight int(4),\n        menu_title varchar(255),\n        menu_tab_default int(1),\n        menu_tab_default_parent_type varchar(10),\n        menu_parent_title varchar(255),\n        menu_parent_tab_weight int(4),\n        -- block fields\n        block int(1),\n        block_title varchar(255),\n        block_use_page_header int(1),\n        block_header longtext,\n        block_header_format int(4) NOT NULL,\n        block_use_page_footer int(1),\n        block_footer longtext,\n        block_footer_format int(4) NOT NULL,\n        block_use_page_empty int(1),\n        block_empty longtext,\n        block_empty_format int(4) NOT NULL,\n        block_type varchar(20),\n        nodes_per_block int(5),\n        block_more int(1),\n        -- breadcrumb\n        breadcrumb_no_home int(1), -- true means start breadcrumb trail from 'Home'.\n        -- other\n        changed int(11),\n        view_args_php longtext,\n        is_cacheable int(1),\n        PRIMARY KEY (vid),\n        KEY (name)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      db_query("CREATE TABLE if not exists {view_sort} (\n        vid int(10) unsigned NOT NULL default '0',\n        position int(2),\n        field varchar(255),\n        sortorder varchar(5),\n        options varchar(255),\n        tablename varchar(255),\n        KEY (vid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      db_query("CREATE TABLE if not exists {view_argument} (\n        vid int(10) unsigned NOT NULL default '0',\n        type varchar(255),\n        argdefault varchar(255),\n        title varchar(255),\n        options varchar(255),\n        position int(2),\n        wildcard varchar(32),\n        wildcard_substitution varchar(32),\n        KEY (vid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      db_query("CREATE TABLE if not exists {view_tablefield} (\n        vid int(10) unsigned NOT NULL default '0',\n        tablename varchar(255),\n        field varchar(255),\n        label varchar(255),\n        handler varchar(255),\n        sortable int(1),\n        defaultsort varchar(5),\n        options varchar(255),\n        position int(2),\n        KEY (vid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      db_query("CREATE TABLE if not exists {view_filter} (\n        vid int(10) unsigned NOT NULL default '0',\n        tablename varchar(255),\n        field varchar(255),\n        value longtext,\n        operator varchar(20),\n        options varchar(255),\n        position int(2),\n        KEY (vid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      db_query("CREATE TABLE if not exists {view_exposed_filter} (\n        vid int(10) unsigned NOT NULL default '0',\n        field varchar(255),\n        label varchar(255),\n        optional int(1),\n        is_default int(1),\n        operator int(1),\n        single int(1),\n        position int(2),\n        KEY (vid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      db_query("CREATE TABLE {cache_views} (\n        cid varchar(255) NOT NULL default '',\n        data longblob,\n        expire int NOT NULL default '0',\n        created int NOT NULL default '0',\n        headers text,\n        PRIMARY KEY (cid),\n        INDEX expire (expire)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
      $success = TRUE;
      break;
    case 'pgsql':
      db_query("CREATE TABLE {view_view} (\n        vid SERIAL,\n        name varchar(32) NOT NULL UNIQUE,\n        description varchar(255),\n        access varchar(255),\n        -- page fields\n        page smallint,\n        page_title varchar(255),\n        page_header text,\n        page_header_format smallint NOT NULL,\n        page_empty text,\n        page_empty_format smallint NOT NULL,\n        page_footer text,\n        page_footer_format smallint NOT NULL,\n        page_type varchar(20),\n        use_pager smallint,\n        nodes_per_page smallint, \n        url varchar(255),\n        -- menu fields\n        menu smallint,\n        menu_tab smallint,\n        menu_tab_weight smallint,\n        menu_title varchar(255),\n        menu_tab_default smallint,\n        menu_tab_default_parent_type varchar(10),\n        menu_parent_title varchar(255),\n        menu_parent_tab_weight smallint,\n        -- block fields\n        block smallint,\n        block_title varchar(255),\n        block_use_page_header smallint,\n        block_header text,\n        block_header_format smallint NOT NULL,\n        block_use_page_footer smallint,\n        block_footer text,\n        block_footer_format smallint NOT NULL,\n        block_use_page_empty smallint,\n        block_empty text,\n        block_empty_format smallint NOT NULL,\n        block_type varchar(20),\n        nodes_per_block smallint,\n        block_more smallint,\n        -- breadcrumb\n        breadcrumb_no_home smallint, -- true means start breadcrumb trail from 'Home'.\n        -- other\n        changed integer,\n        view_args_php text,\n        is_cacheable smallint,\n        PRIMARY KEY (vid)\n      )");
      db_query("CREATE INDEX {view_view}_name_idx ON {view_view} (name)");
      db_query("CREATE TABLE {view_sort} (\n        vid integer NOT NULL default '0',\n        position smallint,\n        field varchar(255),\n        sortorder varchar(5),\n        options varchar(255),\n        tablename varchar(255)\n      )");
      db_query("CREATE INDEX {view_sort}_vid_idx ON {view_sort} (vid)");
      db_query("CREATE TABLE {view_argument} (\n        vid integer NOT NULL default '0',\n        type varchar(255),\n        argdefault varchar(255),\n        title varchar(255),\n        options varchar(255),\n        position smallint,\n        wildcard varchar(32),\n        wildcard_substitution varchar(32)\n      )");
      db_query("CREATE INDEX {view_argument}_vid_idx ON {view_argument} (vid)");
      db_query("CREATE TABLE {view_tablefield} (\n        vid integer NOT NULL default '0',\n        tablename varchar(255),\n        field varchar(255),\n        label varchar(255),\n        handler varchar(255),\n        sortable smallint,\n        defaultsort varchar(5),\n        options varchar(255),\n        position smallint\n      )");
      db_query("CREATE INDEX {view_tablefield}_vid_idx ON {view_tablefield} (vid)");
      db_query("CREATE TABLE {view_filter} (\n        vid integer NOT NULL default '0',\n        tablename varchar(255),\n        field varchar(255),\n        value text,\n        operator varchar(20),\n        options varchar(255),\n        position smallint\n      )");
      db_query("CREATE INDEX {view_filter}_vid_idx ON {view_filter} (vid)");
      db_query("CREATE TABLE {view_exposed_filter} (\n        vid integer NOT NULL default '0',\n        field varchar(255),\n        label varchar(255),\n        optional smallint,\n        is_default smallint,\n        operator smallint,\n        single smallint,\n        position smallint\n      )");
      db_query("CREATE INDEX {view_exposed_filter}_vid_idx ON {view_exposed_filter} (vid)");
      db_query("CREATE TABLE {cache_views} (\n        cid varchar(255),\n        data bytea,\n        expire integer,\n        created integer,\n        headers text,\n        PRIMARY KEY (cid)\n      )");
      db_query("CREATE INDEX {cache_views}_expire_idx ON {cache_views} (expire)");
      $success = TRUE;
      break;
  }

  // End case
  db_query("UPDATE {system} SET weight = 10 WHERE name = 'views'");
  if ($success) {
    module_enable(array(
      'views_ui',
    ));
    drupal_set_message(t('Views module installed tables successfully.'));
  }
  else {
    drupal_set_message(t('The installation of views module was unsuccessful.'), 'error');
  }
}
function views_uninstall() {
  if (db_table_exists('view_view')) {
    db_query("DROP TABLE {view_view}");
  }
  if (db_table_exists('view_sort')) {
    db_query("DROP TABLE {view_sort}");
  }
  if (db_table_exists('view_argument')) {
    db_query("DROP TABLE {view_argument}");
  }
  if (db_table_exists('view_tablefield')) {
    db_query("DROP TABLE {view_tablefield}");
  }
  if (db_table_exists('view_filter')) {
    db_query("DROP TABLE {view_filter}");
  }
  if (db_table_exists('view_exposed_filter')) {
    db_query("DROP TABLE {view_exposed_filter}");
  }
  if (db_table_exists('cache_views')) {
    db_query("DROP TABLE {cache_views}");
  }
}

/*
 * update module for UTF.
 */
function views_update_1() {
  return _system_update_utf8(array(
    'view_view',
    'view_tablefield',
    'view_filter',
    'view_sort',
    'view_argument',
  ));
}

/**
 * Dummy function cause we're apparently not to skip update #s.
 */
function views_update_2() {

  // dummy function
  return array();
}

/*
 * update module for exposed filters.
 */
function views_update_3() {
  $ret = array();

  //  db_add_column($ret, 'view_view', 'page_empty', 'longtext');
  //  db_add_column($ret, 'view_view', 'page_empty_format', 'int(4)', array('not null' => TRUE));
  //  db_add_column($ret, 'view_view', 'block_use_page_empty', 'int(1)');
  //  db_add_column($ret, 'view_view', 'block_empty', 'longtext');
  //  db_add_column($ret, 'view_view', 'block_empty_format', 'int(4)', array('not null' => TRUE));
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE if not exists {view_exposed_filter} (\n        vid int(10) unsigned NOT NULL default '0',\n        field varchar(255),\n        label varchar(255),\n        optional int(1),\n        is_default int(1),\n        operator int(1),\n        single int(1),\n        position int(2),\n        KEY (vid)\n      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {view_exposed_filter} (\n        vid numeric(10) unsigned NOT NULL default '0',\n        field varchar(255),\n        label varchar(255),\n        optional numeric(1),\n        is_default numeric(1),\n        operator numeric(1),\n        single numeric(1),\n        position numeric(2),\n      )");
      $ret[] = update_sql("CREATE INDEX {view_exposed_filter}_vid_idx ON {view_exposed_filter} (vid)");
      break;
  }
  return $ret;
}
function views_update_4() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $small = 'int(4)';
      $tiny = 'int(1)';
    case 'pgsql':
      $small = $tiny = 'smallint';
      break;
  }
  db_add_column($ret, 'view_view', 'page_footer', 'longtext');
  db_add_column($ret, 'view_view', 'page_footer_format', $small, array(
    'default' => 0,
  ));
  db_add_column($ret, 'view_view', 'block_use_page_footer', $tiny);
  db_add_column($ret, 'view_view', 'block_footer', 'longtext');
  db_add_column($ret, 'view_view', 'block_footer_format', $small, array(
    'default' => 0,
  ));
  db_add_column($ret, 'view_view', 'page_empty', 'longtext');
  db_add_column($ret, 'view_view', 'page_empty_format', $small, array(
    'default' => 0,
  ));
  db_add_column($ret, 'view_view', 'block_use_page_empty', $tiny);
  db_add_column($ret, 'view_view', 'block_empty', 'longtext');
  db_add_column($ret, 'view_view', 'block_empty_format', $small, array(
    'default' => 0,
  ));
  return $ret;
}
function views_update_5() {
  $ret = array();
  db_add_column($ret, 'view_view', 'view_args_php', 'longtext');
  return $ret;
}
function views_update_6() {
  $ret = array();
  db_add_column($ret, 'view_tablefield', 'options', 'varchar(255)');
  return $ret;
}
function views_update_7() {
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {view_argument} MODIFY type varchar(255)");
  return $ret;
}
function views_update_8() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {view_view} MODIFY nodes_per_page int(5)");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {view_view} MODIFY nodes_per_page smallint");
      break;
  }
  return $ret;
}
function views_update_9() {
  $ret = array();
  db_add_column($ret, 'view_argument', 'wildcard', 'varchar(32)');
  db_add_column($ret, 'view_argument', 'wildcard_substitution', 'varchar(32)');
  return $ret;
}
function views_update_10() {
  $ret = array();
  $ret[] = update_sql("UPDATE {system} SET weight = 10 WHERE name = 'views'");
  return $ret;
}
function views_update_11() {

  // running this again because fresh installs of 1.4 didn't get it.
  $ret = array();
  $ret[] = update_sql("UPDATE {system} SET weight = 10 WHERE name = 'views'");
  return $ret;
}
function views_update_12() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_add_column($ret, 'view_view', 'is_cacheable', 'int(1)');
      break;
    case 'pgsql':
      db_add_column($ret, 'view_view', 'is_cacheable', 'smallint');
      break;
  }
  views_make_cache_table($ret);
  include_once './' . drupal_get_path('module', 'views') . '/views.module';
  $result = db_query("SELECT name FROM {view_view} ORDER BY name");
  while ($row = db_fetch_array($result)) {
    $view = views_get_view($row['name']);
    $ret[] = update_sql("UPDATE {view_view} SET is_cacheable = " . (_views_is_cacheable($view) ? "1" : "0") . " WHERE vid = " . $view->vid);
  }
  $ret[] = update_sql("ALTER TABLE {view_view} DROP query");
  $ret[] = update_sql("ALTER TABLE {view_view} DROP countquery");
  views_make_cache_table($ret);
  return $ret;
}
function views_update_13() {
  $ret = array();
  views_make_cache_table($ret);
  $ret[] = update_sql("DELETE FROM {cache_views}");
  return $ret;
}
function views_update_14() {
  $ret = array();
  views_make_cache_table($ret);
  $ret[] = update_sql("DELETE FROM {cache_views}");
  return $ret;
}
function views_update_15() {
  $ret = array();
  db_add_column($ret, 'view_view', 'menu_tab_default_parent_type', 'varchar(10)');
  db_add_column($ret, 'view_view', 'menu_parent_title', 'varchar(255)');
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_add_column($ret, 'view_view', 'menu_parent_tab_weight', 'int(4)');
      break;
    case 'pgsql':
      db_add_column($ret, 'view_view', 'menu_parent_tab_weight', 'smallint');
      break;
  }
  return $ret;
}
function views_update_16() {
  $ret = array();
  $ret[] = update_sql("UPDATE {view_view} SET menu_tab_default_parent_type = 'normal' WHERE menu_tab_default_parent_type = '' OR menu_tab_default_parent_type IS NULL");
  return $ret;
}

/**
 * This should go in every update to ensure that it's there from a 4.7 -> 5.x
 * update.
 */
function views_make_cache_table(&$ret) {
  if (!db_table_exists('cache_views')) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        $ret[] = update_sql("CREATE TABLE {cache_views} (\n            cid varchar(255) NOT NULL default '',\n            data longblob,\n            expire int NOT NULL default '0',\n            created int NOT NULL default '0',\n            headers text,\n            PRIMARY KEY (cid),\n            INDEX expire (expire)\n          ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
        break;
      case 'pgsql':
        $ret[] = update_sql("CREATE TABLE {cache_views} (\n            cid varchar(255),\n            data bytea,\n            expire integer,\n            created integer,\n            headers text,\n            PRIMARY KEY (cid)\n          )");
        $ret[] = update_sql("CREATE INDEX {cache_views}_expire_idx ON {cache_views} (expire)");
        break;
    }
  }
}