tripal_core.install 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. // The foreign key specification doesn't really add one to the
  13. // Drupal schema, it is just used internally, but we want one.
  14. db_query('
  15. ALTER TABLE {tripal_custom_tables}
  16. ADD CONSTRAINT tripal_custom_tables_fk1
  17. FOREIGN KEY (mview_id) REFERENCES {tripal_mviews} (mview_id)
  18. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  19. ');
  20. // Warn users that they need to install chado before installing anything else.
  21. drupal_set_message(t(
  22. 'Tripal requires the <a href="!link">installation fo the Chado Schema</a> before any other modules can be installed.',
  23. array('!link' => url('admin/tripal/storage/chado/chado_install'))),
  24. 'warning');
  25. }
  26. /**
  27. * Implementation of hook_schema().
  28. *
  29. * @ingroup tripal_core
  30. */
  31. function tripal_core_schema() {
  32. // get the schemas defined by this install file
  33. $schema = tripal_core_get_schemas();
  34. // if this module is already installed and enabled, then we want to provide
  35. // the schemas for all of the custom tables. This will allow Views to
  36. // see the schemas. We check if the module is installed because during
  37. // installation we don't want to make these custom tables available as we don't
  38. // want them created in the Drupal database. The custom tables go in the
  39. // Chado database.
  40. if (db_table_exists('tripal_custom_tables')) {
  41. $sql = 'SELECT * FROM {tripal_custom_tables}';
  42. $results = db_query($sql);
  43. foreach ($results as $custom) {
  44. $schema[$custom->table_name] = unserialize($custom->schema);
  45. }
  46. }
  47. return $schema;
  48. }
  49. /**
  50. * Implementation of hook_uninstall().
  51. *
  52. * @ingroup tripal_core
  53. */
  54. function tripal_core_uninstall() {
  55. // drop the foreign key between tripal_custom_tables and tripal_mviews
  56. // so that Drupal can then drop the tables
  57. db_query('
  58. ALTER TABLE {tripal_custom_tables}
  59. DROP CONSTRAINT tripal_custom_tables_fk1 CASCADE
  60. ');
  61. }
  62. /**
  63. * This function simply defines all tables needed for the module to work
  64. * correctly. By putting the table definitions in a separate function we
  65. * can easily provide the entire list for hook_install or individual
  66. * tables for an update.
  67. *
  68. * @ingroup tripal_core
  69. */
  70. function tripal_core_get_schemas() {
  71. $schema = array();
  72. // get all the various schema parts and join them together
  73. $temp = tripal_core_get_jobs_schema();
  74. foreach ($temp as $table => $arr) {
  75. $schema[$table] = $arr;
  76. }
  77. $temp = tripal_core_get_mviews_schema();
  78. foreach ($temp as $table => $arr) {
  79. $schema[$table] = $arr;
  80. }
  81. $temp = tripal_core_get_custom_tables_schema();
  82. foreach ($temp as $table => $arr) {
  83. $schema[$table] = $arr;
  84. }
  85. $temp = tripal_core_get_tripal_token_schema();
  86. foreach ($temp as $table => $arr) {
  87. $schema[$table] = $arr;
  88. }
  89. $temp = tripal_core_get_tripal_toc_schema();
  90. foreach ($temp as $table => $arr) {
  91. $schema[$table] = $arr;
  92. }
  93. $temp = tripal_core_get_tripal_vars_schema();
  94. foreach ($temp as $table => $arr) {
  95. $schema[$table] = $arr;
  96. }
  97. return $schema;
  98. }
  99. /**
  100. * Describes the Tripal Materialized View (tripal_mviews) table
  101. * This table keeps track of all materialized views created by Tripal and stored in chado
  102. *
  103. * @ingroup tripal_core
  104. */
  105. function tripal_core_get_mviews_schema() {
  106. $schema = array();
  107. $schema['tripal_mviews'] = array(
  108. 'fields' => array(
  109. 'mview_id' => array(
  110. 'type' => 'serial',
  111. 'unsigned' => TRUE,
  112. 'not NULL' => TRUE
  113. ),
  114. 'name' => array(
  115. 'type' => 'varchar',
  116. 'length' => 255,
  117. 'not NULL' => TRUE
  118. ),
  119. 'modulename' => array(
  120. 'type' => 'varchar',
  121. 'length' => 50,
  122. 'not NULL' => TRUE,
  123. 'description' => 'The module name that provides the callback for this job'
  124. ),
  125. 'mv_table' => array(
  126. 'type' => 'varchar',
  127. 'length' => 128,
  128. 'not NULL' => FALSE
  129. ),
  130. 'mv_specs' => array(
  131. 'type' => 'text',
  132. 'size' => 'normal',
  133. 'not NULL' => FALSE
  134. ),
  135. 'mv_schema' => array(
  136. 'type' => 'text',
  137. 'size' => 'normal',
  138. 'not NULL' => FALSE
  139. ),
  140. 'indexed' => array(
  141. 'type' => 'text',
  142. 'size' => 'normal',
  143. 'not NULL' => FALSE
  144. ),
  145. 'query' => array(
  146. 'type' => 'text',
  147. 'size' => 'normal',
  148. 'not NULL' => TRUE
  149. ),
  150. 'special_index' => array(
  151. 'type' => 'text',
  152. 'size' => 'normal',
  153. 'not NULL' => FALSE
  154. ),
  155. 'last_update' => array(
  156. 'type' => 'int',
  157. 'not NULL' => FALSE,
  158. 'description' => 'UNIX integer time'
  159. ),
  160. 'status' => array(
  161. 'type' => 'text',
  162. 'size' => 'normal',
  163. 'not NULL' => FALSE
  164. ),
  165. 'comment' => array(
  166. 'type' => 'text',
  167. 'size' => 'normal',
  168. 'not NULL' => FALSE
  169. ),
  170. ),
  171. 'indexes' => array(
  172. 'mview_id' => array('mview_id')
  173. ),
  174. 'unique keys' => array(
  175. 'mv_table' => array('mv_table'),
  176. 'mv_name' => array('name'),
  177. ),
  178. 'primary key' => array('mview_id'),
  179. );
  180. return $schema;
  181. }
  182. /**
  183. * Describes the Tripal Jobs (tripal_jobs) table
  184. * This table keeps track of all tripal jobs including their current status and is used
  185. * by tripal_launch_jobs to determine which jobs need to be run
  186. *
  187. * @ingroup tripal_core
  188. */
  189. function tripal_core_get_jobs_schema() {
  190. $schema = array();
  191. $schema['tripal_jobs'] = array(
  192. 'fields' => array(
  193. 'job_id' => array(
  194. 'type' => 'serial',
  195. 'unsigned' => TRUE,
  196. 'not NULL' => TRUE
  197. ),
  198. 'uid' => array(
  199. 'type' => 'int',
  200. 'unsigned' => TRUE,
  201. 'not NULL' => TRUE,
  202. 'description' => 'The Drupal userid of the submitee'
  203. ),
  204. 'job_name' => array(
  205. 'type' => 'varchar',
  206. 'length' => 255,
  207. 'not NULL' => TRUE
  208. ),
  209. 'modulename' => array(
  210. 'type' => 'varchar',
  211. 'length' => 50,
  212. 'not NULL' => TRUE,
  213. 'description' => 'The module name that provides the callback for this job'
  214. ),
  215. 'callback' => array(
  216. 'type' => 'varchar',
  217. 'length' => 255,
  218. 'not NULL' => TRUE
  219. ),
  220. 'arguments' => array(
  221. 'type' => 'text',
  222. 'size' => 'normal',
  223. 'not NULL' => FALSE
  224. ),
  225. 'progress' => array(
  226. 'type' => 'int',
  227. 'unsigned' => TRUE,
  228. 'default' => 0,
  229. 'not NULL' => FALSE,
  230. 'description' => 'a value from 0 to 100 indicating percent complete'
  231. ),
  232. 'status' => array(
  233. 'type' => 'varchar',
  234. 'length' => 50,
  235. 'not NULL' => TRUE
  236. ),
  237. 'submit_date' => array(
  238. 'type' => 'int',
  239. 'not NULL' => TRUE,
  240. 'description' => 'UNIX integer submit time'
  241. ),
  242. 'start_time' => array(
  243. 'type' => 'int',
  244. 'not NULL' => FALSE,
  245. 'description' => 'UNIX integer start time'
  246. ),
  247. 'end_time' => array(
  248. 'type' => 'int',
  249. 'not NULL' => FALSE,
  250. 'description' => 'UNIX integer end time'
  251. ),
  252. 'error_msg' => array(
  253. 'type' => 'text',
  254. 'size' => 'normal',
  255. 'not NULL' => FALSE
  256. ),
  257. 'pid' => array(
  258. 'type' => 'int',
  259. 'unsigned' => TRUE,
  260. 'not NULL' => FALSE,
  261. 'description' => 'The process id for the job'
  262. ),
  263. 'priority' => array(
  264. 'type' => 'int',
  265. 'unsigned' => TRUE,
  266. 'not NULL' => TRUE,
  267. 'default' => '0',
  268. 'description' => 'The job priority'
  269. ),
  270. 'mlock' => array(
  271. 'type' => 'int',
  272. 'unsigned' => TRUE,
  273. 'not NULL' => FALSE,
  274. 'description' => 'If set to 1 then all jobs for the module are held until this one finishes'
  275. ),
  276. 'lock' => array(
  277. 'type' => 'int',
  278. 'unsigned' => TRUE,
  279. 'not NULL' => FALSE,
  280. 'description' => 'If set to 1 then all jobs are held until this one finishes'
  281. ),
  282. ),
  283. 'indexes' => array(
  284. 'job_id' => array('job_id'),
  285. 'job_name' => array('job_name')
  286. ),
  287. 'primary key' => array('job_id'),
  288. );
  289. return $schema;
  290. }
  291. /**
  292. * Describes the Tripal Custom Tables (tripal_custom_tables) table
  293. * This keeps track of tables created by Tripal and stored in chado that may or may not
  294. * also be materialized views.
  295. *
  296. * @ingroup tripal_core
  297. */
  298. function tripal_core_get_custom_tables_schema() {
  299. $schema = array();
  300. $schema['tripal_custom_tables'] = array(
  301. 'fields' => array(
  302. 'table_id' => array(
  303. 'type' => 'serial',
  304. 'unsigned' => TRUE,
  305. 'not NULL' => TRUE
  306. ),
  307. 'table_name' => array(
  308. 'type' => 'varchar',
  309. 'length' => 255,
  310. 'not NULL' => TRUE
  311. ),
  312. 'schema' => array(
  313. 'type' => 'text',
  314. 'not NULL' => TRUE
  315. ),
  316. 'mview_id' => array(
  317. 'type' => 'int',
  318. 'not NULL' => FALSE
  319. )
  320. ),
  321. 'indexes' => array(
  322. 'table_id' => array('table_id'),
  323. ),
  324. 'primary key' => array('table_id'),
  325. 'foreign keys' => array(
  326. 'tripal_mviews' => array(
  327. 'table' => 'tripal_mviews',
  328. 'columns' => array(
  329. 'mview_id' => 'mview_id'
  330. ),
  331. ),
  332. ),
  333. );
  334. return $schema;
  335. }
  336. function tripal_core_get_tripal_token_schema() {
  337. $schema = array();
  338. $schema['tripal_token_formats'] = array(
  339. 'fields' => array(
  340. 'tripal_format_id' => array(
  341. 'type' => 'serial',
  342. 'unsigned' => TRUE,
  343. 'not null' => TRUE
  344. ),
  345. 'content_type' => array(
  346. 'type' => 'varchar',
  347. 'length' => 255,
  348. 'not null' => TRUE
  349. ),
  350. 'application' => array(
  351. 'type' => 'varchar',
  352. 'length' => 255,
  353. 'not null' => TRUE
  354. ),
  355. 'format' => array(
  356. 'type' => 'text',
  357. 'not null' => TRUE
  358. ),
  359. 'tokens' => array(
  360. 'type' => 'text',
  361. 'not null' => TRUE
  362. ),
  363. ),
  364. 'unique keys' => array(
  365. 'type_application' => array('content_type', 'application'),
  366. ),
  367. 'primary key' => array('tripal_format_id'),
  368. );
  369. return $schema;
  370. }
  371. /**
  372. *
  373. *
  374. */
  375. function tripal_core_get_tripal_toc_schema() {
  376. $schema = array();
  377. $schema['tripal_toc'] = array(
  378. 'fields' => array(
  379. 'toc_item_id' => array(
  380. 'type' => 'serial',
  381. 'unsigned' => TRUE,
  382. 'not null' => TRUE
  383. ),
  384. 'node_type' => array(
  385. 'type' => 'varchar',
  386. 'length' => 32,
  387. 'not null' => TRUE
  388. ),
  389. 'key' => array(
  390. 'type' => 'varchar',
  391. 'length' => 255,
  392. 'not null' => TRUE,
  393. ),
  394. 'title' => array(
  395. 'type' => 'varchar',
  396. 'length' => 255,
  397. 'not null' => FALSE
  398. ),
  399. 'weight' => array(
  400. 'type' => 'int',
  401. 'not null' => FALSE
  402. ),
  403. 'hide' => array(
  404. 'type' => 'int',
  405. 'size' => 'tiny',
  406. 'not null' => FALSE,
  407. 'default' => 0,
  408. ),
  409. 'nid' => array(
  410. 'type' => 'int',
  411. 'not null' => FALSE,
  412. )
  413. ),
  414. 'indexes' => array(
  415. 'tripal_toc_idx1' => array('node_type', 'key'),
  416. 'tripal_toc_idx2' => array('node_type', 'key', 'nid'),
  417. ),
  418. 'unique keys' => array(
  419. 'tripal_toc_uq1' => array('node_type', 'key', 'nid'),
  420. ),
  421. 'primary key' => array('toc_item_id'),
  422. );
  423. return $schema;
  424. }
  425. /**
  426. *
  427. */
  428. function tripal_core_get_tripal_vars_schema() {
  429. $schema = array();
  430. $schema['tripal_variables'] = array(
  431. 'description' => 'This table houses a list of unique variable names that ' .
  432. 'can be used in the tripal_node_variables table.',
  433. 'fields' => array(
  434. 'variable_id' => array (
  435. 'type' => 'serial',
  436. 'not null' => TRUE,
  437. ),
  438. 'name' => array(
  439. 'type' => 'varchar',
  440. 'length' => 255,
  441. 'not null' => TRUE,
  442. ),
  443. 'description' => array(
  444. 'type' => 'text',
  445. 'not null' => TRUE,
  446. ),
  447. ),
  448. 'primary key' => array (
  449. 0 => 'variable_id',
  450. ),
  451. 'unique keys' => array (
  452. 'tripal_variables_c1' => array (
  453. 0 => 'name',
  454. ),
  455. ),
  456. 'indexes' => array (
  457. 'tripal_variable_names_idx1' => array (
  458. 0 => 'variable_id',
  459. ),
  460. ),
  461. );
  462. $schema['tripal_node_variables'] = array(
  463. 'description' => 'This table is used for storing any type of variable such as ' .
  464. 'a property or setting that should be associated with a Tripal managed Drupal node. This table is '.
  465. 'meant to store non-biological information only. All biological data should be housed ' .
  466. 'in the Chado tables. Be aware that any data stored here will not be made visible ' .
  467. 'through services such as Tripal Web Services and therefore can be a good place to ' .
  468. 'hide application specific settings.',
  469. 'fields' => array (
  470. 'node_variable_id' => array (
  471. 'type' => 'serial',
  472. 'not null' => TRUE,
  473. ),
  474. 'nid' => array (
  475. 'type' => 'int',
  476. 'not null' => TRUE,
  477. ),
  478. 'variable_id' => array (
  479. 'type' => 'int',
  480. 'not null' => TRUE,
  481. ),
  482. 'value' => array (
  483. 'type' => 'text',
  484. 'not null' => FALSE,
  485. ),
  486. 'rank' => array (
  487. 'type' => 'int',
  488. 'not null' => TRUE,
  489. 'default' => 0,
  490. ),
  491. ),
  492. 'primary key' => array (
  493. 0 => 'node_variable_id',
  494. ),
  495. 'unique keys' => array (
  496. 'tripal_node_variables_c1' => array (
  497. 0 => 'nid',
  498. 1 => 'variable_id',
  499. 2 => 'rank',
  500. ),
  501. ),
  502. 'indexes' => array (
  503. 'tripal_node_variables_idx1' => array (
  504. 0 => 'variable_id',
  505. ),
  506. ),
  507. 'foreign keys' => array (
  508. 'tripal_variables' => array (
  509. 'table' => 'tripal_variables',
  510. 'columns' => array (
  511. 'variable_id' => 'variable_id',
  512. ),
  513. ),
  514. ),
  515. );
  516. return $schema;
  517. }
  518. /**
  519. * Adds an mview_id column to the tripal_custom_tables table and makes an assocation between the mview and the custom table
  520. *
  521. */
  522. function tripal_core_update_7200() {
  523. try {
  524. // add an mview column to the tripal_custom_tables table so we
  525. // can associate which of the custom tables are also mviews
  526. if (!db_field_exists('tripal_custom_tables', 'mview_id')) {
  527. $spec = array(
  528. 'type' => 'int',
  529. 'not NULL' => FALSE
  530. );
  531. $keys = array(
  532. 'foreign keys' => array(
  533. 'tripal_mviews' => array(
  534. 'table' => 'tripal_mviews',
  535. 'columns' => array(
  536. 'mview_id' => 'mview_id'
  537. ),
  538. ),
  539. ),
  540. );
  541. db_add_field('tripal_custom_tables', 'mview_id', $spec, $keys);
  542. // the foreign key specification doesn't really add one to the
  543. // Drupal schema, it is just used internally, but we want one
  544. db_query('
  545. ALTER TABLE {tripal_custom_tables}
  546. ADD CONSTRAINT tripal_custom_tables_fk1
  547. FOREIGN KEY (mview_id) REFERENCES {tripal_mviews} (mview_id)
  548. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  549. ');
  550. }
  551. // now link the materialized view to it's custom table entry
  552. $mviews = db_select('tripal_mviews', 'tmv')
  553. ->fields('tmv', array('mview_id', 'mv_table'))
  554. ->execute();
  555. foreach ($mviews as $mview) {
  556. db_update('tripal_custom_tables')
  557. ->fields(array(
  558. 'mview_id' => $mview->mview_id
  559. ))
  560. ->condition('table_name', $mview->mv_table)
  561. ->execute();
  562. }
  563. }
  564. catch (\PDOException $e) {
  565. $error = $e->getMessage();
  566. throw new DrupalUpdateException('Could not update tripal_mviews table and link to custom tables: '. $error);
  567. }
  568. }
  569. /**
  570. * Fixes missing language for nodes and URL aliases. This may take awhile...
  571. *
  572. */
  573. function tripal_core_update_7201() {
  574. try {
  575. $sql = "UPDATE {node} SET language = :language WHERE language = ''";
  576. db_query($sql, array(':language' => LANGUAGE_NONE));
  577. $sql = "UPDATE {url_alias} SET language = :language WHERE language = ''";
  578. db_query($sql, array(':language' => LANGUAGE_NONE));
  579. }
  580. catch (\PDOException $e) {
  581. $error = $e->getMessage();
  582. throw new DrupalUpdateException('Could not reset language for nodes and url aliases: '. $error);
  583. }
  584. }
  585. /**
  586. * Adds a tripal_token_formats table for custom page titles and URL paths
  587. */
  588. function tripal_core_update_7202() {
  589. try {
  590. $schema = drupal_get_schema_unprocessed('tripal_core', 'tripal_token_formats');
  591. db_create_table('tripal_token_formats', $schema);
  592. }
  593. catch (Exception $e) {
  594. $error = $e->getMessage();
  595. throw new DrupalUpdateException('Could not add tripal_token_formats table: '. $error);
  596. }
  597. }
  598. /**
  599. * Adds a tripal_toc table for customizing the table of contents on each Tripal page.
  600. */
  601. function tripal_core_update_7203() {
  602. try {
  603. $schema = drupal_get_schema_unprocessed('tripal_core', 'tripal_toc');
  604. db_create_table('tripal_toc', $schema);
  605. }
  606. catch (Exception $e) {
  607. $error = $e->getMessage();
  608. throw new DrupalUpdateException('Could not add tripal_toc table: '. $error);
  609. }
  610. }
  611. /**
  612. * Adds the tripal_variable_terms and a tripal_node_variables tables
  613. */
  614. function tripal_core_update_7204() {
  615. try {
  616. $schema = drupal_get_schema_unprocessed('tripal_core', 'tripal_variables');
  617. db_create_table('tripal_variables', $schema);
  618. $schema = drupal_get_schema_unprocessed('tripal_core', 'tripal_node_variables');
  619. db_create_table('tripal_node_variables', $schema);
  620. }
  621. catch (Exception $e) {
  622. $error = $e->getMessage();
  623. throw new DrupalUpdateException('Could not add new tables table: '. $error);
  624. }
  625. }