-
Notifications
You must be signed in to change notification settings - Fork 74
/
search.php
582 lines (550 loc) · 24.2 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
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
<?php
/**
* Quick and advanced search modes.
*
* Quick Search prefixes can be applied in the following order:
*
* hd: Only search HD objects
* **1/2 Movie search
*
* Use ^ and/or $ to anchor the search, and enclose in // to use regex.
*
* @license GPL
*
* @package MythWeb
* @subpackage TV
*
**/
// Load the sorting routines
require_once 'includes/sorting.php';
// Path-based search overrides request-based search type.
if ($Path[2]) {
// Don't forget to take all of the remaining path.
$_SESSION['search']['s'] = implode('/', array_splice($Path, 2));
$_SESSION['search']['type'] = 'q';
$_REQUEST['search'] = true;
}
// Sorting should re-trigger the search
if ($_REQUEST['sortby'])
$_REQUEST['search'] = true;
// Pull in updates
if (isset($_REQUEST['type']))
$_SESSION['search']['type'] = $_REQUEST['type'];
if (isset($_REQUEST['s']))
$_SESSION['search']['s'] = trim($_REQUEST['s']);
if (isset($_REQUEST['as'])) {
$_SESSION['search']['as'] = $_REQUEST['as'];
$_SESSION['search']['af'] = $_REQUEST['af'];
$_SESSION['search']['aj'] = $_REQUEST['aj'];
$_SESSION['search']['ctype'] = $_REQUEST['ctype'];
$_SESSION['search']['categories'] = $_REQUEST['categories'];
$_SESSION['search']['hd'] = $_REQUEST['hd'] ? true : false;
$_SESSION['search']['commfree'] = $_REQUEST['commfree'] ? true : false;
$_SESSION['search']['unwatched'] = $_REQUEST['unwatched'] ? true : false;
$_SESSION['search']['scheduled'] = $_REQUEST['scheduled'] ? true : false;
$_SESSION['search']['generic'] = $_REQUEST['generic'] ? true : false;
$_SESSION['search']['distinctTitle'] = $_REQUEST['distinctTitle'] ? true : false;
$_SESSION['search']['stars_gt'] = floatVal($_REQUEST['stars_gt']);
$_SESSION['search']['stars_lt'] = floatVal($_REQUEST['stars_lt']);
$_SESSION['search']['airdate_start'] = trim($_REQUEST['airdate_start']);
$_SESSION['search']['airdate_end'] = trim($_REQUEST['airdate_end']);
$_SESSION['search']['starttime'] = trim($_REQUEST['starttime']);
$_SESSION['search']['endtime'] = trim($_REQUEST['endtime']);
}
// Session defaults
if (!is_array($_SESSION['search']['ctype']) || empty($_SESSION['search']['ctype']))
$_SESSION['search']['ctype'] = Program::category_types();
if (!is_array($_SESSION['search']['categories']) || empty($_SESSION['search']['categories']))
$_SESSION['search']['categories'] = array();
if (!isset($_SESSION['search']['stars_gt']) || !isset($_SESSION['search']['stars_lt'])) {
$_SESSION['search']['stars_gt'] = 0;
$_SESSION['search']['stars_lt'] = 1;
}
if (!isset($_SESSION['search']['starttime'])) {
$_SESSION['search']['starttime'] = 'now';
}
if (!isset($_SESSION['search']['endtime'])) {
$_SESSION['search']['endtime'] = '+ 2 weeks';
}
if (!is_array($_SESSION['search']['as']) || empty($_SESSION['search']['as'])) {
if (!is_string($_SESSION['search']['as']))
$_SESSION['search']['as'] = '';
$_SESSION['search']['as'] = array($_SESSION['search']['as']);
}
if (!is_array($_SESSION['search']['af'])) {
if (gettype($_SESSION['search']['af']) == 'string')
$_SESSION['search']['af'] = array(array($_SESSION['search']['af']));
else
$_SESSION['search']['af'] = array(array('title'));
}
if (!is_array($_SESSION['search']['aj'])) {
if (gettype($_SESSION['search']['aj']) == 'string')
$_SESSION['search']['aj'] = array($_SESSION['search']['aj']);
else
$_SESSION['search']['aj'] = array('AND');
}
// Compact any empty strings or fields, and make sure the data is in the right format
$fixeds = array();
$fixedf = array();
$fixedj = array();
foreach ($_SESSION['search']['as'] as $i => $str) {
if ($i > 0 && !preg_match('/\S/', $str))
continue;
// How is this joined to the previous string?
if (empty($_SESSION['search']['aj'][$i]))
$fixedj[] = 'AND';
else
$fixedj[] = $_SESSION['search']['aj'][$i];
// Fields for this string
$fields = array();
if (is_array($_SESSION['search']['af'][$i])) {
foreach ($_SESSION['search']['af'][$i] as $j => $field) {
if (!preg_match('/\S/', $field)) {
if ($j > 0)
continue;
else
$field = 'title';
}
$fields[] = $field;
}
}
else
$_SESSION['search']['af'][$i] = array('title');
$fixeds[] = trim($str);
$fixedf[] = $fields;
}
$_SESSION['search']['as'] = $fixeds;
$_SESSION['search']['af'] = $fixedf;
$_SESSION['search']['aj'] = $fixedj;
// Swap mismatched star restrictions
if ($_SESSION['search']['stars_gt'] > $_SESSION['search']['stars_lt']) {
$tmp = $_SESSION['search']['stars_gt'];
$_SESSION['search']['stars_gt'] = $_SESSION['search']['stars_lt'];
$_SESSION['search']['stars_lt'] = $tmp;
}
// Actually perform a search?
if (isset($_REQUEST['search'])) {
// Start the query
$query = array();
$extra_query = array();
// Advanced search?
if ($_SESSION['search']['type'] == 'a') {
// Which kinds of programs are we searching through?
$extra_query[] = 'program.category_type IN ('
.implode(',', $db->escape_array($_SESSION['search']['ctype']))
.')';
if (count($_SESSION['search']['categories']) && !in_array(t('All'), $_SESSION['search']['categories'])) {
$extra_query[] = 'program.category IN ('
.implode(',', $db->escape_array($_SESSION['search']['categories']))
.')';
}
// Star restrictions only apply to movies
if (in_array('movie', $_SESSION['search']['ctype'])) {
if (count($_SESSION['search']['ctype']) > 1) {
$extra_query[] = 'IF(program.category_type = "movie",'
.' program.stars >= '.number_format($_SESSION['search']['stars_gt'], 3, '.', '')
.' AND program.stars <= '.number_format($_SESSION['search']['stars_lt'] + 0.001, 3, '.', '')
.', 1)';
}
else {
$extra_query[] = ' program.stars >= '.number_format($_SESSION['search']['stars_gt'], 3, '.', '')
.' AND program.stars <= '.number_format($_SESSION['search']['stars_lt'] + 0.001, 3, '.', '');
}
}
// Date range
if (preg_match('/\S/', $_SESSION['search']['starttime'])) {
$tmpdate = strtotime($_SESSION['search']['starttime']);
if ($tmpdate == false) {
}
else {
$extra_query[] = 'program.starttime>='.$db->escape(date('Y-m-d H:i:s', $tmpdate));
}
}
if (preg_match('/\S/', $_SESSION['search']['endtime'])) {
$tmpdate = strtotime($_SESSION['search']['endtime']);
if ($tmpdate == false) {
}
else {
$extra_query[] = 'program.endtime<='.$db->escape(date('Y-m-d H:i:s', $tmpdate));
}
}
// Airdate range
if (preg_match('/\S/', $_SESSION['search']['airdate_start'])) {
$tmpdate = $_SESSION['search']['airdate_start'];
if (!preg_match('/\D/', $tmpdate)) // for some reason, strtotime doesn't like dates like 2000
$tmpdate .= '-01-01';
$tmpdate = strtotime($tmpdate);
if ($tmpdate == false) {
}
else {
$extra_query[] = 'IF(program.originalairdate IS NULL,'
.'program.airdate>=' .$db->escape(date('Y', $tmpdate)).','
.'program.originalairdate>='.$db->escape(date('Y-m-d', $tmpdate)).')';
}
}
if (preg_match('/\S/', $_SESSION['search']['airdate_end'])) {
$tmpdate = $_SESSION['search']['airdate_end'];
if (!preg_match('/\D/', $tmpdate)) // for some reason, strtotime doesn't like dates like 2000
$tmpdate .= '-01-01';
$tmpdate = strtotime($tmpdate);
if ($tmpdate == false) {
}
else {
$extra_query[] = 'IF(program.originalairdate IS NULL,'
.'program.airdate<=' .$db->escape(date('Y', $tmpdate)).','
.'program.originalairdate<='.$db->escape(date('Y-m-d', $tmpdate)).')';
}
}
// Build the actual search query
$query = '';
foreach ($_SESSION['search']['as'] as $i => $string) {
// Joiner
if ($i > 0)
$query .= ' '.$_SESSION['search']['aj'][$i];
// Match posibilities
if (!empty($_SESSION['search']['af'][$i])) {
$query .= '(';
list($compare, $search) = prep_search($string);
foreach ($_SESSION['search']['af'][$i] as $j => $af) {
if ($j > 0)
$query .= ' OR ';
switch ($af) {
case 'subtitle':
$query .= 'program.subtitle';
break;
case 'description':
$query .= 'program.description';
break;
case 'category':
$query .= 'program.category';
break;
case 'people':
$query .= 'people.name';
break;
case 'channum':
$query .= 'channel.channum';
break;
case 'channame':
$query .= 'channel.name';
break;
case 'callsign':
$query .= 'channel.callsign';
break;
case 'title':
default:
$query .= 'program.title';
break;
}
// The actual
$query .= " $compare $search";
}
$query .= ')';
}
}
}
// Canned search?
elseif (preg_match('/^\s*canned:\s*(.+)\s*$/i', $_SESSION['search']['s'], $search_name)) {
$search_name = $search_name[1];
// Load the canned searches
require_once 'modules/tv/canned_searches.conf.php';
// Load the local canned searches (if it exists)
if (file_exists('configuration/canned_searches.conf.php'))
include 'configuration/canned_searches.conf.php';
// Find the query
if (empty($Canned_Searches[$search_name]))
add_warning("Unknown canned query: $search_name");
else
$query = $Canned_Searches[$search_name];
}
// Quick search is the default
else {
// Make a backup so we can edit it without affecting the original
$search_str = $_SESSION['search']['s'];
// If it starts with hd: it's an hd-only search
if (preg_match('/^hd:\s*(.+)$/', $search_str, $match)) {
$extra_query[] = 'program.hdtv & 1';
$search_str = $match[1];
}
// If the next thing starts with stars, it's a movie rating query
if (preg_match('#(\\*+\s*(1/2\b|0?\.5\b|-)?)\s*(.*?)$#', $search_str, $match)) {
$starcount = substr_count($match[1], '*') / 5;
if (preg_match('/1\\/2|\\.5|-/', $match[1]))
$starcount += 0.125;
// Add this to the query -- convert european decimal to something mysql can understand
$extra_query[] = 'program.stars >= '.number_format($starcount, 3, '.', '');
// Remove the stars from the search string so we can continue looking for other things
$search_str = $match[2];
}
// Quick search doesn't search the past
$extra_query[] = 'program.endtime >= NOW()';
// Build the query
list($compare, $search) = prep_search($search_str);
if (empty($_REQUEST['field']) || preg_match('/\btitle/i', $_REQUEST['field']))
$query[] = "program.title $compare $search";
if (empty($_REQUEST['field']) || stristr($_REQUEST['field'], 'subtitle'))
$query[] = "program.subtitle $compare $search";
if (empty($_REQUEST['field']) || stristr($_REQUEST['field'], 'desc'))
$query[] = "program.description $compare $search";
if (empty($_REQUEST['field']) || stristr($_REQUEST['field'], 'cat'))
$query[] = "program.category $compare $search";
if (empty($_REQUEST['field']) || stristr($_REQUEST['field'], 'people'))
$query[] = "people.name $compare $search";
$query = implode(' OR ', $query);
}
// HDTV only?
if ($_SESSION['search']['hd'])
$extra_query[] = 'program.hdtv & 1';
if ($_SESSION['search']['generic'])
$extra_query[] = 'program.generic = 0';
// Commercial-free channels only?
if ($_SESSION['search']['commfree'])
$extra_query[] = 'channel.commmethod=-2';
// Finish the query and add any extra parameters
if ($query)
$query = "($query)";
if (count($extra_query) > 0) {
if ($query)
$query .= ' AND ';
$query .= '('.implode(' AND ', $extra_query).')';
}
// Search!
if (!empty($query))
$tmpdate = strtotime($_SESSION['search']['starttime']);
if ($tmpdate == false) {
$Results =& load_all_program_data(time(), strtotime('+1 month'), NULL, false, $query, $_SESSION['search']['distinctTitle']);
} else {
$Results =& load_all_program_data($tmpdate, strtotime('+1 month'), NULL, false, $query, $_SESSION['search']['distinctTitle']);
}
}
// Only show unwatched shows?
if ($_SESSION['search']['unwatched'] && count($Results)) {
foreach ($Results as $key => $show) {
switch($show->recstatus) {
case 'PreviousRecording':
case 'CurrentRecording':
case 'Recorded':
case 'NeverRecord':
unset($Results[$key]);
continue 2;
}
}
}
// Ignore will record shows?
if ($_SESSION['search']['scheduled'] && count($Results)) {
foreach ($Results as $key => $show) {
switch($show->recstatus) {
case 'WillRecord':
case 'Conflict':
case 'EarlierShowing':
case 'LaterShowing':
unset($Results[$key]);
continue 2;
}
}
}
// Query cleanup
if (empty($Results))
$Results = array();
else
sort_programs($Results, 'search_sortby');
// Build a list of titles for figuring out alternate showings. Use the same
// key to make parsing things below a little easier.
$titles = array();
$seen = array();
foreach ($Results as $key => $show) {
$tkey = md5($show->channel->channum.':'.$show->channel->callsign.':'.$show->title.':'.$show->subtitle.':'.$show->description);
$skey = $show->channel->name.$show->starttime.$tkey;
if($seen[$skey]){
unset($Results[$key]);
continue;
}else{
$titles[$tkey][$key] =& $Results[$key];
$seen[$skey] = true;
}
}
// Parse the show list for showings that can be consolidated/folded
$seen = array();
foreach ($Results as $key => $show) {
$tkey = md5($show->channel->channum.':'.$show->channel->callsign.':'.$show->title.':'.$show->subtitle.':'.$show->description);
// Populate extra_showings info for other instances of this show
if (count($titles[$tkey]) > 1) {
foreach (array_keys($titles[$tkey]) as $key2) {
if ($key == $key2)
continue;
$Results[$key2]->extra_showings[] = array($show->chanid, $show->starttime);
}
}
// Delete duplicate showings if we're sorting by title or category (genre)
if (!in_array($_SESSION['search_sortby'][0]['field'], array('title', 'category'))) {
continue;
}
elseif ($seen[$tkey]) {
unset($titles[$tkey][$key]);
unset($Results[$key]);
}
else {
$seen[$tkey] = true;
}
}
unset($titles);
// Load the class for this page
require_once tmpl_dir.'search.php';
// Exit
exit;
/**
* Prep a search string for matching, and return the comparison string
*
* @param
*
* @return array
**/
function prep_search($str) {
// negative match
$neg = '';
if (substr($str, 0, 1) == '!') {
$neg = 'NOT ';
$str = substr($str, 1);
}
// Regex search?
if (preg_match('#^/(.+)/$#', $str, $match))
return array($neg.'REGEXP', search_escape($match[1], 'both'));
// Or figure out where the search should be anchored
elseif (preg_match('#^\^(.+)\$$#', $str, $match))
return array($neg.'LIKE', search_escape($match[1], 'both'));
elseif (preg_match('#^\^(.+)#', $str, $match))
return array($neg.'LIKE', search_escape($match[1], 'start'));
elseif (preg_match('#(.+)\$$#', $str, $match))
return array($neg.'LIKE', search_escape($match[1], 'end'));
// Default is no anchor
return array($neg.'LIKE', search_escape($str));
}
/**
* Helps format search queries.
**/
function search_escape($value, $anchor='none') {
global $db;
// MySQL trims all text fields, so we should do the same
$value = trim($value);
// Where do we anchor? Replace whitespace with the % wildcard, too.
switch ($anchor) {
case 'both':
return $db->escape($value);
case 'start':
return $db->escape(preg_replace('/[\\s\-_]+/', '%', $value).'%');
case 'end':
return $db->escape('%'.preg_replace('/[\\s\-_]+/', '%', $value));
default:
return $db->escape('%'.preg_replace('/[\\s\-_]+/', '%', $value).'%');
}
}
/**
* Prints out the entire advanced search string content block;
**/
function print_advanced_search_strings() {
// Search fields
static $search_fields;
if (empty($search_fields)) {
$search_fields = array('' => '',
'title' => html_entities(t('Title')),
'subtitle' => html_entities(t('Subtitle')),
'description' => html_entities(t('Description')),
'category' => html_entities(t('Category')),
'people' => html_entities(t('People')),
'channum' => html_entities(t('Chan. Number')),
'channame' => html_entities(t('Chan. Name')),
'callsign' => html_entities(t('Chan. Callsign')),
);
}
// Adding a new search string, or just compress the existing one(s)
if (isset($_REQUEST['add_search_string']))
$strings = array_merge($_SESSION['search']['as'], array(''));
else
$strings = array_merge($_SESSION['search']['as']);
// Print each string that we know about
foreach ($strings as $i => $string) {
// Join the strings how?
if ($i != 0) {
echo '<p><select name="aj[', $i, ']">';
foreach (array('AND', 'OR') as $join) {
echo '<option';
if ($join == $_SESSION['search']['aj'][$i])
echo ' SELECTED';
echo ">$join</option>";
}
echo "</select></p>\n";
}
// Adding a new match field, or just compress the existing one(s)
if (empty($_SESSION['search']['af'][$i]))
$_SESSION['search']['af'][$i] = array('title');
if (isset($_REQUEST['add_search_field']) && $_REQUEST['add_search_field'] == $i)
$fields = array_merge($_SESSION['search']['af'][$i], array(''));
else
$fields = array_merge($_SESSION['search']['af'][$i]);
// Which fields do we match against?
foreach ($fields as $j => $af) {
if ($j > 0)
echo ' ', t('or'), ' ';
echo '<select name="af[', $i, '][', $j, ']" id="af', $i, '.', $j, '"',
' onchange="search_field_change(', $i, ')">';
foreach ($search_fields as $field => $name) {
if (empty($j) && empty($field))
continue;
echo '<option value="', $field, '"';
if ($af == $field)
echo ' SELECTED';
echo '>', $name, '</option>';
}
echo '</select>';
}
echo ' <a id="add_search_field_', $i, '" onclick="add_search_field(', $i, ')">', t('more'), '...</a>',
"<br>\n",
t('matches'), ': <input type="text" size="35" name="as[', $i, ']" id="as', $i, '" value="',
html_entities($string), "\" onchange=\"set_search('a')\">\n";
}
echo '<a onclick="add_search_string()">', t('add string'), '...</a>';
}
/**
* Prints out a list of program types, along with checkboxes.
**/
function category_type_list() {
foreach (Program::category_types() as $key => $type) {
$safe_type = html_entities($type);
echo '<input type="checkbox" name="ctype[]" id="ctype_', $key,
'" value="',$safe_type, '"';
if (in_array($type, $_SESSION['search']['ctype']))
echo ' CHECKED';
echo '><label for="ctype_', $key, '">', $safe_type, '</label>';
if ($type == 'movie') {
echo ': ',
movie_star_select('stars_gt'),
' to ',
movie_star_select('stars_lt');
}
echo '<br>';
}
}
/**
* Prints out a list of program types, along with checkboxes.
**/
function category_list() {
echo "<select multiple name='categories[]' size='5'>";
echo "<option ".(in_array(t('All'), $_SESSION['search']['categories']) ? 'selected' : '' ).">".t('All')."</option>";
foreach (program::categories() as $category) {
echo "<option ".(in_array($category, $_SESSION['search']['categories']) ? 'selected' : '' ).">$category</option>";
}
echo "</select>";
}
/**
* Print a <select> with movie star ratings in it.
**/
function movie_star_select($name) {
echo '<select name="', $name, '">';
for ($x=0; $x<=max_stars; $x++) {
$v = number_format($x/max_stars, 3, '.', '');
echo '<option value="', $v, '"';
if ($v == $_SESSION['search'][$name])
echo ' SELECTED';
echo '>', str_repeat(star_character, $x), '</option>';
}
echo '</select>';
}