nodequeue.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the nodequeue module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function nodequeue_schema() {
  10. $schema['nodequeue_queue'] = array(
  11. 'description' => 'Base table for queues, storing global information for each queue',
  12. 'fields' => array(
  13. 'qid' => array(
  14. 'description' => 'The primary identifier for a queue.',
  15. 'type' => 'serial',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE
  18. ),
  19. 'name' => array(
  20. 'description' => 'The machine name for the queue.',
  21. 'type' => 'varchar',
  22. 'length' => 128,
  23. ),
  24. 'title' => array(
  25. 'description' => 'Title of a queue.',
  26. 'type' => 'varchar',
  27. 'length' => 255,
  28. 'not null' => TRUE,
  29. ),
  30. 'subqueue_title' => array(
  31. 'description' => 'Title of a subqueue.',
  32. 'type' => 'varchar',
  33. 'length' => 255,
  34. 'not null' => TRUE,
  35. ),
  36. 'size' => array(
  37. 'description' => 'How many nodes this queue will hold',
  38. 'type' => 'int',
  39. 'default' => 0,
  40. ),
  41. 'link' => array(
  42. 'description' => 'The link text to show under a node to add it to the queue.',
  43. 'type' => 'varchar',
  44. 'length' => 40,
  45. ),
  46. 'link_remove' => array(
  47. 'description' => 'The link text to show under a node to remove it from the queue.',
  48. 'type' => 'varchar',
  49. 'length' => 40,
  50. ),
  51. 'owner' => array(
  52. 'description' => '',
  53. 'type' => 'varchar',
  54. 'length' => 255,
  55. ),
  56. 'show_in_ui' => array(
  57. 'description' => '',
  58. 'type' => 'int',
  59. 'size' => 'tiny',
  60. 'default' => 1,
  61. ),
  62. 'show_in_tab' => array(
  63. 'description' => '',
  64. 'type' => 'int',
  65. 'size' => 'tiny',
  66. 'default' => 1,
  67. ),
  68. 'show_in_links' => array(
  69. 'description' => '',
  70. 'type' => 'int',
  71. 'size' => 'tiny',
  72. 'default' => 1,
  73. ),
  74. 'reference' => array(
  75. 'description' => '',
  76. 'type' => 'varchar',
  77. 'length' => 255,
  78. 'default' => '0',
  79. ),
  80. 'reverse' => array(
  81. 'description' => '',
  82. 'type' => 'int',
  83. 'size' => 'tiny',
  84. 'default' => 0,
  85. ),
  86. 'i18n' => array(
  87. 'description' => '',
  88. 'type' => 'int',
  89. 'size' => 'tiny',
  90. 'default' => 1,
  91. ),
  92. ), // fields
  93. 'primary key' => array('qid'),
  94. 'unique keys' => array(
  95. 'name' => array('name'),
  96. ),
  97. ); // nodequeue_queue.
  98. $schema['nodequeue_roles'] = array(
  99. 'description' => 'Defines the roles which are allowed to use a particular nodequeue.',
  100. 'fields' => array(
  101. 'qid' => array(
  102. 'description' => 'Primary key for {nodequeue_queue}',
  103. 'type' => 'int',
  104. 'size' => 'big',
  105. 'unsigned' => TRUE,
  106. 'not null' => TRUE
  107. ),
  108. 'rid' => array(
  109. 'description' => 'Primary key for roles',
  110. 'type' => 'int',
  111. 'size' => 'big',
  112. 'unsigned' => TRUE,
  113. 'not null' => TRUE
  114. ),
  115. ), // fields
  116. 'indexes' => array(
  117. 'qid' => array('qid'),
  118. 'rid' => array('rid'),
  119. ), // indexes
  120. ); // nodequeue_roles
  121. $schema['nodequeue_types'] = array(
  122. 'description' => 'Defines the node types which are allowed in each queue',
  123. 'fields' => array(
  124. 'qid' => array(
  125. 'description' => 'Primary key for {nodequeue_queue}',
  126. 'type' => 'int',
  127. 'size' => 'big',
  128. 'unsigned' => TRUE,
  129. 'not null' => TRUE
  130. ),
  131. 'type' => array(
  132. 'description' => 'Node Type',
  133. 'type' => 'varchar',
  134. 'length' => 255,
  135. 'not null' => FALSE
  136. ),
  137. ), // fields
  138. 'indexes' => array(
  139. 'qid' => array('qid'),
  140. 'type' => array('type'),
  141. ), // indexes
  142. ); // nodequeue_types
  143. // Subqueues are minor queues that inherit all of the properties of
  144. // the parent queue. A parent queue must have at least 1 subqueue
  145. // to do anything. Reference is for the use of whatever is creating
  146. // the subqueues in order to link it to some other ID easily.
  147. $schema['nodequeue_subqueue'] = array(
  148. 'description' => 'Subqueues are minor queues that inherit all of the properties of the parent queue. A parent queue must have at least 1 subqueue to do anything. Reference is for the use of whatever is creating the subqueues in order to link it to some other ID easily.',
  149. 'fields' => array(
  150. 'sqid' => array(
  151. 'description' => 'Subqueue identifier',
  152. 'type' => 'serial',
  153. 'unsigned' => TRUE,
  154. 'not null' => TRUE,
  155. ),
  156. 'qid' => array(
  157. 'description' => 'Queue identifier.',
  158. 'type' => 'int',
  159. 'unsigned' => TRUE,
  160. 'not null' => TRUE,
  161. ),
  162. 'reference' => array(
  163. 'description' => '',
  164. 'type' => 'varchar',
  165. 'length' => 255,
  166. 'default' => '0',
  167. 'not null' => FALSE
  168. ),
  169. 'title' => array(
  170. 'description' => '',
  171. 'type' => 'varchar',
  172. 'length' => 255,
  173. 'default' => '',
  174. 'not null' => FALSE
  175. ),
  176. ), // fields
  177. 'primary key' => array('sqid'),
  178. 'indexes' => array(
  179. 'qid' => array('qid'),
  180. 'reference' => array('reference'),
  181. 'title' => array('title'),
  182. ), // indexes
  183. ); // nodequeue_subqueue
  184. $schema['nodequeue_nodes'] = array(
  185. 'description' => 'Indicates which nodes are in which queues/subqueues.',
  186. 'fields' => array(
  187. 'qid' => array(
  188. 'description' => 'Queue id',
  189. 'type' => 'int',
  190. 'unsigned' => TRUE,
  191. 'not null' => TRUE,
  192. ),
  193. 'sqid' => array(
  194. 'description' => 'Subqueue this node is in',
  195. 'type' => 'int',
  196. 'unsigned' => TRUE,
  197. 'not null' => TRUE,
  198. ),
  199. 'nid' => array(
  200. 'description' => 'Node id in this subqueue',
  201. 'type' => 'int',
  202. 'unsigned' => TRUE,
  203. 'not null' => FALSE
  204. ),
  205. 'position' => array(
  206. 'description' => 'The position of the node in this subqueue.',
  207. 'type' => 'int',
  208. 'unsigned' => TRUE,
  209. 'not null' => FALSE
  210. ),
  211. 'timestamp' => array(
  212. 'description' => "The time that the item's position in the subqueue was updated.",
  213. 'type' => 'int',
  214. 'unsigned' => TRUE,
  215. 'not null' => TRUE,
  216. 'default' => 0,
  217. ),
  218. ), // fields
  219. 'indexes' => array(
  220. 'sqid' => array('sqid', 'position'),
  221. 'nid' => array('nid'),
  222. 'qid_nid' => array('qid', 'nid'),
  223. ), // indexes
  224. ); // nodequeue_nodes
  225. return $schema;
  226. }
  227. /**
  228. * Implements hook_uninstall().
  229. */
  230. function nodequeue_uninstall() {
  231. // Remove our variables.
  232. $variables = array(
  233. 'nodequeue_use_tab',
  234. 'nodequeue_tab_display_max',
  235. 'nodequeue_tab_name',
  236. 'nodequeue_autocomplete_limit',
  237. 'nodequeue_view_per_queue',
  238. );
  239. foreach ($variables as $variable) {
  240. variable_del($variable);
  241. }
  242. }
  243. /**
  244. * There was a discrepancy between the link/link_remove fields created with
  245. * node_install/node_schema, and the ones created with nodequeue_update_5000.
  246. * This forces everyone to 40 characters.
  247. */
  248. function nodequeue_update_6000() {
  249. $ret = array();
  250. db_change_field('nodequeue_queue', 'link', 'link', array('type' => 'varchar', 'length' => 40));
  251. db_change_field('nodequeue_queue', 'link_remove', 'link_remove', array('type' => 'varchar', 'length' => 40));
  252. return $ret;
  253. }
  254. /**
  255. * @todo Please insert a Doxygen style comment for this hook_update_N.
  256. */
  257. function nodequeue_update_6001() {
  258. $ret = array();
  259. db_add_field('nodequeue_queue', 'i18n', array('description' => '', 'type' => 'int', 'size' => 'tiny', 'default' => 1));
  260. return $ret;
  261. }
  262. // The previous 6002 update has been moved to 5205.
  263. /**
  264. * Remove invalid entries from the nodequeue_nodes table created as a result of
  265. * bugs like http://drupal.org/node/593858.
  266. */
  267. function nodequeue_update_6003() {
  268. $ret = array();
  269. $invalid = db_query("SELECT count(nid) FROM {nodequeue_nodes} WHERE nid = 0")->fetchField();
  270. if (!empty($invalid)) {
  271. db_delete('nodequeue_nodes')
  272. ->condition('nid', 0)
  273. ->execute();
  274. $t = get_t();
  275. $ret[] = array('success' => TRUE, 'query' => $t("Deleted @invalid invalid entries from the {nodequeue_nodes} table.", array('@invalid' => $invalid)));
  276. }
  277. else {
  278. $ret[] = array('success' => TRUE, 'query' => "No invalid entries found in the {nodequeue_nodes} table.");
  279. }
  280. return $ret;
  281. }
  282. /**
  283. * Add a new index to {nodequeue_nodes}
  284. */
  285. function nodequeue_update_6004() {
  286. $ret = array();
  287. db_add_index('nodequeue_nodes', '{nodequeue_nodes}_qid_nid_idx', array('qid', 'nid'));
  288. return $ret;
  289. }
  290. /**
  291. * Rename the 'nodequeue_automatic_views_generation'
  292. * to 'nodequeue_view_per_queue'.
  293. */
  294. function nodequeue_update_6005() {
  295. $ret = array();
  296. $ret[] = update_sql("UPDATE {system} set name = 'nodequeue_view_per_queue' where name = 'nodequeue_automatic_views_generation'");
  297. return $ret;
  298. }
  299. /**
  300. * Renaming indices so that they're consistent with what Schema module would
  301. * expect.
  302. */
  303. function nodequeue_update_7200() {
  304. // Check if the indices weren't already renamed by a 6.x-2.x-dev version
  305. // prior to 6.x-2.10.
  306. if (!db_index_exists('nodequeue_nodes', 'sqid')) {
  307. // Delete existing indexes
  308. $del_indices = array(
  309. 'nodequeue_roles' => array('qid', 'rid'),
  310. 'nodequeue_types' => array('qid', 'type'),
  311. 'nodequeue_subqueue' => array('qid', 'reference', 'title'),
  312. 'nodequeue_nodes' => array('sqid', 'qid_nid'),
  313. );
  314. foreach ($del_indices as $table => $indices) {
  315. foreach ($indices as $k => $index) {
  316. db_drop_index($table, $table . "_" . $index . "_idx");
  317. }
  318. }
  319. // Naming convention incorrect for this one
  320. db_drop_index("nodequeue_nodes", "nodequeue_subqueue_nid_idx");
  321. // Create new indexes
  322. $add_indexes = array(
  323. 'nodequeue_roles' => array(
  324. 'qid' => array('qid'),
  325. 'rid' => array('rid'),
  326. ),
  327. 'nodequeue_types' => array(
  328. 'qid' => array('qid'),
  329. 'type' => array('type'),
  330. ),
  331. 'nodequeue_subqueue' => array(
  332. 'qid' => array('qid'),
  333. 'reference' => array('reference'),
  334. 'title' => array('title'),
  335. ),
  336. 'nodequeue_nodes' => array(
  337. 'sqid' => array('sqid', 'position'),
  338. 'qid_nid' => array('qid', 'nid'),
  339. 'nid' => array('nid'),
  340. ),
  341. );
  342. foreach ($add_indexes as $table => $indices) {
  343. foreach ($indices as $name => $columns) {
  344. db_add_index($table, $name, $columns);
  345. }
  346. }
  347. }
  348. }
  349. /**
  350. * Provide machine names and auto-generation of machine names for existing
  351. * queues.
  352. */
  353. function nodequeue_update_7201() {
  354. // Check that the name field hasn't been created by a 6.x-2.x-dev version
  355. // prior to 6.x-2.10.
  356. if (!db_field_exists('nodequeue_queue', 'name')) {
  357. $name_field = array(
  358. 'description' => 'The machine name for the queue.',
  359. 'type' => 'varchar',
  360. 'length' => 128,
  361. );
  362. db_add_field('nodequeue_queue', 'name', $name_field);
  363. db_add_unique_key('nodequeue_queue', 'name', array('name'));
  364. // Auto-generate machine names for existing queues and subqueues. Existing
  365. // queues will be given the first 128 characters of the title, with all
  366. // spaces replaced with underline characters, made lowercase and trimmed
  367. // of excess whitespace. Nodequeues with empty titles will receive a name
  368. // like 'queue_$qid'.
  369. $result = db_query("SELECT qid, title FROM {nodequeue_queue}");
  370. foreach ($result as $queue) {
  371. if (!empty($queue->title)) {
  372. // Basic formatting.
  373. $new_name = strtolower(trim(substr($queue->title, 0, 128)));
  374. // Regex to strip out all unwanted characters.
  375. $new_name = preg_replace('/[^a-z0-9_]+/', '_', $new_name);
  376. // Check if this queue name already exists.
  377. if (nodequeue_machine_name_exists($new_name)) {
  378. $tmp_name = $new_name;
  379. $i = 0;
  380. do {
  381. // Append an incrementing numeric suffix until we find a unique name.
  382. $unique_suffix = '_' . $i;
  383. $new_name = truncate_utf8($tmp_name, 128 - drupal_strlen($unique_suffix, TRUE)) . $unique_suffix;
  384. $i++;
  385. } while (nodequeue_machine_name_exists($new_name));
  386. }
  387. }
  388. else {
  389. // Default to a name like 'queue_$qid' for queues that don't have a title.
  390. $new_name = 'queue_' . $queue->qid;
  391. }
  392. db_update('nodequeue_queue')
  393. ->fields(array('name' => $new_name))
  394. ->condition('qid', $queue->qid)
  395. ->execute();
  396. }
  397. }
  398. }