Projet

Général

Profil

Paste
Télécharger (15,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / rules.install @ 76df55b7

1
<?php
2

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

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

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

    
25
/**
26
 * Implements hook_uninstall().
27
 */
28
function rules_uninstall() {
29
  variable_del('rules_event_whitelist');
30
  variable_del('rules_debug');
31
}
32

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

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

    
307
  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.');
308
}
309

    
310
/**
311
 * Add in the exportable entity db columns as required by the entity API.
312
 */
313
function rules_update_7201() {
314
  // Previously this was update 7200, so check whether we need to run it really.
315
  // The update has been moved as 7200 needs to be the 6.x-7.x upgrade.
316
  if (!db_field_exists('rules_config', 'status')) {
317
    db_add_field('rules_config', 'status', array(
318
      'type' => 'int',
319
      'not null' => TRUE,
320
      'default' => ENTITY_CUSTOM,
321
      'size' => 'tiny',
322
      'description' => 'The exportable status of the entity.',
323
    ));
324
    // The module column did already exist before.
325
  }
326
}
327

    
328
/**
329
 * Add an index for the rules configuration plugin column.
330
 */
331
function rules_update_7202() {
332
  db_add_index('rules_config', 'plugin', array('plugin'));
333
}
334

    
335
/**
336
 * Fix the length of the rules_config.name column.
337
 */
338
function rules_update_7203() {
339
  db_drop_unique_key('rules_config', 'name');
340
  $keys = array(
341
    'unique keys' => array(
342
      'name' => array('name'),
343
    ),
344
  );
345
  db_change_field('rules_config', 'name', 'name', array(
346
    'type' => 'varchar',
347
    'length' => '64',
348
    'not null' => TRUE,
349
    'description' => 'The name of the configuration.',
350
  ), $keys);
351
}
352

    
353
/**
354
 * Add a table for rules-config tags.
355
 */
356
function rules_update_7204() {
357
  if (!db_table_exists('rules_tags')) {
358
    $schema['rules_tags'] = array(
359
      'fields' => array(
360
        'id' => array(
361
          'type' => 'int',
362
          'unsigned' => TRUE,
363
          'not null' => TRUE,
364
          'description' => 'The primary identifier of the configuration.',
365
        ),
366
        'tag' => array(
367
          'type' => 'varchar',
368
          'length' => '255',
369
          'not null' => TRUE,
370
          'description' => 'The tag string associated with this configuration',
371
        ),
372
      ),
373
      'primary key' => array('id', 'tag'),
374
      'foreign keys' => array(
375
        'table' => 'rules_config',
376
        'columns' => array('id' => 'id'),
377
      ),
378
    );
379
    db_create_table('rules_tags', $schema['rules_tags']);
380
  }
381
}
382

    
383
/**
384
 * Add the rules_dependencies table and the rules_config.dirty column.
385
 */
386
function rules_update_7205() {
387
  if (!db_table_exists('rules_dependencies')) {
388
    $schema['rules_dependencies'] = array(
389
      'fields' => array(
390
        'id' => array(
391
          'type' => 'int',
392
          'unsigned' => TRUE,
393
          'not null' => TRUE,
394
          'description' => 'The primary identifier of the configuration.',
395
        ),
396
        'module' => array(
397
          'type' => 'varchar',
398
          'length' => '255',
399
          'not null' => TRUE,
400
          'description' => 'The name of the module that is required for the configuration.',
401
        ),
402
      ),
403
      'primary key' => array('id', 'module'),
404
      'indexes' => array(
405
        'module' => array('module'),
406
      ),
407
      'foreign keys' => array(
408
        'table' => 'rules_config',
409
        'columns' => array('id' => 'id'),
410
      ),
411
    );
412
    db_create_table('rules_dependencies', $schema['rules_dependencies']);
413
  }
414
  if (!db_field_exists('rules_config', 'dirty')) {
415
    db_add_field('rules_config', 'dirty', array(
416
      'type' => 'int',
417
      'not null' => TRUE,
418
      'default' => 0,
419
      'size' => 'tiny',
420
    ));
421
  }
422
}
423

    
424
/**
425
 * Flush all caches.
426
 */
427
function rules_update_7206() {
428
  // The update system is going to flush all caches anyway, so nothing to do.
429
}
430

    
431
/**
432
 * Flush all caches.
433
 */
434
function rules_update_7207() {
435
  // The update system is going to flush all caches anyway, so nothing to do.
436
}
437

    
438
/**
439
 * Flush all caches to update the data_is_empty condition info.
440
 */
441
function rules_update_7208() {
442
  // The update system is going to flush all caches anyway, so nothing to do.
443
}
444

    
445
/**
446
 * Creates a flag that enables a permission for using components.
447
 */
448
function rules_update_7209() {
449
  // Create a access exposed flag column.
450
  db_add_field('rules_config', 'access_exposed', array(
451
    'type' => 'int',
452
    'not null' => TRUE,
453
    'default' => 0,
454
    'size' => 'tiny',
455
    'description' => 'Whether to use a permission to control access for using components.',
456
  ));
457
}
458

    
459
/**
460
 * Deletes the unused rules_empty_sets variable.
461
 */
462
function rules_update_7210() {
463
  variable_del('rules_empty_sets');
464
}
465

    
466
/**
467
 * Creates the "owner" column.
468
 */
469
function rules_update_7211() {
470
  // Create a owner column.
471
  if (!db_field_exists('rules_config', 'owner')) {
472
    db_add_field('rules_config', 'owner', array(
473
      'description' => 'The name of the module via which the rule has been configured.',
474
      'type' => 'varchar',
475
      'length' => 255,
476
      'not null' => TRUE,
477
      'default' => 'rules',
478
    ));
479
  }
480
}
481

    
482
/**
483
 * Make sure registry gets rebuilt to avoid upgrade troubles.
484
 */
485
function rules_update_7212() {
486
  // Make sure module information gets refreshed and registry is rebuilt.
487
  drupal_static_reset('system_rebuild_module_data');
488
  registry_rebuild();
489
}
490

    
491
/**
492
 * Recover the "owner" property for broken configurations.
493
 */
494
function rules_update_7213() {
495
  $rows= db_select('rules_config', 'c')
496
    ->fields('c')
497
    ->condition('status', ENTITY_OVERRIDDEN)
498
    ->condition('owner', 'rules', '<>')
499
    ->execute()
500
    ->fetchAllAssoc('id');
501

    
502
  foreach ($rows as $id => $row) {
503
    if ($row->module == $row->owner) {
504
      db_update('rules_config')
505
        ->condition('id', $id)
506
        ->fields(array('owner' => 'rules'))
507
        ->execute();
508
    }
509
  }
510
}
511

    
512
/**
513
 * Switch out the rules_event_whitelist variable for a cache equivalent.
514
 */
515
function rules_update_7214() {
516
  // Set new event_whitelist cache cid.
517
  rules_set_cache('rules_event_whitelist', variable_get('rules_event_whitelist', array()));
518
  // Delete old conf variable.
519
  variable_del('rules_event_whitelist');
520
  // Avoid any missing class errors.
521
  registry_rebuild();
522
  // Clear and rebuild Rules caches.
523
  // See: rules_admin_settings_cache_rebuild_submit.
524
  rules_clear_cache();
525
  rules_get_cache();
526
  _rules_rebuild_component_cache();
527
  RulesEventSet::rebuildEventCache();
528
}