3 * This library contains functions that implement the database load processing
4 * of external database files into openEMR
7 * @link https://www.open-emr.org
8 * @author Rohit Kumar <pandit.rohit@netsity.com>
9 * @author (Mac) Kevin McAloon <mcaloon@patienthealthcareanalytics.com>
10 * @author Brady Miller <brady.g.miller@gmail.com>
11 * @author Roberto Vasquez <robertogagliotta@gmail.com>
12 * @copyright Copyright (c) 2011 Phyaura, LLC <info@phyaura.com>
13 * @copyright Copyright (c) 2019 Brady Miller <brady.g.miller@gmail.com>
14 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
17 // Function to copy a package to temp
18 // $type (RXNORM, SNOMED etc.)
19 function temp_copy($filename, $type)
22 if (!file_exists($filename)) {
26 if (!file_exists($GLOBALS['temporary_files_dir']."/".$type)) {
27 if (!mkdir($GLOBALS['temporary_files_dir']."/".$type, 0777, true)) {
32 if (copy($filename, $GLOBALS['temporary_files_dir']."/".$type."/".basename($filename))) {
39 // Function to unarchive a package
40 // $type (RXNORM, SNOMED etc.)
41 function temp_unarchive($filename, $type)
43 $filename = $GLOBALS['temporary_files_dir']."/".$type."/".basename($filename);
44 if (!file_exists($filename)) {
47 // let's uzip the file
48 // use checksums to determine the "version"
50 $zip = new ZipArchive;
51 if ($zip->open($filename) === true) {
52 if (!($zip->extractTo($GLOBALS['temporary_files_dir']."/".$type))) {
64 // Function to import the RXNORM tables
65 // $is_windows_flag - pass the IS_WINDOWS constant
66 function rxnorm_import($is_windows_flag)
70 $dirScripts = $GLOBALS['temporary_files_dir']."/RXNORM/scripts/mysql";
71 $dir = $GLOBALS['temporary_files_dir']."/RXNORM/rrf";
72 $dir=str_replace('\\', '/', $dir);
75 $rx_info['rxnatomarchive'] = array('title' => "Archive Data", 'dir' => "$dir", 'origin' => "RXNATOMARCHIVE.RRF", 'filename' => "RXNATOMARCHIVE.RRF", 'table' => "rxnatomarchive", 'required' => 0);
76 $rx_info['rxnconso'] = array('title' => "Concept Names and Sources", 'dir' => "$dir", 'origin' => "RXNCONSO.RRF", 'filename' => "RXNCONSO.RRF", 'table' => "rxnconso", 'required' => 1);
77 $rx_info['rxncui'] = array('title' => "Retired RXCUI Data", 'dir' => "$dir", 'origin' => "RXNCUI.RRF", 'filename' => "RXNCUI.RRF", 'table' => "rxncui", 'required' => 1);
78 $rx_info['rxncuichanges'] = array('title' => "Concept Changes", 'dir' => "$dir", 'origin' => "RXNCUICHANGES.RRF", 'filename' => "RXNCUICHANGES.RRF", 'table' => "rxncuichanges", 'required' => 1);
79 $rx_info['rxndoc'] = array('title' => "Documentation for Abbreviated Values", 'dir' => "$dir", 'origin' => "RXNDOC.RRF", 'filename' => "RXNDOC.RRF", 'table' => "rxndoc", 'required' => 1);
80 $rx_info['rxnrel'] = array('title' => "Relationships", 'dir' => "$dir", 'origin' => "RXNREL.RRF", 'filename' => "RXNREL.RRF", 'table' => "rxnrel", 'required' => 1);
81 $rx_info['rxnsab'] = array('title' => "Source Information", 'dir' => "$dir", 'origin' => "RXNSAB.RRF", 'filename' => "RXNSAB.RRF", 'table' => "rxnsab", 'required' => 0);
82 $rx_info['rxnsat'] = array('title' => "Simple Concept and Atom Attributes", 'dir' => "$dir", 'origin' => "RXNSAT.RRF", 'filename' => "RXNSAT.RRF", 'table' => "rxnsat", 'required' => 0);
83 $rx_info['rxnsty'] = array('title' => "Semantic Types ", 'dir' => "$dir", 'origin' => "RXNSTY.RRF", 'filename' => "RXNSTY.RRF", 'table' => "rxnsty", 'required' => 1);
86 $file_load = file_get_contents($dirScripts.'/Table_scripts_mysql_rxn.sql', true);
87 if ($is_windows_flag) {
88 $data_load = file_get_contents($dirScripts.'/Load_scripts_mysql_rxn_win.sql', true);
90 $data_load = file_get_contents($dirScripts.'/Load_scripts_mysql_rxn_unix.sql', true);
93 $indexes_load = file_get_contents($dirScripts.'/Indexes_mysql_rxn.sql', true);
96 // Creating the structure for table and applying indexes
99 $file_array=explode(";", $file_load);
100 foreach ($file_array as $val) {
101 if (trim($val)!='') {
102 sqlStatementNoLog($val);
106 $indexes_array=explode(";", $indexes_load);
108 foreach ($indexes_array as $val1) {
109 if (trim($val1)!='') {
110 sqlStatementNoLog($val1);
115 // Settings to drastically speed up import with InnoDB
116 sqlStatementNoLog("SET autocommit=0");
117 sqlStatementNoLog("START TRANSACTION");
118 $data=explode(";", $data_load);
119 foreach ($data as $val) {
120 foreach ($rx_info as $key => $value) {
121 $file_name= $value['origin'];
122 $replacement=$dir."/".$file_name;
124 $pattern='/'.$file_name.'/';
125 if (strpos($val, $file_name) !== false) {
126 $val1 = str_replace($file_name, $replacement, $val);
127 if (trim($val1)!='') {
128 sqlStatementNoLog($val1);
134 // Settings to drastically speed up import with InnoDB
135 sqlStatementNoLog("COMMIT");
136 sqlStatementNoLog("SET autocommit=1");
141 // Function to import SNOMED tables
142 function snomed_import($us_extension = false)
146 $table_array_for_snomed=array(
147 "sct_concepts_drop"=>"DROP TABLE IF EXISTS `sct_concepts`",
148 "sct_concepts_structure"=>"CREATE TABLE IF NOT EXISTS `sct_concepts` (
149 `ConceptId` bigint(20) NOT NULL,
150 `ConceptStatus` int(11) NOT NULL,
151 `FullySpecifiedName` varchar(255) NOT NULL,
152 `CTV3ID` varchar(5) NOT NULL,
153 `SNOMEDID` varchar(8) NOT NULL,
154 `IsPrimitive` tinyint(1) NOT NULL,
155 PRIMARY KEY (`ConceptId`)
157 "sct_descriptions_drop"=>"DROP TABLE IF EXISTS `sct_descriptions`",
158 "sct_descriptions_structure"=>"CREATE TABLE IF NOT EXISTS `sct_descriptions` (
159 `DescriptionId` bigint(20) NOT NULL,
160 `DescriptionStatus` int(11) NOT NULL,
161 `ConceptId` bigint(20) NOT NULL,
162 `Term` varchar(255) NOT NULL,
163 `InitialCapitalStatus` tinyint(1) NOT NULL,
164 `DescriptionType` int(11) NOT NULL,
165 `LanguageCode` varchar(8) NOT NULL,
166 PRIMARY KEY (`DescriptionId`)
168 "sct_relationships_drop"=>"DROP TABLE IF EXISTS `sct_relationships`",
169 "sct_relationships_structure"=>"CREATE TABLE IF NOT EXISTS `sct_relationships` (
170 `RelationshipId` bigint(20) NOT NULL,
171 `ConceptId1` bigint(20) NOT NULL,
172 `RelationshipType` bigint(20) NOT NULL,
173 `ConceptId2` bigint(20) NOT NULL,
174 `CharacteristicType` int(11) NOT NULL,
175 `Refinability` int(11) NOT NULL,
176 `RelationshipGroup` int(11) NOT NULL,
177 PRIMARY KEY (`RelationshipId`)
182 $dir_snomed = $GLOBALS['temporary_files_dir']."/SNOMED/";
183 $sub_path="Terminology/Content/";
185 $dir=str_replace('\\', '/', $dir);
187 // executing the create statement for tables, these are defined in snomed_capture.inc file
188 // this is skipped if the US extension is being added
189 if (!$us_extension) {
190 foreach ($table_array_for_snomed as $val) {
191 if (trim($val)!='') {
197 // reading the SNOMED directory and identifying the files to import and replacing the variables by originals values.
198 if (is_dir($dir) && $handle = opendir($dir)) {
199 while (false !== ($filename = readdir($handle))) {
200 if ($filename != "." && $filename != ".." && !strpos($filename, "zip")) {
201 $path=$dir."".$filename."/".$sub_path;
202 if (!(is_dir($path))) {
203 $path=$dir."".$filename."/RF1Release/".$sub_path;
206 if (is_dir($path) && $handle1 = opendir($path)) {
207 while (false !== ($filename1 = readdir($handle1))) {
208 $load_script="Load data local infile '#FILENAME#' into table #TABLE# fields terminated by '\\t' ESCAPED BY '' lines terminated by '\\n' ignore 1 lines ";
209 $array_replace=array("#FILENAME#","#TABLE#");
210 if ($filename1 != "." && $filename1 != "..") {
211 $file_replace=$path.$filename1;
212 if (strpos($filename1, "Concepts") !== false) {
213 $new_str=str_replace($array_replace, array($file_replace,"sct_concepts"), $load_script);
216 if (strpos($filename1, "Descriptions") !== false) {
217 $new_str=str_replace($array_replace, array($file_replace,"sct_descriptions"), $load_script);
220 if (strpos($filename1, "Relationships") !== false) {
221 $new_str=str_replace($array_replace, array($file_replace,"sct_relationships"), $load_script);
225 sqlStatement($new_str);
241 function drop_old_sct()
243 $array_to_truncate=array(
244 "sct_concepts_drop"=>"DROP TABLE IF EXISTS `sct_concepts`",
245 "sct_descriptions_drop"=>"DROP TABLE IF EXISTS `sct_descriptions`",
246 "sct_relationships_drop"=>"DROP TABLE IF EXISTS `sct_relationships`"
248 foreach ($array_to_truncate as $val) {
249 if (trim($val)!='') {
255 function drop_old_sct2()
257 $array_to_truncate=array(
258 "sct2_concept_drop"=>"DROP TABLE IF EXISTS `sct2_concept`",
259 "sct2_description_drop"=>"DROP TABLE IF EXISTS `sct2_description`",
260 "sct2_identifier_drop"=>"DROP TABLE IF EXISTS `sct2_identifier`",
261 "sct2_relationship_drop"=>"DROP TABLE IF EXISTS `sct2_relationship`",
262 "sct2_statedrelationship_drop"=>"DROP TABLE IF EXISTS `sct2_statedrelationship`",
263 "sct2_textdefinition_drop"=>"DROP TABLE IF EXISTS `sct2_textdefinition`"
265 foreach ($array_to_truncate as $val) {
266 if (trim($val)!='') {
272 function chg_ct_external_torf1()
274 sqlStatement("UPDATE code_types SET ct_external = 2 WHERE ct_key = 'SNOMED'");
275 sqlStatement("UPDATE code_types SET ct_external = 7 WHERE ct_key = 'SNOMED-CT'");
276 sqlStatement("UPDATE code_types SET ct_external = 9 WHERE ct_key = 'SNOMED-PR'");
279 function chg_ct_external_torf2()
281 sqlStatement("UPDATE code_types SET ct_external = 10 WHERE ct_key = 'SNOMED'");
282 sqlStatement("UPDATE code_types SET ct_external = 11 WHERE ct_key = 'SNOMED-CT'");
283 sqlStatement("UPDATE code_types SET ct_external = 12 WHERE ct_key = 'SNOMED-PR'");
286 function snomedRF2_import()
290 $table_array_for_snomed=array(
291 "sct2_concept_drop"=>"DROP TABLE IF EXISTS `sct2_concept`",
292 "sct2_concept_structure"=>"CREATE TABLE IF NOT EXISTS `sct2_concept` (
293 `id` bigint(20) NOT NULL,
294 `effectiveTime` date NOT NULL,
295 `active` int(11) NOT NULL,
296 `moduleId` bigint(20) NOT NULL,
297 `definitionStatusId` bigint(25) NOT NULL,
300 "sct2_description_drop"=>"DROP TABLE IF EXISTS `sct2_description`",
301 "sct2_description_structure"=>"CREATE TABLE IF NOT EXISTS `sct2_description` (
302 `id` bigint(20) NOT NULL,
303 `effectiveTime` date NOT NULL,
304 `active` bigint(11) NOT NULL,
305 `moduleId` bigint(25) NOT NULL,
306 `conceptId` bigint(20) NOT NULL,
307 `languageCode` varchar(8) NOT NULL,
308 `typeId` bigint(25) NOT NULL,
309 `term` varchar(255) NOT NULL,
310 `caseSignificanceId` bigint(25) NOT NULL,
311 PRIMARY KEY (`id`, `active`, `conceptId`)
313 "sct2_identifier_drop"=>"DROP TABLE IF EXISTS `sct2_identifier`",
314 "sct2_identifier_structure"=>"CREATE TABLE IF NOT EXISTS `sct2_identifier` (
315 `identifierSchemeId` bigint(25) NOT NULL,
316 `alternateIdentifier` bigint(25) NOT NULL,
317 `effectiveTime` date NOT NULL,
318 `active` int(11) NOT NULL,
319 `moduleId` bigint(25) NOT NULL,
320 `referencedComponentId` bigint(25) NOT NULL,
321 PRIMARY KEY (`identifierSchemeId`)
323 "sct2_relationship_drop"=>"DROP TABLE IF EXISTS `sct2_relationship`",
324 "sct2_relationship_structure"=>"CREATE TABLE IF NOT EXISTS `sct2_relationship` (
325 `id` bigint(20) NOT NULL,
326 `effectiveTime` date NOT NULL,
327 `active` int(11) NOT NULL,
328 `moduleId` bigint(25) NOT NULL,
329 `sourceId` bigint(20) NOT NULL,
330 `destinationId` bigint(20) NOT NULL,
331 `typeId` bigint(25) NOT NULL,
332 `characteristicTypeId` bigint(25) NOT NULL,
333 `modifierId` bigint(25) NOT NULL,
336 "sct2_statedrelationship_drop"=>"DROP TABLE IF EXISTS `sct2_statedrelationship`",
337 "sct2_statedrelationship_structure"=>"CREATE TABLE IF NOT EXISTS `sct2_statedrelationship` (
338 `id` bigint(20) NOT NULL,
339 `effectiveTime` date NOT NULL,
340 `active` int(11) NOT NULL,
341 `moduleId` bigint(25) NOT NULL,
342 `sourceId` bigint(20) NOT NULL,
343 `destinationId` bigint(20) NOT NULL,
344 `relationshipGroup` int(11) NOT NULL,
345 `typeId` bigint(25) NOT NULL,
348 "sct2_textdefinition_drop"=>"DROP TABLE IF EXISTS `sct2_textdefinition`",
349 "sct2_textdefinition_structure"=>"CREATE TABLE IF NOT EXISTS `sct2_textdefinition` (
350 `id` bigint(20) NOT NULL,
351 `effectiveTime` date NOT NULL,
352 `active` int(11) NOT NULL,
353 `moduleId` bigint(25) NOT NULL,
354 `conceptId` bigint(20) NOT NULL,
355 `languageCode` varchar(8) NOT NULL,
356 `typeId` bigint(25) NOT NULL,
357 `term` varchar(655) NOT NULL,
363 $dir_snomed = $GLOBALS['temporary_files_dir']."/SNOMED/";
364 // $sub_path="Terminology/Content/";
365 $sub_path="Full/Terminology/";
367 $dir=str_replace('\\', '/', $dir);
369 // executing the create statement for tables, these are defined in snomed_capture.inc file
370 // this is skipped if the US extension is being added
371 //if (!$us_extension) {
372 //var_dump($us_extension);
373 foreach ($table_array_for_snomed as $val) {
374 if (trim($val)!='') {
380 // reading the SNOMED directory and identifying the files to import and replacing the variables by originals values.
381 if (is_dir($dir) && $handle = opendir($dir)) {
382 while (false !== ($filename = readdir($handle))) {
383 if ($filename != "." && $filename != ".." && !strpos($filename, "zip")) {
384 $path=$dir."".$filename."/".$sub_path;
385 if (!(is_dir($path))) {
386 $path=$dir."".$filename."/RF2Release/".$sub_path;
388 if (is_dir($path) && $handle1 = opendir($path)) {
389 while (false !== ($filename1 = readdir($handle1))) {
390 $load_script="Load data local infile '#FILENAME#' into table #TABLE# fields terminated by '\\t' ESCAPED BY '' lines terminated by '\\n' ignore 1 lines ";
391 $array_replace=array("#FILENAME#","#TABLE#");
392 if ($filename1 != "." && $filename1 != "..") {
393 $file_replace=$path.$filename1;
394 if (strpos($filename1, "Concept") !== false) {
395 $new_str=str_replace($array_replace, array($file_replace,"sct2_concept"), $load_script);
397 if (strpos($filename1, "Description") !== false) {
398 $new_str=str_replace($array_replace, array($file_replace,"sct2_description"), $load_script);
400 if (strpos($filename1, "Identifier") !== false) {
401 $new_str=str_replace($array_replace, array($file_replace,"sct2_identifier"), $load_script);
403 if (strpos($filename1, "Relationship") !== false) {
404 $new_str=str_replace($array_replace, array($file_replace,"sct2_relationship"), $load_script);
406 if (strpos($filename1, "StatedRelationship") !== false) {
407 $new_str=str_replace($array_replace, array($file_replace,"sct2_statedrelationship"), $load_script);
409 if (strpos($filename1, "TextDefinition") !== false) {
410 $new_str=str_replace($array_replace, array($file_replace,"sct2_textdefinition"), $load_script);
413 sqlStatement($new_str);
426 // Function to import ICD tables $type differentiates ICD 9, 10 and eventually 11 (circa 2018 :-) etc.
428 function icd_import($type)
432 $dir_icd = $GLOBALS['temporary_files_dir']."/".$type."/";
433 $dir=str_replace('\\', '/', $dir_icd);
437 // the incoming array is a metadata array containing keys that substr match to the incoming filename
438 // followed by the field name, position and length of each fixed length text record in the incoming
439 // flat files. There are separate definitions for ICD 9 and 10 based on the type passed in
441 if ($type == 'ICD9') {
442 $incoming['SHORT_DX'] = array('#TABLENAME#' => "icd9_dx_code",
443 '#FLD1#' => "dx_code", '#POS1#' => 1, '#LEN1#' => 5,
444 '#FLD2#' => "short_desc", '#POS2#' => 7, '#LEN2#' => 60);
445 $incoming['SHORT_SG'] = array('#TABLENAME#' => "icd9_sg_code",
446 '#FLD1#' => "sg_code", '#POS1#' => 1, '#LEN1#' => 4,
447 '#FLD2#' => "short_desc", '#POS2#' => 6, '#LEN2#' => 60);
448 $incoming['LONG_SG'] = array('#TABLENAME#' => "icd9_sg_long_code",
449 '#FLD1#' => "sg_code", '#POS1#' => 1, '#LEN1#' => 4,
450 '#FLD2#' => "long_desc", '#POS2#' => 6, '#LEN2#' => 300);
451 $incoming['LONG_DX'] = array('#TABLENAME#' => "icd9_dx_long_code",
452 '#FLD1#' => "dx_code", '#POS1#' => 1, '#LEN1#' => 5,
453 '#FLD2#' => "long_desc", '#POS2#' => 7, '#LEN2#' => 300);
455 $incoming['icd10pcs_order_'] = array('#TABLENAME#' => "icd10_pcs_order_code",
456 '#FLD1#' => "pcs_code", '#POS1#' => 7, '#LEN1#' => 7,
457 '#FLD2#' => "valid_for_coding", '#POS2#' => 15, '#LEN2#' => 1,
458 '#FLD3#' => "short_desc", '#POS3#' => 17, '#LEN3#' => 60,
459 '#FLD4#' => "long_desc", '#POS4#' => 78, '#LEN4#' => 300);
460 $incoming['icd10cm_order_'] = array('#TABLENAME#' => "icd10_dx_order_code",
461 '#FLD1#' => "dx_code", '#POS1#' =>7, '#LEN1#' => 7,
462 '#FLD2#' => "valid_for_coding", '#POS2#' => 15, '#LEN2#' => 1,
463 '#FLD3#' => "short_desc", '#POS3#' => 17, '#LEN3#' => 60,
464 '#FLD4#' => "long_desc", '#POS4#' => 78, '#LEN4#' => 300);
465 $incoming['reimb_map_pr_'] = array('#TABLENAME#' => "icd10_reimbr_pcs_9_10",
466 '#FLD1#' => "code", '#POS1#' => 1, '#LEN1#' => 7,
467 '#FLD2#' => "code_cnt", '#POS2#' => 9, '#LEN2#' => 1,
468 '#FLD3#' => "ICD9_01", '#POS3#' => 11, '#LEN3#' => 5,
469 '#FLD4#' => "ICD9_02", '#POS4#' => 17, '#LEN4#' => 5,
470 '#FLD5#' => "ICD9_03", '#POS5#' => 23, '#LEN5#' => 5,
471 '#FLD6#' => "ICD9_04", '#POS6#' => 29, '#LEN6#' => 5,
472 '#FLD7#' => "ICD9_05", '#POS7#' => 35, '#LEN7#' => 5,
473 '#FLD8#' => "ICD9_06", '#POS8#' => 41, '#LEN8#' => 5);
474 $incoming['reimb_map_dx_'] = array('#TABLENAME#' => "icd10_reimbr_dx_9_10",
475 '#FLD1#' => "code", '#POS1#' => 1, '#LEN1#' => 7,
476 '#FLD2#' => "code_cnt", '#POS2#' => 9, '#LEN2#' => 1,
477 '#FLD3#' => "ICD9_01", '#POS3#' => 11, '#LEN3#' => 5,
478 '#FLD4#' => "ICD9_02", '#POS4#' => 17, '#LEN4#' => 5,
479 '#FLD5#' => "ICD9_03", '#POS5#' => 23, '#LEN5#' => 5,
480 '#FLD6#' => "ICD9_04", '#POS6#' => 29, '#LEN6#' => 5,
481 '#FLD7#' => "ICD9_05", '#POS7#' => 35, '#LEN7#' => 5,
482 '#FLD8#' => "ICD9_06", '#POS8#' => 41, '#LEN8#' => 5);
483 $incoming['I10gem'] = array('#TABLENAME#' => "icd10_gem_dx_10_9",
484 '#FLD1#' => "dx_icd10_source", '#POS1#' => 1, '#LEN1#' => 7,
485 '#FLD2#' => "dx_icd9_target", '#POS2#' => 9, '#LEN2#' => 5,
486 '#FLD3#' => "flags", '#POS3#' => 15, '#LEN3#' => 5);
487 $incoming['I9gem'] = array('#TABLENAME#' => "icd10_gem_dx_9_10",
488 '#FLD1#' => "dx_icd9_source", '#POS1#' => 1, '#LEN1#' => 5,
489 '#FLD2#' => "dx_icd10_target", '#POS2#' => 7, '#LEN2#' => 7,
490 '#FLD3#' => "flags", '#POS3#' => 15, '#LEN3#' => 5);
491 $incoming['gem_pcsi9'] = array('#TABLENAME#' => "icd10_gem_pcs_10_9",
492 '#FLD1#' => "pcs_icd10_source", '#POS1#' => 1, '#LEN1#' => 7,
493 '#FLD2#' => "pcs_icd9_target", '#POS2#' => 9, '#LEN2#' => 5,
494 '#FLD3#' => "flags", '#POS3#' => 15, '#LEN3#' => 5);
495 $incoming['gem_i9pcs'] = array('#TABLENAME#' => "icd10_gem_pcs_9_10",
496 '#FLD1#' => "pcs_icd9_source", '#POS1#' => 1, '#LEN1#' => 5,
497 '#FLD2#' => "pcs_icd10_target", '#POS2#' => 7, '#LEN2#' => 7,
498 '#FLD3#' => "flags", '#POS3#' => 15, '#LEN3#' => 5);
501 // set up the start of the load script to be appended from the incoming array defined above where incoming
503 $db_load = "LOAD DATA LOCAL INFILE '#INFILE#' INTO TABLE #TABLENAME# FIELDS TERMINATED BY '\0' (@var) SET revision = 0, ";
504 $col_template = "#FLD# = trim(Substring(@var, #POS#, #LEN#))";
506 // load all data and set active revision
507 if (is_dir($dir) && $handle = opendir($dir)) {
508 while (false !== ($filename = readdir($handle))) {
509 // bypass unwanted entries
510 if (!stripos($filename, ".txt") || stripos($filename, "diff") || stripos($filename, "addenda")) {
514 // reset the sql load command and susbtitute the filename
516 $run_sql = str_replace("#INFILE#", $dir . $filename, $run_sql);
517 $keys = array_keys($incoming);
518 while ($this_key = array_pop($keys)) {
519 if (stripos($filename, $this_key) !== false) {
520 // now substitute the tablename
521 $run_sql = str_replace("#TABLENAME#", $incoming[$this_key]['#TABLENAME#'], $run_sql);
523 // the range defines the maximum number of fields contained
524 // in any of the incoming files
525 foreach (range(1, 8) as $field) {
526 $fld = "#FLD" . $field . "#";
527 $nxtfld = "#FLD" . ($field+1) . "#";
528 $pos = "#POS" . $field . "#";
529 $len = "#LEN" . $field . "#";
531 // concat this fields template in the sql string
532 $run_sql .= $col_template;
533 $run_sql = str_replace("#FLD#", $incoming[$this_key][$fld], $run_sql);
534 $run_sql = str_replace("#POS#", $incoming[$this_key][$pos], $run_sql);
535 $run_sql = str_replace("#LEN#", $incoming[$this_key][$len], $run_sql);
536 // at the end of this table's field list
537 if (!array_key_exists($nxtfld, $incoming[$this_key])) {
544 sqlStatement($run_sql);
546 // now update the revision for this load
547 $res = sqlStatement("SELECT max(revision) rev FROM " . escape_table_name($incoming[$this_key]['#TABLENAME#']));
548 $row = sqlFetchArray($res);
549 $next_rev = $row['rev'] + 1;
550 $run_sql = "UPDATE " . $incoming[$this_key]['#TABLENAME#'] . " SET active = 0";
552 $run_sql = "UPDATE " . $incoming[$this_key]['#TABLENAME#'] . " SET active = 1, revision = ? WHERE revision = 0";
553 sqlQuery($run_sql, array($next_rev));
561 echo htmlspecialchars(xl('ERROR: No ICD import directory.'), ENT_NOQUOTES)."<br>";
565 // now update the tables where necessary
566 if ($type == 'ICD9') {
567 sqlStatement("update `icd9_dx_code` SET formatted_dx_code = dx_code");
568 sqlStatement("update `icd9_dx_code` SET formatted_dx_code = concat(concat(left(dx_code, 3), '.'), substr(dx_code, 4)) WHERE dx_code RLIKE '^[V0-9]{1}.*' AND LENGTH(dx_code) > 3");
569 sqlStatement("update `icd9_dx_code` SET formatted_dx_code = concat(concat(left(dx_code, 4), '.'), substr(dx_code, 5)) WHERE dx_code RLIKE '^[E]{1}.*' AND LENGTH(dx_code) > 4");
570 sqlStatement("update `icd9_sg_code` SET formatted_sg_code = concat(concat(left(sg_code, 2), '.'), substr(sg_code, 3))");
571 sqlStatement("update `icd9_dx_code` A, `icd9_dx_long_code` B set A.long_desc = B.long_desc where A.dx_code = B.dx_code and A.active = 1 and A.long_desc is NULL");
572 sqlStatement("update `icd9_sg_code` A, `icd9_sg_long_code` B set A.long_desc = B.long_desc where A.sg_code = B.sg_code and A.active = 1 and A.long_desc is NULL");
574 sqlStatement("update `icd10_dx_order_code` SET formatted_dx_code = dx_code");
575 sqlStatement("update `icd10_dx_order_code` SET formatted_dx_code = concat(concat(left(dx_code, 3), '.'), substr(dx_code, 4)) WHERE LENGTH(dx_code) > 3");
581 function valueset_import($type)
583 $dir_valueset = $GLOBALS['temporary_files_dir']."/".$type."/";
584 $dir = str_replace('\\', '/', $dir_valueset);
586 // Settings to drastically speed up import with InnoDB
587 sqlStatementNoLog("SET autocommit=0");
588 sqlStatementNoLog("START TRANSACTION");
589 if (is_dir($dir) && $handle = opendir($dir)) {
590 while (false !== ($filename = readdir($handle))) {
591 if (stripos($filename, ".xml")) {
592 $abs_path = $dir.$filename;
593 $xml = simplexml_load_file($abs_path, null, null, 'ns0', true);
594 foreach ($xml->DescribedValueSet as $vset) {
595 $vset_attr = $vset->attributes();
596 $nqf = $vset->xpath('ns0:Group[@displayName="NQF Number"]/ns0:Keyword');
597 foreach ($vset->ConceptList as $cp) {
598 foreach ($nqf as $nqf_code) {
599 foreach ($cp->Concept as $con) {
600 $con_attr = $con->attributes();
601 sqlStatementNoLog("INSERT INTO valueset values(?,?,?,?,?,?,?) on DUPLICATE KEY UPDATE code_system = values(code_system),description = values(description),valueset_name = values(valueset_name)", array($nqf_code,$con_attr->code,$con_attr->codeSystem,$con_attr->codeSystemName,$vset_attr->ID,$con_attr->displayName,$vset_attr->displayName));
607 sqlStatementNoLog("UPDATE valueset set code_type='SNOMED CT' where code_type='SNOMEDCT'");
608 sqlStatementNoLog("UPDATE valueset set code_type='ICD9' where code_type='ICD9CM'");
609 sqlStatementNoLog("UPDATE valueset set code_type='ICD10' where code_type='ICD10CM'");
614 // Settings to drastically speed up import with InnoDB
615 sqlStatementNoLog("COMMIT");
616 sqlStatementNoLog("SET autocommit=1");
620 // Function to clean up temp files
621 // $type (RXNORM etc.)
622 function temp_dir_cleanup($type)
624 if (is_dir($GLOBALS['temporary_files_dir']."/".$type)) {
625 rmdir_recursive($GLOBALS['temporary_files_dir']."/".$type);
629 // Function to update version tracker table if successful
630 // $type (RXNORM etc.)
631 function update_tracker_table($type, $revision, $version, $file_checksum)
633 if ($type == 'RXNORM') {
634 sqlStatement("INSERT INTO `standardized_tables_track` (`imported_date`,`name`,`revision_date`, `revision_version`, `file_checksum`) VALUES (NOW(),'RXNORM',?,?,?)", array($revision, $version, $file_checksum));
636 } else if ($type == 'SNOMED') {
637 sqlStatement("INSERT INTO `standardized_tables_track` (`imported_date`,`name`,`revision_date`, `revision_version`, `file_checksum`) VALUES (NOW(),'SNOMED',?,?,?)", array($revision, $version, $file_checksum));
639 } else if ($type == 'ICD9') {
640 sqlStatement("INSERT INTO `standardized_tables_track` (`imported_date`,`name`,`revision_date`, `revision_version`, `file_checksum`) VALUES (NOW(),'ICD9',?,?,?)", array($revision, $version, $file_checksum));
642 } else if ($type == 'CQM_VALUESET') {
643 sqlStatement("INSERT INTO `standardized_tables_track` (`imported_date`,`name`,`revision_date`, `revision_version`, `file_checksum`) VALUES (NOW(),'CQM_VALUESET',?,?,?)", array($revision, $version, $file_checksum));
645 } else { // $type == 'ICD10')
646 sqlStatement("INSERT INTO `standardized_tables_track` (`imported_date`,`name`,`revision_date`, `revision_version`, `file_checksum`) VALUES (NOW(),'ICD10',?,?,?)", array($revision, $version, $file_checksum));
653 // Function to delete an entire directory
654 function rmdir_recursive($dir)
656 $files = scandir($dir);
657 array_shift($files); // remove '.' from array
658 array_shift($files); // remove '..' from array
660 foreach ($files as $file) {
661 $file = $dir . '/' . $file;
663 rmdir_recursive($file);
673 // function to cleanup temp, copy and unarchive the zip file
674 function handle_zip_file($mode, $file)
676 // 1. copy the file to temp directory
677 if (!temp_copy($file, $mode)) {
678 echo htmlspecialchars(xl('ERROR: Unable to copy the file.'), ENT_NOQUOTES)."<br>";
679 temp_dir_cleanup($mode);
683 // 2. unarchive the file
684 if (!temp_unarchive($file, $mode)) {
685 echo htmlspecialchars(xl('ERROR: Unable to extract the file.'), ENT_NOQUOTES)."<br>";
686 temp_dir_cleanup($mode);