tripal_pub.install 11 KB

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