rules.drush.inc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * @file
  4. * Rules module drush integration.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function rules_drush_command() {
  10. $items = array();
  11. $items['rules-list'] = array(
  12. 'description' => 'List all the active and inactive rules for your site.',
  13. 'drupal dependencies' => array('rules'),
  14. 'aliases' => array('rules'),
  15. 'outputformat' => array(
  16. 'default' => 'table',
  17. 'pipe-format' => 'list',
  18. 'field-labels' => array(
  19. 'rule' => dt('Rule'),
  20. 'label' => dt('Label'),
  21. 'event' => dt('Event'),
  22. 'active' => dt('Active'),
  23. 'status' => dt('Status'),
  24. ),
  25. 'output-data-type' => 'format-table',
  26. ),
  27. );
  28. $items['rules-enable'] = array(
  29. 'description' => 'Enable a rule on your site.',
  30. 'arguments' => array(
  31. 'rule' => 'Rule name to enable.',
  32. ),
  33. 'drupal dependencies' => array('rules'),
  34. 'aliases' => array('re'),
  35. );
  36. $items['rules-disable'] = array(
  37. 'description' => 'Disable a rule on your site.',
  38. 'arguments' => array(
  39. 'rule' => 'Rule name to export.',
  40. ),
  41. 'drupal dependencies' => array('rules'),
  42. 'aliases' => array('rd'),
  43. );
  44. $items['rules-revert'] = array(
  45. 'description' => 'Revert a rule to its original state on your site.',
  46. 'arguments' => array(
  47. 'rule' => 'Rule name to revert.',
  48. ),
  49. 'drupal dependencies' => array('rules'),
  50. );
  51. $items['rules-delete'] = array(
  52. 'description' => 'Delete a rule on your site.',
  53. 'arguments' => array(
  54. 'rule' => 'Rules name to delete.',
  55. ),
  56. 'drupal dependencies' => array('rules'),
  57. );
  58. $items['rules-export'] = array(
  59. 'description' => 'Export a rule.',
  60. 'arguments' => array(
  61. 'rule' => 'Rules name to export.',
  62. ),
  63. 'drupal dependencies' => array('rules'),
  64. );
  65. return $items;
  66. }
  67. /**
  68. * Implements hook_drush_help().
  69. */
  70. function rules_drush_help($section) {
  71. switch ($section) {
  72. case 'drush:rules-list':
  73. return dt('List all the rules on your site.');
  74. case 'drush:rules-enable':
  75. return dt('Enable/activate a rule on your site.');
  76. case 'drush:rules-disable':
  77. return dt('Disable/deactivate a rule on your site.');
  78. case 'drush:rules-revert':
  79. return dt('Revert a module-provided rule to its original state on your site.');
  80. case 'drush:rules-delete':
  81. return dt('Delete a rule on your site.');
  82. case 'drush:rules-export':
  83. return dt('Export a rule.');
  84. }
  85. }
  86. /**
  87. * Get a list of all rules.
  88. */
  89. function drush_rules_list() {
  90. $rules = rules_config_load_multiple(FALSE);
  91. $rows = array();
  92. foreach ($rules as $rule) {
  93. if (!empty($rule->name) && !empty($rule->label)) {
  94. $events = array();
  95. $event_info = rules_fetch_data('event_info');
  96. if ($rule instanceof RulesTriggerableInterface) {
  97. foreach ($rule->events() as $event_name) {
  98. $event_info += array(
  99. $event_name => array(
  100. 'label' => dt('Unknown event "!event_name"', array('!event_name' => $event_name)),
  101. ),
  102. );
  103. $events[] = check_plain($event_info[$event_name]['label']);
  104. }
  105. }
  106. $rows[$rule->name] = array(
  107. 'rule' => $rule->name,
  108. 'label' => $rule->label,
  109. 'event' => implode(', ', $events),
  110. 'active' => $rule->active ? dt('Enabled') : dt('Disabled'),
  111. 'status' => $rule->status ? theme('entity_status', array('status' => $rule->status, 'html' => FALSE)) : '',
  112. );
  113. }
  114. }
  115. if (version_compare(DRUSH_VERSION, '6.0', '<')) {
  116. drush_print_table($rows, TRUE);
  117. }
  118. return $rows;
  119. }
  120. /**
  121. * Enable a rule on the site.
  122. */
  123. function drush_rules_enable() {
  124. $args = func_get_args();
  125. $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
  126. if (empty($rule_name)) {
  127. return drush_set_error('', 'No rule name given.');
  128. }
  129. $rule = rules_config_load($rule_name);
  130. if (empty($rule)) {
  131. return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
  132. }
  133. if (empty($rule->active)) {
  134. $rule->active = TRUE;
  135. $rule->save();
  136. drush_log(dt('The rule "!name" has been enabled.', array('!name' => $rule_name)), 'success');
  137. }
  138. else {
  139. drush_log(dt('The rule "!name" is already enabled.', array('!name' => $rule_name)), 'warning');
  140. }
  141. }
  142. /**
  143. * Disable a rule on the site.
  144. */
  145. function drush_rules_disable() {
  146. $args = func_get_args();
  147. $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
  148. if (empty($rule_name)) {
  149. return drush_set_error('', 'No rule name given.');
  150. }
  151. $rule = rules_config_load($rule_name);
  152. if (empty($rule)) {
  153. return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
  154. }
  155. if (!empty($rule->active)) {
  156. $rule->active = FALSE;
  157. $rule->save();
  158. drush_log(dt('The rule "!name" has been disabled.', array('!name' => $rule_name)), 'success');
  159. }
  160. else {
  161. drush_log(dt('The rule "!name" is already disabled.', array('!name' => $rule_name)), 'warning');
  162. }
  163. }
  164. /**
  165. * Reverts a rule on the site.
  166. */
  167. function drush_rules_revert() {
  168. $args = func_get_args();
  169. $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
  170. if (empty($rule_name)) {
  171. return drush_set_error('', 'No rule name given.');
  172. }
  173. $rule = rules_config_load($rule_name);
  174. if (empty($rule)) {
  175. return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
  176. }
  177. if (($rule->status & ENTITY_OVERRIDDEN) == ENTITY_OVERRIDDEN) {
  178. if (drush_confirm(dt('Are you sure you want to revert the rule named "!rule-name"? This action cannot be undone.', array('!rule-name' => $rule_name)))) {
  179. $rule->delete();
  180. drush_log(dt('The rule "!name" has been reverted to its default state.', array('!name' => $rule_name)), 'success');
  181. }
  182. else {
  183. drush_user_abort();
  184. }
  185. }
  186. else {
  187. drush_log(dt('The rule "!name" has not been overridden and can\'t be reverted.', array('!name' => $rule_name)), 'warning');
  188. }
  189. }
  190. /**
  191. * Deletes a rule on the site.
  192. */
  193. function drush_rules_delete() {
  194. $args = func_get_args();
  195. $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
  196. if (empty($rule_name)) {
  197. return drush_set_error('', 'No rule name given.');
  198. }
  199. $rule = rules_config_load($rule_name);
  200. if (empty($rule)) {
  201. return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
  202. }
  203. if (drush_confirm(dt('Are you sure you want to delete the rule named "!rule-name"? This action cannot be undone.', array('!rule-name' => $rule_name)))) {
  204. $rule->delete();
  205. drush_log(dt('The rule "!name" has been deleted.', array('!name' => $rule_name)), 'success');
  206. }
  207. else {
  208. drush_user_abort();
  209. }
  210. }
  211. /**
  212. * Exports a single rule.
  213. */
  214. function drush_rules_export() {
  215. $args = func_get_args();
  216. $rule_name = (!empty($args) && is_array($args)) ? array_shift($args) : '';
  217. if (empty($rule_name)) {
  218. return drush_set_error('', dt('No rule name given.'));
  219. }
  220. $rule = rules_config_load($rule_name);
  221. if (empty($rule)) {
  222. return drush_set_error('', dt('Could not load rule named "!rule-name".', array('!rule-name' => $rule_name)));
  223. }
  224. drush_print($rule->export());
  225. drush_log(dt('The rule "!name" has been exported.', array('!name' => $rule_name)), 'success');
  226. }