-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathting_field_search.install
334 lines (307 loc) · 10.1 KB
/
ting_field_search.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* @file
*
* Install file for Ting field search.
*/
/**
* Implements hook_enable().
*/
function ting_field_search_enable() {
// We'll do this in hook_modules_enabled() in the module file.
// See comments there for more details.
// ting_field_search_install_backend();
}
/**
* Implements hook_disable().
*/
function ting_field_search_disable() {
// We need to do this on disable, since our class wont be available anymore.
if (ting_field_search_get_status()) {
variable_del('cache_class_cache_ting');
}
}
/**
* Implements hook_schema().
*/
function ting_field_search_schema() {
$schema['ting_field_search_profile'] = array(
'description' => 'Holds the definitions of the installed search profiles.',
'export' => array(
'key' => 'name',
'key name' => 'Name',
'admin_title' => 'title',
'primary key' => 'pid',
'identifier' => 'profile',
'default hook' => 'ting_field_search_default_profiles',
'api' => array(
'owner' => 'ting_field_search',
'api' => 'ting_field_search_default_profiles',
'minimum_version' => 1,
'current_version' => 1,
),
// Take full control of the export workflow.
'subrecords callback' => 'ting_field_search_subrecords_callback',
'load callback' => 'ting_field_search_profiles_load',
'load multiple callback' => 'ting_field_search_profiles_load_mutliple',
'load all callback' => 'ting_field_search_profiles_load_all',
'create callback' => 'ting_field_search_profile_create',
'save callback' => 'ting_field_search_profile_save',
'delete callback' => 'ting_field_search_profile_delete',
'export callback' => 'ting_field_search_profile_export',
),
'fields' => array(
'pid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'The primary identifier for a profile.',
'no export' => TRUE,
),
'name' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
'description' => 'The machine name of the profile.',
),
'title' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
'description' => 'Profile title: Displayed to users of the site.',
),
'config' => array(
'type' => 'blob',
'size' => 'big',
'not null' => TRUE,
'serialize' => TRUE,
'description' => 'Serialized profile configuration that do not warrant a single column.',
),
'weight' => array(
'type' => 'int',
'size' => 'small',
'not null' => TRUE,
'default' => 0,
'description' => 'Profile weight: Used to order profiles in UI.',
),
),
'primary key' => array('pid'),
'unique keys' => array(
'name' => array('name'),
),
);
// For performance reasons keep facet settings in a seperate table.
// TODO: Consider moving these settings to the new config field.
$schema['ting_field_search_facet'] = array(
'description' => 'Holds facet settings for each profile.',
'fields' => array(
'name' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
'description' => 'Facet name: Name of the facet (e.g. facet.type).',
),
'pid' => array(
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'description' => 'Foreign key: ID of the associated profile.',
'default' => 0,
),
'title' => array(
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
'description' => 'Facet title: The title of the facet, as shown to users in UI',
),
'sorting' => array(
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => 'default',
'description' => 'Facet sorting. Sorting of the terms in the facet.',
),
'weight' => array(
'type' => 'int',
'size' => 'small',
'not null' => TRUE,
'default' => 1,
'description' => 'Facet weight: Used to order facets in the UI.'
),
),
'primary key' => array('name', 'pid'),
'foreign keys' => array(
'profile' => array(
'table' => 'ting_field_search_profile',
'columns' => array('profile' => 'pid'),
),
),
);
return $schema;
}
/**
* Implements hook_requirements().
*/
function ting_field_search_requirements($phase) {
$requirements = array();
if ($phase != 'runtime') {
return $requirements;
}
if (!ting_field_search_get_status()) {
$requirements['ting_field_search'] = array(
'title' => t('Ting field search'),
'value' => t('Ting field search is NOT installed correctly and will not work proberly. Please go to <a href="@install">Installation</a> to correct this.', array(
'@install' => url('admin/config/ting-field-search/install', array(
'query' => drupal_get_destination()
)),
)
),
'severity' => REQUIREMENT_ERROR,
);
}
return $requirements;
}
/**
* Helper function to load profiles in update functions.
* For schema version 7000 -> ...
*/
function _ting_field_search_load_profiles_7000() {
return db_select('ting_field_search_profile', 'tp')
->fields('tp')
->execute()
->fetchAllAssoc('name');
}
/**
* Adds the new config field to the profile table.
* Adds new serial primary key to profile table.
* Update foreign key in facet table to point to the new profile primary key.
*/
function ting_field_search_update_7000() {
// We need to migrate existing data, so fetch the profiles now.
$profiles = _ting_field_search_load_profiles_7000();
// The fields to be migrated to the new config field.
$migrate_fields = array(
'active',
'exposed',
'alter_links',
'use_facets',
'term_count',
'facet_count',
);
$migrate_data = array();
foreach ($profiles as $name => $profile) {
$profile = (array) $profile;
$config = array_intersect_key($profile , array_flip($migrate_fields));
$migrate_data[$name] = $config;
}
// Now that we got the data; drop the fields.
foreach ($migrate_fields as $field) {
db_drop_field('ting_field_search_profile', $field);
}
// Add the config field and migrate existing data, if any.
$config_field = array(
'type' => 'blob',
'size' => 'big',
'serialize' => TRUE,
'not null' => FALSE,
'description' => 'Serialized profile configuration that do not warrant a single column.',
);
db_add_field('ting_field_search_profile', 'config', $config_field);
foreach ($migrate_data as $name => $config) {
db_merge('ting_field_search_profile')
->key(array(
'name' => $name,
))
->fields(array(
'config' => serialize($config),
))
->execute();
}
$config_field['not null'] = TRUE;
db_change_field('ting_field_search_profile', 'config', 'config', $config_field);
// Add new serial primary key (Profile ID).
db_drop_primary_key('ting_field_search_profile');
$pid = array(
'type' => 'serial',
'not null' => TRUE,
'unsigned' => TRUE,
'description' => 'The primary identifier for a profile.'
);
db_add_field('ting_field_search_profile', 'pid', $pid, array(
'primary key' => array('pid'),
));
// We'll still keep the machine-name as unique key, because we use it as
// parameter in the URL and it will be necesarry for adding exportability.
db_add_unique_key('ting_field_search_profile', 'name', array('name'));
// By using the new profile ID as link between profiles and facet settings
// we allow for updating of profile machine-name without too much hassle.
db_add_field('ting_field_search_facet', 'pid', array(
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'default' => 0,
'description' => 'Foreign key: ID of the associated profile.',
));
// Load the profiles a new, so that we get the newly generated ID's.
$profiles = _ting_field_search_load_profiles_7000();
foreach ($profiles as $name => $profile) {
db_update('ting_field_search_facet')
->fields(array('pid' => $profile->pid))
->condition('profile', $name)
->execute();
}
db_drop_primary_key('ting_field_search_facet');
db_drop_field('ting_field_search_facet', 'profile');
db_add_primary_key('ting_field_search_facet', array('name', 'pid'));
}
/**
* Move the query and profile fields to the config array.
* Migrate existing config values to the new array structure.
*/
function ting_field_search_update_7001() {
// We need to migrate existing data, so fetch the profiles now.
$profiles = _ting_field_search_load_profiles_7000();
$migrate_map = array(
'exposed' => 'user_interaction',
'sticky' => 'user_interaction',
'alter_links' => 'user_interaction',
'query' => 'search_request',
'new_materials' => 'search_request',
'use_facets' => 'facets',
'facet_count' => 'facets',
'term_count' => 'facets',
);
$new_configs = array();
foreach ($profiles as $name => $profile) {
$old_config = unserialize($profile->config);
$old_config['query'] = $profile->query;
$new_config = array();
$new_config['search_result'] = $old_config['search_result'];
$new_config['webtrends'] = $old_config['webtrends'];
foreach ($migrate_map as $setting => $destination) {
if (isset($old_config[$setting])) {
$new_config[$destination][$setting] = $old_config[$setting];
}
}
// The VIP profile is being split up and needs special handling.
$new_config['search_request']['search_well_profile'] = $profile->profile;
$new_configs[$name] = $new_config;
}
// Drop the fields and save the new config arrays.
db_drop_field('ting_field_search_profile', 'profile');
db_drop_field('ting_field_search_profile', 'query');
foreach ($new_configs as $name => $new_config) {
db_merge('ting_field_search_profile')
->key(array(
'name' => $name,
))
->fields(array(
'config' => serialize($new_config),
))
->execute();
}
}