]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm
Lots ...
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Storage / QueryParser.pm
1 use strict;
2 use warnings;
3
4 package QueryParser;
5 use OpenSRF::Utils::JSON;
6
7 =head1 NAME
8
9 QueryParser - basic QueryParser class
10
11 =head1 SYNOPSIS
12
13 use QueryParser;
14 my $QParser = QueryParser->new(%args);
15
16 =head1 DESCRIPTION
17
18 Main entrypoint into the QueryParser functionality.
19
20 =head1 FUNCTIONS
21
22 =cut
23
24 # Note that the first key must match the name of the package.
25 our %parser_config = (
26     QueryParser => {
27         filters => [],
28         modifiers => [],
29         operators => { 
30             'and' => '&&',
31             'or' => '||',
32             float_start => '{{',
33             float_end => '}}',
34             group_start => '(',
35             group_end => ')',
36             required => '+',
37             disallowed => '-',
38             modifier => '#'
39         }
40     }
41 );
42
43 sub canonicalize {
44     my $self = shift;
45     return QueryParser::Canonicalize::abstract_query2str_impl(
46         $self->parse_tree->to_abstract_query(@_)
47     );
48 }
49
50
51 =head2 facet_class_count
52
53     $count = $QParser->facet_class_count();
54 =cut
55
56 sub facet_class_count {
57     my $self = shift;
58     return @{$self->facet_classes};
59 }
60
61 =head2 search_class_count
62
63     $count = $QParser->search_class_count();
64 =cut
65
66 sub search_class_count {
67     my $self = shift;
68     return @{$self->search_classes};
69 }
70
71 =head2 filter_count
72
73     $count = $QParser->filter_count();
74 =cut
75
76 sub filter_count {
77     my $self = shift;
78     return @{$self->filters};
79 }
80
81 =head2 modifier_count
82
83     $count = $QParser->modifier_count();
84 =cut
85
86 sub modifier_count {
87     my $self = shift;
88     return @{$self->modifiers};
89 }
90
91 =head2 custom_data
92
93     $data = $QParser->custom_data($class);
94 =cut
95
96 sub custom_data {
97     my $class = shift;
98     $class = ref($class) || $class;
99
100     $parser_config{$class}{custom_data} ||= {};
101     return $parser_config{$class}{custom_data};
102 }
103
104 =head2 operators
105
106     $operators = $QParser->operators();
107
108 Returns hashref of the configured operators.
109 =cut
110
111 sub operators {
112     my $class = shift;
113     $class = ref($class) || $class;
114
115     $parser_config{$class}{operators} ||= {};
116     return $parser_config{$class}{operators};
117 }
118
119 sub allow_nested_modifiers {
120     my $class = shift;
121     my $v = shift;
122     $class = ref($class) || $class;
123
124     $parser_config{$class}{allow_nested_modifiers} = $v if (defined $v);
125     return $parser_config{$class}{allow_nested_modifiers};
126 }
127
128 =head2 filters
129
130     $filters = $QParser->filters();
131
132 Returns arrayref of the configured filters.
133 =cut
134
135 sub filters {
136     my $class = shift;
137     $class = ref($class) || $class;
138
139     $parser_config{$class}{filters} ||= [];
140     return $parser_config{$class}{filters};
141 }
142
143 =head2 filter_callbacks
144
145     $filter_callbacks = $QParser->filter_callbacks();
146
147 Returns hashref of the configured filter callbacks.
148 =cut
149
150 sub filter_callbacks {
151     my $class = shift;
152     $class = ref($class) || $class;
153
154     $parser_config{$class}{filter_callbacks} ||= {};
155     return $parser_config{$class}{filter_callbacks};
156 }
157
158 =head2 modifiers
159
160     $modifiers = $QParser->modifiers();
161
162 Returns arrayref of the configured modifiers.
163 =cut
164
165 sub modifiers {
166     my $class = shift;
167     $class = ref($class) || $class;
168
169     $parser_config{$class}{modifiers} ||= [];
170     return $parser_config{$class}{modifiers};
171 }
172
173 =head2 new
174
175     $QParser = QueryParser->new(%args);
176
177 Creates a new QueryParser object.
178 =cut
179
180 sub new {
181     my $class = shift;
182     $class = ref($class) || $class;
183
184     my %opts = @_;
185
186     my $self = bless {} => $class;
187
188     for my $o (keys %{QueryParser->operators}) {
189         $class->operator($o => QueryParser->operator($o)) unless ($class->operator($o));
190     }
191
192     for my $opt ( keys %opts) {
193         $self->$opt( $opts{$opt} ) if ($self->can($opt));
194     }
195
196     return $self;
197 }
198
199 =head2 new_plan
200
201     $query_plan = $QParser->new_plan();
202
203 Create a new query plan.
204 =cut
205
206 sub new_plan {
207     my $self = shift;
208     my $pkg = ref($self) || $self;
209     return do{$pkg.'::query_plan'}->new( QueryParser => $self, @_ );
210 }
211
212 =head2 add_search_filter
213
214     $QParser->add_search_filter($filter, [$callback]);
215
216 Adds a filter with the specified name and an optional callback to the
217 QueryParser configuration.
218 =cut
219
220 sub add_search_filter {
221     my $pkg = shift;
222     $pkg = ref($pkg) || $pkg;
223     my $filter = shift;
224     my $callback = shift;
225
226     return $filter if (grep { $_ eq $filter } @{$pkg->filters});
227     push @{$pkg->filters}, $filter;
228     $pkg->filter_callbacks->{$filter} = $callback if ($callback);
229     return $filter;
230 }
231
232 =head2 add_search_modifier
233
234     $QParser->add_search_modifier($modifier);
235
236 Adds a modifier with the specified name to the QueryParser configuration.
237 =cut
238
239 sub add_search_modifier {
240     my $pkg = shift;
241     $pkg = ref($pkg) || $pkg;
242     my $modifier = shift;
243
244     return $modifier if (grep { $_ eq $modifier } @{$pkg->modifiers});
245     push @{$pkg->modifiers}, $modifier;
246     return $modifier;
247 }
248
249 =head2 add_facet_class
250
251     $QParser->add_facet_class($facet_class);
252
253 Adds a facet class with the specified name to the QueryParser configuration.
254 =cut
255
256 sub add_facet_class {
257     my $pkg = shift;
258     $pkg = ref($pkg) || $pkg;
259     my $class = shift;
260
261     return $class if (grep { $_ eq $class } @{$pkg->facet_classes});
262
263     push @{$pkg->facet_classes}, $class;
264     $pkg->facet_fields->{$class} = [];
265
266     return $class;
267 }
268
269 =head2 add_search_class
270
271     $QParser->add_search_class($class);
272
273 Adds a search class with the specified name to the QueryParser configuration.
274 =cut
275
276 sub add_search_class {
277     my $pkg = shift;
278     $pkg = ref($pkg) || $pkg;
279     my $class = shift;
280
281     return $class if (grep { $_ eq $class } @{$pkg->search_classes});
282
283     push @{$pkg->search_classes}, $class;
284     $pkg->search_fields->{$class} = [];
285     $pkg->default_search_class( $pkg->search_classes->[0] ) if (@{$pkg->search_classes} == 1);
286
287     return $class;
288 }
289
290 =head2 add_search_modifier
291
292     $op = $QParser->operator($operator, [$newvalue]);
293
294 Retrieves or sets value for the specified operator. Valid operators and
295 their defaults are as follows:
296
297 =over 4
298
299 =item * and => &&
300
301 =item * or => ||
302
303 =item * group_start => (
304
305 =item * group_end => )
306
307 =item * required => +
308
309 =item * disallowed => -
310
311 =item * modifier => #
312
313 =back
314
315 =cut
316
317 sub operator {
318     my $class = shift;
319     $class = ref($class) || $class;
320     my $opname = shift;
321     my $op = shift;
322
323     return undef unless ($opname);
324
325     $parser_config{$class}{operators} ||= {};
326     $parser_config{$class}{operators}{$opname} = $op if ($op);
327
328     return $parser_config{$class}{operators}{$opname};
329 }
330
331 =head2 facet_classes
332
333     $classes = $QParser->facet_classes([\@newclasses]);
334
335 Returns arrayref of all configured facet classes after optionally
336 replacing configuration.
337 =cut
338
339 sub facet_classes {
340     my $class = shift;
341     $class = ref($class) || $class;
342     my $classes = shift;
343
344     $parser_config{$class}{facet_classes} ||= [];
345     $parser_config{$class}{facet_classes} = $classes if (ref($classes) && @$classes);
346     return $parser_config{$class}{facet_classes};
347 }
348
349 =head2 search_classes
350
351     $classes = $QParser->search_classes([\@newclasses]);
352
353 Returns arrayref of all configured search classes after optionally
354 replacing the previous configuration.
355 =cut
356
357 sub search_classes {
358     my $class = shift;
359     $class = ref($class) || $class;
360     my $classes = shift;
361
362     $parser_config{$class}{classes} ||= [];
363     $parser_config{$class}{classes} = $classes if (ref($classes) && @$classes);
364     return $parser_config{$class}{classes};
365 }
366
367 =head2 add_query_normalizer
368
369     $function = $QParser->add_query_normalizer($class, $field, $func, [\@params]);
370
371 =cut
372
373 sub add_query_normalizer {
374     my $pkg = shift;
375     $pkg = ref($pkg) || $pkg;
376     my $class = shift;
377     my $field = shift;
378     my $func = shift;
379     my $params = shift || [];
380
381     # do not add if function AND params are identical to existing member
382     return $func if (grep {
383         $_->{function} eq $func and 
384         OpenSRF::Utils::JSON->perl2JSON($_->{params}) eq OpenSRF::Utils::JSON->perl2JSON($params)
385     } @{$pkg->query_normalizers->{$class}->{$field}});
386
387     push(@{$pkg->query_normalizers->{$class}->{$field}}, { function => $func, params => $params });
388
389     return $func;
390 }
391
392 =head2 query_normalizers
393
394     $normalizers = $QParser->query_normalizers($class, $field);
395
396 Returns a list of normalizers associated with the specified search class
397 and field
398 =cut
399
400 sub query_normalizers {
401     my $pkg = shift;
402     $pkg = ref($pkg) || $pkg;
403
404     my $class = shift;
405     my $field = shift;
406
407     $parser_config{$pkg}{normalizers} ||= {};
408     if ($class) {
409         if ($field) {
410             $parser_config{$pkg}{normalizers}{$class}{$field} ||= [];
411             return $parser_config{$pkg}{normalizers}{$class}{$field};
412         } else {
413             return $parser_config{$pkg}{normalizers}{$class};
414         }
415     }
416
417     return $parser_config{$pkg}{normalizers};
418 }
419
420 =head2 add_filter_normalizer
421
422     $normalizer = $QParser->add_filter_normalizer($filter, $func, [\@params]);
423
424 Adds a normalizer function to the specified filter.
425 =cut
426
427 sub add_filter_normalizer {
428     my $pkg = shift;
429     $pkg = ref($pkg) || $pkg;
430     my $filter = shift;
431     my $func = shift;
432     my $params = shift || [];
433
434     return $func if (grep { $_ eq $func } @{$pkg->filter_normalizers->{$filter}});
435
436     push(@{$pkg->filter_normalizers->{$filter}}, { function => $func, params => $params });
437
438     return $func;
439 }
440
441 =head2 filter_normalizers
442
443     $normalizers = $QParser->filter_normalizers($filter);
444
445 Return arrayref of normalizer functions associated with the specified filter.
446 =cut
447
448 sub filter_normalizers {
449     my $pkg = shift;
450     $pkg = ref($pkg) || $pkg;
451
452     my $filter = shift;
453
454     $parser_config{$pkg}{filter_normalizers} ||= {};
455     if ($filter) {
456         $parser_config{$pkg}{filter_normalizers}{$filter} ||= [];
457         return $parser_config{$pkg}{filter_normalizers}{$filter};
458     }
459
460     return $parser_config{$pkg}{filter_normalizers};
461 }
462
463 =head2 default_search_class
464
465     $default_class = $QParser->default_search_class([$class]);
466
467 Set or return the default search class.
468 =cut
469
470 sub default_search_class {
471     my $pkg = shift;
472     $pkg = ref($pkg) || $pkg;
473     my $class = shift;
474     $QueryParser::parser_config{$pkg}{default_class} = $pkg->add_search_class( $class ) if $class;
475
476     return $QueryParser::parser_config{$pkg}{default_class};
477 }
478
479 =head2 remove_facet_class
480
481     $QParser->remove_facet_class($class);
482
483 Remove the specified facet class from the configuration.
484 =cut
485
486 sub remove_facet_class {
487     my $pkg = shift;
488     $pkg = ref($pkg) || $pkg;
489     my $class = shift;
490
491     return $class if (!grep { $_ eq $class } @{$pkg->facet_classes});
492
493     $pkg->facet_classes( [ grep { $_ ne $class } @{$pkg->facet_classes} ] );
494     delete $QueryParser::parser_config{$pkg}{facet_fields}{$class};
495
496     return $class;
497 }
498
499 =head2 remove_search_class
500
501     $QParser->remove_search_class($class);
502
503 Remove the specified search class from the configuration.
504 =cut
505
506 sub remove_search_class {
507     my $pkg = shift;
508     $pkg = ref($pkg) || $pkg;
509     my $class = shift;
510
511     return $class if (!grep { $_ eq $class } @{$pkg->search_classes});
512
513     $pkg->search_classes( [ grep { $_ ne $class } @{$pkg->search_classes} ] );
514     delete $QueryParser::parser_config{$pkg}{fields}{$class};
515
516     return $class;
517 }
518
519 =head2 add_facet_field
520
521     $QParser->add_facet_field($class, $field);
522
523 Adds the specified field (and facet class if it doesn't already exist)
524 to the configuration.
525 =cut
526
527 sub add_facet_field {
528     my $pkg = shift;
529     $pkg = ref($pkg) || $pkg;
530     my $class = shift;
531     my $field = shift;
532
533     $pkg->add_facet_class( $class );
534
535     return { $class => $field }  if (grep { $_ eq $field } @{$pkg->facet_fields->{$class}});
536
537     push @{$pkg->facet_fields->{$class}}, $field;
538
539     return { $class => $field };
540 }
541
542 =head2 facet_fields
543
544     $fields = $QParser->facet_fields($class);
545
546 Returns arrayref with list of fields for specified facet class.
547 =cut
548
549 sub facet_fields {
550     my $class = shift;
551     $class = ref($class) || $class;
552
553     $parser_config{$class}{facet_fields} ||= {};
554     return $parser_config{$class}{facet_fields};
555 }
556
557 =head2 add_search_field
558
559     $QParser->add_search_field($class, $field);
560
561 Adds the specified field (and facet class if it doesn't already exist)
562 to the configuration.
563 =cut
564
565 sub add_search_field {
566     my $pkg = shift;
567     $pkg = ref($pkg) || $pkg;
568     my $class = shift;
569     my $field = shift;
570
571     $pkg->add_search_class( $class );
572
573     return { $class => $field }  if (grep { $_ eq $field } @{$pkg->search_fields->{$class}});
574
575     push @{$pkg->search_fields->{$class}}, $field;
576
577     return { $class => $field };
578 }
579
580 =head2 search_fields
581
582     $fields = $QParser->search_fields();
583
584 Returns arrayref with list of configured search fields.
585 =cut
586
587 sub search_fields {
588     my $class = shift;
589     $class = ref($class) || $class;
590
591     $parser_config{$class}{fields} ||= {};
592     return $parser_config{$class}{fields};
593 }
594
595 =head2 add_search_class_alias
596
597     $QParser->add_search_class_alias($class, $alias);
598 =cut
599
600 sub add_search_class_alias {
601     my $pkg = shift;
602     $pkg = ref($pkg) || $pkg;
603     my $class = shift;
604     my $alias = shift;
605
606     $pkg->add_search_class( $class );
607
608     return { $class => $alias }  if (grep { $_ eq $alias } @{$pkg->search_class_aliases->{$class}});
609
610     push @{$pkg->search_class_aliases->{$class}}, $alias;
611
612     return { $class => $alias };
613 }
614
615 =head2 search_class_aliases
616
617     $aliases = $QParser->search_class_aliases($class);
618 =cut
619
620 sub search_class_aliases {
621     my $class = shift;
622     $class = ref($class) || $class;
623
624     $parser_config{$class}{class_map} ||= {};
625     return $parser_config{$class}{class_map};
626 }
627
628 =head2 add_search_field_alias
629
630     $QParser->add_search_field_alias($class, $field, $alias);
631 =cut
632
633 sub add_search_field_alias {
634     my $pkg = shift;
635     $pkg = ref($pkg) || $pkg;
636     my $class = shift;
637     my $field = shift;
638     my $alias = shift;
639
640     return { $class => { $field => $alias } }  if (grep { $_ eq $alias } @{$pkg->search_field_aliases->{$class}{$field}});
641
642     push @{$pkg->search_field_aliases->{$class}{$field}}, $alias;
643
644     return { $class => { $field => $alias } };
645 }
646
647 =head2 search_field_aliases
648
649     $aliases = $QParser->search_field_aliases();
650 =cut
651
652 sub search_field_aliases {
653     my $class = shift;
654     $class = ref($class) || $class;
655
656     $parser_config{$class}{field_alias_map} ||= {};
657     return $parser_config{$class}{field_alias_map};
658 }
659
660 =head2 remove_facet_field
661
662     $QParser->remove_facet_field($class, $field);
663 =cut
664
665 sub remove_facet_field {
666     my $pkg = shift;
667     $pkg = ref($pkg) || $pkg;
668     my $class = shift;
669     my $field = shift;
670
671     return { $class => $field }  if (!$pkg->facet_fields->{$class} || !grep { $_ eq $field } @{$pkg->facet_fields->{$class}});
672
673     $pkg->facet_fields->{$class} = [ grep { $_ ne $field } @{$pkg->facet_fields->{$class}} ];
674
675     return { $class => $field };
676 }
677
678 =head2 remove_search_field
679
680     $QParser->remove_search_field($class, $field);
681 =cut
682
683 sub remove_search_field {
684     my $pkg = shift;
685     $pkg = ref($pkg) || $pkg;
686     my $class = shift;
687     my $field = shift;
688
689     return { $class => $field }  if (!$pkg->search_fields->{$class} || !grep { $_ eq $field } @{$pkg->search_fields->{$class}});
690
691     $pkg->search_fields->{$class} = [ grep { $_ ne $field } @{$pkg->search_fields->{$class}} ];
692
693     return { $class => $field };
694 }
695
696 =head2 remove_search_field_alias
697
698     $QParser->remove_search_field_alias($class, $field, $alias);
699 =cut
700
701 sub remove_search_field_alias {
702     my $pkg = shift;
703     $pkg = ref($pkg) || $pkg;
704     my $class = shift;
705     my $field = shift;
706     my $alias = shift;
707
708     return { $class => { $field => $alias } }  if (!$pkg->search_field_aliases->{$class}{$field} || !grep { $_ eq $alias } @{$pkg->search_field_aliases->{$class}{$field}});
709
710     $pkg->search_field_aliases->{$class}{$field} = [ grep { $_ ne $alias } @{$pkg->search_field_aliases->{$class}{$field}} ];
711
712     return { $class => { $field => $alias } };
713 }
714
715 =head2 remove_search_class_alias
716
717     $QParser->remove_search_class_alias($class, $alias);
718 =cut
719
720 sub remove_search_class_alias {
721     my $pkg = shift;
722     $pkg = ref($pkg) || $pkg;
723     my $class = shift;
724     my $alias = shift;
725
726     return { $class => $alias }  if (!$pkg->search_class_aliases->{$class} || !grep { $_ eq $alias } @{$pkg->search_class_aliases->{$class}});
727
728     $pkg->search_class_aliases->{$class} = [ grep { $_ ne $alias } @{$pkg->search_class_aliases->{$class}} ];
729
730     return { $class => $alias };
731 }
732
733 =head2 debug
734
735     $debug = $QParser->debug([$debug]);
736
737 Return or set whether debugging output is enabled.
738 =cut
739
740 sub debug {
741     my $self = shift;
742     my $q = shift;
743     $self->{_debug} = $q if (defined $q);
744     return $self->{_debug};
745 }
746
747 =head2 query
748
749     $query = $QParser->query([$query]);
750
751 Return or set the query.
752 =cut
753
754 sub query {
755     my $self = shift;
756     my $q = shift;
757     $self->{_query} = " $q " if (defined $q);
758     return $self->{_query};
759 }
760
761 =head2 parse_tree
762
763     $parse_tree = $QParser->parse_tree([$parse_tree]);
764
765 Return or set the parse tree associated with the QueryParser.
766 =cut
767
768 sub parse_tree {
769     my $self = shift;
770     my $q = shift;
771     $self->{_parse_tree} = $q if (defined $q);
772     return $self->{_parse_tree};
773 }
774
775 sub floating_plan {
776     my $self = shift;
777     my $q = shift;
778     $self->{_top} = $q if (defined $q);
779     return $self->{_top};
780 }
781
782 =head2 parse
783
784     $QParser->parse([$query]);
785
786 Parse the specified query, or the query already associated with the QueryParser
787 object.
788 =cut
789
790 sub parse {
791     my $self = shift;
792     my $pkg = ref($self) || $self;
793     warn " ** parse package is $pkg\n" if $self->debug;
794 #    $self->parse_tree(
795 #        $self->decompose(
796 #            $self->query( shift() )
797 #        )
798 #    );
799
800     $self->decompose( $self->query( shift() ) );
801
802     if ($self->floating_plan) {
803         $self->floating_plan->add_node( $self->parse_tree );
804         $self->parse_tree( $self->floating_plan );
805     }
806
807     $self->parse_tree->plan_level(0);
808
809     return $self;
810 }
811
812 =head2 decompose
813
814     ($struct, $remainder) = $QParser->decompose($querystring, [$current_class], [$recursing], [$phrase_helper]);
815
816 This routine does the heavy work of parsing the query string recursively.
817 Returns the top level query plan, or the query plan from a lower level plus
818 the portion of the query string that needs to be processed at a higher level.
819 =cut
820
821 our $last_class = '';
822 our $last_type = '';
823 our $floating = 0;
824 our $fstart;
825
826 sub decompose {
827     my $self = shift;
828     my $pkg = ref($self) || $self;
829
830
831     $_ = shift;
832     my $current_class = shift || $self->default_search_class;
833
834     my $recursing = shift || 0;
835     my $phrase_helper = shift || 0;
836
837     # Build the search class+field uber-regexp
838     my $search_class_re = '^\s*(';
839     my $first_class = 1;
840
841     warn '  'x$recursing." ** decompose package is $pkg\n" if $self->debug;
842
843     my %seen_classes;
844     for my $class ( keys %{$pkg->search_field_aliases} ) {
845         warn '  'x$recursing." *** ... Looking for search fields in $class\n" if $self->debug;
846
847         for my $field ( keys %{$pkg->search_field_aliases->{$class}} ) {
848             warn '  'x$recursing." *** ... Looking for aliases of $field\n" if $self->debug;
849
850             for my $alias ( @{$pkg->search_field_aliases->{$class}{$field}} ) {
851                 my $aliasr = qr/$alias/;
852                 s/(^|\s+)$aliasr\|/$1$class\|$field#$alias\|/g;
853                 s/(^|\s+)$aliasr[:=]/$1$class\|$field#$alias:/g;
854                 warn '  'x$recursing." *** Rewriting: $alias ($aliasr) as $class\|$field\n" if $self->debug;
855             }
856         }
857
858         $search_class_re .= '|' unless ($first_class);
859         $first_class = 0;
860         $search_class_re .= $class . '(?:[|#][^:|]+)*';
861         $seen_classes{$class} = 1;
862     }
863
864     for my $class ( keys %{$pkg->search_class_aliases} ) {
865
866         for my $alias ( @{$pkg->search_class_aliases->{$class}} ) {
867             my $aliasr = qr/$alias/;
868             s/(^|[^|])\b$aliasr\|/$1$class#$alias\|/g;
869             s/(^|[^|])\b$aliasr[:=]/$1$class#$alias:/g;
870             warn '  'x$recursing." *** Rewriting: $alias ($aliasr) as $class\n" if $self->debug;
871         }
872
873         if (!$seen_classes{$class}) {
874             $search_class_re .= '|' unless ($first_class);
875             $first_class = 0;
876
877             $search_class_re .= $class . '(?:[|#][^:|]+)*';
878             $seen_classes{$class} = 1;
879         }
880     }
881     $search_class_re .= '):';
882
883     warn '  'x$recursing." ** Rewritten query: $_\n" if $self->debug;
884     warn '  'x$recursing." ** Search class RE: $search_class_re\n" if $self->debug;
885
886     my $required_re = $pkg->operator('required');
887     $required_re = qr/\Q$required_re\E/;
888
889     my $disallowed_re = $pkg->operator('disallowed');
890     $disallowed_re = qr/\Q$disallowed_re\E/;
891
892     my $and_re = $pkg->operator('and');
893     $and_re = qr/^\s*\Q$and_re\E/;
894
895     my $or_re = $pkg->operator('or');
896     $or_re = qr/^\s*\Q$or_re\E/;
897
898     my $group_start = $pkg->operator('group_start');
899     my $group_start_re = qr/^\s*\Q$group_start\E/;
900
901     my $group_end = $pkg->operator('group_end');
902     my $group_end_re = qr/^\s*\Q$group_end\E/;
903
904     my $float_start = $pkg->operator('float_start');
905     my $float_start_re = qr/^\s*\Q$float_start\E/;
906
907     my $float_end = $pkg->operator('float_end');
908     my $float_end_re = qr/^\s*\Q$float_end\E/;
909
910     my $modifier_tag_re = $pkg->operator('modifier');
911     $modifier_tag_re = qr/^\s*\Q$modifier_tag_re\E/;
912
913
914     # Build the filter and modifier uber-regexps
915     my $facet_re = '^\s*(-?)((?:' . join( '|', @{$pkg->facet_classes}) . ')(?:\|\w+)*)\[(.+?)\]';
916     warn '  'x$recursing." ** Facet RE: $facet_re\n" if $self->debug;
917
918     my $filter_re = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . ')\(([^()]+)\)';
919     my $filter_as_class_re = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . '):\s*(\S+)';
920
921     my $modifier_re = '^\s*'.$modifier_tag_re.'(' . join( '|', @{$pkg->modifiers}) . ')\b';
922     my $modifier_as_class_re = '^\s*(' . join( '|', @{$pkg->modifiers}) . '):\s*(\S+)';
923
924     my $struct = shift || $self->new_plan( level => $recursing );
925     $self->parse_tree( $struct ) if (!$self->parse_tree);
926
927     my $remainder = '';
928
929     while (!$remainder) {
930         warn '  'x$recursing."Start of the loop. last_type: $last_type, joiner: ".$struct->joiner.", struct: $struct\n" if $self->debug;
931         if ($last_type eq 'FEND' and $fstart and $fstart !=  $struct) { # fall back further
932             $remainder = $_;
933             last;
934         } elsif ($last_type eq 'FEND') {
935             $fstart = undef;
936             $last_type = '';
937         }
938
939         if (/^\s*$/) { # end of an explicit group
940             local $last_type = '';
941             last;
942         } elsif (/$float_end_re/) { # end of an explicit group
943             warn '  'x$recursing."Encountered explicit float end, remainder: $'\n" if $self->debug;
944
945             $remainder = $';
946             $_ = '';
947
948             $floating = 0;
949             $last_type = 'FEND';
950             last;
951         } elsif (/$group_end_re/) { # end of an explicit group
952             warn '  'x$recursing."Encountered explicit group end, remainder: $'\n" if $self->debug;
953
954             $remainder = $';
955             $_ = '';
956
957             local $last_type = '';
958         } elsif ($self->filter_count && /$filter_re/) { # found a filter
959             warn '  'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
960
961             my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
962             $_ = $';
963
964             my $filter = $2;
965             my $params = [ split '[,]+', $3 ];
966
967             if ($pkg->filter_callbacks->{$filter}) {
968                 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
969                 $_ = "$replacement $_" if ($replacement);
970             } else {
971                 $struct->new_filter( $filter => $params, $negate );
972             }
973
974
975             local $last_type = '';
976         } elsif ($self->filter_count && /$filter_as_class_re/) { # found a filter
977             warn '  'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
978
979             my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
980             $_ = $';
981
982             my $filter = $2;
983             my $params = [ split '[,]+', $3 ];
984
985             if ($pkg->filter_callbacks->{$filter}) {
986                 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
987                 $_ = "$replacement $_" if ($replacement);
988             } else {
989                 $struct->new_filter( $filter => $params, $negate );
990             }
991
992             local $last_type = '';
993         } elsif ($self->modifier_count && /$modifier_re/) { # found a modifier
994             warn '  'x$recursing."Encountered search modifier: $1\n" if $self->debug;
995
996             $_ = $';
997             if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
998                 warn '  'x$recursing."  Search modifiers only allowed at the top level of the query\n" if $self->debug;
999             } else {
1000                 $struct->new_modifier($1);
1001             }
1002
1003             local $last_type = '';
1004         } elsif ($self->modifier_count && /$modifier_as_class_re/) { # found a modifier
1005             warn '  'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1006
1007             my $mod = $1;
1008
1009             $_ = $';
1010             if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1011                 warn '  'x$recursing."  Search modifiers only allowed at the top level of the query\n" if $self->debug;
1012             } elsif ($2 =~ /^[ty1]/i) {
1013                 $struct->new_modifier($mod);
1014             }
1015
1016             local $last_type = '';
1017         } elsif (/$float_start_re/) { # start of an explicit float
1018             warn '  'x$recursing."Encountered explicit float start\n" if $self->debug;
1019             $floating = 1;
1020             $fstart = $struct;
1021
1022             $last_class = $current_class;
1023             $current_class = undef;
1024
1025             $self->floating_plan( $self->new_plan( floating => 1 ) ) if (!$self->floating_plan);
1026
1027             # pass the floating_plan struct to be modified by the float'ed chunk
1028             my ($floating_plan, $subremainder) = $self->new( debug => $self->debug )->decompose( $', undef, undef, undef,  $self->floating_plan);
1029             $_ = $subremainder;
1030             warn '  'x$recursing."Remainder after explicit float: $_\n" if $self->debug;
1031
1032             $current_class = $last_class;
1033
1034             $last_type = '';
1035         } elsif (/$group_start_re/) { # start of an explicit group
1036             warn '  'x$recursing."Encountered explicit group start\n" if $self->debug;
1037
1038             my ($substruct, $subremainder) = $self->decompose( $', $current_class, $recursing + 1 );
1039             $struct->add_node( $substruct ) if ($substruct);
1040             $_ = $subremainder;
1041             warn '  'x$recursing."Query remainder after bool group: $_\n" if $self->debug;
1042
1043             local $last_type = '';
1044
1045         } elsif (/$and_re/) { # ANDed expression
1046             $_ = $';
1047             warn '  'x$recursing."Encountered AND\n" if $self->debug;
1048             do {warn '  'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1049             do {warn '  'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1050             local $last_type = 'AND';
1051
1052             warn '  'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1053             my $LHS = $struct;
1054             #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1055             my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 1 );
1056             $_ = $subremainder;
1057
1058             warn '  'x$recursing."RHS built\n" if $self->debug;
1059             warn '  'x$recursing."Post-AND remainder: $subremainder\n" if $self->debug;
1060
1061             my $wrapper = $self->new_plan( level => $recursing + 1 );
1062
1063             if ($LHS->floating) {
1064                 $wrapper->{query} = $LHS->{query};
1065                 my $outer_wrapper = $self->new_plan( level => $recursing + 1 );
1066                 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1067                 $LHS->{query} = [$outer_wrapper];
1068                 $struct = $LHS;
1069             } else {
1070                 $wrapper->add_node($_) for ($LHS, $RHS);
1071                 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1072                 $struct = $self->new_plan( level => $recursing );
1073                 $struct->add_node($wrapper);
1074             }
1075
1076             $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1077
1078             local $last_type = '';
1079         } elsif (/$or_re/) { # ORed expression
1080             $_ = $';
1081             warn '  'x$recursing."Encountered OR\n" if $self->debug;
1082             do {warn '  'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1083             do {warn '  'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1084             local $last_type = 'OR';
1085
1086             warn '  'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1087             my $LHS = $struct;
1088             #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1089             my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 2 );
1090             $_ = $subremainder;
1091
1092             warn '  'x$recursing."RHS built\n" if $self->debug;
1093             warn '  'x$recursing."Post-OR remainder: $subremainder\n" if $self->debug;
1094
1095             my $wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1096
1097             if ($LHS->floating) {
1098                 $wrapper->{query} = $LHS->{query};
1099                 my $outer_wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1100                 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1101                 $LHS->{query} = [$outer_wrapper];
1102                 $struct = $LHS;
1103             } else {
1104                 $wrapper->add_node($_) for ($LHS, $RHS);
1105                 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1106                 $struct = $self->new_plan( level => $recursing );
1107                 $struct->add_node($wrapper);
1108             }
1109
1110             $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1111
1112             local $last_type = '';
1113         } elsif ($self->facet_class_count && /$facet_re/) { # changing current class
1114             warn '  'x$recursing."Encountered facet: $1$2 => $3\n" if $self->debug;
1115
1116             my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1117             my $facet = $2;
1118             my $facet_value = [ split '\s*#\s*', $3 ];
1119             $struct->new_facet( $facet => $facet_value, $negate );
1120             $_ = $';
1121
1122             local $last_type = '';
1123         } elsif ($self->search_class_count && /$search_class_re/) { # changing current class
1124
1125             if ($last_type eq 'CLASS') {
1126                 $struct->remove_last_node( $current_class );
1127                 warn '  'x$recursing."Encountered class change with no searches!\n" if $self->debug;
1128             }
1129
1130             warn '  'x$recursing."Encountered class change: $1\n" if $self->debug;
1131
1132             $current_class = $struct->classed_node( $1 )->requested_class();
1133             $_ = $';
1134
1135             local $last_type = 'CLASS';
1136         } elsif (/^\s*($required_re|$disallowed_re)?"([^"]+)"/) { # phrase, always anded
1137             warn '  'x$recursing.'Encountered' . ($1 ? " ['$1' modified]" : '') . " phrase: $2\n" if $self->debug;
1138
1139             my $req_ness = $1 || '';
1140             my $phrase = $2;
1141
1142             if (!$phrase_helper) {
1143                 warn '  'x$recursing."Recursing into decompose with the phrase as a subquery\n" if $self->debug;
1144                 my $after = $';
1145                 my ($substruct, $subremainder) = $self->decompose( qq/$req_ness"$phrase"/, $current_class, $recursing + 1, 1 );
1146                 $struct->add_node( $substruct ) if ($substruct);
1147                 $_ = $after;
1148             } else {
1149                 warn '  'x$recursing."Directly parsing the phrase subquery\n" if $self->debug;
1150                 $struct->joiner( '&' );
1151
1152                 my $class_node = $struct->classed_node($current_class);
1153
1154                 if ($req_ness eq $pkg->operator('disallowed')) {
1155                     $class_node->add_dummy_atom( node => $class_node );
1156                     $class_node->add_unphrase( $phrase );
1157                     $phrase = '';
1158                     #$phrase =~ s/(^|\s)\b/$1-/g;
1159                 } else { 
1160                     $class_node->add_phrase( $phrase );
1161                 }
1162                 $_ = $phrase . $';
1163
1164             }
1165
1166             local $last_type = '';
1167
1168         } elsif (/^\s*$required_re([^${group_end}${float_end}\s"]+)/) { # phrase, always anded
1169             warn '  'x$recursing."Encountered required atom (mini phrase), transforming for phrase parse: $1\n" if $self->debug;
1170
1171             $_ = '"' . $1 . '"' . $';
1172
1173             local $last_type = '';
1174         } elsif (/^\s*([^${group_end}${float_end}\s]+)/o) { # atom
1175             warn '  'x$recursing."Encountered atom: $1\n" if $self->debug;
1176             warn '  'x$recursing."Remainder: $'\n" if $self->debug;
1177
1178             my $atom = $1;
1179             my $after = $';
1180
1181             $_ = $after;
1182             local $last_type = '';
1183
1184             my $class_node = $struct->classed_node($current_class);
1185
1186             my $prefix = ($atom =~ s/^$disallowed_re//o) ? '!' : '';
1187             my $truncate = ($atom =~ s/\*$//o) ? '*' : '';
1188
1189             if ($atom ne '' and !grep { $atom =~ /^\Q$_\E+$/ } ('&','|')) { # throw away & and |, not allowed in tsquery, and not really useful anyway
1190 #                $class_node->add_phrase( $atom ) if ($atom =~ s/^$required_re//o);
1191 #                $class_node->add_unphrase( $atom ) if ($prefix eq '!');
1192
1193                 $class_node->add_fts_atom( $atom, suffix => $truncate, prefix => $prefix, node => $class_node );
1194                 $struct->joiner( '&' );
1195             }
1196
1197             local $last_type = '';
1198         } 
1199
1200         last unless ($_);
1201
1202     }
1203
1204     $struct = undef if 
1205         scalar(@{$struct->query_nodes}) == 0 &&
1206         scalar(@{$struct->filters}) == 0 &&
1207         !$struct->top_plan;
1208
1209     return $struct if !wantarray;
1210     return ($struct, $remainder);
1211 }
1212
1213 =head2 find_class_index
1214
1215     $index = $QParser->find_class_index($class, $query);
1216 =cut
1217
1218 sub find_class_index {
1219     my $class = shift;
1220     my $query = shift;
1221
1222     my ($class_part, @field_parts) = split '\|', $class;
1223     $class_part ||= $class;
1224
1225     for my $idx ( 0 .. scalar(@$query) - 1 ) {
1226         next unless ref($$query[$idx]);
1227         return $idx if ( $$query[$idx]{requested_class} && $class eq $$query[$idx]{requested_class} );
1228     }
1229
1230     push(@$query, { classname => $class_part, (@field_parts ? (fields => \@field_parts) : ()), requested_class => $class, ftsquery => [], phrases => [] });
1231     return -1;
1232 }
1233
1234 =head2 core_limit
1235
1236     $limit = $QParser->core_limit([$limit]);
1237
1238 Return and/or set the core_limit.
1239 =cut
1240
1241 sub core_limit {
1242     my $self = shift;
1243     my $l = shift;
1244     $self->{core_limit} = $l if ($l);
1245     return $self->{core_limit};
1246 }
1247
1248 =head2 superpage
1249
1250     $superpage = $QParser->superpage([$superpage]);
1251
1252 Return and/or set the superpage.
1253 =cut
1254
1255 sub superpage {
1256     my $self = shift;
1257     my $l = shift;
1258     $self->{superpage} = $l if ($l);
1259     return $self->{superpage};
1260 }
1261
1262 =head2 superpage_size
1263
1264     $size = $QParser->superpage_size([$size]);
1265
1266 Return and/or set the superpage size.
1267 =cut
1268
1269 sub superpage_size {
1270     my $self = shift;
1271     my $l = shift;
1272     $self->{superpage_size} = $l if ($l);
1273     return $self->{superpage_size};
1274 }
1275
1276
1277 #-------------------------------
1278 package QueryParser::_util;
1279
1280 # At this level, joiners are always & or |.  This is not
1281 # the external, configurable representation of joiners that
1282 # defaults to # && and ||.
1283 sub is_joiner {
1284     my $str = shift;
1285
1286     return (not ref $str and ($str eq '&' or $str eq '|'));
1287 }
1288
1289 sub default_joiner { '&' }
1290
1291 # 0 for different, 1 for the same.
1292 sub compare_abstract_atoms {
1293     my ($left, $right) = @_;
1294
1295     foreach (qw/prefix suffix content/) {
1296         no warnings;    # undef can stand in for '' here
1297         return 0 unless $left->{$_} eq $right->{$_};
1298     }
1299
1300     return 1;
1301 }
1302
1303 sub fake_abstract_atom_from_phrase {
1304     my $phrase = shift;
1305     my $neg = shift;
1306     my $qp_class = shift || 'QueryParser';
1307
1308     my $prefix = '"';
1309     if ($neg) {
1310         $prefix =
1311             $QueryParser::parser_config{$qp_class}{operators}{disallowed} .
1312             $prefix;
1313     }
1314
1315     return {
1316         "type" => "atom", "prefix" => $prefix, "suffix" => '"',
1317         "content" => $phrase
1318     }
1319 }
1320
1321 sub find_arrays_in_abstract {
1322     my ($hash) = @_;
1323
1324     my @arrays;
1325     foreach my $key (keys %$hash) {
1326         if (ref $hash->{$key} eq "ARRAY") {
1327             push @arrays, $hash->{$key};
1328             foreach (@{$hash->{$key}}) {
1329                 push @arrays, find_arrays_in_abstract($_);
1330             }
1331         }
1332     }
1333
1334     return @arrays;
1335 }
1336
1337 #-------------------------------
1338 package QueryParser::Canonicalize;  # not OO
1339 use Data::Dumper;
1340
1341 sub _abstract_query2str_filter {
1342     my $f = shift;
1343     my $qp_class = shift || 'QueryParser';
1344     my $qpconfig = $QueryParser::parser_config{$qp_class};
1345
1346     return sprintf(
1347         '%s%s(%s)',
1348         $f->{negate} ? $qpconfig->{operators}{disallowed} : "",
1349         $f->{name},
1350         join(",", @{$f->{args}})
1351     );
1352 }
1353
1354 sub _abstract_query2str_modifier {
1355     my $f = shift;
1356     my $qp_class = shift || 'QueryParser';
1357     my $qpconfig = $QueryParser::parser_config{$qp_class};
1358
1359     return $qpconfig->{operators}{modifier} . $f;
1360 }
1361
1362 sub _kid_list {
1363     my $children = shift;
1364     my $op = (keys %$children)[0];
1365     return @{$$children{$op}};
1366 }
1367
1368
1369 # This should produce an equivalent query to the original, given an
1370 # abstract_query.
1371 sub abstract_query2str_impl {
1372     my $abstract_query  = shift;
1373     my $depth = shift || 0;
1374
1375     my $qp_class ||= shift || 'QueryParser';
1376     my $force_qp_node = shift || 0;
1377     my $qpconfig = $QueryParser::parser_config{$qp_class};
1378
1379     my $fs = $qpconfig->{operators}{float_start};
1380     my $fe = $qpconfig->{operators}{float_end};
1381     my $gs = $qpconfig->{operators}{group_start};
1382     my $ge = $qpconfig->{operators}{group_end};
1383     my $and = $qpconfig->{operators}{and};
1384     my $or = $qpconfig->{operators}{or};
1385
1386     my $isnode = 0;
1387     my $size = 0;
1388     my $q = "";
1389
1390     if (exists $abstract_query->{type}) {
1391         if ($abstract_query->{type} eq 'query_plan') {
1392             $q .= join(" ", map { _abstract_query2str_filter($_, $qp_class) } @{$abstract_query->{filters}}) if
1393                 exists $abstract_query->{filters};
1394
1395             $q .= ($q ? ' ' : '') . join(" ", map { _abstract_query2str_modifier($_, $qp_class) } @{$abstract_query->{modifiers}}) if
1396                 exists $abstract_query->{modifiers};
1397
1398             $size = _kid_list($abstract_query->{children});
1399             $isnode = 1 if ($size > 1 and ($force_qp_node or $depth));
1400             #warn "size: $size, depth: $depth, isnode: $isnode, AQ: ".Dumper($abstract_query);
1401         } elsif ($abstract_query->{type} eq 'node') {
1402             if ($abstract_query->{alias}) {
1403                 $q .= ($q ? ' ' : '') . $abstract_query->{alias};
1404                 $q .= "|$_" foreach @{$abstract_query->{alias_fields}};
1405             } else {
1406                 $q .= ($q ? ' ' : '') . $abstract_query->{class};
1407                 $q .= "|$_" foreach @{$abstract_query->{fields}};
1408             }
1409             $q .= ":";
1410             $isnode = 1;
1411         } elsif ($abstract_query->{type} eq 'atom') {
1412             my $prefix = $abstract_query->{prefix} || '';
1413             $prefix = $qpconfig->{operators}{disallowed} if $prefix eq '!';
1414             $q .= ($q ? ' ' : '') . $prefix .
1415                 ($abstract_query->{content} || '') .
1416                 ($abstract_query->{suffix} || '');
1417         } elsif ($abstract_query->{type} eq 'facet') {
1418             # facet syntax [ # ] is hardcoded I guess?
1419             my $prefix = $abstract_query->{negate} ? $qpconfig->{operators}{disallowed} : '';
1420             $q .= ($q ? ' ' : '') . $prefix . $abstract_query->{name} . "[" .
1421                 join(" # ", @{$abstract_query->{values}}) . "]";
1422         }
1423     }
1424
1425     my $next_depth = int($size > 1);
1426
1427     if (exists $abstract_query->{children}) {
1428
1429         my $op = (keys(%{$abstract_query->{children}}))[0];
1430
1431         if ($abstract_query->{floating}) { # always the top node!
1432             my $sub_node = pop @{$abstract_query->{children}{$op}};
1433
1434             $abstract_query->{floating} = 0;
1435             $q = $fs . " " . abstract_query2str_impl($abstract_query,0,$qp_class, 1) . $fe. " ";
1436
1437             $abstract_query = $sub_node;
1438         }
1439
1440         if ($abstract_query && exists $abstract_query->{children}) {
1441             $op = (keys(%{$abstract_query->{children}}))[0];
1442             $q .= ($q ? ' ' : '') . join(
1443                 ($op eq '&' ? ' ' : " $or "),
1444                 map {
1445                     my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1446                 } @{$abstract_query->{children}{$op}}
1447             );
1448         }
1449     } elsif ($abstract_query->{'&'} or $abstract_query->{'|'}) {
1450         my $op = (keys(%{$abstract_query}))[0];
1451         $q .= ($q ? ' ' : '') . join(
1452             ($op eq '&' ? ' ' : " $or "),
1453             map {
1454                     my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1455             } @{$abstract_query->{$op}}
1456         );
1457     }
1458
1459     $q = "$gs$q$ge" if ($isnode);
1460
1461     return $q;
1462 }
1463
1464 #-------------------------------
1465 package QueryParser::query_plan;
1466
1467 sub QueryParser {
1468     my $self = shift;
1469     return undef unless ref($self);
1470     return $self->{QueryParser};
1471 }
1472
1473 sub new {
1474     my $pkg = shift;
1475     $pkg = ref($pkg) || $pkg;
1476     my %args = (query => [], joiner => '&', @_);
1477
1478     return bless \%args => $pkg;
1479 }
1480
1481 sub new_node {
1482     my $self = shift;
1483     my $pkg = ref($self) || $self;
1484     my $node = do{$pkg.'::node'}->new( plan => $self, @_ );
1485     $self->add_node( $node );
1486     return $node;
1487 }
1488
1489 sub new_facet {
1490     my $self = shift;
1491     my $pkg = ref($self) || $self;
1492     my $name = shift;
1493     my $args = shift;
1494     my $negate = shift;
1495
1496     my $node = do{$pkg.'::facet'}->new( plan => $self, name => $name, 'values' => $args, negate => $negate );
1497     $self->add_node( $node );
1498
1499     return $node;
1500 }
1501
1502 sub new_filter {
1503     my $self = shift;
1504     my $pkg = ref($self) || $self;
1505     my $name = shift;
1506     my $args = shift;
1507     my $negate = shift;
1508
1509     my $node = do{$pkg.'::filter'}->new( plan => $self, name => $name, args => $args, negate => $negate );
1510     $self->add_filter( $node );
1511
1512     return $node;
1513 }
1514
1515
1516 sub _merge_filters {
1517     my $left_filter = shift;
1518     my $right_filter = shift;
1519     my $join = shift;
1520
1521     return undef unless $left_filter or $right_filter;
1522     return $right_filter unless $left_filter;
1523     return $left_filter unless $right_filter;
1524
1525     my $args = $left_filter->{args} || [];
1526
1527     if ($join eq '|') {
1528         push(@$args, @{$right_filter->{args}});
1529
1530     } else {
1531         # find the intersect values
1532         my %new_vals;
1533         map { $new_vals{$_} = 1 } @{$right_filter->{args} || []};
1534         $args = [ grep { $new_vals{$_} } @$args ];
1535     }
1536
1537     $left_filter->{args} = $args;
1538     return $left_filter;
1539 }
1540
1541 sub collapse_filters {
1542     my $self = shift;
1543     my $name = shift;
1544
1545     # start by merging any filters at this level.
1546     # like-level filters are always ORed together
1547
1548     my $cur_filter;
1549     my @cur_filters = grep {$_->name eq $name } @{ $self->filters };
1550     if (@cur_filters) {
1551         $cur_filter = shift @cur_filters;
1552         my $args = $cur_filter->{args} || [];
1553         $cur_filter = _merge_filters($cur_filter, $_, '|') for @cur_filters;
1554     }
1555
1556     # next gather the collapsed filters from sub-plans and 
1557     # merge them with our own
1558
1559     my @subquery = @{$self->{query}};
1560
1561     while (@subquery) {
1562         my $blob = shift @subquery;
1563         shift @subquery; # joiner
1564         next unless $blob->isa('QueryParser::query_plan');
1565         my $sub_filter = $blob->collapse_filters($name);
1566         $cur_filter = _merge_filters($cur_filter, $sub_filter, $self->joiner);
1567     }
1568
1569     if ($self->QueryParser->debug) {
1570         my @args = ($cur_filter and $cur_filter->{args}) ? @{$cur_filter->{args}} : ();
1571         warn "collapse_filters($name) => [@args]\n";
1572     }
1573
1574     return $cur_filter;
1575 }
1576
1577 sub find_filter {
1578     my $self = shift;
1579     my $needle = shift;;
1580     return undef unless ($needle);
1581
1582     my $filter = $self->collapse_filters($needle);
1583
1584     warn "find_filter($needle) => " . 
1585         (($filter and $filter->{args}) ? "@{$filter->{args}}" : '[]') . "\n" 
1586         if $self->QueryParser->debug;
1587
1588     return $filter ? ($filter) : ();
1589 }
1590
1591 sub find_modifier {
1592     my $self = shift;
1593     my $needle = shift;;
1594     return undef unless ($needle);
1595     return grep { $_->name eq $needle } @{ $self->modifiers };
1596 }
1597
1598 sub new_modifier {
1599     my $self = shift;
1600     my $pkg = ref($self) || $self;
1601     my $name = shift;
1602
1603     my $node = do{$pkg.'::modifier'}->new( $name );
1604     $self->add_modifier( $node );
1605
1606     return $node;
1607 }
1608
1609 sub classed_node {
1610     my $self = shift;
1611     my $requested_class = shift;
1612
1613     my $node;
1614     for my $n (@{$self->{query}}) {
1615         next unless (ref($n) && $n->isa( 'QueryParser::query_plan::node' ));
1616         if ($n->requested_class eq $requested_class) {
1617             $node = $n;
1618             last;
1619         }
1620     }
1621
1622     if (!$node) {
1623         $node = $self->new_node;
1624         $node->requested_class( $requested_class );
1625     }
1626
1627     return $node;
1628 }
1629
1630 sub remove_last_node {
1631     my $self = shift;
1632     my $requested_class = shift;
1633
1634     my $old = pop(@{$self->query_nodes});
1635     pop(@{$self->query_nodes}) if (@{$self->query_nodes});
1636
1637     return $old;
1638 }
1639
1640 sub query_nodes {
1641     my $self = shift;
1642     return $self->{query};
1643 }
1644
1645 sub floating {
1646     my $self = shift;
1647     my $f = shift;
1648     $self->{floating} = $f if (defined $f);
1649     return $self->{floating};
1650 }
1651
1652 sub add_node {
1653     my $self = shift;
1654     my $node = shift;
1655
1656     $self->{query} ||= [];
1657     push(@{$self->{query}}, $self->joiner) if (@{$self->{query}});
1658     push(@{$self->{query}}, $node);
1659
1660     return $self;
1661 }
1662
1663 sub top_plan {
1664     my $self = shift;
1665
1666     return $self->{level} ? 0 : 1;
1667 }
1668
1669 sub plan_level {
1670     my $self = shift;
1671     my $level = shift;
1672
1673     if (defined $level) {
1674         $self->{level} = $level;
1675         for (@{$self->query_nodes}) {
1676             $_->plan_level($level + 1) if (ref and $_->isa('QueryParser::query_plan'));
1677         }
1678     }
1679             
1680     return $self->{level};
1681 }
1682
1683 sub joiner {
1684     my $self = shift;
1685     my $joiner = shift;
1686
1687     $self->{joiner} = $joiner if ($joiner);
1688     return $self->{joiner};
1689 }
1690
1691 sub modifiers {
1692     my $self = shift;
1693     $self->{modifiers} ||= [];
1694     return $self->{modifiers};
1695 }
1696
1697 sub add_modifier {
1698     my $self = shift;
1699     my $modifier = shift;
1700
1701     $self->{modifiers} ||= [];
1702     $self->{modifiers} = [ grep {$_->name ne $modifier->name} @{$self->{modifiers}} ];
1703
1704     push(@{$self->{modifiers}}, $modifier);
1705
1706     return $self;
1707 }
1708
1709 sub facets {
1710     my $self = shift;
1711     $self->{facets} ||= [];
1712     return $self->{facets};
1713 }
1714
1715 sub add_facet {
1716     my $self = shift;
1717     my $facet = shift;
1718
1719     $self->{facets} ||= [];
1720     $self->{facets} = [ grep {$_->name ne $facet->name} @{$self->{facets}} ];
1721
1722     push(@{$self->{facets}}, $facet);
1723
1724     return $self;
1725 }
1726
1727 sub filters {
1728     my $self = shift;
1729     $self->{filters} ||= [];
1730     return $self->{filters};
1731 }
1732
1733 sub add_filter {
1734     my $self = shift;
1735     my $filter = shift;
1736
1737     $self->{filters} ||= [];
1738
1739     push(@{$self->{filters}}, $filter);
1740
1741     return $self;
1742 }
1743
1744 # %opts supports two options at this time:
1745 #   no_phrases :
1746 #       If true, do not do anything to the phrases and unphrases
1747 #       fields on any discovered nodes.
1748 #   with_config :
1749 #       If true, also return the query parser config as part of the blob.
1750 #       This will get set back to 0 before recursion to avoid repetition.
1751 sub to_abstract_query {
1752     my $self = shift;
1753     my %opts = @_;
1754
1755     my $pkg = ref $self->QueryParser || $self->QueryParser;
1756
1757     my $abstract_query = {
1758         type => "query_plan",
1759         floating => $self->floating,
1760         level => $self->plan_level,
1761         filters => [map { $_->to_abstract_query } @{$self->filters}],
1762         modifiers => [map { $_->to_abstract_query } @{$self->modifiers}]
1763     };
1764
1765     if ($opts{with_config}) {
1766         $opts{with_config} = 0;
1767         $abstract_query->{config} = $QueryParser::parser_config{$pkg};
1768     }
1769
1770     my $kids = [];
1771
1772     for my $qnode (@{$self->query_nodes}) {
1773         # Remember: qnode can be a joiner string, a node, or another query_plan
1774
1775         if (QueryParser::_util::is_joiner($qnode)) {
1776             if ($abstract_query->{children}) {
1777                 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
1778                 next if $open_joiner eq $qnode;
1779
1780                 my $oldroot = $abstract_query->{children};
1781                 $kids = [$oldroot];
1782                 $abstract_query->{children} = {$qnode => $kids};
1783             } else {
1784                 $abstract_query->{children} = {$qnode => $kids};
1785             }
1786         } else {
1787             push @$kids, $qnode->to_abstract_query(%opts);
1788         }
1789     }
1790
1791     $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
1792     return $abstract_query;
1793 }
1794
1795
1796 #-------------------------------
1797 package QueryParser::query_plan::node;
1798 use Data::Dumper;
1799 $Data::Dumper::Indent = 0;
1800
1801 sub new {
1802     my $pkg = shift;
1803     $pkg = ref($pkg) || $pkg;
1804     my %args = @_;
1805
1806     return bless \%args => $pkg;
1807 }
1808
1809 sub new_atom {
1810     my $self = shift;
1811     my $pkg = ref($self) || $self;
1812     return do{$pkg.'::atom'}->new( @_ );
1813 }
1814
1815 sub requested_class { # also split into classname, fields and alias
1816     my $self = shift;
1817     my $class = shift;
1818
1819     if ($class) {
1820         my @afields;
1821         my (undef, $alias) = split '#', $class;
1822         if ($alias) {
1823             $class =~ s/#[^|]+//;
1824             ($alias, @afields) = split '\|', $alias;
1825         }
1826
1827         my @fields = @afields;
1828         my ($class_part, @field_parts) = split '\|', $class;
1829         for my $f (@field_parts) {
1830              push(@fields, $f) unless (grep { $f eq $_ } @fields);
1831         }
1832
1833         $class_part ||= $class;
1834
1835         $self->{requested_class} = $class;
1836         $self->{alias} = $alias if $alias;
1837         $self->{alias_fields} = \@afields if $alias;
1838         $self->{classname} = $class_part;
1839         $self->{fields} = \@fields;
1840     }
1841
1842     return $self->{requested_class};
1843 }
1844
1845 sub plan {
1846     my $self = shift;
1847     my $plan = shift;
1848
1849     $self->{plan} = $plan if ($plan);
1850     return $self->{plan};
1851 }
1852
1853 sub alias {
1854     my $self = shift;
1855     my $alias = shift;
1856
1857     $self->{alias} = $alias if ($alias);
1858     return $self->{alias};
1859 }
1860
1861 sub alias_fields {
1862     my $self = shift;
1863     my $alias = shift;
1864
1865     $self->{alias_fields} = $alias if ($alias);
1866     return $self->{alias_fields};
1867 }
1868
1869 sub classname {
1870     my $self = shift;
1871     my $class = shift;
1872
1873     $self->{classname} = $class if ($class);
1874     return $self->{classname};
1875 }
1876
1877 sub fields {
1878     my $self = shift;
1879     my @fields = @_;
1880
1881     $self->{fields} ||= [];
1882     $self->{fields} = \@fields if (@fields);
1883     return $self->{fields};
1884 }
1885
1886 sub phrases {
1887     my $self = shift;
1888     my @phrases = @_;
1889
1890     $self->{phrases} ||= [];
1891     $self->{phrases} = \@phrases if (@phrases);
1892     return $self->{phrases};
1893 }
1894
1895 sub unphrases {
1896     my $self = shift;
1897     my @phrases = @_;
1898
1899     $self->{unphrases} ||= [];
1900     $self->{unphrases} = \@phrases if (@phrases);
1901     return $self->{unphrases};
1902 }
1903
1904 sub add_phrase {
1905     my $self = shift;
1906     my $phrase = shift;
1907
1908     push(@{$self->phrases}, $phrase);
1909
1910     return $self;
1911 }
1912
1913 sub add_unphrase {
1914     my $self = shift;
1915     my $phrase = shift;
1916
1917     push(@{$self->unphrases}, $phrase);
1918
1919     return $self;
1920 }
1921
1922 sub query_atoms {
1923     my $self = shift;
1924     my @query_atoms = @_;
1925
1926     $self->{query_atoms} ||= [];
1927     $self->{query_atoms} = \@query_atoms if (@query_atoms);
1928     return $self->{query_atoms};
1929 }
1930
1931 sub add_fts_atom {
1932     my $self = shift;
1933     my $atom = shift;
1934
1935     if (!ref($atom)) {
1936         my $content = $atom;
1937         my @parts = @_;
1938
1939         $atom = $self->new_atom( content => $content, @parts );
1940     }
1941
1942     push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1943     push(@{$self->query_atoms}, $atom);
1944
1945     return $self;
1946 }
1947
1948 sub add_dummy_atom {
1949     my $self = shift;
1950     my @parts = @_;
1951
1952     my $atom = $self->new_atom( @parts, dummy => 1 );
1953
1954     push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1955     push(@{$self->query_atoms}, $atom);
1956
1957     return $self;
1958 }
1959
1960 # This will find up to one occurence of @$short_list within @$long_list, and
1961 # replace it with the single atom $replacement.
1962 sub replace_phrase_in_abstract_query {
1963     my ($self, $short_list, $long_list, $replacement) = @_;
1964
1965     my $success = 0;
1966     my @already = ();
1967     my $goal = scalar @$short_list;
1968
1969     for (my $i = 0; $i < scalar (@$long_list); $i++) {
1970         my $right = $long_list->[$i];
1971
1972         if (QueryParser::_util::compare_abstract_atoms(
1973             $short_list->[scalar @already], $right
1974         )) {
1975             push @already, $i;
1976         } elsif (scalar @already) {
1977             @already = ();
1978             next;
1979         }
1980
1981         if (scalar @already == $goal) {
1982             splice @$long_list, $already[0], scalar(@already), $replacement;
1983             $success = 1;
1984             last;
1985         }
1986     }
1987
1988     return $success;
1989 }
1990
1991 sub to_abstract_query {
1992     my $self = shift;
1993     my %opts = @_;
1994
1995     my $pkg = ref $self->plan->QueryParser || $self->plan->QueryParser;
1996
1997     my $abstract_query = {
1998         "type" => "node",
1999         "alias" => $self->alias,
2000         "alias_fields" => $self->alias_fields,
2001         "class" => $self->classname,
2002         "fields" => $self->fields
2003     };
2004
2005     my $kids = [];
2006
2007     for my $qatom (@{$self->query_atoms}) {
2008         if (QueryParser::_util::is_joiner($qatom)) {
2009             if ($abstract_query->{children}) {
2010                 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2011                 next if $open_joiner eq $qatom;
2012
2013                 my $oldroot = $abstract_query->{children};
2014                 $kids = [$oldroot];
2015                 $abstract_query->{children} = {$qatom => $kids};
2016             } else {
2017                 $abstract_query->{children} = {$qatom => $kids};
2018             }
2019         } else {
2020             push @$kids, $qatom->to_abstract_query;
2021         }
2022     }
2023
2024     $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2025
2026     if ($self->{phrases} and not $opts{no_phrases}) {
2027         for my $phrase (@{$self->{phrases}}) {
2028             # Phrases appear duplication in a real QP tree, and we don't want
2029             # that duplication in our abstract query.  So for all our phrases,
2030             # break them into atoms as QP would, and remove any matching
2031             # sequences of atoms from our abstract query.
2032
2033             my $tmptree = $self->{plan}->{QueryParser}->new(query => '"'.$phrase.'"')->parse->parse_tree;
2034             if ($tmptree) {
2035                 # For a well-behaved phrase, we should now have only one node
2036                 # in the $tmptree query plan, and that node should have an
2037                 # orderly list of atoms and joiners.
2038
2039                 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
2040                     my $tmplist;
2041
2042                     eval {
2043                         $tmplist = $tmptree->{query}->[0]->to_abstract_query(
2044                             no_phrases => 1
2045                         )->{children}->{'&'}->[0]->{children}->{'&'};
2046                     };
2047                     next if $@;
2048
2049                     foreach (
2050                         QueryParser::_util::find_arrays_in_abstract($abstract_query->{children})
2051                     ) {
2052                         last if $self->replace_phrase_in_abstract_query(
2053                             $tmplist,
2054                             $_,
2055                             QueryParser::_util::fake_abstract_atom_from_phrase($phrase, undef, $pkg)
2056                         );
2057                     }
2058                 }
2059             }
2060         }
2061     }
2062
2063     # Do the same as the preceding block for unphrases (negated phrases).
2064     if ($self->{unphrases} and not $opts{no_phrases}) {
2065         for my $phrase (@{$self->{unphrases}}) {
2066             my $tmptree = $self->{plan}->{QueryParser}->new(
2067                 query => $QueryParser::parser_config{$pkg}{operators}{disallowed}.
2068                     '"' . $phrase . '"'
2069             )->parse->parse_tree;
2070
2071             if ($tmptree) {
2072                 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
2073                     my $tmplist;
2074
2075                     eval {
2076                         $tmplist = $tmptree->{query}->[0]->to_abstract_query(
2077                             no_phrases => 1
2078                         )->{children}->{'&'}->[0]->{children}->{'&'};
2079                     };
2080                     next if $@;
2081
2082                     foreach (
2083                         QueryParser::_util::find_arrays_in_abstract($abstract_query->{children})
2084                     ) {
2085                         last if $self->replace_phrase_in_abstract_query(
2086                             $tmplist,
2087                             $_,
2088                             QueryParser::_util::fake_abstract_atom_from_phrase($phrase, 1, $pkg)
2089                         );
2090                     }
2091                 }
2092             }
2093         }
2094     }
2095
2096     $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2097     return $abstract_query;
2098 }
2099
2100 #-------------------------------
2101 package QueryParser::query_plan::node::atom;
2102
2103 sub new {
2104     my $pkg = shift;
2105     $pkg = ref($pkg) || $pkg;
2106     my %args = @_;
2107
2108     return bless \%args => $pkg;
2109 }
2110
2111 sub node {
2112     my $self = shift;
2113     return undef unless (ref $self);
2114     return $self->{node};
2115 }
2116
2117 sub content {
2118     my $self = shift;
2119     return undef unless (ref $self);
2120     return $self->{content};
2121 }
2122
2123 sub prefix {
2124     my $self = shift;
2125     return undef unless (ref $self);
2126     return $self->{prefix};
2127 }
2128
2129 sub suffix {
2130     my $self = shift;
2131     return undef unless (ref $self);
2132     return $self->{suffix};
2133 }
2134
2135 sub to_abstract_query {
2136     my ($self) = @_;
2137     
2138     return {
2139         (map { $_ => $self->$_ } qw/prefix suffix content/),
2140         "type" => "atom"
2141     };
2142 }
2143 #-------------------------------
2144 package QueryParser::query_plan::filter;
2145
2146 sub new {
2147     my $pkg = shift;
2148     $pkg = ref($pkg) || $pkg;
2149     my %args = @_;
2150
2151     return bless \%args => $pkg;
2152 }
2153
2154 sub plan {
2155     my $self = shift;
2156     return $self->{plan};
2157 }
2158
2159 sub name {
2160     my $self = shift;
2161     return $self->{name};
2162 }
2163
2164 sub negate {
2165     my $self = shift;
2166     return $self->{negate};
2167 }
2168
2169 sub args {
2170     my $self = shift;
2171     return $self->{args};
2172 }
2173
2174 sub to_abstract_query {
2175     my ($self) = @_;
2176     
2177     return {
2178         map { $_ => $self->$_ } qw/name negate args/
2179     };
2180 }
2181
2182 #-------------------------------
2183 package QueryParser::query_plan::facet;
2184
2185 sub new {
2186     my $pkg = shift;
2187     $pkg = ref($pkg) || $pkg;
2188     my %args = @_;
2189
2190     return bless \%args => $pkg;
2191 }
2192
2193 sub plan {
2194     my $self = shift;
2195     return $self->{plan};
2196 }
2197
2198 sub name {
2199     my $self = shift;
2200     return $self->{name};
2201 }
2202
2203 sub negate {
2204     my $self = shift;
2205     return $self->{negate};
2206 }
2207
2208 sub values {
2209     my $self = shift;
2210     return $self->{'values'};
2211 }
2212
2213 sub to_abstract_query {
2214     my ($self) = @_;
2215
2216     return {
2217         (map { $_ => $self->$_ } qw/name negate values/),
2218         "type" => "facet"
2219     };
2220 }
2221
2222 #-------------------------------
2223 package QueryParser::query_plan::modifier;
2224
2225 sub new {
2226     my $pkg = shift;
2227     $pkg = ref($pkg) || $pkg;
2228     my $modifier = shift;
2229     my $negate = shift;
2230
2231     return bless { name => $modifier, negate => $negate } => $pkg;
2232 }
2233
2234 sub name {
2235     my $self = shift;
2236     return $self->{name};
2237 }
2238
2239 sub negate {
2240     my $self = shift;
2241     return $self->{negate};
2242 }
2243
2244 sub to_abstract_query {
2245     my ($self) = @_;
2246     
2247     return $self->name;
2248 }
2249 1;
2250