tripal_core.install 17 KB

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