This commit was manufactured as part of Drupal's Great Git Migration to
[project/cck.git] / userreference.install
1 <?php
2 // $Id$
3
4 /**
5 * Implementation of hook_install().
6 */
7 function userreference_install() {
8 }
9
10 /**
11 * Add node ID column so we can delete old revisions at node delete time.
12 */
13 function userreference_update_1() {
14 $ret = array();
15
16 switch ($GLOBALS['db_type']) {
17 case 'pgsql':
18 db_add_column($ret, 'node_field_userreference_data', 'nid', 'integer', array('not null' => TRUE, 'default' => 0));
19 break;
20
21 case 'mysql':
22 case 'mysqli':
23 $ret[] = update_sql("ALTER TABLE {node_field_userreference_data} ADD COLUMN nid int(10) NOT NULL DEFAULT 0");
24 break;
25 }
26
27 return $ret;
28 }
29
30 /**
31 * Populate nid column in existing user reference fields.
32 */
33 function userreference_update_2() {
34 // Multi-part update
35 if (!isset($_SESSION['userreference_update_2'])) {
36 $_SESSION['userreference_update_2'] = 0;
37 $_SESSION['userreference_update_2_max'] = db_result(db_query("SELECT COUNT(*) FROM {node_field_userreference_data}"));;
38 }
39
40 $limit = 20;
41 $result = db_query_range("SELECT nr.nid, nfud.vid, nfud.field_name, nfud.delta FROM {node_field_userreference_data} nfud LEFT JOIN {node_revisions} nr ON nr.vid = nfud.vid", $_SESSION['userreference_update_2'], $limit);
42 if (db_num_rows($result) == 0) {
43 unset($_SESSION['userreference_update_2']);
44 unset($_SESSION['userreference_update_2_max']);
45 return array();
46 }
47 while ($data = db_fetch_object($result)) {
48 $_SESSION['userreference_update_2']++;
49 db_query("UPDATE {node_field_userreference_data} SET nid = %d WHERE vid = %d AND field_name = '%s' AND delta = %d", $data->nid, $data->vid, $data->field_name, $data->delta);
50 }
51
52 return array('#finished' => $_SESSION['userreference_update_2'] / $_SESSION['userreference_update_2_max']);
53 }
54
55 /**
56 * Data is now stored in per-field tables.
57 */
58 function userreference_update_3() {
59 $ret = array();
60
61 include_once('./'. drupal_get_path('module', 'content') .'/content.module');
62 include_once('./'. drupal_get_path('module', 'content') .'/content_admin.inc');
63
64 content_clear_type_cache();
65 $fields = content_fields();
66
67 foreach ($fields as $field) {
68 switch ($field['type']) {
69 case 'userreference':
70 $columns = array(
71 'uid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),
72 );
73 // the following line will trigger (negligible) warnings if content_update_5 was run before
74 // (column already exists)
75 @content_alter_db_field(array(), array(), $field, $columns);
76 $db_info = content_database_info($field);
77 $table = $db_info['table'];
78 if ($field['multiple']) {
79 $ret[] = update_sql('INSERT INTO {'. $table .'} (vid, delta, nid, '. $field['field_name'] ."_uid) SELECT vid, delta, nid, field_uid FROM {node_field_userreference_data} WHERE field_name = '". $field['field_name'] ."'");
80 }
81 else {
82 $ret[] = update_sql('INSERT INTO {'. $table .'} (vid, nid, '. $field['field_name'] ."_uid) SELECT vid, nid, field_uid FROM {node_field_userreference_data} WHERE field_name = '". $field['field_name'] ."'");
83 }
84 break;
85 }
86 }
87
88 $ret[] = update_sql('DROP TABLE {node_field_userreference_data}');
89
90 db_query('DELETE FROM {cache}');
91 return $ret;
92 }
93
94 /**
95 * Set the value columns to accept NULL values
96 * and replace 0 with NULL in the field data tables
97 */
98 function userreference_update_4() {
99 $ret = array();
100
101 include_once('./'. drupal_get_path('module', 'content') .'/content.module');
102 include_once('./'. drupal_get_path('module', 'content') .'/content_admin.inc');
103
104 $fields = content_fields();
105
106
107 foreach ($fields as $field) {
108 switch ($field['type']) {
109 case 'userreference':
110 $db_info = content_database_info($field);
111 $columns_old = $db_info['columns'];
112 $columns = $columns_old;
113 $columns['uid']['not null'] = FALSE;
114 $columns['uid']['default'] = NULL;
115 // force the old values : if the db info was rebuilt before the update is run,
116 // it will already contain the new values, and nothing gets changed in content_alter_db_field
117 $columns_old['uid']['not null'] = TRUE;
118 $columns_old['uid']['default'] = 0;
119
120 content_alter_db_field($field, $columns_old, $field, $columns);
121 $ret[] = array(
122 'query' => strtr('The field %field_name has been updated to accept NULL values.', array('%field_name' => $field['field_name'])),
123 'success' => TRUE
124 );
125
126 $ret[] = update_sql("UPDATE {". $db_info['table'] ."} SET ". $db_info['columns']['uid']['column'] ." = NULL WHERE ". $db_info['columns']['uid']['column'] ." = 0");
127 break;
128 }
129 }
130
131 db_query('DELETE FROM {cache}');
132 return $ret;
133 }