tripal_core.install 17 KB

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