content.install 11.2 KB
Newer Older
JonBob's avatar
JonBob committed
1
<?php
2
// $Id$
JonBob's avatar
JonBob committed
3

4 5 6
/**
 * Implementation of hook_install().
 */
JonBob's avatar
JonBob committed
7 8 9 10
function content_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
11
      db_query("CREATE TABLE {node_type_content} (
JonBob's avatar
JonBob committed
12 13
          type_name varchar(32) NOT NULL default '',
          label varchar(255) NOT NULL default '',
JonBob's avatar
JonBob committed
14 15
          description mediumtext NOT NULL,
          help mediumtext NOT NULL,
JonBob's avatar
JonBob committed
16
          title_label varchar(255) NOT NULL default '',
17
          PRIMARY KEY (type_name)
yched's avatar
yched committed
18
        ) /*!40100 DEFAULT CHARACTER SET utf8 */");
JonBob's avatar
JonBob committed
19 20 21
      db_query("CREATE TABLE {node_field} (
          field_name varchar(32) NOT NULL default '',
          type varchar(127) NOT NULL default '',
JonBob's avatar
JonBob committed
22
          global_settings mediumtext NOT NULL,
JonBob's avatar
JonBob committed
23 24
          required int NOT NULL default '0',
          multiple int NOT NULL default '0',
JonBob's avatar
JonBob committed
25
          db_storage int NOT NULL default '0',
26
          PRIMARY KEY (field_name)
yched's avatar
yched committed
27
        ) /*!40100 DEFAULT CHARACTER SET utf8 */");
JonBob's avatar
JonBob committed
28 29 30 31 32 33
      db_query("CREATE TABLE {node_field_instance} (
        field_name varchar(32) NOT NULL default '',
        type_name varchar(32) NOT NULL default '',
        weight int NOT NULL default '0',
        label varchar(255) NOT NULL default '',
        widget_type varchar(32) NOT NULL default '',
JonBob's avatar
JonBob committed
34
        widget_settings mediumtext NOT NULL,
35
        display_settings mediumtext NOT NULL,
JonBob's avatar
JonBob committed
36
        description mediumtext NOT NULL,
37
        PRIMARY KEY (field_name,type_name)
yched's avatar
yched committed
38
      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
JonBob's avatar
JonBob committed
39
      break;
40

JonBob's avatar
JonBob committed
41
    case 'pgsql':
42
      db_query("CREATE TABLE {node_type_content} (
JonBob's avatar
JonBob committed
43 44
          type_name varchar(32) NOT NULL default '',
          label varchar(255) NOT NULL default '',
JonBob's avatar
JonBob committed
45 46
          description text NOT NULL,
          help text NOT NULL,
JonBob's avatar
JonBob committed
47
          title_label varchar(255) NOT NULL default '',
48
          PRIMARY KEY (type_name)
JonBob's avatar
JonBob committed
49 50 51 52
        )");
      db_query("CREATE TABLE {node_field} (
          field_name varchar(32) NOT NULL default '',
          type varchar(127) NOT NULL default '',
JonBob's avatar
JonBob committed
53
          global_settings text NOT NULL,
JonBob's avatar
JonBob committed
54 55
          required integer NOT NULL default '0',
          multiple integer NOT NULL default '0',
JonBob's avatar
JonBob committed
56
          db_storage integer NOT NULL default '0',
57
          PRIMARY KEY (field_name)
JonBob's avatar
JonBob committed
58 59 60 61 62 63 64
        )");
      db_query("CREATE TABLE {node_field_instance} (
          field_name varchar(32) NOT NULL default '',
          type_name varchar(32) NOT NULL default '',
          weight integer NOT NULL default '0',
          label varchar(255) NOT NULL default '',
          widget_type varchar(32) NOT NULL default '',
JonBob's avatar
JonBob committed
65
          widget_settings text NOT NULL,
66
          display_settings text NOT NULL,
JonBob's avatar
JonBob committed
67
          description text NOT NULL,
68
          PRIMARY KEY (field_name,type_name)
JonBob's avatar
JonBob committed
69 70 71 72 73
        )");
      break;
  }
}

JonBob's avatar
JonBob committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/**
 * Add storage for per-field help text.
 */
function content_update_2() {
  $ret = array();

  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_add_column($ret, 'node_field_instance', 'description', 'text', array('not null' => TRUE, 'default' => ''));
      break;

    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {node_field_instance} ADD COLUMN description mediumtext NOT NULL");
      break;
  }

  return $ret;
}
JonBob's avatar
JonBob committed
93

JonBob's avatar
JonBob committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
/**
 * Add information about where data is stored.
 */
