votingapi.install 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /**
  3. * @file
  4. * Installation file for VotingAPI module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function votingapi_schema() {
  10. $schema['votingapi_vote'] = array(
  11. 'fields' => array(
  12. 'vote_id' => array(
  13. 'type' => 'serial',
  14. 'unsigned' => TRUE,
  15. 'not null' => TRUE,
  16. ),
  17. 'entity_type' => array(
  18. 'type' => 'varchar',
  19. 'length' => 64,
  20. 'not null' => TRUE,
  21. 'default' => 'node',
  22. ),
  23. 'entity_id' => array(
  24. 'type' => 'int',
  25. 'unsigned' => TRUE,
  26. 'not null' => TRUE,
  27. 'default' => 0,
  28. ),
  29. 'value' => array(
  30. 'type' => 'float',
  31. 'not null' => TRUE,
  32. 'default' => 0,
  33. ),
  34. 'value_type' => array(
  35. 'type' => 'varchar',
  36. 'length' => 64,
  37. 'not null' => TRUE,
  38. 'default' => 'percent',
  39. ),
  40. 'tag' => array(
  41. 'type' => 'varchar',
  42. 'length' => 64,
  43. 'not null' => TRUE,
  44. 'default' => 'vote',
  45. ),
  46. 'uid' => array(
  47. 'type' => 'int',
  48. 'unsigned' => TRUE,
  49. 'not null' => TRUE,
  50. 'default' => 0,
  51. ),
  52. 'timestamp' => array(
  53. 'type' => 'int',
  54. 'unsigned' => TRUE,
  55. 'not null' => TRUE,
  56. 'default' => 0,
  57. ),
  58. 'vote_source' => array('type' => 'varchar', 'length' => 255),
  59. ),
  60. 'primary key' => array('vote_id'),
  61. 'indexes' => array(
  62. 'content_uid' => array('entity_type', 'entity_id', 'uid'),
  63. 'content_uid_2' => array('entity_type', 'uid'),
  64. 'content_source' => array('entity_type', 'entity_id', 'vote_source'),
  65. 'content_value_tag' => array(
  66. 'entity_type',
  67. 'entity_id',
  68. 'value_type',
  69. 'tag',
  70. ),
  71. ),
  72. );
  73. $schema['votingapi_cache'] = array(
  74. 'fields' => array(
  75. 'vote_cache_id' => array(
  76. 'type' => 'serial',
  77. 'unsigned' => TRUE,
  78. 'not null' => TRUE,
  79. ),
  80. 'entity_type' => array(
  81. 'type' => 'varchar',
  82. 'length' => 64,
  83. 'not null' => TRUE,
  84. 'default' => 'node',
  85. ),
  86. 'entity_id' => array(
  87. 'type' => 'int',
  88. 'unsigned' => TRUE,
  89. 'not null' => TRUE,
  90. 'default' => 0,
  91. ),
  92. 'value' => array(
  93. 'type' => 'float',
  94. 'not null' => TRUE,
  95. 'default' => 0,
  96. ),
  97. 'value_type' => array(
  98. 'type' => 'varchar',
  99. 'length' => 64,
  100. 'not null' => TRUE,
  101. 'default' => 'percent',
  102. ),
  103. 'tag' => array(
  104. 'type' => 'varchar',
  105. 'length' => 64,
  106. 'not null' => TRUE,
  107. 'default' => 'vote',
  108. ),
  109. 'function' => array(
  110. 'type' => 'varchar',
  111. 'length' => 64,
  112. 'not null' => TRUE,
  113. 'default' => '',
  114. ),
  115. 'timestamp' => array(
  116. 'type' => 'int',
  117. 'unsigned' => TRUE,
  118. 'not null' => TRUE,
  119. 'default' => 0,
  120. ),
  121. ),
  122. 'primary key' => array('vote_cache_id'),
  123. 'indexes' => array(
  124. 'content' => array('entity_type', 'entity_id'),
  125. 'content_function' => array('entity_type', 'entity_id', 'function'),
  126. 'content_tag_func' => array(
  127. 'entity_type',
  128. 'entity_id',
  129. 'tag',
  130. 'function',
  131. ),
  132. 'content_vtype_tag' => array(
  133. 'entity_type',
  134. 'entity_id',
  135. 'value_type',
  136. 'tag',
  137. ),
  138. 'content_vtype_tag_func' => array(
  139. 'entity_type',
  140. 'entity_id',
  141. 'value_type',
  142. 'tag',
  143. 'function',
  144. ),
  145. ),
  146. );
  147. return $schema;
  148. }
  149. /**
  150. * The most recent update removed for version-to-version compatibility.
  151. */
  152. function votingapi_update_last_removed() {
  153. return 6101;
  154. }
  155. /**
  156. * Fix default values for entity_type columns.
  157. */
  158. function votingapi_update_7203() {
  159. db_change_field('votingapi_vote', 'entity_type', 'entity_type', array(
  160. 'type' => 'varchar',
  161. 'length' => 64,
  162. 'not null' => TRUE,
  163. 'default' => 'node',
  164. ));
  165. db_change_field('votingapi_cache', 'entity_type', 'entity_type', array(
  166. 'type' => 'varchar',
  167. 'length' => 64,
  168. 'not null' => TRUE,
  169. 'default' => 'node',
  170. ));
  171. }
  172. /**
  173. * Queue orphaned votes for deletion (nodes and comments).
  174. */
  175. function votingapi_update_7202() {
  176. $tables[] = 'votingapi_vote';
  177. $tables[] = 'votingapi_cache';
  178. $types[] = array('node', 'node', 'nid');
  179. $types[] = array('comment', 'comment', 'cid');
  180. $queue = DrupalQueue::get('VotingAPIOrphaned', TRUE);
  181. foreach ($types as $w) {
  182. list($type, $type_table, $type_key) = $w;
  183. if (!db_table_exists($type_table)) {
  184. continue;
  185. }
  186. foreach ($tables as $table) {
  187. $sql = "SELECT v.entity_type, v.entity_id FROM {{$table}} v
  188. LEFT OUTER JOIN {{$type_table}} e ON v.entity_id=e.$type_key
  189. WHERE v.entity_type='$type' AND e.$type_key IS NULL
  190. GROUP BY v.entity_type, v.entity_id";
  191. $result = db_query($sql);
  192. foreach ($result as $row) {
  193. $queue->createItem((array) $row);
  194. }
  195. }
  196. }
  197. }
  198. /**
  199. * Rename the 'content_type' and 'content_id' columns.
  200. *
  201. * This update renames the 'content_type' and 'content_id' columns to
  202. * entity_type and entity_id in order to reduce confusion about 'content types'
  203. * versus 'node types'.
  204. */
  205. function votingapi_update_7201() {
  206. $index['votingapi_vote'] = array(
  207. 'content_uid' => array('entity_type', 'entity_id', 'uid'),
  208. 'content_uid_2' => array('entity_type', 'uid'),
  209. 'content_source' => array('entity_type', 'entity_id', 'vote_source'),
  210. 'content_value_tag' => array(
  211. 'entity_type',
  212. 'entity_id',
  213. 'value_type',
  214. 'tag',
  215. ),
  216. );
  217. $index['votingapi_cache'] = array(
  218. 'content' => array('entity_type', 'entity_id'),
  219. 'content_function' => array('entity_type', 'entity_id', 'function'),
  220. 'content_tag_func' => array('entity_type', 'entity_id', 'tag', 'function'),
  221. 'content_vtype_tag' => array(
  222. 'entity_type',
  223. 'entity_id',
  224. 'value_type',
  225. 'tag',
  226. ),
  227. 'content_vtype_tag_func' => array(
  228. 'entity_type',
  229. 'entity_id',
  230. 'value_type',
  231. 'tag',
  232. 'function',
  233. ),
  234. );
  235. // Remove all existing indexes.
  236. foreach ($index as $table => $data) {
  237. foreach ($data as $index_name => $columns) {
  238. if (db_index_exists($table, $index_name)) {
  239. db_drop_index($table, $index_name);
  240. }
  241. }
  242. }
  243. // Change fields.
  244. foreach (array('votingapi_vote', 'votingapi_cache') as $table) {
  245. db_change_field($table, 'content_type', 'entity_type', array(
  246. 'type' => 'varchar',
  247. 'length' => 64,
  248. 'not null' => TRUE,
  249. 'default' => 'node',
  250. ));
  251. db_change_field($table, 'content_id', 'entity_id', array(
  252. 'type' => 'int',
  253. 'unsigned' => TRUE,
  254. 'not null' => TRUE,
  255. 'default' => 0,
  256. ));
  257. db_change_field($table, 'value_type', 'value_type', array(
  258. 'type' => 'varchar',
  259. 'length' => 64,
  260. 'not null' => TRUE,
  261. 'default' => 'percent',
  262. ));
  263. db_change_field($table, 'tag', 'tag', array(
  264. 'type' => 'varchar',
  265. 'length' => 64,
  266. 'not null' => TRUE,
  267. 'default' => 'vote',
  268. ));
  269. }
  270. // Recreate the indexes.
  271. foreach ($index as $table => $data) {
  272. foreach ($data as $index_name => $columns) {
  273. db_add_index($table, $index_name, $columns);
  274. }
  275. }
  276. return t('Updated VotingAPI table structure.');
  277. }
  278. /**
  279. * Update the structure of data storage.
  280. */
  281. function votingapi_update_7205() {
  282. $query = db_select('votingapi_vote', 'v');
  283. $query->fields('v', array('vote_source', 'vote_id'));
  284. $results = $query->execute()->fetchAll();
  285. if (!empty($results)) {
  286. foreach ($results as $result) {
  287. if (!empty($result) && !empty($result->vote_source)) {
  288. $data = hash('sha256', serialize($result->vote_source));
  289. $query = db_update('votingapi_vote');
  290. $query->fields(array('vote_source' => $data));
  291. $query->condition('vote_id', $result->vote_id);
  292. $query->execute();
  293. }
  294. }
  295. }
  296. }
  297. /**
  298. * Implements hook_uninstall().
  299. */
  300. function votingapi_uninstall() {
  301. // Delete variables created by voteapi module.
  302. $variables = array(
  303. 'votingapi_last_cron',
  304. 'votingapi_anonymous_window',
  305. 'votingapi_user_window',
  306. 'votingapi_calculation_schedule',
  307. );
  308. foreach ($variables as $variable) {
  309. variable_del($variable);
  310. }
  311. }