Committed addlistitem.php after including json_encode function so as slashes and...
[openemr.git] / library / standard_tables_capture.inc
blobe0dea38d230cf9efedeb4b8bae8104f36def2100
1 <?php
2 /*******************************************************************/
3 // Copyright (C) 2011 Phyaura, LLC <info@phyaura.com>
4 //
5 // Authors:
6 //         Rohit Kumar <pandit.rohit@netsity.com>
7 //         Brady Miller <brady@sparmy.com>
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (at your option) any later version.
13 /*******************************************************************/
14 // This library contains functions for importing the RxNorm and SNOMED
15 // databases.
16 /*******************************************************************/
18 // Function to copy a package to temp
19 // $type (rxnorm, snomed etc.)
20 function temp_copy($filename,$type) {
22     if(!file_exists($filename)) {
23         return false;
24     }
26     if(!mkdir($GLOBALS['temporary_files_dir']."/".$type, 0777, true)) {
27         return false;
28     }
30     if(copy($filename,$GLOBALS['temporary_files_dir']."/".$type."/".basename($filename))) {
31         return true;
32     }
33     else {
34         return false;
35     }
38 // Function to unarchive a package
39 // $type (rxnorm, snomed etc.)
40 function temp_unarchive($filename,$type) {
41     $filename = $GLOBALS['temporary_files_dir']."/".$type."/".basename($filename);
42     if(!file_exists($filename)) {
43         return false;
44     }
45     else {
46         $zip = new ZipArchive;
47         if ($zip->open($filename) === TRUE) {
48             if (!($zip->extractTo($GLOBALS['temporary_files_dir']."/".$type))) {
49                 return false;
50             }
51             $zip->close();
52             return true;
53         }
54         else {
55             return false;
56         }
57     }
61 // Function to import the rxnorm tables
62 // $is_windows_flag - pass the IS_WINDOWS constant
63 function rxnorm_import($is_windows_flag) {
65     $rx_info = array();
66     $rx_info['rxnatomarchive'] = array('title' => "Archive Data", 'dir' => "$dir", 'origin' => "RXNATOMARCHIVE.RRF", 'filename' => "RXNATOMARCHIVE.RRF", 'table' => "rxnatomarchive", 'required' => 0);
67     $rx_info['rxnconso'] = array('title' => "Concept Names and Sources", 'dir' => "$dir", 'origin' => "RXNCONSO.RRF", 'filename' => "RXNCONSO.RRF", 'table' => "rxnconso",  'required' => 1);
68     $rx_info['rxncui'] = array('title' => "Retired RXCUI Data", 'dir' => "$dir", 'origin' => "RXNCUI.RRF", 'filename' => "RXNCUI.RRF", 'table' => "rxncui", 'required' => 1);
69     $rx_info['rxncuichanges'] = array('title' => "Concept Changes", 'dir' => "$dir", 'origin' => "RXNCUICHANGES.RRF", 'filename' => "RXNCUICHANGES.RRF", 'table' => "rxncuichanges", 'required' => 1);
70     $rx_info['rxndoc'] = array('title' => "Documentation for Abbreviated Values", 'dir' => "$dir", 'origin' => "RXNDOC.RRF", 'filename' => "RXNDOC.RRF", 'table' => "rxndoc", 'required' => 1);
71     $rx_info['rxnrel'] = array('title' => "Relationships", 'dir' => "$dir", 'origin' => "RXNREL.RRF", 'filename' => "RXNREL.RRF", 'table' => "rxnrel", 'required' => 1);
72     $rx_info['rxnsab'] = array('title' => "Source Information", 'dir' => "$dir", 'origin' => "RXNSAB.RRF", 'filename' => "RXNSAB.RRF", 'table' => "rxnsab", 'required' => 0);
73     $rx_info['rxnsat'] = array('title' => "Simple Concept and Atom Attributes", 'dir' => "$dir", 'origin' => "RXNSAT.RRF", 'filename' => "RXNSAT.RRF", 'table' => "rxnsat", 'required' => 0);
74     $rx_info['rxnsty'] = array('title' => "Semantic Types ", 'dir' => "$dir", 'origin' => "RXNSTY.RRF", 'filename' => "RXNSTY.RRF", 'table' => "rxnsty", 'required' => 1);
76     // set paths
77     $dirScripts = $GLOBALS['temporary_files_dir']."/rxnorm/scripts/mysql";
78     $dir = $GLOBALS['temporary_files_dir']."/rxnorm/rrf";
79     $dir=str_replace('\\','/',$dir);
81     // load scripts
82     $file_load = file_get_contents($dirScripts.'/Table_scripts_mysql_rxn.sql', true);
83     if ($is_windows_flag) {
84         $data_load = file_get_contents($dirScripts.'/Load_scripts_mysql_rxn_win.sql', true);
85     }
86     else {
87         $data_load = file_get_contents($dirScripts.'/Load_scripts_mysql_rxn_unix.sql', true);
88     }
89     $indexes_load = file_get_contents($dirScripts.'/Indexes_mysql_rxn.sql', true);
91     //
92     // Creating the structure for table and applying indexes
93     //
95     $file_array=explode(";",$file_load);
96     foreach($file_array as $val){
97         if(trim($val)!='')
98         {
99             sqlStatement($val);
100         }
101     }
103     $indexes_array=explode(";",$indexes_load);
105     foreach($indexes_array as $val1){
106         if(trim($val1)!='')
107         {
108             sqlStatement($val1);
109         }
110     }
112     $data=explode(";",$data_load);
113     foreach($data as $val)
114     {
115         foreach($rx_info as $key => $value)
116         {
117             $file_name= $value['origin'];
118             $replacement=$dir."/".$file_name;
120             $pattern='/'.$file_name.'/';
121             if(strpos($val,$file_name))     {
122                 $val1[]=       str_replace($file_name,$replacement,$val);
123             }
124         }
125     }
126     foreach($val1 as $val){
127         if(trim($val)!='')      {
128             sqlStatement($val);
129         }
130     }
132     return true;
135 // Function to import snomed tables
136 function snomed_import() {
138     // set up array
139     $table_array_for_snomed=array(
140         "sct_concepts_drop"=>"DROP TABLE IF EXISTS `sct_concepts`",
141         "sct_concepts_structure"=>"CREATE TABLE IF NOT EXISTS `sct_concepts` (
142             `ConceptId` bigint(20) NOT NULL,
143             `ConceptStatus` int(11) NOT NULL,
144             `FullySpecifiedName` varchar(255) NOT NULL,
145             `CTV3ID` varchar(5) NOT NULL,
146             `SNOMEDID` varchar(8) NOT NULL,
147             `IsPrimitive` tinyint(1) NOT NULL,
148             PRIMARY KEY (`ConceptId`)
149             ) ENGINE=MyISAM",
150         "sct_descriptions_drop"=>"DROP TABLE IF EXISTS `sct_descriptions`",
151         "sct_descriptions_structure"=>"CREATE TABLE IF NOT EXISTS `sct_descriptions` (
152             `DescriptionId` bigint(20) NOT NULL,
153             `DescriptionStatus` int(11) NOT NULL,
154             `ConceptId` bigint(20) NOT NULL,
155             `Term` varchar(255) NOT NULL,
156             `InitialCapitalStatus` tinyint(1) NOT NULL,
157             `DescriptionType` int(11) NOT NULL,
158             `LanguageCode` varchar(8) NOT NULL,
159             PRIMARY KEY (`DescriptionId`)
160             ) ENGINE=MyISAM",
161         "sct_relationships_drop"=>"DROP TABLE IF EXISTS `sct_relationships`",
162         "sct_relationships_structure"=>"CREATE TABLE IF NOT EXISTS `sct_relationships` (
163             `RelationshipId` bigint(20) NOT NULL,
164             `ConceptId1` bigint(20) NOT NULL,
165             `RelationshipType` bigint(20) NOT NULL,
166             `ConceptId2` bigint(20) NOT NULL,
167             `CharacteristicType` int(11) NOT NULL,
168             `Refinability` int(11) NOT NULL,
169             `RelationshipGroup` int(11) NOT NULL,
170             PRIMARY KEY (`RelationshipId`)
171             ) ENGINE=MyISAM"
174     // set up paths
175     $dir_snomed = $GLOBALS['temporary_files_dir']."/snomed/";
176     $sub_path="Terminology/Content/";
177     $dir=$dir_snomed;
178     $dir=str_replace('\\','/',$dir);
180     // executing the create statement for tables, these are defined in snomed_capture.inc file
181     foreach($table_array_for_snomed as $val){
182         if(trim($val)!=''){
183             sqlStatement($val);
184         }
185     }
187     // reading the snomed directry and identifying the files to import and replacing the variables by originals values.
188     if( is_dir($dir) && $handle = opendir($dir)) {
189         while (false !== ($filename = readdir($handle))) {
190             if ($filename != "." && $filename != ".." && strpos($filename,"zip")==0) {
191                 $path=$dir."".$filename."/".$sub_path;
192                 if( is_dir($path) && $handle1 = opendir($path)) {
193                     while (false !== ($filename1 = readdir($handle1))) {
194                         $load_script="Load data local infile '#FILENAME#' into table #TABLE# fields terminated by '\\t' ESCAPED BY '' lines terminated by '\\n' ignore 1 lines   ";
195                         $array_replace=array("#FILENAME#","#TABLE#");
196                         if ($filename1 != "." && $filename1 != "..") {
197                             $file_replace=$path.$filename1;
198                             if(strpos($filename1,"Concepts")){
199                                 $new_str=str_replace($array_replace,array($file_replace,"sct_concepts"),$load_script);
200                             }   
201                             if(strpos($filename1,"Descriptions")){
202                                 $new_str=str_replace($array_replace,array($file_replace,"sct_descriptions"),$load_script);
203                             }
204                             if(strpos($filename1,"Relationships")){
205                                 $new_str=str_replace($array_replace,array($file_replace,"sct_relationships"),$load_script);
206                             }
207                             if($new_str!=''){
208                                 sqlStatement($new_str);
209                             }
210                         }
211                     }
212                 }
213                 closedir($handle1);
214             }
215         }
216         closedir($handle);
217     }
219     return true;
222 // Function to clean up temp files
223 // $type (rxnorm etc.)
224 function temp_dir_cleanup($type) {
225     if(is_dir($GLOBALS['temporary_files_dir']."/".$type)) {
226         rmdir_recursive($GLOBALS['temporary_files_dir']."/".$type);
227     }
230 // Function to update version tracker table if successful
231 // $type (rxnorm etc.)
232 function update_tracker_table($type,$revision) {
233     if ($type == 'rxnorm') {
234         sqlStatement("INSERT INTO `standardized_tables_track` (`imported_date`,`name`,`revision_date`) VALUES (NOW(),'RXNORM',?)", array($revision) );
235         return true;
236     }
237     else { //$type == 'snomed'
238         sqlStatement("INSERT INTO `standardized_tables_track` (`imported_date`,`name`,`revision_date`) VALUES (NOW(),'SNOMED',?)", array($revision) );
239         return true;
240     }
241     return false;
244 // Function to delete an entire directory
245 function rmdir_recursive($dir) {
246     $files = scandir($dir);
247     array_shift($files);    // remove '.' from array
248     array_shift($files);    // remove '..' from array
250     foreach ($files as $file) {
251         $file = $dir . '/' . $file;
252         if (is_dir($file)) {
253             rmdir_recursive($file);
254             rmdir($file);
255         } else {
256             unlink($file);
257         }
258     }
259     rmdir($dir);