Skip to content
Snippets Groups Projects
updates.inc 44.4 KiB
Newer Older
                           word varchar(50) NOT NULL default '',
                           count float default NULL,
                           PRIMARY KEY word (word)
                           )");
      break;
    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {search_dataset} (
        sid integer NOT NULL default '0',
        type varchar(16) default NULL,
        data text NOT NULL default '')");
      $ret[] = update_sql("CREATE INDEX {search_dataset}_sid_type_idx ON {search_dataset}(sid, type)");

      $ret[] = update_sql("CREATE TABLE {search_index} (
        word varchar(50) NOT NULL default '',
        sid integer NOT NULL default '0',
        type varchar(16) default NULL,
        fromsid integer NOT NULL default '0',
        fromtype varchar(16) default NULL,
        score float default NULL)");
      $ret[] = update_sql("CREATE INDEX {search_index}_sid_type_idx ON {search_index}(sid, type)");
      $ret[] = update_sql("CREATE INDEX {search_index}_fromsid_fromtype_idx ON {search_index}(fromsid, fromtype)");
      $ret[] = update_sql("CREATE INDEX {search_index}_word_idx ON {search_index}(word)");

      $ret[] = update_sql("CREATE TABLE {search_total} (
        word varchar(50) NOT NULL default '',
        count float default NULL,
        PRIMARY KEY(word))");
      break;
    default:
      break;
  }
  return $ret;
}

function update_151() {
  $ret = array();

  $ts = variable_get('theme_settings', null);

  // set up data array so we can loop over both sets of links
  $menus = array(0 => array('links_var' => 'primary_links',
                            'toggle_var' => 'toggle_primary_links',
                            'more_var' => 'primary_links_more',
                            'menu_name' => t('Primary links'),
                            'menu_var' => 'menu_primary_menu',
                            'pid' => 0),
                 1 => array('links_var' => 'secondary_links',
                            'toggle_var' => 'toggle_secondary_links',
                            'more_var' => 'secondary_links_more',
                            'menu_name' => t('Secondary links'),
                            'menu_var' => 'menu_secondary_menu',
                            'pid' => 0));

  for ($loop = 0; $loop <= 1 ; $loop ++) {
    // create new Primary and Secondary links menus
    $menus[$loop]['pid'] = db_next_id('{menu}_mid');
    $ret[] = update_sql("INSERT INTO {menu} (mid, pid, path, title, description, weight, type) " .
                         "VALUES ({$menus[$loop]['pid']}, 0, '', '{$menus[$loop]['menu_name']}', '', 0, 115)");

    // insert all entries from theme links into new menus
    $num_inserted = 0;
    if (is_array($ts) && is_array($ts[$menus[$loop]['links_var']])) {
      $links = $ts[$menus[$loop]['links_var']];
      for ($i = 0; $i < count($links['text']); $i++) {
        if ($links['text'][$i] != "" && $links['link'][$i] != "") {
          $num_inserted ++;
          $node_unalias = db_fetch_array(db_query("SELECT src FROM {url_alias} WHERE dst = '%s'", $links['link'][$i]));
          if (is_array($node_unalias)) {
      $link_path = $node_unalias['src'];
    }
    else {
      $link_path = $links['link'][$i];
    }

          $mid = db_next_id('{menu}_mid');
          $ret[] = update_sql("INSERT INTO {menu} (mid, pid, path, title, description, weight, type) " .
                               "VALUES ($mid, {$menus[$loop]['pid']}, '" . db_escape_string($link_path) .
                               "', '" . db_escape_string($links['text'][$i]) .
                               "', '" . db_escape_string($links['description'][$i]) . "', 0, 118)");
        }
      }
      // delete Secondary links if not populated.
      if ($loop == 1 && $num_inserted == 0) {
        db_query("DELETE FROM {menu} WHERE mid={$menus[$loop]['pid']}");
      }
    }

    // set menu_primary_menu variable appropriately
    if (!$ts[$menus[$loop]['toggle_var']] || $num_inserted == 0) {
      variable_set($menus[$loop]['menu_var'], 0);
    }
    else {
      variable_set($menus[$loop]['menu_var'], $menus[$loop]['pid']);
    }
    variable_del($menus[$loop]['toggle_var']);
    unset($ts[$menus[$loop]['toggle_var']]);
    variable_del($menus[$loop]['links_var']);
    unset($ts[$menus[$loop]['links_var']]);
    variable_del($menus[$loop]['more_var']);
    unset($ts[$menus[$loop]['more_var']]);
  }

  if (is_array($ts)) {
    variable_set('theme_settings', $ts);
  }

  $ret[] = update_sql("UPDATE {system} SET status = 1 WHERE name = 'menu'");

  return $ret;
}

function update_152() {
  $ret = array();

  // Postgresql only update
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {forum} RENAME shadow TO shadow_old");
      break;
    case 'mysql':
    case 'mysqli':
      break;
  }

  return $ret;
}

function update_153(){
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {contact} DROP CONSTRAINT {contact}_pkey");
      $ret[] = update_sql("CREATE SEQUENCE {contact}_cid_seq");
      db_add_column($ret, 'contact', 'cid', 'int', array('not null' => TRUE, 'default' => "nextval('{contact}_cid_seq')"));
      $ret[] = update_sql("ALTER TABLE {contact} ADD PRIMARY KEY (cid)");
      $ret[] = update_sql("ALTER TABLE {contact} ADD CONSTRAINT {contact}_category_key UNIQUE (category)");
      break;
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {contact} DROP PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {contact} ADD COLUMN cid int(11) NOT NULL PRIMARY KEY auto_increment");
      $ret[] = update_sql("ALTER TABLE {contact} ADD UNIQUE KEY category (category)");
      break;
  }
  return $ret;
}

