chado_install.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. *
  4. *
  5. * @ingroup tripal_core
  6. */
  7. function tripal_core_chado_v1_11_load_form (){
  8. $form['description'] = array(
  9. '#type' => 'item',
  10. '#value' => t("Click the submit button below to install Chado into the Drupal database. <br><font color=\"red\">WARNING:</font> use this only for a new chado installation or reinstall completely. This will erase any data currently in the chado database. If you are
  11. using chado in a database external to the Drupal database with a 'chado' entry in the 'settings.php' \$db_url argument then this option will intall chado but it will not be usable. The external database specified in the settings.php file takes precedence."),
  12. '#weight' => 1,
  13. );
  14. $form['button'] = array(
  15. '#type' => 'submit',
  16. '#value' => t('Install Chado'),
  17. '#weight' => 2,
  18. );
  19. return $form;
  20. }
  21. /**
  22. *
  23. * @ingroup tripal_core
  24. */
  25. function tripal_core_chado_v1_11_load_form_submit ($form, &$form_state){
  26. global $user;
  27. $args = array();
  28. tripal_add_job("Install Chado",'tripal_core',
  29. 'tripal_core_install_chado',$args,$user->uid);
  30. return '';
  31. }
  32. /**
  33. *
  34. *
  35. * @ingroup tripal_core
  36. */
  37. function tripal_core_install_chado ($dummy = NULL, $job = NULL){
  38. $schema_file = drupal_get_path('module', 'tripal_core').'/default_schema.sql';
  39. $init_file = drupal_get_path('module', 'tripal_core').'/initialize.sql';
  40. tripal_core_reset_chado_schema();
  41. tripal_core_install_sql($schema_file);
  42. tripal_core_install_sql($init_file);
  43. }
  44. /**
  45. *
  46. *
  47. * @ingroup tripal_core
  48. */
  49. function tripal_core_reset_chado_schema (){
  50. global $active_db;
  51. // iterate through the lines of the schema file and rebuild the SQL
  52. pg_query($active_db,"drop schema chado cascade");
  53. pg_query($active_db,"drop schema genetic_code cascade");
  54. pg_query($active_db,"drop schema so cascade");
  55. pg_query($active_db,"drop schema frange cascade");
  56. pg_query($active_db,"create schema chado");
  57. pg_query($active_db,"create language plpgsql");
  58. }
  59. /**
  60. *
  61. *
  62. * @ingroup tripal_core
  63. */
  64. function tripal_core_install_sql ($sql_file){
  65. global $active_db;
  66. pg_query($active_db,"set search_path to chado,public");
  67. print "Loading $sql_file...\n";
  68. $lines = file($sql_file,FILE_SKIP_EMPTY_LINES);
  69. if(!$lines){
  70. return 'Cannot open $schema_file';
  71. }
  72. $stack = array();
  73. $in_string = 0;
  74. $query = '';
  75. $i = 0;
  76. foreach ($lines as $line_num => $line) {
  77. $i++;
  78. $type = '';
  79. // find and remove comments except when inside of strings
  80. if(preg_match('/--/',$line) and !$in_string and !preg_match("/'.*?--.*?'/",$line)){
  81. $line = preg_replace('/--.*$/','',$line); // remove comments
  82. }
  83. if(preg_match('/\/\*.*?\*\//',$line)){
  84. $line = preg_replace('/\/\*.*?\*\//','',$line); // remove comments
  85. }
  86. // skip empty lines
  87. if(preg_match('/^\s*$/',$line) or strcmp($line,'')==0){
  88. continue;
  89. }
  90. // Find SQL for new objects
  91. if(preg_match('/^\s*CREATE\s+TABLE/i',$line) and !$in_string){
  92. $stack[] = 'table';
  93. $line = preg_replace("/public./","chado.",$line);
  94. }
  95. if(preg_match('/^\s*ALTER\s+TABLE/i',$line) and !$in_string){
  96. $stack[] = 'alter table';
  97. $line = preg_replace("/public./","chado.",$line);
  98. }
  99. if(preg_match('/^\s*SET/i',$line) and !$in_string){
  100. $stack[] = 'set';
  101. }
  102. if(preg_match('/^\s*CREATE\s+SCHEMA/i',$line) and !$in_string){
  103. $stack[] = 'schema';
  104. }
  105. if(preg_match('/^\s*CREATE\s+SEQUENCE/i',$line) and !$in_string){
  106. $stack[] = 'sequence';
  107. $line = preg_replace("/public./","chado.",$line);
  108. }
  109. if(preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i',$line) and !$in_string){
  110. $stack[] = 'view';
  111. $line = preg_replace("/public./","chado.",$line);
  112. }
  113. if(preg_match('/^\s*COMMENT/i',$line) and !$in_string and sizeof($stack)==0){
  114. $stack[] = 'comment';
  115. $line = preg_replace("/public./","chado.",$line);
  116. }
  117. if(preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i',$line) and !$in_string){
  118. $stack[] = 'function';
  119. $line = preg_replace("/public./","chado.",$line);
  120. }
  121. if(preg_match('/^\s*CREATE\s+INDEX/i',$line) and !$in_string){
  122. $stack[] = 'index';
  123. }
  124. if(preg_match('/^\s*INSERT\s+INTO/i',$line) and !$in_string){
  125. $stack[] = 'insert';
  126. $line = preg_replace("/public./","chado.",$line);
  127. }
  128. if(preg_match('/^\s*CREATE\s+TYPE/i',$line) and !$in_string){
  129. $stack[] = 'type';
  130. }
  131. if(preg_match('/^\s*GRANT/i',$line) and !$in_string){
  132. $stack[] = 'grant';
  133. }
  134. if(preg_match('/^\s*CREATE\s+AGGREGATE/i',$line) and !$in_string){
  135. $stack[] = 'aggregate';
  136. }
  137. // determine if we are in a string that spans a line
  138. $matches = preg_match_all("/[']/i",$line,$temp);
  139. $in_string = $in_string - ($matches % 2);
  140. $in_string = abs($in_string);
  141. // if we've reached the end of an object the pop the stack
  142. if(strcmp($stack[sizeof($stack)-1],'table') == 0 and preg_match('/\);\s*$/',$line)){
  143. $type = array_pop($stack);
  144. }
  145. if(strcmp($stack[sizeof($stack)-1],'alter table') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  146. $type = array_pop($stack);
  147. }
  148. if(strcmp($stack[sizeof($stack)-1],'set') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  149. $type = array_pop($stack);
  150. }
  151. if(strcmp($stack[sizeof($stack)-1],'schema') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  152. $type = array_pop($stack);
  153. }
  154. if(strcmp($stack[sizeof($stack)-1],'sequence') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  155. $type = array_pop($stack);
  156. }
  157. if(strcmp($stack[sizeof($stack)-1],'view') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  158. $type = array_pop($stack);
  159. }
  160. if(strcmp($stack[sizeof($stack)-1],'comment') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  161. $type = array_pop($stack);
  162. }
  163. if(strcmp($stack[sizeof($stack)-1],'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i",$line)){
  164. $type = array_pop($stack);
  165. }
  166. if(strcmp($stack[sizeof($stack)-1],'index') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  167. $type = array_pop($stack);
  168. }
  169. if(strcmp($stack[sizeof($stack)-1],'insert') == 0 and preg_match('/\);\s*$/',$line)){
  170. $type = array_pop($stack);
  171. }
  172. if(strcmp($stack[sizeof($stack)-1],'type') == 0 and preg_match('/\);\s*$/',$line)){
  173. $type = array_pop($stack);
  174. }
  175. if(strcmp($stack[sizeof($stack)-1],'grant') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  176. $type = array_pop($stack);
  177. }
  178. if(strcmp($stack[sizeof($stack)-1],'aggregate') == 0 and preg_match('/\);\s*$/',$line)){
  179. $type = array_pop($stack);
  180. }
  181. // if we're in a recognized SQL statement then let's keep track of lines
  182. if($type or sizeof($stack) > 0){
  183. $query .= "$line";
  184. } else {
  185. print "UNHANDLED $i, $in_string: $line";
  186. return tripal_core_chado_install_done();
  187. }
  188. if(preg_match_all("/\n/",$query,$temp) > 100){
  189. print "SQL query is too long. Terminating:\n$query\n";
  190. return tripal_core_chado_install_done();
  191. }
  192. if($type and sizeof($stack) == 0){
  193. print "Adding $type: line $i\n";
  194. // rewrite the set serach_path to make 'public' be 'chado'
  195. if(strcmp($type,'set')==0){
  196. $query = preg_replace("/public/m","chado",$query);
  197. }
  198. $result = pg_query($active_db, $query);
  199. if(!$result){
  200. $error = pg_last_error();
  201. print "Installation failed:\nSQL $i, $in_string: $query\n$error\n";
  202. pg_query($active_db,"set search_path to public,chado");
  203. return tripal_core_chado_install_done();
  204. }
  205. $query = '';
  206. }
  207. }
  208. print "Installation Complete!\n";
  209. tripal_core_chado_install_done();
  210. }
  211. /**
  212. *
  213. *
  214. * @ingroup tripal_core
  215. */
  216. function tripal_core_chado_install_done (){
  217. // return the search path to normal
  218. global $active_db;
  219. pg_query($active_db,"set search_path to public,chado");
  220. }