function content_update_3() {
  $ret = array();

  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_add_column($ret, 'node_field', 'db_storage', 'integer', array('not null' => TRUE, 'default' => '0'));
      break;

    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {node_field} ADD COLUMN db_storage int NOT NULL default 0");
      break;
  }

  return $ret;
}

/**
 * Add tables for content types to store their data.
 */
function content_update_4() {
  $ret = array();

  $result = db_query("SELECT type_name FROM {node_type}");
  while ($type = db_fetch_object($result)) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        $ret[] = update_sql("CREATE TABLE {node_". strtr($type->type_name, '-', '_') ."} (
            vid int unsigned NOT NULL default '0',
            nid int unsigned NOT NULL default '0',
            PRIMARY KEY (vid)
yched's avatar
yched committed
129
          ) /*!40100 DEFAULT CHARACTER SET utf8 */");
JonBob's avatar
JonBob committed
130 131 132 133
        break;

      case 'pgsql':
        $ret[] = update_sql("CREATE TABLE {node_". strtr($type->type_name, '-', '_') ."} (
134 135
            vid integer NOT NULL default '0' CHECK(vid >= 0),
            nid integer NOT NULL default '0' CHECK(nid >= 0),
JonBob's avatar
JonBob committed
136 137 138
            PRIMARY KEY (vid)
          )");
        break;
139
    }
JonBob's avatar
JonBob committed
140 141 142 143 144 145 146 147 148 149 150
  }

  return $ret;
}

/**
 * Move data from per-field storage to per-content-type storage where possible.
 */
function content_update_5() {
  $ret = array();

151 152
  include_once('./'.drupal_get_path('module', 'content') .'/content.module');
  include_once('./'.drupal_get_path('module', 'content') .'/content_admin.inc');
JonBob's avatar
JonBob committed
153 154 155 156 157 158 159 160 161

  $result = db_query('SELECT nf.field_name FROM {node_field} nf LEFT JOIN {node_field_instance} nfi ON nfi.field_name = nf.field_name WHERE nf.multiple = 0 AND nf.db_storage = 0 GROUP BY nfi.field_name HAVING COUNT(*) = 1');

  if (db_num_rows($result)) {
    // Multi-part update
    if (!isset($_SESSION['content_update_5'])) {
      $_SESSION['content_update_5'] = 0;
      $_SESSION['content_update_5_max'] = db_num_rows($result);
    }
162

JonBob's avatar
JonBob committed
163
    $field = db_fetch_array($result);
John Hwang's avatar
John Hwang committed
164
    $fields = content_fields();
JonBob's avatar
JonBob committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    $field = $fields[$field['field_name']];
    $field_types = _content_field_types();
    $field_type = $field_types[$field['type']];
    $columns = module_invoke($field_type['module'], 'field_settings', 'database columns', $field);

    $ret[] = update_sql("UPDATE {node_field} SET db_storage = ". CONTENT_DB_STORAGE_PER_CONTENT_TYPE ." WHERE field_name = '". $field['field_name'] ."'");
    if (is_array($columns) && count($columns)) {
      $new_field = $field;
      $new_field['db_storage'] = CONTENT_DB_STORAGE_PER_CONTENT_TYPE;
      content_alter_db_field($field, $columns, $new_field, $columns);
    }

    $_SESSION['content_update_5']++;
    $ret['#finished'] = $_SESSION['content_update_5'] / $_SESSION['content_update_5_max'];
    return $ret;
  }
}
182 183 184 185 186 187 188

/**
 * The cache for nodes has changed to account for revisions correctly.
 */
function content_update_6() {
  return array(update_sql('DELETE FROM {cache}'));
}
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

/**
 * Rename the "content-" prefix to "content_" to aid in form theming.
 */
