tripal_core.install 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * @file
  4. * Contains functions used to install/uninstall tripal_core.
  5. */
  6. /**
  7. * Implementation of hook_install().
  8. *
  9. * @ingroup tripal_core
  10. */
  11. function tripal_core_install() {
  12. }
  13. /**
  14. * Implementation of hook_schema().
  15. *
  16. * @ingroup tripal_core
  17. */
  18. function tripal_core_schema() {
  19. // get the schemas defined by this install file
  20. $schema = tripal_core_get_schemas();
  21. // if this module is already installed and enabled, then we want to provide
  22. // the schemas for all of the custom tables. This will allow Views to
  23. // see the schemas. We check if the module is installed because during
  24. // installation we don't want to make these custom tables available as we don't
  25. // want them created in the Drupal database. The custom tables go in the
  26. // Chado database.
  27. if (db_table_exists('tripal_custom_tables')) {
  28. $sql = 'SELECT * FROM {tripal_custom_tables}';
  29. $results = db_query($sql);
  30. foreach ($results as $custom) {
  31. $schema[$custom->table_name] = unserialize($custom->schema);
  32. }
  33. }
  34. return $schema;
  35. }
  36. /**
  37. * Implementation of hook_uninstall().
  38. *
  39. * @ingroup tripal_core
  40. */
  41. function tripal_core_uninstall() {
  42. // drop the foreign key between tripal_custom_tables and tripal_mviews
  43. // so that Drupal can then drop the tables
  44. db_query('
  45. ALTER TABLE {tripal_custom_tables}
  46. DROP CONSTRAINT tripal_custom_tables_fk1 CASCADE
  47. ');
  48. }
  49. /**
  50. * This function simply defines all tables needed for the module to work
  51. * correctly. By putting the table definitions in a separate function we
  52. * can easily provide the entire list for hook_install or individual
  53. * tables for an update.
  54. *
  55. * @ingroup tripal_core
  56. */
  57. function tripal_core_get_schemas() {
  58. $schema = array();
  59. // get all the various schema parts and join them together
  60. $temp = tripal_core_get_tripal_token_schema();
  61. foreach ($temp as $table => $arr) {
  62. $schema[$table] = $arr;
  63. }
  64. $temp = tripal_core_get_tripal_toc_schema();
  65. foreach ($temp as $table => $arr) {
  66. $schema[$table] = $arr;
  67. }
  68. $temp = tripal_core_get_tripal_vars_schema();
  69. foreach ($temp as $table => $arr) {
  70. $schema[$table] = $arr;
  71. }
  72. return $schema;
  73. }
  74. /**
  75. *
  76. *
  77. */
  78. function tripal_core_get_tripal_toc_schema() {
  79. $schema = array();
  80. $schema['tripal_toc'] = array(
  81. 'fields' => array(
  82. 'toc_item_id' => array(
  83. 'type' => 'serial',
  84. 'unsigned' => TRUE,
  85. 'not null' => TRUE
  86. ),
  87. 'node_type' => array(
  88. 'type' => 'varchar',
  89. 'length' => 32,
  90. 'not null' => TRUE
  91. ),
  92. 'key' => array(
  93. 'type' => 'varchar',
  94. 'length' => 255,
  95. 'not null' => TRUE,
  96. ),
  97. 'title' => array(
  98. 'type' => 'varchar',
  99. 'length' => 255,
  100. 'not null' => FALSE
  101. ),
  102. 'weight' => array(
  103. 'type' => 'int',
  104. 'not null' => FALSE
  105. ),
  106. 'hide' => array(
  107. 'type' => 'int',
  108. 'size' => 'tiny',
  109. 'not null' => FALSE,
  110. 'default' => 0,
  111. ),
  112. 'nid' => array(
  113. 'type' => 'int',
  114. 'not null' => FALSE,
  115. )
  116. ),
  117. 'indexes' => array(
  118. 'tripal_toc_idx1' => array('node_type', 'key'),
  119. 'tripal_toc_idx2' => array('node_type', 'key', 'nid'),
  120. ),
  121. 'unique keys' => array(
  122. 'tripal_toc_uq1' => array('node_type', 'key', 'nid'),
  123. ),
  124. 'primary key' => array('toc_item_id'),
  125. );
  126. return $schema;
  127. }
  128. /**
  129. *
  130. */
  131. function tripal_core_get_tripal_vars_schema() {
  132. $schema = array();
  133. $schema['tripal_node_variables'] = array(
  134. 'description' => 'This table is used for storing any type of variable such as ' .
  135. 'a property or setting that should be associated with a Tripal managed Drupal node. This table is '.
  136. 'meant to store non-biological information only. All biological data should be housed ' .
  137. 'in the Chado tables. Be aware that any data stored here will not be made visible ' .
  138. 'through services such as Tripal Web Services and therefore can be a good place to ' .
  139. 'hide application specific settings.',
  140. 'fields' => array (
  141. 'node_variable_id' => array (
  142. 'type' => 'serial',
  143. 'not null' => TRUE,
  144. ),
  145. 'nid' => array (
  146. 'type' => 'int',
  147. 'not null' => TRUE,
  148. ),
  149. 'variable_id' => array (
  150. 'type' => 'int',
  151. 'not null' => TRUE,
  152. ),
  153. 'value' => array (
  154. 'type' => 'text',
  155. 'not null' => FALSE,
  156. ),
  157. 'rank' => array (
  158. 'type' => 'int',
  159. 'not null' => TRUE,
  160. 'default' => 0,
  161. ),
  162. ),
  163. 'primary key' => array (
  164. 0 => 'node_variable_id',
  165. ),
  166. 'unique keys' => array (
  167. 'tripal_node_variables_c1' => array (
  168. 0 => 'nid',
  169. 1 => 'variable_id',
  170. 2 => 'rank',
  171. ),
  172. ),
  173. 'indexes' => array (
  174. 'tripal_node_variables_idx1' => array (
  175. 0 => 'variable_id',
  176. ),
  177. ),
  178. 'foreign keys' => array (
  179. 'tripal_variables' => array (
  180. 'table' => 'tripal_variables',
  181. 'columns' => array (
  182. 'variable_id' => 'variable_id',
  183. ),
  184. ),
  185. ),
  186. );
  187. return $schema;
  188. }
  189. /**
  190. * Adds an mview_id column to the tripal_custom_tables table and makes an assocation between the mview and the custom table
  191. *
  192. */
  193. function tripal_core_update_7200() {
  194. try {
  195. // add an mview column to the tripal_custom_tables table so we
  196. // can associate which of the custom tables are also mviews
  197. if (!db_field_exists('tripal_custom_tables', 'mview_id')) {
  198. $spec = array(
  199. 'type' => 'int',
  200. 'not NULL' => FALSE
  201. );
  202. $keys = array(
  203. 'foreign keys' => array(
  204. 'tripal_mviews' => array(
  205. 'table' => 'tripal_mviews',
  206. 'columns' => array(
  207. 'mview_id' => 'mview_id'
  208. ),
  209. ),
  210. ),
  211. );
  212. db_add_field('tripal_custom_tables', 'mview_id', $spec, $keys);
  213. // the foreign key specification doesn't really add one to the
  214. // Drupal schema, it is just used internally, but we want one
  215. db_query('
  216. ALTER TABLE {tripal_custom_tables}
  217. ADD CONSTRAINT tripal_custom_tables_fk1
  218. FOREIGN KEY (mview_id) REFERENCES {tripal_mviews} (mview_id)
  219. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  220. ');
  221. }
  222. // now link the materialized view to it's custom table entry
  223. $mviews = db_select('tripal_mviews', 'tmv')
  224. ->fields('tmv', array('mview_id', 'mv_table'))
  225. ->execute();
  226. foreach ($mviews as $mview) {
  227. db_update('tripal_custom_tables')
  228. ->fields(array(
  229. 'mview_id' => $mview->mview_id
  230. ))
  231. ->condition('table_name', $mview->mv_table)
  232. ->execute();
  233. }
  234. }
  235. catch (\PDOException $e) {
  236. $error = $e->getMessage();
  237. throw new DrupalUpdateException('Could not update tripal_mviews table and link to custom tables: '. $error);
  238. }
  239. }
  240. /**
  241. * Fixes missing language for nodes and URL aliases. This may take awhile...
  242. *
  243. */
  244. function tripal_core_update_7201() {
  245. try {
  246. $sql = "UPDATE {node} SET language = :language WHERE language = ''";
  247. db_query($sql, array(':language' => LANGUAGE_NONE));
  248. $sql = "UPDATE {url_alias} SET language = :language WHERE language = ''";
  249. db_query($sql, array(':language' => LANGUAGE_NONE));
  250. }
  251. catch (\PDOException $e) {
  252. $error = $e->getMessage();
  253. throw new DrupalUpdateException('Could not reset language for nodes and url aliases: '. $error);
  254. }
  255. }
  256. /**
  257. * Adds a tripal_token_formats table for custom page titles and URL paths
  258. */
  259. function tripal_core_update_7202() {
  260. try {
  261. $schema = drupal_get_schema_unprocessed('tripal_core', 'tripal_token_formats');
  262. db_create_table('tripal_token_formats', $schema);
  263. }
  264. catch (Exception $e) {
  265. $error = $e->getMessage();
  266. throw new DrupalUpdateException('Could not add tripal_token_formats table: '. $error);
  267. }
  268. }
  269. /**
  270. * Adds a tripal_toc table for customizing the table of contents on each Tripal page.
  271. */
  272. function tripal_core_update_7203() {
  273. try {
  274. $schema = drupal_get_schema_unprocessed('tripal_core', 'tripal_toc');
  275. db_create_table('tripal_toc', $schema);
  276. }
  277. catch (Exception $e) {
  278. $error = $e->getMessage();
  279. throw new DrupalUpdateException('Could not add tripal_toc table: '. $error);
  280. }
  281. }
  282. /**
  283. * Adds the tripal_variable_terms and a tripal_node_variables tables
  284. */
  285. function tripal_core_update_7204() {
  286. try {
  287. $schema = drupal_get_schema_unprocessed('tripal_core', 'tripal_variables');
  288. db_create_table('tripal_variables', $schema);
  289. $schema = drupal_get_schema_unprocessed('tripal_core', 'tripal_node_variables');
  290. db_create_table('tripal_node_variables', $schema);
  291. }
  292. catch (Exception $e) {
  293. $error = $e->getMessage();
  294. throw new DrupalUpdateException('Could not add new tables table: '. $error);
  295. }
  296. }