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