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