rules.install 14.4 KB
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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
<?php

/**
 * @file Rules - Installation file.
 */

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

/**
 * Implements hook_uninstall().
 */
function rules_uninstall() {
  variable_del('rules_event_whitelist');
  variable_del('rules_debug');
}

/**
 * Implements hook_schema().
 */
function rules_schema() {
  $schema['rules_config'] = array(
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'The internal identifier for any configuration.',
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => '64',
        'not null' => TRUE,
        'description' => 'The name of the configuration.',
      ),
      'label' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'description' => 'The label of the configuration.',
        'default' => 'unlabeled',
      ),
     'plugin' => array(
        'type' => 'varchar',
        'length' => 127,
        'not null' => TRUE,
        'description' => 'The name of the plugin of this configuration.',
      ),
      'active' => array(
        'description' => 'Boolean indicating whether the configuration is active. Usage depends on how the using module makes use of it.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Weight of the configuration. Usage depends on how the using module makes use of it.',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // Set the default to ENTITY_CUSTOM without using the constant as it is
        // not safe to use it at this point.
        'default' => 0x01,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'dirty' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Dirty configurations fail the integrity check, e.g. due to missing dependencies.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
      'owner' => array(
        'description' => 'The name of the module via which the rule has been configured.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'rules',
      ),
      'access_exposed' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Whether to use a permission to control access for using components.',
      ),
      'data' => array(
        'type' => 'blob',
        'size' => 'big',
        'not null' => FALSE,
        'serialize' => TRUE,
        'description' => 'Everything else, serialized.',
      ),
    ),
    'primary key' => array('id'),
    'unique keys' => array(
      'name' => array('name'),
    ),
    'indexes' => array(
      'plugin' => array('plugin'),
    ),
  );
  $schema['rules_trigger'] = array(
    'fields' => array(
      'id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The primary identifier of the configuration.',
      ),
      'event' => array(
        'type' => 'varchar',
        'length' => '127',
        'not null' => TRUE,
        'default' => '',
        'description' => 'The name of the event on which the configuration should be triggered.',
      ),
    ),
    'primary key' => array('id', 'event'),
    'foreign keys' => array(
      'table' => 'rules_config',
      'columns' => array('id' => 'id'),
    ),
  );
  $schema['rules_tags'] = array(
    'fields' => array(
      'id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The primary identifier of the configuration.',
      ),
      'tag' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'description' => 'The tag string associated with this configuration',
      ),
    ),
    'primary key' => array('id', 'tag'),
    'foreign keys' => array(
      'table' => 'rules_config',
      'columns' => array('id' => 'id'),
    ),
  );
  $schema['rules_dependencies'] = array(
    'fields' => array(
      'id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The primary identifier of the configuration.',
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'description' => 'The name of the module that is required for the configuration.',
      ),
    ),
    'primary key' => array('id', 'module'),
    'indexes' => array(
      'module' => array('module'),
    ),
    'foreign keys' => array(
      'table' => 'rules_config',
      'columns' => array('id' => 'id'),
    ),
  );
  $schema['cache_rules'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_rules']['description'] = 'Cache table for the rules engine to store configured items.';
  return $schema;
}

/**
 * Upgrade from Rules 6.x-1.x to 7.x.
 */