function update_154() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_add_column($ret, 'contact', 'weight', 'smallint', array('not null' => TRUE, 'default' => 0));
      db_add_column($ret, 'contact', 'selected', 'smallint', array('not null' => TRUE, 'default' => 0));
      break;
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {contact} ADD COLUMN weight tinyint(3) NOT NULL DEFAULT 0");
      $ret[] = update_sql("ALTER TABLE {contact} ADD COLUMN selected tinyint(1) NOT NULL DEFAULT 0");
      break;
  }
  return $ret;
}

function update_155() {
  $ret = array();

  // Postgresql only update
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      $ret[] = update_sql("DROP TABLE {cache}");
      $ret[] = update_sql("CREATE TABLE {cache} (
        cid varchar(255) NOT NULL default '',
        data bytea default '',
        expire integer NOT NULL default '0',
        created integer NOT NULL default '0',
        headers text default '',
        PRIMARY KEY (cid)
        )");
      $ret[] = update_sql("CREATE INDEX {cache}_expire_idx ON {cache}(expire)");
      break;
    case 'mysql':
    case 'mysqli':
      break;
  }

  return $ret;
}


/**
 * Adds a column to a database. Uses syntax appropriate for PostgreSQL.
 * Saves result of SQL commands in $ret array.
 *
 * Note: when you add a column with NOT NULL and you are not sure if there are rows in table already,
 *  you MUST also add DEFAULT. Otherwise PostgreSQL won't work if the table is not empty. If NOT NULL and
 *  DEFAULT is set the Postgresql version will set values of the added column in old rows to the DEFAULT value.
 *
 * @param $ret
 *  Array to which results will be added.
 * @param $table
 *  Name of the table, without {}
 * @param $column
 *  Name of the column
 * @param $type
 *  Type of column
 * @param $attributes
 *  Additional optional attributes. Recognized atributes:
 *    - not null    => TRUE/FALSE
 *    - default     => NULL/FALSE/value (with or without '', it wont' be added)
 * @return
 *  nothing, but modifies $ret parametr.
 */
function db_add_column(&$ret, $table, $column, $type, $attributes = array()) {
  if (array_key_exists('not null', $attributes) and $attributes['not null']) {
    $not_null = 'NOT NULL';
  }
  if (array_key_exists('default', $attributes)) {
    if (is_null($attributes['default'])) {
      $default_val = 'NULL';
      $default = 'default NULL';
    }
    elseif ($attributes['default'] === FALSE) {
      $default = '';
    }
    else {
      $default_val = "$attributes[default]";
      $default = "default $attributes[default]";
    }
  }

  $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column $type");
  if ($default) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET $default"); }
  if ($not_null) {
    if ($default) { $ret[] = update_sql("UPDATE {". $table ."} SET $column = $default_val"); }
    $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column SET NOT NULL");
  }
}

/**
 * Changes a column definition. Uses syntax appropriate for PostgreSQL.
 * Saves result of SQL commands in $ret array.
 *
 * @param $ret
 *  Array to which results will be added.
 * @param $table
 *  Name of the table, without {}
 * @param $column
 *  Name of the column to change
 * @param $column_new
 *  New name for the column (set to the same as $column if you don't want to change the name)
 * @param $type
 *  Type of column
 * @param $attributes
 *  Additional optional attributes. Recognized atributes:
 *    - not null    => TRUE/FALSE
 *    - default     => NULL/FALSE/value (with or without '', it wont' be added)
 * @return
 *  nothing, but modifies $ret parametr.
 */
function db_change_column(&$ret, $table, $column, $column_new, $type, $attributes = array()) {
  if (array_key_exists('not null', $attributes) and $attributes['not null']) {
    $not_null = 'NOT NULL';
  }
  if (array_key_exists('default', $attributes)) {
    if (is_null($attributes['default'])) {
      $default_val = 'NULL';
      $default = 'default NULL';
    }
    elseif ($attributes['default'] === FALSE) {
      $default = '';
    }
    else {
      $default_val = "$attributes[default]";
      $default = "default $attributes[default]";
    }
  }

  $ret[] = update_sql("ALTER TABLE {". $table ."} RENAME $column TO ". $column ."_old");
  $ret[] = update_sql("ALTER TABLE {". $table ."} ADD $column_new $type");
  $ret[] = update_sql("UPDATE {". $table ."} SET $column_new = ". $column ."_old");
  if ($default) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET $default"); }
  if ($not_null) { $ret[] = update_sql("ALTER TABLE {". $table ."} ALTER $column_new SET NOT NULL"); }
  // We don't drop columns for now
  // $ret[] = update_sql("ALTER TABLE {". $table ."} DROP ". $column ."_old");
}


function update_sql($sql) {
  $edit = $_POST["edit"];
  $result = db_query($sql);
  if ($result) {
    return array('1', check_plain($sql) ."\n<span class=\"success\">OK</span>\n");
  }
  else {
    return array('0', check_plain($sql) ."\n<span class=\"failure\">FAILED</span>\n");
  }
}

?>