taxonomy.install 16.1 KB
Newer Older
1 2 3
<?php
// $Id$

4 5 6 7 8
/**
 * @file
 * Install, update and uninstall functions for the taxonomy module.
 */

9
/**
10
 * Implements hook_uninstall().
11 12
 */
function taxonomy_uninstall() {
13 14 15
  // Remove variables.
  variable_del('taxonomy_override_selector');
  variable_del('taxonomy_terms_per_page_admin');
16 17
}

18
/**
19
 * Implements hook_schema().
20 21
 */
function taxonomy_schema() {
22
  $schema['taxonomy_term_data'] = array(
23
    'description' => 'Stores term information.',
24
    'fields' => array(
25 26 27 28
      'tid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
29
        'description' => 'Primary Key: Unique term ID.',
30 31 32 33 34 35
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
36
        'description' => 'The {taxonomy_vocabulary}.vid of the vocabulary to which the term is assigned.',
37 38 39 40 41 42
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
43
        'description' => 'The term name.',
44
        'translatable' => TRUE,
45 46 47 48 49
      ),
      'description' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
50
        'description' => 'A description of the term.',
51
        'translatable' => TRUE,
52
      ),
53 54 55 56 57 58 59
      'format' => array(
        'type' => 'int',
        'size' => 'small',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {filter_format}.format of the description.',
      ),
60 61 62 63
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
64
        'description' => 'The weight of this term in relation to other terms.',
65
      ),
66 67
    ),
    'primary key' => array('tid'),
68 69 70
    'foreign keys' => array(
      'vid' => array('taxonomy_vocabulary' => 'vid'),
    ),
71 72
    'indexes' => array(
      'taxonomy_tree' => array('vid', 'weight', 'name'),
73
      'vid_name' => array('vid', 'name'),
74
      'name' => array('name'),
75
    ),
76 77
  );

78
  $schema['taxonomy_term_hierarchy'] = array(
79
    'description' => 'Stores the hierarchical relationship between terms.',
80
    'fields' => array(
81 82 83 84 85
      'tid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
86
        'description' => 'Primary Key: The {taxonomy_term_data}.tid of the term.',
87 88 89 90 91 92
      ),
      'parent' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
93
        'description' => "Primary Key: The {taxonomy_term_data}.tid of the term's parent. 0 indicates no parent.",
94
      ),
95 96 97 98
    ),
    'indexes' => array(
      'parent' => array('parent'),
    ),
99 100 101
    'foreign keys' => array(
      'tid' => array('taxonomy_term_data' => 'tid'),
    ),
102 103 104
    'primary key' => array('tid', 'parent'),
  );

105
  $schema['taxonomy_vocabulary'] = array(
106
    'description' => 'Stores vocabulary information.',
107
    'fields' => array(
108 109 110 111
      'vid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
112
        'description' => 'Primary Key: Unique vocabulary ID.',
113 114 115 116 117 118
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
119
        'description' => 'Name of the vocabulary.',
120
        'translatable' => TRUE,
121
      ),
122 123 124 125 126 127 128
      'machine_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The vocabulary machine name.',
      ),
129 130 131 132
      'description' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
133
        'description' => 'Description of the vocabulary.',
134
        'translatable' => TRUE,
135 136 137 138 139 140 141
      ),
      'hierarchy' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
142
        'description' => 'The type of hierarchy allowed within the vocabulary. (0 = disabled, 1 = single, 2 = multiple)',
143 144 145 146 147 148
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
149
        'description' => 'The module which created the vocabulary.',
150 151 152 153 154
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
155
        'description' => 'The weight of this vocabulary in relation to other vocabularies.',
156
      ),
157 158
    ),
    'primary key' => array('vid'),
159 160 161
    'indexes' => array(
      'list' => array('weight', 'name'),
    ),
162 163 164
    'unique keys' => array(
      'machine_name' => array('machine_name'),
    ),
165 166
  );

