2007-10-05 16:07:22 +00:00
<?php
// $Id$
2009-05-13 19:42:18 +00:00
/**
* @file
* Install, update and uninstall functions for the taxonomy module.
*/
2009-01-19 01:53:26 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_uninstall().
2009-01-19 01:53:26 +00:00
*/
function taxonomy_uninstall() {
2009-01-31 16:22:50 +00:00
// Remove variables.
variable_del('taxonomy_override_selector');
variable_del('taxonomy_terms_per_page_admin');
2009-01-19 01:53:26 +00:00
}
2007-10-05 16:07:22 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_schema().
2007-10-05 16:07:22 +00:00
*/
function taxonomy_schema() {
2009-01-14 21:16:21 +00:00
$schema['taxonomy_term_data'] = array(
2008-11-15 13:01:11 +00:00
'description' => 'Stores term information.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'tid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
2008-11-15 13:01:11 +00:00
'description' => 'Primary Key: Unique term ID.',
2007-10-10 11:39:35 +00:00
),
'vid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
2009-01-14 21:16:21 +00:00
'description' => 'The {taxonomy_vocabulary}.vid of the vocabulary to which the term is assigned.',
2007-10-10 11:39:35 +00:00
),
'name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'The term name.',
2009-10-16 19:06:25 +00:00
'translatable' => TRUE,
2007-10-10 11:39:35 +00:00
),
'description' => array(
'type' => 'text',
'not null' => FALSE,
'size' => 'big',
2008-11-15 13:01:11 +00:00
'description' => 'A description of the term.',
2009-10-16 19:06:25 +00:00
'translatable' => TRUE,
2007-10-10 11:39:35 +00:00
),
2009-11-07 14:18:07 +00:00
'format' => array(
'type' => 'int',
'size' => 'small',
'not null' => TRUE,
'default' => 0,
'description' => 'The {filter_format}.format of the description.',
),
2007-10-10 11:39:35 +00:00
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
2008-11-15 13:01:11 +00:00
'description' => 'The weight of this term in relation to other terms.',
2007-10-10 11:39:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
'primary key' => array('tid'),
2009-06-01 22:07:10 +00:00
'foreign keys' => array(
'vid' => array('taxonomy_vocabulary' => 'vid'),
),
2008-01-08 07:46:41 +00:00
'indexes' => array(
'taxonomy_tree' => array('vid', 'weight', 'name'),
2007-12-18 12:59:22 +00:00
'vid_name' => array('vid', 'name'),
),
2007-10-05 16:07:22 +00:00
);
2009-01-14 21:16:21 +00:00
$schema['taxonomy_term_hierarchy'] = array(
2008-11-15 13:01:11 +00:00
'description' => 'Stores the hierarchical relationship between terms.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'tid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
2009-01-14 21:16:21 +00:00
'description' => 'Primary Key: The {taxonomy_term_data}.tid of the term.',
2007-10-10 11:39:35 +00:00
),
'parent' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
2009-01-14 21:16:21 +00:00
'description' => "Primary Key: The {taxonomy_term_data}.tid of the term's parent. 0 indicates no parent.",
2007-10-10 11:39:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
'indexes' => array(
'parent' => array('parent'),
),
2009-06-01 22:07:10 +00:00
'foreign keys' => array(
'tid' => array('taxonomy_term_data' => 'tid'),
),
2007-10-05 16:07:22 +00:00
'primary key' => array('tid', 'parent'),
);
2009-01-14 21:16:21 +00:00
$schema['taxonomy_vocabulary'] = array(
2008-11-15 13:01:11 +00:00
'description' => 'Stores vocabulary information.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2007-10-10 11:39:35 +00:00
'vid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
2008-11-15 13:01:11 +00:00
'description' => 'Primary Key: Unique vocabulary ID.',
2007-10-10 11:39:35 +00:00
),
'name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'Name of the vocabulary.',
2009-10-16 19:06:25 +00:00
'translatable' => TRUE,
2007-10-10 11:39:35 +00:00
),
2009-06-12 13:59:56 +00:00
'machine_name' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The vocabulary machine name.',
),
2007-10-10 11:39:35 +00:00
'description' => array(
'type' => 'text',
'not null' => FALSE,
'size' => 'big',
2008-11-15 13:01:11 +00:00
'description' => 'Description of the vocabulary.',
2009-10-16 19:06:25 +00:00
'translatable' => TRUE,
2007-10-10 11:39:35 +00:00
),
'relations' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
2008-11-15 13:01:11 +00:00
'description' => 'Whether or not related terms are enabled within the vocabulary. (0 = disabled, 1 = enabled)',
2007-10-10 11:39:35 +00:00
),
'hierarchy' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
2008-11-15 13:01:11 +00:00
'description' => 'The type of hierarchy allowed within the vocabulary. (0 = disabled, 1 = single, 2 = multiple)',
2007-10-10 11:39:35 +00:00
),
'module' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
2008-11-15 13:01:11 +00:00
'description' => 'The module which created the vocabulary.',
2007-10-10 11:39:35 +00:00
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
2009-10-08 07:58:47 +00:00
'description' => 'The weight of this vocabulary in relation to other vocabularies.',
2007-10-10 11:39:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
'primary key' => array('vid'),
2007-12-18 12:59:22 +00:00
'indexes' => array(
'list' => array('weight', 'name'),
),
2007-10-05 16:07:22 +00:00
);
2009-10-08 07:58:47 +00:00
$schema['taxonomy_index'] = array(
'description' => 'Maintains denormalized information about node/term relationships.',
2007-10-05 16:07:22 +00:00
'fields' => array(
2009-10-08 07:58:47 +00:00
'nid' => array(
'description' => 'The {node}.nid this record tracks.',
2007-10-10 11:39:35 +00:00
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
2009-10-08 07:58:47 +00:00
'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,
2007-12-08 14:06:23 +00:00
'not null' => TRUE,
2009-10-08 07:58:47 +00:00
'default'=> 0,
2007-10-10 11:39:35 +00:00
),
2007-10-05 16:07:22 +00:00
),
2007-12-18 12:59:22 +00:00
'indexes' => array(
2009-10-08 07:58:47 +00:00
'term_node' => array('tid', 'sticky', 'created'),
2007-12-18 12:59:22 +00:00
),
2009-06-01 22:07:10 +00:00
'foreign keys' => array(
2009-10-08 07:58:47 +00:00
'node' => 'nid',
'taxonomy_term_data' => 'tid',
2009-06-01 22:07:10 +00:00
),
2007-10-05 16:07:22 +00:00
);
return $schema;
}
2009-06-12 13:59:56 +00:00
/**
* Add vocabulary machine_name column.
*/
function taxonomy_update_7002() {
$field = array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'The vocabulary machine name.',
);
2009-09-29 15:13:57 +00:00
db_add_field('taxonomy_vocabulary', 'machine_name', $field);
2009-06-12 13:59:56 +00:00
foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
$machine_name = 'vocabulary_' . $vid;
db_update('taxonomy_vocabulary')
->fields(array('machine_name' => 'vocabulary_' . $vid))
->condition('vid', $vid)
->execute();
2009-10-15 12:44:36 +00:00
field_attach_create_bundle('taxonomy_term', $machine_name);
2009-06-12 13:59:56 +00:00
}
}
2009-08-04 06:50:07 +00:00
/**
* 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() {
2009-09-29 15:13:57 +00:00
db_drop_field('taxonomy_vocabulary', 'relations');
2009-08-04 06:50:07 +00:00
}
2009-10-08 07:58:47 +00:00
/**
* 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'),
),
'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,
'type' => 'taxonomy_term',
'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,
'description' => $vocabulary->help,
'widget' => array(
'type' => $vocabulary->tags ? 'taxonomy_autocomplete' : 'select',
),
);
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');
}
}
2009-11-07 14:18:07 +00:00
/**
* Add vocabulary machine_name column.
*/
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.',
));
}