-
Notifications
You must be signed in to change notification settings - Fork 5
/
domain.install
595 lines (560 loc) · 18.5 KB
/
domain.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<?php
/**
* @file
* Install file.
*/
/**
* Implements hook_install().
*/
function domain_install() {
domain_set_primary_domain();
}
/**
* Implements hook_schema().
*/
function domain_schema() {
$schema['domain'] = [
'description' => 'The base table for domain records',
'fields' => [
'domain_id' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Domain numeric id.',
],
'subdomain' => [
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
'description' => 'Registered DNS entry, will match HTTP_HOST requests',
],
'sitename' => [
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
'description' => 'Site display name',
],
'scheme' => [
'type' => 'varchar',
'length' => '8',
'not null' => TRUE,
'default' => 'http',
'description' => 'Protocol',
],
'valid' => [
'type' => 'varchar',
'length' => '1',
'not null' => TRUE,
'default' => '1',
'description' => 'Active status',
],
'weight' => [
'type' => 'int',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0,
'description' => 'Sort order',
],
'is_default' => [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Indicates primary domain',
],
'machine_name' => [
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
'description' => 'The machine name for this domain.',
],
],
'primary key' => ['domain_id'],
'indexes' => [
'subdomain' => ['subdomain'],
'weight' => ['weight'],
'is_default' => ['is_default'],
],
'foreign_keys' => [
'domain_id' => ['domain_export' => 'domain_id'],
'machine_name' => ['domain_export' => 'machine_name'],
],
];
$schema['domain_access'] = [
'description' => 'Stores domain information for each node',
'fields' => [
'nid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Node id, foreign key to {node}',
],
'gid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Node access grant id',
],
'realm' => [
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
'description' => 'Node access realm',
],
],
'primary key' => ['nid', 'gid', 'realm'],
'indexes' => [
'nid' => ['nid'],
],
'foreign_keys' => [
'nid' => ['node' => 'nid'],
],
];
$schema['domain_editor'] = [
'description' => 'Stores domain information for each user',
'fields' => [
'uid' => [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'User id, foreign key to {user}',
],
'domain_id' => [
'type' => 'int',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0,
'description' => 'Domain id, foreign key to {domain}',
],
],
'primary key' => ['uid', 'domain_id'],
'foreign_keys' => [
'uid' => ['user' => 'uid'],
'domain_id' => ['domain' => 'domain_id'],
],
];
$schema['domain_export'] = [
'description' => 'Stores canonical machine names for domains.',
'fields' => [
'domain_id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Domain id. Automatic master key.',
],
'machine_name' => [
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
'description' => 'The machine name for this domain.',
],
],
'unique keys' => ['machine_name' => ['machine_name']],
'indexes' => [
'domain_id' => ['domain_id'],
],
];
return $schema;
}
/**
* Checks module requirements.
*/
function domain_requirements($phase) {
$requirements = [];
switch ($phase) {
case 'install':
module_load_include('module', 'domain');
$root = strtolower(rtrim($_SERVER['HTTP_HOST']));
if ($error = domain_valid_domain($root)) {
$requirements['domain'] = [
'title' => t('Domain Access'),
'value' => $error . t('If you are using drush, please provide the --uri option (e.g. drush en domain --uri="http://example.com/optional_subdirectory").'),
'severity' => REQUIREMENT_ERROR,
];
}
break;
case 'runtime':
$messages = [];
$severity = REQUIREMENT_ERROR;
// Ensure we have a primary domain.
$check = domain_default();
if ($check['domain_id'] == 0) {
$updated = t('set by an administrator');
if (user_access('administer domains')) {
$updated = l(t('set properly'), 'admin/structure/domain');
}
$messages[] = t('The site has no primary domain and needs to be !updated.', ['!updated' => $updated]);
}
// Check for domain_id 0.
$list = domain_update_module_check();
domain_update_messages($messages, $list);
// Now report.
$t = get_t();
if (empty($messages)) {
$severity = REQUIREMENT_OK;
$messages[] = t('Module installed correctly.');
}
$requirements['domain'] = [
'title' => $t('Domain Access'),
'value' => theme('item_list', ['items' => $messages]),
'severity' => $severity,
];
break;
}
return $requirements;
}
/**
* Update note.
*
* Upgrading from Drupal 5 to Drupal 7 is not supported.
* You must first upgrade to Drupal 6.x.2.3 or higher, and then proceed to
* Drupal 7.
*
*/
/**
* Change the edit and delete permissions.
*/
function domain_update_7001(&$sandbox) {
db_update('role_permission')
->condition('permission', 'edit domain nodes')
->fields(['permission' => 'edit domain content'])
->execute();
db_update('role_permission')
->condition('permission', 'delete domain nodes')
->fields(['permission' => 'delete domain content'])
->execute();
return t('Updated Domain Access permission names.');
}
/**
* Add sorting to domains.
*/
function domain_update_7300(&$sandbox) {
if (db_field_exists('domain', 'weight')) {
return t('No update required');
}
db_add_field('domain', 'weight', [
'type' => 'int',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0,
], []);
db_add_index('domain', 'weight', ['weight']);
return t('Domain sorting added.');
}
/**
* Add default domain flag and weight the default higher.
*/
function domain_update_7301(&$sandbox) {
if (db_field_exists('domain', 'is_default')) {
return t('No update required');
}
db_add_field('domain', 'is_default', [
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
], []);
db_update('domain')
->fields(['is_default' => 1, 'weight' => -1])
->condition('domain_id', 0)
->execute();
return t('Default domain updated.');
}
/**
* Add an index on {domain}.is_default.
*/
function domain_update_7302(&$sandbox) {
if (!db_index_exists('domain', 'is_default')) {
db_add_index('domain', 'is_default', ['is_default']);
}
return t('Domain default indexed properly.');
}
/**
* Remove the zero record from the database.
*/
function domain_update_7303(&$sandbox) {
// We grab the default domain, remove it from the database, and
// then re-save it into the table, using the new value as the default domain.
$default = db_query("SELECT * FROM {domain} WHERE domain_id = 0")->fetchAssoc();
if (empty($default)) {
return t('Domain Access did not find an existing domain 0. No updates required.');
}
// We have to do a delete and re-insert here to properly increment the id.
// Since the module might be disabled, we can't use drupal_write_record();
db_delete('domain')
->condition('domain_id', 0)
->execute();
db_insert('domain')
->fields([
'subdomain' => $default['subdomain'],
'sitename' => $default['sitename'],
'scheme' => $default['scheme'],
'valid' => $default['valid'],
'weight' => $default['weight'],
'is_default' => $default['is_default'],
])
->execute();
// Update tables that use a domain_id and we control directly.
$default_id = db_query("SELECT domain_id FROM {domain} WHERE is_default = 1")->fetchField();
db_update('domain_editor')
->fields(['domain_id' => $default_id])
->condition('domain_id', 0)
->execute();
db_update('domain_access')
->fields(['gid' => $default_id])
->condition('gid', 0)
->condition('realm', 'domain_id')
->execute();
db_update('node_access')
->fields(['gid' => $default_id])
->condition('gid', 0)
->condition('realm', 'domain_id')
->execute();
// Update message.
return t('Domain Access updated domain 0 successfully');
}
/**
* Rebuild the {domain} table.
*/
function domain_update_7304(&$sandbox) {
// Reset the domain_id field as a plain integer.
$spec = [
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Domain numeric id.',
];
db_change_field('domain', 'domain_id', 'domain_id', $spec);
db_drop_primary_key('domain');
// Add the machine name column.
if (!db_field_exists('domain', 'machine_name')) {
$spec = [
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
'description' => 'The machine name for this domain.',
];
db_add_field('domain', 'machine_name', $spec);
}
// Now we can safely add the new primary key.
db_add_primary_key('domain', ['domain_id']);
return t('{domain} table updated with machine_name column.');
}
/**
* Allow domains to be made exportable.
*/
function domain_update_7305(&$sandbox) {
if (db_table_exists('domain_export')) {
return t('{domain_export} table exists.');
}
$schema = [
'description' => 'Stores canonical machine names for domains.',
'fields' => [
'domain_id' => [
'type' => 'serial',
'description' => 'Domain id. Automatic master key.',
],
'machine_name' => [
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
'description' => 'The machine name for this domain.',
],
],
'primary key' => ['machine_name'],
'indexes' => [
'domain_id' => ['domain_id'],
],
];
db_create_table('domain_export', $schema);
return t('{domain_export} table created.');
}
/**
* Insert existing domain records into {domain_export} and {domain}.
*/
function domain_update_7306(&$sandbox) {
$domains = db_query("SELECT domain_id, subdomain FROM {domain} ORDER BY domain_id")->fetchAll();
foreach ($domains as $domain) {
$query = db_insert('domain_export')
->fields([
'domain_id' => $domain->domain_id,
'machine_name' => domain_update_machine_name($domain->subdomain),
]);
$query->execute();
$query = db_update('domain')
->condition('domain_id', $domain->domain_id)
->fields([
'machine_name' => domain_update_machine_name($domain->subdomain),
]);
$query->execute();
}
return t('{domain_export} table populated.');
}
/**
* Updates the {domain_export} table for new key handling.
*/
function domain_update_7307(&$sandbox) {
db_drop_primary_key('domain_export');
// Just in case a unique key already exists, drop it.
db_drop_unique_key('domain_export', 'machine_name');
db_add_unique_key('domain_export', 'machine_name', ['machine_name']);
return t('{domain_export} table keys updated.');
}
/**
* Generates a machine name during the update process.
*
* @param $subdomain
* The subdomain string of the record, which should be unique.
*
* @return
* A string with dot and colon transformed to underscore.
*/
function domain_update_machine_name($subdomain) {
return preg_replace('/[^a-z0-9_]+/', '_', $subdomain);
}
/**
* Replace domain_behavior with default node access settings.
*/
function domain_update_7308(&$sandbox) {
$behavior = config_get('domain.settings', 'domain_behavior');
if (function_exists('node_type_get_types')) {
$types = node_type_get_types();
foreach ($types as $key => $type) {
$node_access_values = ['DOMAIN_ACTIVE'];
$arr = [];
$all_sites = count(config_get('domain.settings', 'domain_node_')[$key]) == 0 || !isset(config_get('domain.settings', 'domain_node_')[$key]) ? 0 : config_get('domain.settings', 'domain_node_')[$key];
if ($behavior == 1 || $all_sites == 1) {
$node_access_values[] = 'DOMAIN_ALL';
}
$arr[$key] = $node_access_values;
config_set('domain.settings', 'domain_node_', $arr);
}
}
config_clear('domain.settings', 'domain_behavior');
return t('Ported domain behavior to default node access settings.');
}
/**
* Updates domain_roles variable to use machine names.
*/
function domain_update_7309(&$sandbox) {
backdrop_load('module', 'domain');
$defaults = config_get('domain.settings', 'domain_roles');
$new = [];
foreach ($defaults as $rid => $domains) {
// We cannot use a constant here. See https://drupal.org/node/2042461.
$new[$rid]['DOMAIN_ALL'] = $domains['all'];
foreach ($domains as $domain_id => $check) {
$domain = domain_lookup($domain_id);
if ($domain != -1) {
$new[$rid][$domain['machine_name']] = $check;
}
}
}
config_set('domain.settings', 'domain_roles', $new);
}
/**
* Updates node grants for unpublished content.
*/
function domain_update_7310(&$sandbox) {
node_access_rebuild(TRUE);
}
/**
* Remove stale domain_node_{bundle} variables.
*/
function domain_update_7311(&$sandbox) {
if (function_exists('node_type_get_types')) {
$variable_names = [];
foreach (node_type_get_types() as $key => $type) {
$variable_names[] = 'domain_node_' . $key;
}
db_delete('variable')
->condition('name', 'domain_node_%', 'LIKE')
->condition('name', $variable_names, 'NOT IN')
->execute();
}
}
/**
* Migrate domain variables to config.
*/
function domain_update_1000() {
$config = config('domain.settings');
$config->set('domain_list_size', update_variable_get('domain_list_size', 25));
$config->set('domain_sitename_override', update_variable_get('domain_sitename_override', 1));
$config->set('domain_debug', update_variable_get('domain_debug', 0));
$config->set('domain_force_admin', update_variable_get('domain_force_admin', 0));
$config->set('domain_vertical_tab', update_variable_get('domain_vertical_tab', 0));
$config->set('domain_collapse_options', update_variable_get('domain_collapse_options', 0));
$config->set('domain_www', update_variable_get('domain_www', 0));
$config->set('domain_search', update_variable_get('domain_search', 0));
$config->set('domain_seo', update_variable_get('domain_seo', 0));
$config->set('domain_edit_on_primary', update_variable_get('domain_edit_on_primary', 1));
$config->set('domain_default_source', update_variable_get('domain_default_source', 0));
$config->set('domain_default', update_variable_get('domain_default', 0));
$config->set('domain_grant_all', update_variable_get('domain_grant_all', 'user/*/track'));
$config->set('domain_cron_rule', update_variable_get('domain_cron_rule', 1));
$config->set('domain_xmlrpc_rule', update_variable_get('domain_xmlrpc_rule', 0));
$config->set('domain_paths', update_variable_get('domain_paths', "node/%n node/%n/edit comment/reply/%n node/add/book/parent/%n book/export/html/%n node/%n/outline"));
$config->set('domain_classes', update_variable_get('domain_classes', 'domain-[current-domain:machine_name]'));
$config->set('domain_allow_non_ascii', update_variable_get('domain_allow_non_ascii', FALSE));
$config->set('domain_add_roles', update_variable_get('domain_add_roles', 0));
$config->set('domain_roles', update_variable_get('domain_roles', []));
$config->set('domain_mymodule', update_variable_get('domain_mymodule', 0));
$config->set('domain_behavior', update_variable_get('domain_behavior', 0));
$config->set('domain_hide_errors', update_variable_get('domain_hide_errors', FALSE));
$config->set('domain_select_format', update_variable_get('domain_select_format', 0));
$config->set('domain_scheme', update_variable_get('domain_scheme', 'http://'));
$config->set('domain_skip_domain_check', update_variable_get('domain_skip_domain_check', 0));
$config->set('domain_node_', update_variable_get('domain_node_', []));
$config->set('domain_last_node_update', update_variable_get('domain_last_node_update', 0));
$config->set('user_picture_default', update_variable_get('user_picture_default', ""));
$config->set('domain_bootstrap_modules', update_variable_get('domain_bootstrap_modules', []));
$config->set('anonymous', update_variable_get('anonymous', 'Anonymous'));
$config->set('settings', update_variable_get('settings', []));
$config->save();
update_variable_del('domain_list_size');
update_variable_del('domain_sitename_override');
update_variable_del('domain_debug');
update_variable_del('domain_force_admin');
update_variable_del('domain_vertical_tab');
update_variable_del('domain_collapse_options');
update_variable_del('domain_www');
update_variable_del('domain_search');
update_variable_del('domain_seo');
update_variable_del('domain_edit_on_primary');
update_variable_del('domain_default_source');
update_variable_del('domain_grant_all');
update_variable_del('domain_cron_rule');
update_variable_del('domain_xmlrpc_rule');
update_variable_del('domain_paths');
update_variable_del('domain_classes');
update_variable_del('domain_allow_non_ascii');
update_variable_del('domain_add_roles');
update_variable_del('domain_roles');
update_variable_del('domain_mymodule');
update_variable_del('domain_behavior');
update_variable_del('domain_node_key');
update_variable_del('domain_hide_errors');
update_variable_del('domain_select_format');
update_variable_del('domain_scheme');
update_variable_del('domain_skip_domain_check');
update_variable_del('domain_node_type');
update_variable_del('domain_last_node_update');
update_variable_del('user_picture_default');
update_variable_del('domain_bootstrap_modules');
update_variable_del('anonymous');
update_variable_del('domain_default');
update_variable_del('settings');
}