167 168
  $schema['taxonomy_index'] = array(
    'description' => 'Maintains denormalized information about node/term relationships.',
169
    'fields' => array(
170 171
      'nid' => array(
        'description' => 'The {node}.nid this record tracks.',
172 173 174 175 176
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
      'tid' => array(
         'description' => 'The term ID.',
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
         'default' => 0,
      ),
      'sticky' => array(
        'description' => 'Boolean indicating whether the node is sticky.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the node was created.',
        'type' => 'int',
        'unsigned' => TRUE,
195
        'not null' => TRUE,
196
        'default'=> 0,
197
      ),
198
    ),
199
    'indexes' => array(
200
      'term_node' => array('tid', 'sticky', 'created'),
201
      'nid' => array('nid'),
202
    ),
203
    'foreign keys' => array(
204 205
      'node' => 'nid',
      'taxonomy_term_data' => 'tid',
206
    ),
207 208 209 210 211
  );

  return $schema;
}

212 213 214 215 216 217 218 219 220 221
/**
 * Implements hook_update_dependencies().
 */
function taxonomy_update_dependencies() {
  // Taxonomy update 7002 creates comment Field API bundles and therefore must
  // run after the Field module has been enabled, but before upgrading field
  // data.
  $dependencies['taxonomy'][7002] = array(
    'system' => 7049,
  );
222 223 224
  $dependencies['user'][7006] = array(
    'taxonomy' => 7002,
  );
225 226 227 228 229 230 231
  $dependencies['system'][7050] = array(
    'taxonomy' => 7002,
  );
  // It also must run before nodes are upgraded to use the Field API.
  $dependencies['node'][7006] = array(
    'taxonomy' => 7002,
  );
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  return $dependencies;
}

/**
 * Rename taxonomy tables.
 */
function taxonomy_update_7001() {
  db_rename_table('term_data', 'taxonomy_term_data');
  db_rename_table('term_hierarchy', 'taxonomy_term_hierarchy');
  db_rename_table('term_node', 'taxonomy_term_node');
  db_rename_table('term_relation', 'taxonomy_term_relation');
  db_rename_table('term_synonym', 'taxonomy_term_synonym');
  db_rename_table('vocabulary', 'taxonomy_vocabulary');
  db_rename_table('vocabulary_node_types', 'taxonomy_vocabulary_node_type');
}

249
/**
250
 * Add {vocabulary}.machine_name column.
251 252
 */
function taxonomy_update_7002() {
253 254 255 256 257 258 259
  $field = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
    'description' => 'The vocabulary machine name.',
  );
260

261
  db_add_field('taxonomy_vocabulary', 'machine_name', $field);
262

263 264 265 266
  // Do a direct query here, rather than calling taxonomy_get_vocabularies(),
  // in case Taxonomy module is disabled.
  $vids = db_query('SELECT vid FROM {taxonomy_vocabulary}')->fetchCol();
  foreach ($vids as $vid) {
267 268
    $machine_name = 'vocabulary_' . $vid;
    db_update('taxonomy_vocabulary')
269
      ->fields(array('machine_name' => $machine_name))
270 271 272
      ->condition('vid', $vid)
      ->execute();
    field_attach_create_bundle('taxonomy_term', $machine_name);
273
  }
274 275 276 277

  // The machine_name unique key can only be added after we ensure the
  // machine_name column contains unique values.
  db_add_unique_key('taxonomy_vocabulary', 'machine_name', array('machine_name'));
278
}
279 280 281 282 283 284 285 286

/**
 * Remove the related terms setting from vocabularies.
 *
 * This setting has not been used since Drupal 6. The {taxonomy_relations} table
 * itself is retained to allow for data to be upgraded.
 */
function taxonomy_update_7003() {
287
  db_drop_field('taxonomy_vocabulary', 'relations');
288
}
289 290 291 292 293 294 295 296 297 298 299 300 301 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

/**
 * Move taxonomy vocabulary associations for nodes to fields and field instances.
 */
