chado_install.php 9.2 KB

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