]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm
QueryParser: Treat Unphrases as negated phrases
[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_op = $pkg->operator('required');
887     my $required_re = qr/\Q$required_op\E/;
888
889     my $disallowed_op = $pkg->operator('disallowed');
890     my $disallowed_re = qr/\Q$disallowed_op\E/;
891
892     my $and_op = $pkg->operator('and');
893     my $and_re = qr/^\s*\Q$and_op\E/;
894
895     my $or_op = $pkg->operator('or');
896     my $or_re = qr/^\s*\Q$or_op\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 = $pkg->operator('modifier');
911     my $modifier_tag_re = qr/^\s*\Q$modifier_tag\E/;
912
913     # Group start/end normally are ( and ), but can be overridden.
914     # We thus include ( and ) specifically due to filters, as well as : for classes.
915     my $phrase_cleanup_re = qr/\s*(\Q$required_op\E|\Q$disallowed_op\E|\Q$and_op\E|\Q$or_op\E|\Q$group_start\E|\Q$group_end\E|\Q$float_start\E|\Q$float_end\E|\Q$modifier_tag\E|:|\(|\))/;
916
917     # Build the filter and modifier uber-regexps
918     my $facet_re = '^\s*(-?)((?:' . join( '|', @{$pkg->facet_classes}) . ')(?:\|\w+)*)\[(.+?)\]';
919     warn '  'x$recursing." ** Facet RE: $facet_re\n" if $self->debug;
920
921     my $filter_re = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . ')\(([^()]+)\)';
922     my $filter_as_class_re = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . '):\s*(\S+)';
923
924     my $modifier_re = '^\s*'.$modifier_tag_re.'(' . join( '|', @{$pkg->modifiers}) . ')\b';
925     my $modifier_as_class_re = '^\s*(' . join( '|', @{$pkg->modifiers}) . '):\s*(\S+)';
926
927     my $struct = shift || $self->new_plan( level => $recursing );
928     $self->parse_tree( $struct ) if (!$self->parse_tree);
929
930     my $remainder = '';
931
932     while (!$remainder) {
933         warn '  'x$recursing."Start of the loop. last_type: $last_type, joiner: ".$struct->joiner.", struct: $struct\n" if $self->debug;
934         if ($last_type eq 'FEND' and $fstart and $fstart !=  $struct) { # fall back further
935             $remainder = $_;
936             last;
937         } elsif ($last_type eq 'FEND') {
938             $fstart = undef;
939             $last_type = '';
940         }
941
942         if (/^\s*$/) { # end of an explicit group
943             local $last_type = '';
944             last;
945         } elsif (/$float_end_re/) { # end of an explicit group
946             warn '  'x$recursing."Encountered explicit float end, remainder: $'\n" if $self->debug;
947
948             $remainder = $';
949             $_ = '';
950
951             $floating = 0;
952             $last_type = 'FEND';
953             last;
954         } elsif (/$group_end_re/) { # end of an explicit group
955             warn '  'x$recursing."Encountered explicit group end, remainder: $'\n" if $self->debug;
956
957             $remainder = $';
958             $_ = '';
959
960             local $last_type = '';
961         } elsif ($self->filter_count && /$filter_re/) { # found a filter
962             warn '  'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
963
964             my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
965             $_ = $';
966
967             my $filter = $2;
968             my $params = [ split '[,]+', $3 ];
969
970             if ($pkg->filter_callbacks->{$filter}) {
971                 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
972                 $_ = "$replacement $_" if ($replacement);
973             } else {
974                 $struct->new_filter( $filter => $params, $negate );
975             }
976
977
978             local $last_type = '';
979         } elsif ($self->filter_count && /$filter_as_class_re/) { # found a filter
980             warn '  'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
981
982             my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
983             $_ = $';
984
985             my $filter = $2;
986             my $params = [ split '[,]+', $3 ];
987
988             if ($pkg->filter_callbacks->{$filter}) {
989                 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
990                 $_ = "$replacement $_" if ($replacement);
991             } else {
992                 $struct->new_filter( $filter => $params, $negate );
993             }
994
995             local $last_type = '';
996         } elsif ($self->modifier_count && /$modifier_re/) { # found a modifier
997             warn '  'x$recursing."Encountered search modifier: $1\n" if $self->debug;
998
999             $_ = $';
1000             if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1001                 warn '  'x$recursing."  Search modifiers only allowed at the top level of the query\n" if $self->debug;
1002             } else {
1003                 $struct->new_modifier($1);
1004             }
1005
1006             local $last_type = '';
1007         } elsif ($self->modifier_count && /$modifier_as_class_re/) { # found a modifier
1008             warn '  'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1009
1010             my $mod = $1;
1011
1012             $_ = $';
1013             if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1014                 warn '  'x$recursing."  Search modifiers only allowed at the top level of the query\n" if $self->debug;
1015             } elsif ($2 =~ /^[ty1]/i) {
1016                 $struct->new_modifier($mod);
1017             }
1018
1019             local $last_type = '';
1020         } elsif (/$float_start_re/) { # start of an explicit float
1021             warn '  'x$recursing."Encountered explicit float start\n" if $self->debug;
1022             $floating = 1;
1023             $fstart = $struct;
1024
1025             $last_class = $current_class;
1026             $current_class = undef;
1027
1028             $self->floating_plan( $self->new_plan( floating => 1 ) ) if (!$self->floating_plan);
1029
1030             # pass the floating_plan struct to be modified by the float'ed chunk
1031             my ($floating_plan, $subremainder) = $self->new( debug => $self->debug )->decompose( $', undef, undef, undef,  $self->floating_plan);
1032             $_ = $subremainder;
1033             warn '  'x$recursing."Remainder after explicit float: $_\n" if $self->debug;
1034
1035             $current_class = $last_class;
1036
1037             $last_type = '';
1038         } elsif (/$group_start_re/) { # start of an explicit group
1039             warn '  'x$recursing."Encountered explicit group start\n" if $self->debug;
1040
1041             my ($substruct, $subremainder) = $self->decompose( $', $current_class, $recursing + 1 );
1042             $struct->add_node( $substruct ) if ($substruct);
1043             $_ = $subremainder;
1044             warn '  'x$recursing."Query remainder after bool group: $_\n" if $self->debug;
1045
1046             local $last_type = '';
1047
1048         } elsif (/$and_re/) { # ANDed expression
1049             $_ = $';
1050             warn '  'x$recursing."Encountered AND\n" if $self->debug;
1051             do {warn '  'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1052             do {warn '  'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1053             local $last_type = 'AND';
1054
1055             warn '  'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1056             my $LHS = $struct;
1057             #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1058             my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 1 );
1059             $_ = $subremainder;
1060
1061             warn '  'x$recursing."RHS built\n" if $self->debug;
1062             warn '  'x$recursing."Post-AND remainder: $subremainder\n" if $self->debug;
1063
1064             my $wrapper = $self->new_plan( level => $recursing + 1 );
1065
1066             if ($LHS->floating) {
1067                 $wrapper->{query} = $LHS->{query};
1068                 my $outer_wrapper = $self->new_plan( level => $recursing + 1 );
1069                 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1070                 $LHS->{query} = [$outer_wrapper];
1071                 $struct = $LHS;
1072             } else {
1073                 $wrapper->add_node($_) for ($LHS, $RHS);
1074                 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1075                 $struct = $self->new_plan( level => $recursing );
1076                 $struct->add_node($wrapper);
1077             }
1078
1079             $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1080
1081             local $last_type = '';
1082         } elsif (/$or_re/) { # ORed expression
1083             $_ = $';
1084             warn '  'x$recursing."Encountered OR\n" if $self->debug;
1085             do {warn '  'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1086             do {warn '  'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1087             local $last_type = 'OR';
1088
1089             warn '  'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1090             my $LHS = $struct;
1091             #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1092             my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 2 );
1093             $_ = $subremainder;
1094
1095             warn '  'x$recursing."RHS built\n" if $self->debug;
1096             warn '  'x$recursing."Post-OR remainder: $subremainder\n" if $self->debug;
1097
1098             my $wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1099
1100             if ($LHS->floating) {
1101                 $wrapper->{query} = $LHS->{query};
1102                 my $outer_wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1103                 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1104                 $LHS->{query} = [$outer_wrapper];
1105                 $struct = $LHS;
1106             } else {
1107                 $wrapper->add_node($_) for ($LHS, $RHS);
1108                 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1109                 $struct = $self->new_plan( level => $recursing );
1110                 $struct->add_node($wrapper);
1111             }
1112
1113             $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1114
1115             local $last_type = '';
1116         } elsif ($self->facet_class_count && /$facet_re/) { # changing current class
1117             warn '  'x$recursing."Encountered facet: $1$2 => $3\n" if $self->debug;
1118
1119             my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1120             my $facet = $2;
1121             my $facet_value = [ split '\s*#\s*', $3 ];
1122             $struct->new_facet( $facet => $facet_value, $negate );
1123             $_ = $';
1124
1125             local $last_type = '';
1126         } elsif ($self->search_class_count && /$search_class_re/) { # changing current class
1127
1128             if ($last_type eq 'CLASS') {
1129                 $struct->remove_last_node( $current_class );
1130                 warn '  'x$recursing."Encountered class change with no searches!\n" if $self->debug;
1131             }
1132
1133             warn '  'x$recursing."Encountered class change: $1\n" if $self->debug;
1134
1135             $current_class = $struct->classed_node( $1 )->requested_class();
1136             $_ = $';
1137
1138             local $last_type = 'CLASS';
1139         } elsif (/^\s*($required_re|$disallowed_re)?"([^"]+)"/) { # phrase, always anded
1140             warn '  'x$recursing.'Encountered' . ($1 ? " ['$1' modified]" : '') . " phrase: $2\n" if $self->debug;
1141
1142             my $req_ness = $1 || '';
1143             my $phrase = $2;
1144
1145             if (!$phrase_helper) {
1146                 warn '  'x$recursing."Recursing into decompose with the phrase as a subquery\n" if $self->debug;
1147                 my $after = $';
1148                 my ($substruct, $subremainder) = $self->decompose( qq/$req_ness"$phrase"/, $current_class, $recursing + 1, 1 );
1149                 $struct->add_node( $substruct ) if ($substruct);
1150                 $_ = $after;
1151             } else {
1152                 warn '  'x$recursing."Directly parsing the phrase subquery\n" if $self->debug;
1153                 $struct->joiner( '&' );
1154
1155                 my $class_node = $struct->classed_node($current_class);
1156
1157                 if ($req_ness eq $disallowed_op) {
1158                     $class_node->negate(1);
1159                 }
1160                 $class_node->add_phrase( $phrase );
1161
1162                 # Cleanup the phrase to make it so that we don't parse things in it as anything other than atoms
1163                 $phrase =~ s/$phrase_cleanup_re/ /g;
1164
1165                 $_ = $phrase . $';
1166
1167             }
1168
1169             local $last_type = '';
1170
1171         } elsif (/^\s*($required_re|$disallowed_re)([^${group_end}${float_end}\s"]+)/) { # convert require/disallow word to {un}phrase
1172             warn '  'x$recursing."Encountered required atom (mini phrase), transforming for phrase parse: $1\n" if $self->debug;
1173
1174             $_ = $1 . '"' . $2 . '"' . $';
1175
1176             local $last_type = '';
1177         } elsif (/^\s*([^${group_end}${float_end}\s]+)/o) { # atom
1178             warn '  'x$recursing."Encountered atom: $1\n" if $self->debug;
1179             warn '  'x$recursing."Remainder: $'\n" if $self->debug;
1180
1181             my $atom = $1;
1182             my $after = $';
1183
1184             $_ = $after;
1185             local $last_type = '';
1186
1187             my $class_node = $struct->classed_node($current_class);
1188
1189             my $prefix = ($atom =~ s/^$disallowed_re//o) ? '!' : '';
1190             my $truncate = ($atom =~ s/\*$//o) ? '*' : '';
1191
1192             if ($atom ne '' and !grep { $atom =~ /^\Q$_\E+$/ } ('&','|')) { # throw away & and |, not allowed in tsquery, and not really useful anyway
1193 #                $class_node->add_phrase( $atom ) if ($atom =~ s/^$required_re//o);
1194
1195                 $class_node->add_fts_atom( $atom, suffix => $truncate, prefix => $prefix, node => $class_node );
1196                 $struct->joiner( '&' );
1197             }
1198
1199             local $last_type = '';
1200         } 
1201
1202         last unless ($_);
1203
1204     }
1205
1206     $struct = undef if 
1207         scalar(@{$struct->query_nodes}) == 0 &&
1208         scalar(@{$struct->filters}) == 0 &&
1209         !$struct->top_plan;
1210
1211     return $struct if !wantarray;
1212     return ($struct, $remainder);
1213 }
1214
1215 =head2 find_class_index
1216
1217     $index = $QParser->find_class_index($class, $query);
1218 =cut
1219
1220 sub find_class_index {
1221     my $class = shift;
1222     my $query = shift;
1223
1224     my ($class_part, @field_parts) = split '\|', $class;
1225     $class_part ||= $class;
1226
1227     for my $idx ( 0 .. scalar(@$query) - 1 ) {
1228         next unless ref($$query[$idx]);
1229         return $idx if ( $$query[$idx]{requested_class} && $class eq $$query[$idx]{requested_class} );
1230     }
1231
1232     push(@$query, { classname => $class_part, (@field_parts ? (fields => \@field_parts) : ()), requested_class => $class, ftsquery => [], phrases => [] });
1233     return -1;
1234 }
1235
1236 =head2 core_limit
1237
1238     $limit = $QParser->core_limit([$limit]);
1239
1240 Return and/or set the core_limit.
1241 =cut
1242
1243 sub core_limit {
1244     my $self = shift;
1245     my $l = shift;
1246     $self->{core_limit} = $l if ($l);
1247     return $self->{core_limit};
1248 }
1249
1250 =head2 superpage
1251
1252     $superpage = $QParser->superpage([$superpage]);
1253
1254 Return and/or set the superpage.
1255 =cut
1256
1257 sub superpage {
1258     my $self = shift;
1259     my $l = shift;
1260     $self->{superpage} = $l if ($l);
1261     return $self->{superpage};
1262 }
1263
1264 =head2 superpage_size
1265
1266     $size = $QParser->superpage_size([$size]);
1267
1268 Return and/or set the superpage size.
1269 =cut
1270
1271 sub superpage_size {
1272     my $self = shift;
1273     my $l = shift;
1274     $self->{superpage_size} = $l if ($l);
1275     return $self->{superpage_size};
1276 }
1277
1278
1279 #-------------------------------
1280 package QueryParser::_util;
1281
1282 # At this level, joiners are always & or |.  This is not
1283 # the external, configurable representation of joiners that
1284 # defaults to # && and ||.
1285 sub is_joiner {
1286     my $str = shift;
1287
1288     return (not ref $str and ($str eq '&' or $str eq '|'));
1289 }
1290
1291 sub default_joiner { '&' }
1292
1293 # 0 for different, 1 for the same.
1294 sub compare_abstract_atoms {
1295     my ($left, $right) = @_;
1296
1297     foreach (qw/prefix suffix content/) {
1298         no warnings;    # undef can stand in for '' here
1299         return 0 unless $left->{$_} eq $right->{$_};
1300     }
1301
1302     return 1;
1303 }
1304
1305 sub fake_abstract_atom_from_phrase {
1306     my $phrase = shift;
1307     my $neg = shift;
1308     my $qp_class = shift || 'QueryParser';
1309
1310     my $prefix = '"';
1311     if ($neg) {
1312         $prefix =
1313             $QueryParser::parser_config{$qp_class}{operators}{disallowed} .
1314             $prefix;
1315     }
1316
1317     return {
1318         "type" => "atom", "prefix" => $prefix, "suffix" => '"',
1319         "content" => $phrase
1320     }
1321 }
1322
1323 sub find_arrays_in_abstract {
1324     my ($hash) = @_;
1325
1326     my @arrays;
1327     foreach my $key (keys %$hash) {
1328         if (ref $hash->{$key} eq "ARRAY") {
1329             push @arrays, $hash->{$key};
1330             foreach (@{$hash->{$key}}) {
1331                 push @arrays, find_arrays_in_abstract($_);
1332             }
1333         }
1334     }
1335
1336     return @arrays;
1337 }
1338
1339 #-------------------------------
1340 package QueryParser::Canonicalize;  # not OO
1341 use Data::Dumper;
1342
1343 sub _abstract_query2str_filter {
1344     my $f = shift;
1345     my $qp_class = shift || 'QueryParser';
1346     my $qpconfig = $QueryParser::parser_config{$qp_class};
1347
1348     return sprintf(
1349         '%s%s(%s)',
1350         $f->{negate} ? $qpconfig->{operators}{disallowed} : "",
1351         $f->{name},
1352         join(",", @{$f->{args}})
1353     );
1354 }
1355
1356 sub _abstract_query2str_modifier {
1357     my $f = shift;
1358     my $qp_class = shift || 'QueryParser';
1359     my $qpconfig = $QueryParser::parser_config{$qp_class};
1360
1361     return $qpconfig->{operators}{modifier} . $f;
1362 }
1363
1364 sub _kid_list {
1365     my $children = shift;
1366     my $op = (keys %$children)[0];
1367     return @{$$children{$op}};
1368 }
1369
1370
1371 # This should produce an equivalent query to the original, given an
1372 # abstract_query.
1373 sub abstract_query2str_impl {
1374     my $abstract_query  = shift;
1375     my $depth = shift || 0;
1376
1377     my $qp_class ||= shift || 'QueryParser';
1378     my $force_qp_node = shift || 0;
1379     my $qpconfig = $QueryParser::parser_config{$qp_class};
1380
1381     my $fs = $qpconfig->{operators}{float_start};
1382     my $fe = $qpconfig->{operators}{float_end};
1383     my $gs = $qpconfig->{operators}{group_start};
1384     my $ge = $qpconfig->{operators}{group_end};
1385     my $and = $qpconfig->{operators}{and};
1386     my $or = $qpconfig->{operators}{or};
1387
1388     my $isnode = 0;
1389     my $size = 0;
1390     my $q = "";
1391
1392     if (exists $abstract_query->{type}) {
1393         if ($abstract_query->{type} eq 'query_plan') {
1394             $q .= join(" ", map { _abstract_query2str_filter($_, $qp_class) } @{$abstract_query->{filters}}) if
1395                 exists $abstract_query->{filters};
1396
1397             $q .= ($q ? ' ' : '') . join(" ", map { _abstract_query2str_modifier($_, $qp_class) } @{$abstract_query->{modifiers}}) if
1398                 exists $abstract_query->{modifiers};
1399
1400             $size = _kid_list($abstract_query->{children});
1401             $isnode = 1 if ($size > 1 and ($force_qp_node or $depth));
1402             #warn "size: $size, depth: $depth, isnode: $isnode, AQ: ".Dumper($abstract_query);
1403         } elsif ($abstract_query->{type} eq 'node') {
1404             if ($abstract_query->{alias}) {
1405                 $q .= ($q ? ' ' : '') . $abstract_query->{alias};
1406                 $q .= "|$_" foreach @{$abstract_query->{alias_fields}};
1407             } else {
1408                 $q .= ($q ? ' ' : '') . $abstract_query->{class};
1409                 $q .= "|$_" foreach @{$abstract_query->{fields}};
1410             }
1411             $q .= ":";
1412             $isnode = 1;
1413         } elsif ($abstract_query->{type} eq 'atom') {
1414             my $prefix = $abstract_query->{prefix} || '';
1415             $prefix = $qpconfig->{operators}{disallowed} if $prefix eq '!';
1416             $q .= ($q ? ' ' : '') . $prefix .
1417                 ($abstract_query->{content} || '') .
1418                 ($abstract_query->{suffix} || '');
1419         } elsif ($abstract_query->{type} eq 'facet') {
1420             # facet syntax [ # ] is hardcoded I guess?
1421             my $prefix = $abstract_query->{negate} ? $qpconfig->{operators}{disallowed} : '';
1422             $q .= ($q ? ' ' : '') . $prefix . $abstract_query->{name} . "[" .
1423                 join(" # ", @{$abstract_query->{values}}) . "]";
1424         }
1425     }
1426
1427     my $next_depth = int($size > 1);
1428
1429     if (exists $abstract_query->{children}) {
1430
1431         my $op = (keys(%{$abstract_query->{children}}))[0];
1432
1433         if ($abstract_query->{floating}) { # always the top node!
1434             my $sub_node = pop @{$abstract_query->{children}{$op}};
1435
1436             $abstract_query->{floating} = 0;
1437             $q = $fs . " " . abstract_query2str_impl($abstract_query,0,$qp_class, 1) . $fe. " ";
1438
1439             $abstract_query = $sub_node;
1440         }
1441
1442         if ($abstract_query && exists $abstract_query->{children}) {
1443             $op = (keys(%{$abstract_query->{children}}))[0];
1444             $q .= ($q ? ' ' : '') . join(
1445                 ($op eq '&' ? ' ' : " $or "),
1446                 map {
1447                     my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1448                 } @{$abstract_query->{children}{$op}}
1449             );
1450         }
1451     } elsif ($abstract_query->{'&'} or $abstract_query->{'|'}) {
1452         my $op = (keys(%{$abstract_query}))[0];
1453         $q .= ($q ? ' ' : '') . join(
1454             ($op eq '&' ? ' ' : " $or "),
1455             map {
1456                     my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1457             } @{$abstract_query->{$op}}
1458         );
1459     }
1460
1461     $q = "$gs$q$ge" if ($isnode);
1462
1463     return $q;
1464 }
1465
1466 #-------------------------------
1467 package QueryParser::query_plan;
1468
1469 sub QueryParser {
1470     my $self = shift;
1471     return undef unless ref($self);
1472     return $self->{QueryParser};
1473 }
1474
1475 sub new {
1476     my $pkg = shift;
1477     $pkg = ref($pkg) || $pkg;
1478     my %args = (query => [], joiner => '&', @_);
1479
1480     return bless \%args => $pkg;
1481 }
1482
1483 sub new_node {
1484     my $self = shift;
1485     my $pkg = ref($self) || $self;
1486     my $node = do{$pkg.'::node'}->new( plan => $self, @_ );
1487     $self->add_node( $node );
1488     return $node;
1489 }
1490
1491 sub new_facet {
1492     my $self = shift;
1493     my $pkg = ref($self) || $self;
1494     my $name = shift;
1495     my $args = shift;
1496     my $negate = shift;
1497
1498     my $node = do{$pkg.'::facet'}->new( plan => $self, name => $name, 'values' => $args, negate => $negate );
1499     $self->add_node( $node );
1500
1501     return $node;
1502 }
1503
1504 sub new_filter {
1505     my $self = shift;
1506     my $pkg = ref($self) || $self;
1507     my $name = shift;
1508     my $args = shift;
1509     my $negate = shift;
1510
1511     my $node = do{$pkg.'::filter'}->new( plan => $self, name => $name, args => $args, negate => $negate );
1512     $self->add_filter( $node );
1513
1514     return $node;
1515 }
1516
1517
1518 sub _merge_filters {
1519     my $left_filter = shift;
1520     my $right_filter = shift;
1521     my $join = shift;
1522
1523     return undef unless $left_filter or $right_filter;
1524     return $right_filter unless $left_filter;
1525     return $left_filter unless $right_filter;
1526
1527     my $args = $left_filter->{args} || [];
1528
1529     if ($join eq '|') {
1530         push(@$args, @{$right_filter->{args}});
1531
1532     } else {
1533         # find the intersect values
1534         my %new_vals;
1535         map { $new_vals{$_} = 1 } @{$right_filter->{args} || []};
1536         $args = [ grep { $new_vals{$_} } @$args ];
1537     }
1538
1539     $left_filter->{args} = $args;
1540     return $left_filter;
1541 }
1542
1543 sub collapse_filters {
1544     my $self = shift;
1545     my $name = shift;
1546
1547     # start by merging any filters at this level.
1548     # like-level filters are always ORed together
1549
1550     my $cur_filter;
1551     my @cur_filters = grep {$_->name eq $name } @{ $self->filters };
1552     if (@cur_filters) {
1553         $cur_filter = shift @cur_filters;
1554         my $args = $cur_filter->{args} || [];
1555         $cur_filter = _merge_filters($cur_filter, $_, '|') for @cur_filters;
1556     }
1557
1558     # next gather the collapsed filters from sub-plans and 
1559     # merge them with our own
1560
1561     my @subquery = @{$self->{query}};
1562
1563     while (@subquery) {
1564         my $blob = shift @subquery;
1565         shift @subquery; # joiner
1566         next unless $blob->isa('QueryParser::query_plan');
1567         my $sub_filter = $blob->collapse_filters($name);
1568         $cur_filter = _merge_filters($cur_filter, $sub_filter, $self->joiner);
1569     }
1570
1571     if ($self->QueryParser->debug) {
1572         my @args = ($cur_filter and $cur_filter->{args}) ? @{$cur_filter->{args}} : ();
1573         warn "collapse_filters($name) => [@args]\n";
1574     }
1575
1576     return $cur_filter;
1577 }
1578
1579 sub find_filter {
1580     my $self = shift;
1581     my $needle = shift;;
1582     return undef unless ($needle);
1583
1584     my $filter = $self->collapse_filters($needle);
1585
1586     warn "find_filter($needle) => " . 
1587         (($filter and $filter->{args}) ? "@{$filter->{args}}" : '[]') . "\n" 
1588         if $self->QueryParser->debug;
1589
1590     return $filter ? ($filter) : ();
1591 }
1592
1593 sub find_modifier {
1594     my $self = shift;
1595     my $needle = shift;;
1596     return undef unless ($needle);
1597     return grep { $_->name eq $needle } @{ $self->modifiers };
1598 }
1599
1600 sub new_modifier {
1601     my $self = shift;
1602     my $pkg = ref($self) || $self;
1603     my $name = shift;
1604
1605     my $node = do{$pkg.'::modifier'}->new( $name );
1606     $self->add_modifier( $node );
1607
1608     return $node;
1609 }
1610
1611 sub classed_node {
1612     my $self = shift;
1613     my $requested_class = shift;
1614
1615     my $node;
1616     for my $n (@{$self->{query}}) {
1617         next unless (ref($n) && $n->isa( 'QueryParser::query_plan::node' ));
1618         if ($n->requested_class eq $requested_class) {
1619             $node = $n;
1620             last;
1621         }
1622     }
1623
1624     if (!$node) {
1625         $node = $self->new_node;
1626         $node->requested_class( $requested_class );
1627     }
1628
1629     return $node;
1630 }
1631
1632 sub remove_last_node {
1633     my $self = shift;
1634     my $requested_class = shift;
1635
1636     my $old = pop(@{$self->query_nodes});
1637     pop(@{$self->query_nodes}) if (@{$self->query_nodes});
1638
1639     return $old;
1640 }
1641
1642 sub query_nodes {
1643     my $self = shift;
1644     return $self->{query};
1645 }
1646
1647 sub floating {
1648     my $self = shift;
1649     my $f = shift;
1650     $self->{floating} = $f if (defined $f);
1651     return $self->{floating};
1652 }
1653
1654 sub add_node {
1655     my $self = shift;
1656     my $node = shift;
1657
1658     $self->{query} ||= [];
1659     push(@{$self->{query}}, $self->joiner) if (@{$self->{query}});
1660     push(@{$self->{query}}, $node);
1661
1662     return $self;
1663 }
1664
1665 sub top_plan {
1666     my $self = shift;
1667
1668     return $self->{level} ? 0 : 1;
1669 }
1670
1671 sub plan_level {
1672     my $self = shift;
1673     my $level = shift;
1674
1675     if (defined $level) {
1676         $self->{level} = $level;
1677         for (@{$self->query_nodes}) {
1678             $_->plan_level($level + 1) if (ref and $_->isa('QueryParser::query_plan'));
1679         }
1680     }
1681             
1682     return $self->{level};
1683 }
1684
1685 sub joiner {
1686     my $self = shift;
1687     my $joiner = shift;
1688
1689     $self->{joiner} = $joiner if ($joiner);
1690     return $self->{joiner};
1691 }
1692
1693 sub modifiers {
1694     my $self = shift;
1695     $self->{modifiers} ||= [];
1696     return $self->{modifiers};
1697 }
1698
1699 sub add_modifier {
1700     my $self = shift;
1701     my $modifier = shift;
1702
1703     $self->{modifiers} ||= [];
1704     $self->{modifiers} = [ grep {$_->name ne $modifier->name} @{$self->{modifiers}} ];
1705
1706     push(@{$self->{modifiers}}, $modifier);
1707
1708     return $self;
1709 }
1710
1711 sub facets {
1712     my $self = shift;
1713     $self->{facets} ||= [];
1714     return $self->{facets};
1715 }
1716
1717 sub add_facet {
1718     my $self = shift;
1719     my $facet = shift;
1720
1721     $self->{facets} ||= [];
1722     $self->{facets} = [ grep {$_->name ne $facet->name} @{$self->{facets}} ];
1723
1724     push(@{$self->{facets}}, $facet);
1725
1726     return $self;
1727 }
1728
1729 sub filters {
1730     my $self = shift;
1731     $self->{filters} ||= [];
1732     return $self->{filters};
1733 }
1734
1735 sub add_filter {
1736     my $self = shift;
1737     my $filter = shift;
1738
1739     $self->{filters} ||= [];
1740
1741     push(@{$self->{filters}}, $filter);
1742
1743     return $self;
1744 }
1745
1746 # %opts supports two options at this time:
1747 #   no_phrases :
1748 #       If true, do not do anything to the phrases
1749 #       fields on any discovered nodes.
1750 #   with_config :
1751 #       If true, also return the query parser config as part of the blob.
1752 #       This will get set back to 0 before recursion to avoid repetition.
1753 sub to_abstract_query {
1754     my $self = shift;
1755     my %opts = @_;
1756
1757     my $pkg = ref $self->QueryParser || $self->QueryParser;
1758
1759     my $abstract_query = {
1760         type => "query_plan",
1761         floating => $self->floating,
1762         level => $self->plan_level,
1763         filters => [map { $_->to_abstract_query } @{$self->filters}],
1764         modifiers => [map { $_->to_abstract_query } @{$self->modifiers}]
1765     };
1766
1767     if ($opts{with_config}) {
1768         $opts{with_config} = 0;
1769         $abstract_query->{config} = $QueryParser::parser_config{$pkg};
1770     }
1771
1772     my $kids = [];
1773
1774     for my $qnode (@{$self->query_nodes}) {
1775         # Remember: qnode can be a joiner string, a node, or another query_plan
1776
1777         if (QueryParser::_util::is_joiner($qnode)) {
1778             if ($abstract_query->{children}) {
1779                 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
1780                 next if $open_joiner eq $qnode;
1781
1782                 my $oldroot = $abstract_query->{children};
1783                 $kids = [$oldroot];
1784                 $abstract_query->{children} = {$qnode => $kids};
1785             } else {
1786                 $abstract_query->{children} = {$qnode => $kids};
1787             }
1788         } else {
1789             push @$kids, $qnode->to_abstract_query(%opts);
1790         }
1791     }
1792
1793     $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
1794     return $abstract_query;
1795 }
1796
1797
1798 #-------------------------------
1799 package QueryParser::query_plan::node;
1800 use Data::Dumper;
1801 $Data::Dumper::Indent = 0;
1802
1803 sub new {
1804     my $pkg = shift;
1805     $pkg = ref($pkg) || $pkg;
1806     my %args = @_;
1807
1808     return bless \%args => $pkg;
1809 }
1810
1811 sub new_atom {
1812     my $self = shift;
1813     my $pkg = ref($self) || $self;
1814     return do{$pkg.'::atom'}->new( @_ );
1815 }
1816
1817 sub requested_class { # also split into classname, fields and alias
1818     my $self = shift;
1819     my $class = shift;
1820
1821     if ($class) {
1822         my @afields;
1823         my (undef, $alias) = split '#', $class;
1824         if ($alias) {
1825             $class =~ s/#[^|]+//;
1826             ($alias, @afields) = split '\|', $alias;
1827         }
1828
1829         my @fields = @afields;
1830         my ($class_part, @field_parts) = split '\|', $class;
1831         for my $f (@field_parts) {
1832              push(@fields, $f) unless (grep { $f eq $_ } @fields);
1833         }
1834
1835         $class_part ||= $class;
1836
1837         $self->{requested_class} = $class;
1838         $self->{alias} = $alias if $alias;
1839         $self->{alias_fields} = \@afields if $alias;
1840         $self->{classname} = $class_part;
1841         $self->{fields} = \@fields;
1842     }
1843
1844     return $self->{requested_class};
1845 }
1846
1847 sub plan {
1848     my $self = shift;
1849     my $plan = shift;
1850
1851     $self->{plan} = $plan if ($plan);
1852     return $self->{plan};
1853 }
1854
1855 sub alias {
1856     my $self = shift;
1857     my $alias = shift;
1858
1859     $self->{alias} = $alias if ($alias);
1860     return $self->{alias};
1861 }
1862
1863 sub alias_fields {
1864     my $self = shift;
1865     my $alias = shift;
1866
1867     $self->{alias_fields} = $alias if ($alias);
1868     return $self->{alias_fields};
1869 }
1870
1871 sub classname {
1872     my $self = shift;
1873     my $class = shift;
1874
1875     $self->{classname} = $class if ($class);
1876     return $self->{classname};
1877 }
1878
1879 sub fields {
1880     my $self = shift;
1881     my @fields = @_;
1882
1883     $self->{fields} ||= [];
1884     $self->{fields} = \@fields if (@fields);
1885     return $self->{fields};
1886 }
1887
1888 sub phrases {
1889     my $self = shift;
1890     my @phrases = @_;
1891
1892     $self->{phrases} ||= [];
1893     $self->{phrases} = \@phrases if (@phrases);
1894     return $self->{phrases};
1895 }
1896
1897 sub add_phrase {
1898     my $self = shift;
1899     my $phrase = shift;
1900
1901     push(@{$self->phrases}, $phrase);
1902
1903     return $self;
1904 }
1905
1906 sub negate {
1907     my $self = shift;
1908     my $negate = shift;
1909
1910     $self->{negate} = $negate if (defined $negate);
1911
1912     return $self->{negate};
1913 }
1914
1915 sub query_atoms {
1916     my $self = shift;
1917     my @query_atoms = @_;
1918
1919     $self->{query_atoms} ||= [];
1920     $self->{query_atoms} = \@query_atoms if (@query_atoms);
1921     return $self->{query_atoms};
1922 }
1923
1924 sub add_fts_atom {
1925     my $self = shift;
1926     my $atom = shift;
1927
1928     if (!ref($atom)) {
1929         my $content = $atom;
1930         my @parts = @_;
1931
1932         $atom = $self->new_atom( content => $content, @parts );
1933     }
1934
1935     push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1936     push(@{$self->query_atoms}, $atom);
1937
1938     return $self;
1939 }
1940
1941 sub add_dummy_atom {
1942     my $self = shift;
1943     my @parts = @_;
1944
1945     my $atom = $self->new_atom( @parts, dummy => 1 );
1946
1947     push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1948     push(@{$self->query_atoms}, $atom);
1949
1950     return $self;
1951 }
1952
1953 # This will find up to one occurence of @$short_list within @$long_list, and
1954 # replace it with the single atom $replacement.
1955 sub replace_phrase_in_abstract_query {
1956     my ($self, $short_list, $long_list, $replacement) = @_;
1957
1958     my $success = 0;
1959     my @already = ();
1960     my $goal = scalar @$short_list;
1961
1962     for (my $i = 0; $i < scalar (@$long_list); $i++) {
1963         my $right = $long_list->[$i];
1964
1965         if (QueryParser::_util::compare_abstract_atoms(
1966             $short_list->[scalar @already], $right
1967         )) {
1968             push @already, $i;
1969         } elsif (scalar @already) {
1970             @already = ();
1971             next;
1972         }
1973
1974         if (scalar @already == $goal) {
1975             splice @$long_list, $already[0], scalar(@already), $replacement;
1976             $success = 1;
1977             last;
1978         }
1979     }
1980
1981     return $success;
1982 }
1983
1984 sub to_abstract_query {
1985     my $self = shift;
1986     my %opts = @_;
1987
1988     my $pkg = ref $self->plan->QueryParser || $self->plan->QueryParser;
1989
1990     my $abstract_query = {
1991         "type" => "node",
1992         "alias" => $self->alias,
1993         "alias_fields" => $self->alias_fields,
1994         "class" => $self->classname,
1995         "fields" => $self->fields
1996     };
1997
1998     my $kids = [];
1999
2000     for my $qatom (@{$self->query_atoms}) {
2001         if (QueryParser::_util::is_joiner($qatom)) {
2002             if ($abstract_query->{children}) {
2003                 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2004                 next if $open_joiner eq $qatom;
2005
2006                 my $oldroot = $abstract_query->{children};
2007                 $kids = [$oldroot];
2008                 $abstract_query->{children} = {$qatom => $kids};
2009             } else {
2010                 $abstract_query->{children} = {$qatom => $kids};
2011             }
2012         } else {
2013             push @$kids, $qatom->to_abstract_query;
2014         }
2015     }
2016
2017     $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2018
2019     if ($self->{phrases} and not $opts{no_phrases}) {
2020         for my $phrase (@{$self->{phrases}}) {
2021             # Phrases appear duplication in a real QP tree, and we don't want
2022             # that duplication in our abstract query.  So for all our phrases,
2023             # break them into atoms as QP would, and remove any matching
2024             # sequences of atoms from our abstract query.
2025
2026             my $tmp_prefix = '';
2027             $tmp_prefix = $QueryParser::parser_config{$pkg}{operators}{disallowed} if ($self->{negate});
2028
2029             my $tmptree = $self->{plan}->{QueryParser}->new(query => $tmp_prefix.'"'.$phrase.'"')->parse->parse_tree;
2030             if ($tmptree) {
2031                 # For a well-behaved phrase, we should now have only one node
2032                 # in the $tmptree query plan, and that node should have an
2033                 # orderly list of atoms and joiners.
2034
2035                 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
2036                     my $tmplist;
2037
2038                     eval {
2039                         $tmplist = $tmptree->{query}->[0]->to_abstract_query(
2040                             no_phrases => 1
2041                         )->{children}->{'&'}->[0]->{children}->{'&'};
2042                     };
2043                     next if $@;
2044
2045                     foreach (
2046                         QueryParser::_util::find_arrays_in_abstract($abstract_query->{children})
2047                     ) {
2048                         last if $self->replace_phrase_in_abstract_query(
2049                             $tmplist,
2050                             $_,
2051                             QueryParser::_util::fake_abstract_atom_from_phrase($phrase, $self->{negate}, $pkg)
2052                         );
2053                     }
2054                 }
2055             }
2056         }
2057     }
2058
2059     $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2060     return $abstract_query;
2061 }
2062
2063 #-------------------------------
2064 package QueryParser::query_plan::node::atom;
2065
2066 sub new {
2067     my $pkg = shift;
2068     $pkg = ref($pkg) || $pkg;
2069     my %args = @_;
2070
2071     return bless \%args => $pkg;
2072 }
2073
2074 sub node {
2075     my $self = shift;
2076     return undef unless (ref $self);
2077     return $self->{node};
2078 }
2079
2080 sub content {
2081     my $self = shift;
2082     return undef unless (ref $self);
2083     return $self->{content};
2084 }
2085
2086 sub prefix {
2087     my $self = shift;
2088     return undef unless (ref $self);
2089     return $self->{prefix};
2090 }
2091
2092 sub suffix {
2093     my $self = shift;
2094     return undef unless (ref $self);
2095     return $self->{suffix};
2096 }
2097
2098 sub to_abstract_query {
2099     my ($self) = @_;
2100     
2101     return {
2102         (map { $_ => $self->$_ } qw/prefix suffix content/),
2103         "type" => "atom"
2104     };
2105 }
2106 #-------------------------------
2107 package QueryParser::query_plan::filter;
2108
2109 sub new {
2110     my $pkg = shift;
2111     $pkg = ref($pkg) || $pkg;
2112     my %args = @_;
2113
2114     return bless \%args => $pkg;
2115 }
2116
2117 sub plan {
2118     my $self = shift;
2119     return $self->{plan};
2120 }
2121
2122 sub name {
2123     my $self = shift;
2124     return $self->{name};
2125 }
2126
2127 sub negate {
2128     my $self = shift;
2129     return $self->{negate};
2130 }
2131
2132 sub args {
2133     my $self = shift;
2134     return $self->{args};
2135 }
2136
2137 sub to_abstract_query {
2138     my ($self) = @_;
2139     
2140     return {
2141         map { $_ => $self->$_ } qw/name negate args/
2142     };
2143 }
2144
2145 #-------------------------------
2146 package QueryParser::query_plan::facet;
2147
2148 sub new {
2149     my $pkg = shift;
2150     $pkg = ref($pkg) || $pkg;
2151     my %args = @_;
2152
2153     return bless \%args => $pkg;
2154 }
2155
2156 sub plan {
2157     my $self = shift;
2158     return $self->{plan};
2159 }
2160
2161 sub name {
2162     my $self = shift;
2163     return $self->{name};
2164 }
2165
2166 sub negate {
2167     my $self = shift;
2168     return $self->{negate};
2169 }
2170
2171 sub values {
2172     my $self = shift;
2173     return $self->{'values'};
2174 }
2175
2176 sub to_abstract_query {
2177     my ($self) = @_;
2178
2179     return {
2180         (map { $_ => $self->$_ } qw/name negate values/),
2181         "type" => "facet"
2182     };
2183 }
2184
2185 #-------------------------------
2186 package QueryParser::query_plan::modifier;
2187
2188 sub new {
2189     my $pkg = shift;
2190     $pkg = ref($pkg) || $pkg;
2191     my $modifier = shift;
2192     my $negate = shift;
2193
2194     return bless { name => $modifier, negate => $negate } => $pkg;
2195 }
2196
2197 sub name {
2198     my $self = shift;
2199     return $self->{name};
2200 }
2201
2202 sub negate {
2203     my $self = shift;
2204     return $self->{negate};
2205 }
2206
2207 sub to_abstract_query {
2208     my ($self) = @_;
2209     
2210     return $self->name;
2211 }
2212 1;
2213