function rules_update_7200() {
  // Create the new db tables first.
  $schema['rules_config'] = array(
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'The internal identifier for any configuration.',
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'description' => 'The name of the configuration.',
      ),
      'label' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'description' => 'The label of the configuration.',
        'default' => 'unlabeled',
      ),
     'plugin' => array(
        'type' => 'varchar',
        'length' => 127,
        'not null' => TRUE,
        'description' => 'The name of the plugin of this configuration.',
      ),
      'active' => array(
        'description' => 'Boolean indicating whether the configuration is active. Usage depends on how the using module makes use of it.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Weight of the configuration. Usage depends on how the using module makes use of it.',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // Set the default to ENTITY_CUSTOM without using the constant as it is
        // not safe to use it at this point.
        'default' => 0x01,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
      'data' => array(
        'type' => 'blob',
        'size' => 'big',
        'not null' => FALSE,
        'serialize' => TRUE,
        'description' => 'Everything else, serialized.',
      ),
    ),
    'primary key' => array('id'),
    'unique keys' => array(
      'name' => array('name'),
    ),
  );
  $schema['rules_trigger'] = array(
    'fields' => array(
      'id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'The primary identifier of the configuration.',
      ),
      'event' => array(
        'type' => 'varchar',
        'length' => '127',
        'not null' => TRUE,
        'default' => '',
        'description' => 'The name of the event on which the configuration should be triggered.',
      ),
    ),
    'primary key' => array('id', 'event'),
    'foreign keys' => array(
      'table' => 'rules_config',
      'columns' => array('id' => 'id'),
    ),
  );
  db_create_table('rules_config', $schema['rules_config']);
  db_create_table('rules_trigger', $schema['rules_trigger']);
  // The cache table already exists, but changed. So re-create it.
  db_drop_table('cache_rules');
  $schema['cache_rules'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_rules']['description'] = 'Cache table for the rules engine to store configured items.';
  db_create_table('cache_rules', $schema['cache_rules']);
  // Remove deprecated variables.
  variable_del('rules_inactive_sets');
  variable_del('rules_show_fixed');
  variable_del('rules_hide_token_message');
  variable_del('rules_counter');

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

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

/**
 * Add an index for the rules configuration plugin column.
 */
function rules_update_7202() {
  db_add_index('rules_config', 'plugin', array('plugin'));
}

/**
 * Fix the length of the rules_config.name column.
 */
function rules_update_7203() {
  db_drop_unique_key('rules_config', 'name');
  $keys = array(
    'unique keys' => array(
      'name' => array('name'),
    ),
  );
  db_change_field('rules_config', 'name', 'name', array(
    'type' => 'varchar',
    'length' => '64',
    'not null' => TRUE,
    'description' => 'The name of the configuration.',
  ), $keys);
}

/**
 * Add a table for rules-config tags.
 */
function rules_update_7204() {
  if (!db_table_exists('rules_tags')) {
    $schema['rules_tags'] = array(
      'fields' => array(
        'id' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'description' => 'The primary identifier of the configuration.',
        ),
        'tag' => array(
          'type' => 'varchar',
          'length' => '255',
          'not null' => TRUE,
          'description' => 'The tag string associated with this configuration',
        ),
      ),
      'primary key' => array('id', 'tag'),
      'foreign keys' => array(
        'table' => 'rules_config',
        'columns' => array('id' => 'id'),
      ),
    );
    db_create_table('rules_tags', $schema['rules_tags']);
  }
}

/**
 * Add the rules_dependencies table and the rules_config.dirty column.
 */
function rules_update_7205() {
  if (!db_table_exists('rules_dependencies')) {
    $schema['rules_dependencies'] = array(
      'fields' => array(
        'id' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'description' => 'The primary identifier of the configuration.',
        ),
        'module' => array(
          'type' => 'varchar',
          'length' => '255',
          'not null' => TRUE,
          'description' => 'The name of the module that is required for the configuration.',
        ),
      ),
      'primary key' => array('id', 'module'),
      'indexes' => array(
        'module' => array('module'),
      ),
      'foreign keys' => array(
        'table' => 'rules_config',
        'columns' => array('id' => 'id'),
      ),
    );
    db_create_table('rules_dependencies', $schema['rules_dependencies']);
  }
  if (!db_field_exists('rules_config', 'dirty')) {
    db_add_field('rules_config', 'dirty', array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
      'size' => 'tiny',
    ));
  }
}

/**
 * Flush all caches.
 */
function rules_update_7206() {
  // The update system is going to flush all caches anyway, so nothing to do.
}

/**
 * Flush all caches.
 */
function rules_update_7207() {
  // The update system is going to flush all caches anyway, so nothing to do.
}

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

/**
 * Creates a flag that enables a permission for using components.
 */
function rules_update_7209() {
  // Create a access exposed flag column.
  db_add_field('rules_config', 'access_exposed', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'size' => 'tiny',
    'description' => 'Whether to use a permission to control access for using components.',
  ));
}

/**
 * Deletes the unused rules_empty_sets variable.
 */
function rules_update_7210() {
  variable_del('rules_empty_sets');
}

/**
 * Creates the "owner" column.
 */
function rules_update_7211() {
  // Create a owner column.
  if (!db_field_exists('rules_config', 'owner')) {
    db_add_field('rules_config', 'owner', array(
      'description' => 'The name of the module via which the rule has been configured.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => 'rules',
    ));
  }
}

/**
 * Make sure registry gets rebuilt to avoid upgrade troubles.
 */
function rules_update_7212() {
  // Make sure module information gets refreshed and registry is rebuilt.
  drupal_static_reset('system_rebuild_module_data');
  registry_rebuild();
}

/**
 * Recover the "owner" property for broken configurations.
 */
function rules_update_7213() {
  $rows= db_select('rules_config', 'c')
    ->fields('c')
    ->condition('status', ENTITY_OVERRIDDEN)
    ->condition('owner', 'rules', '<>')
    ->execute()
    ->fetchAllAssoc('id');

  foreach ($rows as $id => $row) {
    if ($row->module == $row->owner) {
      db_update('rules_config')
        ->condition('id', $id)
        ->fields(array('owner' => 'rules'))
        ->execute();
    }
  }
}