]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm
QueryParser: Protect phrase parsing
[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 $pkg->operator('disallowed')) {
1158                     $class_node->add_dummy_atom( node => $class_node );
1159                     $class_node->add_unphrase( $phrase );
1160                     $phrase = '';
1161                     #$phrase =~ s/(^|\s)\b/$1-/g;
1162                 } else { 
1163                     $class_node->add_phrase( $phrase );
1164                 }
1165
1166                 # Cleanup the phrase to make it so that we don't parse things in it as anything other than atoms
1167                 $phrase =~ s/$phrase_cleanup_re/ /g;
1168
1169                 $_ = $phrase . $';
1170
1171             }
1172
1173             local $last_type = '';
1174
1175         } elsif (/^\s*($required_re|$disallowed_re)([^${group_end}${float_end}\s"]+)/) { # convert require/disallow word to {un}phrase
1176             warn '  'x$recursing."Encountered required atom (mini phrase), transforming for phrase parse: $1\n" if $self->debug;
1177
1178             $_ = $1 . '"' . $2 . '"' . $';
1179
1180             local $last_type = '';
1181         } elsif (/^\s*([^${group_end}${float_end}\s]+)/o) { # atom
1182             warn '  'x$recursing."Encountered atom: $1\n" if $self->debug;
1183             warn '  'x$recursing."Remainder: $'\n" if $self->debug;
1184
1185             my $atom = $1;
1186             my $after = $';
1187
1188             $_ = $after;
1189             local $last_type = '';
1190
1191             my $class_node = $struct->classed_node($current_class);
1192
1193             my $prefix = ($atom =~ s/^$disallowed_re//o) ? '!' : '';
1194             my $truncate = ($atom =~ s/\*$//o) ? '*' : '';
1195
1196             if ($atom ne '' and !grep { $atom =~ /^\Q$_\E+$/ } ('&','|')) { # throw away & and |, not allowed in tsquery, and not really useful anyway
1197 #                $class_node->add_phrase( $atom ) if ($atom =~ s/^$required_re//o);
1198 #                $class_node->add_unphrase( $atom ) if ($prefix eq '!');
1199
1200                 $class_node->add_fts_atom( $atom, suffix => $truncate, prefix => $prefix, node => $class_node );
1201                 $struct->joiner( '&' );
1202             }
1203
1204             local $last_type = '';
1205         } 
1206
1207         last unless ($_);
1208
1209     }
1210
1211     $struct = undef if 
1212         scalar(@{$struct->query_nodes}) == 0 &&
1213         scalar(@{$struct->filters}) == 0 &&
1214         !$struct->top_plan;
1215
1216     return $struct if !wantarray;
1217     return ($struct, $remainder);
1218 }
1219
1220 =head2 find_class_index
1221
1222     $index = $QParser->find_class_index($class, $query);
1223 =cut
1224
1225 sub find_class_index {
1226     my $class = shift;
1227     my $query = shift;
1228
1229     my ($class_part, @field_parts) = split '\|', $class;
1230     $class_part ||= $class;
1231
1232     for my $idx ( 0 .. scalar(@$query) - 1 ) {
1233         next unless ref($$query[$idx]);
1234         return $idx if ( $$query[$idx]{requested_class} && $class eq $$query[$idx]{requested_class} );
1235     }
1236
1237     push(@$query, { classname => $class_part, (@field_parts ? (fields => \@field_parts) : ()), requested_class => $class, ftsquery => [], phrases => [] });
1238     return -1;
1239 }
1240
1241 =head2 core_limit
1242
1243     $limit = $QParser->core_limit([$limit]);
1244
1245 Return and/or set the core_limit.
1246 =cut
1247
1248 sub core_limit {
1249     my $self = shift;
1250     my $l = shift;
1251     $self->{core_limit} = $l if ($l);
1252     return $self->{core_limit};
1253 }
1254
1255 =head2 superpage
1256
1257     $superpage = $QParser->superpage([$superpage]);
1258
1259 Return and/or set the superpage.
1260 =cut
1261
1262 sub superpage {
1263     my $self = shift;
1264     my $l = shift;
1265     $self->{superpage} = $l if ($l);
1266     return $self->{superpage};
1267 }
1268
1269 =head2 superpage_size
1270
1271     $size = $QParser->superpage_size([$size]);
1272
1273 Return and/or set the superpage size.
1274 =cut
1275
1276 sub superpage_size {
1277     my $self = shift;
1278     my $l = shift;
1279     $self->{superpage_size} = $l if ($l);
1280     return $self->{superpage_size};
1281 }
1282
1283
1284 #-------------------------------
1285 package QueryParser::_util;
1286
1287 # At this level, joiners are always & or |.  This is not
1288 # the external, configurable representation of joiners that
1289 # defaults to # && and ||.
1290 sub is_joiner {
1291     my $str = shift;
1292
1293     return (not ref $str and ($str eq '&' or $str eq '|'));
1294 }
1295
1296 sub default_joiner { '&' }
1297
1298 # 0 for different, 1 for the same.
1299 sub compare_abstract_atoms {
1300     my ($left, $right) = @_;
1301
1302     foreach (qw/prefix suffix content/) {
1303         no warnings;    # undef can stand in for '' here
1304         return 0 unless $left->{$_} eq $right->{$_};
1305     }
1306
1307     return 1;
1308 }
1309
1310 sub fake_abstract_atom_from_phrase {
1311     my $phrase = shift;
1312     my $neg = shift;
1313     my $qp_class = shift || 'QueryParser';
1314
1315     my $prefix = '"';
1316     if ($neg) {
1317         $prefix =
1318             $QueryParser::parser_config{$qp_class}{operators}{disallowed} .
1319             $prefix;
1320     }
1321
1322     return {
1323         "type" => "atom", "prefix" => $prefix, "suffix" => '"',
1324         "content" => $phrase
1325     }
1326 }
1327
1328 sub find_arrays_in_abstract {
1329     my ($hash) = @_;
1330
1331     my @arrays;
1332     foreach my $key (keys %$hash) {
1333         if (ref $hash->{$key} eq "ARRAY") {
1334             push @arrays, $hash->{$key};
1335             foreach (@{$hash->{$key}}) {
1336                 push @arrays, find_arrays_in_abstract($_);
1337             }
1338         }
1339     }
1340
1341     return @arrays;
1342 }
1343
1344 #-------------------------------
1345 package QueryParser::Canonicalize;  # not OO
1346 use Data::Dumper;
1347
1348 sub _abstract_query2str_filter {
1349     my $f = shift;
1350     my $qp_class = shift || 'QueryParser';
1351     my $qpconfig = $QueryParser::parser_config{$qp_class};
1352
1353     return sprintf(
1354         '%s%s(%s)',
1355         $f->{negate} ? $qpconfig->{operators}{disallowed} : "",
1356         $f->{name},
1357         join(",", @{$f->{args}})
1358     );
1359 }
1360
1361 sub _abstract_query2str_modifier {
1362     my $f = shift;
1363     my $qp_class = shift || 'QueryParser';
1364     my $qpconfig = $QueryParser::parser_config{$qp_class};
1365
1366     return $qpconfig->{operators}{modifier} . $f;
1367 }
1368
1369 sub _kid_list {
1370     my $children = shift;
1371     my $op = (keys %$children)[0];
1372     return @{$$children{$op}};
1373 }
1374
1375
1376 # This should produce an equivalent query to the original, given an
1377 # abstract_query.
1378 sub abstract_query2str_impl {
1379     my $abstract_query  = shift;
1380     my $depth = shift || 0;
1381
1382     my $qp_class ||= shift || 'QueryParser';
1383     my $force_qp_node = shift || 0;
1384     my $qpconfig = $QueryParser::parser_config{$qp_class};
1385
1386     my $fs = $qpconfig->{operators}{float_start};
1387     my $fe = $qpconfig->{operators}{float_end};
1388     my $gs = $qpconfig->{operators}{group_start};
1389     my $ge = $qpconfig->{operators}{group_end};
1390     my $and = $qpconfig->{operators}{and};
1391     my $or = $qpconfig->{operators}{or};
1392
1393     my $isnode = 0;
1394     my $size = 0;
1395     my $q = "";
1396
1397     if (exists $abstract_query->{type}) {
1398         if ($abstract_query->{type} eq 'query_plan') {
1399             $q .= join(" ", map { _abstract_query2str_filter($_, $qp_class) } @{$abstract_query->{filters}}) if
1400                 exists $abstract_query->{filters};
1401
1402             $q .= ($q ? ' ' : '') . join(" ", map { _abstract_query2str_modifier($_, $qp_class) } @{$abstract_query->{modifiers}}) if
1403                 exists $abstract_query->{modifiers};
1404
1405             $size = _kid_list($abstract_query->{children});
1406             $isnode = 1 if ($size > 1 and ($force_qp_node or $depth));
1407             #warn "size: $size, depth: $depth, isnode: $isnode, AQ: ".Dumper($abstract_query);
1408         } elsif ($abstract_query->{type} eq 'node') {
1409             if ($abstract_query->{alias}) {
1410                 $q .= ($q ? ' ' : '') . $abstract_query->{alias};
1411                 $q .= "|$_" foreach @{$abstract_query->{alias_fields}};
1412             } else {
1413                 $q .= ($q ? ' ' : '') . $abstract_query->{class};
1414                 $q .= "|$_" foreach @{$abstract_query->{fields}};
1415             }
1416             $q .= ":";
1417             $isnode = 1;
1418         } elsif ($abstract_query->{type} eq 'atom') {
1419             my $prefix = $abstract_query->{prefix} || '';
1420             $prefix = $qpconfig->{operators}{disallowed} if $prefix eq '!';
1421             $q .= ($q ? ' ' : '') . $prefix .
1422                 ($abstract_query->{content} || '') .
1423                 ($abstract_query->{suffix} || '');
1424         } elsif ($abstract_query->{type} eq 'facet') {
1425             # facet syntax [ # ] is hardcoded I guess?
1426             my $prefix = $abstract_query->{negate} ? $qpconfig->{operators}{disallowed} : '';
1427             $q .= ($q ? ' ' : '') . $prefix . $abstract_query->{name} . "[" .
1428                 join(" # ", @{$abstract_query->{values}}) . "]";
1429         }
1430     }
1431
1432     my $next_depth = int($size > 1);
1433
1434     if (exists $abstract_query->{children}) {
1435
1436         my $op = (keys(%{$abstract_query->{children}}))[0];
1437
1438         if ($abstract_query->{floating}) { # always the top node!
1439             my $sub_node = pop @{$abstract_query->{children}{$op}};
1440
1441             $abstract_query->{floating} = 0;
1442             $q = $fs . " " . abstract_query2str_impl($abstract_query,0,$qp_class, 1) . $fe. " ";
1443
1444             $abstract_query = $sub_node;
1445         }
1446
1447         if ($abstract_query && exists $abstract_query->{children}) {
1448             $op = (keys(%{$abstract_query->{children}}))[0];
1449             $q .= ($q ? ' ' : '') . join(
1450                 ($op eq '&' ? ' ' : " $or "),
1451                 map {
1452                     my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1453                 } @{$abstract_query->{children}{$op}}
1454             );
1455         }
1456     } elsif ($abstract_query->{'&'} or $abstract_query->{'|'}) {
1457         my $op = (keys(%{$abstract_query}))[0];
1458         $q .= ($q ? ' ' : '') . join(
1459             ($op eq '&' ? ' ' : " $or "),
1460             map {
1461                     my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1462             } @{$abstract_query->{$op}}
1463         );
1464     }
1465
1466     $q = "$gs$q$ge" if ($isnode);
1467
1468     return $q;
1469 }
1470
1471 #-------------------------------
1472 package QueryParser::query_plan;
1473
1474 sub QueryParser {
1475     my $self = shift;
1476     return undef unless ref($self);
1477     return $self->{QueryParser};
1478 }
1479
1480 sub new {
1481     my $pkg = shift;
1482     $pkg = ref($pkg) || $pkg;
1483     my %args = (query => [], joiner => '&', @_);
1484
1485     return bless \%args => $pkg;
1486 }
1487
1488 sub new_node {
1489     my $self = shift;
1490     my $pkg = ref($self) || $self;
1491     my $node = do{$pkg.'::node'}->new( plan => $self, @_ );
1492     $self->add_node( $node );
1493     return $node;
1494 }
1495
1496 sub new_facet {
1497     my $self = shift;
1498     my $pkg = ref($self) || $self;
1499     my $name = shift;
1500     my $args = shift;
1501     my $negate = shift;
1502
1503     my $node = do{$pkg.'::facet'}->new( plan => $self, name => $name, 'values' => $args, negate => $negate );
1504     $self->add_node( $node );
1505
1506     return $node;
1507 }
1508
1509 sub new_filter {
1510     my $self = shift;
1511     my $pkg = ref($self) || $self;
1512     my $name = shift;
1513     my $args = shift;
1514     my $negate = shift;
1515
1516     my $node = do{$pkg.'::filter'}->new( plan => $self, name => $name, args => $args, negate => $negate );
1517     $self->add_filter( $node );
1518
1519     return $node;
1520 }
1521
1522
1523 sub _merge_filters {
1524     my $left_filter = shift;
1525     my $right_filter = shift;
1526     my $join = shift;
1527
1528     return undef unless $left_filter or $right_filter;
1529     return $right_filter unless $left_filter;
1530     return $left_filter unless $right_filter;
1531
1532     my $args = $left_filter->{args} || [];
1533
1534     if ($join eq '|') {
1535         push(@$args, @{$right_filter->{args}});
1536
1537     } else {
1538         # find the intersect values
1539         my %new_vals;
1540         map { $new_vals{$_} = 1 } @{$right_filter->{args} || []};
1541         $args = [ grep { $new_vals{$_} } @$args ];
1542     }
1543
1544     $left_filter->{args} = $args;
1545     return $left_filter;
1546 }
1547
1548 sub collapse_filters {
1549     my $self = shift;
1550     my $name = shift;
1551
1552     # start by merging any filters at this level.
1553     # like-level filters are always ORed together
1554
1555     my $cur_filter;
1556     my @cur_filters = grep {$_->name eq $name } @{ $self->filters };
1557     if (@cur_filters) {
1558         $cur_filter = shift @cur_filters;
1559         my $args = $cur_filter->{args} || [];
1560         $cur_filter = _merge_filters($cur_filter, $_, '|') for @cur_filters;
1561     }
1562
1563     # next gather the collapsed filters from sub-plans and 
1564     # merge them with our own
1565
1566     my @subquery = @{$self->{query}};
1567
1568     while (@subquery) {
1569         my $blob = shift @subquery;
1570         shift @subquery; # joiner
1571         next unless $blob->isa('QueryParser::query_plan');
1572         my $sub_filter = $blob->collapse_filters($name);
1573         $cur_filter = _merge_filters($cur_filter, $sub_filter, $self->joiner);
1574     }
1575
1576     if ($self->QueryParser->debug) {
1577         my @args = ($cur_filter and $cur_filter->{args}) ? @{$cur_filter->{args}} : ();
1578         warn "collapse_filters($name) => [@args]\n";
1579     }
1580
1581     return $cur_filter;
1582 }
1583
1584 sub find_filter {
1585     my $self = shift;
1586     my $needle = shift;;
1587     return undef unless ($needle);
1588
1589     my $filter = $self->collapse_filters($needle);
1590
1591     warn "find_filter($needle) => " . 
1592         (($filter and $filter->{args}) ? "@{$filter->{args}}" : '[]') . "\n" 
1593         if $self->QueryParser->debug;
1594
1595     return $filter ? ($filter) : ();
1596 }
1597
1598 sub find_modifier {
1599     my $self = shift;
1600     my $needle = shift;;
1601     return undef unless ($needle);
1602     return grep { $_->name eq $needle } @{ $self->modifiers };
1603 }
1604
1605 sub new_modifier {
1606     my $self = shift;
1607     my $pkg = ref($self) || $self;
1608     my $name = shift;
1609
1610     my $node = do{$pkg.'::modifier'}->new( $name );
1611     $self->add_modifier( $node );
1612
1613     return $node;
1614 }
1615
1616 sub classed_node {
1617     my $self = shift;
1618     my $requested_class = shift;
1619
1620     my $node;
1621     for my $n (@{$self->{query}}) {
1622         next unless (ref($n) && $n->isa( 'QueryParser::query_plan::node' ));
1623         if ($n->requested_class eq $requested_class) {
1624             $node = $n;
1625             last;
1626         }
1627     }
1628
1629     if (!$node) {
1630         $node = $self->new_node;
1631         $node->requested_class( $requested_class );
1632     }
1633
1634     return $node;
1635 }
1636
1637 sub remove_last_node {
1638     my $self = shift;
1639     my $requested_class = shift;
1640
1641     my $old = pop(@{$self->query_nodes});
1642     pop(@{$self->query_nodes}) if (@{$self->query_nodes});
1643
1644     return $old;
1645 }
1646
1647 sub query_nodes {
1648     my $self = shift;
1649     return $self->{query};
1650 }
1651
1652 sub floating {
1653     my $self = shift;
1654     my $f = shift;
1655     $self->{floating} = $f if (defined $f);
1656     return $self->{floating};
1657 }
1658
1659 sub add_node {
1660     my $self = shift;
1661     my $node = shift;
1662
1663     $self->{query} ||= [];
1664     push(@{$self->{query}}, $self->joiner) if (@{$self->{query}});
1665     push(@{$self->{query}}, $node);
1666
1667     return $self;
1668 }
1669
1670 sub top_plan {
1671     my $self = shift;
1672
1673     return $self->{level} ? 0 : 1;
1674 }
1675
1676 sub plan_level {
1677     my $self = shift;
1678     my $level = shift;
1679
1680     if (defined $level) {
1681         $self->{level} = $level;
1682         for (@{$self->query_nodes}) {
1683             $_->plan_level($level + 1) if (ref and $_->isa('QueryParser::query_plan'));
1684         }
1685     }
1686             
1687     return $self->{level};
1688 }
1689
1690 sub joiner {
1691     my $self = shift;
1692     my $joiner = shift;
1693
1694     $self->{joiner} = $joiner if ($joiner);
1695     return $self->{joiner};
1696 }
1697
1698 sub modifiers {
1699     my $self = shift;
1700     $self->{modifiers} ||= [];
1701     return $self->{modifiers};
1702 }
1703
1704 sub add_modifier {
1705     my $self = shift;
1706     my $modifier = shift;
1707
1708     $self->{modifiers} ||= [];
1709     $self->{modifiers} = [ grep {$_->name ne $modifier->name} @{$self->{modifiers}} ];
1710
1711     push(@{$self->{modifiers}}, $modifier);
1712
1713     return $self;
1714 }
1715
1716 sub facets {
1717     my $self = shift;
1718     $self->{facets} ||= [];
1719     return $self->{facets};
1720 }
1721
1722 sub add_facet {
1723     my $self = shift;
1724     my $facet = shift;
1725
1726     $self->{facets} ||= [];
1727     $self->{facets} = [ grep {$_->name ne $facet->name} @{$self->{facets}} ];
1728
1729     push(@{$self->{facets}}, $facet);
1730
1731     return $self;
1732 }
1733
1734 sub filters {
1735     my $self = shift;
1736     $self->{filters} ||= [];
1737     return $self->{filters};
1738 }
1739
1740 sub add_filter {
1741     my $self = shift;
1742     my $filter = shift;
1743
1744     $self->{filters} ||= [];
1745
1746     push(@{$self->{filters}}, $filter);
1747
1748     return $self;
1749 }
1750
1751 # %opts supports two options at this time:
1752 #   no_phrases :
1753 #       If true, do not do anything to the phrases and unphrases
1754 #       fields on any discovered nodes.
1755 #   with_config :
1756 #       If true, also return the query parser config as part of the blob.
1757 #       This will get set back to 0 before recursion to avoid repetition.
1758 sub to_abstract_query {
1759     my $self = shift;
1760     my %opts = @_;
1761
1762     my $pkg = ref $self->QueryParser || $self->QueryParser;
1763
1764     my $abstract_query = {
1765         type => "query_plan",
1766         floating => $self->floating,
1767         level => $self->plan_level,
1768         filters => [map { $_->to_abstract_query } @{$self->filters}],
1769         modifiers => [map { $_->to_abstract_query } @{$self->modifiers}]
1770     };
1771
1772     if ($opts{with_config}) {
1773         $opts{with_config} = 0;
1774         $abstract_query->{config} = $QueryParser::parser_config{$pkg};
1775     }
1776
1777     my $kids = [];
1778
1779     for my $qnode (@{$self->query_nodes}) {
1780         # Remember: qnode can be a joiner string, a node, or another query_plan
1781
1782         if (QueryParser::_util::is_joiner($qnode)) {
1783             if ($abstract_query->{children}) {
1784                 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
1785                 next if $open_joiner eq $qnode;
1786
1787                 my $oldroot = $abstract_query->{children};
1788                 $kids = [$oldroot];
1789                 $abstract_query->{children} = {$qnode => $kids};
1790             } else {
1791                 $abstract_query->{children} = {$qnode => $kids};
1792             }
1793         } else {
1794             push @$kids, $qnode->to_abstract_query(%opts);
1795         }
1796     }
1797
1798     $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
1799     return $abstract_query;
1800 }
1801
1802
1803 #-------------------------------
1804 package QueryParser::query_plan::node;
1805 use Data::Dumper;
1806 $Data::Dumper::Indent = 0;
1807
1808 sub new {
1809     my $pkg = shift;
1810     $pkg = ref($pkg) || $pkg;
1811     my %args = @_;
1812
1813     return bless \%args => $pkg;
1814 }
1815
1816 sub new_atom {
1817     my $self = shift;
1818     my $pkg = ref($self) || $self;
1819     return do{$pkg.'::atom'}->new( @_ );
1820 }
1821
1822 sub requested_class { # also split into classname, fields and alias
1823     my $self = shift;
1824     my $class = shift;
1825
1826     if ($class) {
1827         my @afields;
1828         my (undef, $alias) = split '#', $class;
1829         if ($alias) {
1830             $class =~ s/#[^|]+//;
1831             ($alias, @afields) = split '\|', $alias;
1832         }
1833
1834         my @fields = @afields;
1835         my ($class_part, @field_parts) = split '\|', $class;
1836         for my $f (@field_parts) {
1837              push(@fields, $f) unless (grep { $f eq $_ } @fields);
1838         }
1839
1840         $class_part ||= $class;
1841
1842         $self->{requested_class} = $class;
1843         $self->{alias} = $alias if $alias;
1844         $self->{alias_fields} = \@afields if $alias;
1845         $self->{classname} = $class_part;
1846         $self->{fields} = \@fields;
1847     }
1848
1849     return $self->{requested_class};
1850 }
1851
1852 sub plan {
1853     my $self = shift;
1854     my $plan = shift;
1855
1856     $self->{plan} = $plan if ($plan);
1857     return $self->{plan};
1858 }
1859
1860 sub alias {
1861     my $self = shift;
1862     my $alias = shift;
1863
1864     $self->{alias} = $alias if ($alias);
1865     return $self->{alias};
1866 }
1867
1868 sub alias_fields {
1869     my $self = shift;
1870     my $alias = shift;
1871
1872     $self->{alias_fields} = $alias if ($alias);
1873     return $self->{alias_fields};
1874 }
1875
1876 sub classname {
1877     my $self = shift;
1878     my $class = shift;
1879
1880     $self->{classname} = $class if ($class);
1881     return $self->{classname};
1882 }
1883
1884 sub fields {
1885     my $self = shift;
1886     my @fields = @_;
1887
1888     $self->{fields} ||= [];
1889     $self->{fields} = \@fields if (@fields);
1890     return $self->{fields};
1891 }
1892
1893 sub phrases {
1894     my $self = shift;
1895     my @phrases = @_;
1896
1897     $self->{phrases} ||= [];
1898     $self->{phrases} = \@phrases if (@phrases);
1899     return $self->{phrases};
1900 }
1901
1902 sub unphrases {
1903     my $self = shift;
1904     my @phrases = @_;
1905
1906     $self->{unphrases} ||= [];
1907     $self->{unphrases} = \@phrases if (@phrases);
1908     return $self->{unphrases};
1909 }
1910
1911 sub add_phrase {
1912     my $self = shift;
1913     my $phrase = shift;
1914
1915     push(@{$self->phrases}, $phrase);
1916
1917     return $self;
1918 }
1919
1920 sub add_unphrase {
1921     my $self = shift;
1922     my $phrase = shift;
1923
1924     push(@{$self->unphrases}, $phrase);
1925
1926     return $self;
1927 }
1928
1929 sub query_atoms {
1930     my $self = shift;
1931     my @query_atoms = @_;
1932
1933     $self->{query_atoms} ||= [];
1934     $self->{query_atoms} = \@query_atoms if (@query_atoms);
1935     return $self->{query_atoms};
1936 }
1937
1938 sub add_fts_atom {
1939     my $self = shift;
1940     my $atom = shift;
1941
1942     if (!ref($atom)) {
1943         my $content = $atom;
1944         my @parts = @_;
1945
1946         $atom = $self->new_atom( content => $content, @parts );
1947     }
1948
1949     push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1950     push(@{$self->query_atoms}, $atom);
1951
1952     return $self;
1953 }
1954
1955 sub add_dummy_atom {
1956     my $self = shift;
1957     my @parts = @_;
1958
1959     my $atom = $self->new_atom( @parts, dummy => 1 );
1960
1961     push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1962     push(@{$self->query_atoms}, $atom);
1963
1964     return $self;
1965 }
1966
1967 # This will find up to one occurence of @$short_list within @$long_list, and
1968 # replace it with the single atom $replacement.
1969 sub replace_phrase_in_abstract_query {
1970     my ($self, $short_list, $long_list, $replacement) = @_;
1971
1972     my $success = 0;
1973     my @already = ();
1974     my $goal = scalar @$short_list;
1975
1976     for (my $i = 0; $i < scalar (@$long_list); $i++) {
1977         my $right = $long_list->[$i];
1978
1979         if (QueryParser::_util::compare_abstract_atoms(
1980             $short_list->[scalar @already], $right
1981         )) {
1982             push @already, $i;
1983         } elsif (scalar @already) {
1984             @already = ();
1985             next;
1986         }
1987
1988         if (scalar @already == $goal) {
1989             splice @$long_list, $already[0], scalar(@already), $replacement;
1990             $success = 1;
1991             last;
1992         }
1993     }
1994
1995     return $success;
1996 }
1997
1998 sub to_abstract_query {
1999     my $self = shift;
2000     my %opts = @_;
2001
2002     my $pkg = ref $self->plan->QueryParser || $self->plan->QueryParser;
2003
2004     my $abstract_query = {
2005         "type" => "node",
2006         "alias" => $self->alias,
2007         "alias_fields" => $self->alias_fields,
2008         "class" => $self->classname,
2009         "fields" => $self->fields
2010     };
2011
2012     my $kids = [];
2013
2014     for my $qatom (@{$self->query_atoms}) {
2015         if (QueryParser::_util::is_joiner($qatom)) {
2016             if ($abstract_query->{children}) {
2017                 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2018                 next if $open_joiner eq $qatom;
2019
2020                 my $oldroot = $abstract_query->{children};
2021                 $kids = [$oldroot];
2022                 $abstract_query->{children} = {$qatom => $kids};
2023             } else {
2024                 $abstract_query->{children} = {$qatom => $kids};
2025             }
2026         } else {
2027             push @$kids, $qatom->to_abstract_query;
2028         }
2029     }
2030
2031     $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2032
2033     if ($self->{phrases} and not $opts{no_phrases}) {
2034         for my $phrase (@{$self->{phrases}}) {
2035             # Phrases appear duplication in a real QP tree, and we don't want
2036             # that duplication in our abstract query.  So for all our phrases,
2037             # break them into atoms as QP would, and remove any matching
2038             # sequences of atoms from our abstract query.
2039
2040             my $tmptree = $self->{plan}->{QueryParser}->new(query => '"'.$phrase.'"')->parse->parse_tree;
2041             if ($tmptree) {
2042                 # For a well-behaved phrase, we should now have only one node
2043                 # in the $tmptree query plan, and that node should have an
2044                 # orderly list of atoms and joiners.
2045
2046                 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
2047                     my $tmplist;
2048
2049                     eval {
2050                         $tmplist = $tmptree->{query}->[0]->to_abstract_query(
2051                             no_phrases => 1
2052                         )->{children}->{'&'}->[0]->{children}->{'&'};
2053                     };
2054                     next if $@;
2055
2056                     foreach (
2057                         QueryParser::_util::find_arrays_in_abstract($abstract_query->{children})
2058                     ) {
2059                         last if $self->replace_phrase_in_abstract_query(
2060                             $tmplist,
2061                             $_,
2062                             QueryParser::_util::fake_abstract_atom_from_phrase($phrase, undef, $pkg)
2063                         );
2064                     }
2065                 }
2066             }
2067         }
2068     }
2069
2070     # Do the same as the preceding block for unphrases (negated phrases).
2071     if ($self->{unphrases} and not $opts{no_phrases}) {
2072         for my $phrase (@{$self->{unphrases}}) {
2073             my $tmptree = $self->{plan}->{QueryParser}->new(
2074                 query => $QueryParser::parser_config{$pkg}{operators}{disallowed}.
2075                     '"' . $phrase . '"'
2076             )->parse->parse_tree;
2077
2078             if ($tmptree) {
2079                 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
2080                     my $tmplist;
2081
2082                     eval {
2083                         $tmplist = $tmptree->{query}->[0]->to_abstract_query(
2084                             no_phrases => 1
2085                         )->{children}->{'&'}->[0]->{children}->{'&'};
2086                     };
2087                     next if $@;
2088
2089                     foreach (
2090                         QueryParser::_util::find_arrays_in_abstract($abstract_query->{children})
2091                     ) {
2092                         last if $self->replace_phrase_in_abstract_query(
2093                             $tmplist,
2094                             $_,
2095                             QueryParser::_util::fake_abstract_atom_from_phrase($phrase, 1, $pkg)
2096                         );
2097                     }
2098                 }
2099             }
2100         }
2101     }
2102
2103     $abstract_query->{children} ||= { QueryParser::_util::default_joiner() => $kids };
2104     return $abstract_query;
2105 }
2106
2107 #-------------------------------
2108 package QueryParser::query_plan::node::atom;
2109
2110 sub new {
2111     my $pkg = shift;
2112     $pkg = ref($pkg) || $pkg;
2113     my %args = @_;
2114
2115     return bless \%args => $pkg;
2116 }
2117
2118 sub node {
2119     my $self = shift;
2120     return undef unless (ref $self);
2121     return $self->{node};
2122 }
2123
2124 sub content {
2125     my $self = shift;
2126     return undef unless (ref $self);
2127     return $self->{content};
2128 }
2129
2130 sub prefix {
2131     my $self = shift;
2132     return undef unless (ref $self);
2133     return $self->{prefix};
2134 }
2135
2136 sub suffix {
2137     my $self = shift;
2138     return undef unless (ref $self);
2139     return $self->{suffix};
2140 }
2141
2142 sub to_abstract_query {
2143     my ($self) = @_;
2144     
2145     return {
2146         (map { $_ => $self->$_ } qw/prefix suffix content/),
2147         "type" => "atom"
2148     };
2149 }
2150 #-------------------------------
2151 package QueryParser::query_plan::filter;
2152
2153 sub new {
2154     my $pkg = shift;
2155     $pkg = ref($pkg) || $pkg;
2156     my %args = @_;
2157
2158     return bless \%args => $pkg;
2159 }
2160
2161 sub plan {
2162     my $self = shift;
2163     return $self->{plan};
2164 }
2165
2166 sub name {
2167     my $self = shift;
2168     return $self->{name};
2169 }
2170
2171 sub negate {
2172     my $self = shift;
2173     return $self->{negate};
2174 }
2175
2176 sub args {
2177     my $self = shift;
2178     return $self->{args};
2179 }
2180
2181 sub to_abstract_query {
2182     my ($self) = @_;
2183     
2184     return {
2185         map { $_ => $self->$_ } qw/name negate args/
2186     };
2187 }
2188
2189 #-------------------------------
2190 package QueryParser::query_plan::facet;
2191
2192 sub new {
2193     my $pkg = shift;
2194     $pkg = ref($pkg) || $pkg;
2195     my %args = @_;
2196
2197     return bless \%args => $pkg;
2198 }
2199
2200 sub plan {
2201     my $self = shift;
2202     return $self->{plan};
2203 }
2204
2205 sub name {
2206     my $self = shift;
2207     return $self->{name};
2208 }
2209
2210 sub negate {
2211     my $self = shift;
2212     return $self->{negate};
2213 }
2214
2215 sub values {
2216     my $self = shift;
2217     return $self->{'values'};
2218 }
2219
2220 sub to_abstract_query {
2221     my ($self) = @_;
2222
2223     return {
2224         (map { $_ => $self->$_ } qw/name negate values/),
2225         "type" => "facet"
2226     };
2227 }
2228
2229 #-------------------------------
2230 package QueryParser::query_plan::modifier;
2231
2232 sub new {
2233     my $pkg = shift;
2234     $pkg = ref($pkg) || $pkg;
2235     my $modifier = shift;
2236     my $negate = shift;
2237
2238     return bless { name => $modifier, negate => $negate } => $pkg;
2239 }
2240
2241 sub name {
2242     my $self = shift;
2243     return $self->{name};
2244 }
2245
2246 sub negate {
2247     my $self = shift;
2248     return $self->{negate};
2249 }
2250
2251 sub to_abstract_query {
2252     my ($self) = @_;
2253     
2254     return $self->name;
2255 }
2256 1;
2257