function taxonomy_update_7004() {
  $taxonomy_index = array(
    'description' => 'Maintains denormalized information about node/term relationships.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {node}.nid this record tracks.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'tid' => array(
         'description' => 'The term ID.',
         'type' => 'int',
         'unsigned' => TRUE,
         'not null' => TRUE,
         'default' => 0,
      ),
      'sticky' => array(
        'description' => 'Boolean indicating whether the node is sticky.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the node was created.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default'=> 0,
      ),
    ),
    'indexes' => array(
      'term_node' => array('tid', 'sticky', 'created'),
328
      'nid' => array('nid'),
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    ),
    'foreign keys' => array(
      'node' => 'nid',
      'taxonomy_term_data' => 'tid',
    ),
  );
  db_create_table('taxonomy_index', $taxonomy_index);

  // Use an inline version of Drupal 6 taxonomy_get_vocabularies() here since
  // we can no longer rely on $vocabulary->nodes from the API function.
  $result = db_query('SELECT v.*, n.type FROM {taxonomy_vocabulary} v LEFT JOIN {taxonomy_vocabulary_node_type} n ON v.vid = n.vid ORDER BY v.weight, v.name');
  $vocabularies = array();
  foreach ($result as $record) {
    // If no node types are associated with a vocabulary, the LEFT JOIN will
    // return a NULL value for type.
    if (isset($record->type)) {
      $node_types[$record->vid][$record->type] = $record->type;
      unset($record->type);
      $record->nodes = $node_types[$record->vid];
    }
    elseif (!isset($record->nodes)) {
      $record->nodes = array();
    }
    $vocabularies[$record->vid] = $record;
  }

  foreach ($vocabularies as $vocabulary) {
    $field_name = 'taxonomy_' . $vocabulary->machine_name;
    $field = array(
      'field_name' => $field_name,
359
      'type' => 'taxonomy_term_reference',
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
      'cardinality' => $vocabulary->multiple || $vocabulary->tags ? FIELD_CARDINALITY_UNLIMITED : 1,
      'settings' => array(
        'required' => $vocabulary->required ? TRUE : FALSE,
        'allowed_values' => array(
          array(
            'vid' => $vocabulary->vid,
            'parent' => 0,
          ),
        ),
      ),
    );
    field_create_field($field);

    foreach ($vocabulary->nodes as $bundle) {
      $instance = array(
        'label' => $vocabulary->name,
        'field_name' => $field_name,
        'bundle' => $bundle,
378
        'entity_type' => 'node',
379 380 381 382
        'description' => $vocabulary->help,
        'widget' => array(
          'type' => $vocabulary->tags ? 'taxonomy_autocomplete' : 'select',
        ),
383 384 385 386 387 388 389 390 391 392
        'display' => array(
          'default' => array(
            'type' => 'taxonomy_term_reference_link',
            'weight' => 10,
          ),
          'teaser' => array(
            'type' => 'taxonomy_term_reference_link',
            'weight' => 10,
          ),
        ),
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
      );
      field_create_instance($instance);
    }
  }
  db_drop_table('taxonomy_vocabulary_node_type');
  $fields = array('help', 'multiple', 'required', 'tags');
  foreach ($fields as $field) {
    db_drop_field('taxonomy_vocabulary', $field);
  }
}

/**
 * Migrate {taxonomy_term_node} table to field storage.
 */
