rules.install 15 KB

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