Projet

Général

Profil

Paste
Télécharger (16,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / rules.install @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Rules - Installation file.
6
 */
7

    
8
/**
9
 * Implements hook_enable().
10
 */
11
function rules_enable() {
12
  // Enable evaluation of Rules right after enabling the module.
13
  rules_event_invocation_enabled(TRUE);
14
}
15

    
16
/**
17
 * Implements hook_install().
18
 */
19
function rules_install() {
20
  module_load_include('inc', 'rules', 'modules/events');
21
  // Set the modules' weight to 20, see
22
  // https://www.drupal.org/node/445084#comment-1533280 for the reasoning.
23
  db_query("UPDATE {system} SET weight = 20 WHERE name = 'rules'");
24
}
25

    
26
/**
27
 * Implements hook_uninstall().
28
 */
29
function rules_uninstall() {
30
  variable_del('rules_debug');
31
  variable_del('rules_debug_log');
32
  variable_del('rules_log_errors');
33
  variable_del('rules_log_level');
34

    
35
  variable_del('rules_clean_path');
36
  variable_del('rules_path_cleaning_callback');
37
  variable_del('rules_path_lower_case');
38
  variable_del('rules_path_replacement_char');
39
  variable_del('rules_path_transliteration');
40

    
41
  // Delete all the debug region variables and then clear the variables cache.
42
  db_delete('variable')
43
    ->condition('name', 'rules_debug_region_%', 'LIKE')
44
    ->execute();
45
  cache_clear_all('variables', 'cache_bootstrap');
46
}
47

    
48
/**
49
 * Implements hook_schema().
50
 */
51
function rules_schema() {
52
  $schema['rules_config'] = array(
53
    'fields' => array(
54
      'id' => array(
55
        'type' => 'serial',
56
        'not null' => TRUE,
57
        'description' => 'The internal identifier for any configuration.',
58
      ),
59
      'name' => array(
60
        'type' => 'varchar',
61
        'length' => '64',
62
        'not null' => TRUE,
63
        'description' => 'The name of the configuration.',
64
      ),
65
      'label' => array(
66
        'type' => 'varchar',
67
        'length' => '255',
68
        'not null' => TRUE,
69
        'description' => 'The label of the configuration.',
70
        'default' => 'unlabeled',
71
      ),
72
      'plugin' => array(
73
        'type' => 'varchar',
74
        'length' => 127,
75
        'not null' => TRUE,
76
        'description' => 'The name of the plugin of this configuration.',
77
      ),
78
      'active' => array(
79
        'description' => 'Boolean indicating whether the configuration is active. Usage depends on how the using module makes use of it.',
80
        'type' => 'int',
81
        'not null' => TRUE,
82
        'default' => 1,
83
      ),
84
      'weight' => array(
85
        'type' => 'int',
86
        'not null' => TRUE,
87
        'default' => 0,
88
        'size' => 'tiny',
89
        'description' => 'Weight of the configuration. Usage depends on how the using module makes use of it.',
90
      ),
91
      'status' => array(
92
        'type' => 'int',
93
        'not null' => TRUE,
94
        // Set the default to ENTITY_CUSTOM without using the constant as it is
95
        // not safe to use it at this point.
96
        'default' => 0x01,
97
        'size' => 'tiny',
98
        'description' => 'The exportable status of the entity.',
99
      ),
100
      'dirty' => array(
101
        'type' => 'int',
102
        'not null' => TRUE,
103
        'default' => 0,
104
        'size' => 'tiny',
105
        'description' => 'Dirty configurations fail the integrity check, e.g. due to missing dependencies.',
106
      ),
107
      'module' => array(
108
        'description' => 'The name of the providing module if the entity has been defined in code.',
109
        'type' => 'varchar',
110
        'length' => 255,
111
        'not null' => FALSE,
112
      ),
113
      'owner' => array(
114
        'description' => 'The name of the module via which the rule has been configured.',
115
        'type' => 'varchar',
116
        'length' => 255,
117
        'not null' => TRUE,
118
        'default' => 'rules',
119
      ),
120
      'access_exposed' => array(
121
        'type' => 'int',
122
        'not null' => TRUE,
123
        'default' => 0,
124
        'size' => 'tiny',
125
        'description' => 'Whether to use a permission to control access for using components.',
126
      ),
127
      'data' => array(
128
        'type' => 'blob',
129
        'size' => 'big',
130
        'not null' => FALSE,
131
        'serialize' => TRUE,
132
        'description' => 'Everything else, serialized.',
133
      ),
134
    ),
135
    'primary key' => array('id'),
136
    'unique keys' => array(
137
      'name' => array('name'),
138
    ),
139
    'indexes' => array(
140
      'plugin' => array('plugin', 'active'),
141
    ),
142
  );
143
  $schema['rules_trigger'] = array(
144
    'fields' => array(
145
      'id' => array(
146
        'type' => 'int',
147
        'unsigned' => TRUE,
148
        'not null' => TRUE,
149
        'description' => 'The primary identifier of the configuration.',
150
      ),
151
      'event' => array(
152
        'type' => 'varchar',
153
        'length' => '127',
154
        'not null' => TRUE,
155
        'default' => '',
156
        'description' => 'The name of the event on which the configuration should be triggered.',
157
      ),
158
    ),
159
    'primary key' => array('id', 'event'),
160
    'foreign keys' => array(
161
      'table' => 'rules_config',
162
      'columns' => array('id' => 'id'),
163
    ),
164
  );
165
  $schema['rules_tags'] = array(
166
    'fields' => array(
167
      'id' => array(
168
        'type' => 'int',
169
        'unsigned' => TRUE,
170
        'not null' => TRUE,
171
        'description' => 'The primary identifier of the configuration.',
172
      ),
173
      'tag' => array(
174
        'type' => 'varchar',
175
        'length' => '255',
176
        'not null' => TRUE,
177
        'description' => 'The tag string associated with this configuration',
178
      ),
179
    ),
180
    'primary key' => array('id', 'tag'),
181
    'foreign keys' => array(
182
      'table' => 'rules_config',
183
      'columns' => array('id' => 'id'),
184
    ),
185
  );
186
  $schema['rules_dependencies'] = array(
187
    'fields' => array(
188
      'id' => array(
189
        'type' => 'int',
190
        'unsigned' => TRUE,
191
        'not null' => TRUE,
192
        'description' => 'The primary identifier of the configuration.',
193
      ),
194
      'module' => array(
195
        'type' => 'varchar',
196
        'length' => '255',
197
        'not null' => TRUE,
198
        'description' => 'The name of the module that is required for the configuration.',
199
      ),
200
    ),
201
    'primary key' => array('id', 'module'),
202
    'indexes' => array(
203
      'module' => array('module'),
204
    ),
205
    'foreign keys' => array(
206
      'table' => 'rules_config',
207
      'columns' => array('id' => 'id'),
208
    ),
209
  );
210
  $schema['cache_rules'] = drupal_get_schema_unprocessed('system', 'cache');
211
  $schema['cache_rules']['description'] = 'Cache table for the rules engine to store configured items.';
212
  return $schema;
213
}
214

    
215
/**
216
 * Upgrade from Rules 6.x-1.x to 7.x.
217
 */
218
function rules_update_7200() {
219
  // Create the new db tables first.
220
  $schema['rules_config'] = array(
221
    'fields' => array(
222
      'id' => array(
223
        'type' => 'serial',
224
        'not null' => TRUE,
225
        'description' => 'The internal identifier for any configuration.',
226
      ),
227
      'name' => array(
228
        'type' => 'varchar',
229
        'length' => '255',
230
        'not null' => TRUE,
231
        'description' => 'The name of the configuration.',
232
      ),
233
      'label' => array(
234
        'type' => 'varchar',
235
        'length' => '255',
236
        'not null' => TRUE,
237
        'description' => 'The label of the configuration.',
238
        'default' => 'unlabeled',
239
      ),
240
      'plugin' => array(
241
        'type' => 'varchar',
242
        'length' => 127,
243
        'not null' => TRUE,
244
        'description' => 'The name of the plugin of this configuration.',
245
      ),
246
      'active' => array(
247
        'description' => 'Boolean indicating whether the configuration is active. Usage depends on how the using module makes use of it.',
248
        'type' => 'int',
249
        'not null' => TRUE,
250
        'default' => 1,
251
      ),
252
      'weight' => array(
253
        'type' => 'int',
254
        'not null' => TRUE,
255
        'default' => 0,
256
        'size' => 'tiny',
257
        'description' => 'Weight of the configuration. Usage depends on how the using module makes use of it.',
258
      ),
259
      'status' => array(
260
        'type' => 'int',
261
        'not null' => TRUE,
262
        // Set the default to ENTITY_CUSTOM without using the constant as it is
263
        // not safe to use it at this point.
264
        'default' => 0x01,
265
        'size' => 'tiny',
266
        'description' => 'The exportable status of the entity.',
267
      ),
268
      'module' => array(
269
        'description' => 'The name of the providing module if the entity has been defined in code.',
270
        'type' => 'varchar',
271
        'length' => 255,
272
        'not null' => FALSE,
273
      ),
274
      'data' => array(
275
        'type' => 'blob',
276
        'size' => 'big',
277
        'not null' => FALSE,
278
        'serialize' => TRUE,
279
        'description' => 'Everything else, serialized.',
280
      ),
281
    ),
282
    'primary key' => array('id'),
283
    'unique keys' => array(
284
      'name' => array('name'),
285
    ),
286
  );
287
  $schema['rules_trigger'] = array(
288
    'fields' => array(
289
      'id' => array(
290
        'type' => 'int',
291
        'unsigned' => TRUE,
292
        'not null' => TRUE,
293
        'description' => 'The primary identifier of the configuration.',
294
      ),
295
      'event' => array(
296
        'type' => 'varchar',
297
        'length' => '127',
298
        'not null' => TRUE,
299
        'default' => '',
300
        'description' => 'The name of the event on which the configuration should be triggered.',
301
      ),
302
    ),
303
    'primary key' => array('id', 'event'),
304
    'foreign keys' => array(
305
      'table' => 'rules_config',
306
      'columns' => array('id' => 'id'),
307
    ),
308
  );
309
  db_create_table('rules_config', $schema['rules_config']);
310
  db_create_table('rules_trigger', $schema['rules_trigger']);
311
  // The cache table already exists, but changed. So re-create it.
312
  db_drop_table('cache_rules');
313
  $schema['cache_rules'] = drupal_get_schema_unprocessed('system', 'cache');
314
  $schema['cache_rules']['description'] = 'Cache table for the rules engine to store configured items.';
315
  db_create_table('cache_rules', $schema['cache_rules']);
316
  // Remove deprecated variables.
317
  variable_del('rules_inactive_sets');
318
  variable_del('rules_show_fixed');
319
  variable_del('rules_hide_token_message');
320
  variable_del('rules_counter');
321

    
322
  return t('The database tables for Rules 2.x have been created. The old tables from Rules 1.x are still available and contain your rules, which are not updated yet.');
323
}
324

    
325
/**
326
 * Add in the exportable entity db columns as required by the entity API.
327
 */
328
function rules_update_7201() {
329
  // Previously this was update 7200, so check whether we need to run it really.
330
  // The update has been moved as 7200 needs to be the 6.x-7.x upgrade.
331
  if (!db_field_exists('rules_config', 'status')) {
332
    db_add_field('rules_config', 'status', array(
333
      'type' => 'int',
334
      'not null' => TRUE,
335
      'default' => ENTITY_CUSTOM,
336
      'size' => 'tiny',
337
      'description' => 'The exportable status of the entity.',
338
    ));
339
    // The module column did already exist before.
340
  }
341
}
342

    
343
/**
344
 * Add an index for the rules configuration plugin column.
345
 */
346
function rules_update_7202() {
347
  db_add_index('rules_config', 'plugin', array('plugin'));
348
}
349

    
350
/**
351
 * Fix the length of the rules_config.name column.
352
 */
353
function rules_update_7203() {
354
  db_drop_unique_key('rules_config', 'name');
355
  $keys = array(
356
    'unique keys' => array(
357
      'name' => array('name'),
358
    ),
359
  );
360
  db_change_field('rules_config', 'name', 'name', array(
361
    'type' => 'varchar',
362
    'length' => '64',
363
    'not null' => TRUE,
364
    'description' => 'The name of the configuration.',
365
  ), $keys);
366
}
367

    
368
/**
369
 * Add a table for rules-config tags.
370
 */
371
function rules_update_7204() {
372
  if (!db_table_exists('rules_tags')) {
373
    $schema['rules_tags'] = array(
374
      'fields' => array(
375
        'id' => array(
376
          'type' => 'int',
377
          'unsigned' => TRUE,
378
          'not null' => TRUE,
379
          'description' => 'The primary identifier of the configuration.',
380
        ),
381
        'tag' => array(
382
          'type' => 'varchar',
383
          'length' => '255',
384
          'not null' => TRUE,
385
          'description' => 'The tag string associated with this configuration',
386
        ),
387
      ),
388
      'primary key' => array('id', 'tag'),
389
      'foreign keys' => array(
390
        'table' => 'rules_config',
391
        'columns' => array('id' => 'id'),
392
      ),
393
    );
394
    db_create_table('rules_tags', $schema['rules_tags']);
395
  }
396
}
397

    
398
/**
399
 * Add the rules_dependencies table and the rules_config.dirty column.
400
 */
401
function rules_update_7205() {
402
  if (!db_table_exists('rules_dependencies')) {
403
    $schema['rules_dependencies'] = array(
404
      'fields' => array(
405
        'id' => array(
406
          'type' => 'int',
407
          'unsigned' => TRUE,
408
          'not null' => TRUE,
409
          'description' => 'The primary identifier of the configuration.',
410
        ),
411
        'module' => array(
412
          'type' => 'varchar',
413
          'length' => '255',
414
          'not null' => TRUE,
415
          'description' => 'The name of the module that is required for the configuration.',
416
        ),
417
      ),
418
      'primary key' => array('id', 'module'),
419
      'indexes' => array(
420
        'module' => array('module'),
421
      ),
422
      'foreign keys' => array(
423
        'table' => 'rules_config',
424
        'columns' => array('id' => 'id'),
425
      ),
426
    );
427
    db_create_table('rules_dependencies', $schema['rules_dependencies']);
428
  }
429
  if (!db_field_exists('rules_config', 'dirty')) {
430
    db_add_field('rules_config', 'dirty', array(
431
      'type' => 'int',
432
      'not null' => TRUE,
433
      'default' => 0,
434
      'size' => 'tiny',
435
    ));
436
  }
437
}
438

    
439
/**
440
 * Flush all caches.
441
 */
442
function rules_update_7206() {
443
  // The update system is going to flush all caches anyway, so nothing to do.
444
}
445

    
446
/**
447
 * Flush all caches.
448
 */
449
function rules_update_7207() {
450
  // The update system is going to flush all caches anyway, so nothing to do.
451
}
452

    
453
/**
454
 * Flush all caches to update the data_is_empty condition info.
455
 */
456
function rules_update_7208() {
457
  // The update system is going to flush all caches anyway, so nothing to do.
458
}
459

    
460
/**
461
 * Creates a flag that enables a permission for using components.
462
 */
463
function rules_update_7209() {
464
  // Create a access exposed flag column.
465
  db_add_field('rules_config', 'access_exposed', array(
466
    'type' => 'int',
467
    'not null' => TRUE,
468
    'default' => 0,
469
    'size' => 'tiny',
470
    'description' => 'Whether to use a permission to control access for using components.',
471
  ));
472
}
473

    
474
/**
475
 * Deletes the unused rules_empty_sets variable.
476
 */
477
function rules_update_7210() {
478
  variable_del('rules_empty_sets');
479
}
480

    
481
/**
482
 * Creates the "owner" column.
483
 */
484
function rules_update_7211() {
485
  // Create a owner column.
486
  if (!db_field_exists('rules_config', 'owner')) {
487
    db_add_field('rules_config', 'owner', array(
488
      'description' => 'The name of the module via which the rule has been configured.',
489
      'type' => 'varchar',
490
      'length' => 255,
491
      'not null' => TRUE,
492
      'default' => 'rules',
493
    ));
494
  }
495
}
496

    
497
/**
498
 * Make sure registry gets rebuilt to avoid upgrade troubles.
499
 */
500
function rules_update_7212() {
501
  // Make sure module information gets refreshed and registry is rebuilt.
502
  drupal_static_reset('system_rebuild_module_data');
503
  registry_rebuild();
504
}
505

    
506
/**
507
 * Recover the "owner" property for broken configurations.
508
 */
509
function rules_update_7213() {
510
  $rows = db_select('rules_config', 'c')
511
    ->fields('c')
512
    ->condition('status', ENTITY_OVERRIDDEN)
513
    ->condition('owner', 'rules', '<>')
514
    ->execute()
515
    ->fetchAllAssoc('id');
516

    
517
  foreach ($rows as $id => $row) {
518
    if ($row->module == $row->owner) {
519
      db_update('rules_config')
520
        ->condition('id', $id)
521
        ->fields(array('owner' => 'rules'))
522
        ->execute();
523
    }
524
  }
525
}
526

    
527
/**
528
 * Switch out the rules_event_whitelist variable for a cache equivalent.
529
 */
530
function rules_update_7214() {
531
  // Enable Rules if currently disabled so that this update won't fail.
532
  $disable_rules = FALSE;
533
  if (!module_exists('rules')) {
534
    module_enable(array('rules'));
535
    $disable_rules = TRUE;
536
  }
537
  // Set new event_whitelist cache cid.
538
  rules_set_cache('rules_event_whitelist', variable_get('rules_event_whitelist', array()));
539
  // Delete old conf variable.
540
  variable_del('rules_event_whitelist');
541
  // Avoid any missing class errors.
542
  registry_rebuild();
543
  // Clear and rebuild Rules caches.
544
  // See: rules_admin_settings_cache_rebuild_submit.
545
  rules_clear_cache();
546
  rules_get_cache();
547
  _rules_rebuild_component_cache();
548
  RulesEventSet::rebuildEventCache();
549
  // Disable Rules again if it was disabled before this update started.
550
  if ($disable_rules) {
551
    module_disable(array('rules'));
552
  }
553
}
554

    
555
/**
556
 * Add an index for retrieving active config of a certain plugin.
557
 */
558
function rules_update_7215() {
559
  if (db_index_exists('rules_config', 'plugin')) {
560
    db_drop_index('rules_config', 'plugin');
561
  }
562
  db_add_index('rules_config', 'plugin', array('plugin', 'active'));
563
}