This repository has been archived by the owner on Feb 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_filter.php
executable file
·113 lines (99 loc) · 2.93 KB
/
search_filter.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
<?php
/*
* Génère le gexf des scholars à partir de la base sqlite
*/
include("parametres.php");
//include("../common/library/fonctions_php.php");
include("normalize.php");
$base = new PDO("sqlite:" . $dbname);
$category = trim(strtolower($_GET['category']));
$term = trim(strtolower($_GET['term']));
$q = "%".sanitize_input($term)."%";
$cat = '';
$query = '';
if ($category == 'country' || $category == 'countries') {
$cat = "country";
$query = 'LIKE upper(\''.strtoupper($q).'\')';
} elseif ($category == 'organization' || $category == 'organizations') {
$cat = "affiliation";
$query = 'LIKE upper(\''.strtoupper($q).'\')';
} elseif ($category == 'keyword' || $category == 'keywords') {
$cat = "keywords";
$query = 'LIKE upper(\''.strtoupper($q).'\')';
} elseif ($category == 'tag' || $category == 'tags') {
$cat = "tags";
$query = 'LIKE upper(\''.strtoupper($q).'\')';
} elseif ($category == 'labs' || $category == 'laboratories' || $category == 'laboratory') {
$cat = "lab";
$query = 'LIKE upper(\''.strtoupper($q).'\')';
} else {
echo ("ERROR");
exit();
}
$filtered = array (
"yes", "1", "0", "nvgfpmeilym", "no", "mr", "ms", "", " ", " "
);
function filter_word($value) {
if ($value == null) return true;
return ! in_array(strtolower($value),$filtered);
}
$req = "SELECT ".$cat." AS key, count(".$cat.") AS value FROM scholars WHERE ".$cat." ".$query." GROUP BY ".$cat." ORDER BY value DESC";
$results = array();
$i = 0;
foreach ($base->query($req) as $row) {
$nb = $row['value'];
if ($cat == "keywords" || $cat == "tags") {
//echo "in keywords\n";
$words = explode(",", $row["key"]);
foreach ($words as $word) {
$pos = strpos($word,$term);
if($pos === false) {
continue;
}
//echo "match found\n";
// echo "(".$value." contains ".$term." ?)";
if (filter_word($word)) {
if (array_key_exists($word, $results)) {
$results[ $word ] += intval($nb);
} else {
$results[ $word ] = intval($nb);
}
}
}
} else {
$word = $row["key"];
if ($cat == "country") {
$word = normalize_country($word);
}
if (filter_word($word)) {
if (array_key_exists($word, $results)) {
$results[ $word ] += intval($nb);
} else {
$results[ $word ] = intval($nb);
}
}
}
}
$nbresults = sizeof($results);
$results = array_slice($results,0,20);
$nbresults2 = sizeof($results);
$completion = array(
"results" => array()
);
foreach($results as $key => $value) {
array_push($completion["results"], array(
'id' => $key,
'label' => $key,
// 'value' => $value,
'score' => $value,
// F*** it, I'll put the meta data here...
'category' => $cat,
"term" => $term,
"size" => $nbresults2,
"total" => $nbresults,
"remaining" => ($nbresults - $nbresults2)
));
}
$i = 0;
echo json_encode($completion);
?>