chado_install.php 8.4 KB

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