function content_update_7() {
  $ret = array();

  $type_result = db_query("SELECT type_name FROM {node_type} WHERE type_name LIKE 'content-%%'");

  if (db_num_rows($type_result)) {
    // Multi-part update
    if (!isset($_SESSION['content_update_7'])) {
      $_SESSION['content_update_7'] = 0;
      $_SESSION['content_update_7_max'] = db_num_rows($type_result);
    }

    $type = db_fetch_object($type_result);
    $old_type_name = $type->type_name;
    $new_type_name = str_replace('content-', 'content_', $old_type_name);
    $ret[] = update_sql("UPDATE {node} SET type = '". $new_type_name ."' WHERE type = '". $old_type_name ."'");
    $ret[] = update_sql("UPDATE {node_type} SET type_name = '". $new_type_name ."' WHERE type_name = '". $old_type_name ."'");
    $ret[] = update_sql("UPDATE {node_field_instance} SET type_name = '". $new_type_name ."' WHERE type_name = '". $old_type_name ."'");
    $ret[] = update_sql("UPDATE {vocabulary_node_types} SET type = '". $new_type_name ."' WHERE type = '". $old_type_name ."'");

    $variable_result = db_query("SELECT name, value FROM {variable} WHERE name LIKE '%%%s%%' OR value LIKE '%%%s%%'", $old_type_name, $old_type_name);
    while ($variable = db_fetch_object($variable_result)) {
      $new_name = str_replace($old_type_name, $new_type_name, $variable->name);
      $new_value = str_replace($old_type_name, $new_type_name, $variable->value);
      db_query("UPDATE {variable} SET name = '%s', value = '%s' WHERE name = '%s'", $new_name, $new_value, $variable->name);
    }

    $ret[] = update_sql('DELETE FROM {cache}');

    $_SESSION['content_update_7']++;
    $ret['#finished'] = $_SESSION['content_update_7'] / $_SESSION['content_update_7_max'];
    return $ret;
  }
}
227 228 229 230 231 232

/**
 * Rename the "node_type" table to avoid a conflict with a later core addition
 */
function content_update_8() {
  $ret = array();
233

234 235 236 237 238 239 240 241 242 243 244 245 246
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('RENAME TABLE {node_type} TO {node_type_content}');
      break;

    case 'pgsql':
      $ret[] = update_sql('ALTER TABLE {node_type} RENAME TO {node_type_content}');
      break;
  }

  return $ret;
}
247 248 249 250 251 252 253 254 255

/**
 * Add display_settings column
 */
function content_update_9() {
  $ret = array();

  switch ($GLOBALS['db_type']) {
    case 'pgsql':
256
      db_add_column($ret, 'node_field_instance', 'display_settings', 'text', array('not null' => TRUE, 'default' => "''"));
257 258 259 260 261 262 263 264 265 266
      break;

    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {node_field_instance} ADD COLUMN display_settings mediumtext NOT NULL");
      break;
  }

  return $ret;
}
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

/**
 *  Fix corrupted db due to a bug in 1.3 release (http://drupal.org/node/115332)
 */
function content_update_10() {
  $ret = array();

  include_once('./'. drupal_get_path('module', 'content') .'/content.module');
  include_once('./'. drupal_get_path('module', 'content') .'/content_admin.inc');

  // drop fields with no field instances
  $fields = array();
  $result = db_query("SELECT DISTINCT(field_name)  FROM {node_field_instance}");
  while ($row = db_fetch_array($result)) {
    $fields[] = "'". $row['field_name'] ."'";
  }
283 284 285
  if ($fields) {
    $ret[] = update_sql("DELETE FROM {node_field} WHERE field_name NOT IN (". implode(', ', $fields) .")");
  }
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

  // set invalid 'per field storage' back to 'per content type'
  $result = db_query("SELECT field_name FROM {node_field} WHERE multiple = 0 AND db_storage = %d", CONTENT_DB_STORAGE_PER_FIELD);
  while ($row = db_fetch_array($result)) {
    $count = db_num_rows(db_query("SELECT field_name FROM {node_field_instance} WHERE field_name = '%s'", $row['field_name']));
    if ($count == 1) {
      $field = content_fields($row['field_name']);
      $db_info = content_database_info($field);
      $new_field = $field;
      $new_field['db_storage'] = CONTENT_DB_STORAGE_PER_CONTENT_TYPE;
      content_alter_db_field($field, $db_info['columns'], $new_field, $db_info['columns']);
    }
  }

  return $ret;
}
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

/**
 *  Set text db columns to accept NULL values for mysql (see http://drupal.org/node/108094)
 */
function content_update_11() {
  $ret = array();

  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      include_once('./'. drupal_get_path('module', 'content') .'/content.module');
      include_once('./'. drupal_get_path('module', 'content') .'/content_admin.inc');

      $types = content_types();
      $fields = content_fields();

      foreach ($fields as $field) {
        $db_info = content_database_info($field);
        foreach ($db_info['columns'] as $column => $attributes) {
          if (in_array($attributes['type'], array('text', 'mediumtext', 'longtext'))) {
            content_db_change_column($db_info['table'], $attributes['column'], $attributes['column'], $attributes['type'], $attributes);
            $ret[] = array(
              'query' => strtr('The text field %field has been updated to accept NULL values.', array('%field' => $field['field_name'])),
              'success' => TRUE
            );
          }
        }
      }
  }

  return $ret;
}