-
Notifications
You must be signed in to change notification settings - Fork 5
/
domain.drush.inc
456 lines (437 loc) · 12.6 KB
/
domain.drush.inc
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
<?php
/**
* @file
* Drush commands for Domain Access.
*/
/**
* Implements hook_drush_command().
*/
function domain_drush_command() {
$items = array();
$items['domain-list'] = array(
'description' => 'List active domains for the site.',
'examples' => array(
'drush domain-list',
'drush domains',
),
'aliases' => array('domains'),
);
$items['domain-add'] = array(
'description' => 'Add a new domain to the site.',
'examples' => array(
'drush domain-add example.com \'My Test Site\'',
'drush domain-add example.com \'My Test Site\' --inactive=1 --https==1',
'drush domain-add example.com \'My Test Site\' --weight=10',
),
'arguments' => array(
'domain' => 'The domain to register (e.g. example.com).',
'sitename' => 'The name of the site (e.g. Domain Two).',
),
'options' => array(
'inactive' => 'Set the domain to inactive status if set.',
'https' => 'Use https protocol for this domain if set.',
'weight' => 'Set the order (weight) of the domain.',
),
);
$items['domain-delete'] = array(
'description' => 'Delete a domain from the site.',
'examples' => array(
'drush domain-delete example.com',
'drush domain-delete 1',
),
'arguments' => array(
'domain' => 'The domain to register (e.g. example.com or the domain_id).',
),
);
$items['domain-test'] = array(
'description' => 'Tests domains for proper response. If run from a subfolder, you must specify the --uri.',
'examples' => array(
'drush domain-test',
'drush domain-test example.com',
'drush domain-test 1',
),
'arguments' => array(
'domain' => 'The domain to register (e.g. example.com or the domain_id). If no value is passed, all domains are tested.',
),
);
$items['domain-default'] = array(
'description' => 'Sets the default domain. If run from a subfolder, you must specify the --uri.',
'examples' => array(
'drush domain-default example.com',
'drush domain-default 1',
'drush domain-default 1 --skip_check=1',
),
'arguments' => array(
'domain' => 'The domain to make default (e.g. example.com or the domain_id).',
),
'options' => array(
'skip_check' => 'Bypass the domain response test.'
),
);
$items['generate-domains'] = array(
'description' => 'Generate domains for testing.',
'arguments' => array(
'primary' => 'The primary domain to use. This will be created and used for *.example.com subdomains.',
),
'options' => array(
'count' => 'The count of extra domains to generate. Default is 15.',
'empty' => 'Pass empty=1 to truncate the {domain} table before creating records.'
),
'examples' => array(
'drush domain-generate example.com',
'drush domain-generate example.com --count=25',
'drush domain-generate example.com --count=25 --empty=1',
'drush gend',
'drush gend --count=25',
'drush gend --count=25 --empty=1',
),
'aliases' => array('gend'),
);
$items['domain-repair'] = array(
'description' => 'Updates domain_id 0 records in dependent tables.',
'examples' => array(
'drush domain-repair',
),
);
return $items;
}
/**
* Implements hook_drush_help().
*/
function domain_drush_help($section) {
$items = domain_drush_command();
$name = str_replace('domain:', '', $section);
if (isset($items[$name])) {
return dt($items[$name]['description']);
}
}
/**
* Show the domain list.
*/
function drush_domain_list() {
$domains = domain_domains();
$header = array(
'weight' => dt('weight'),
'sitename' => dt('sitename'),
'subdomain' => dt('domain'),
'scheme' => dt('scheme'),
'valid' => dt('active'),
'is_default' => dt('is_default'),
'domain_id' => dt('domain_id'),
'machine_name' => dt('machine_name'),
);
$rows = array(array_values($header));
foreach ($domains as $domain) {
$row = array();
foreach ($header as $key => $name) {
$row[] = check_plain($domain[$key]);
}
$rows[] = $row;
}
drush_print_table($rows, TRUE);
}
/**
* Generate a list of domains for testing.
*
* In my environment, I name subdomains one.* two.* up to ten. I also use
* foo.* bar.* and baz.*. We also want a non-subdomain here and use
* myexample.com.
*
* The script may also add test1, test2, test3 up to any number to test a
* large number of domains. This test is mostly for UI testing.
*
* @param $primary
* The root domain to use for domain creation.
*
* @return
* A list of the domains created.
*/
function drush_domain_generate_domains($primary = 'example.com') {
// Check the number of domains to create.
$count = drush_get_option('count');
$domains = domain_domains(TRUE);
if (empty($count)) {
$count = 15;
}
// Ensure we don't duplicate any domains.
$existing = array();
if (!empty($domains)) {
foreach ($domains as $domain) {
$existing[] = $domain['subdomain'];
}
}
// Set up one.* and so on.
$names = array(
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'foo',
'bar',
'baz',
);
// Set the creation array.
$new = array($primary);
foreach ($names as $name) {
$new[] = $name . '.' . $primary;
}
// Include a non subdomain.
$new[] = 'my' . $primary;
// Filter against existing so we can count correctly.
$prepared = array();
foreach ($new as $key => $value) {
if (!in_array($value, $existing)) {
$prepared[] = $value;
}
}
// Add any test domains.
if ($count > 15 || empty($prepared)) {
// Find the highest numbered test domain.
$start = db_query("SELECT sitename FROM {domain} WHERE sitename LIKE 'test%' ORDER BY domain_id DESC")->fetchField();
$start = (int) str_ireplace('test', '', $start);
$j = count($prepared);
for ($i = $start + 1; $j <= $count; $i++) {
$prepared[] = 'test' . $i . '.' . $primary;
$j++;
}
}
// Get the initial item weight for sorting.
$start_weight = db_query("SELECT weight FROM {domain} ORDER BY weight DESC")->fetchField();
$prepared = array_slice($prepared, 0, $count);
foreach ($prepared as $key => $item) {
$record = array(
'sitename' => ($item != $primary) ? ucwords(str_replace(".$primary", '', $item)) : config_get('system.core', 'site_name'),
'subdomain' => strtolower($item),
'scheme' => 'http',
'valid' => 1,
'weight' => ($item != $primary) ? $key + $start_weight + 1 : -1,
'is_default' => 0,
);
$created = domain_save($record, $record);
drush_print(dt('Created !domain.', array('!domain' => $record['sitename'])));
}
db_update('domain')
->condition('subdomain', $primary)
->fields(array('is_default' => 1))
->execute();
if (empty($new)) {
drush_print(dt('No new domains were created.'));
}
}
/**
* Validate the domain generation script.
*
* @param $primary
* The root domain to use for domain creation.
*/
function drush_domain_generate_domains_validate($primary = 'example.com') {
if ($empty = drush_get_option('empty')) {
db_query("TRUNCATE TABLE {domain}");
}
$error = domain_valid_domain($primary);
if (!empty($error)) {
return drush_set_error('domain', $error);
}
}
/**
* Add a new domain.
*
* @param $subdomain
* The domain name to register.
* @param $sitename
* The sitename to use for this domain.
*
* @return
* The domain created or an error message.
*/
function drush_domain_add($subdomain, $sitename) {
$start_weight = (int) db_query("SELECT weight FROM {domain} ORDER BY weight DESC")->fetchField();
$record = array(
'subdomain' => strtolower($subdomain),
'sitename' => $sitename,
'valid' => (!drush_get_option('invalid')) ? 1 : 0,
'scheme' => (!drush_get_option('https')) ? 'http' : 'https',
'weight' => ($weight = drush_get_option('weight')) ? $weight : $start_weight + 1,
'is_default' => ($is_default = drush_get_option('is_default')) ? $is_default : 0,
);
if (!empty($record['is_default'])) {
$error = domain_check_response($record, TRUE);
if ($error) {
return drush_set_error('domain', $error);
}
}
$domain = domain_save($record, $record);
if (isset($domain['domain_id'])) {
drush_print(dt('Created @domain for @sitename.', array('@domain' => $domain['subdomain'], '@sitename' => $domain['sitename'])));
}
else {
drush_print(dt('The request could not be completed.'));
}
}
/**
* Validate the domain add script.
*
* @param $subdomain
* The domain name to register.
* @param $sitename
* The sitename to use for this domain.
*/
function drush_domain_add_validate($subdomain, $sitename) {
$error = domain_drush_validate_domain($subdomain);
if (!empty($error)) {
return drush_set_error('domain', $error);
}
}
/**
* Drush version of domain_validate.
*
* @param $subdomain
* The domain name to validate for syntax and uniqueness.
*
* @return
* An array of errors encountered.
*
* @see domain_validate()
*/
function domain_drush_validate_domain($subdomain) {
$error = domain_validate($subdomain);
$output = '';
foreach ($error as $msg) {
$output .= $msg;
}
return $output;
}
/**
* Delete a domain record.
*/
function drush_domain_delete($argument = NULL) {
if (is_null($argument)) {
drush_set_error('domain', dt('You must specify a domain to delete.'));
}
// Resolve the domain.
if ($domain = drush_domain_get_from_argument($argument)) {
$domains = array($domain);
}
else {
return;
}
if (!empty($domain['is_default'])) {
return drush_set_error('domain', dt('The primary domain may not be deleted.'));
}
// Set options for re-assigning content.
$list = domain_domains();
$options = array('0' => t('Do not reassign'));
foreach ($list as $data) {
if ($data['domain_id'] != $domain['domain_id']) {
$options[$data['domain_id']] = $data['subdomain'];
}
}
$content = drush_choice($options, t('Reassign content to:'), '!value');
if (empty($content)) {
return;
}
$users = drush_choice($options, t('Reassign users to:'), '!value');
if (empty($users)) {
return;
}
$values['domain_access'] = (!empty($content)) ? $content : 'none';
$values['domain_editor'] = (!empty($content)) ? $users : 'none';
domain_delete($domain, $values);
drush_print(dt('Domain record deleted.'));
}
/**
* Test a domain record.
*/
function drush_domain_test($argument = NULL) {
if (is_null($argument)) {
$domains = domain_domains();
}
else {
if ($domain = drush_domain_get_from_argument($argument)) {
$domains = array($domain);
}
else {
return;
}
}
foreach ($domains as $domain) {
$error = domain_check_response($domain, TRUE);
if ($error) {
drush_print(dt('Fail: !error' , array('!error' => $error)));
}
else {
drush_print(dt('Success: !url tested successfully.', array('!url' => domain_get_path($domain))));
}
}
}
/**
* Set the default domain id.
*/
function drush_domain_default($argument) {
// Resolve the domain.
if ($domain = drush_domain_get_from_argument($argument)) {
$domains = array($domain);
}
else {
return;
}
// Check for domain response.
$check = drush_get_option('skip_check');
if (empty($check)) {
$error = domain_check_response($domain, TRUE);
if ($error) {
drush_set_error($error);
drush_print(dt('You may disable this error by passing --skip_check=1.'));
return;
}
}
$domain['is_default'] = 1;
domain_save($domain, $domain);
drush_print(dt('!domain set to primary domain.', array('!domain' => $domain['subdomain'])));
}
/**
* Converts a domain string or domain_id to a $domain array.
*
* On failure, throws a drush error.
*/
function drush_domain_get_from_argument($argument) {
$domain = domain_lookup($argument);
if ($domain == -1) {
$domain = domain_lookup(NULL, $argument);
}
if ($domain == -1) {
drush_set_error('domain', dt('Domain record not found.'));
return FALSE;
}
return $domain;
}
/**
* Replaces domain_id 0 records with the default domain.
*/
function drush_domain_repair() {
$list = domain_update_module_check();
if (empty($list)) {
drush_print(dt('All tables are up-to-date.'));
return;
}
$modules = array();
drush_print(dt('The following modules require a data update:'));
drush_print();
foreach ($list as $item) {
drush_print(' * ' . $item['name']);
}
drush_print();
$choice = drush_choice(array(1 => dt('Update')), dt('Update database?'), '!value');
if ($choice) {
$success = domain_update_zero_records(domain_update_tables($list));
if ($success) {
drush_print(dt('!count table(s) updated successfully.', array('!count' => count($list))));
}
}
}