tripal_pub.install 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. $cdi = db_select('tripal_cv_defaults', 't')
  270. ->fields('t', array('cv_default_id'))
  271. ->condition('table_name', 'pub')
  272. ->condition('field_name', 'type_id')
  273. ->execute()
  274. ->fetchField();
  275. if (!$cdi) {
  276. db_insert('tripal_cv_defaults')
  277. ->fields(array(
  278. 'table_name' => 'pub',
  279. 'field_name' => 'type_id',
  280. 'cv_id' => $cv_id
  281. ))
  282. ->execute();
  283. }
  284. // use the new pub_property CV we just added
  285. $cdi = db_select('tripal_cv_defaults', 't')
  286. ->fields('t', array('cv_default_id'))
  287. ->condition('table_name', 'pubprop')
  288. ->condition('field_name', 'type_id')
  289. ->execute()
  290. ->fetchField();
  291. if (!$cdi) {
  292. db_insert('tripal_cv_defaults')
  293. ->fields(array(
  294. 'table_name' => 'pubprop',
  295. 'field_name' => 'type_id',
  296. 'cv_id' => $cv_id
  297. ))
  298. ->execute();
  299. }
  300. }
  301. catch (\PDOException $e) {
  302. $error = $e->getMessage();
  303. throw new DrupalUpdateException('Failed to add pub_property vocabulary: '. $error);
  304. }
  305. // add the pub_property CV
  306. try {
  307. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_property'")->fetchField();
  308. if (!$cv_id) {
  309. // add the vocabulary
  310. $cv_id = db_insert('chado.cv')
  311. ->fields(array(
  312. 'name' => 'pub_property',
  313. 'definition' => 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  314. ))
  315. ->execute();
  316. }
  317. }
  318. catch (\PDOException $e) {
  319. $error = $e->getMessage();
  320. throw new DrupalUpdateException('Failed to add pub_property vocabulary: '. $error);
  321. }
  322. // add the pub_type CV
  323. try {
  324. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_type'")->fetchField();
  325. if (!$cv_id) {
  326. // add the vocabulary
  327. $cv_id = db_insert('chado.cv')
  328. ->fields(array(
  329. 'name' => 'pub_type',
  330. 'definition' => 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  331. ))
  332. ->execute();
  333. }
  334. }
  335. catch (\PDOException $e) {
  336. $error = $e->getMessage();
  337. throw new DrupalUpdateException('Failed to add pub_type vocabulary: '. $error);
  338. }
  339. // add the pub_relationship CV
  340. try {
  341. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_relationship'")->fetchField();
  342. if (!$cv_id) {
  343. // add the vocabulary
  344. $cv_id = db_insert('chado.cv')
  345. ->fields(array(
  346. 'name' => 'pub_relationship',
  347. 'definition' => 'Contains types of relationships between publications.'
  348. ))
  349. ->execute();
  350. }
  351. // use the new pub_property CV we just added
  352. $cdi = db_select('tripal_cv_defaults', 't')
  353. ->fields('t', array('cv_default_id'))
  354. ->condition('table_name', 'pub_relationship')
  355. ->condition('field_name', 'type_id')
  356. ->execute()
  357. ->fetchField();
  358. if (!$cdi) {
  359. db_insert('tripal_cv_defaults')
  360. ->fields(array(
  361. 'table_name' => 'pub_relationship',
  362. 'field_name' => 'type_id',
  363. 'cv_id' => $cv_id
  364. ))
  365. ->execute();
  366. }
  367. }
  368. catch (\PDOException $e) {
  369. $error = $e->getMessage();
  370. throw new DrupalUpdateException('Failed to add pub_relationship vocabulary: '. $error);
  371. }
  372. }
  373. /**
  374. * Implementation of hook_update_dependencies(). It specifies a list of
  375. * other modules whose updates must be run prior to this one.
  376. */
  377. function tripal_pub_update_dependencies() {
  378. $dependencies = array();
  379. // the tripal_cv update 7200 must run prior to update 7200 of this module
  380. $dependencies['tripal_pub'][7200] = array(
  381. 'tripal_cv' => 7200
  382. );
  383. return $dependencies;
  384. }
  385. /**
  386. * Adds missing foreign key constraints
  387. *
  388. */
  389. function tripal_pub_update_7201() {
  390. // there was a bug in the function for creating a custom table that
  391. // kept foreign key constraints from being added. So, we need to add those
  392. // to keep from error messages appear, we will drop the FK if it already
  393. // exists and then re-add it.
  394. try {
  395. $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'pubauthor_contact_pubauthor_id_fkey'))->fetchField();
  396. if ($fkey_exists) {
  397. db_query('
  398. ALTER TABLE chado.pubauthor_contact
  399. DROP CONSTRAINT pubauthor_contact_pubauthor_id_fkey CASCADE
  400. ');
  401. db_query('
  402. ALTER TABLE chado.pubauthor_contact
  403. DROP CONSTRAINT pubauthor_contact_contact_id_fkey CASCADE
  404. ');
  405. }
  406. db_query('
  407. ALTER TABLE chado.pubauthor_contact
  408. ADD CONSTRAINT pubauthor_contact_pubauthor_id_fkey
  409. FOREIGN KEY (pubauthor_id) REFERENCES chado.pubauthor (pubauthor_id)
  410. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  411. ');
  412. db_query('
  413. ALTER TABLE chado.pubauthor_contact
  414. ADD CONSTRAINT pubauthor_contact_contact_id_fkey
  415. FOREIGN KEY (contact_id) REFERENCES chado.contact (contact_id)
  416. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  417. ');
  418. }
  419. catch (\PDOException $e) {
  420. $error = $e->getMessage();
  421. throw new DrupalUpdateException('Failed to update foriegn key: '. $error);
  422. }
  423. }
  424. /**
  425. * Updates path of tripal_pub OBO to be relative.
  426. */
  427. function tripal_pub_update_7202() {
  428. try {
  429. // Remove duplicates.
  430. db_delete('tripal_cv_obo')
  431. ->condition('name', 'Tripal Publication')
  432. ->execute();
  433. // Add in the updated path.
  434. $obo_path = '{tripal_pub}/files/tpub.obo';
  435. $obo_id = tripal_insert_obo('Tripal Publication', $obo_path);
  436. }
  437. catch (\PDOException $e) {
  438. $error = $e->getMessage();
  439. throw new DrupalUpdateException('Failed to update tripal_pub OBO path: '. $error);
  440. }
  441. }