tripal_core.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 IF EXISTS 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_jobs_schema();
  78. foreach ($temp as $table => $arr) {
  79. $schema[$table] = $arr;
  80. }
  81. $temp = tripal_core_mviews_schema();
  82. foreach ($temp as $table => $arr) {
  83. $schema[$table] = $arr;
  84. }
  85. $temp = tripal_core_custom_tables_schema();
  86. foreach ($temp as $table => $arr) {
  87. $schema[$table] = $arr;
  88. }
  89. $temp = tripal_core_tripal_token_schema();
  90. foreach ($temp as $table => $arr) {
  91. $schema[$table] = $arr;
  92. }
  93. $temp = tripal_core_tripal_toc_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_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_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_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_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. 'indexes' => array(
  365. 'tripal_format_id' => array('tripal_format_id'),
  366. 'type_application' => array('content_type', 'application'),
  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_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. * Adds an mview_id column to the tripal_custom_tables table and makes an assocation between the mview and the custom table
  431. *
  432. */
  433. function tripal_core_update_7200() {
  434. try {
  435. // add an mview column to the tripal_custom_tables table so we
  436. // can associate which of the custom tables are also mviews
  437. if (!db_field_exists('tripal_custom_tables', 'mview_id')) {
  438. $spec = array(
  439. 'type' => 'int',
  440. 'not NULL' => FALSE
  441. );
  442. $keys = array(
  443. 'foreign keys' => array(
  444. 'tripal_mviews' => array(
  445. 'table' => 'tripal_mviews',
  446. 'columns' => array(
  447. 'mview_id' => 'mview_id'
  448. ),
  449. ),
  450. ),
  451. );
  452. db_add_field('tripal_custom_tables', 'mview_id', $spec, $keys);
  453. // the foreign key specification doesn't really add one to the
  454. // Drupal schema, it is just used internally, but we want one
  455. db_query('
  456. ALTER TABLE {tripal_custom_tables}
  457. ADD CONSTRAINT tripal_custom_tables_fk1
  458. FOREIGN KEY (mview_id) REFERENCES {tripal_mviews} (mview_id)
  459. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  460. ');
  461. }
  462. // now link the materialized view to it's custom table entry
  463. $mviews = db_select('tripal_mviews', 'tmv')
  464. ->fields('tmv', array('mview_id', 'mv_table'))
  465. ->execute();
  466. foreach ($mviews as $mview) {
  467. db_update('tripal_custom_tables')
  468. ->fields(array(
  469. 'mview_id' => $mview->mview_id
  470. ))
  471. ->condition('table_name', $mview->mv_table)
  472. ->execute();
  473. }
  474. }
  475. catch (\PDOException $e) {
  476. $error = $e->getMessage();
  477. throw new DrupalUpdateException('Could not update tripal_mviews table and link to custom tables: '. $error);
  478. }
  479. }
  480. /**
  481. * Fixes missing language for nodes and URL aliases. This may take awhile...
  482. *
  483. */
  484. function tripal_core_update_7201() {
  485. try {
  486. $sql = "UPDATE {node} SET language = :language WHERE language = ''";
  487. db_query($sql, array(':language' => LANGUAGE_NONE));
  488. $sql = "UPDATE {url_alias} SET language = :language WHERE language = ''";
  489. db_query($sql, array(':language' => LANGUAGE_NONE));
  490. }
  491. catch (\PDOException $e) {
  492. $error = $e->getMessage();
  493. throw new DrupalUpdateException('Could not reset language for nodes and url aliases: '. $error);
  494. }
  495. }
  496. /**
  497. * Adds a tripal_token_formats table for custom page titles and URL paths
  498. */
  499. function tripal_core_update_7202() {
  500. try {
  501. $schema = drupal_get_schema_unprocessed('tripal_core', 'tripal_token_formats');
  502. db_create_table('tripal_token_formats', $schema);
  503. }
  504. catch (Exception $e) {
  505. $error = $e->getMessage();
  506. throw new DrupalUpdateException('Could not add tripal_token_formats table: '. $error);
  507. }
  508. }
  509. /**
  510. * Adds a tripal_toc table for customizing the table of contents on each Tripal page.
  511. */
  512. function tripal_core_update_7203() {
  513. try {
  514. $schema = drupal_get_schema_unprocessed('tripal_core', 'tripal_toc');
  515. db_create_table('tripal_toc', $schema);
  516. }
  517. catch (Exception $e) {
  518. $error = $e->getMessage();
  519. throw new DrupalUpdateException('Could not add tripal_toc table: '. $error);
  520. }
  521. }