tripal_pub.install 11 KB

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