-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain_migrate.admin.inc
More file actions
428 lines (326 loc) · 16.9 KB
/
domain_migrate.admin.inc
File metadata and controls
428 lines (326 loc) · 16.9 KB
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
<?php
/*
* All the forms and form_submit functions are created here
*/
function domain_migrate_generate_book_form($form_state) {
$form = array();
$form['generate_book_form'] = array(
'#type' => 'fieldset',
'#title' => t('Generate file for book migration'),
'#tree' => TRUE,
);
$books = book_get_books();
$bookarray = array();
foreach($books as $book) {
$bookarray[$book['bid']] = $book['title'];
}
$form['generate_book_form']['domain_migrate_books'] = array(
'#type' => 'checkboxes',
'#title' => t('Choose books to migrate'),
'#default_value' => variable_get('domain_migrate_books', array('book')),
'#options' => $bookarray,
);
$form['generate_book_form']['domain_migrate_books_subdomain_name'] = array(
'#type' => 'radios',
'#title' => t('Generate subdomain prefix name'),
'#default_value' => variable_get('domain_migrate_books_subdomain_name', 'path'),
'#options' => array('path'=>'Top level book page url path' , 'language'=> 'Top level book page language', 'bid' => 'Book ID'),
'#required' => TRUE,
);
$form['generate_book_form']['domain_migrate_books_modify_subdomain'] = array(
'#type' => 'radios',
'#title' => t('Modify the subdomain prefix name by adding/subtracting text'),
'#default_value' => variable_get('domain_migrate_books_modify_subdomain', 'no'),
'#options' => array('no' => 'Don\'t modify path' , 'add'=>'Add text to beginning of path' , 'subtract'=> 'Subtract text from beginning of path'),
'#required' => TRUE,
'#description'=>'The subtract option only makes sense if you have selected the "Top level book page url path" option above, or if you have chosen "Top level book page language" and your root language is the same for each book (e.g. <em>en-IE</em>, <em>en-US</em>, <em>en-AU</em>).',
);
$form['generate_book_form']['domain_migrate_books_subdomain_string'] = array(
'#type' => 'textfield',
'#title' => t('Text to add/subtract from subdomain prefix'),
'#default_value' => variable_get('domain_migrate_books_subdomain_string', ''),
);
$form['generate_book_form']['domain_migrate_books_sitename'] = array(
'#type' => 'radios',
'#title' => t('Generate domain sitename'),
'#default_value' => variable_get('domain_migrate_books_sitename', 'subdomain'),
'#options' => array('subdomain'=>'The subdomain name (capitalized, underscores and hyphens replaced with spaces)' , 'book_title'=> 'The top level book page title'),
'#required' => TRUE,
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Generate migration file'));
return $form;
}
function domain_migrate_generate_book_form_submit($form, &$form_state) {
$txtFile = drupal_get_path('module','domain_migrate') . '/subdomains.txt';
$fh = fopen($txtFile, 'w');
$stringData = '"bid","domain","sitename"\n'."\r\n";
fwrite($fh, $stringData);
foreach ($form_state['values']['generate_book_form']['domain_migrate_books'] as $bid => $value) {
if($value > 0) {
$sd_name = $form_state['values']['generate_book_form']['domain_migrate_books_subdomain_name'];
switch($sd_name) {
case 'path':
$sd_prefix = drupal_get_path_alias('node/'.$bid);
break;
case 'language':
$node = node_load($bid);
$sd_prefix = $node->language;
break;
case 'bid':
$sd_prefix = $bid;
break;
}
$sd_string = $form_state['values']['generate_book_form']['domain_migrate_books_subdomain_string'];
$sd_modify = $form_state['values']['generate_book_form']['domain_migrate_books_modify_subdomain'];
domain_migrate_modify_subdomain(&$sd_prefix, $sd_string, $sd_modify);
$sd_snoption = $form_state['values']['generate_book_form']['domain_migrate_books_sitename'];
switch($sd_snoption) {
case 'subdomain':
$sd_sitename = str_replace(array('_','-'),' ',$sd_prefix);
$sd_sitename = ucwords($sd_sitename);
break;
case 'book_title':
$node = node_load($bid);
$sd_sitename = $node->title;
break;
}
$stringData = '"'.$bid .'","'. $sd_prefix .'","'. $sd_sitename.'"\n'."\r\n";
fwrite($fh, $stringData);
}
}
variable_set('domain_migrate_books', $form_state['values']['generate_book_form']['domain_migrate_books']);
variable_set('domain_migrate_books_subdomain_name', $form_state['values']['generate_book_form']['domain_migrate_books_subdomain_name']);
variable_set('domain_migrate_books_subdomain_string', $form_state['values']['generate_book_form']['domain_migrate_books_subdomain_string']);
variable_set('domain_migrate_books_modify_subdomain', $form_state['values']['generate_book_form']['domain_migrate_books_modify_subdomain']);
variable_set('domain_migrate_books_sitename', $form_state['values']['generate_book_form']['domain_migrate_books_sitename']);
drupal_set_message('Subdomain file written to '.$txtFile);
fclose($fh);
}
function domain_migrate_generate_taxonomy_form($form_state) {
$form = array();
$form['generate_taxonomy_form'] = array(
'#type' => 'fieldset',
'#title' => t('Generate file for taxonomy migration'),
'#tree' => TRUE,
);
$result = db_query("SELECT tid, name FROM {term_data}");
$termarray = array();
while ($term = db_fetch_array($result)) {
$termarray[$term['tid']] = $term['name'];
}
$form['generate_taxonomy_form']['domain_migrate_terms'] = array(
'#type' => 'checkboxes',
'#title' => t('Choose taxonomy terms to migrate'),
'#default_value' => variable_get('domain_migrate_terms', array('terms')),
'#options' => $termarray,
);
$form['generate_taxonomy_form']['domain_migrate_terms_subdomain_name'] = array(
'#type' => 'radios',
'#title' => t('Generate subdomain prefix name'),
'#default_value' => variable_get('domain_migrate_terms_subdomain_name', 'name'),
'#options' => array('name'=>'Term name' , 'tid' => 'Term ID'),
'#required' => TRUE,
'#description'=>'Note that the domain sitename will be automatically generated from the term name.'
);
$form['generate_taxonomy_form']['domain_migrate_terms_modify_subdomain'] = array(
'#type' => 'radios',
'#title' => t('Modify the subdomain prefix name by adding/subtracting text'),
'#default_value' => variable_get('domain_migrate_terms_modify_subdomain', 'no'),
'#options' => array('no' => 'Don\'t modify path' , 'add'=>'Add text to beginning of path' , 'subtract'=> 'Subtract text from beginning of path'),
'#required' => TRUE,
);
$form['generate_taxonomy_form']['domain_migrate_terms_subdomain_string'] = array(
'#type' => 'textfield',
'#title' => t('Text to add/subtract from subdomain prefix'),
'#default_value' => variable_get('domain_migrate_terms_subdomain_string', ''),
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Generate migration file'));
return $form;
}
function domain_migrate_generate_taxonomy_form_submit($form, &$form_state) {
$txtFile = drupal_get_path('module','domain_migrate') . '/subdomains.txt';
$fh = fopen($txtFile, 'w');
$stringData = '"tid","domain","sitename"\n'."\r\n";
fwrite($fh, $stringData);
foreach ($form_state['values']['generate_taxonomy_form']['domain_migrate_terms'] as $tid => $value) {
if ($value > 0) {
$term = taxonomy_get_term($tid);
$sd_name = $form_state['values']['generate_taxonomy_form']['domain_migrate_terms_subdomain_name'];
switch($sd_name) {
case 'term':
$sd_prefix = strtolower(str_replace(' ','_',$term->name));
break;
case 'tid':
$sd_prefix = $tid;
break;
}
$sd_string = $form_state['values']['generate_taxonomy_form']['domain_migrate_terms_subdomain_string'];
$sd_modify = $form_state['values']['generate_taxonomy_form']['domain_migrate_terms_modify_subdomain'];
domain_migrate_modify_subdomain(&$sd_prefix, $sd_string, $sd_modify);
$sd_sitename = ucwords($term->name);
$stringData = '"'.$tid .'","'. $sd_prefix .'","'. $sd_sitename.'"\n'."\r\n";
fwrite($fh, $stringData);
}
}
variable_set('domain_migrate_terms', $form_state['values']['generate_taxonomy_form']['domain_migrate_terms']);
variable_set('domain_migrate_terms_subdomain_name', $form_state['values']['generate_taxonomy_form']['domain_migrate_terms_subdomain_name']);
variable_set('domain_migrate_terms_subdomain_string', $form_state['values']['generate_taxonomy_form']['domain_migrate_terms_subdomain_string']);
variable_set('domain_migrate_terms_modify_subdomain', $form_state['values']['generate_taxonomy_form']['domain_migrate_terms_modify_subdomain']);
drupal_set_message('Subdomain file written to '.$txtFile);
fclose($fh);
}
function domain_migrate_settings_form($form_state) {
$form = array();
$form['domain_migrate_general_settings'] = array(
'#type' => 'fieldset',
'#title' => t('General settings'),
'#tree' => FALSE,
);
$form['domain_migrate_general_settings']['domain_migrate_override_grants'] = array(
'#type' => 'checkbox',
'#title' => t('Override existing node-domain relationships during migration'),
'#default_value' => variable_get('domain_migrate_override grants', TRUE),
'#description'=>'If unchecked, the node will be assigned to the new domain in addition to any domains it was already assigned to. If checked, it will be assigned to the new domain only. This setting will not affect nodes assigned to all affiliates.',
);
$form['domain_migrate_general_settings']['domain_migrate_create_menus'] = array(
'#type' => 'checkboxes',
'#title' => t('Create and assign menus for each domain'),
'#default_value' => variable_get('domain_migrate_create_menus', array()),
'#options' => array('primary' => 'Primary Links' , 'secondary'=>'Secondary Links' , 'navigation'=> 'Navigation'),
'#description'=>'For each box checked, a new menu will be created and will appear instead of the sitewide menu for that domain.',
);
if (module_exists('locale')) {
$form['domain_migrate_language_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Migration language settings'),
'#tree' => FALSE,
);
$form['domain_migrate_language_settings']['domain_migrate_assign_language'] = array(
'#type' => 'radios',
'#title' => t('Assign language to domain'),
'#default_value' => variable_get('domain_migrate_assign_language', 'no'),
'#options' => array('no'=>'Don\'t assign a language' , 'book'=> 'Assign language to domainbased on language of top-level book', 'subdomain' => 'Assign a public language to domain based on subdomain name, create it if not already available', 'private'=> 'Create and assign a private domain language to the domain based on subdomain name' ),
'#required' => TRUE,
'#description'=>'The third option only makes sense if your subdomain name matches a Google RFC-4646 recognised language code (e.g. <em>en</em>, <em>en-US</em>, <em>de-AT</em>). The fourth option creates a RFC-4646 compliant \'private\' language for your domain (e.g. <em>en-x-media</em>, <em>de-x-blog</em>) - this can serve as a workaround to avoid path conflicts if you will have nodes with the same URL alias on different domains.',
);
$form['domain_migrate_language_settings']['domain_migrate_language_nodes'] = array(
'#type' => 'checkbox',
'#title' => t('Assign domain language to all nodes and aliases on that domain'),
'#default_value' => variable_get('domain_migrate_language_nodes', FALSE),
'#description'=>'Recommended if you have enabled domain-based language negotiation and you choose to assign a language to each domain.',
);
}
if (module_exists('book')) {
$form['domain_migrate_book_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Book to domain migration settings'),
'#tree' => FALSE,
);
$form['domain_migrate_book_settings']['domain_migrate_user_role'] = array(
'#type' => 'checkboxes',
'#title' => t('Assign author of top level book page node to domain'),
'#default_value' => variable_get('domain_migrate_user_role', array('user')),
'#options' => user_roles(TRUE),
'#description'=>'If you select a role, the author of the top level book page node will be given that role on that domain. If no role is selected, the author is not assigned to the domain. Selecting authenticated user only will assign the author to the domain without giving them any custom roles on that domain.',
);
$form['domain_migrate_book_settings']['domain_migrate_alter_path'] = array(
'#type' => 'checkbox',
'#title' => t('Remove top-level book page path from URL alias of all book nodes'),
'#default_value' => variable_get('domain_migrate_alter_path', FALSE),
'#description'=>'Use this if the book hierarchy is reflected in the URL path of the book nodes. Helpful for moving from subfolders to subdomains e.g. <em>www.example.com/sub/path</em> to <em>sub.example.com/path</em>.',
);
$form['domain_migrate_book_settings']['domain_migrate_htaccess_redirect'] = array(
'#type' => 'checkbox',
'#title' => t('Generate .htaccess rules for redirecting book URLs to their subdomains'),
'#default_value' => variable_get('domain_migrate_htaccess_redirect', FALSE),
'#description'=>'Again, helpful for moving from subfolders to subdomains. At the moment, it only works if the "Remove top-level book page path" checkbox has been ticked too',
);
}
return system_settings_form($form);
}
function domain_migrate_form($form_state) {
$form = array();
$form['domain_migrate_form'] = array(
'#type' => 'fieldset',
'#title' => t('Migrate nodes to domains'),
'#tree' => TRUE,
'#description' => 'Submitting this form will cause many irreversible changes to your database - please back it up! Also, be sure that your settings have been saved.',
);
$form['domain_migrate_form']['subdomains'] = array(
'#type' => 'textarea',
'#title' => t('Paste migration file contents'),
'#description' => t('Comma separated values: (Book ID, Domain prefix, Domain sitename)'),
'#default_value' => isset($form_state['values']) ? $form_state['values']['drupal_aliases']['subdomains'] :
file_get_contents(drupal_get_path('module','domain_migrate') . '/subdomains.txt',TRUE),
'#rows' => 10,
'#required' => FALSE,
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Migrate nodes to domain'));
return $form;
}
/**
* Submit for migration form
*/
function domain_migrate_form_submit($form, &$form_state) {
$is_book = TRUE;
$subdomains = csv_to_array($form_state['values']['domain_migrate_form']['subdomains'], $is_book);
domain_migrate_build_domains($subdomains, $is_book);
// rebuilds the form with entered values
$form_state['rebuild'] = TRUE;
}
/**
* Add/subtract text from subdomain string
*/
function domain_migrate_modify_subdomain(&$sd_prefix, $sd_string, $sd_modify) {
if (!empty($sd_string)) {
switch($sd_modify) {
case 'add':
$sd_prefix = $sd_string . $sd_prefix;
break;
case 'subtract':
$sd_pos = strpos($sd_prefix, $sd_string);
if($sd_pos === 0) {
$sd_prefix = substr($sd_prefix, strlen($sd_string));
}
break;
case 'no':
break;
}
}
}
/*
* The following 2 functions are taken from the php str_getcsv page (http://php.net/manual/en/function.str-getcsv.php)
* We are using csv_explode instead of str_getcsv to support PHP < 5.3
*/
function csv_to_array($csv, &$is_book, $delimiter = ',', $enclosure = '"', $escape = '\\', $terminator = '\n') {
$r = array();
$rows = explode($terminator, trim($csv));
$names = array_shift($rows);
$names = csv_explode($names, $delimiter, $enclosure);
// Check wehther the first line is bid or tid
$is_book = ($names[0] == 'bid');
$nc = count($names);
foreach ($rows as $row) {
if (trim($row)) {
$values = csv_explode($row, $delimiter, $enclosure);
if (!$values) $values = array_fill(0, $nc, NULL);
$r[] = array_combine($names, $values);
}
}
return $r;
}
function csv_explode($str, $delim=',', $enclose='"', $preserve=false){
$resArr = array();
$n = 0;
$expEncArr = explode($enclose, $str);
foreach($expEncArr as $EncItem){
if($n++%2){
array_push($resArr, array_pop($resArr) . ($preserve?$enclose:'') . $EncItem.($preserve?$enclose:''));
}else{
$expDelArr = explode($delim, $EncItem);
array_push($resArr, array_pop($resArr) . array_shift($expDelArr));
$resArr = array_merge($resArr, $expDelArr);
}
}
return $resArr;
}