Removing translation directories
[project/cck.git] / nodereference.install
CommitLineData
76c4cefe
JC
1<?php
2
2cd2740e
JC
3/**
4 * Implementation of hook_install().
5 */
76c4cefe 6function nodereference_install() {
76c4cefe
JC
7}
8
bbbd884e
JC
9/**
10 * Add node ID column so we can delete old revisions at node delete time.
11 */
59f3a596
JC
12function nodereference_update_1() {
13 $ret = array();
76c4cefe 14
59f3a596
JC
15 switch ($GLOBALS['db_type']) {
16 case 'pgsql':
17 db_add_column($ret, 'node_field_nodereference_data', 'nid', 'integer', array('not null' => TRUE, 'default' => 0));
18 break;
19
20 case 'mysql':
21 case 'mysqli':
22 $ret[] = update_sql("ALTER TABLE {node_field_nodereference_data} ADD COLUMN nid int(10) NOT NULL DEFAULT 0");
23 break;
24 }
25
26 return $ret;
27}
28
bbbd884e
JC
29/**
30 * Populate nid column in existing node reference fields.
31 */
59f3a596
JC
32function nodereference_update_2() {
33 // Multi-part update
34 if (!isset($_SESSION['nodereference_update_2'])) {
35 $_SESSION['nodereference_update_2'] = 0;
36 $_SESSION['nodereference_update_2_max'] = db_result(db_query("SELECT COUNT(*) FROM {node_field_nodereference_data}"));;
37 }
38
39 $limit = 20;
40 $result = db_query_range("SELECT nr.nid, nfnd.vid, nfnd.field_name, nfnd.delta FROM {node_field_nodereference_data} nfnd LEFT JOIN {node_revisions} nr ON nr.vid = nfnd.vid", $_SESSION['nodereference_update_2'], $limit);
41 if (db_num_rows($result) == 0) {
42 unset($_SESSION['nodereference_update_2']);
43 unset($_SESSION['nodereference_update_2_max']);
44 return array();
45 }
46 while ($data = db_fetch_object($result)) {
47 $_SESSION['nodereference_update_2']++;
48 db_query("UPDATE {node_field_nodereference_data} SET nid = %d WHERE vid = %d AND field_name = '%s' AND delta = %d", $data->nid, $data->vid, $data->field_name, $data->delta);
49 }
50
51 return array('#finished' => $_SESSION['nodereference_update_2'] / $_SESSION['nodereference_update_2_max']);
52}
bbbd884e
JC
53
54/**
55 * Data is now stored in per-field tables.
56 */
57function nodereference_update_3() {
58 $ret = array();
59
b632625d
YC
60 include_once('./'. drupal_get_path('module', 'content') .'/content.module');
61 include_once('./'. drupal_get_path('module', 'content') .'/content_admin.inc');
bbbd884e 62
d658947d 63 content_clear_type_cache();
39f169c7 64 $fields = content_fields();
bbbd884e
JC
65
66 foreach ($fields as $field) {
67 switch ($field['type']) {
68 case 'nodereference':
69 $columns = array(
70 'nid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
71 );
d658947d
YC
72 // the following line will trigger (negligible) warnings if content_update_5 was run before
73 // (column already exists)
74 @content_alter_db_field(array(), array(), $field, $columns);
75 $db_info = content_database_info($field);
76 $table = $db_info['table'];
bbbd884e 77 if ($field['multiple']) {
d658947d 78 $ret[] = update_sql('INSERT INTO {'. $table .'} (vid, delta, nid, '. $field['field_name'] ."_nid) SELECT vid, delta, nid, field_nid FROM {node_field_nodereference_data} WHERE field_name = '". $field['field_name'] ."'");
bbbd884e
JC
79 }
80 else {
d658947d 81 $ret[] = update_sql('INSERT INTO {'. $table .'} (vid, nid, '. $field['field_name'] ."_nid) SELECT vid, nid, field_nid FROM {node_field_nodereference_data} WHERE field_name = '". $field['field_name'] ."'");
bbbd884e
JC
82 }
83 break;
84 }
85 }
86
87 $ret[] = update_sql('DROP TABLE {node_field_nodereference_data}');
88
89 db_query('DELETE FROM {cache}');
90 return $ret;
91}