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