rules.install 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. /**
  3. * @file
  4. * Rules - Installation file.
  5. */
  6. /**
  7. * Implements hook_enable().
  8. */
  9. function rules_enable() {
  10. // Enable evaluation of Rules right after enabling the module.
  11. rules_event_invocation_enabled(TRUE);
  12. }
  13. /**
  14. * Implements hook_install().
  15. */
  16. function rules_install() {
  17. module_load_include('inc', 'rules', 'modules/events');
  18. // Set the modules' weight to 20, see
  19. // https://www.drupal.org/node/445084#comment-1533280 for the reasoning.
  20. db_query("UPDATE {system} SET weight = 20 WHERE name = 'rules'");
  21. }
  22. /**
  23. * Implements hook_uninstall().
  24. */
  25. function rules_uninstall() {
  26. variable_del('rules_debug');
  27. variable_del('rules_debug_log');
  28. variable_del('rules_log_errors');
  29. variable_del('rules_log_level');
  30. variable_del('rules_clean_path');
  31. variable_del('rules_path_cleaning_callback');
  32. variable_del('rules_path_lower_case');
  33. variable_del('rules_path_replacement_char');
  34. variable_del('rules_path_transliteration');
  35. // Delete all the debug region variables and then clear the variables cache.
  36. db_delete('variable')
  37. ->condition('name', 'rules_debug_region_%', 'LIKE')
  38. ->execute();
  39. cache_clear_all('variables', 'cache_bootstrap');
  40. }
  41. /**
  42. * Implements hook_schema().
  43. */
  44. function rules_schema() {
  45. $schema['rules_config'] = array(
  46. 'fields' => array(
  47. 'id' => array(
  48. 'type' => 'serial',
  49. 'not null' => TRUE,
  50. 'description' => 'The internal identifier for any configuration.',
  51. ),
  52. 'name' => array(
  53. 'type' => 'varchar',
  54. 'length' => '64',
  55. 'not null' => TRUE,
  56. 'description' => 'The name of the configuration.',
  57. ),
  58. 'label' => array(
  59. 'type' => 'varchar',
  60. 'length' => '255',
  61. 'not null' => TRUE,
  62. 'description' => 'The label of the configuration.',
  63. 'default' => 'unlabeled',
  64. ),
  65. 'plugin' => array(
  66. 'type' => 'varchar',
  67. 'length' => 127,
  68. 'not null' => TRUE,
  69. 'description' => 'The name of the plugin of this configuration.',
  70. ),
  71. 'active' => array(
  72. 'description' => 'Boolean indicating whether the configuration is active. Usage depends on how the using module makes use of it.',
  73. 'type' => 'int',
  74. 'not null' => TRUE,
  75. 'default' => 1,
  76. ),
  77. 'weight' => array(
  78. 'type' => 'int',
  79. 'not null' => TRUE,
  80. 'default' => 0,
  81. 'size' => 'tiny',
  82. 'description' => 'Weight of the configuration. Usage depends on how the using module makes use of it.',
  83. ),
  84. 'status' => array(
  85. 'type' => 'int',
  86. 'not null' => TRUE,
  87. // Set the default to ENTITY_CUSTOM without using the constant as it is
  88. // not safe to use it at this point.
  89. 'default' => 0x01,
  90. 'size' => 'tiny',
  91. 'description' => 'The exportable status of the entity.',
  92. ),
  93. 'dirty' => array(
  94. 'type' => 'int',
  95. 'not null' => TRUE,
  96. 'default' => 0,
  97. 'size' => 'tiny',
  98. 'description' => 'Dirty configurations fail the integrity check, e.g. due to missing dependencies.',
  99. ),
  100. 'module' => array(
  101. 'description' => 'The name of the providing module if the entity has been defined in code.',
  102. 'type' => 'varchar',
  103. 'length' => 255,
  104. 'not null' => FALSE,
  105. ),
  106. 'owner' => array(
  107. 'description' => 'The name of the module via which the rule has been configured.',
  108. 'type' => 'varchar',
  109. 'length' => 255,
  110. 'not null' => TRUE,
  111. 'default' => 'rules',
  112. ),
  113. 'access_exposed' => array(
  114. 'type' => 'int',
  115. 'not null' => TRUE,
  116. 'default' => 0,
  117. 'size' => 'tiny',
  118. 'description' => 'Whether to use a permission to control access for using components.',
  119. ),
  120. 'data' => array(
  121. 'type' => 'blob',
  122. 'size' => 'big',
  123. 'not null' => FALSE,
  124. 'serialize' => TRUE,
  125. 'description' => 'Everything else, serialized.',
  126. ),
  127. ),
  128. 'primary key' => array('id'),
  129. 'unique keys' => array(
  130. 'name' => array('name'),
  131. ),
  132. 'indexes' => array(
  133. 'plugin' => array('plugin', 'active'),
  134. ),
  135. );
  136. $schema['rules_trigger'] = array(
  137. 'fields' => array(
  138. 'id' => array(
  139. 'type' => 'int',
  140. 'unsigned' => TRUE,
  141. 'not null' => TRUE,
  142. 'description' => 'The primary identifier of the configuration.',
  143. ),
  144. 'event' => array(
  145. 'type' => 'varchar',
  146. 'length' => '127',
  147. 'not null' => TRUE,
  148. 'default' => '',
  149. 'description' => 'The name of the event on which the configuration should be triggered.',
  150. ),
  151. ),
  152. 'primary key' => array('id', 'event'),
  153. 'foreign keys' => array(
  154. 'table' => 'rules_config',
  155. 'columns' => array('id' => 'id'),
  156. ),
  157. );
  158. $schema['rules_tags'] = array(
  159. 'fields' => array(
  160. 'id' => array(
  161. 'type' => 'int',
  162. 'unsigned' => TRUE,
  163. 'not null' => TRUE,
  164. 'description' => 'The primary identifier of the configuration.',
  165. ),
  166. 'tag' => array(
  167. 'type' => 'varchar',
  168. 'length' => '255',
  169. 'not null' => TRUE,
  170. 'description' => 'The tag string associated with this configuration',
  171. ),
  172. ),
  173. 'primary key' => array('id', 'tag'),
  174. 'foreign keys' => array(
  175. 'table' => 'rules_config',
  176. 'columns' => array('id' => 'id'),
  177. ),
  178. );
  179. $schema['rules_dependencies'] = array(
  180. 'fields' => array(
  181. 'id' => array(
  182. 'type' => 'int',
  183. 'unsigned' => TRUE,
  184. 'not null' => TRUE,
  185. 'description' => 'The primary identifier of the configuration.',
  186. ),
  187. 'module' => array(
  188. 'type' => 'varchar',
  189. 'length' => '255',
  190. 'not null' => TRUE,
  191. 'description' => 'The name of the module that is required for the configuration.',
  192. ),
  193. ),
  194. 'primary key' => array('id', 'module'),
  195. 'indexes' => array(
  196. 'module' => array('module'),
  197. ),
  198. 'foreign keys' => array(
  199. 'table' => 'rules_config',
  200. 'columns' => array('id' => 'id'),
  201. ),
  202. );
  203. $schema['cache_rules'] = drupal_get_schema_unprocessed('system', 'cache');
  204. $schema['cache_rules']['description'] = 'Cache table for the rules engine to store configured items.';
  205. return $schema;
  206. }
  207. /**
  208. * Upgrade from Rules 6.x-1.x to 7.x.
  209. */
  210. function rules_update_7200() {
  211. // Create the new db tables first.
  212. $schema['rules_config'] = array(
  213. 'fields' => array(
  214. 'id' => array(
  215. 'type' => 'serial',
  216. 'not null' => TRUE,
  217. 'description' => 'The internal identifier for any configuration.',
  218. ),
  219. 'name' => array(
  220. 'type' => 'varchar',
  221. 'length' => '255',
  222. 'not null' => TRUE,
  223. 'description' => 'The name of the configuration.',
  224. ),
  225. 'label' => array(
  226. 'type' => 'varchar',
  227. 'length' => '255',
  228. 'not null' => TRUE,
  229. 'description' => 'The label of the configuration.',
  230. 'default' => 'unlabeled',
  231. ),
  232. 'plugin' => array(
  233. 'type' => 'varchar',
  234. 'length' => 127,
  235. 'not null' => TRUE,
  236. 'description' => 'The name of the plugin of this configuration.',
  237. ),
  238. 'active' => array(
  239. 'description' => 'Boolean indicating whether the configuration is active. Usage depends on how the using module makes use of it.',
  240. 'type' => 'int',
  241. 'not null' => TRUE,
  242. 'default' => 1,
  243. ),
  244. 'weight' => array(
  245. 'type' => 'int',
  246. 'not null' => TRUE,
  247. 'default' => 0,
  248. 'size' => 'tiny',
  249. 'description' => 'Weight of the configuration. Usage depends on how the using module makes use of it.',
  250. ),
  251. 'status' => array(
  252. 'type' => 'int',
  253. 'not null' => TRUE,
  254. // Set the default to ENTITY_CUSTOM without using the constant as it is
  255. // not safe to use it at this point.
  256. 'default' => 0x01,
  257. 'size' => 'tiny',
  258. 'description' => 'The exportable status of the entity.',
  259. ),
  260. 'module' => array(
  261. 'description' => 'The name of the providing module if the entity has been defined in code.',
  262. 'type' => 'varchar',
  263. 'length' => 255,
  264. 'not null' => FALSE,
  265. ),
  266. 'data' => array(
  267. 'type' => 'blob',
  268. 'size' => 'big',
  269. 'not null' => FALSE,
  270. 'serialize' => TRUE,
  271. 'description' => 'Everything else, serialized.',
  272. ),
  273. ),
  274. 'primary key' => array('id'),
  275. 'unique keys' => array(
  276. 'name' => array('name'),
  277. ),
  278. );
  279. $schema['rules_trigger'] = array(
  280. 'fields' => array(
  281. 'id' => array(
  282. 'type' => 'int',
  283. 'unsigned' => TRUE,
  284. 'not null' => TRUE,
  285. 'description' => 'The primary identifier of the configuration.',
  286. ),
  287. 'event' => array(
  288. 'type' => 'varchar',
  289. 'length' => '127',
  290. 'not null' => TRUE,
  291. 'default' => '',
  292. 'description' => 'The name of the event on which the configuration should be triggered.',
  293. ),
  294. ),
  295. 'primary key' => array('id', 'event'),
  296. 'foreign keys' => array(
  297. 'table' => 'rules_config',
  298. 'columns' => array('id' => 'id'),
  299. ),
  300. );
  301. db_create_table('rules_config', $schema['rules_config']);
  302. db_create_table('rules_trigger', $schema['rules_trigger']);
  303. // The cache table already exists, but changed. So re-create it.
  304. db_drop_table('cache_rules');
  305. $schema['cache_rules'] = drupal_get_schema_unprocessed('system', 'cache');
  306. $schema['cache_rules']['description'] = 'Cache table for the rules engine to store configured items.';
  307. db_create_table('cache_rules', $schema['cache_rules']);
  308. // Remove deprecated variables.
  309. variable_del('rules_inactive_sets');
  310. variable_del('rules_show_fixed');
  311. variable_del('rules_hide_token_message');
  312. variable_del('rules_counter');
  313. 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.');
  314. }
  315. /**
  316. * Add in the exportable entity db columns as required by the entity API.
  317. */
  318. function rules_update_7201() {
  319. // Previously this was update 7200, so check whether we need to run it really.
  320. // The update has been moved as 7200 needs to be the 6.x-7.x upgrade.
  321. if (!db_field_exists('rules_config', 'status')) {
  322. db_add_field('rules_config', 'status', array(
  323. 'type' => 'int',
  324. 'not null' => TRUE,
  325. 'default' => ENTITY_CUSTOM,
  326. 'size' => 'tiny',
  327. 'description' => 'The exportable status of the entity.',
  328. ));
  329. // The module column did already exist before.
  330. }
  331. }
  332. /**
  333. * Add an index for the rules configuration plugin column.
  334. */
  335. function rules_update_7202() {
  336. db_add_index('rules_config', 'plugin', array('plugin'));
  337. }
  338. /**
  339. * Fix the length of the rules_config.name column.
  340. */
  341. function rules_update_7203() {
  342. db_drop_unique_key('rules_config', 'name');
  343. $keys = array(
  344. 'unique keys' => array(
  345. 'name' => array('name'),
  346. ),
  347. );
  348. db_change_field('rules_config', 'name', 'name', array(
  349. 'type' => 'varchar',
  350. 'length' => '64',
  351. 'not null' => TRUE,
  352. 'description' => 'The name of the configuration.',
  353. ), $keys);
  354. }
  355. /**
  356. * Add a table for rules-config tags.
  357. */
  358. function rules_update_7204() {
  359. if (!db_table_exists('rules_tags')) {
  360. $schema['rules_tags'] = array(
  361. 'fields' => array(
  362. 'id' => array(
  363. 'type' => 'int',
  364. 'unsigned' => TRUE,
  365. 'not null' => TRUE,
  366. 'description' => 'The primary identifier of the configuration.',
  367. ),
  368. 'tag' => array(
  369. 'type' => 'varchar',
  370. 'length' => '255',
  371. 'not null' => TRUE,
  372. 'description' => 'The tag string associated with this configuration',
  373. ),
  374. ),
  375. 'primary key' => array('id', 'tag'),
  376. 'foreign keys' => array(
  377. 'table' => 'rules_config',
  378. 'columns' => array('id' => 'id'),
  379. ),
  380. );
  381. db_create_table('rules_tags', $schema['rules_tags']);
  382. }
  383. }
  384. /**
  385. * Add the rules_dependencies table and the rules_config.dirty column.
  386. */
  387. function rules_update_7205() {
  388. if (!db_table_exists('rules_dependencies')) {
  389. $schema['rules_dependencies'] = array(
  390. 'fields' => array(
  391. 'id' => array(
  392. 'type' => 'int',
  393. 'unsigned' => TRUE,
  394. 'not null' => TRUE,
  395. 'description' => 'The primary identifier of the configuration.',
  396. ),
  397. 'module' => array(
  398. 'type' => 'varchar',
  399. 'length' => '255',
  400. 'not null' => TRUE,
  401. 'description' => 'The name of the module that is required for the configuration.',
  402. ),
  403. ),
  404. 'primary key' => array('id', 'module'),
  405. 'indexes' => array(
  406. 'module' => array('module'),
  407. ),
  408. 'foreign keys' => array(
  409. 'table' => 'rules_config',
  410. 'columns' => array('id' => 'id'),
  411. ),
  412. );
  413. db_create_table('rules_dependencies', $schema['rules_dependencies']);
  414. }
  415. if (!db_field_exists('rules_config', 'dirty')) {
  416. db_add_field('rules_config', 'dirty', array(
  417. 'type' => 'int',
  418. 'not null' => TRUE,
  419. 'default' => 0,
  420. 'size' => 'tiny',
  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. * Flush all caches.
  432. */
  433. function rules_update_7207() {
  434. // The update system is going to flush all caches anyway, so nothing to do.
  435. }
  436. /**
  437. * Flush all caches to update the data_is_empty condition info.
  438. */
  439. function rules_update_7208() {
  440. // The update system is going to flush all caches anyway, so nothing to do.
  441. }
  442. /**
  443. * Creates a flag that enables a permission for using components.
  444. */
  445. function rules_update_7209() {
  446. // Create a access exposed flag column.
  447. db_add_field('rules_config', 'access_exposed', array(
  448. 'type' => 'int',
  449. 'not null' => TRUE,
  450. 'default' => 0,
  451. 'size' => 'tiny',
  452. 'description' => 'Whether to use a permission to control access for using components.',
  453. ));
  454. }
  455. /**
  456. * Deletes the unused rules_empty_sets variable.
  457. */
  458. function rules_update_7210() {
  459. variable_del('rules_empty_sets');
  460. }
  461. /**
  462. * Creates the "owner" column.
  463. */
  464. function rules_update_7211() {
  465. // Create a owner column.
  466. if (!db_field_exists('rules_config', 'owner')) {
  467. db_add_field('rules_config', 'owner', array(
  468. 'description' => 'The name of the module via which the rule has been configured.',
  469. 'type' => 'varchar',
  470. 'length' => 255,
  471. 'not null' => TRUE,
  472. 'default' => 'rules',
  473. ));
  474. }
  475. }
  476. /**
  477. * Make sure registry gets rebuilt to avoid upgrade troubles.
  478. */
  479. function rules_update_7212() {
  480. // Make sure module information gets refreshed and registry is rebuilt.
  481. drupal_static_reset('system_rebuild_module_data');
  482. registry_rebuild();
  483. }
  484. /**
  485. * Recover the "owner" property for broken configurations.
  486. */
  487. function rules_update_7213() {
  488. $rows = db_select('rules_config', 'c')
  489. ->fields('c')
  490. ->condition('status', ENTITY_OVERRIDDEN)
  491. ->condition('owner', 'rules', '<>')
  492. ->execute()
  493. ->fetchAllAssoc('id');
  494. foreach ($rows as $id => $row) {
  495. if ($row->module == $row->owner) {
  496. db_update('rules_config')
  497. ->condition('id', $id)
  498. ->fields(array('owner' => 'rules'))
  499. ->execute();
  500. }
  501. }
  502. }
  503. /**
  504. * Switch out the rules_event_whitelist variable for a cache equivalent.
  505. */
  506. function rules_update_7214() {
  507. // Enable Rules if currently disabled so that this update won't fail.
  508. $disable_rules = FALSE;
  509. if (!module_exists('rules')) {
  510. module_enable(array('rules'));
  511. $disable_rules = TRUE;
  512. }
  513. // Set new event_whitelist cache cid.
  514. rules_set_cache('rules_event_whitelist', variable_get('rules_event_whitelist', array()));
  515. // Delete old conf variable.
  516. variable_del('rules_event_whitelist');
  517. // Avoid any missing class errors.
  518. registry_rebuild();
  519. // Clear and rebuild Rules caches.
  520. // See: rules_admin_settings_cache_rebuild_submit.
  521. rules_clear_cache();
  522. rules_get_cache();
  523. _rules_rebuild_component_cache();
  524. RulesEventSet::rebuildEventCache();
  525. // Disable Rules again if it was disabled before this update started.
  526. if ($disable_rules) {
  527. module_disable(array('rules'));
  528. }
  529. }
  530. /**
  531. * Add an index for retrieving active config of a certain plugin.
  532. */
  533. function rules_update_7215() {
  534. if (db_index_exists('rules_config', 'plugin')) {
  535. db_drop_index('rules_config', 'plugin');
  536. }
  537. db_add_index('rules_config', 'plugin', array('plugin', 'active'));
  538. }