/[drupal]/drupal/modules/taxonomy/taxonomy.install
ViewVC logotype

Contents of /drupal/modules/taxonomy/taxonomy.install

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.27 - (show annotations) (download) (as text)
Fri Oct 16 19:06:24 2009 UTC (6 weeks, 2 days ago) by dries
Branch: MAIN
Changes since 1.26: +5 -1 lines
File MIME type: text/x-php
- Patch #593746 by #593746 by sun, andypost: prepare Drupal core for dynamic data translation.
1 <?php
2 // $Id: taxonomy.install,v 1.26 2009/10/15 14:34:07 dries Exp $
3
4 /**
5 * @file
6 * Install, update and uninstall functions for the taxonomy module.
7 */
8
9 /**
10 * Implement hook_uninstall().
11 */
12 function taxonomy_uninstall() {
13 // Remove variables.
14 variable_del('taxonomy_override_selector');
15 variable_del('taxonomy_terms_per_page_admin');
16 }
17
18 /**
19 * Implement hook_schema().
20 */
21 function taxonomy_schema() {
22 $schema['taxonomy_term_data'] = array(
23 'description' => 'Stores term information.',
24 'fields' => array(
25 'tid' => array(
26 'type' => 'serial',
27 'unsigned' => TRUE,
28 'not null' => TRUE,
29 'description' => 'Primary Key: Unique term ID.',
30 ),
31 'vid' => array(
32 'type' => 'int',
33 'unsigned' => TRUE,
34 'not null' => TRUE,
35 'default' => 0,
36 'description' => 'The {taxonomy_vocabulary}.vid of the vocabulary to which the term is assigned.',
37 ),
38 'name' => array(
39 'type' => 'varchar',
40 'length' => 255,
41 'not null' => TRUE,
42 'default' => '',
43 'description' => 'The term name.',
44 'translatable' => TRUE,
45 ),
46 'description' => array(
47 'type' => 'text',
48 'not null' => FALSE,
49 'size' => 'big',
50 'description' => 'A description of the term.',
51 'translatable' => TRUE,
52 ),
53 'weight' => array(
54 'type' => 'int',
55 'not null' => TRUE,
56 'default' => 0,
57 'size' => 'tiny',
58 'description' => 'The weight of this term in relation to other terms.',
59 ),
60 ),
61 'primary key' => array('tid'),
62 'foreign keys' => array(
63 'vid' => array('taxonomy_vocabulary' => 'vid'),
64 ),
65 'indexes' => array(
66 'taxonomy_tree' => array('vid', 'weight', 'name'),
67 'vid_name' => array('vid', 'name'),
68 ),
69 );
70
71 $schema['taxonomy_term_hierarchy'] = array(
72 'description' => 'Stores the hierarchical relationship between terms.',
73 'fields' => array(
74 'tid' => array(
75 'type' => 'int',
76 'unsigned' => TRUE,
77 'not null' => TRUE,
78 'default' => 0,
79 'description' => 'Primary Key: The {taxonomy_term_data}.tid of the term.',
80 ),
81 'parent' => array(
82 'type' => 'int',
83 'unsigned' => TRUE,
84 'not null' => TRUE,
85 'default' => 0,
86 'description' => "Primary Key: The {taxonomy_term_data}.tid of the term's parent. 0 indicates no parent.",
87 ),
88 ),
89 'indexes' => array(
90 'parent' => array('parent'),
91 ),
92 'foreign keys' => array(
93 'tid' => array('taxonomy_term_data' => 'tid'),
94 ),
95 'primary key' => array('tid', 'parent'),
96 );
97
98 $schema['taxonomy_vocabulary'] = array(
99 'description' => 'Stores vocabulary information.',
100 'fields' => array(
101 'vid' => array(
102 'type' => 'serial',
103 'unsigned' => TRUE,
104 'not null' => TRUE,
105 'description' => 'Primary Key: Unique vocabulary ID.',
106 ),
107 'name' => array(
108 'type' => 'varchar',
109 'length' => 255,
110 'not null' => TRUE,
111 'default' => '',
112 'description' => 'Name of the vocabulary.',
113 'translatable' => TRUE,
114 ),
115 'machine_name' => array(
116 'type' => 'varchar',
117 'length' => 255,
118 'not null' => TRUE,
119 'default' => '',
120 'description' => 'The vocabulary machine name.',
121 ),
122 'description' => array(
123 'type' => 'text',
124 'not null' => FALSE,
125 'size' => 'big',
126 'description' => 'Description of the vocabulary.',
127 'translatable' => TRUE,
128 ),
129 'relations' => array(
130 'type' => 'int',
131 'unsigned' => TRUE,
132 'not null' => TRUE,
133 'default' => 0,
134 'size' => 'tiny',
135 'description' => 'Whether or not related terms are enabled within the vocabulary. (0 = disabled, 1 = enabled)',
136 ),
137 'hierarchy' => array(
138 'type' => 'int',
139 'unsigned' => TRUE,
140 'not null' => TRUE,
141 'default' => 0,
142 'size' => 'tiny',
143 'description' => 'The type of hierarchy allowed within the vocabulary. (0 = disabled, 1 = single, 2 = multiple)',
144 ),
145 'module' => array(
146 'type' => 'varchar',
147 'length' => 255,
148 'not null' => TRUE,
149 'default' => '',
150 'description' => 'The module which created the vocabulary.',
151 ),
152 'weight' => array(
153 'type' => 'int',
154 'not null' => TRUE,
155 'default' => 0,
156 'size' => 'tiny',
157 'description' => 'The weight of this vocabulary in relation to other vocabularies.',
158 ),
159 ),
160 'primary key' => array('vid'),
161 'indexes' => array(
162 'list' => array('weight', 'name'),
163 ),
164 );
165
166 $schema['taxonomy_index'] = array(
167 'description' => 'Maintains denormalized information about node/term relationships.',
168 'fields' => array(
169 'nid' => array(
170 'description' => 'The {node}.nid this record tracks.',
171 'type' => 'int',
172 'unsigned' => TRUE,
173 'not null' => TRUE,
174 'default' => 0,
175 ),
176 'tid' => array(
177 'description' => 'The term ID.',
178 'type' => 'int',
179 'unsigned' => TRUE,
180 'not null' => TRUE,
181 'default' => 0,
182 ),
183 'sticky' => array(
184 'description' => 'Boolean indicating whether the node is sticky.',
185 'type' => 'int',
186 'not null' => FALSE,
187 'default' => 0,
188 'size' => 'tiny',
189 ),
190 'created' => array(
191 'description' => 'The Unix timestamp when the node was created.',
192 'type' => 'int',
193 'unsigned' => TRUE,
194 'not null' => TRUE,
195 'default'=> 0,
196 ),
197 ),
198 'indexes' => array(
199 'term_node' => array('tid', 'sticky', 'created'),
200 ),
201 'foreign keys' => array(
202 'node' => 'nid',
203 'taxonomy_term_data' => 'tid',
204 ),
205 );
206
207 return $schema;
208 }
209
210 /**
211 * Add vocabulary machine_name column.
212 */
213 function taxonomy_update_7002() {
214 $field = array(
215 'type' => 'varchar',
216 'length' => 255,
217 'not null' => TRUE,
218 'default' => '',
219 'description' => 'The vocabulary machine name.',
220 );
221
222 db_add_field('taxonomy_vocabulary', 'machine_name', $field);
223
224 foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
225 $machine_name = 'vocabulary_' . $vid;
226 db_update('taxonomy_vocabulary')
227 ->fields(array('machine_name' => 'vocabulary_' . $vid))
228 ->condition('vid', $vid)
229 ->execute();
230 field_attach_create_bundle('taxonomy_term', $machine_name);
231 }
232 }
233
234 /**
235 * Remove the related terms setting from vocabularies.
236 *
237 * This setting has not been used since Drupal 6. The {taxonomy_relations} table
238 * itself is retained to allow for data to be upgraded.
239 */
240 function taxonomy_update_7003() {
241 db_drop_field('taxonomy_vocabulary', 'relations');
242 }
243
244 /**
245 * Move taxonomy vocabulary associations for nodes to fields and field instances.
246 */
247 function taxonomy_update_7004() {
248 $taxonomy_index = array(
249 'description' => 'Maintains denormalized information about node/term relationships.',
250 'fields' => array(
251 'nid' => array(
252 'description' => 'The {node}.nid this record tracks.',
253 'type' => 'int',
254 'unsigned' => TRUE,
255 'not null' => TRUE,
256 'default' => 0,
257 ),
258 'tid' => array(
259 'description' => 'The term ID.',
260 'type' => 'int',
261 'unsigned' => TRUE,
262 'not null' => TRUE,
263 'default' => 0,
264 ),
265 'sticky' => array(
266 'description' => 'Boolean indicating whether the node is sticky.',
267 'type' => 'int',
268 'not null' => FALSE,
269 'default' => 0,
270 'size' => 'tiny',
271 ),
272 'created' => array(
273 'description' => 'The Unix timestamp when the node was created.',
274 'type' => 'int',
275 'unsigned' => TRUE,
276 'not null' => TRUE,
277 'default'=> 0,
278 ),
279 ),
280 'indexes' => array(
281 'term_node' => array('tid', 'sticky', 'created'),
282 ),
283 'foreign keys' => array(
284 'node' => 'nid',
285 'taxonomy_term_data' => 'tid',
286 ),
287 );
288 db_create_table('taxonomy_index', $taxonomy_index);
289
290 // Use an inline version of Drupal 6 taxonomy_get_vocabularies() here since
291 // we can no longer rely on $vocabulary->nodes from the API function.
292 $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');
293 $vocabularies = array();
294 foreach ($result as $record) {
295 // If no node types are associated with a vocabulary, the LEFT JOIN will
296 // return a NULL value for type.
297 if (isset($record->type)) {
298 $node_types[$record->vid][$record->type] = $record->type;
299 unset($record->type);
300 $record->nodes = $node_types[$record->vid];
301 }
302 elseif (!isset($record->nodes)) {
303 $record->nodes = array();
304 }
305 $vocabularies[$record->vid] = $record;
306 }
307
308 foreach ($vocabularies as $vocabulary) {
309 $field_name = 'taxonomy_' . $vocabulary->machine_name;
310 $field = array(
311 'field_name' => $field_name,
312 'type' => 'taxonomy_term',
313 'cardinality' => $vocabulary->multiple || $vocabulary->tags ? FIELD_CARDINALITY_UNLIMITED : 1,
314 'settings' => array(
315 'required' => $vocabulary->required ? TRUE : FALSE,
316 'allowed_values' => array(
317 array(
318 'vid' => $vocabulary->vid,
319 'parent' => 0,
320 ),
321 ),
322 ),
323 );
324 field_create_field($field);
325
326 foreach ($vocabulary->nodes as $bundle) {
327 $instance = array(
328 'label' => $vocabulary->name,
329 'field_name' => $field_name,
330 'bundle' => $bundle,
331 'description' => $vocabulary->help,
332 'widget' => array(
333 'type' => $vocabulary->tags ? 'taxonomy_autocomplete' : 'select',
334 ),
335 );
336 field_create_instance($instance);
337 }
338 }
339 db_drop_table('taxonomy_vocabulary_node_type');
340 $fields = array('help', 'multiple', 'required', 'tags');
341 foreach ($fields as $field) {
342 db_drop_field('taxonomy_vocabulary', $field);
343 }
344 }
345
346 /**
347 * Migrate {taxonomy_term_node} table to field storage.
348 */
349 function taxonomy_update_7005(&$sandbox) {
350 // Since we are upgrading from Drupal 6, we know that only
351 // field_sql_storage.module will be enabled.
352 $field = field_info_field($field['field_name']);
353 $data_table = _field_sql_storage_tablename($field);
354 $revision_table = _field_sql_storage_revision_tablename($field);
355 $etid = _field_sql_storage_etid('node');
356 $value_column = $field['field_name'] . '_value';
357 $columns = array('etid', 'entity_id', 'revision_id', 'bundle', 'delta', $value_column);
358
359 // This is a multi-pass update. On the first call we need to initialize some
360 // variables.
361 if (!isset($sandbox['total'])) {
362 $sandbox['last'] = 0;
363 $sandbox['count'] = 0;
364
365 $query = db_select('taxonomy_term_node', 't');
366 $sandbox['total'] = $query->countQuery()->execute()->fetchField();
367 $found = (bool) $sandbox['total'];
368 }
369 else {
370 // We do each pass in batches of 1000, this should result in a
371 // maximum of 2000 insert queries each operation.
372 $batch = 1000 + $sandbox['last'];
373
374 // Query and save data for the current revision.
375 $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);
376 $deltas = array();
377 foreach ($result as $record) {
378 $found = TRUE;
379 $sandbox['count'] += 1;
380 // Start deltas from 0, and increment by one for each
381 // term attached to a node.
382 $deltas[$record->nid] = isset($deltas[$record->nid]) ? ++$deltas[$record->nid] : 0;
383 $values = array($etid, $record->nid, $record->vid, $record->type, $deltas[$record->nid], $record->tid);
384 db_insert($data_table)->fields($columns)->values($values)->execute();
385
386 // Update the {taxonomy_index} table.
387 db_insert('taxonomy_index')
388 ->fields(array('nid', 'tid', 'sticky', 'created',))
389 ->values(array($record->nid, $record->tid, $record->sticky, $record->created))
390 ->execute();
391 }
392
393 // Query and save data for all revisions.
394 $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]);
395 $deltas = array();
396 foreach ($result as $record) {
397 $found = TRUE;
398 $sandbox['count'] += 1;
399 // Start deltas at 0, and increment by one for each term attached to a revision.
400 $deltas[$record->vid] = isset($deltas[$record->vid]) ? ++$deltas[$record->vid] : 0;
401 $values = array($etid, $record->nid, $record->vid, $record->type, $deltas[$record->vid], $record->tid);
402 db_insert($revision_table)->fields($columns)->values($values)->execute();
403 }
404 $sandbox['last'] = $batch;
405 }
406 if (!$found) {
407 db_drop_table('taxonomy_term_node');
408 }
409 }

  ViewVC Help
Powered by ViewVC 1.1.2