tripal_pub.install 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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_legacy_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_legacy_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_legacy_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_legacy_pub
  63. */
  64. function tripal_pub_uninstall() {
  65. }
  66. /**
  67. * Implements hook_enable().
  68. *
  69. * @ingroup tripal_legacy_pub
  70. */
  71. function tripal_pub_enable() {
  72. }
  73. /**
  74. * Implementation of hook_schema().
  75. *
  76. * @ingroup tripal_legacy_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_legacy_pub
  154. */
  155. // This function was moved to tripal_chado/includes/setup/tripal_chado.setup.inc
  156. /* function tripal_pub_add_custom_tables() {
  157. $schema = array (
  158. 'table' => 'pubauthor_contact',
  159. 'fields' => array (
  160. 'pubauthor_contact_id' => array (
  161. 'type' => 'serial',
  162. 'not null' => true,
  163. ),
  164. 'contact_id' => array (
  165. 'type' => 'int',
  166. 'not null' => true,
  167. ),
  168. 'pubauthor_id' => array (
  169. 'type' => 'int',
  170. 'not null' => true,
  171. ),
  172. ),
  173. 'primary key' => array (
  174. 0 => 'pubauthor_contact_id',
  175. ),
  176. 'unique keys' => array (
  177. 'pubauthor_contact_c1' => array (
  178. 0 => 'contact_id',
  179. 1 => 'pubauthor_id',
  180. ),
  181. ),
  182. 'foreign keys' => array (
  183. 'contact' => array (
  184. 'table' => 'contact',
  185. 'columns' => array (
  186. 'contact_id' => 'contact_id',
  187. ),
  188. ),
  189. 'pubauthor' => array (
  190. 'table' => 'pubauthor',
  191. 'columns' => array (
  192. 'pubauthor_id' => 'pubauthor_id',
  193. ),
  194. ),
  195. ),
  196. );
  197. chado_create_custom_table('pubauthor_contact', $schema, TRUE);
  198. } */
  199. /**
  200. * This is the required update for tripal_pub when upgrading from Drupal core API 6.x.
  201. *
  202. */
  203. function tripal_pub_update_7200() {
  204. // add the tripal_pub CV and set it to be the default for pub types and pub properties
  205. try {
  206. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'tripal_pub'")->fetchField();
  207. if (!$cv_id) {
  208. // add the vocabulary
  209. $cv_id = db_insert('chado.cv')
  210. ->fields(array(
  211. 'name' => 'tripal_pub',
  212. '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.'
  213. ))
  214. ->execute();
  215. }
  216. // use the new pub_property CV we just added
  217. db_insert('tripal_cv_defaults')
  218. ->fields(array(
  219. 'table_name' => 'pub',
  220. 'field_name' => 'type_id',
  221. 'cv_id' => $cv_id
  222. ))
  223. ->execute();
  224. // use the new pub_property CV we just added
  225. db_insert('tripal_cv_defaults')
  226. ->fields(array(
  227. 'table_name' => 'pubprop',
  228. 'field_name' => 'type_id',
  229. 'cv_id' => $cv_id
  230. ))
  231. ->execute();
  232. }
  233. catch (\PDOException $e) {
  234. $error = $e->getMessage();
  235. throw new DrupalUpdateException('Failed to add pub_property vocabulary: '. $error);
  236. }
  237. // add the pub_property CV
  238. try {
  239. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_property'")->fetchField();
  240. if (!$cv_id) {
  241. // add the vocabulary
  242. $cv_id = db_insert('chado.cv')
  243. ->fields(array(
  244. 'name' => 'pub_property',
  245. 'definition' => 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  246. ))
  247. ->execute();
  248. }
  249. }
  250. catch (\PDOException $e) {
  251. $error = $e->getMessage();
  252. throw new DrupalUpdateException('Failed to add pub_property vocabulary: '. $error);
  253. }
  254. // add the pub_type CV
  255. try {
  256. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_type'")->fetchField();
  257. if (!$cv_id) {
  258. // add the vocabulary
  259. $cv_id = db_insert('chado.cv')
  260. ->fields(array(
  261. 'name' => 'pub_type',
  262. 'definition' => 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  263. ))
  264. ->execute();
  265. }
  266. }
  267. catch (\PDOException $e) {
  268. $error = $e->getMessage();
  269. throw new DrupalUpdateException('Failed to add pub_type vocabulary: '. $error);
  270. }
  271. // add the pub_relationship CV
  272. try {
  273. $cv_id = db_query("SELECT cv_id FROM chado.cv WHERE name = 'pub_relationship'")->fetchField();
  274. if (!$cv_id) {
  275. // add the vocabulary
  276. $cv_id = db_insert('chado.cv')
  277. ->fields(array(
  278. 'name' => 'pub_relationship',
  279. 'definition' => 'Contains types of relationships between publications.'
  280. ))
  281. ->execute();
  282. }
  283. // use the new pub_property CV we just added
  284. db_insert('tripal_cv_defaults')
  285. ->fields(array(
  286. 'table_name' => 'pub_relationship',
  287. 'field_name' => 'type_id',
  288. 'cv_id' => $cv_id
  289. ))
  290. ->execute();
  291. }
  292. catch (\PDOException $e) {
  293. $error = $e->getMessage();
  294. throw new DrupalUpdateException('Failed to add pub_relationship vocabulary: '. $error);
  295. }
  296. }
  297. /**
  298. * Implementation of hook_update_dependencies(). It specifies a list of
  299. * other modules whose updates must be run prior to this one.
  300. */
  301. function tripal_pub_update_dependencies() {
  302. $dependencies = array();
  303. // the tripal_cv update 7200 must run prior to update 7200 of this module
  304. $dependencies['tripal_pub'][7200] = array(
  305. 'tripal_cv' => 7200
  306. );
  307. return $dependencies;
  308. }
  309. /**
  310. * Adds missing foreign key constraints
  311. *
  312. */
  313. function tripal_pub_update_7201() {
  314. // there was a bug in the function for creating a custom table that
  315. // kept foreign key constraints from being added. So, we need to add those
  316. // to keep from error messages appear, we will drop the FK if it already
  317. // exists and then re-add it.
  318. try {
  319. $fkey_exists = db_query('SELECT TRUE FROM pg_constraint WHERE conname = :constraint', array(':constraint' => 'pubauthor_contact_pubauthor_id_fkey'))->fetchField();
  320. if ($fkey_exists) {
  321. db_query('
  322. ALTER TABLE chado.pubauthor_contact
  323. DROP CONSTRAINT pubauthor_contact_pubauthor_id_fkey CASCADE
  324. ');
  325. db_query('
  326. ALTER TABLE chado.pubauthor_contact
  327. DROP CONSTRAINT pubauthor_contact_contact_id_fkey CASCADE
  328. ');
  329. }
  330. db_query('
  331. ALTER TABLE chado.pubauthor_contact
  332. ADD CONSTRAINT pubauthor_contact_pubauthor_id_fkey
  333. FOREIGN KEY (pubauthor_id) REFERENCES chado.pubauthor (pubauthor_id)
  334. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  335. ');
  336. db_query('
  337. ALTER TABLE chado.pubauthor_contact
  338. ADD CONSTRAINT pubauthor_contact_contact_id_fkey
  339. FOREIGN KEY (contact_id) REFERENCES chado.contact (contact_id)
  340. ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  341. ');
  342. }
  343. catch (\PDOException $e) {
  344. $error = $e->getMessage();
  345. throw new DrupalUpdateException('Failed to update foriegn key: '. $error);
  346. }
  347. }
  348. /**
  349. * Updates path of tripal_pub OBO to be relative.
  350. */
  351. function tripal_pub_update_7202() {
  352. try {
  353. // Remove duplicates.
  354. db_delete('tripal_cv_obo')
  355. ->condition('name', 'Tripal Publication')
  356. ->execute();
  357. // Add in the updated path.
  358. $obo_path = '{tripal_pub}/files/tpub.obo';
  359. $obo_id = tripal_insert_obo('Tripal Publication', $obo_path);
  360. }
  361. catch (\PDOException $e) {
  362. $error = $e->getMessage();
  363. throw new DrupalUpdateException('Failed to update tripal_pub OBO path: '. $error);
  364. }
  365. }