/
Librivox_search.php
422 lines (322 loc) · 11.3 KB
/
Librivox_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
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
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Librivox_search{
// this is searching logic for the main, public-facing catalog search
// may be some complications, so keeping it all together here
public function __construct()
{
}
public function __get($var)
{
return get_instance()->$var;
}
/*
*
* Advanced title search. All conditions are AND
*
*
*/
function advanced_title_search($params)
{
$params['like_left'] = '%';
$params['like_right'] = '%';
$sql = '';
/* Title*/
$title = ' ';
if (!empty($params['title']) )
{
$title_search = '%' . str_replace(' ', '%', $params['title']) . '%';
$title = 'AND st.`search_field` LIKE ' . $this->db->escape($title_search);
}
/* Status */
$status = ' ';
if (!empty($params['status']) )
{
$status_group['complete'] = array(PROJECT_STATUS_COMPLETE);
$status_group['in_progress'] = array(PROJECT_STATUS_FULLY_SUBSCRIBED, PROJECT_STATUS_PROOF_LISTENING, PROJECT_STATUS_VALIDATION);
$status_group['open'] = array(PROJECT_STATUS_OPEN);
$status_group['all'] = array(PROJECT_STATUS_COMPLETE, PROJECT_STATUS_OPEN, PROJECT_STATUS_FULLY_SUBSCRIBED, PROJECT_STATUS_PROOF_LISTENING, PROJECT_STATUS_VALIDATION);
$status_array = sprintf('"%s"', implode('", "', $status_group[$params['status']]));
$status = ' AND p.status IN ('. $status_array .') ';
}
/* Project type */
$project_type = ' ';
if (!empty($params['project_type']) && $params['project_type'] != 'either')
{
if ($params['project_type'] == 'solo')
{
$project_type = ' AND p.project_type = "solo"';
}
else
{
$project_type = ' AND p.project_type != "solo"';
}
}
/* Language */
$language = ' ';
$language_clause = ' ';
$section_language_clause = ' ';
$recorded_language = (int)$params['recorded_language'];
if ($recorded_language !== 0)
{
//$language = ' AND p.language_id = ' . $params['recorded_language'] ;
// Projects having at least one section in that language.
$section_project_ids = $this->_get_projects_by_section_language($recorded_language);
//$language_clause = ' AND p.id IN (' . implode(',', $section_project_ids) . ') ';
$language_clause = ' AND ( p.language_id = ' . $recorded_language . ' OR p.id IN (' . implode(',', $section_project_ids) . ') ) ';
$section_language_clause = '
AND ( p.language_id = ' . $recorded_language . '
OR st.section_language_id = ' . $recorded_language . ') ';
}
/* Genre */
$genre_clause = '';
if (!empty($params['genre_id']))
{
$this->load->model('genre_model');
$genre = $this->genre_model->get($params['genre_id']);
$genre_clause = ' JOIN project_genres pg ON (pg.project_id = p.id) ';
$genre_clause .= ' JOIN genres g ON (g.id = pg.genre_id) AND g.lineage LIKE "'. $genre->lineage . '%"';
}
/* Keywords */
$keyword_clause = '';
if (!empty($params['keywords']))
{
$keywords = explode(' ', $params['keywords']); //maybe preg_match if extra spaces cause trouble - thinnk we're ok
$keywords = array_map('trim', $keywords); // clean it up
$escaped_keywords = [];
foreach ($keywords as $keyword)
$escaped_keywords[] = $this->db->escape($keyword);
$in_keywords = implode(", ", $escaped_keywords);
$keyword_clause = ' JOIN project_keywords pk ON (pk.project_id = p.id) ';
$keyword_clause .= ' JOIN keywords k ON (k.id = pk.keyword_id) AND k.value IN ('. $in_keywords .') ';
}
//Do not combine these - they are separate conditions that both must be true
$author_clause = '';
$section_author_clause = '';
if (!empty($params['author']))
{
$params['author'] = str_replace(' ', '%', $params['author']);
$project_ids = $this->_get_projects_by_author($params['author']);
$section_project_ids = $this->_get_projects_by_section_author($params['author']);
$project_ids = array_merge($project_ids, $section_project_ids);
$author_clause = ' AND p.id IN (' . implode(',', $project_ids) . ') ';
$author_ids = $this->_get_author_ids($params['author']);
$section_author_clause = ' AND ( st.source_id IN (' . implode(',', $project_ids) . ') AND st.section_author_id IN (' . implode(',', $author_ids) . ') ) ';
}
$reader_clause = '';
if (!empty($params['reader']))
{
$exact_match = (isset($params['exact_match'])) ? true: false;
$project_ids = $this->_get_projects_by_reader($params['reader'], $exact_match);
$reader_clause = ' AND p.id IN (' . implode(',', $project_ids) . ') ';
}
// ======================================================================================================================================
// Combine all of our results as a sub-query, so we can count the results in one place.
$sql .= 'WITH results AS (';
//***** Projects from compilations *****//
$cols = array();
$cols[] = 'st.source_table';
$cols[] = 'st.search_field';
$cols[] = 'p.title';
$cols[] = 'COALESCE(p.date_catalog, "2001-01-01")';
$cols[] = 'p.id';
$cols[] = 'p.project_type';
$cols[] = 'p.title_prefix';
$cols[] = 'p.url_librivox';
$cols[] = 'p.url_forum';
$cols[] = 'p.status';
$cols[] = 'p.coverart_thumbnail';
$cols[] = 'p.zip_url';
$cols[] = 'p.zip_size';
$cols[] = 'l.language';
$cols[] = 'l.id AS language_id';
$cols[] = 'l.two_letter_code';
$sql .= '
SELECT "title" AS blade, ' . implode(',' , $cols) . '
FROM search_table st
JOIN projects p ON (p.id = st.source_id AND st.source_table IN ("projects" ) )
JOIN languages l ON (l.id = p.language_id ' . $language . ')
' . $genre_clause . '
' . $keyword_clause . '
WHERE 1
' . $status . '
' . $project_type . '
' . $title . '
' . $author_clause . '
' . $reader_clause . '
' . $language_clause . ' ';
//***** Sections from compilations *****//
$cols = array();
$cols[] = 'st.source_table';
$cols[] = 'st.search_field';
$cols[] = 'p.title';
$cols[] = 'COALESCE(p.date_catalog, "2001-01-01")';
$cols[] = 'p.id';
$cols[] = 'p.project_type';
$cols[] = 'p.title_prefix';
$cols[] = 'p.url_librivox';
$cols[] = 'p.url_forum';
$cols[] = 'p.status';
$cols[] = 'p.coverart_thumbnail';
$cols[] = 'p.zip_url';
$cols[] = 'p.zip_size';
$cols[] = 'l.language';
$cols[] = 'l.id AS language_id';
$cols[] = 'l.two_letter_code';
$sql .= '
UNION
SELECT "title" AS blade, ' . implode(',' , $cols) . '
FROM search_table st
JOIN projects p ON (p.id = st.source_id AND st.source_table IN ( "sections" ) )
JOIN languages l ON (l.id = p.language_id ' . $language . ')
' . $genre_clause . '
' . $keyword_clause . '
WHERE 1
' . $status . '
' . $project_type . '
' . $title . '
' . $section_author_clause . '
' . $reader_clause . '
' . $section_language_clause . ' ';
//* Groups *//
unset($cols);
$cols[] = '""';
$cols[] = '""';
$cols[] = 'gr.name AS title';
$cols[] = '"2001-01-01"';
$cols[] = 'gr.id';
$cols[] = '""';
$cols[] = '""';
$cols[] = '""';
$cols[] = '""';
$cols[] = '""';
$cols[] = '""';
$cols[] = '""';
$cols[] = '""';
$cols[] = '""';
$cols[] = '""';
$cols[] = '""';
$sql .= '
UNION
SELECT "group" AS blade,' . implode(',' , $cols) . '
FROM search_table st
JOIN groups gr ON (gr.id = st.source_id AND st.source_table = "groups" )
JOIN group_projects gp ON (gp.group_id = gr.id)
JOIN projects p ON (p.id = gp.project_id)
JOIN languages l ON (l.id = p.language_id ' . $language . ')
' . $genre_clause . '
' . $keyword_clause . '
WHERE 1
' . $status . '
' . $project_type . '
' . $title . '
' . $author_clause . '
' . $reader_clause . '
' . $language_clause . ' ';
//***** finalize query parts *****//
$sql .= ") SELECT *, COUNT(*) OVER() as full_count FROM results";
$sql .= ($params['sort_order'] == 'catalog_date') ? ' ORDER BY 5 DESC ' : ' ORDER BY 4 ASC ';
$sql .= ' LIMIT ' . $params['offset'] . ', ' . $params['limit'];
$query = $this->db->query($sql);
//echo $this->db->last_query();
return $query->result_array();
}
/*
Utility functions
*/
// return an array of project ids where author or pseudonym linked
private function _get_projects_by_author($author)
{
$sql = 'SELECT pa.project_id
FROM project_authors pa
JOIN authors a ON (pa.author_id = a.id)
WHERE CONCAT(a.first_name, " " , a.last_name) LIKE ?
AND a.linked_to = 0
UNION
SELECT pa.project_id
FROM project_authors pa
JOIN authors a ON (pa.author_id = a.id)
JOIN author_pseudonyms ap ON (ap.author_id = a.id)
WHERE CONCAT(ap.first_name, " " , ap.last_name) LIKE ?
AND a.linked_to = 0 ';
$author_like = "%$author%";
$query = $this->db->query($sql, array($author_like, $author_like));
return $this->ids($query, 'project_id');
}
// return an array of project ids where author or pseudonym linked to section
private function _get_projects_by_section_author($author)
{
$sql = 'SELECT st.source_id
FROM search_table st
JOIN authors a ON (st.section_author_id = a.id)
WHERE CONCAT(a.first_name, " " , a.last_name) LIKE ?
AND a.linked_to = 0
UNION
SELECT st.source_id
FROM search_table st
JOIN authors a ON (st.section_author_id = a.id)
JOIN author_pseudonyms ap ON (ap.author_id = a.id)
WHERE CONCAT(ap.first_name, " " , ap.last_name) LIKE ?
AND a.linked_to = 0 ';
$author_like = "%$author%";
$query = $this->db->query($sql, array($author_like, $author_like));
return $this->ids($query, 'source_id');
}
// return an array of author ids where author or pseudonym matches search
private function _get_author_ids($author)
{
$sql = 'SELECT a.id
FROM authors a
WHERE CONCAT(a.first_name, " " , a.last_name) LIKE ?
AND a.linked_to = 0
UNION
SELECT a.id
FROM authors a
JOIN author_pseudonyms ap ON (ap.author_id = a.id)
WHERE CONCAT(ap.first_name, " " , ap.last_name) LIKE ?
AND a.linked_to = 0 ';
$author_like = "%$author%";
$query = $this->db->query($sql, array($author_like, $author_like));
return $this->ids($query);
}
private function _get_reader_ids($reader, $exact_match)
{
$sql = 'SELECT u.id
FROM users u
WHERE u.display_name LIKE ?
OR u.username LIKE ?';
$reader_like = $exact_match ? $reader : "%$reader%";
$query = $this->db->query($sql, array($reader_like, $reader_like));
return $this->ids($query);
}
private function _get_projects_by_section_language($language_id)
{
$sql = 'SELECT st.source_id
FROM search_table st
WHERE section_language_id = ? ';
$query = $this->db->query($sql, array($language_id));
return $this->ids($query, 'source_id');
}
private function _get_projects_by_reader($reader, $exact_match)
{
$sql = 'SELECT pr.project_id
FROM project_readers pr
WHERE pr.display_name LIKE ?
OR pr.username LIKE ?';
$reader_like = $exact_match ? $reader : "%$reader%";
$query = $this->db->query($sql, array($reader_like, $reader_like));
return $this->ids($query, 'project_id');
}
private function ids($query, $id_column = 'id')
{
$ids = [];
foreach ($query->result('array') as $key => $value)
{
$ids[] = $value[$id_column];
}
if (empty($ids))
{
$ids[] = 0; // To prevent SQL syntax errors of the form "WHERE id IN ()".
}
return $ids;
}
}