tripal_pub.install 14 KB

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