Projet

Général

Profil

Paste
Télécharger (6,18 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / votingapi / votingapi.drush.inc @ 503b3f7b

1
<?php
2

    
3
/**
4
 * @file
5
 *  Generate votingapi votes, recalculate results for existing votes, or flush
6
 *  VotingAPI data entirely.
7
 */
8

    
9
/**
10
 * Implements of hook_drush_help().
11
 */
12
function votingapi_drush_help($section) {
13
  switch ($section) {
14
    case 'drush:generate-votes':
15
      return dt('Usage: drush generate-votes <entity_type> <vote_type>.');
16
    case 'drush:votingapi-recalculate':
17
      return dt('Usage: drush votingapi-recalculate <entity_type>.');
18
    case 'drush:votingapi-flush':
19
      return dt('Usage: drush votingapi-flush <entity_type>.');
20
  }
21
}
22

    
23
/**
24
 * Implements of hook_drush_command().
25
 */
26
function votingapi_drush_command() {
27
  $items['generate-votes'] = array(
28
    'description' => 'Creates dummy voting data.',
29
    'arguments' => array(
30
      'entity_type' => 'The type of entity to generate votes for.',
31
      'vote_type' => 'The type of votes to generate, defaults to \'percent\'.',
32
    ),
33
    'drupal dependencies' => array('devel_generate'),
34
    'options' => array(
35
      'kill' => 'Specify \'kill\' to delete all existing votes before generating new ones.',
36
      'age' => 'The maximum age, in seconds, of each vote.',
37
      'node_types' => 'A comma delimited list of node types to generate votes for, if the entity type is \'node\'.',
38
    ),
39
    'aliases' => array('genv'),
40
  );
41
  $items['votingapi-recalculate'] = array(
42
    'description' => 'Regenerates voting results from raw vote data.',
43
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, // Various D7 code assumes we have a uid.
44
    'arguments' => array(
45
      'entity_type' => 'The type of entity to recalculate vote results for.',
46
    ),
47
    'aliases' => array('vcalc'),
48
  );
49
  $items['votingapi-flush'] = array(
50
    'description' => 'Deletes all existing voting data.',
51
    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, // Various D7 code assumes we have a uid.
52
    'arguments' => array(
53
      'entity_type' => 'The type of entity whose voting data should be flushed.',
54
    ),
55
    'aliases' => array('vflush'),
56
  );
57
  return $items;
58
}
59

    
60

    
61
/**
62
 * Command callback. Generate a number of votes.
63
 */
64
function drush_votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent') {
65
  $options = array(
66
    'kill' => drush_get_option('kill'),
67
    'age' => drush_get_option('age'),
68
    'types' => drush_get_option('types'),
69
  );
70

    
71
  votingapi_generate_votes($entity_type, $vote_type, $options);
72

    
73
  drush_log(t('Generating @vtype votes for @etype entities.', array('@vtype' => $vote_type, '@etype' => $entity_type)), 'success');
74
}
75

    
76
/**
77
 * Utility function to generate votes.
78
 */
79
function votingapi_generate_votes($entity_type = 'node', $vote_type = 'percent', $options = array()) {
80
  module_load_include('inc', 'devel_generate');
81
  $options += array(
82
    'age' => 36000,
83
    'types' => array(),
84
    'kill' => FALSE,
85
  );
86

    
87
  if (!empty($options['kill_votes'])) {
88
    db_truncate('votingapi_vote');
89
    db_truncate('votingapi_cache');
90
  }
91

    
92
  if (($schema = drupal_get_schema($entity_type)) && ($entity_id_column = array_shift($schema['primary key']))) {
93
    // oh look we found a schema yay
94
  }
95
  else {
96
    $entity_type = 'node';
97
    $entity_id_column = 'nid';
98
  }
99
  $uids = devel_get_users();
100

    
101
  $query = db_select($entity_type, 'e')->fields('e', array($entity_id_column));
102
  if ($entity_type == 'node' && !empty($options['types'])) {
103
    $query->condition('e.type', $options['types'], 'IN');
104
  }
105
  $results = $query->execute()->fetchAll(PDO::FETCH_ASSOC);
106

    
107
  foreach ($results as $entity) {
108
    _votingapi_cast_votes($entity_type, $entity[$entity_id_column], $options['age'], $uids, $vote_type);
109
  }
110
}
111

    
112
/**
113
 * Utility function to generate votes on a node by a set of users.
114
 */
