tripal_core.install 17 KB

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