-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathredirect.install
150 lines (129 loc) · 5.65 KB
/
redirect.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
<?php
/**
* Update hooks for the Redirect module.
*/
use Drupal\redirect\Entity\Redirect;
use Drupal\Core\Database\Database;
use Drupal\views\Entity\View;
use Symfony\Component\Yaml\Yaml;
/**
* Rehash redirects to account for case insensitivity.
*/
function redirect_update_8100(&$sandbox) {
// Loop through 100 redirects at a time.
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_rid'] = 0;
// Note, because MySQL can treat `foo = LOWER(foo)`, all records must be checked.
$sandbox['max'] = db_query('SELECT COUNT(1) FROM {redirect}')->fetchField();
}
$result = db_select('redirect', 'r')
->fields('r', ['rid', 'redirect_source__path', 'redirect_source__query', 'language', 'hash'])
->condition('rid', $sandbox['current_rid'], '>')
->range(0, 100)
->orderBy('rid', 'ASC')
->execute();
foreach ($result as $row) {
$query = !empty($row->redirect_source__query) ? unserialize($row->redirect_source__query): [];
$new_hash = Redirect::generateHash($row->redirect_source__path, (array) $query, $row->language);
if ($row->hash != $new_hash) {
// Do a direct query to speed things up.
db_update('redirect')
->fields(['hash' => $new_hash])
->condition('rid', $row->rid)
->execute();
}
$sandbox['progress']++;
$sandbox['current_rid'] = $row->rid;
}
// Reset caches.
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
}
/**
* Update the {redirect} table.
*/
function redirect_update_8101() {
// Get the current schema, change the length.
$key_value_store_schema = \Drupal::keyValue('entity.storage_schema.sql');
$schema = $key_value_store_schema->get('redirect.field_schema_data.hash');
$schema['redirect']['fields']['hash']['length'] = 64;
// Set the max_length of the hash column to 64 characters.
Database::getConnection()
->schema()
->changeField('redirect', 'hash', 'hash', $schema['redirect']['fields']['hash']);
// Update the last installed field definition and field schema.
/** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value_store */
\Drupal::entityManager()->clearCachedFieldDefinitions();
$key_value_store_definition = \Drupal::keyValue('entity.definitions.installed');
$storage_definitions = $key_value_store_definition->get('redirect.field_storage_definitions');
$storage_definitions['hash'] = $storage_definition = \Drupal::entityManager()
->getFieldStorageDefinitions('redirect')['hash'];
$key_value_store_definition->set('redirect.field_storage_definitions', $storage_definitions);
// Update the stored schema.
$key_value_store_schema->set('redirect.field_schema_data.hash', $schema);
}
/**
* Update settings based on existing settings and Globalredirect settings.
*/
function redirect_update_8102() {
// Load default configuration.
$redirect_settings = \Drupal::config('redirect.settings');
$globalredirect_settings = \Drupal::config('globalredirect.settings');
$config_factory = \Drupal::configFactory();
$redirect = $config_factory->getEditable('redirect.settings');
$nonclean_to_clean = $redirect_settings->get('global_clean');
$admin_path = $redirect_settings->get('global_admin_paths');
$frontpage_redirect = $redirect_settings->get('global_home');
$deslash = $redirect_settings->get('global_deslash');
$message = NULL;
// If Globalredirect configuration exists, use those settings.
if ((!$globalredirect_settings->isNew())) {
$access_check = $globalredirect_settings->get('access_check');
$normalize_aliases = $globalredirect_settings->get('normalize_aliases');
$content_location_header = $globalredirect_settings->get('content_location_header');
$term_path_handler = $globalredirect_settings->get('term_path_handler');
$deslash = $globalredirect_settings->get('deslash');
$frontpage_redirect = $globalredirect_settings->get('frontpage_redirect');
$nonclean_to_clean = $globalredirect_settings->get('nonclean_to_clean');
$redirect->set('access_check', $access_check);
$redirect->set('normalize_aliases', $normalize_aliases);
$redirect->set('content_location_header', $content_location_header);
$redirect->set('term_path_handler', $term_path_handler);
$message = 'The Globalredirect module functionality has been merged into redirect, it should be uninstalled now.';
}
else {
$redirect->set('access_check', FALSE);
$redirect->set('normalize_aliases', TRUE);
$redirect->set('content_location_header', FALSE);
$redirect->set('term_path_handler', TRUE);
}
// Update new redirect settings names.
$redirect->set('nonclean_to_clean', $nonclean_to_clean);
$redirect->set('admin_path', $admin_path);
$redirect->set('frontpage_redirect', $frontpage_redirect);
$redirect->set('deslash', $deslash);
// Remove old names of redirect settings.
$redirect->clear('global_clean');
$redirect->clear('global_admin_paths');
$redirect->clear('global_home');
$redirect->clear('global_deslash');
$redirect->save();
return $message;
}
/**
* Creates the new default redirect view.
*/
function redirect_update_8103() {
$message = NULL;
// Only create if the redirect view doesn't exist and views is enabled.
if (!View::load('redirect') && \Drupal::moduleHandler()->moduleExists('views')) {
$config_path = drupal_get_path('module', 'redirect') . '/config/install/views.view.redirect.yml';
$data = Yaml::parse($config_path);
\Drupal::configFactory()->getEditable('views.view.redirect')->setData($data)->save(TRUE);
$message = 'The new redirect view has been created.';
}
else {
$message = 'Not creating a redirect view since it already exists.';
}
return $message;
}