115
function _votingapi_cast_votes($etype, $eid, $timestamp = 0, $uids = array(), $style = 'percent') {
116
  $votes = array();
117
  static $tags;
118
  if (!isset($tags)) {
119
    $tags = explode(' ', devel_create_greeking(30));
120
  }
121
  foreach ($uids as $uid) {
122
    switch ($style) {
123
      case 'percent':
124
        $votes[] = array(
125
          'uid' => $uid,
126
          'entity_type' => $etype,
127
          'entity_id' => $eid,
128
          'value_type' => 'percent',
129
          'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp),
130
          'value' => mt_rand(1, 5) * 20,
131
          'tag' => $tags[mt_rand(0, 30)],
132
        );
133
        break;
134
      case 'points':
135
        if (rand(0, 3)) {
136
          $votes[] = array(
137
            'uid' => $uid,
138
            'entity_type' => $etype,
139
            'entity_id' => $eid,
140
            'value_type' => 'points',
141
            'timestamp' => REQUEST_TIME - mt_rand(0, $timestamp),
142
            'value' => rand(0, 1) ? 1 : -1,
143
          );
144
        }
145
        break;
146
    }
147
  }
148
  votingapi_set_votes($votes, array());
149
}
150

    
151
/**
152
 * Command callback. Recalculate voting results.
153
 */
154
function drush_votingapi_recalculate($entity_type = 'node', $entity_id = NULL) {
155
  // Prep some starter query objects.
156
  $data = array();
157
  if (empty($entity_id)) {
158
    $votes = db_select('votingapi_vote', 'vv')
159
      ->fields('vv', array('entity_type', 'entity_id'))
160
      ->condition('entity_type', $entity_type, '=')->distinct(TRUE)
161
      ->execute()->fetchAll(PDO::FETCH_ASSOC);
162
      $message = t('Rebuilt voting results for @type votes.', array('@type' => $entity_type));
163
  }
164
  else {
165
    $votes[] = array('entity_type' => $entity_type, 'entity_id' => $entity_id);
166
    $message = t('Rebuilt all voting results.');
167
  }
168

    
169
  foreach ($votes as $vote) {
170
    votingapi_recalculate_results($vote['entity_type'], $vote['entity_id'], TRUE);
171
  }
172

    
173
  drush_log($message, 'success');
174
}
175

    
176

    
177
/**
178
 * Command callback. Flush votes and results.
179
 */
180
function drush_votingapi_flush($entity_type = NULL, $entity_id = NULL) {
181
  if (drush_confirm(dt("Delete @type voting data?", array('@type' => empty($entity_type) ? dt('all') : $entity_type)))) {
182
    $cache = db_delete('votingapi_cache');
183
    $votes = db_delete('votingapi_vote');
184

    
185
    if (!empty($entity_type)) {
186
      $cache->condition('entity_type', $entity_type);
187
      $votes->condition('entity_type', $entity_type);
188
    }
189
    if (!empty($entity_id)) {
190
      $cache->condition('entity_id', $entity_id);
191
      $votes->condition('entity_id', $entity_id);
192
    }
193

    
194
    $cache->execute();
195
    $votes->execute();
196

    
197
    drush_log(t('Flushed vote data for @type entities.', array('@type' => empty($entity_type) ? t('all') : $entity_type)), 'success');
198
  }
199
}