votingapi.drush.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @file
  4. * Drush commands to generate votingapi votes, recalculate results
  5. * for existing votes, or flush VotingAPI data entirely.
  6. */
  7. /**
  8. * Implements of hook_drush_help().
  9. */
  10. function votingapi_drush_help($section) {
  11. switch ($section) {
  12. case 'drush:generate-votes':
  13. return dt('Usage: drush generate-votes <entity_type> <vote_type>.');
  14. case 'drush:votingapi-recalculate':
  15. return dt('Usage: drush votingapi-recalculate <entity_type>.');
  16. case 'drush:votingapi-flush':
  17. return dt('Usage: drush votingapi-flush <entity_type>.');
  18. }
  19. }
  20. /**
  21. * Implements of hook_drush_command().
  22. */
  23. function votingapi_drush_command() {
  24. $items['generate-votes'] = array(
  25. 'description' => 'Creates dummy voting data.',
  26. 'arguments' => array(
  27. 'entity_type' => 'The type of entity to generate votes for.',
  28. 'vote_type' => 'The type of votes to generate, defaults to \'percent\'.',
  29. ),
  30. 'drupal dependencies' => array('devel_generate'),
  31. 'options' => array(
  32. 'kill' => 'Specify \'kill\' to delete all existing votes before generating new ones.',
  33. 'age' => 'The maximum age, in seconds, of each vote.',
  34. 'node_types' => 'A comma delimited list of node types to generate votes for, if the entity type is \'node\'.',
  35. ),
  36. 'aliases' => array('genv'),
  37. );
  38. $items['votingapi-recalculate'] = array(
  39. 'description' => 'Regenerates voting results from raw vote data.',
  40. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
  41. // Various D7 code assumes we have a uid.
  42. 'arguments' => array(
  43. 'entity_type' => 'The type of entity to recalculate vote results for.',
  44. ),
  45. 'aliases' => array('vcalc'),
  46. );
  47. $items['votingapi-flush'] = array(
  48. 'description' => 'Deletes all existing voting data.',
  49. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
  50. // Various D7 code assumes we have a uid.
  51. 'arguments' => array(
  52. 'entity_type' => 'The type of entity whose voting data should be flushed.',
  53. ),
  54. 'aliases' => array('vflush'),
  55. );
  56. return $items;
  57. }
  58. /**
  59. * Command callback. Generate a number of votes.
  60. */
  61. function drush_votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent') {
  62. $options = array(
  63. 'kill' => drush_get_option('kill'),
  64. 'age' => drush_get_option('age'),
  65. 'types' => drush_get_option('types'),
  66. );
  67. require_once(drupal_get_path('module', 'votingapi') . '/votingapi.devel.inc');
  68. votingapi_generate_votes($entity_type, $vote_type, $options);
  69. drush_log(t('Generating @vtype votes for @etype entities.', array(
  70. '@vtype' => $vote_type,
  71. '@etype' => $entity_type,
  72. )), 'success');
  73. }
  74. /**
  75. * Command callback. Recalculate voting results.
  76. */
  77. function drush_votingapi_recalculate($entity_type = 'node', $entity_id = NULL) {
  78. // Prep some starter query objects.
  79. $data = array();
  80. if (empty($entity_id)) {
  81. $votes = db_select('votingapi_vote', 'vv')
  82. ->fields('vv', array('entity_type', 'entity_id'))
  83. ->condition('entity_type', $entity_type, '=')->distinct(TRUE)
  84. ->execute()->fetchAll(PDO::FETCH_ASSOC);
  85. $message = t('Rebuilt voting results for @type votes.', array('@type' => $entity_type));
  86. }
  87. else {
  88. $votes[] = array('entity_type' => $entity_type, 'entity_id' => $entity_id);
  89. $message = t('Rebuilt all voting results.');
  90. }
  91. foreach ($votes as $vote) {
  92. votingapi_recalculate_results($vote['entity_type'], $vote['entity_id'], TRUE);
  93. }
  94. drush_log($message, 'success');
  95. }
  96. /**
  97. * Command callback. Flush votes and results.
  98. */
  99. function drush_votingapi_flush($entity_type = NULL, $entity_id = NULL) {
  100. if (drush_confirm(dt("Delete @type voting data?", array('@type' => empty($entity_type) ? dt('all') : $entity_type)))) {
  101. $cache = db_delete('votingapi_cache');
  102. $votes = db_delete('votingapi_vote');
  103. if (!empty($entity_type)) {
  104. $cache->condition('entity_type', $entity_type);
  105. $votes->condition('entity_type', $entity_type);
  106. }
  107. if (!empty($entity_id)) {
  108. $cache->condition('entity_id', $entity_id);
  109. $votes->condition('entity_id', $entity_id);
  110. }
  111. $cache->execute();
  112. $votes->execute();
  113. drush_log(t('Flushed vote data for @type entities.', array('@type' => empty($entity_type) ? t('all') : $entity_type)), 'success');
  114. }
  115. }