PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/votingapi/votingapi.drush.inc

#
Pascal | 199 lines | 59 code | 17 blank | 123 comment | 9 complexity | 194b1b81f2cc94e91e254c3b94471ed5 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file
  4. * Generate votingapi votes, recalculate results for existing votes, or flush
  5. * VotingAPI data entirely.
  6. */
  7. /**
  8. * Implementation 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. * Implementation 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, // Various D7 code assumes we have a uid.
  41. 'arguments' => array(
  42. 'entity_type' => 'The type of entity to recalculate vote results for.',
  43. ),
  44. 'aliases' => array('vcalc'),
  45. );
  46. $items['votingapi-flush'] = array(
  47. 'description' => 'Deletes all existing voting data.',
  48. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, // Various D7 code assumes we have a uid.
  49. 'arguments' => array(
  50. 'entity_type' => 'The type of entity whose voting data should be flushed.',
  51. ),
  52. 'aliases' => array('vflush'),
  53. );
  54. return $items;
  55. }
  56. /**
  57. * Command callback. Generate a number of votes.
  58. */
  59. function drush_votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent') {
  60. $options = array(
  61. 'kill' => drush_get_option('kill'),
  62. 'age' => drush_get_option('age'),
  63. 'types' => drush_get_option('types'),
  64. );
  65. votingapi_generate_votes($entity_type, $vote_type, $options);
  66. drush_log(t('Generating @vtype votes for @etype entities.', array('@vtype' => $vote_type, '@etype' => $entity_type)), 'success');
  67. }
  68. /**
  69. * Utility function to generate votes.
  70. */
  71. function votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent', $options = array()) {
  72. module_load_include('inc', 'devel_generate');
  73. $options += array(
  74. 'age' => 36000,
  75. 'types' => $roles = drush_get_option('types') ? explode(',', drush_get_option('types')) : array(),
  76. 'kill' => FALSE,
  77. );
  78. if (!empty($options['kill_votes'])) {
  79. db_truncate('votingapi_vote');
  80. db_truncate('votingapi_cache');
  81. }
  82. if (($schema = drupal_get_schema($entity_type)) && ($entity_id_column = array_shift($schema['primary key']))) {
  83. // oh look we found a schema yay
  84. }
  85. else {
  86. $entity_type = 'node';
  87. $entity_id_column = 'nid';
  88. }
  89. $uids = devel_get_users();
  90. $query = db_select($entity_type, 'e')->fields('e', array($entity_id_column));
  91. if ($entity_type == 'node' && !empty($options['types'])) {
  92. $query->condition('e.type', $node_types, 'IN');
  93. }
  94. $results = $query->execute()->fetchAll(PDO::FETCH_ASSOC);
  95. foreach ($results as $entity) {
  96. _votingapi_cast_votes($entity_type, $entity[$entity_id_column], $options['age'], $uids, $vote_type);
  97. }
  98. }
  99. /**
  100. * Utility function to generate votes on a node by a set of users.
  101. */
  102. function _votingapi_cast_votes($etype, $eid, $timestamp = 0, $uids = array(), $style = 'percent') {
  103. $votes = array();
  104. static $tags;
  105. if (!isset($tags)) {
  106. $tags = explode(' ', devel_create_greeking(30));
  107. }
  108. foreach ($uids as $uid) {
  109. switch ($style) {
  110. case 'percent':
  111. $votes[] = array(
  112. 'uid' => $uid,
  113. 'entity_type' => $etype,
  114. 'entity_id' => $eid,
  115. 'value_type' => 'percent',
  116. 'timestamp' => REQUEST_TIME - rand(0, REQUEST_TIME - $timestamp),
  117. 'value' => mt_rand(1, 5) * 20,
  118. 'tag' => $tags[mt_rand(0, 30)],
  119. );
  120. break;
  121. case 'points':
  122. if (rand(0, 3)) {
  123. $votes[] = array(
  124. 'uid' => $uid,
  125. 'entity_type' => $etype,
  126. 'entity_id' => $eid,
  127. 'value_type' => 'points',
  128. 'timestamp' => REQUEST_TIME - rand(0, REQUEST_TIME - $timestamp),
  129. 'value' => rand(0, 1) ? 1 : -1,
  130. );
  131. }
  132. break;
  133. }
  134. }
  135. votingapi_set_votes($votes, array());
  136. }
  137. /**
  138. * Command callback. Recalculate voting results.
  139. */
  140. function drush_votingapi_recalculate($entity_type = 'node', $entity_id = NULL) {
  141. // Prep some starter query objects.
  142. $data = array();
  143. if (empty($entity_id)) {
  144. $votes = db_select('votingapi_vote', 'vv')
  145. ->fields('vv', array('entity_type', 'entity_id'))
  146. ->condition('entity_type', $entity_type, '=')->distinct(TRUE)
  147. ->execute()->fetchAll(PDO::FETCH_ASSOC);
  148. $message = t('Rebuilt voting results for @type votes.', array('@type' => $entity_type));
  149. }
  150. else {
  151. $votes[] = array('entity_type' => $entity_type, 'entity_id' => $entity_id);
  152. $message = t('Rebuilt all voting results.');
  153. }
  154. foreach ($votes as $vote) {
  155. votingapi_recalculate_results($entity_type, $entity_id, TRUE);
  156. }
  157. drush_log($message, 'success');
  158. }
  159. /**
  160. * Command callback. Flush votes and results.
  161. */
  162. function drush_votingapi_flush($entity_type = NULL, $entity_id = NULL) {
  163. if (drush_confirm(dt("Delete @type voting data?", array('@type' => empty($entity_type) ? dt('all') : $entity_type)))) {
  164. $cache = db_delete('votingapi_cache');
  165. $votes = db_delete('votingapi_vote');
  166. if (!empty($entity_type)) {
  167. $cache->condition('entity_type', $entity_type);
  168. $votes->condition('entity_type', $entity_type);
  169. }
  170. if (!empty($entity_id)) {
  171. $cache->condition('entity_id', $entity_id);
  172. $votes->condition('entity_id', $entity_id);
  173. }
  174. $cache->execute();
  175. $votes->execute();
  176. drush_log(t('Flushed vote data for @type entities.', array('@type' => empty($entity_type) ? t('all') : $entity_type)), 'success');
  177. }
  178. }