tripal_pub.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. /**
  3. * @file
  4. * Installation of the publication module
  5. */
  6. /**
  7. * Implements hook_disable().
  8. * Disable default views when module is disabled
  9. *
  10. * @ingroup tripal_pub
  11. */
  12. function tripal_pub_disable() {
  13. // Disable all default views provided by this module
  14. require_once("tripal_pub.views_default.inc");
  15. $views = tripal_pub_views_default_views();
  16. foreach (array_keys($views) as $view_name) {
  17. tripal_disable_view($view_name,FALSE,array('suppress_error' => TRUE));
  18. }
  19. }
  20. /**
  21. * Implementation of hook_requirements().
  22. *
  23. * @ingroup tripal_pub
  24. */
  25. function tripal_pub_requirements($phase) {
  26. $requirements = array();
  27. if ($phase == 'install') {
  28. // make sure chado is installed
  29. if (!$GLOBALS["chado_is_installed"]) {
  30. $requirements ['tripal_pub'] = array(
  31. 'title' => "tripal_pub",
  32. 'value' => "ERROR: Chado must be installed before this module can be enabled",
  33. 'severity' => REQUIREMENT_ERROR,
  34. );
  35. }
  36. }
  37. return $requirements;
  38. }
  39. /**
  40. * Implementation of hook_install().
  41. *
  42. * @ingroup tripal_pub
  43. */
  44. function tripal_pub_install() {
  45. global $base_path;
  46. // add loading of the the tripal pub ontology to the job queue
  47. $obo_path = '{tripal_pub}/files/tpub.obo';
  48. $obo_id = tripal_insert_obo('Tripal Publication', $obo_path);
  49. tripal_submit_obo_job(array('obo_id' => $obo_id));
  50. tripal_pub_add_cvs();
  51. tripal_pub_add_cvterms();
  52. // add the custom tables
  53. tripal_pub_add_custom_tables();
  54. // set the default vocabularies
  55. tripal_set_default_cv('pub', 'type_id', 'tripal_pub');
  56. tripal_set_default_cv('pubprop', 'type_id', 'tripal_pub');
  57. tripal_set_default_cv('pub_relationship', 'type_id', 'pub_relationship');
  58. }
  59. /**
  60. * Implementation of hook_uninstall().
  61. *
  62. * @ingroup tripal_pub
  63. */
  64. function tripal_pub_uninstall() {
  65. }
  66. /**
  67. * Implements hook_enable().
  68. *
  69. * @ingroup tripal_pub
  70. */
  71. function tripal_pub_enable() {
  72. // make sure we have our supported databases
  73. tripal_insert_db(
  74. array(
  75. 'name' => 'PMID',
  76. 'description' => 'PubMed',
  77. 'url' => 'http://www.ncbi.nlm.nih.gov/pubmed',
  78. 'urlprefix' => 'http://www.ncbi.nlm.nih.gov/pubmed/'
  79. ),
  80. array('update_existing' => TRUE)
  81. );
  82. tripal_insert_db(
  83. array(
  84. 'name' => 'AGL',
  85. 'description' => 'USDA National Agricultural Library',
  86. 'url' => 'http://agricola.nal.usda.gov/'
  87. ),
  88. array('update_existing' => TRUE)
  89. );
  90. variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
  91. }
  92. /**
  93. * Implementation of hook_schema().
  94. *
  95. * @ingroup tripal_pub
  96. */
  97. function tripal_pub_schema() {
  98. $schema['chado_pub'] = array(
  99. 'fields' => array(
  100. 'vid' => array(
  101. 'type' => 'int',
  102. 'unsigned' => TRUE,
  103. 'not null' => TRUE, 'default' => 0
  104. ),
  105. 'nid' => array(
  106. 'type' => 'int',
  107. 'unsigned' => TRUE,
  108. 'not null' => TRUE,
  109. 'default' => 0
  110. ),
  111. 'pub_id' => array(
  112. 'type' => 'int',
  113. 'not null' => TRUE,
  114. 'default' => 0
  115. ),
  116. 'sync_date' => array(
  117. 'type' => 'int',
  118. 'not null' => FALSE,
  119. 'description' => 'UNIX integer sync date/time'
  120. ),
  121. ),
  122. 'indexes' => array(
  123. 'pub_id' => array('pub_id')
  124. ),
  125. 'unique keys' => array(
  126. 'nid_vid' => array('nid', 'vid'),
  127. 'vid' => array('vid')
  128. ),
  129. 'primary key' => array('nid'),
  130. );
  131. $schema['tripal_pub_import'] = array(
  132. 'fields' => array(
  133. 'pub_import_id' => array(
  134. 'type' => 'serial',
  135. 'not null' => TRUE
  136. ),
  137. 'name' => array(
  138. 'type' => 'varchar',
  139. 'length' => 255,
  140. 'not null' => TRUE
  141. ),
  142. 'criteria' => array(
  143. 'type' => 'text',
  144. 'size' => 'normal',
  145. 'not null' => TRUE,
  146. 'description' => 'Contains a serialized PHP array containing the search criteria'
  147. ),
  148. 'disabled' => array(
  149. 'type' => 'int',
  150. 'unsigned' => TRUE,
  151. 'not NULL' => TRUE,
  152. 'default' => 0
  153. ),
  154. 'do_contact' => array(
  155. 'type' => 'int',
  156. 'unsigned' => TRUE,
  157. 'not NULL' => TRUE,
  158. 'default' => 0
  159. ),
  160. ),
  161. 'primary key' => array('pub_import_id'),
  162. 'indexes' => array(
  163. 'name' => array('name')
  164. ),
  165. );
  166. return $schema;
  167. }
  168. /**
  169. * Add custom table related to publications
  170. * - pubauthor_contact
  171. *
  172. * @ingroup tripal_pub
  173. */
  174. function tripal_pub_add_custom_tables() {
  175. $schema = array (
  176. 'table' => 'pubauthor_contact',
  177. 'fields' => array (
  178. 'pubauthor_contact_id' => array (
  179. 'type' => 'serial',
  180. 'not null' => true,
  181. ),
  182. 'contact_id' => array (
  183. 'type' => 'int',
  184. 'not null' => true,
  185. ),
  186. 'pubauthor_id' => array (
  187. 'type' => 'int',
  188. 'not null' => true,
  189. ),
  190. ),
  191. 'primary key' => array (
  192. 0 => 'pubauthor_contact_id',
  193. ),
  194. 'unique keys' => array (
  195. 'pubauthor_contact_c1' => array (
  196. 0 => 'contact_id',
  197. 1 => 'pubauthor_id',
  198. ),
  199. ),
  200. 'foreign keys' => array (
  201. 'contact' => array (
  202. 'table' => 'contact',
  203. 'columns' => array (
  204. 'contact_id' => 'contact_id',
  205. ),
  206. ),
  207. 'pubauthor' => array (
  208. 'table' => 'pubauthor',
  209. 'columns' => array (
  210. 'pubauthor_id' => 'pubauthor_id',
  211. ),
  212. ),
  213. ),
  214. );
  215. chado_create_custom_table('pubauthor_contact', $schema, TRUE);
  216. }
  217. /**
  218. * Add cvs related to publications
  219. *
  220. * @ingroup tripal_pub
  221. */
  222. function tripal_pub_add_cvs() {
  223. // Add the cv for pub properties
  224. tripal_insert_cv(
  225. 'tripal_pub',
  226. 'A heirarchical set of terms for describing a publication. It is intended to be used as the default vocabularies in Tripal for publication types and contact properties.'
  227. );
  228. // Add the cv for pub types
  229. tripal_insert_cv(
  230. 'pub_type',
  231. 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  232. );
  233. // Add the cv for pub properties
  234. tripal_insert_cv(
  235. 'pub_property',
  236. 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  237. );
  238. // Add cv for relationship types
  239. tripal_insert_cv(
  240. 'pub_relationship',
  241. 'Contains types of relationships between publications.'
  242. );
  243. }
  244. /**
  245. * Add cvterms related to publications
  246. *
  247. * @ingroup tripal_pub
  248. */
  249. function tripal_pub_add_cvterms() {
  250. }
  251. /**
  252. * This is the required update for tripal_pub when upgrading from Drupal core API 6.x.
  253. *
  254. */
  255. function tripal_pub_update_7200() {
  256. // add the tripal_pub CV and set it to be the default for pub types and pub properties
  257. try {
  258. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'tripal_pub'")->fetchField();
  259. if (!$cv_id) {
  260. // add the vocabulary
  261. $cv_id = db_insert('chado.cv')
  262. ->fields(array(
  263. 'name' => 'tripal_pub',
  264. 'definition' => 'A heirarchical set of terms for describing a publication. It is intended to be used as the default vocabularies in Tripal for publication types and contact properties.'
  265. ))
  266. ->execute();
  267. }
  268. // use the new pub_property CV we just added
  269. db_insert('tripal_cv_defaults')
  270. ->fields(array(
  271. 'table_name' => 'pub',
  272. 'field_name' => 'type_id',
  273. 'cv_id' => $cv_id
  274. ))
  275. ->execute();
  276. // use the new pub_property CV we just added
  277. db_insert('tripal_cv_defaults')
  278. ->fields(array(
  279. 'table_name' => 'pubprop',
  280. 'field_name' => 'type_id',
  281. 'cv_id' => $cv_id
  282. ))
  283. ->execute();
  284. }
  285. catch (\PDOException $e) {
  286. $error = $e->getMessage();
  287. throw new DrupalUpdateException('Failed to add pub_property vocabulary: '. $error);
  288. }
  289. // add the pub_property CV
  290. try {
  291. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_property'")->fetchField();
  292. if (!$cv_id) {
  293. // add the vocabulary
  294. $cv_id = db_insert('chado.cv')
  295. ->fields(array(
  296. 'name' => 'pub_property',
  297. 'definition' => 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  298. ))
  299. ->execute();
  300. }
  301. }
  302. catch (\PDOException $e) {
  303. $error = $e->getMessage();
  304. throw new DrupalUpdateException('Failed to add pub_property vocabulary: '. $error);
  305. }
  306. // add the pub_type CV
  307. try {
  308. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_type'")->fetchField();
  309. if (!$cv_id) {
  310. // add the vocabulary
  311. $cv_id = db_insert('chado.cv')
  312. ->fields(array(
  313. 'name' => 'pub_type',
  314. 'definition' => 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  315. ))
  316. ->execute();
  317. }
  318. }
  319. catch (\PDOException $e) {
  320. $error = $e->getMessage();
  321. throw new DrupalUpdateException('Failed to add pub_type vocabulary: '. $error);
  322. }
  323. // add the pub_relationship CV
  324. try {
  325. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_relationship'")->fetchField();
  326. if (!$cv_id) {
  327. // add the vocabulary
  328. $cv_id = db_insert('chado.cv')
  329. ->fields(array(
  330. 'name' => 'pub_relationship',
  331. 'definition' => 'Contains types of relationships between publications.'
  332. ))
  333. ->execute();
  334. }
  335. // use the new pub_property CV we just added
  336. db_insert('tripal_cv_defaults')
  337. ->fields(array(
  338. 'table_name' => 'pub_relationship',
  339. 'field_name' => 'type_id',
  340. 'cv_id' => $cv_id
  341. ))
  342. ->execute();
  343. }
  344. catch (\PDOException $e) {
  345. $error = $e->getMessage();
  346. throw new DrupalUpdateException('Failed to add pub_relationship vocabulary: '. $error);
  347. }
  348. }
  349. /**
  350. * Implementation of hook_update_dependencies(). It specifies a list of
  351. * other modules whose updates must be run prior to this one.
  352. */
  353. function tripal_pub_update_dependencies() {
  354. $dependencies = array();
  355. // the tripal_cv update 7200 must run prior to update 7200 of this module
  356. $dependencies['tripal_pub'][7200] = array(
  357. 'tripal_cv' => 7200
  358. );
  359. return $dependencies;
  360. }
  361. /**
  362. * Adds missing foreign key constraints
  363. *
  364. */
  365. function tripal_pub_update_7201() {
  366. // there was a bug in the function for creating a custom table that
  367. // kept foreign key constraints from being added. So, we need to add those
  368. // to keep from error messages appear, we will drop the FK if it already
  369. // exists and then re-add it.
  370. try {
  371. $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'pubauthor_contact_pubauthor_id_fkey'))->fetchField();
  372. if ($fkey_exists) {
  373. db_query('
  374. ALTER TABLE chado.pubauthor_contact
  375. DROP CONSTRAINT pubauthor_contact_pubauthor_id_fkey CASCADE
  376. ');
  377. db_query('
  378. ALTER TABLE chado.pubauthor_contact
  379. DROP CONSTRAINT pubauthor_contact_contact_id_fkey CASCADE
  380. ');
  381. }
  382. db_query('
  383. ALTER TABLE chado.pubauthor_contact
  384. ADD CONSTRAINT pubauthor_contact_pubauthor_id_fkey
  385. FOREIGN KEY (pubauthor_id) REFERENCES chado.pubauthor (pubauthor_id)
  386. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  387. ');
  388. db_query('
  389. ALTER TABLE chado.pubauthor_contact
  390. ADD CONSTRAINT pubauthor_contact_contact_id_fkey
  391. FOREIGN KEY (contact_id) REFERENCES chado.contact (contact_id)
  392. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  393. ');
  394. }
  395. catch (\PDOException $e) {
  396. $error = $e->getMessage();
  397. throw new DrupalUpdateException('Failed to update foriegn key: '. $error);
  398. }
  399. }
  400. /**
  401. * Updates path of tripal_pub OBO to be relative.
  402. */
  403. function tripal_pub_update_7202() {
  404. try {
  405. // Remove duplicates.
  406. db_delete('tripal_cv_obo')
  407. ->condition('name', 'Tripal Publication')
  408. ->execute();
  409. // Add in the updated path.
  410. $obo_path = '{tripal_pub}/files/tpub.obo';
  411. $obo_id = tripal_insert_obo('Tripal Publication', $obo_path);
  412. }
  413. catch (\PDOException $e) {
  414. $error = $e->getMessage();
  415. throw new DrupalUpdateException('Failed to update tripal_pub OBO path: '. $error);
  416. }
  417. }