-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
query.php
1402 lines (1202 loc) · 36.3 KB
/
query.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
/**
* @package Joomla.Administrator
* @subpackage com_finder
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('_JEXEC') or die;
JLoader::register('FinderIndexerHelper', __DIR__ . '/helper.php');
JLoader::register('FinderIndexerTaxonomy', __DIR__ . '/taxonomy.php');
JLoader::register('FinderHelperRoute', JPATH_SITE . '/components/com_finder/helpers/route.php');
JLoader::register('FinderHelperLanguage', JPATH_ADMINISTRATOR . '/components/com_finder/helpers/language.php');
/**
* Query class for the Finder indexer package.
*
* @package Joomla.Administrator
* @subpackage com_finder
* @since 2.5
*/
class FinderIndexerQuery
{
/**
* Flag to show whether the query can return results.
*
* @var boolean
* @since 2.5
*/
public $search;
/**
* The query input string.
*
* @var string
* @since 2.5
*/
public $input;
/**
* The language of the query.
*
* @var string
* @since 2.5
*/
public $language;
/**
* The query string matching mode.
*
* @var string
* @since 2.5
*/
public $mode;
/**
* The included tokens.
*
* @var array
* @since 2.5
*/
public $included = array();
/**
* The excluded tokens.
*
* @var array
* @since 2.5
*/
public $excluded = array();
/**
* The tokens to ignore because no matches exist.
*
* @var array
* @since 2.5
*/
public $ignored = array();
/**
* The operators used in the query input string.
*
* @var array
* @since 2.5
*/
public $operators = array();
/**
* The terms to highlight as matches.
*
* @var array
* @since 2.5
*/
public $highlight = array();
/**
* The number of matching terms for the query input.
*
* @var integer
* @since 2.5
*/
public $terms;
/**
* The static filter id.
*
* @var string
* @since 2.5
*/
public $filter;
/**
* The taxonomy filters. This is a multi-dimensional array of taxonomy
* branches as the first level and then the taxonomy nodes as the values.
*
* For example:
* $filters = array(
* 'Type' = array(10, 32, 29, 11, ...);
* 'Label' = array(20, 314, 349, 91, 82, ...);
* ...
* );
*
* @var array
* @since 2.5
*/
public $filters = array();
/**
* The start date filter.
*
* @var string
* @since 2.5
*/
public $date1;
/**
* The end date filter.
*
* @var string
* @since 2.5
*/
public $date2;
/**
* The start date filter modifier.
*
* @var string
* @since 2.5
*/
public $when1;
/**
* The end date filter modifier.
*
* @var string
* @since 2.5
*/
public $when2;
/**
* Method to instantiate the query object.
*
* @param array $options An array of query options.
*
* @since 2.5
* @throws Exception on database error.
*/
public function __construct($options)
{
// Get the input string.
$this->input = isset($options['input']) ? $options['input'] : null;
// Get the empty query setting.
$this->empty = isset($options['empty']) ? (bool) $options['empty'] : false;
// Get the input language.
$this->language = !empty($options['language']) ? $options['language'] : FinderIndexerHelper::getDefaultLanguage();
$this->language = FinderIndexerHelper::getPrimaryLanguage($this->language);
// Get the matching mode.
$this->mode = 'AND';
// Initialize the temporary date storage.
$this->dates = new JRegistry;
// Populate the temporary date storage.
if (isset($options['date1']) && !empty($options['date1']))
{
$this->dates->set('date1', $options['date1']);
}
if (isset($options['date2']) && !empty($options['date1']))
{
$this->dates->set('date2', $options['date2']);
}
if (isset($options['when1']) && !empty($options['date1']))
{
$this->dates->set('when1', $options['when1']);
}
if (isset($options['when2']) && !empty($options['date1']))
{
$this->dates->set('when2', $options['when2']);
}
// Process the static taxonomy filters.
if (isset($options['filter']) && !empty($options['filter']))
{
$this->processStaticTaxonomy($options['filter']);
}
// Process the dynamic taxonomy filters.
if (isset($options['filters']) && !empty($options['filters']))
{
$this->processDynamicTaxonomy($options['filters']);
}
// Get the date filters.
$d1 = $this->dates->get('date1');
$d2 = $this->dates->get('date2');
$w1 = $this->dates->get('when1');
$w2 = $this->dates->get('when2');
// Process the date filters.
if (!empty($d1) || !empty($d2))
{
$this->processDates($d1, $d2, $w1, $w2);
}
// Process the input string.
$this->processString($this->input, $this->language, $this->mode);
// Get the number of matching terms.
foreach ($this->included as $token)
{
$this->terms += count($token->matches);
}
// Remove the temporary date storage.
unset($this->dates);
/*
* Lastly, determine whether this query can return a result set.
*/
// Check if we have a query string.
if (!empty($this->input))
{
$this->search = true;
}
// Check if we can search without a query string.
elseif ($this->empty && (!empty($this->filter) || !empty($this->filters) || !empty($this->date1) || !empty($this->date2)))
{
$this->search = true;
}
// We do not have a valid search query.
else
{
$this->search = false;
}
}
/**
* Method to convert the query object into a URI string.
*
* @param string $base The base URI. [optional]
*
* @return string The complete query URI.
*
* @since 2.5
*/
public function toURI($base = null)
{
// Set the base if not specified.
if (empty($base))
{
$base = 'index.php?option=com_finder&view=search';
}
// Get the base URI.
$uri = JURI::getInstance($base);
// Add the static taxonomy filter if present.
if (!empty($this->filter))
{
$uri->setVar('f', $this->filter);
}
// Get the filters in the request.
$input = JFactory::getApplication()->input;
$t = $input->request->get('t', array(), 'array');
// Add the dynamic taxonomy filters if present.
if (!empty($this->filters))
{
foreach ($this->filters as $nodes)
{
foreach ($nodes as $node)
{
if (!in_array($node, $t))
{
continue;
}
$uri->setVar('t[]', $node);
}
}
}
// Add the input string if present.
if (!empty($this->input))
{
$uri->setVar('q', $this->input);
}
// Add the start date if present.
if (!empty($this->date1))
{
$uri->setVar('d1', $this->date1);
}
// Add the end date if present.
if (!empty($this->date2))
{
$uri->setVar('d2', $this->date2);
}
// Add the start date modifier if present.
if (!empty($this->when1))
{
$uri->setVar('w1', $this->when1);
}
// Add the end date modifier if present.
if (!empty($this->when2))
{
$uri->setVar('w2', $this->when2);
}
// Add a menu item id if one is not present.
if (!$uri->getVar('Itemid'))
{
// Get the menu item id.
$query = array(
'view' => $uri->getVar('view'),
'f' => $uri->getVar('f'),
'q' => $uri->getVar('q')
);
$item = FinderHelperRoute::getItemid($query);
// Add the menu item id if present.
if ($item !== null)
{
$uri->setVar('Itemid', $item);
}
}
return $uri->toString(array('path', 'query'));
}
/**
* Method to get a list of excluded search term ids.
*
* @return array An array of excluded term ids.
*
* @since 2.5
*/
public function getExcludedTermIds()
{
$results = array();
// Iterate through the excluded tokens and compile the matching terms.
for ($i = 0, $c = count($this->excluded); $i < $c; $i++)
{
$results = array_merge($results, $this->excluded[$i]->matches);
}
// Sanitize the terms.
//@TODO: Should toInteger use $return?
$return = array_unique($results);
JArrayHelper::toInteger($results);
return $results;
}
/**
* Method to get a list of included search term ids.
*
* @return array An array of included term ids.
*
* @since 2.5
*/
public function getIncludedTermIds()
{
$results = array();
// Iterate through the included tokens and compile the matching terms.
for ($i = 0, $c = count($this->included); $i < $c; $i++)
{
// Check if we have any terms.
if (empty($this->included[$i]->matches))
{
continue;
}
// Get the term.
$term = $this->included[$i]->term;
// Prepare the container for the term if necessary.
if (!array_key_exists($term, $results))
{
$results[$term] = array();
}
// Add the matches to the stack.
$results[$term] = array_merge($results[$term], $this->included[$i]->matches);
}
// Sanitize the terms.
foreach ($results as $key => $value)
{
$results[$key] = array_unique($results[$key]);
JArrayHelper::toInteger($results[$key]);
}
return $results;
}
/**
* Method to get a list of required search term ids.
*
* @return array An array of required term ids.
*
* @since 2.5
*/
public function getRequiredTermIds()
{
$results = array();
// Iterate through the included tokens and compile the matching terms.
for ($i = 0, $c = count($this->included); $i < $c; $i++)
{
// Check if the token is required.
if ($this->included[$i]->required)
{
// Get the term.
$term = $this->included[$i]->term;
// Prepare the container for the term if necessary.
if (!array_key_exists($term, $results))
{
$results[$term] = array();
}
// Add the matches to the stack.
$results[$term] = array_merge($results[$term], $this->included[$i]->matches);
}
}
// Sanitize the terms.
foreach ($results as $key => $value)
{
$results[$key] = array_unique($results[$key]);
JArrayHelper::toInteger($results[$key]);
}
return $results;
}
/**
* Method to process the static taxonomy input. The static taxonomy input
* comes in the form of a pre-defined search filter that is assigned to the
* search form.
*
* @param integer $filterId The id of static filter.
*
* @return boolean True on success, false on failure.
*
* @since 2.5
* @throws Exception on database error.
*/
protected function processStaticTaxonomy($filterId)
{
// Get the database object.
$db = JFactory::getDBO();
// Initialize user variables
$user = JFactory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
// Load the predefined filter.
$query = $db->getQuery(true);
$query->select('f.' . $db->quoteName('data') . ', f.' . $db->quoteName('params'));
$query->from($db->quoteName('#__finder_filters') . ' AS f');
$query->where('f.' . $db->quoteName('filter_id') . ' = ' . (int) $filterId);
$db->setQuery($query);
$return = $db->loadObject();
// Check for a database error.
if ($db->getErrorNum())
{
// Throw database error exception.
throw new Exception($db->getErrorMsg(), 500);
}
// Check the returned filter.
if (empty($return))
{
return false;
}
// Set the filter.
$this->filter = (int) $filterId;
// Get a parameter object for the filter date options.
$registry = new JRegistry;
$registry->loadString($return->params);
$params = $registry;
// Set the dates if not already set.
$this->dates->def('d1', $params->get('d1'));
$this->dates->def('d2', $params->get('d2'));
$this->dates->def('w1', $params->get('w1'));
$this->dates->def('w2', $params->get('w2'));
// Remove duplicates and sanitize.
$filters = explode(',', $return->data);
$filters = array_unique($filters);
JArrayHelper::toInteger($filters);
// Remove any values of zero.
if (array_search(0, $filters, true) !== false)
{
unset($filters[array_search(0, $filters, true)]);
}
// Check if we have any real input.
if (empty($filters))
{
return true;
}
/*
* Create the query to get filters from the database. We do this for
* two reasons: one, it allows us to ensure that the filters being used
* are real; two, we need to sort the filters by taxonomy branch.
*/
$query->clear();
$query->select('t1.id, t1.title, t2.title AS branch');
$query->from($db->quoteName('#__finder_taxonomy') . ' AS t1');
$query->join('INNER', $db->quoteName('#__finder_taxonomy') . ' AS t2 ON t2.id = t1.parent_id');
$query->where('t1.state = 1');
$query->where('t1.' . $db->quoteName('access') . ' IN (' . $groups . ')');
$query->where('t1.id IN (' . implode(',', $filters) . ')');
$query->where('t2.state = 1');
$query->where('t2.' . $db->quoteName('access') . ' IN (' . $groups . ')');
// Load the filters.
$db->setQuery($query);
$results = $db->loadObjectList();
// Check for a database error.
if ($db->getErrorNum())
{
// Throw database error exception.
throw new Exception($db->getErrorMsg(), 500);
}
// Sort the filter ids by branch.
foreach ($results as $result)
{
$this->filters[$result->branch][$result->title] = (int) $result->id;
}
return true;
}
/**
* Method to process the dynamic taxonomy input. The dynamic taxonomy input
* comes in the form of select fields that the user chooses from. The
* dynamic taxonomy input is processed AFTER the static taxonomy input
* because the dynamic options can be used to further narrow a static
* taxonomy filter.
*
* @param array $filters An array of taxonomy node ids.
*
* @return boolean True on success.
*
* @since 2.5
* @throws Exception on database error.
*/
protected function processDynamicTaxonomy($filters)
{
// Initialize user variables
$user = JFactory::getUser();
$groups = implode(',', $user->getAuthorisedViewLevels());
// Remove duplicates and sanitize.
$filters = array_unique($filters);
JArrayHelper::toInteger($filters);
// Remove any values of zero.
if (array_search(0, $filters, true) !== false)
{
unset($filters[array_search(0, $filters, true)]);
}
// Check if we have any real input.
if (empty($filters))
{
return true;
}
// Get the database object.
$db = JFactory::getDBO();
$query = $db->getQuery(true);
/*
* Create the query to get filters from the database. We do this for
* two reasons: one, it allows us to ensure that the filters being used
* are real; two, we need to sort the filters by taxonomy branch.
*/
$query->select('t1.id, t1.title, t2.title AS branch');
$query->from($db->quoteName('#__finder_taxonomy') . ' AS t1');
$query->join('INNER', $db->quoteName('#__finder_taxonomy') . ' AS t2 ON t2.id = t1.parent_id');
$query->where('t1.state = 1');
$query->where('t1.' . $db->quoteName('access') . ' IN (' . $groups . ')');
$query->where('t1.id IN (' . implode(',', $filters) . ')');
$query->where('t2.state = 1');
$query->where('t2.' . $db->quoteName('access') . ' IN (' . $groups . ')');
// Load the filters.
$db->setQuery($query);
$results = $db->loadObjectList();
// Check for a database error.
if ($db->getErrorNum())
{
// Throw database error exception.
throw new Exception($db->getErrorMsg(), 500);
}
// Cleared filter branches.
$cleared = array();
/*
* Sort the filter ids by branch. Because these filters are designed to
* override and further narrow the items selected in the static filter,
* we will clear the values from the static filter on a branch by
* branch basis before adding the dynamic filters. So, if the static
* filter defines a type filter of "articles" and three "category"
* filters but the user only limits the category further, the category
* filters will be flushed but the type filters will not.
*/
foreach ($results as $result)
{
// Check if the branch has been cleared.
if (!in_array($result->branch, $cleared))
{
// Clear the branch.
$this->filters[$result->branch] = array();
// Add the branch to the cleared list.
$cleared[] = $result->branch;
}
// Add the filter to the list.
$this->filters[$result->branch][$result->title] = (int) $result->id;
}
return true;
}
/**
* Method to process the query date filters to determine start and end
* date limitations.
*
* @param string $date1 The first date filter.
* @param string $date2 The second date filter.
* @param string $when1 The first date modifier.
* @param string $when2 The second date modifier.
*
* @return boolean True on success.
*
* @since 2.5
*/
protected function processDates($date1, $date2, $when1, $when2)
{
// Clean up the inputs.
$date1 = JString::trim(JString::strtolower($date1));
$date2 = JString::trim(JString::strtolower($date2));
$when1 = JString::trim(JString::strtolower($when1));
$when2 = JString::trim(JString::strtolower($when2));
// Get the time offset.
$offset = JFactory::getApplication()->getCfg('offset');
// Array of allowed when values.
$whens = array('before', 'after', 'exact');
// The value of 'today' is a special case that we need to handle.
if ($date1 === JString::strtolower(JText::_('COM_FINDER_QUERY_FILTER_TODAY')))
{
$today = JFactory::getDate('now', $offset);
$date1 = $today->format('%Y-%m-%d');
}
// Try to parse the date string.
$date = JFactory::getDate($date1, $offset);
// Check if the date was parsed successfully.
if ($date->toUnix() !== null)
{
// Set the date filter.
$this->date1 = $date->toSQL();
$this->when1 = in_array($when1, $whens) ? $when1 : 'before';
}
// The value of 'today' is a special case that we need to handle.
if ($date2 === JString::strtolower(JText::_('COM_FINDER_QUERY_FILTER_TODAY')))
{
$today = JFactory::getDate('now', $offset);
$date2 = $today->format('%Y-%m-%d');
}
// Try to parse the date string.
$date = JFactory::getDate($date2, $offset);
// Check if the date was parsed successfully.
if ($date->toUnix() !== null)
{
// Set the date filter.
$this->date2 = $date->toSQL();
$this->when2 = in_array($when2, $whens) ? $when2 : 'before';
}
return true;
}
/**
* Method to process the query input string and extract required, optional,
* and excluded tokens; taxonomy filters; and date filters.
*
* @param string $input The query input string.
* @param string $lang The query input language.
* @param string $mode The query matching mode.
*
* @return boolean True on success.
*
* @since 2.5
* @throws Exception on database error.
*/
protected function processString($input, $lang, $mode)
{
// Clean up the input string.
$input = html_entity_decode($input, ENT_QUOTES, 'UTF-8');
$input = JString::strtolower($input);
$input = preg_replace('#\s+#mi', ' ', $input);
$input = JString::trim($input);
$debug = JFactory::getConfig()->get('debug_lang');
/*
* First, we need to handle string based modifiers. String based
* modifiers could potentially include things like "category:blah" or
* "before:2009-10-21" or "type:article", etc.
*/
$patterns = array(
'before' => JText::_('COM_FINDER_FILTER_WHEN_BEFORE'),
'after' => JText::_('COM_FINDER_FILTER_WHEN_AFTER')
);
// Add the taxonomy branch titles to the possible patterns.
foreach (FinderIndexerTaxonomy::getBranchTitles() as $branch)
{
// Add the pattern.
$patterns[$branch] = JString::strtolower(JText::_(FinderHelperLanguage::branchSingular($branch)));
}
// Container for search terms and phrases.
$terms = array();
$phrases = array();
// Cleared filter branches.
$cleared = array();
/*
* Compile the suffix pattern. This is used to match the values of the
* filter input string. Single words can be input directly, multi-word
* values have to be wrapped in double quotes.
*/
$quotes = html_entity_decode('‘’'', ENT_QUOTES, 'UTF-8');
$suffix = '(([\w\d' . $quotes . '-]+)|\"([\w\d\s' . $quotes . '-]+)\")';
/*
* Iterate through the possible filter patterns and search for matches.
* We need to match the key, colon, and a value pattern for the match
* to be valid.
*/
foreach ($patterns as $modifier => $pattern)
{
$matches = array();
if ($debug)
{
$pattern = substr($pattern, 2, -2);
}
// Check if the filter pattern is in the input string.
if (preg_match('#' . $pattern . '\s*:\s*' . $suffix . '#mi', $input, $matches))
{
// Get the value given to the modifier.
$value = isset($matches[3]) ? $matches[3] : $matches[1];
// Now we have to handle the filter string.
switch ($modifier)
{
// Handle a before and after date filters.
case 'before':
case 'after':
{
// Get the time offset.
$offset = JFactory::getApplication()->getCfg('offset');
// Array of allowed when values.
$whens = array('before', 'after', 'exact');
// The value of 'today' is a special case that we need to handle.
if ($value === JString::strtolower(JText::_('COM_FINDER_QUERY_FILTER_TODAY')))
{
$today = JFactory::getDate('now', $offset);
$value = $today->format('%Y-%m-%d');
}
// Try to parse the date string.
$date = JFactory::getDate($value, $offset);
// Check if the date was parsed successfully.
if ($date->toUnix() !== null)
{
// Set the date filter.
$this->date1 = $date->toSQL();
$this->when1 = in_array($modifier, $whens) ? $modifier : 'before';
}
break;
}
// Handle a taxonomy branch filter.
default:
{
// Try to find the node id.
$return = FinderIndexerTaxonomy::getNodeByTitle($modifier, $value);
// Check if the node id was found.
if ($return)
{
// Check if the branch has been cleared.
if (!in_array($modifier, $cleared))
{
// Clear the branch.
$this->filters[$modifier] = array();
// Add the branch to the cleared list.
$cleared[] = $modifier;
}
// Add the filter to the list.
$this->filters[$modifier][$return->title] = (int) $return->id;
}
break;
}
}
// Clean up the input string again.
$input = str_replace($matches[0], '', $input);
$input = preg_replace('#\s+#mi', ' ', $input);
$input = JString::trim($input);
}
}
/*
* Extract the tokens enclosed in double quotes so that we can handle
* them as phrases.
*/
if (JString::strpos($input, '"') !== false)
{
$matches = array();
// Extract the tokens enclosed in double quotes.
if (preg_match_all('#\"([^"]+)\"#mi', $input, $matches))
{
/*
* One or more phrases were found so we need to iterate through
* them, tokenize them as phrases, and remove them from the raw
* input string before we move on to the next processing step.
*/
foreach ($matches[1] as $key => $match)
{
// Find the complete phrase in the input string.
$pos = JString::strpos($input, $matches[0][$key]);
$len = JString::strlen($matches[0][$key]);
// Add any terms that are before this phrase to the stack.
if (JString::trim(JString::substr($input, 0, $pos)))
{
$terms = array_merge($terms, explode(' ', JString::trim(JString::substr($input, 0, $pos))));
}
// Strip out everything up to and including the phrase.
$input = JString::substr($input, $pos + $len);
// Clean up the input string again.
$input = preg_replace('#\s+#mi', ' ', $input);
$input = JString::trim($input);
// Get the number of words in the phrase.
$parts = explode(' ', $match);
// Check if the phrase is longer than three words.
if (count($parts) > 3)
{
/*
* If the phrase is longer than three words, we need to
* break it down into smaller chunks of phrases that
* are less than or equal to three words. We overlap
* the chunks so that we can ensure that a match is
* found for the complete phrase and not just portions
* of it.
*/
for ($i = 0, $c = count($parts); $i < $c; $i += 2)
{
// Set up the chunk.
$chunk = array();
// The chunk has to be assembled based on how many
// pieces are available to use.
switch ($c - $i)
{
// If only one word is left, we can break from
// the switch and loop because the last word
// was already used at the end of the last
// chunk.
case 1:
break 2;
// If there words are left, we use them both as
// the last chunk of the phrase and we're done.
case 2:
$chunk[] = $parts[$i];
$chunk[] = $parts[$i + 1];
break;
// If there are three or more words left, we
// build a three word chunk and continue on.
default:
$chunk[] = $parts[$i];
$chunk[] = $parts[$i + 1];
$chunk[] = $parts[$i + 2];
break;
}
// If the chunk is not empty, add it as a phrase.
if (count($chunk))
{
$phrases[] = implode(' ', $chunk);
$terms[] = implode(' ', $chunk);
}
}
}
else
{
// The phrase is <= 3 words so we can use it as is.
$phrases[] = $match;
$terms[] = $match;
}
}
}
}
// Add the remaining terms if present.
if (!empty($input))
{
$terms = array_merge($terms, explode(' ', $input));
}
// An array of our boolean operators. $operator => $translation
$operators = array(
'AND' => JString::strtolower(JText::_('COM_FINDER_QUERY_OPERATOR_AND')),
'OR' => JString::strtolower(JText::_('COM_FINDER_QUERY_OPERATOR_OR')),
'NOT' => JString::strtolower(JText::_('COM_FINDER_QUERY_OPERATOR_NOT'))
);
// If language debugging is enabled you need to ignore the debug strings in matching.
if (JDEBUG)
{
$debugStrings = array('**', '??');
$operators = str_replace($debugStrings, '', $operators);
}
/*
* Iterate through the terms and perform any sorting that needs to be
* done based on boolean search operators. Terms that are before an