tripal_core.install 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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_tripaltoken_schema();
  90. foreach ($temp as $table => $arr) {
  91. $schema[$table] = $arr;
  92. }
  93. return $schema;
  94. }
  95. /**
  96. * Describes the Tripal Materialized View (tripal_mviews) table
  97. * This table keeps track of all materialized views created by Tripal and stored in chado
  98. *
  99. * @ingroup tripal_core
  100. */
  101. function tripal_core_mviews_schema() {
  102. $schema = array();
  103. $schema['tripal_mviews'] = array(
  104. 'fields' => array(
  105. 'mview_id' => array(
  106. 'type' => 'serial',
  107. 'unsigned' => TRUE,
  108. 'not NULL' => TRUE
  109. ),
  110. 'name' => array(
  111. 'type' => 'varchar',
  112. 'length' => 255,
  113. 'not NULL' => TRUE
  114. ),
  115. 'modulename' => array(
  116. 'type' => 'varchar',
  117. 'length' => 50,
  118. 'not NULL' => TRUE,
  119. 'description' => 'The module name that provides the callback for this job'
  120. ),
  121. 'mv_table' => array(
  122. 'type' => 'varchar',
  123. 'length' => 128,
  124. 'not NULL' => FALSE
  125. ),
  126. 'mv_specs' => array(
  127. 'type' => 'text',
  128. 'size' => 'normal',
  129. 'not NULL' => FALSE
  130. ),
  131. 'mv_schema' => array(
  132. 'type' => 'text',
  133. 'size' => 'normal',
  134. 'not NULL' => FALSE
  135. ),
  136. 'indexed' => array(
  137. 'type' => 'text',
  138. 'size' => 'normal',
  139. 'not NULL' => FALSE
  140. ),
  141. 'query' => array(
  142. 'type' => 'text',
  143. 'size' => 'normal',
  144. 'not NULL' => TRUE
  145. ),
  146. 'special_index' => array(
  147. 'type' => 'text',
  148. 'size' => 'normal',
  149. 'not NULL' => FALSE
  150. ),
  151. 'last_update' => array(
  152. 'type' => 'int',
  153. 'not NULL' => FALSE,
  154. 'description' => 'UNIX integer time'
  155. ),
  156. 'status' => array(
  157. 'type' => 'text',
  158. 'size' => 'normal',
  159. 'not NULL' => FALSE
  160. ),
  161. 'comment' => array(
  162. 'type' => 'text',
  163. 'size' => 'normal',
  164. 'not NULL' => FALSE
  165. ),
  166. ),
  167. 'indexes' => array(
  168. 'mview_id' => array('mview_id')
  169. ),
  170. 'unique keys' => array(
  171. 'mv_table' => array('mv_table'),
  172. 'mv_name' => array('name'),
  173. ),
  174. 'primary key' => array('mview_id'),
  175. );
  176. return $schema;
  177. }
  178. /**
  179. * Describes the Tripal Jobs (tripal_jobs) table
  180. * This table keeps track of all tripal jobs including their current status and is used
  181. * by tripal_launch_jobs to determine which jobs need to be run
  182. *
  183. * @ingroup tripal_core
  184. */
  185. function tripal_core_jobs_schema() {
  186. $schema = array();
  187. $schema['tripal_jobs'] = array(
  188. 'fields' => array(
  189. 'job_id' => array(
  190. 'type' => 'serial',
  191. 'unsigned' => TRUE,
  192. 'not NULL' => TRUE
  193. ),
  194. 'uid' => array(
  195. 'type' => 'int',
  196. 'unsigned' => TRUE,
  197. 'not NULL' => TRUE,
  198. 'description' => 'The Drupal userid of the submitee'
  199. ),
  200. 'job_name' => array(
  201. 'type' => 'varchar',
  202. 'length' => 255,
  203. 'not NULL' => TRUE
  204. ),
  205. 'modulename' => array(
  206. 'type' => 'varchar',
  207. 'length' => 50,
  208. 'not NULL' => TRUE,
  209. 'description' => 'The module name that provides the callback for this job'
  210. ),
  211. 'callback' => array(
  212. 'type' => 'varchar',
  213. 'length' => 255,
  214. 'not NULL' => TRUE
  215. ),
  216. 'arguments' => array(
  217. 'type' => 'text',
  218. 'size' => 'normal',
  219. 'not NULL' => FALSE
  220. ),
  221. 'progress' => array(
  222. 'type' => 'int',
  223. 'unsigned' => TRUE,
  224. 'default' => 0,
  225. 'not NULL' => FALSE,
  226. 'description' => 'a value from 0 to 100 indicating percent complete'
  227. ),
  228. 'status' => array(
  229. 'type' => 'varchar',
  230. 'length' => 50,
  231. 'not NULL' => TRUE
  232. ),
  233. 'submit_date' => array(
  234. 'type' => 'int',
  235. 'not NULL' => TRUE,
  236. 'description' => 'UNIX integer submit time'
  237. ),
  238. 'start_time' => array(
  239. 'type' => 'int',
  240. 'not NULL' => FALSE,
  241. 'description' => 'UNIX integer start time'
  242. ),
  243. 'end_time' => array(
  244. 'type' => 'int',
  245. 'not NULL' => FALSE,
  246. 'description' => 'UNIX integer end time'
  247. ),
  248. 'error_msg' => array(
  249. 'type' => 'text',
  250. 'size' => 'normal',
  251. 'not NULL' => FALSE
  252. ),
  253. 'pid' => array(
  254. 'type' => 'int',
  255. 'unsigned' => TRUE,
  256. 'not NULL' => FALSE,
  257. 'description' => 'The process id for the job'
  258. ),
  259. 'priority' => array(
  260. 'type' => 'int',
  261. 'unsigned' => TRUE,
  262. 'not NULL' => TRUE,
  263. 'default' => '0',
  264. 'description' => 'The job priority'
  265. ),
  266. 'mlock' => array(
  267. 'type' => 'int',
  268. 'unsigned' => TRUE,
  269. 'not NULL' => FALSE,
  270. 'description' => 'If set to 1 then all jobs for the module are held until this one finishes'
  271. ),
  272. 'lock' => array(
  273. 'type' => 'int',
  274. 'unsigned' => TRUE,
  275. 'not NULL' => FALSE,
  276. 'description' => 'If set to 1 then all jobs are held until this one finishes'
  277. ),
  278. ),
  279. 'indexes' => array(
  280. 'job_id' => array('job_id'),
  281. 'job_name' => array('job_name')
  282. ),
  283. 'primary key' => array('job_id'),
  284. );
  285. return $schema;
  286. }
  287. /**
  288. * Describes the Tripal Custom Tables (tripal_custom_tables) table
  289. * This keeps track of tables created by Tripal and stored in chado that may or may not
  290. * also be materialized views.
  291. *
  292. * @ingroup tripal_core
  293. */
  294. function tripal_core_custom_tables_schema() {
  295. $schema = array();
  296. $schema['tripal_custom_tables'] = array(
  297. 'fields' => array(
  298. 'table_id' => array(
  299. 'type' => 'serial',
  300. 'unsigned' => TRUE,
  301. 'not NULL' => TRUE
  302. ),
  303. 'table_name' => array(
  304. 'type' => 'varchar',
  305. 'length' => 255,
  306. 'not NULL' => TRUE
  307. ),
  308. 'schema' => array(
  309. 'type' => 'text',
  310. 'not NULL' => TRUE
  311. ),
  312. 'mview_id' => array(
  313. 'type' => 'int',
  314. 'not NULL' => FALSE
  315. )
  316. ),
  317. 'indexes' => array(
  318. 'table_id' => array('table_id'),
  319. ),
  320. 'primary key' => array('table_id'),
  321. 'foreign keys' => array(
  322. 'tripal_mviews' => array(
  323. 'table' => 'tripal_mviews',
  324. 'columns' => array(
  325. 'mview_id' => 'mview_id'
  326. ),
  327. ),
  328. ),
  329. );
  330. return $schema;
  331. }
  332. function tripal_core_tripaltoken_schema() {
  333. $schema = array();
  334. $schema['tripal_token_formats'] = array(
  335. 'fields' => array(
  336. 'tripal_format_id' => array(
  337. 'type' => 'serial',
  338. 'unsigned' => TRUE,
  339. 'not NULL' => TRUE
  340. ),
  341. 'content_type' => array(
  342. 'type' => 'varchar',
  343. 'length' => 255,
  344. 'not NULL' => TRUE
  345. ),
  346. 'application' => array(
  347. 'type' => 'varchar',
  348. 'length' => 255,
  349. 'not NULL' => TRUE
  350. ),
  351. 'format' => array(
  352. 'type' => 'text',
  353. 'not NULL' => TRUE
  354. ),
  355. 'tokens' => array(
  356. 'type' => 'text',
  357. 'not NULL' => TRUE
  358. ),
  359. ),
  360. 'indexes' => array(
  361. 'tripal_format_id' => array('tripal_format_id'),
  362. 'type_application' => array('content_type', 'application'),
  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. * Adds an mview_id column to the tripal_custom_tables table and makes an assocation between the mview and the custom table
  373. *
  374. */
  375. function tripal_core_update_7200() {
  376. try {
  377. // add an mview column to the tripal_custom_tables table so we
  378. // can associate which of the custom tables are also mviews
  379. if (!db_field_exists('tripal_custom_tables', 'mview_id')) {
  380. $spec = array(
  381. 'type' => 'int',
  382. 'not NULL' => FALSE
  383. );
  384. $keys = array(
  385. 'foreign keys' => array(
  386. 'tripal_mviews' => array(
  387. 'table' => 'tripal_mviews',
  388. 'columns' => array(
  389. 'mview_id' => 'mview_id'
  390. ),
  391. ),
  392. ),
  393. );
  394. db_add_field('tripal_custom_tables', 'mview_id', $spec, $keys);
  395. // the foreign key specification doesn't really add one to the
  396. // Drupal schema, it is just used internally, but we want one
  397. db_query('
  398. ALTER TABLE {tripal_custom_tables}
  399. ADD CONSTRAINT tripal_custom_tables_fk1
  400. FOREIGN KEY (mview_id) REFERENCES {tripal_mviews} (mview_id)
  401. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  402. ');
  403. }
  404. // now link the materialized view to it's custom table entry
  405. $mviews = db_select('tripal_mviews', 'tmv')
  406. ->fields('tmv', array('mview_id', 'mv_table'))
  407. ->execute();
  408. foreach ($mviews as $mview) {
  409. db_update('tripal_custom_tables')
  410. ->fields(array(
  411. 'mview_id' => $mview->mview_id
  412. ))
  413. ->condition('table_name', $mview->mv_table)
  414. ->execute();
  415. }
  416. }
  417. catch (\PDOException $e) {
  418. $error = $e->getMessage();
  419. throw new DrupalUpdateException('Could not update tripal_mviews table and link to custom tables: '. $error);
  420. }
  421. }
  422. /**
  423. * Fixes missing language for nodes and URL aliases. This may take awhile...
  424. *
  425. */
  426. function tripal_core_update_7201() {
  427. try {
  428. $sql = "UPDATE {node} SET language = :language WHERE language = ''";
  429. db_query($sql, array(':language' => LANGUAGE_NONE));
  430. $sql = "UPDATE {url_alias} SET language = :language WHERE language = ''";
  431. db_query($sql, array(':language' => LANGUAGE_NONE));
  432. }
  433. catch (\PDOException $e) {
  434. $error = $e->getMessage();
  435. throw new DrupalUpdateException('Could not reset language for nodes and url aliases: '. $error);
  436. }
  437. }
  438. /**
  439. * Adds in tripal token formats table to handle page title and path rewrites
  440. */
  441. function tripal_core_update_7202() {
  442. $schema = array();
  443. $schema['tripal_token_formats'] = array(
  444. 'fields' => array(
  445. 'tripal_format_id' => array(
  446. 'type' => 'serial',
  447. 'unsigned' => TRUE,
  448. 'not NULL' => TRUE
  449. ),
  450. 'content_type' => array(
  451. 'type' => 'varchar',
  452. 'length' => 255,
  453. 'not NULL' => TRUE
  454. ),
  455. 'application' => array(
  456. 'type' => 'varchar',
  457. 'length' => 255,
  458. 'not NULL' => TRUE
  459. ),
  460. 'format' => array(
  461. 'type' => 'text',
  462. 'not NULL' => TRUE
  463. ),
  464. 'tokens' => array(
  465. 'type' => 'text',
  466. 'not NULL' => TRUE
  467. ),
  468. ),
  469. 'indexes' => array(
  470. 'tripal_format_id' => array('tripal_format_id'),
  471. 'type_application' => array('content_type', 'application'),
  472. ),
  473. 'unique keys' => array(
  474. 'type_application' => array('content_type', 'application'),
  475. ),
  476. 'primary key' => array('tripal_format_id'),
  477. );
  478. db_create_table('tripal_token_formats', $schema['tripal_token_formats']);
  479. }