function taxonomy_update_7005(&$sandbox) {
  // Since we are upgrading from Drupal 6, we know that only
  // field_sql_storage.module will be enabled.
  $field = field_info_field($field['field_name']);
  $data_table = _field_sql_storage_tablename($field);
  $revision_table = _field_sql_storage_revision_tablename($field);
  $etid = _field_sql_storage_etid('node');
  $value_column = $field['field_name'] . '_value';
  $columns = array('etid', 'entity_id', 'revision_id', 'bundle', 'delta', $value_column);

  // This is a multi-pass update. On the first call we need to initialize some
  // variables.
  if (!isset($sandbox['total'])) {
    $sandbox['last'] = 0;
    $sandbox['count'] = 0;

    $query = db_select('taxonomy_term_node', 't');
    $sandbox['total'] = $query->countQuery()->execute()->fetchField();
    $found = (bool) $sandbox['total'];
  }
  else {
    // We do each pass in batches of 1000, this should result in a
    // maximum of 2000 insert queries each operation.
    $batch = 1000 + $sandbox['last'];

    // Query and save data for the current revision.
    $result = db_query_range('SELECT td.tid, tn.nid, td.weight, tn.vid, n2.type, n2.created, n2.sticky FROM {taxonomy_term_data} td INNER JOIN {taxonomy_term_node} tn ON td.tid = tn.tid INNER JOIN {node} n2 ON tn.nid = n2.nid INNER JOIN {node} n ON tn.vid = n.vid AND td.vid = :vocabulary_id ORDER BY td.weight ASC', array(':vocabulary_id' => $vocabulary->vid), $sandbox['last'], $batch);
    $deltas = array();
    foreach ($result as $record) {
      $found = TRUE;
      $sandbox['count'] += 1;
      // Start deltas from 0, and increment by one for each
      // term attached to a node.
      $deltas[$record->nid] = isset($deltas[$record->nid]) ? ++$deltas[$record->nid] : 0;
      $values = array($etid, $record->nid, $record->vid, $record->type, $deltas[$record->nid], $record->tid);
      db_insert($data_table)->fields($columns)->values($values)->execute();

      // Update the {taxonomy_index} table.
      db_insert('taxonomy_index')
        ->fields(array('nid', 'tid', 'sticky', 'created',))
        ->values(array($record->nid, $record->tid, $record->sticky, $record->created))
        ->execute();
    }

    // Query and save data for all revisions.
    $result = db_query('SELECT td.tid, tn.nid, td.weight, tn.vid, n.type FROM {taxonomy_term_data} td INNER JOIN {taxonomy_term_node} tn ON td.tid = tn.tid AND td.vid = :vocabulary_id INNER JOIN {node} n ON tn.nid = n.nid ORDER BY td.weight ASC', array(':vocabulary_id' => $vocabulary->vid), $sandbox['last'][$batch]);
    $deltas = array();
    foreach ($result as $record) {
      $found = TRUE;
      $sandbox['count'] += 1;
      // Start deltas at 0, and increment by one for each term attached to a revision.
      $deltas[$record->vid] = isset($deltas[$record->vid]) ? ++$deltas[$record->vid] : 0;
      $values = array($etid, $record->nid, $record->vid, $record->type, $deltas[$record->vid], $record->tid);
      db_insert($revision_table)->fields($columns)->values($values)->execute();
    }
    $sandbox['last'] = $batch;
  }
  if (!$found) {
   db_drop_table('taxonomy_term_node');
  }
}
468 469

/**
470
 * Add {taxonomy_term_data}.format column.
471 472 473 474 475 476 477 478 479 480 481
 */
function taxonomy_update_7006() {
  db_add_field('taxonomy_term_data', 'format', array(
    'type' => 'int',
    'size' => 'small',
    'not null' => TRUE,
    'default' => 0,
    'description' => 'The {filter_format}.format of the description.',
  ));
}

482
/**
483
 * Add index on {taxonomy_term_data}.name column to speed up taxonomy_get_term_by_name().
484 485 486 487 488
 */
function taxonomy_update_7007() {
  db_add_index('taxonomy_term_data', 'name', array('name'));
}

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
/**
 * Change the weight columns to normal int.
 */
function taxonomy_update_7008() {
  db_drop_index('taxonomy_term_data', 'taxonomy_tree');
  db_change_field('taxonomy_term_data', 'weight', 'weight', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'description' => 'The weight of this term in relation to other terms.',
  ), array(
    'indexes' => array(
       'taxonomy_tree' => array('vid', 'weight', 'name'),
    ),
  ));

  db_drop_index('taxonomy_vocabulary', 'list');
  db_change_field('taxonomy_vocabulary', 'weight', 'weight', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'description' => 'The weight of this vocabulary in relation to other vocabularies.',
  ), array(
    'indexes' => array(
      'list' => array('weight', 'name'),
    ),
  ));
}