forked from mkalkbrenner/themekey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
themekey.install
385 lines (314 loc) · 12.1 KB
/
themekey.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
<?php
/**
* @file
* Database schema of
* @see themekey.module
*
* @author Markus Kalkbrenner | Cocomore AG
* @see http://drupal.org/user/124705
*
* @author profix898
* @see http://drupal.org/user/35192
*/
/**
* Implements hook_schema().
*/
function themekey_schema() {
$schema = array();
$schema['themekey_properties'] = array(
'fields' => array(
'id' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
'property' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'operator' => array('type' => 'varchar', 'length' => 2, 'not null' => TRUE, 'default' => '='),
'value' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'weight' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
'theme' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'enabled' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'wildcards' => array('type' => 'text', 'not null' => TRUE, 'size' => 'big', 'serialize' => TRUE),
'parent' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
),
'primary key' => array('id'),
'indexes' => array(
'enabled_parent_weight' => array('enabled', 'parent', 'weight'),
'parent_weight' => array('parent', 'weight'),
),
);
return $schema;
}
/**
* Implements hook_install().
*/
function themekey_install() {
drupal_install_schema('themekey');
}
/**
* Implements hook_uninstall().
*/
function themekey_uninstall() {
// Drop tables
drupal_uninstall_schema('themekey');
// Remove variables
db_query("DELETE FROM {variable} WHERE name LIKE 'themekey_%%'");
cache_clear_all('variables', 'cache');
}
/**
* Implements hook_update_N().
*
* Update property 'nid' to 'node:nid'
*/
function themekey_update_6001() {
$ret = array();
//
$result = db_query('SELECT * FROM {themekey_properties} WHERE property = \'nid\'');
while ($item = db_fetch_array($result)) {
if (db_result(db_query('SELECT COUNT(id) FROM {themekey_properties} WHERE property = \'node:nid\' AND value = \'%s\'', $item['value'])) > 0) {
$ret[] = update_sql('DELETE FROM {themekey_properties} WHERE id = '. $item['id']);
}
else {
$ret[] = update_sql('UPDATE {themekey_properties} SET property = \'node:nid\' WHERE id = '. $item['id']);
}
}
return $ret;
}
/**
* Implements hook_update_N().
*/
function themekey_update_6100() {
$properties = variable_get('themekey_properties', array());
foreach ($properties as $key => &$property) {
if (array_key_exists('path', $property) && $key === $property['path']) {
$property['path'] = FALSE;
}
}
variable_set('themekey_properties', $properties);
return array();
}
/**
* Implements hook_update_N().
*/
function themekey_update_6101() {
$result = update_sql("UPDATE {system} SET weight = 0 WHERE name = 'themekey'");
return array($result);
}
/**
* Implements hook_update_N().
*/
function themekey_update_6102() {
if (module_exists('forum')) {
variable_set('themekey_module_forum_triggers_taxonomy_vid', 1);
}
if (module_exists('taxonomy_menu')) {
variable_set('themekey_module_taxonomy_menu_triggers_taxonomy_tid', 1);
}
return array();
}
/**
* Implements hook_update_N().
*/
function themekey_update_6103() {
variable_del('themekey_nodediscover');
return array();
}
/**
* Implements hook_update_N().
*/
function themekey_update_6104() {
$result = update_sql("ALTER TABLE {themekey_properties} DROP COLUMN callbacks");
return array($result);
}
/**
* Implements hook_update_N().
*/
function themekey_update_6105() {
global $db_type;
$return = array();
// we need to handle upgrade of module ThemeKey UI here because
// it will fail when triggered at themekey_ui.install after
// ThemeKey upgrade from 6.x-1.1 to 6.x.2.0
if (module_exists('themekey_ui')) {
$schema_version = drupal_get_installed_schema_version('themekey_ui');
if (6100 > $schema_version) {
$return = drupal_install_schema('themekey_ui');
if (!variable_get('themekey_nodeaspath', 0)) {
$sql = '';
if (0 === strpos($db_type, 'mysql')) {
$sql = "SELECT id, value, theme, nid, vid FROM {themekey_properties} JOIN {node_revisions} ON (value = nid) WHERE property = 'node:nid' AND conditions = '%s'";
}
elseif (0 === strpos($db_type, 'pqsql')) {
$sql = "SELECT id, value, theme, nid, vid FROM {themekey_properties} JOIN {node_revisions} ON (value = nid::character varying) WHERE property = 'node:nid' AND conditions = '%s'";
}
if ($result = db_query($sql, 'a:0:{}')) {
$insert_sql = "INSERT INTO {themekey_ui_node_theme} (nid, vid, theme) VALUES (%d, %d, '%s')";
$delete_sql = "DELETE FROM {themekey_properties} WHERE id = %d";
while ($row = db_fetch_array($result)) {
if ($return['INSERT']['success'] = db_query($insert_sql, $row['nid'], $row['vid'], $row['theme'])) {
if (!($return['DELETE']['success'] = db_query($delete_sql, $row['id']))) {
break;
}
}
else {
$return['INSERT']['success'] = FALSE;
break;
}
}
if (isset($return['INSERT'])) {
$return['INSERT']['query'] = check_plain($insert_sql);
}
if (isset($return['DELETE'])) {
$return['DELETE']['query'] = check_plain($delete_sql);
}
}
else {
$return['SELECT'] = array('success' => FALSE, 'query' => check_plain($sql));
}
}
variable_del('themekey_nodeaspath');
}
}
return $return;
}
/**
* Function _themekey_properties_explode_conditions()
* converts conditions formatted as string into an array.
* It was part of themekey_build.inc up to version 6.x-1.2.
* Now it's only required one more time to perform
* themekey_update_6200()
*
* @param $conditions
* ThemeKey conditions as string
*
* @return
* ThemeKey conditions as array
*/
function _themekey_properties_explode_conditions($conditions) {
if (!is_array($conditions)) {
$parts = array_filter(explode(';', $conditions));
$conditions = array();
foreach ($parts as $part) {
$part = trim($part);
if (preg_match('/(.*?)([<>=!~])(.*)/', $part, $matches)) {
$conditions[] = array(
'property' => trim($matches[1]),
'operator' => trim($matches[2]),
'value' => trim($matches[3])
);
}
}
}
return $conditions;
}
/**
* Implements hook_update_N().
*/
function themekey_update_6200() {
$field = array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => '0', 'initial' => '1');
db_add_field($ret, 'themekey_properties', 'enabled', $field);
db_drop_index($ret, 'themekey_properties', 'property');
db_add_index($ret, 'themekey_properties', 'enabled', array('enabled'));
$field = array('type' => 'text', 'not null' => TRUE);
db_add_field($ret, 'themekey_properties', 'wildcards', $field);
$weight = ((int) db_result(db_query("SELECT MIN(weight) FROM {themekey_properties}"))) - 1;
$insert_query = "INSERT INTO {themekey_properties} (property, value, weight, conditions, theme, enabled, wildcards) VALUES ('drupal:path', '%s', %d, '%s', '%s', 1, '%s')";
$result = db_query("SELECT * FROM {themekey_paths} WHERE custom = 1");
while ($item = db_fetch_array($result)) {
$conditions = unserialize($item['conditions']);
if (is_array($conditions) && !empty($conditions)) {
if (!is_array($conditions[0])) {
// ThemeKey 6.x-1.1 stored conditions for paths as simple string within an array
$conditions = _themekey_properties_explode_conditions($conditions[0]);
}
}
$insert_success = db_query($insert_query, $item['path'], $weight, serialize($conditions), $item['theme'], $item['wildcards']);
$ret[] = array('success' => $insert_success, 'query' => $insert_query);
$ret[] = update_sql('DELETE FROM {themekey_paths} WHERE id = '. $item['id']);
}
db_drop_field($ret, 'themekey_paths', 'conditions');
db_drop_field($ret, 'themekey_paths', 'custom');
db_drop_field($ret, 'themekey_paths', 'theme');
return $ret;
}
/**
* Implements hook_update_N().
*/
function themekey_update_6201() {
// Don't rebuild anymore when themekey_update_6202() will run
//themekey_rebuild();
return array();
}
/**
* Implements hook_update_N().
*/
function themekey_update_6202() {
$ret = array();
$field_operator = array('type' => 'varchar', 'length' => 2, 'not null' => TRUE, 'default' => '=', 'initial' => '=');
db_add_field($ret, 'themekey_properties', 'operator', $field_operator);
$field_parent = array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'initial' => 0);
db_add_field($ret, 'themekey_properties', 'parent', $field_parent);
if ($result = db_query("SELECT * FROM {themekey_properties} WHERE conditions <> '%s'", serialize(array()))) {
$insert_query = "INSERT INTO {themekey_properties} (property, operator, value, weight, theme, enabled, wildcards, parent) VALUES ('%s', '%s', '%s', %d, '%s', %d, '%s', %d)";
while ($row = db_fetch_array($result)) {
$conditions = unserialize($row['conditions']);
if (is_array($conditions)) {
$parent = $row['id'];
foreach ($conditions as $condition) {
$insert_result = db_query($insert_query, $condition['property'], $condition['operator'], $condition['value'], 0, $row['theme'], $row['enabled'], serialize(array()), $parent);
if ($insert_result) {
$parent = db_last_insert_id('themekey_properties', 'id');
}
$ret[] = array('success' => $insert_result, 'query' => $insert_query);
}
}
}
}
db_drop_field($ret, 'themekey_properties', 'conditions');
db_drop_index($ret, 'themekey_properties', 'enabled');
db_drop_index($ret, 'themekey_properties', 'weight');
db_add_index($ret, 'themekey_properties', 'enabled_parent_weight', array('enabled', 'parent', 'weight'));
db_add_index($ret, 'themekey_properties', 'parent_weight', array('parent', 'weight'));
db_drop_index($ret, 'themekey_paths', 'path');
db_drop_index($ret, 'themekey_paths', 'fit');
db_drop_index($ret, 'themekey_paths', 'weight');
db_add_index($ret, 'themekey_paths', 'path_fit_weight', array('path', 'fit', 'weight'));
return $ret;
}
/**
* Implements hook_update_N().
*/
function themekey_update_6203() {
// calling themekey_rebuild() at this point will cause an error because
// modified schema is not yet populated
// therefor we show a message
drupal_set_message(t("ThemeKey won't work correctly until you click 'Save configuration' at !settings_link. If you don't see a 'Save configuration' button you need to clear your cache first.", array('!settings_link' => l(t("ThemeKey settings"), 'admin/settings/themekey/settings'))), 'error');
if (module_exists('themekey_ui')) {
drupal_set_message(t("ThemeKey UI won't work correctly until click 'Save configuration' at !settings_link. If you don't see a 'Save configuration' button you need to clear your cache first.", array('!settings_link' => l(t("ThemeKey UI settings"), 'admin/settings/themekey/settings/ui'))), 'error');
}
return array();
}
/**
* Implements hook_update_N().
*/
function themekey_update_6300() {
if (!defined('THEMEKEY_PAGECACHE_UNSUPPORTED')) {
define('THEMEKEY_PAGECACHE_UNSUPPORTED', 0);
define('THEMEKEY_PAGECACHE_SUPPORTED', 1);
define('THEMEKEY_PAGECACHE_TIMEBASED', 2);
}
module_load_include('inc', 'themekey', 'themekey_build');
themekey_rebuild();
$ret = array();
db_drop_table($ret, 'themekey_paths');
return $ret;
}
/**
* Implements hook_update_N().
*/
function themekey_update_6301() {
if (!defined('THEMEKEY_PAGECACHE_UNSUPPORTED')) {
define('THEMEKEY_PAGECACHE_UNSUPPORTED', 0);
define('THEMEKEY_PAGECACHE_SUPPORTED', 1);
define('THEMEKEY_PAGECACHE_TIMEBASED', 2);
}
module_load_include('inc', 'themekey', 'themekey_build');
themekey_rebuild();
return array();
}