-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquant.install
179 lines (156 loc) · 4.84 KB
/
quant.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
<?php
/**
* @file
* Install hook definitions for Quant.
*/
use Drupal\Core\Database\Database;
use Drupal\views\Entity\View;
use Symfony\Component\Yaml\Yaml;
/**
* Perform setup tasks for Quant.
*/
function quant_install() {
$config = \Drupal::configFactory()->getEditable('quant.token_settings');
$config->set('secret', bin2hex(random_bytes(32)));
$config->save();
}
/**
* Changes to the token schema definition to support path protection.
*/
function quant_update_9001(&$sandbox) {
// Remove any stale tokens.
if (method_exists(\Drupal::service('quant.token_manager'), 'release')) {
\Drupal::service('quant.token_manager')->release();
}
$schema = Database::getConnection()->schema();
$spec = [
'type' => 'text',
'size' => 'normal',
'not null' => FALSE,
'description' => 'A path to register for the token',
];
$schema->changeField('quant_token', 'nid', 'route', $spec);
$spec = [
'type' => 'text',
'size' => 'normal',
'not null' => TRUE,
'description' => 'Created timestamp for the token',
];
$schema->changeField('quant_token', 'created', 'created', $spec);
$config = \Drupal::configFactory()->getEditable('quant.token_settings');
$config->set('timeout', '+1 minute');
$config->save();
}
/**
* Add configuration option to disable token validation.
*/
function quant_update_9002(&$sandbox) {
$config = \Drupal::configFactory()->getEditable('quant.token_settings');
$config->set('disable', FALSE);
$config->save();
}
/**
* Support JWT for internal request tokens.
*/
function quant_update_9003(&$sandbox) {
$config = \Drupal::configFactory()->getEditable('quant.token_settings');
$config->set('secret', bin2hex(random_bytes(32)));
$config->set('strict', FALSE);
$config->save();
// Remove the token table.
$schema = Database::getConnection()->schema();
$schema->dropTable('quant_token');
}
/**
* Add default configuration for automated link following.
*/
function quant_update_9004(&$sandbox) {
$xpaths = [
'//li[contains(@class,"pager__item--next")]/a[contains(@href,"page=")]',
'//li[contains(@class,"pager__item--first")]/a[starts-with(@href, "/")]',
];
$config = \Drupal::configFactory()->getEditable('quant.settings');
$config->set('xpath_selectors', implode(PHP_EOL, $xpaths));
$config->save();
}
/**
* Disables draft content handling if Workbench Moderation is installed.
*/
function quant_update_9005(&$sandbox) {
if (\Drupal::moduleHandler()->moduleExists('workbench_moderation')) {
$config = \Drupal::configFactory()->getEditable('quant.settings');
$config->set('disable_content_drafts', 1);
$config->save();
}
}
/**
* Add xpath selector to support new pager markup.
*/
function quant_update_9006(&$sandbox) {
$new_xpath = [
'//li[contains(@class,"page-item")]/a[contains(@href,"page=")]',
];
// Add new xpath to existing xpaths.
$config = \Drupal::configFactory()->getEditable('quant.settings');
$xpaths = explode(PHP_EOL, $config->get('xpath_selectors'));
$xpaths = array_merge($xpaths, $new_xpath);
$config->set('xpath_selectors', implode(PHP_EOL, $xpaths));
$config->save();
}
/**
* Enables Quant setting to show page info block.
*/
function quant_update_9007(&$sandbox) {
$config = \Drupal::configFactory()->getEditable('quant.settings');
$config->set('quant_show_page_info_block', 1);
$config->save();
}
/**
* Adds Quant metadata views.
*/
function quant_update_9008() {
// Enable views if not already.
if (!\Drupal::moduleHandler()->moduleExists('views')) {
\Drupal::moduleHandler()->install('views');
}
// Keep track of what gets saved.
$saved = [];
$installPath = \Drupal::service('extension.list.module')->getPath('quant') . '/config/install/';
$optionalPath = \Drupal::service('extension.list.module')->getPath('quant') . '/config/optional/';
$names = [
'quant_metadata_file' => $installPath,
'quant_metadata_node' => $installPath,
'quant_metadata_redirect' => $optionalPath,
'quant_metadata_taxonomy' => $installPath,
];
foreach ($names as $name => $path) {
// Only create if the view doesn't exist.
if (!View::load($name)) {
$view = 'views.view.' . $name;
$configPath = $path . '/' . $view . '.yml';
$data = Yaml::parse(file_get_contents($configPath));
try {
\Drupal::configFactory()->getEditable($view)->setData($data)->save(TRUE);
$saved[] = $name;
}
catch (\Exception $e) {
// Can happen if dependencies are not met.
}
}
}
// Create message showing status.
$unsaved = [];
foreach ($names as $name => $path) {
if (!in_array($name, $saved)) {
$unsaved[] = $name;
}
}
$message = [];
if (!empty($unsaved)) {
$message[] = 'Unsaved views: ' . implode(', ', $unsaved);
}
if (!empty($saved)) {
$message[] = 'Saved views: ' . implode(', ', $saved);
}
return implode('\n\n', $message);
}