-
Notifications
You must be signed in to change notification settings - Fork 2
/
search.php
235 lines (202 loc) · 7.4 KB
/
search.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
<?php
require_once 'src/TemplateRenderer.class.php';
include('php/wordcloud.class.php');
include_once 'php/database.php';
$connection = connectToDB();
// Database queries
$totalSql = "SELECT COUNT(*) FROM Document";
$docDistSql = "SELECT YEAR(creation) doc_year, COUNT(*) doc_total FROM Document GROUP BY doc_year ORDER BY doc_year";
$authorQuery = "SELECT np.name name, np.id id,
if(
RIGHT(np.name, LOCATE(' ', REVERSE(np.name)) - 1) <>'',
RIGHT(np.name, LOCATE(' ', REVERSE(np.name)) - 1), np.name)
surname, count(*) frequency
FROM NormalizedPerson np
INNER JOIN Document d
ON np.id=d.sentFromPerson
GROUP BY id, name, surname
ORDER BY surname";
$recipientQuery = "SELECT np.name name, np.id id,
if(
RIGHT(np.name, LOCATE(' ', REVERSE(np.name)) - 1) <>'',
RIGHT(np.name, LOCATE(' ', REVERSE(np.name)) - 1), np.name)
surname, count(*) frequency
FROM NormalizedPerson np
INNER JOIN Document d
ON np.id=d.sentToPerson
GROUP BY id, name, surname
ORDER BY surname";
$fromQuery = "SELECT np.name name, np.id id, count(*) frequency
FROM NormalizedPlace np
INNER JOIN Document d
ON np.id=d.sentFromPlace
GROUP BY id, name
ORDER BY name";
$toQuery = "SELECT np.name name, np.id id, count(*) frequency
FROM NormalizedPlace np
INNER JOIN Document d
ON np.id=d.sentToPlace
GROUP BY id, name
ORDER BY name";
$yearQuery = "SELECT left(creation,4) year, count(*) frequency
FROM Document
WHERE creation > '1'
GROUP BY year
ORDER BY 1";
$placesQuery = "SELECT np.name name, np.id id
FROM NormalizedPlace np;";
$peopleQuery = "SELECT np.name name, np.id id
FROM NormalizedPerson np;";
$totalResult = mysqli_query($connection, $totalSql);
$totalDocs = mysqli_result($totalResult, 0);
logString($totalDocs);
$docDistResult = mysqli_query($connection, $docDistSql);
$findAuthor = mysqli_query( $connection, $authorQuery);
$findRecipient = mysqli_query( $connection, $recipientQuery);
$findFrom = mysqli_query( $connection, $fromQuery);
$findTo = mysqli_query( $connection, $toQuery);
$findYears = mysqli_query( $connection, $yearQuery);
$findPeople = mysqli_query( $connection, $peopleQuery);
$findPlaces = mysqli_query( $connection, $placesQuery);
$numAuthors = mysqli_num_rows($findAuthor);
$numRecipients = mysqli_num_rows($findRecipient);
$numFrom = mysqli_num_rows($findFrom);
$numTo = mysqli_num_rows($findTo);
$numYears = mysqli_num_rows($findYears);
$numPeople = mysqli_num_rows($findPeople);
$numPlaces = mysqli_num_rows($findPlaces);
// GET parameters array
$search_params = array();
// Process GET parameters
if(array_key_exists('query', $_GET)) {
$search_params['query'] = $_GET['query'];
}
if (array_key_exists('fromPersonId', $_GET)) {
$search_params['fromPersonId'] = $_GET['fromPersonId'];
}
if (array_key_exists('toPersonId', $_GET)) {
$search_params['toPersonId'] = $_GET['toPersonId'];
}
if (array_key_exists('fromYear', $_GET)) {
$search_params['fromYear'] = $_GET['fromYear'];
}
if (array_key_exists('toYear', $_GET)) {
$search_params['toYear'] = $_GET['toYear'];
}
if (array_key_exists('fromPlaceId', $_GET)) {
$search_params['fromPlaceId'] = $_GET['fromPlaceId'];
}
if (array_key_exists('toPlaceId', $_GET)) {
$search_params['toPlaceId'] = $_GET['toPlaceId'];
}
if (array_key_exists('sentiment', $_GET)) {
$search_params['sentiment'] = $_GET['sentiment'];
}
// Determine total number of documents in database
$docDistDocs = array();
while($docDistRow = $docDistResult->fetch_assoc()) {
$docDistDocs[$docDistRow['doc_year']] = $docDistRow['doc_total'];
}
$docDistDocs = json_encode($docDistDocs);
// Search template dropdown options
$search_dropdowns = array();
$placeIdToNames = array();
$personIdToNames = array();
$fromPersonList = array();
$toPersonList = array();
$yearList = array();
$fromPlaceList = array();
$toPlaceList = array();
$allSentiments = array();
// Generate $placeIdToNames JSON for template
$i=0;
while ($i < $numPlaces) {
$row = mysqli_fetch_array($findPlaces);
$placeId = $row['id'];
$placeName = $row['name'];
$placeIdToNames[$placeId] = $placeName;
$i++;
}
$placeIdToNames = json_encode($placeIdToNames);
// Generate $personIdToNames JSON for template
$i=0;
while ($i < $numPeople) {
$row = mysqli_fetch_array($findPeople);
$personId = $row['id'];
$personName = $row['name'];
$personIdToNames[$personId] = $personName;
$i++;
}
$personIdToNames = json_encode($personIdToNames);
// Function to generate list of authors and recipients
function listPersons($numRows, $queryName) {
$personList = array();
$i=0;
while ($i < $numRows) {
$row = mysqli_fetch_array($queryName);
$personId = $row['id'];
$personName = $row['name'];
$docFrequency = $row['frequency'];
$personList[$personId] = array(
'person_name' => $personName,
'doc_frequency' => $docFrequency,
);
$i++;
}
return $personList;
}
// Generate the lists
$fromPersonList = listPersons($numAuthors, $findAuthor);
$toPersonList = listPersons($numRecipients, $findRecipient);
// Generate list of years
$i=0;
while ($i < $numYears) {
$row = mysqli_fetch_array($findYears);
$year = $row['year'];
$docFrequency = $row['frequency'];
$yearList[$year] = $docFrequency;
$i++;
}
// Function to generate list of places
function listPlaces($numRows, $queryName) {
$placeList = array();
$i=0;
while ($i < $numRows) {
$row = mysqli_fetch_array($queryName);
$placeId = $row['id'];
$placeName = $row['name'];
$docFrequency = $row['frequency'];
$placeList[$placeId] = array(
'place_name' => $placeName,
'doc_frequency' => $docFrequency,
);
$i++;
}
return $placeList;
}
// Generate place lists
$fromPlaceList = listPlaces($numFrom, $findFrom);
$toPlaceList = listPlaces($numTo, $findTo);
// Populate sentiments array
$allSentiments['positive'] = 'positive';
$allSentiments['neutral'] = 'neutral';
$allSentiments['negative'] = 'negative';
$search_dropdowns = array(
'placeIdToNames' => $placeIdToNames,
'personIdToNames' => $personIdToNames,
'fromPersonList' => $fromPersonList,
'toPersonList' => $toPersonList,
'yearList' => $yearList,
'fromPlaceList' => $fromPlaceList,
'toPlaceList' => $toPlaceList,
'allSentiments' => $allSentiments,
);
$template = new TemplateRenderer();
// Include any variables as an array in the second param
print $template->render('search.html.twig', array(
'search_params' => $search_params,
'search_dropdowns' => $search_dropdowns,
'totalDocsCount' => $totalDocs,
'totalDocDistribution' => $docDistDocs,
'body_id' => 'search'
));