chado_install.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. if(tripal_core_reset_chado_schema()){
  41. tripal_core_install_sql($schema_file);
  42. tripal_core_install_sql($init_file);
  43. } else {
  44. print "ERROR: cannot install chado. Please check database permissions\n";
  45. exit;
  46. }
  47. }
  48. /**
  49. *
  50. *
  51. * @ingroup tripal_core
  52. */
  53. function tripal_core_reset_chado_schema (){
  54. global $active_db;
  55. // iterate through the lines of the schema file and rebuild the SQL
  56. pg_query($active_db,"drop schema chado cascade");
  57. pg_query($active_db,"drop schema genetic_code cascade");
  58. pg_query($active_db,"drop schema so cascade");
  59. pg_query($active_db,"drop schema frange cascade");
  60. pg_query($active_db,"create schema chado");
  61. pg_query($active_db,"create language plpgsql");
  62. // check that the chado schema now exists
  63. $sql = "SELECT nspname
  64. FROM pg_namespace
  65. WHERE has_schema_privilege(nspname, 'USAGE') and nspname = 'chado'
  66. ORDER BY nspname";
  67. $schema = db_fetch_object(db_query($sql));
  68. if(strcmp($schema->nspname,'chado')!=0){
  69. return 0;
  70. }
  71. return 1;
  72. }
  73. /**
  74. *
  75. *
  76. * @ingroup tripal_core
  77. */
  78. function tripal_core_install_sql ($sql_file){
  79. global $active_db;
  80. pg_query($active_db,"set search_path to chado,public");
  81. print "Loading $sql_file...\n";
  82. $lines = file($sql_file,FILE_SKIP_EMPTY_LINES);
  83. if(!$lines){
  84. return 'Cannot open $schema_file';
  85. }
  86. $stack = array();
  87. $in_string = 0;
  88. $query = '';
  89. $i = 0;
  90. foreach ($lines as $line_num => $line) {
  91. $i++;
  92. $type = '';
  93. // find and remove comments except when inside of strings
  94. if(preg_match('/--/',$line) and !$in_string and !preg_match("/'.*?--.*?'/",$line)){
  95. $line = preg_replace('/--.*$/','',$line); // remove comments
  96. }
  97. if(preg_match('/\/\*.*?\*\//',$line)){
  98. $line = preg_replace('/\/\*.*?\*\//','',$line); // remove comments
  99. }
  100. // skip empty lines
  101. if(preg_match('/^\s*$/',$line) or strcmp($line,'')==0){
  102. continue;
  103. }
  104. // Find SQL for new objects
  105. if(preg_match('/^\s*CREATE\s+TABLE/i',$line) and !$in_string){
  106. $stack[] = 'table';
  107. $line = preg_replace("/public./","chado.",$line);
  108. }
  109. if(preg_match('/^\s*ALTER\s+TABLE/i',$line) and !$in_string){
  110. $stack[] = 'alter table';
  111. $line = preg_replace("/public./","chado.",$line);
  112. }
  113. if(preg_match('/^\s*SET/i',$line) and !$in_string){
  114. $stack[] = 'set';
  115. }
  116. if(preg_match('/^\s*CREATE\s+SCHEMA/i',$line) and !$in_string){
  117. $stack[] = 'schema';
  118. }
  119. if(preg_match('/^\s*CREATE\s+SEQUENCE/i',$line) and !$in_string){
  120. $stack[] = 'sequence';
  121. $line = preg_replace("/public./","chado.",$line);
  122. }
  123. if(preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*VIEW/i',$line) and !$in_string){
  124. $stack[] = 'view';
  125. $line = preg_replace("/public./","chado.",$line);
  126. }
  127. if(preg_match('/^\s*COMMENT/i',$line) and !$in_string and sizeof($stack)==0){
  128. $stack[] = 'comment';
  129. $line = preg_replace("/public./","chado.",$line);
  130. }
  131. if(preg_match('/^\s*CREATE\s+(?:OR\s+REPLACE\s+)*FUNCTION/i',$line) and !$in_string){
  132. $stack[] = 'function';
  133. $line = preg_replace("/public./","chado.",$line);
  134. }
  135. if(preg_match('/^\s*CREATE\s+INDEX/i',$line) and !$in_string){
  136. $stack[] = 'index';
  137. }
  138. if(preg_match('/^\s*INSERT\s+INTO/i',$line) and !$in_string){
  139. $stack[] = 'insert';
  140. $line = preg_replace("/public./","chado.",$line);
  141. }
  142. if(preg_match('/^\s*CREATE\s+TYPE/i',$line) and !$in_string){
  143. $stack[] = 'type';
  144. }
  145. if(preg_match('/^\s*GRANT/i',$line) and !$in_string){
  146. $stack[] = 'grant';
  147. }
  148. if(preg_match('/^\s*CREATE\s+AGGREGATE/i',$line) and !$in_string){
  149. $stack[] = 'aggregate';
  150. }
  151. // determine if we are in a string that spans a line
  152. $matches = preg_match_all("/[']/i",$line,$temp);
  153. $in_string = $in_string - ($matches % 2);
  154. $in_string = abs($in_string);
  155. // if we've reached the end of an object the pop the stack
  156. if(strcmp($stack[sizeof($stack)-1],'table') == 0 and preg_match('/\);\s*$/',$line)){
  157. $type = array_pop($stack);
  158. }
  159. if(strcmp($stack[sizeof($stack)-1],'alter table') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  160. $type = array_pop($stack);
  161. }
  162. if(strcmp($stack[sizeof($stack)-1],'set') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  163. $type = array_pop($stack);
  164. }
  165. if(strcmp($stack[sizeof($stack)-1],'schema') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  166. $type = array_pop($stack);
  167. }
  168. if(strcmp($stack[sizeof($stack)-1],'sequence') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  169. $type = array_pop($stack);
  170. }
  171. if(strcmp($stack[sizeof($stack)-1],'view') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  172. $type = array_pop($stack);
  173. }
  174. if(strcmp($stack[sizeof($stack)-1],'comment') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  175. $type = array_pop($stack);
  176. }
  177. if(strcmp($stack[sizeof($stack)-1],'function') == 0 and preg_match("/LANGUAGE.*?;\s+$/i",$line)){
  178. $type = array_pop($stack);
  179. }
  180. if(strcmp($stack[sizeof($stack)-1],'index') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  181. $type = array_pop($stack);
  182. }
  183. if(strcmp($stack[sizeof($stack)-1],'insert') == 0 and preg_match('/\);\s*$/',$line)){
  184. $type = array_pop($stack);
  185. }
  186. if(strcmp($stack[sizeof($stack)-1],'type') == 0 and preg_match('/\);\s*$/',$line)){
  187. $type = array_pop($stack);
  188. }
  189. if(strcmp($stack[sizeof($stack)-1],'grant') == 0 and preg_match('/;\s*$/',$line) and !$in_string){
  190. $type = array_pop($stack);
  191. }
  192. if(strcmp($stack[sizeof($stack)-1],'aggregate') == 0 and preg_match('/\);\s*$/',$line)){
  193. $type = array_pop($stack);
  194. }
  195. // if we're in a recognized SQL statement then let's keep track of lines
  196. if($type or sizeof($stack) > 0){
  197. $query .= "$line";
  198. } else {
  199. print "UNHANDLED $i, $in_string: $line";
  200. return tripal_core_chado_install_done();
  201. }
  202. if(preg_match_all("/\n/",$query,$temp) > 100){
  203. print "SQL query is too long. Terminating:\n$query\n";
  204. return tripal_core_chado_install_done();
  205. }
  206. if($type and sizeof($stack) == 0){
  207. print "Adding $type: line $i\n";
  208. // rewrite the set serach_path to make 'public' be 'chado'
  209. if(strcmp($type,'set')==0){
  210. $query = preg_replace("/public/m","chado",$query);
  211. }
  212. $result = pg_query($active_db, $query);
  213. if(!$result){
  214. $error = pg_last_error();
  215. print "Installation failed:\nSQL $i, $in_string: $query\n$error\n";
  216. pg_query($active_db,"set search_path to public,chado");
  217. return tripal_core_chado_install_done();
  218. }
  219. $query = '';
  220. }
  221. }
  222. print "Installation Complete!\n";
  223. tripal_core_chado_install_done();
  224. }
  225. /**
  226. *
  227. *
  228. * @ingroup tripal_core
  229. */
  230. function tripal_core_chado_install_done (){
  231. // return the search path to normal
  232. global $active_db;
  233. pg_query($active_db,"set search_path to public,chado");
  234. }