-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConceptSearchFactory.inc.php
executable file
·1371 lines (1244 loc) · 48.4 KB
/
ConceptSearchFactory.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once(MCL_ROOT . 'Collection.inc.php');
require_once(MCL_ROOT . 'ConceptSearchGroup.inc.php');
require_once(MCL_ROOT . 'ConceptSearchTermCollection.inc.php');
require_once(MCL_ROOT . 'ConceptSearchTerm.inc.php');
require_once(MCL_ROOT . 'ConceptSearchResultsGroup.inc.php');
require_once(MCL_ROOT . 'ConceptSearchResults.inc.php');
require_once(MCL_ROOT . 'ConceptCollection.inc.php');
require_once(MCL_ROOT . 'Concept.inc.php');
require_once(MCL_ROOT . 'ConceptMapping.inc.php');
require_once(MCL_ROOT . 'ConceptDescription.inc.php');
require_once(MCL_ROOT . 'ConceptName.inc.php');
require_once(MCL_ROOT . 'ConceptSearchSqlCollection.inc.php');
/**
* ConceptSearchFactory is used to search the concept dictionary.
*/
class ConceptSearchFactory
{
/**
* Whether to display debug code.
*/
public $debug = false;
/**
* Whether to display verbose information.
*/
public $verbose = false;
/**
* ConceptSearchSourceCollection object that contains definitions of all
* concept sources that ConceptSearch can search.
*/
private $coll_source = null;
/**
* Array of search term objects.
*/
private $arr_search_term = array();
/**
* Constructor
*/
public function __construct() {
// do nothing
}
/**
* Set ConceptSearchSourceCollection object
* @param ConceptSearchSourceCollection $coll_source
*/
public function setConceptSearchSourceCollection(ConceptSearchSourceCollection $coll_source)
{
$this->coll_source = $coll_source;
}
/**
* Execute the search described in the ConceptSearch object.
* @param ConceptSearch $cs
* @return ConceptSearchResults
*/
public function search(ConceptSearch $cs)
{
// Display the search groups if in debug mode
if ($this->debug) {
echo '<pre style="font-size:8pt;">', var_dump($cs->arr_search_group), '</pre>';
}
// Start the stopwatch
$start_time = microtime(true);
// Perform the base concept search - this queries just the concept table based on the search criteria
$cc = $this->_loadConcepts($cs);
// TODO: Pagination - possibly through use of temp table on the MCL or default database
// Load meta-data for the concepts returned by _loadConcepts
if ( $cc->getCount() )
{
// Iterate through dictionary sources in the collection
foreach ( $cc->getDictionarySources() as $dict_db )
{
$css_dict = $cs->getAllSources()->getDictionary($dict_db);
// Load all the concept data for this search
if ( $cs->load_descriptions ) $this->_loadConceptDescriptions ( $css_dict , $cs , $cc );
if ( $cs->load_concept_attributes ) $this->_loadConceptAttributes ( $css_dict , $cs , $cc );
if ( $cs->load_qa_sets ) $this->_loadQASets ( $css_dict , $cs , $cc );
if ( $cs->load_mappings ) $this->_loadMappings ( $css_dict , $cs , $cc );
if ( $cs->load_concept_sets ) $this->_loadConceptSets ( $css_dict , $cs , $cc );
if ( $cs->load_concept_list_names ) $this->_loadConceptListNames ( $css_dict , $cs , $cc );
// Load concept names last - this is last so that it can get names for all the dependencies as well
$this->_loadConceptNames( $css_dict , $cs , $cc );
}
// Assign relevancies - after all names, descriptions, mappings loaded
foreach ($cc->getSearchResultsGroupIds() as $csrg_id)
{
$csrg = $cc->getSearchResultsGroup( $csrg_id );
$this->_assignRelevancy($csrg);
//echo $csrg->toString();
}
}
else
{
// Set debug info
if ( $this->debug || $this->verbose ) {
echo '<p>No concepts returned in this search</p>';
}
}
// Stop the stopwatch
$stop_time = microtime(true);
// Creat the search results object
$csr = new ConceptSearchResults( $cs , $cc );
$csr->start_time = $start_time ;
$csr->stop_time = $stop_time ;
return $csr;
}
/**
* Get a ConceptSearchSourceCollection of sources for the specified search group
* @param ConceptSearchGroup $csg
* @param ConceptSearch $cs
* @return ConceptSearchSourceCollection
*/
private function getSearchGroupSources(ConceptSearch $cs, ConceptSearchGroup $csg)
{
if ( $arr_source = $csg->getInlineSources() )
{
// TODO: Get ConceptSearchSource objects for the referenced inline sources
return null;
}
else
{
return $cs->getSelectedSources();
}
}
/**
* Return a ConceptCollection containing the concepts that match the search
* criteria described in the passed ConceptSearch object. It iterates through
* each search group. This function only returns fields from the concept table.
* It is intended to be followed by other functions to retrieve additional
* concept info.
* @param ConceptSearch $cs
* @access private
*/
private function _loadConcepts(ConceptSearch $cs)
{
$cc = new ConceptCollection();
$csrg = null;
$group_i = 0;
// Iterate through ConceptSearchGroup objects, perform base query and
// create the concepts for each
foreach ( array_keys($cs->arr_search_group) as $group_key )
{
$group_i++;
$csg = $cs->arr_search_group[ $group_key ];
$csrg = new ConceptSearchResultsGroup( $csg );
$ctsc = $csg->getSearchTermCollection();
$cc->addGroup( $csrg );
// Get sql statements for this search group
$coll_csg_sql = $this->_buildSqlFromConceptSearchGroup( $cs , $csg );
$csrg->coll_sql_obj = $coll_csg_sql;
// Debug info for the search group
if ( $this->debug || $this->verbose ) {
echo '<p><b>Concept Search Group ' . $group_i . ':</b> ' . $csg->query . '<br><ul>';
}
// Iterate through the sql collection and execute
foreach ($coll_csg_sql->getKeys() as $key)
{
$sql_obj = $coll_csg_sql->Get($key);
// Debug info for sql statements
if ( $this->debug || $this->verbose )
{
echo '</li><strong>' . $sql_obj->source->getKey();
if ($sql_obj->css_sub_list_dict) echo ' - ' . $sql_obj->css_sub_list_dict->getKey();
echo ' : </strong> ';
if ($sql_obj->sql) echo htmlentities($sql_obj->sql);
else echo '<em>[[ EMPTY SEARCH QUERY ]]</em></li>';
}
// Skip if no sql
if ( !$sql_obj->sql ) continue;
// Get the connection and execute
$css_dict = $sql_obj->getDictionarySource();
$conn = $css_dict->getConnection();
$rsc_search = mysql_query( $sql_obj->sql , $conn );
if ( !$rsc_search ) {
trigger_error("could not query db in ConceptSearchFactory::_loadConcepts: " . mysql_error());
}
// Create/get the concepts and add to the collection
while ( $row = mysql_fetch_assoc($rsc_search) )
{
// Create the concept if it does not already exist
$c = null;
if ( !($c = $cc->getConcept($row['concept_id'], $css_dict)) )
{
$c = new Concept(
$row[ 'concept_id' ],
$row[ 'retired' ],
$row[ 'is_set' ],
$row[ 'class_id' ],
$row[ 'class_name' ],
$row[ 'datatype_id' ],
$row[ 'datatype_name' ]
);
$c->uuid = $row['uuid']; // keeping this separate to add version compatibility later
if ( $row['date_created' ] ) $c->setAttribute( 'Date created' , $row['date_created' ] );
if ( $row['retired_by' ] ) $c->setAttribute( 'Date Retired' , $row['retired_by' ] );
if ( $row['date_retired' ] ) $c->setAttribute( 'Retired by' , $row['date_retired' ] );
if ( $row['retire_reason'] ) $c->setAttribute( 'Retire reason' , $row['retire_reason'] );
$c->css_dict = $css_dict;
$cc->addConcept( $c );
}
// Relevancy - assign Fulltext Search relevancy only for now
$relevancy = null;
if ( isset($row['relevancy']) ) $relevancy = $row['relevancy'];
// Add to the search results group
$csrg->addConcept( $c , $relevancy );
}
// Debug/verbose info
if ($this->debug || $this->verbose) {
echo '<ul><li><strong>' . $csrg->getCount($css_dict) . ' concept(s) returned:</strong> ' .
implode(', ', $csrg->getConceptIds($css_dict));
echo '</li></ul></li>';
}
} // end ConceptSearchSqlCollection loop
// End debug output
if ( $this->debug || $this->verbose ) {
echo '</ul></p>';
}
} // end ConceptSearchGroup loop
return $cc;
}
/**
* Assigns relevancy to all visible terms
* NOTE: This is inefficient because it requires an additional pass through ALL concepts.
* Probably better to replace this with individual SQL queries
*/
private function _assignRelevancy(ConceptSearchResultsGroup $csrg)
{
// Prepare arrays of search terms used to assign concept relevancy
$ctsc = $csrg->csg->getSearchTermCollection();
$arr_numeric_term_type = array(
MCL_SEARCH_TERM_TYPE_UUID,
MCL_SEARCH_TERM_TYPE_CONCEPT_ID,
MCL_SEARCH_TERM_TYPE_CONCEPT_ID_RANGE,
MCL_SEARCH_TERM_TYPE_MAP_CODE
);
$arr_numeric_search_term = $ctsc->getSearchTerms($arr_numeric_term_type, null, true);
$arr_text_term_type = array(
MCL_SEARCH_TERM_TYPE_TEXT,
MCL_SEARCH_TERM_TYPE_CONCEPT_NAME
);
$arr_text_search_term = $ctsc->getSearchTerms($arr_text_term_type, null, true);
// Iterate through each dictionary source in the group
foreach ($csrg->getDictionarySources() as $dict_key)
{
// Iterate through concepts in the current dictionary source
$arr_concept_id = $csrg->getVisibleConceptIds( $dict_key );
foreach ($arr_concept_id as $_concept_id)
{
// Get the concept and relevancy
$c = $csrg->getConcept( $_concept_id , $dict_key );
$relevancy = $csrg->getRelevancy($c);
// Bump up relevancy for exact matches: CONCEPT_ID, ID RANGES, MAP_CODE, UUID
if ($relevancy < MCL_MAX_RELEVANCY) {
foreach ($arr_numeric_search_term as $search_term) {
if ($search_term->isMatch($c)) {
$relevancy = MCL_MAX_RELEVANCY;
break;
}
}
}
// Bump up exact name matches: TEXT, NAME
if ($relevancy < MCL_MAX_RELEVANCY)
{
// loop through each concept name
foreach ($c->getConceptNameIds() as $concept_name_id) {
$concept_name = $c->getConceptName($concept_name_id)->name;
if (str_word_count($concept_name) !== count($arr_text_search_term)) break;
$match = true;
foreach ($arr_text_search_term as $search_term) {
if (!$search_term->isMatch($c, MCL_SEARCH_TERM_TYPE_CONCEPT_NAME)) {
$match = false;
break;
}
}
if ($match) {
$relevancy = MCL_MAX_RELEVANCY;
break;
}
}
}
// Set relevancy
$csrg->setRelevancy($c, $relevancy);
} // End concept loop
} // End dictionary source loop
}
/**
* Load the names of the concept lists that are associated with the concepts
* in the concept collection.
* @param ConceptSearchSource $css_dict
* @param ConceptSearch $cs
* @param ConceptCollection $cc
*/
private function _loadConceptListNames(ConceptSearchSource $css_dict, ConceptSearch $cs, ConceptCollection $cc)
{
// TODO: This function needs the MCL connection and should only run if in enhanced mode
// build the sql to determine list membership
$csv_concept_id = $cc->getVisibleConceptIdCsv($css_dict);
$sql_concept_lists =
'select clm.concept_list_id, clm.concept_id ' .
'from mcl.concept_list_map clm ' .
'where clm.concept_id in (' . $csv_concept_id . ')';
if ($this->debug) {
echo '<p><b>Loading parent concept lists for ' . $css_dict->dict_db . ':</b><br> ' . $sql_concept_lists . '</p>';
}
// do the query
$rsc_concept_lists = mysql_query($sql_concept_lists, $css_dict->getConnection());
if (!$rsc_concept_lists) {
trigger_error("could not query db in ConceptSearchFactory::_loadConceptListNames: " . mysql_error(), E_USER_ERROR);
}
// Add concept lists to the concepts
$arr_concept_list_id = array(); // stores unique ids for just the current dictionary source
while ($row = mysql_fetch_assoc($rsc_concept_lists))
{
if ( $c = $cc->getConcept($row['concept_id'], $css_dict) )
{
$_concept_list_id = $row['concept_list_id'];
$c->addConceptList($_concept_list_id);
$arr_concept_list_id[$_concept_list_id] = $_concept_list_id;
}
}
}
/**
* Load concept attributes, such as numeric ranges.
* @param ConceptSearchSource $css_dict
* @param ConceptSearch $cs
* @param ConceptCollection $cc
*/
private function _loadConceptAttributes(ConceptSearchSource $css_dict, ConceptSearch $cs, ConceptCollection $cc)
{
// build the sql to load concept numeric ranges
$csv_concept_id = $cc->getVisibleConceptIdCsv($css_dict);
$sql_attr =
'select cnum.concept_id, cnum.hi_absolute, cnum.hi_critical, cnum.hi_normal, ' .
'cnum.low_absolute, cnum.low_critical, cnum.low_normal, ' .
'cnum.units, cnum.precise ' .
'from concept_numeric cnum ' .
'where cnum.concept_id in (' . $csv_concept_id . ')';
if ($this->debug) {
echo '<p><b>Loading concept attributes for ' . $css_dict->dict_db . ':</b><br> ' . $sql_attr . '</p>';
}
$rsc_attr = mysql_query($sql_attr, $css_dict->getConnection());
if (!$rsc_attr) {
echo "could not query db in ConceptSearchFactory::_loadConceptAttributes: " . mysql_error();
}
// Add numeric range to the concepts
while ($row = mysql_fetch_assoc($rsc_attr)) {
$_concept_id = $row['concept_id'];
$c = $cc->getConcept($_concept_id, $css_dict);
if ($c) {
$range = new ConceptNumericRange(
$row[ 'hi_absolute' ],
$row[ 'hi_critical' ],
$row[ 'hi_normal' ],
$row[ 'low_absolute' ],
$row[ 'low_critical' ],
$row[ 'low_normal' ],
$row[ 'units' ],
$row[ 'precise' ]
);
$c->setNumericRange($range);
}
}
}
/**
* Load concept sets.
* @param ConceptSearchSource $css_dict
* @param ConceptSearch $cs
* @param ConceptCollection $cc
*/
private function _loadConceptSets(ConceptSearchSource $css_dict, ConceptSearch $cs, ConceptCollection $cc)
{
// load the ENTIRE concept set hierarchy for this dictionary source
$sql_concept_set =
'select concept_id, concept_set ' .
'from concept_set ' .
'order by sort_weight';
if ($this->debug) {
echo '<p><b>Loading concept set hierarchy for ' . $css_dict->dict_db . ':</b><br> ' . $sql_concept_set . '</p>';
}
$rsc_concept_set = mysql_query($sql_concept_set, $css_dict->getConnection());
if (!$rsc_concept_set) {
echo "could not query db in ConceptSearchFactory::_loadConceptSets: " . mysql_error();
}
// build the ENTIRE concept set hierarchy for this dictionary source
$arr_child_parent = array();
$arr_parent_child = array();
while ($row = mysql_fetch_assoc($rsc_concept_set))
{
$child_id = $row[ 'concept_id' ];
$parent_id = $row[ 'concept_set' ];
$arr_child_parent[ $child_id ][] = $parent_id;
$arr_parent_child[ $parent_id ][] = $child_id;
}
// add parents to the children for the concepts in this ConceptCollection
foreach (array_keys($arr_child_parent) as $_child_id)
{
if ( !($_child_concept = $cc->getConcept($_child_id, $css_dict)) )
{
$_child_concept = new Concept($_child_id);
$_child_concept->display = false;
$_child_concept->css_dict = $css_dict;
$cc->addConcept($_child_concept);
}
foreach ($arr_child_parent[$_child_id] as $_parent_id) {
$_child_concept->addParent($_parent_id);
}
}
// add children to the parents for the concepts in this ConceptCollection
foreach (array_keys($arr_parent_child) as $_parent_id)
{
if ( !($_parent_concept = $cc->getConcept($_parent_id, $css_dict)) )
{
$_parent_concept = new Concept($_parent_id);
$_parent_concept->display = false;
$_parent_concept->css_dict = $css_dict;
$cc->addConcept($_parent_concept);
}
foreach ($arr_parent_child[$_parent_id] as $_child_id) {
$_parent_concept->addChild($_child_id);
}
}
}
/**
* Load mappings.
* @param ConceptSearchSource $css_dict
* @param ConceptSearch $cs
* @param ConceptCollection $cc
*/
private function _loadMappings(ConceptSearchSource $css_dict, ConceptSearch $cs, ConceptCollection $cc)
{
// build the sql
$csv_concept_id = $cc->getVisibleConceptIdCsv($css_dict);
$sql_maps =
'select cm.concept_map_id, cm.concept_id, cm.source, ' .
'cm.source_code, cs.name source_name ' .
'from concept_map cm ' .
'left join concept_source cs on cs.concept_source_id = cm.source ' .
'where cm.concept_id in (' . $csv_concept_id . ')';
if ($this->debug) {
echo '<p><b>Loading concept mappings for ' . $css_dict->dict_db . ':</b><br> ' . $sql_maps . '</p>';
}
// do the query
$rsc_maps = mysql_query($sql_maps, $css_dict->getConnection());
if (!$rsc_maps) {
echo "could not query db in ConceptSearchFactory::_loadMappings: " . mysql_error();
}
// Add mappings to the concepts
while ($row = mysql_fetch_assoc($rsc_maps)) {
$_concept_id = $row['concept_id'];
$c = $cc->getConcept($_concept_id, $css_dict);
if ($c) {
$c->addMapping(new ConceptMapping(
$row[ 'concept_map_id' ],
$row[ 'source' ],
$row[ 'source_code' ],
$row[ 'source_name' ]
));
}
}
}
/**
* Load question/answer sets
* @param ConceptSearchSource $css_dict
* @param ConceptSearch $cs
* @param ConceptCollection $cc
*/
private function _loadQASets(ConceptSearchSource $css_dict, ConceptSearch $cs, ConceptCollection $cc)
{
// build the sql
$csv_concept_id = $cc->getVisibleConceptIdCsv($css_dict);
$sql_qa =
'select ca.concept_id, ca.answer_concept ' .
'from concept_answer ca ' .
'where ca.concept_id in (' . $csv_concept_id . ') ' .
'or ca.answer_concept in (' . $csv_concept_id . ')';
if ($this->debug) {
echo '<p><b>Loading Question/Answer Sets for ' . $css_dict->dict_db . ':</b><br> ' . $sql_qa . '</p>';
}
// do the query
$rsc_qa = mysql_query($sql_qa, $css_dict->getConnection());
if (!$rsc_qa) {
echo "could not query db in ConceptSearchFactory::_loadQASets: " . mysql_error();
}
// add questions and answers to the concepts
while ($row = mysql_fetch_assoc($rsc_qa))
{
$_question_id = $row[ 'concept_id' ];
$_answer_id = $row[ 'answer_concept' ];
if ( $question = $cc->getConcept( $_question_id , $css_dict ) )
{
$question->addAnswer($_answer_id);
}
if ( $answer = $cc->getConcept( $_answer_id , $css_dict ) )
{
$answer->addQuestion($_question_id);
}
}
}
/**
* Load the descriptions for only the base concepts (not the dependencies).
* @param ConceptSearchSource $css_dict
* @param ConceptSearch $cs
* @param ConceptCollection $cc
*/
private function _loadConceptDescriptions(ConceptSearchSource $css_dict, ConceptSearch $cs, ConceptCollection $cc)
{
// build sql
$csv_concept_id = $cc->getVisibleConceptIdCsv($css_dict);
$sql_desc =
'select cd.concept_description_id, cd.concept_id, cd.description, cd.locale ' .
'from concept_description cd ' .
'where cd.concept_id in (' . $csv_concept_id . ')';
if ($this->debug) {
echo '<p><b>Loading concept descriptions for ' . $css_dict->dict_db . ':</b><br> ' . $sql_desc . '</p>';
}
// do the query
$rsc_desc = mysql_query($sql_desc, $css_dict->getConnection());
if (!$rsc_desc) {
trigger_error("could not query db in ConceptSearchFactory::_loadConceptDescriptions: " . mysql_error(), E_USER_ERROR);
exit();
}
// add descriptions to the concepts
while ($row = mysql_fetch_assoc($rsc_desc))
{
$_concept_id = $row['concept_id'];
$c = $cc->getConcept($_concept_id, $css_dict);
if ($c) {
$c->addDescription(new ConceptDescription(
$row[ 'concept_description_id' ],
$row[ 'description' ],
$row[ 'locale' ]
));
}
}
}
/**
* Loads the concept names for the base concepts, and any of the concept dependencies.
* @param ConceptSearchSource $css_dict
* @param ConceptSearch $cs
* @param ConceptCollection $cc
*/
private function _loadConceptNames(ConceptSearchSource $css_dict, ConceptSearch $cs, ConceptCollection $cc)
{
// build sql for ALL concepts (not just visible ones)
$csv_concept_id = $cc->getConceptIdCsv( $css_dict ) ;
$csv_qa = $cc->getQAIdCsv ( $css_dict ) ;
$sql_concept_names =
'select cn.concept_name_id, cn.concept_id, cn.name, ' .
'cn.locale, cntm.concept_name_tag_id, cn.uuid ' .
'from concept_name cn ' .
'left join concept_name_tag_map cntm on cntm.concept_name_id = cn.concept_name_id ' .
'where cn.concept_id in (' . $csv_concept_id . ') or ' .
'cn.concept_id in (' .
'select cs1.concept_id from concept_set cs1 ' .
'union ' .
'select cs2.concept_set from concept_set cs2 ' .
') ';
if ( $csv_qa ) {
$sql_concept_names .= 'or cn.concept_id in (' . $csv_qa . ') ';
}
if ( $this->debug ) {
echo '<p><b>Loading concept names and synonyms for ' . $css_dict->dict_db .
':</b><br> ' . $sql_concept_names . '</p>';
}
// do the query
$rsc_concept_names = mysql_query( $sql_concept_names , $css_dict->getConnection() );
if ( !$rsc_concept_names ) {
trigger_error("could not query db in ConceptSearchFactory::_loadConceptNames: " . mysql_error(), E_USER_ERROR);
exit();
}
// add names to the ConceptCollection
while ( $row = mysql_fetch_assoc($rsc_concept_names) )
{
$_concept_id = $row[ 'concept_id' ];
$cn = new ConceptName(
$row[ 'concept_name_id' ],
$row[ 'name' ],
$row[ 'locale' ],
$row[ 'concept_name_tag_id' ]
);
$cn->uuid = $row[ 'uuid' ]; // keeping this separate to add version compatibility later
$c = $cc->getConcept( $_concept_id , $css_dict );
if ( !$c ) {
$c = new Concept($_concept_id) ;
$c->display = false ;
$c->css_dict = $css_dict ;
$cc->addConcept($c);
}
$c->addName($cn);
}
}
/**
* Recursive function used by ConceptSearchFactory::_buildSqlFromConceptSource
* for each ConceptSearchTermCollection to create the criteria (the sql where clause).
* If $apply_cs_filter is true, ConceptSearch filter will still only be applied if
* filter_scope is TEXT and if a filter has been set in ConceptSearch. If false, the
* filter will not be applied regardless of settings, which is the case for recursive
* calls to _buildSqlCriteria.
*/
private function _buildSqlCriteria(
ConceptSearch $cs,
ConceptSearchTermCollection $cstc,
ConceptSearchSource $css,
ConceptSearchSource $css_sub_list_dict = null,
$apply_cs_filter = true
)
{
$arr_criteria = array(); // array of sql criteria to be glued together by cstc->glue
$arr_csg_filter = array(); // explicit CSG level filters, e.g. "in" and "notin" operators
// Set the source dictionary and get the database connection
if ($css_sub_list_dict) $css_dict = $css_sub_list_dict;
else $css_dict = $css;
$conn = $css_dict->getConnection();
// ALL - get all concepts
$is_all_search = false;
// TODO: The 'is_all_search' variable is a complete hack used to apply global filters to
// full list/mapsource queries. There needs to be a better way to split between
// terms that should be filtered and those that should not.
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_ALL))
{
$is_all_search = true;
}
// Child Collections
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_COLLECTION))
{
foreach ($arr_search_term as $search_object) {
$child_apply_cs_filter = false;
if ($search_object->glue == MCL_CONCEPT_SEARCH_GLUE_AND) $child_apply_cs_filter = true;
// TODO: The $child_apply_cs_filter logic is a HUGE hack and needs to be fixed
$arr_criteria[] = $this->_buildSqlCriteria($cs, $search_object,
$css, $css_sub_list_dict, $child_apply_cs_filter);
}
}
// Concept ID -
// Glue = OR : c.concept_id in (...)
// Glue = AND : c.concept_id = ... and c.concept_id = ...
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_CONCEPT_ID))
{
if ($cstc->glue == MCL_CONCEPT_SEARCH_GLUE_OR) {
$arr_concept_id = array();
foreach ($arr_search_term as $search_object) {
$arr_concept_id[] = $search_object->needle;
}
$arr_criteria[] = 'c.concept_id in (' . implode(',',$arr_concept_id) . ')';
}
elseif ($cstc->glue == MCL_CONCEPT_SEARCH_GLUE_AND) {
foreach ($arr_search_term as $search_object) {
$arr_criteria[] = 'c.concept_id = ' . $search_object->needle;
}
}
}
// Concept ID Range
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_CONCEPT_ID_RANGE))
{
foreach ($arr_search_term as $search_object) {
list($min, $max) = $search_object->getRange();
$arr_criteria[] = 'c.concept_id >= ' . $min . ' and c.concept_id <= ' . $max;
}
}
// Map Code - Numeric only
// Glue = OR : c.concept_id in (select cm.concept_id from concept_map cm
// where cm.source_code in (...))
// Glue = AND : c.concept_id in (select cm.concept_id from concept_map cm
// where cm.source_code = ... )
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_MAP_CODE, true))
{
if ($cstc->glue == MCL_CONCEPT_SEARCH_GLUE_OR) {
$arr_map_code_numeric = array();
foreach ($arr_search_term as $search_object) {
$arr_map_code_numeric[] = $search_object->needle;
}
$arr_criteria[] = 'c.concept_id in (select cm.concept_id from concept_map cm ' .
'where cm.source_code in (' . implode(',',$arr_map_code_numeric) . '))';
}
elseif ($cstc->glue == MCL_CONCEPT_SEARCH_GLUE_AND) {
foreach ($arr_search_term as $search_object) {
$arr_criteria[] = 'c.concept_id in (select cm.concept_id from concept_map cm ' .
'where cm.source_code = ' . $search_object->needle . ')';
}
}
}
// Map Code - Text only
// Glue = OR : c.concept_id in (select cm.concept_id from concept_map cm
// where cm.source_code in (...))
// Glue = AND : c.concept_id in (select cm.concept_id from concept_map cm
// where cm.source_code = ... )
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_MAP_CODE, false))
{
foreach ($arr_search_term as $search_term) {
$arr_criteria[] =
"c.concept_id in (select cm.concept_id from concept_map cm " .
"where cm.source_code regexp '[[:<:]]" .
mysql_real_escape_string($search_term->needle, $conn) . "')";
}
}
// Map Code Range
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_MAP_CODE_RANGE))
{
list($min, $max) = $search_object->getRange();
$arr_map_code_numeric = array();
for ($i = $min; $i <= $max; $i++) $arr_map_code_numeric[] = $i;
$arr_criteria[] = 'c.concept_id in (select cm.concept_id from concept_map cm ' .
'where cm.source_code in (' . implode(',',$arr_map_code_numeric) . '))';
}
// Text - concept names AND descriptions (does not use concept_word)
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_TEXT))
{
// If FTS, combine terms and do a single boolean search
if ( $css_dict->dict_fulltext_mode == MCL_FULLTEXT_MODE_ON )
{
$_criteria = '';
foreach ($arr_search_term as $search_term) {
if ($_criteria) $_criteria .= ' ';
$_criteria .= '+' . mysql_real_escape_string($search_term->needle, $conn);
}
if ($_criteria) {
$arr_criteria[] =
"c.concept_id in (select cfts.concept_id from concept_fts cfts " .
"where match(name, description, source_code) against('" . $_criteria . "' in boolean mode) )";
}
}
// Use the concept_name and concept_description tables directly if no fulltext index
// NOTE: each search term becomes 2 criteria: 1 for concept_name and one for concept_description
else {
foreach ($arr_search_term as $search_term)
{
$arr_criteria[] =
"(" .
"c.concept_id in (select cn.concept_id from concept_name cn where cn.name regexp '[[:<:]]" .
mysql_real_escape_string($search_term->needle, $conn) . "')" .
") OR (" .
"c.concept_id in (select cd.concept_id from concept_description cd where cd.description regexp '[[:<:]]" .
mysql_real_escape_string($search_term->needle, $conn) . "')" .
")";
}
}
}
// Concept Name
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_CONCEPT_NAME))
{
foreach ($arr_search_term as $search_term)
{
$arr_criteria[] =
"c.concept_id in (select cn.concept_id from concept_name cn where cn.name regexp '[[:<:]]" .
mysql_real_escape_string($search_term->needle, $conn) . "')";
}
}
// Concept Description
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_CONCEPT_DESCRIPTION))
{
foreach ($arr_search_term as $search_term) {
$arr_criteria[] =
"c.concept_id in (select cdesc.concept_id from concept_description cdesc " .
"where cdesc.description regexp '[[:<:]]" .
mysql_real_escape_string($search_term->needle, $conn) . "')";
}
}
// UUID
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_UUID))
{
if ($cstc->glue == MCL_CONCEPT_SEARCH_GLUE_OR)
{
$arr_uuid_full = array();
$arr_uuid_like = array();
foreach ($arr_search_term as $search_object) {
$_needle = mysql_real_escape_string($search_object->needle, $conn);
if (strlen($search_object->needle) < MCL_UUID_LENGTH) {
$arr_criteria[] = "c.uuid LIKE '" . $_needle . "%'";
} else {
$arr_uuid_full[] = "'" . $_needle . "'";
}
if ($arr_uuid_full) $arr_criteria[] = 'c.uuid in (' . implode(',',$arr_uuid_full) . ')';
}
} elseif ($cstc->glue == MCL_CONCEPT_SEARCH_GLUE_AND) {
foreach ($arr_search_term as $search_object) {
if (strlen($search_object->needle) < MCL_UUID_LENGTH) {
$arr_criteria[] = "c.uuid LIKE '" . $_needle . "%'";
} else {
$arr_criteria[] = "c.uuid = '" . $_needle . "'";
}
}
}
}
// IN inline filter (generically handles concept lists and map sources)
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_IN))
{
foreach ($arr_search_term as $search_term)
{
$css = $cs->getAllSources()->resolveSourceIdentifier(
$search_term->needle,
$cs->getSelectedSources()->getDictionaries(),
MCL_SOURCE_TYPE_LIST | MCL_SOURCE_TYPE_MAP
);
if (!$css) {
continue;
}
elseif ($css->type == MCL_SOURCE_TYPE_LIST)
{
// Treat search_term as concept_list.concept_list_id
$arr_csg_filter[] =
"c.concept_id in (" .
"select clm.concept_id from mcl.concept_list_map clm " .
"where clm.concept_list_id = " . $css->list_id .
" and clm.dict_id = " . $css_dict->dict_id .
")";
}
elseif ($css->type == MCL_SOURCE_TYPE_MAP)
{
// Treat search_term as concept_source.concept_source_id
$arr_csg_filter[] =
"c.concept_id in (" .
"select cm.concept_id from concept_map cm " .
"where cm.source = " . $css->map_source_id .
")";
}
}
}
// NOT IN inline filter (generically handles concept lists and map sources)
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_NOT_IN))
{
foreach ($arr_search_term as $search_term)
{
$css = $cs->getAllSources()->resolveSourceIdentifier(
$search_term->needle,
$cs->getSelectedSources()->getDictionaries(),
MCL_SOURCE_TYPE_LIST | MCL_SOURCE_TYPE_MAP
);
if (!$css) {
continue;
}
elseif ($css->type == MCL_SOURCE_TYPE_LIST)
{
// Treat search_term as concept_list.concept_list_id
$arr_csg_filter[] =
"c.concept_id not in (" .
"select clm.concept_id from mcl.concept_list_map clm " .
"where clm.concept_list_id = " . $css->list_id .
" and clm.dict_id = " . $css_dict->dict_id .
")";
}
elseif ($css->type == MCL_SOURCE_TYPE_MAP)
{
// Treat search_term as concept_source.concept_source_id
$arr_csg_filter[] =
"c.concept_id not in (" .
"select cm.concept_id from concept_map cm " .
"where cm.source = " . $css->map_source_id .
")";
}
}
}
// IN LIST inline filter (concept lists)
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_IN_LIST))
{
foreach ($arr_search_term as $search_term)
{
if ($search_term->isInteger()) {
// Treat search_term as concept_list.concept_list_id
$arr_csg_filter[] =
"c.concept_id in (" .
"select clm.concept_id from mcl.concept_list_map clm " .
"where clm.concept_list_id = " . $search_term->needle .
" and clm.dict_id = " . $css_dict->dict_id .
")";
} else {
// Treat search_term as concept_list.list_name
$arr_csg_filter[] =
"c.concept_id in (" .
"select clm.concept_id from mcl.concept_list_map clm " .
"where clm.concept_list_id = (" .
"select concept_list_id from mcl.concept_list " .
"where lower(list_name) = '" . mysql_real_escape_string($search_term->needle, $conn) . "' " .
") " .
"and clm.dict_id = " . $css_dict->dict_id .
")";
}
}
}
// NOT IN LIST inline filter (concept lists)
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_NOT_IN_LIST))
{
foreach ($arr_search_term as $search_term) {
if ($search_term->isInteger()) {
// Treat search_term as concept_list.concept_list_id
$arr_csg_filter[] =
"c.concept_id not in (" .
"select clm.concept_id from mcl.concept_list_map clm " .
"where clm.concept_list_id = " . $search_term->needle .
" and clm.dict_id = " . $css_dict->dict_id .
")";
} else {
// Treat search_term as concept_list.list_name
$arr_csg_filter[] =
"c.concept_id not in (" .
"select clm.concept_id from mcl.concept_list_map clm " .
"where clm.concept_list_id = (" .
"select concept_list_id from mcl.concept_list " .
"where lower(list_name) = '" . mysql_real_escape_string($search_term->needle, $conn) . "' " .
") " .
"and clm.dict_id = " . $css_dict->dict_id .
")";
}
}
}
// IN SOURCE inline filter (map sources)
if ($arr_search_term = $cstc->getSearchTerms(MCL_SEARCH_TERM_TYPE_IN_SOURCE))
{
foreach ($arr_search_term as $search_term) {
if ($search_term->isInteger()) {
// Treat search_term as concept_source.concept_source_id
$arr_csg_filter[] =
"c.concept_id in (select cm.concept_id from concept_map cm where cm.source = " .