tripal_pub.install 11 KB

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