Bug 13017 - Login page image replacement technique fails when browser width exceeds...
[koha.git] / OpenILS / QueryParser.pm
blobf0051f76de49f636bfd099b9239b6f989be3adc1
1 use strict;
2 use warnings;
4 package OpenILS::QueryParser;
5 use JSON;
7 =head1 NAME
9 OpenILS::QueryParser - basic QueryParser class
11 =head1 SYNOPSIS
13 use OpenILS::QueryParser;
14 my $QParser = OpenILS::QueryParser->new(%args);
16 =head1 DESCRIPTION
18 Main entrypoint into the QueryParser functionality.
20 =head1 FUNCTIONS
22 =cut
24 # Note that the first key must match the name of the package.
25 our %parser_config = (
26 'OpenILS::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 negated => '!'
44 sub canonicalize {
45 my $self = shift;
46 return OpenILS::QueryParser::Canonicalize::abstract_query2str_impl(
47 $self->parse_tree->to_abstract_query(@_)
52 =head2 facet_class_count
54 $count = $QParser->facet_class_count();
55 =cut
57 sub facet_class_count {
58 my $self = shift;
59 return @{$self->facet_classes};
62 =head2 search_class_count
64 $count = $QParser->search_class_count();
65 =cut
67 sub search_class_count {
68 my $self = shift;
69 return @{$self->search_classes};
72 =head2 filter_count
74 $count = $QParser->filter_count();
75 =cut
77 sub filter_count {
78 my $self = shift;
79 return @{$self->filters};
82 =head2 modifier_count
84 $count = $QParser->modifier_count();
85 =cut
87 sub modifier_count {
88 my $self = shift;
89 return @{$self->modifiers};
92 =head2 custom_data
94 $data = $QParser->custom_data($class);
95 =cut
97 sub custom_data {
98 my $class = shift;
99 $class = ref($class) || $class;
101 $parser_config{$class}{custom_data} ||= {};
102 return $parser_config{$class}{custom_data};
105 =head2 operators
107 $operators = $QParser->operators();
109 Returns hashref of the configured operators.
110 =cut
112 sub operators {
113 my $class = shift;
114 $class = ref($class) || $class;
116 $parser_config{$class}{operators} ||= {};
117 return $parser_config{$class}{operators};
120 sub allow_nested_modifiers {
121 my $class = shift;
122 my $v = shift;
123 $class = ref($class) || $class;
125 $parser_config{$class}{allow_nested_modifiers} = $v if (defined $v);
126 return $parser_config{$class}{allow_nested_modifiers};
129 =head2 filters
131 $filters = $QParser->filters();
133 Returns arrayref of the configured filters.
134 =cut
136 sub filters {
137 my $class = shift;
138 $class = ref($class) || $class;
140 $parser_config{$class}{filters} ||= [];
141 return $parser_config{$class}{filters};
144 =head2 filter_callbacks
146 $filter_callbacks = $QParser->filter_callbacks();
148 Returns hashref of the configured filter callbacks.
149 =cut
151 sub filter_callbacks {
152 my $class = shift;
153 $class = ref($class) || $class;
155 $parser_config{$class}{filter_callbacks} ||= {};
156 return $parser_config{$class}{filter_callbacks};
159 =head2 modifiers
161 $modifiers = $QParser->modifiers();
163 Returns arrayref of the configured modifiers.
164 =cut
166 sub modifiers {
167 my $class = shift;
168 $class = ref($class) || $class;
170 $parser_config{$class}{modifiers} ||= [];
171 return $parser_config{$class}{modifiers};
174 =head2 new
176 $QParser = OpenILS::QueryParser->new(%args);
178 Creates a new QueryParser object.
179 =cut
181 sub new {
182 my $class = shift;
183 $class = ref($class) || $class;
185 my %opts = @_;
187 my $self = bless {} => $class;
189 for my $o (keys %{OpenILS::QueryParser->operators}) {
190 $class->operator($o => OpenILS::QueryParser->operator($o)) unless ($class->operator($o));
193 for my $opt ( keys %opts) {
194 $self->$opt( $opts{$opt} ) if ($self->can($opt));
197 return $self;
200 =head2 new_plan
202 $query_plan = $QParser->new_plan();
204 Create a new query plan.
205 =cut
207 sub new_plan {
208 my $self = shift;
209 my $pkg = ref($self) || $self;
210 return do{$pkg.'::query_plan'}->new( QueryParser => $self, @_ );
213 =head2 add_search_filter
215 $QParser->add_search_filter($filter, [$callback]);
217 Adds a filter with the specified name and an optional callback to the
218 QueryParser configuration.
219 =cut
221 sub add_search_filter {
222 my $pkg = shift;
223 $pkg = ref($pkg) || $pkg;
224 my $filter = shift;
225 my $callback = shift;
227 return $filter if (grep { $_ eq $filter } @{$pkg->filters});
228 push @{$pkg->filters}, $filter;
229 $pkg->filter_callbacks->{$filter} = $callback if ($callback);
230 return $filter;
233 =head2 add_search_modifier
235 $QParser->add_search_modifier($modifier);
237 Adds a modifier with the specified name to the QueryParser configuration.
238 =cut
240 sub add_search_modifier {
241 my $pkg = shift;
242 $pkg = ref($pkg) || $pkg;
243 my $modifier = shift;
245 return $modifier if (grep { $_ eq $modifier } @{$pkg->modifiers});
246 push @{$pkg->modifiers}, $modifier;
247 return $modifier;
250 =head2 add_facet_class
252 $QParser->add_facet_class($facet_class);
254 Adds a facet class with the specified name to the QueryParser configuration.
255 =cut
257 sub add_facet_class {
258 my $pkg = shift;
259 $pkg = ref($pkg) || $pkg;
260 my $class = shift;
262 return $class if (grep { $_ eq $class } @{$pkg->facet_classes});
264 push @{$pkg->facet_classes}, $class;
265 $pkg->facet_fields->{$class} = [];
267 return $class;
270 =head2 add_search_class
272 $QParser->add_search_class($class);
274 Adds a search class with the specified name to the QueryParser configuration.
275 =cut
277 sub add_search_class {
278 my $pkg = shift;
279 $pkg = ref($pkg) || $pkg;
280 my $class = shift;
282 return $class if (grep { $_ eq $class } @{$pkg->search_classes});
284 push @{$pkg->search_classes}, $class;
285 $pkg->search_fields->{$class} = [];
286 $pkg->default_search_class( $pkg->search_classes->[0] ) if (@{$pkg->search_classes} == 1);
288 return $class;
291 =head2 add_search_modifier
293 $op = $QParser->operator($operator, [$newvalue]);
295 Retrieves or sets value for the specified operator. Valid operators and
296 their defaults are as follows:
298 =over 4
300 =item * and => &&
302 =item * or => ||
304 =item * group_start => (
306 =item * group_end => )
308 =item * required => +
310 =item * disallowed => -
312 =item * modifier => #
314 =back
316 =cut
318 sub operator {
319 my $class = shift;
320 $class = ref($class) || $class;
321 my $opname = shift;
322 my $op = shift;
324 return unless ($opname);
326 $parser_config{$class}{operators} ||= {};
327 $parser_config{$class}{operators}{$opname} = $op if ($op);
329 return $parser_config{$class}{operators}{$opname};
332 =head2 facet_classes
334 $classes = $QParser->facet_classes([\@newclasses]);
336 Returns arrayref of all configured facet classes after optionally
337 replacing configuration.
338 =cut
340 sub facet_classes {
341 my $class = shift;
342 $class = ref($class) || $class;
343 my $classes = shift;
345 $parser_config{$class}{facet_classes} ||= [];
346 $parser_config{$class}{facet_classes} = $classes if (ref($classes) && @$classes);
347 return $parser_config{$class}{facet_classes};
350 =head2 search_classes
352 $classes = $QParser->search_classes([\@newclasses]);
354 Returns arrayref of all configured search classes after optionally
355 replacing the previous configuration.
356 =cut
358 sub search_classes {
359 my $class = shift;
360 $class = ref($class) || $class;
361 my $classes = shift;
363 $parser_config{$class}{classes} ||= [];
364 $parser_config{$class}{classes} = $classes if (ref($classes) && @$classes);
365 return $parser_config{$class}{classes};
368 =head2 add_query_normalizer
370 $function = $QParser->add_query_normalizer($class, $field, $func, [\@params]);
372 =cut
374 sub add_query_normalizer {
375 my $pkg = shift;
376 $pkg = ref($pkg) || $pkg;
377 my $class = shift;
378 my $field = shift;
379 my $func = shift;
380 my $params = shift || [];
382 # do not add if function AND params are identical to existing member
383 return $func if (grep {
384 $_->{function} eq $func and
385 to_json($_->{params}) eq to_json($params)
386 } @{$pkg->query_normalizers->{$class}->{$field}});
388 push(@{$pkg->query_normalizers->{$class}->{$field}}, { function => $func, params => $params });
390 return $func;
393 =head2 query_normalizers
395 $normalizers = $QParser->query_normalizers($class, $field);
397 Returns a list of normalizers associated with the specified search class
398 and field
399 =cut
401 sub query_normalizers {
402 my $pkg = shift;
403 $pkg = ref($pkg) || $pkg;
405 my $class = shift;
406 my $field = shift;
408 $parser_config{$pkg}{normalizers} ||= {};
409 if ($class) {
410 if ($field) {
411 $parser_config{$pkg}{normalizers}{$class}{$field} ||= [];
412 return $parser_config{$pkg}{normalizers}{$class}{$field};
413 } else {
414 return $parser_config{$pkg}{normalizers}{$class};
418 return $parser_config{$pkg}{normalizers};
421 =head2 add_filter_normalizer
423 $normalizer = $QParser->add_filter_normalizer($filter, $func, [\@params]);
425 Adds a normalizer function to the specified filter.
426 =cut
428 sub add_filter_normalizer {
429 my $pkg = shift;
430 $pkg = ref($pkg) || $pkg;
431 my $filter = shift;
432 my $func = shift;
433 my $params = shift || [];
435 return $func if (grep { $_ eq $func } @{$pkg->filter_normalizers->{$filter}});
437 push(@{$pkg->filter_normalizers->{$filter}}, { function => $func, params => $params });
439 return $func;
442 =head2 filter_normalizers
444 $normalizers = $QParser->filter_normalizers($filter);
446 Return arrayref of normalizer functions associated with the specified filter.
447 =cut
449 sub filter_normalizers {
450 my $pkg = shift;
451 $pkg = ref($pkg) || $pkg;
453 my $filter = shift;
455 $parser_config{$pkg}{filter_normalizers} ||= {};
456 if ($filter) {
457 $parser_config{$pkg}{filter_normalizers}{$filter} ||= [];
458 return $parser_config{$pkg}{filter_normalizers}{$filter};
461 return $parser_config{$pkg}{filter_normalizers};
464 =head2 default_search_class
466 $default_class = $QParser->default_search_class([$class]);
468 Set or return the default search class.
469 =cut
471 sub default_search_class {
472 my $pkg = shift;
473 $pkg = ref($pkg) || $pkg;
474 my $class = shift;
475 $OpenILS::QueryParser::parser_config{$pkg}{default_class} = $pkg->add_search_class( $class ) if $class;
477 return $OpenILS::QueryParser::parser_config{$pkg}{default_class};
480 =head2 remove_facet_class
482 $QParser->remove_facet_class($class);
484 Remove the specified facet class from the configuration.
485 =cut
487 sub remove_facet_class {
488 my $pkg = shift;
489 $pkg = ref($pkg) || $pkg;
490 my $class = shift;
492 return $class if (!grep { $_ eq $class } @{$pkg->facet_classes});
494 $pkg->facet_classes( [ grep { $_ ne $class } @{$pkg->facet_classes} ] );
495 delete $OpenILS::QueryParser::parser_config{$pkg}{facet_fields}{$class};
497 return $class;
500 =head2 remove_search_class
502 $QParser->remove_search_class($class);
504 Remove the specified search class from the configuration.
505 =cut
507 sub remove_search_class {
508 my $pkg = shift;
509 $pkg = ref($pkg) || $pkg;
510 my $class = shift;
512 return $class if (!grep { $_ eq $class } @{$pkg->search_classes});
514 $pkg->search_classes( [ grep { $_ ne $class } @{$pkg->search_classes} ] );
515 delete $OpenILS::QueryParser::parser_config{$pkg}{fields}{$class};
517 return $class;
520 =head2 add_facet_field
522 $QParser->add_facet_field($class, $field);
524 Adds the specified field (and facet class if it doesn't already exist)
525 to the configuration.
526 =cut
528 sub add_facet_field {
529 my $pkg = shift;
530 $pkg = ref($pkg) || $pkg;
531 my $class = shift;
532 my $field = shift;
534 $pkg->add_facet_class( $class );
536 return { $class => $field } if (grep { $_ eq $field } @{$pkg->facet_fields->{$class}});
538 push @{$pkg->facet_fields->{$class}}, $field;
540 return { $class => $field };
543 =head2 facet_fields
545 $fields = $QParser->facet_fields($class);
547 Returns arrayref with list of fields for specified facet class.
548 =cut
550 sub facet_fields {
551 my $class = shift;
552 $class = ref($class) || $class;
554 $parser_config{$class}{facet_fields} ||= {};
555 return $parser_config{$class}{facet_fields};
558 =head2 add_search_field
560 $QParser->add_search_field($class, $field);
562 Adds the specified field (and facet class if it doesn't already exist)
563 to the configuration.
564 =cut
566 sub add_search_field {
567 my $pkg = shift;
568 $pkg = ref($pkg) || $pkg;
569 my $class = shift;
570 my $field = shift;
572 $pkg->add_search_class( $class );
574 return { $class => $field } if (grep { $_ eq $field } @{$pkg->search_fields->{$class}});
576 push @{$pkg->search_fields->{$class}}, $field;
578 return { $class => $field };
581 =head2 search_fields
583 $fields = $QParser->search_fields();
585 Returns arrayref with list of configured search fields.
586 =cut
588 sub search_fields {
589 my $class = shift;
590 $class = ref($class) || $class;
592 $parser_config{$class}{fields} ||= {};
593 return $parser_config{$class}{fields};
596 =head2 add_search_class_alias
598 $QParser->add_search_class_alias($class, $alias);
599 =cut
601 sub add_search_class_alias {
602 my $pkg = shift;
603 $pkg = ref($pkg) || $pkg;
604 my $class = shift;
605 my $alias = shift;
607 $pkg->add_search_class( $class );
609 return { $class => $alias } if (grep { $_ eq $alias } @{$pkg->search_class_aliases->{$class}});
611 push @{$pkg->search_class_aliases->{$class}}, $alias;
613 return { $class => $alias };
616 =head2 search_class_aliases
618 $aliases = $QParser->search_class_aliases($class);
619 =cut
621 sub search_class_aliases {
622 my $class = shift;
623 $class = ref($class) || $class;
625 $parser_config{$class}{class_map} ||= {};
626 return $parser_config{$class}{class_map};
629 =head2 add_search_field_alias
631 $QParser->add_search_field_alias($class, $field, $alias);
632 =cut
634 sub add_search_field_alias {
635 my $pkg = shift;
636 $pkg = ref($pkg) || $pkg;
637 my $class = shift;
638 my $field = shift;
639 my $alias = shift;
641 return { $class => { $field => $alias } } if (grep { $_ eq $alias } @{$pkg->search_field_aliases->{$class}{$field}});
643 push @{$pkg->search_field_aliases->{$class}{$field}}, $alias;
645 return { $class => { $field => $alias } };
648 =head2 search_field_aliases
650 $aliases = $QParser->search_field_aliases();
651 =cut
653 sub search_field_aliases {
654 my $class = shift;
655 $class = ref($class) || $class;
657 $parser_config{$class}{field_alias_map} ||= {};
658 return $parser_config{$class}{field_alias_map};
661 =head2 remove_facet_field
663 $QParser->remove_facet_field($class, $field);
664 =cut
666 sub remove_facet_field {
667 my $pkg = shift;
668 $pkg = ref($pkg) || $pkg;
669 my $class = shift;
670 my $field = shift;
672 return { $class => $field } if (!$pkg->facet_fields->{$class} || !grep { $_ eq $field } @{$pkg->facet_fields->{$class}});
674 $pkg->facet_fields->{$class} = [ grep { $_ ne $field } @{$pkg->facet_fields->{$class}} ];
676 return { $class => $field };
679 =head2 remove_search_field
681 $QParser->remove_search_field($class, $field);
682 =cut
684 sub remove_search_field {
685 my $pkg = shift;
686 $pkg = ref($pkg) || $pkg;
687 my $class = shift;
688 my $field = shift;
690 return { $class => $field } if (!$pkg->search_fields->{$class} || !grep { $_ eq $field } @{$pkg->search_fields->{$class}});
692 $pkg->search_fields->{$class} = [ grep { $_ ne $field } @{$pkg->search_fields->{$class}} ];
694 return { $class => $field };
697 =head2 remove_search_field_alias
699 $QParser->remove_search_field_alias($class, $field, $alias);
700 =cut
702 sub remove_search_field_alias {
703 my $pkg = shift;
704 $pkg = ref($pkg) || $pkg;
705 my $class = shift;
706 my $field = shift;
707 my $alias = shift;
709 return { $class => { $field => $alias } } if (!$pkg->search_field_aliases->{$class}{$field} || !grep { $_ eq $alias } @{$pkg->search_field_aliases->{$class}{$field}});
711 $pkg->search_field_aliases->{$class}{$field} = [ grep { $_ ne $alias } @{$pkg->search_field_aliases->{$class}{$field}} ];
713 return { $class => { $field => $alias } };
716 =head2 remove_search_class_alias
718 $QParser->remove_search_class_alias($class, $alias);
719 =cut
721 sub remove_search_class_alias {
722 my $pkg = shift;
723 $pkg = ref($pkg) || $pkg;
724 my $class = shift;
725 my $alias = shift;
727 return { $class => $alias } if (!$pkg->search_class_aliases->{$class} || !grep { $_ eq $alias } @{$pkg->search_class_aliases->{$class}});
729 $pkg->search_class_aliases->{$class} = [ grep { $_ ne $alias } @{$pkg->search_class_aliases->{$class}} ];
731 return { $class => $alias };
734 =head2 debug
736 $debug = $QParser->debug([$debug]);
738 Return or set whether debugging output is enabled.
739 =cut
741 sub debug {
742 my $self = shift;
743 my $q = shift;
744 $self->{_debug} = $q if (defined $q);
745 return $self->{_debug};
748 =head2 query
750 $query = $QParser->query([$query]);
752 Return or set the query.
753 =cut
755 sub query {
756 my $self = shift;
757 my $q = shift;
758 $self->{_query} = " $q " if (defined $q);
759 return $self->{_query};
762 =head2 parse_tree
764 $parse_tree = $QParser->parse_tree([$parse_tree]);
766 Return or set the parse tree associated with the QueryParser.
767 =cut
769 sub parse_tree {
770 my $self = shift;
771 my $q = shift;
772 $self->{_parse_tree} = $q if (defined $q);
773 return $self->{_parse_tree};
776 sub floating_plan {
777 my $self = shift;
778 my $q = shift;
779 $self->{_top} = $q if (defined $q);
780 return $self->{_top};
783 =head2 parse
785 $QParser->parse([$query]);
787 Parse the specified query, or the query already associated with the QueryParser
788 object.
789 =cut
791 sub parse {
792 my $self = shift;
793 my $pkg = ref($self) || $self;
794 warn " ** parse package is $pkg\n" if $self->debug;
795 # $self->parse_tree(
796 # $self->decompose(
797 # $self->query( shift() )
799 # );
801 undef $self->{_parse_tree};
803 $self->decompose( $self->query( shift() ) );
805 if ($self->floating_plan) {
806 $self->floating_plan->add_node( $self->parse_tree );
807 $self->parse_tree( $self->floating_plan );
810 $self->parse_tree->plan_level(0);
812 return $self;
815 =head2 decompose
817 ($struct, $remainder) = $QParser->decompose($querystring, [$current_class], [$recursing], [$phrase_helper]);
819 This routine does the heavy work of parsing the query string recursively.
820 Returns the top level query plan, or the query plan from a lower level plus
821 the portion of the query string that needs to be processed at a higher level.
822 =cut
824 our $last_class = '';
825 our $last_type = '';
826 our $floating = 0;
827 our $fstart;
829 sub decompose {
830 my $self = shift;
831 my $pkg = ref($self) || $self;
834 $_ = shift;
835 my $current_class = shift || $self->default_search_class;
837 my $recursing = shift || 0;
838 my $phrase_helper = shift || 0;
840 # Build the search class+field uber-regexp
841 my $search_class_re = '^\s*(';
842 my $first_class = 1;
844 warn ' 'x$recursing." ** decompose package is $pkg\n" if $self->debug;
846 my %seen_classes;
847 for my $class ( keys %{$pkg->search_field_aliases} ) {
848 warn ' 'x$recursing." *** ... Looking for search fields in $class\n" if $self->debug;
850 for my $field ( keys %{$pkg->search_field_aliases->{$class}} ) {
851 warn ' 'x$recursing." *** ... Looking for aliases of $field\n" if $self->debug;
853 for my $alias ( @{$pkg->search_field_aliases->{$class}{$field}} ) {
854 next unless ($alias);
855 my $aliasr = qr/$alias/;
856 s/(^|\s+)$aliasr\|/$1$class\|$field#$alias\|/g;
857 s/(^|\s+)$aliasr[:=]/$1$class\|$field#$alias:/g;
858 warn ' 'x$recursing." *** Rewriting: $alias ($aliasr) as $class\|$field\n" if $self->debug;
862 $search_class_re .= '|' unless ($first_class);
863 $first_class = 0;
864 $search_class_re .= $class . '(?:[|#][^:|]+)*';
865 $seen_classes{$class} = 1;
868 for my $class ( keys %{$pkg->search_class_aliases} ) {
870 for my $alias ( @{$pkg->search_class_aliases->{$class}} ) {
871 next unless ($alias);
872 my $aliasr = qr/$alias/;
873 s/(^|[^|])\b$aliasr\|/$1$class#$alias\|/g;
874 s/(^|[^|])\b$aliasr[:=]/$1$class#$alias:/g;
875 warn ' 'x$recursing." *** Rewriting: $alias ($aliasr) as $class\n" if $self->debug;
878 if (!$seen_classes{$class}) {
879 $search_class_re .= '|' unless ($first_class);
880 $first_class = 0;
882 $search_class_re .= $class . '(?:[|#][^:|]+)*';
883 $seen_classes{$class} = 1;
886 $search_class_re .= '):';
888 warn ' 'x$recursing." ** Rewritten query: $_\n" if $self->debug;
889 warn ' 'x$recursing." ** Search class RE: $search_class_re\n" if $self->debug;
891 my $required_op = $pkg->operator('required');
892 my $required_re = qr/\Q$required_op\E/;
894 my $disallowed_op = $pkg->operator('disallowed');
895 my $disallowed_re = qr/\Q$disallowed_op\E/;
897 my $negated_op = $pkg->operator('negated');
898 my $negated_re = qr/\Q$negated_op\E/;
900 my $and_op = $pkg->operator('and');
901 my $and_re = qr/^\s*\Q$and_op\E/;
903 my $or_op = $pkg->operator('or');
904 my $or_re = qr/^\s*\Q$or_op\E/;
906 my $group_start = $pkg->operator('group_start');
907 my $group_start_re = qr/^\s*($negated_re|$disallowed_re)?\Q$group_start\E/;
909 my $group_end = $pkg->operator('group_end');
910 my $group_end_re = qr/^\s*\Q$group_end\E/;
912 my $float_start = $pkg->operator('float_start');
913 my $float_start_re = qr/^\s*\Q$float_start\E/;
915 my $float_end = $pkg->operator('float_end');
916 my $float_end_re = qr/^\s*\Q$float_end\E/;
918 my $modifier_tag = $pkg->operator('modifier');
919 my $modifier_tag_re = qr/^\s*\Q$modifier_tag\E/;
921 # Group start/end normally are ( and ), but can be overridden.
922 # We thus include ( and ) specifically due to filters, as well as : for classes.
923 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|\Q$negated_op\E|:|\(|\))/;
925 # Build the filter and modifier uber-regexps
926 my $facet_re = '^\s*(-?)((?:' . join( '|', @{$pkg->facet_classes}) . ')(?:\|\w+)*)\[(.+?)\]';
927 warn ' 'x$recursing." ** Facet RE: $facet_re\n" if $self->debug;
929 my $filter_re = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . ')\(([^()]+)\)';
930 my $filter_as_class_re = '^\s*(-?)(' . join( '|', @{$pkg->filters}) . '):\s*(\S+)';
932 my $modifier_re = '^\s*'.$modifier_tag_re.'(' . join( '|', @{$pkg->modifiers}) . ')\b';
933 my $modifier_as_class_re = '^\s*(' . join( '|', @{$pkg->modifiers}) . '):\s*(\S+)';
935 my $struct = shift || $self->new_plan( level => $recursing );
936 $self->parse_tree( $struct ) if (!$self->parse_tree);
938 my $remainder = '';
940 while (!$remainder) {
941 warn ' 'x$recursing."Start of the loop. last_type: $last_type, joiner: ".$struct->joiner.", struct: $struct\n" if $self->debug;
942 if ($last_type eq 'FEND' and $fstart and $fstart != $struct) { # fall back further
943 $remainder = $_;
944 last;
945 } elsif ($last_type eq 'FEND') {
946 $fstart = undef;
947 $last_type = '';
950 if (/^\s*$/) { # end of an explicit group
951 local $last_type = '';
952 last;
953 } elsif (/$float_end_re/) { # end of an explicit group
954 warn ' 'x$recursing."Encountered explicit float end, remainder: $'\n" if $self->debug;
956 $remainder = $';
957 $_ = '';
959 $floating = 0;
960 $last_type = 'FEND';
961 last;
962 } elsif (/$group_end_re/) { # end of an explicit group
963 warn ' 'x$recursing."Encountered explicit group end, remainder: $'\n" if $self->debug;
965 $remainder = $';
966 $_ = '';
968 local $last_type = '';
969 } elsif ($self->filter_count && /$filter_re/) { # found a filter
970 warn ' 'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
972 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
973 $_ = $';
975 my $filter = $2;
976 my $params = [ split '[,]+', $3 ];
978 if ($pkg->filter_callbacks->{$filter}) {
979 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
980 $_ = "$replacement $_" if ($replacement);
981 } else {
982 $struct->new_filter( $filter => $params, $negate );
986 local $last_type = '';
987 } elsif ($self->filter_count && /$filter_as_class_re/) { # found a filter
988 warn ' 'x$recursing."Encountered search filter: $1$2 set to $3\n" if $self->debug;
990 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
991 $_ = $';
993 my $filter = $2;
994 my $params = [ split '[,]+', $3 ];
996 if ($pkg->filter_callbacks->{$filter}) {
997 my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate);
998 $_ = "$replacement $_" if ($replacement);
999 } else {
1000 $struct->new_filter( $filter => $params, $negate );
1003 local $last_type = '';
1004 } elsif ($self->modifier_count && /$modifier_re/) { # found a modifier
1005 warn ' 'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1007 $_ = $';
1008 if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1009 warn ' 'x$recursing." Search modifiers only allowed at the top level of the query\n" if $self->debug;
1010 } else {
1011 $struct->new_modifier($1);
1014 local $last_type = '';
1015 } elsif ($self->modifier_count && /$modifier_as_class_re/) { # found a modifier
1016 warn ' 'x$recursing."Encountered search modifier: $1\n" if $self->debug;
1018 my $mod = $1;
1020 $_ = $';
1021 if (!($struct->top_plan || $parser_config{$pkg}->{allow_nested_modifiers})) {
1022 warn ' 'x$recursing." Search modifiers only allowed at the top level of the query\n" if $self->debug;
1023 } elsif ($2 =~ /^[ty1]/i) {
1024 $struct->new_modifier($mod);
1027 local $last_type = '';
1028 } elsif (/$float_start_re/) { # start of an explicit float
1029 warn ' 'x$recursing."Encountered explicit float start\n" if $self->debug;
1030 $floating = 1;
1031 $fstart = $struct;
1033 $last_class = $current_class;
1034 $current_class = undef;
1036 $self->floating_plan( $self->new_plan( floating => 1 ) ) if (!$self->floating_plan);
1038 # pass the floating_plan struct to be modified by the float'ed chunk
1039 my ($floating_plan, $subremainder) = $self->new( debug => $self->debug )->decompose( $', undef, undef, undef, $self->floating_plan);
1040 $_ = $subremainder;
1041 warn ' 'x$recursing."Remainder after explicit float: $_\n" if $self->debug;
1043 $current_class = $last_class;
1045 $last_type = '';
1046 } elsif (/$group_start_re/) { # start of an explicit group
1047 warn ' 'x$recursing."Encountered explicit group start\n" if $self->debug;
1048 my $negate = $1;
1049 my ($substruct, $subremainder) = $self->decompose( $', $current_class, $recursing + 1 );
1050 $substruct->negate(1) if ($substruct && $negate);
1051 $struct->add_node( $substruct ) if ($substruct);
1052 $_ = $subremainder;
1053 warn ' 'x$recursing."Query remainder after bool group: $_\n" if $self->debug;
1055 local $last_type = '';
1057 } elsif (/$and_re/) { # ANDed expression
1058 $_ = $';
1059 warn ' 'x$recursing."Encountered AND\n" if $self->debug;
1060 do {warn ' 'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1061 do {warn ' 'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1062 local $last_type = 'AND';
1064 warn ' 'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1065 my $LHS = $struct;
1066 #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1067 my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 1 );
1068 $_ = $subremainder;
1070 warn ' 'x$recursing."RHS built\n" if $self->debug;
1071 warn ' 'x$recursing."Post-AND remainder: $subremainder\n" if $self->debug;
1073 my $wrapper = $self->new_plan( level => $recursing + 1 );
1075 if ($LHS->floating) {
1076 $wrapper->{query} = $LHS->{query};
1077 my $outer_wrapper = $self->new_plan( level => $recursing + 1 );
1078 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1079 $LHS->{query} = [$outer_wrapper];
1080 $struct = $LHS;
1081 } else {
1082 $wrapper->add_node($_) for ($LHS, $RHS);
1083 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1084 $struct = $self->new_plan( level => $recursing );
1085 $struct->add_node($wrapper);
1088 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1090 local $last_type = '';
1091 } elsif (/$or_re/) { # ORed expression
1092 $_ = $';
1093 warn ' 'x$recursing."Encountered OR\n" if $self->debug;
1094 do {warn ' 'x$recursing."!!! Already doing the bool dance for AND\n" if $self->debug; next} if ($last_type eq 'AND');
1095 do {warn ' 'x$recursing."!!! Already doing the bool dance for OR\n" if $self->debug; next} if ($last_type eq 'OR');
1096 local $last_type = 'OR';
1098 warn ' 'x$recursing."Saving LHS, building RHS\n" if $self->debug;
1099 my $LHS = $struct;
1100 #my ($RHS, $subremainder) = $self->decompose( "$group_start $_ $group_end", $current_class, $recursing + 1 );
1101 my ($RHS, $subremainder) = $self->decompose( $_, $current_class, $recursing + 2 );
1102 $_ = $subremainder;
1104 warn ' 'x$recursing."RHS built\n" if $self->debug;
1105 warn ' 'x$recursing."Post-OR remainder: $subremainder\n" if $self->debug;
1107 my $wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1109 if ($LHS->floating) {
1110 $wrapper->{query} = $LHS->{query};
1111 my $outer_wrapper = $self->new_plan( level => $recursing + 1, joiner => '|' );
1112 $outer_wrapper->add_node($_) for ($wrapper,$RHS);
1113 $LHS->{query} = [$outer_wrapper];
1114 $struct = $LHS;
1115 } else {
1116 $wrapper->add_node($_) for ($LHS, $RHS);
1117 $wrapper->plan_level($wrapper->plan_level); # reset levels all the way down
1118 $struct = $self->new_plan( level => $recursing );
1119 $struct->add_node($wrapper);
1122 $self->parse_tree( $struct ) if ($self->parse_tree == $LHS);
1124 local $last_type = '';
1125 } elsif ($self->facet_class_count && /$facet_re/) { # changing current class
1126 warn ' 'x$recursing."Encountered facet: $1$2 => $3\n" if $self->debug;
1128 my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0;
1129 my $facet = $2;
1130 my $facet_value = [ split '\s*#\s*', $3 ];
1131 $struct->new_facet( $facet => $facet_value, $negate );
1132 $_ = $';
1134 local $last_type = '';
1135 } elsif ($self->search_class_count && /$search_class_re/) { # changing current class
1137 if ($last_type eq 'CLASS') {
1138 $struct->remove_last_node( $current_class );
1139 warn ' 'x$recursing."Encountered class change with no searches!\n" if $self->debug;
1142 warn ' 'x$recursing."Encountered class change: $1\n" if $self->debug;
1144 $current_class = $struct->classed_node( $1 )->requested_class();
1145 $_ = $';
1147 local $last_type = 'CLASS';
1148 } elsif (/^\s*($required_re|$disallowed_re|$negated_re)?"([^"]+)"/) { # phrase, always anded
1149 warn ' 'x$recursing.'Encountered' . ($1 ? " ['$1' modified]" : '') . " phrase: $2\n" if $self->debug;
1151 my $req_ness = $1 || '';
1152 $req_ness = $disallowed_op if ($req_ness eq $negated_op);
1153 my $phrase = $2;
1155 if (!$phrase_helper) {
1156 warn ' 'x$recursing."Recursing into decompose with the phrase as a subquery\n" if $self->debug;
1157 my $after = $';
1158 my ($substruct, $subremainder) = $self->decompose( qq/$req_ness"$phrase"/, $current_class, $recursing + 1, 1 );
1159 $struct->add_node( $substruct ) if ($substruct);
1160 $_ = $after;
1161 } else {
1162 warn ' 'x$recursing."Directly parsing the phrase subquery\n" if $self->debug;
1163 $struct->joiner( '&' );
1165 my $class_node = $struct->classed_node($current_class);
1167 if ($req_ness eq $disallowed_op) {
1168 $class_node->negate(1);
1170 $class_node->add_phrase( $phrase );
1172 # Save $' before we clean up $phrase
1173 my $temp_val = $';
1175 # Cleanup the phrase to make it so that we don't parse things in it as anything other than atoms
1176 $phrase =~ s/$phrase_cleanup_re/ /g;
1178 $_ = $temp_val;
1182 local $last_type = '';
1184 } elsif (/^\s*($required_re|$disallowed_re)([^${group_end}${float_end}\s"]+)/) { # convert require/disallow word to {un}phrase
1185 warn ' 'x$recursing."Encountered required atom (mini phrase), transforming for phrase parse: $1\n" if $self->debug;
1187 $_ = $1 . '"' . $2 . '"' . $';
1189 local $last_type = '';
1190 } elsif (/^\s*([^${group_end}${float_end}\s]+)/o) { # atom
1191 warn ' 'x$recursing."Encountered atom: $1\n" if $self->debug;
1192 warn ' 'x$recursing."Remainder: $'\n" if $self->debug;
1194 my $atom = $1;
1195 my $after = $';
1197 $_ = $after;
1198 local $last_type = '';
1200 my $class_node = $struct->classed_node($current_class);
1202 my $prefix = ($atom =~ s/^$negated_re//o) ? '!' : '';
1203 my $truncate = ($atom =~ s/\*$//o) ? '*' : '';
1205 if ($atom ne '' and !grep { $atom =~ /^\Q$_\E+$/ } ('&','|')) { # throw away & and |, not allowed in tsquery, and not really useful anyway
1206 # $class_node->add_phrase( $atom ) if ($atom =~ s/^$required_re//o);
1208 $class_node->add_fts_atom( $atom, suffix => $truncate, prefix => $prefix, node => $class_node );
1209 $struct->joiner( '&' );
1212 local $last_type = '';
1215 last unless ($_);
1219 $struct = undef if
1220 scalar(@{$struct->query_nodes}) == 0 &&
1221 scalar(@{$struct->filters}) == 0 &&
1222 !$struct->top_plan;
1224 return $struct if !wantarray;
1225 return ($struct, $remainder);
1228 =head2 find_class_index
1230 $index = $QParser->find_class_index($class, $query);
1231 =cut
1233 sub find_class_index {
1234 my $class = shift;
1235 my $query = shift;
1237 my ($class_part, @field_parts) = split '\|', $class;
1238 $class_part ||= $class;
1240 for my $idx ( 0 .. scalar(@$query) - 1 ) {
1241 next unless ref($$query[$idx]);
1242 return $idx if ( $$query[$idx]{requested_class} && $class eq $$query[$idx]{requested_class} );
1245 push(@$query, { classname => $class_part, (@field_parts ? (fields => \@field_parts) : ()), requested_class => $class, ftsquery => [], phrases => [] });
1246 return -1;
1249 =head2 core_limit
1251 $limit = $QParser->core_limit([$limit]);
1253 Return and/or set the core_limit.
1254 =cut
1256 sub core_limit {
1257 my $self = shift;
1258 my $l = shift;
1259 $self->{core_limit} = $l if ($l);
1260 return $self->{core_limit};
1263 =head2 superpage
1265 $superpage = $QParser->superpage([$superpage]);
1267 Return and/or set the superpage.
1268 =cut
1270 sub superpage {
1271 my $self = shift;
1272 my $l = shift;
1273 $self->{superpage} = $l if ($l);
1274 return $self->{superpage};
1277 =head2 superpage_size
1279 $size = $QParser->superpage_size([$size]);
1281 Return and/or set the superpage size.
1282 =cut
1284 sub superpage_size {
1285 my $self = shift;
1286 my $l = shift;
1287 $self->{superpage_size} = $l if ($l);
1288 return $self->{superpage_size};
1292 #-------------------------------
1293 package OpenILS::QueryParser::_util;
1295 # At this level, joiners are always & or |. This is not
1296 # the external, configurable representation of joiners that
1297 # defaults to # && and ||.
1298 sub is_joiner {
1299 my $str = shift;
1301 return (not ref $str and ($str eq '&' or $str eq '|'));
1304 sub default_joiner { '&' }
1306 # 0 for different, 1 for the same.
1307 sub compare_abstract_atoms {
1308 my ($left, $right) = @_;
1310 foreach (qw/prefix suffix content/) {
1311 no warnings; # undef can stand in for '' here
1312 return 0 unless $left->{$_} eq $right->{$_};
1315 return 1;
1318 sub fake_abstract_atom_from_phrase {
1319 my $phrase = shift;
1320 my $neg = shift;
1321 my $qp_class = shift || 'OpenILS::QueryParser';
1323 my $prefix = '"';
1324 if ($neg) {
1325 $prefix =
1326 $OpenILS::QueryParser::parser_config{$qp_class}{operators}{disallowed} .
1327 $prefix;
1330 return {
1331 "type" => "atom", "prefix" => $prefix, "suffix" => '"',
1332 "content" => $phrase
1336 sub find_arrays_in_abstract {
1337 my ($hash) = @_;
1339 my @arrays;
1340 foreach my $key (keys %$hash) {
1341 if (ref $hash->{$key} eq "ARRAY") {
1342 push @arrays, $hash->{$key};
1343 foreach (@{$hash->{$key}}) {
1344 push @arrays, find_arrays_in_abstract($_);
1349 return @arrays;
1352 #-------------------------------
1353 package OpenILS::QueryParser::Canonicalize; # not OO
1354 use Data::Dumper;
1356 sub _abstract_query2str_filter {
1357 my $f = shift;
1358 my $qp_class = shift || 'OpenILS::QueryParser';
1359 my $qpconfig = $OpenILS::QueryParser::parser_config{$qp_class};
1361 return sprintf(
1362 '%s%s(%s)',
1363 $f->{negate} ? $qpconfig->{operators}{disallowed} : "",
1364 $f->{name},
1365 join(",", @{$f->{args}})
1369 sub _abstract_query2str_modifier {
1370 my $f = shift;
1371 my $qp_class = shift || 'OpenILS::QueryParser';
1372 my $qpconfig = $OpenILS::QueryParser::parser_config{$qp_class};
1374 return $qpconfig->{operators}{modifier} . $f;
1377 sub _kid_list {
1378 my $children = shift;
1379 my $op = (keys %$children)[0];
1380 return @{$$children{$op}};
1384 # This should produce an equivalent query to the original, given an
1385 # abstract_query.
1386 sub abstract_query2str_impl {
1387 my $abstract_query = shift;
1388 my $depth = shift || 0;
1390 my $qp_class ||= shift || 'OpenILS::QueryParser';
1391 my $force_qp_node = shift || 0;
1392 my $qpconfig = $OpenILS::QueryParser::parser_config{$qp_class};
1394 my $fs = $qpconfig->{operators}{float_start};
1395 my $fe = $qpconfig->{operators}{float_end};
1396 my $gs = $qpconfig->{operators}{group_start};
1397 my $ge = $qpconfig->{operators}{group_end};
1398 my $and = $qpconfig->{operators}{and};
1399 my $or = $qpconfig->{operators}{or};
1400 my $ng = $qpconfig->{operators}{negated};
1402 my $isnode = 0;
1403 my $negate = '';
1404 my $size = 0;
1405 my $q = "";
1407 if (exists $abstract_query->{type}) {
1408 if ($abstract_query->{type} eq 'query_plan') {
1409 $q .= join(" ", map { _abstract_query2str_filter($_, $qp_class) } @{$abstract_query->{filters}}) if
1410 exists $abstract_query->{filters};
1412 $q .= ($q ? ' ' : '') . join(" ", map { _abstract_query2str_modifier($_, $qp_class) } @{$abstract_query->{modifiers}}) if
1413 exists $abstract_query->{modifiers};
1415 $size = _kid_list($abstract_query->{children});
1416 if ($abstract_query->{negate}) {
1417 $isnode = 1;
1418 $negate = $ng;
1420 $isnode = 1 if ($size > 1 and ($force_qp_node or $depth));
1421 #warn "size: $size, depth: $depth, isnode: $isnode, AQ: ".Dumper($abstract_query);
1422 } elsif ($abstract_query->{type} eq 'node') {
1423 if ($abstract_query->{alias}) {
1424 $q .= ($q ? ' ' : '') . $abstract_query->{alias};
1425 $q .= "|$_" foreach @{$abstract_query->{alias_fields}};
1426 } else {
1427 $q .= ($q ? ' ' : '') . $abstract_query->{class};
1428 $q .= "|$_" foreach @{$abstract_query->{fields}};
1430 $q .= ":";
1431 $isnode = 1;
1432 } elsif ($abstract_query->{type} eq 'atom') {
1433 my $prefix = $abstract_query->{prefix} || '';
1434 $prefix = $qpconfig->{operators}{negated} if $prefix eq '!';
1435 $q .= ($q ? ' ' : '') . $prefix .
1436 ($abstract_query->{content} || '') .
1437 ($abstract_query->{suffix} || '');
1438 } elsif ($abstract_query->{type} eq 'facet') {
1439 # facet syntax [ # ] is hardcoded I guess?
1440 my $prefix = $abstract_query->{negate} ? $qpconfig->{operators}{disallowed} : '';
1441 $q .= ($q ? ' ' : '') . $prefix . $abstract_query->{name} . "[" .
1442 join(" # ", @{$abstract_query->{values}}) . "]";
1446 my $next_depth = int($size > 1);
1448 if (exists $abstract_query->{children}) {
1450 my $op = (keys(%{$abstract_query->{children}}))[0];
1452 if ($abstract_query->{floating}) { # always the top node!
1453 my $sub_node = pop @{$abstract_query->{children}{$op}};
1455 $abstract_query->{floating} = 0;
1456 $q = $fs . " " . abstract_query2str_impl($abstract_query,0,$qp_class, 1) . $fe. " ";
1458 $abstract_query = $sub_node;
1461 if ($abstract_query && exists $abstract_query->{children}) {
1462 $op = (keys(%{$abstract_query->{children}}))[0];
1463 $q .= ($q ? ' ' : '') . join(
1464 ($op eq '&' ? ' ' : " $or "),
1465 map {
1466 my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1467 } @{$abstract_query->{children}{$op}}
1470 } elsif ($abstract_query->{'&'} or $abstract_query->{'|'}) {
1471 my $op = (keys(%{$abstract_query}))[0];
1472 $q .= ($q ? ' ' : '') . join(
1473 ($op eq '&' ? ' ' : " $or "),
1474 map {
1475 my $x = abstract_query2str_impl($_, $depth + $next_depth, $qp_class, $force_qp_node); $x =~ s/^\s+//; $x =~ s/\s+$//; $x;
1476 } @{$abstract_query->{$op}}
1480 $q = "$gs$q$ge" if ($isnode);
1481 $q = $negate . $q if ($q);;
1483 return $q;
1486 #-------------------------------
1487 package OpenILS::QueryParser::query_plan;
1489 sub QueryParser {
1490 my $self = shift;
1491 return unless ref($self);
1492 return $self->{QueryParser};
1495 sub new {
1496 my $pkg = shift;
1497 $pkg = ref($pkg) || $pkg;
1498 my %args = (query => [], joiner => '&', @_);
1500 return bless \%args => $pkg;
1503 sub new_node {
1504 my $self = shift;
1505 my $pkg = ref($self) || $self;
1506 my $node = do{$pkg.'::node'}->new( plan => $self, @_ );
1507 $self->add_node( $node );
1508 return $node;
1511 sub new_facet {
1512 my $self = shift;
1513 my $pkg = ref($self) || $self;
1514 my $name = shift;
1515 my $args = shift;
1516 my $negate = shift;
1518 my $node = do{$pkg.'::facet'}->new( plan => $self, name => $name, 'values' => $args, negate => $negate );
1519 $self->add_node( $node );
1521 return $node;
1524 sub new_filter {
1525 my $self = shift;
1526 my $pkg = ref($self) || $self;
1527 my $name = shift;
1528 my $args = shift;
1529 my $negate = shift;
1531 my $node = do{$pkg.'::filter'}->new( plan => $self, name => $name, args => $args, negate => $negate );
1532 $self->add_filter( $node );
1534 return $node;
1538 sub _merge_filters {
1539 my $left_filter = shift;
1540 my $right_filter = shift;
1541 my $join = shift;
1543 return unless $left_filter or $right_filter;
1544 return $right_filter unless $left_filter;
1545 return $left_filter unless $right_filter;
1547 my $args = $left_filter->{args} || [];
1549 if ($join eq '|') {
1550 push(@$args, @{$right_filter->{args}});
1552 } else {
1553 # find the intersect values
1554 my %new_vals;
1555 map { $new_vals{$_} = 1 } @{$right_filter->{args} || []};
1556 $args = [ grep { $new_vals{$_} } @$args ];
1559 $left_filter->{args} = $args;
1560 return $left_filter;
1563 sub collapse_filters {
1564 my $self = shift;
1565 my $name = shift;
1567 # start by merging any filters at this level.
1568 # like-level filters are always ORed together
1570 my $cur_filter;
1571 my @cur_filters = grep {$_->name eq $name } @{ $self->filters };
1572 if (@cur_filters) {
1573 $cur_filter = shift @cur_filters;
1574 my $args = $cur_filter->{args} || [];
1575 $cur_filter = _merge_filters($cur_filter, $_, '|') for @cur_filters;
1578 # next gather the collapsed filters from sub-plans and
1579 # merge them with our own
1581 my @subquery = @{$self->{query}};
1583 while (@subquery) {
1584 my $blob = shift @subquery;
1585 shift @subquery; # joiner
1586 next unless $blob->isa('OpenILS::QueryParser::query_plan');
1587 my $sub_filter = $blob->collapse_filters($name);
1588 $cur_filter = _merge_filters($cur_filter, $sub_filter, $self->joiner);
1591 if ($self->QueryParser->debug) {
1592 my @args = ($cur_filter and $cur_filter->{args}) ? @{$cur_filter->{args}} : ();
1593 warn "collapse_filters($name) => [@args]\n";
1596 return $cur_filter;
1599 sub find_filter {
1600 my $self = shift;
1601 my $needle = shift;;
1602 return unless ($needle);
1604 my $filter = $self->collapse_filters($needle);
1606 warn "find_filter($needle) => " .
1607 (($filter and $filter->{args}) ? "@{$filter->{args}}" : '[]') . "\n"
1608 if $self->QueryParser->debug;
1610 return $filter ? ($filter) : ();
1613 sub find_modifier {
1614 my $self = shift;
1615 my $needle = shift;;
1616 return unless ($needle);
1617 return grep { $_->name eq $needle } @{ $self->modifiers };
1620 sub new_modifier {
1621 my $self = shift;
1622 my $pkg = ref($self) || $self;
1623 my $name = shift;
1625 my $node = do{$pkg.'::modifier'}->new( $name );
1626 $self->add_modifier( $node );
1628 return $node;
1631 sub classed_node {
1632 my $self = shift;
1633 my $requested_class = shift;
1635 my $node;
1636 for my $n (@{$self->{query}}) {
1637 next unless (ref($n) && $n->isa( 'OpenILS::QueryParser::query_plan::node' ));
1638 if ($n->requested_class eq $requested_class) {
1639 $node = $n;
1640 last;
1644 if (!$node) {
1645 $node = $self->new_node;
1646 $node->requested_class( $requested_class );
1649 return $node;
1652 sub remove_last_node {
1653 my $self = shift;
1654 my $requested_class = shift;
1656 my $old = pop(@{$self->query_nodes});
1657 pop(@{$self->query_nodes}) if (@{$self->query_nodes});
1659 return $old;
1662 sub query_nodes {
1663 my $self = shift;
1664 return $self->{query};
1667 sub floating {
1668 my $self = shift;
1669 my $f = shift;
1670 $self->{floating} = $f if (defined $f);
1671 return $self->{floating};
1674 sub add_node {
1675 my $self = shift;
1676 my $node = shift;
1678 $self->{query} ||= [];
1679 push(@{$self->{query}}, $self->joiner) if (@{$self->{query}});
1680 push(@{$self->{query}}, $node);
1682 return $self;
1685 sub top_plan {
1686 my $self = shift;
1688 return $self->{level} ? 0 : 1;
1691 sub plan_level {
1692 my $self = shift;
1693 my $level = shift;
1695 if (defined $level) {
1696 $self->{level} = $level;
1697 for (@{$self->query_nodes}) {
1698 $_->plan_level($level + 1) if (ref and $_->isa('OpenILS::QueryParser::query_plan'));
1702 return $self->{level};
1705 sub joiner {
1706 my $self = shift;
1707 my $joiner = shift;
1709 $self->{joiner} = $joiner if ($joiner);
1710 return $self->{joiner};
1713 sub modifiers {
1714 my $self = shift;
1715 $self->{modifiers} ||= [];
1716 return $self->{modifiers};
1719 sub add_modifier {
1720 my $self = shift;
1721 my $modifier = shift;
1723 $self->{modifiers} ||= [];
1724 $self->{modifiers} = [ grep {$_->name ne $modifier->name} @{$self->{modifiers}} ];
1726 push(@{$self->{modifiers}}, $modifier);
1728 return $self;
1731 sub facets {
1732 my $self = shift;
1733 $self->{facets} ||= [];
1734 return $self->{facets};
1737 sub add_facet {
1738 my $self = shift;
1739 my $facet = shift;
1741 $self->{facets} ||= [];
1742 $self->{facets} = [ grep {$_->name ne $facet->name} @{$self->{facets}} ];
1744 push(@{$self->{facets}}, $facet);
1746 return $self;
1749 sub filters {
1750 my $self = shift;
1751 $self->{filters} ||= [];
1752 return $self->{filters};
1755 sub add_filter {
1756 my $self = shift;
1757 my $filter = shift;
1759 $self->{filters} ||= [];
1761 push(@{$self->{filters}}, $filter);
1763 return $self;
1766 sub negate {
1767 my $self = shift;
1768 my $negate = shift;
1770 $self->{negate} = $negate if (defined $negate);
1772 return $self->{negate};
1775 # %opts supports two options at this time:
1776 # no_phrases :
1777 # If true, do not do anything to the phrases
1778 # fields on any discovered nodes.
1779 # with_config :
1780 # If true, also return the query parser config as part of the blob.
1781 # This will get set back to 0 before recursion to avoid repetition.
1782 sub to_abstract_query {
1783 my $self = shift;
1784 my %opts = @_;
1786 my $pkg = ref $self->QueryParser || $self->QueryParser;
1788 my $abstract_query = {
1789 type => "query_plan",
1790 floating => $self->floating,
1791 level => $self->plan_level,
1792 filters => [map { $_->to_abstract_query } @{$self->filters}],
1793 modifiers => [map { $_->to_abstract_query } @{$self->modifiers}],
1794 negate => $self->negate
1797 if ($opts{with_config}) {
1798 $opts{with_config} = 0;
1799 $abstract_query->{config} = $OpenILS::QueryParser::parser_config{$pkg};
1802 my $kids = [];
1804 for my $qnode (@{$self->query_nodes}) {
1805 # Remember: qnode can be a joiner string, a node, or another query_plan
1807 if (OpenILS::QueryParser::_util::is_joiner($qnode)) {
1808 if ($abstract_query->{children}) {
1809 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
1810 next if $open_joiner eq $qnode;
1812 my $oldroot = $abstract_query->{children};
1813 $kids = [$oldroot];
1814 $abstract_query->{children} = {$qnode => $kids};
1815 } else {
1816 $abstract_query->{children} = {$qnode => $kids};
1818 } else {
1819 push @$kids, $qnode->to_abstract_query(%opts);
1823 $abstract_query->{children} ||= { OpenILS::QueryParser::_util::default_joiner() => $kids };
1824 return $abstract_query;
1828 #-------------------------------
1829 package OpenILS::QueryParser::query_plan::node;
1830 use Data::Dumper;
1831 $Data::Dumper::Indent = 0;
1833 sub new {
1834 my $pkg = shift;
1835 $pkg = ref($pkg) || $pkg;
1836 my %args = @_;
1838 return bless \%args => $pkg;
1841 sub new_atom {
1842 my $self = shift;
1843 my $pkg = ref($self) || $self;
1844 return do{$pkg.'::atom'}->new( @_ );
1847 sub requested_class { # also split into classname, fields and alias
1848 my $self = shift;
1849 my $class = shift;
1851 if ($class) {
1852 my @afields;
1853 my (undef, $alias) = split '#', $class;
1854 if ($alias) {
1855 $class =~ s/#[^|]+//;
1856 ($alias, @afields) = split '\|', $alias;
1859 my @fields = @afields;
1860 my ($class_part, @field_parts) = split '\|', $class;
1861 for my $f (@field_parts) {
1862 push(@fields, $f) unless (grep { $f eq $_ } @fields);
1865 $class_part ||= $class;
1867 $self->{requested_class} = $class;
1868 $self->{alias} = $alias if $alias;
1869 $self->{alias_fields} = \@afields if $alias;
1870 $self->{classname} = $class_part;
1871 $self->{fields} = \@fields;
1874 return $self->{requested_class};
1877 sub plan {
1878 my $self = shift;
1879 my $plan = shift;
1881 $self->{plan} = $plan if ($plan);
1882 return $self->{plan};
1885 sub alias {
1886 my $self = shift;
1887 my $alias = shift;
1889 $self->{alias} = $alias if ($alias);
1890 return $self->{alias};
1893 sub alias_fields {
1894 my $self = shift;
1895 my $alias = shift;
1897 $self->{alias_fields} = $alias if ($alias);
1898 return $self->{alias_fields};
1901 sub classname {
1902 my $self = shift;
1903 my $class = shift;
1905 $self->{classname} = $class if ($class);
1906 return $self->{classname};
1909 sub fields {
1910 my $self = shift;
1911 my @fields = @_;
1913 $self->{fields} ||= [];
1914 $self->{fields} = \@fields if (@fields);
1915 return $self->{fields};
1918 sub phrases {
1919 my $self = shift;
1920 my @phrases = @_;
1922 $self->{phrases} ||= [];
1923 $self->{phrases} = \@phrases if (@phrases);
1924 return $self->{phrases};
1927 sub add_phrase {
1928 my $self = shift;
1929 my $phrase = shift;
1931 push(@{$self->phrases}, $phrase);
1933 return $self;
1936 sub negate {
1937 my $self = shift;
1938 my $negate = shift;
1940 $self->{negate} = $negate if (defined $negate);
1942 return $self->{negate};
1945 sub query_atoms {
1946 my $self = shift;
1947 my @query_atoms = @_;
1949 $self->{query_atoms} ||= [];
1950 $self->{query_atoms} = \@query_atoms if (@query_atoms);
1951 return $self->{query_atoms};
1954 sub add_fts_atom {
1955 my $self = shift;
1956 my $atom = shift;
1958 if (!ref($atom)) {
1959 my $content = $atom;
1960 my @parts = @_;
1962 $atom = $self->new_atom( content => $content, @parts );
1965 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1966 push(@{$self->query_atoms}, $atom);
1968 return $self;
1971 sub add_dummy_atom {
1972 my $self = shift;
1973 my @parts = @_;
1975 my $atom = $self->new_atom( @parts, dummy => 1 );
1977 push(@{$self->query_atoms}, $self->plan->joiner) if (@{$self->query_atoms});
1978 push(@{$self->query_atoms}, $atom);
1980 return $self;
1983 # This will find up to one occurence of @$short_list within @$long_list, and
1984 # replace it with the single atom $replacement.
1985 sub replace_phrase_in_abstract_query {
1986 my ($self, $short_list, $long_list, $replacement) = @_;
1988 my $success = 0;
1989 my @already = ();
1990 my $goal = scalar @$short_list;
1992 for (my $i = 0; $i < scalar (@$long_list); $i++) {
1993 my $right = $long_list->[$i];
1995 if (OpenILS::QueryParser::_util::compare_abstract_atoms(
1996 $short_list->[scalar @already], $right
1997 )) {
1998 push @already, $i;
1999 } elsif (scalar @already) {
2000 @already = ();
2001 next;
2004 if (scalar @already == $goal) {
2005 splice @$long_list, $already[0], scalar(@already), $replacement;
2006 $success = 1;
2007 last;
2011 return $success;
2014 sub to_abstract_query {
2015 my $self = shift;
2016 my %opts = @_;
2018 my $pkg = ref $self->plan->QueryParser || $self->plan->QueryParser;
2020 my $abstract_query = {
2021 "type" => "node",
2022 "alias" => $self->alias,
2023 "alias_fields" => $self->alias_fields,
2024 "class" => $self->classname,
2025 "fields" => $self->fields
2028 my $kids = [];
2030 for my $qatom (@{$self->query_atoms}) {
2031 if (OpenILS::QueryParser::_util::is_joiner($qatom)) {
2032 if ($abstract_query->{children}) {
2033 my $open_joiner = (keys(%{$abstract_query->{children}}))[0];
2034 next if $open_joiner eq $qatom;
2036 my $oldroot = $abstract_query->{children};
2037 $kids = [$oldroot];
2038 $abstract_query->{children} = {$qatom => $kids};
2039 } else {
2040 $abstract_query->{children} = {$qatom => $kids};
2042 } else {
2043 push @$kids, $qatom->to_abstract_query;
2047 $abstract_query->{children} ||= { OpenILS::QueryParser::_util::default_joiner() => $kids };
2049 if ($self->{phrases} and not $opts{no_phrases}) {
2050 for my $phrase (@{$self->{phrases}}) {
2051 # Phrases appear duplication in a real QP tree, and we don't want
2052 # that duplication in our abstract query. So for all our phrases,
2053 # break them into atoms as QP would, and remove any matching
2054 # sequences of atoms from our abstract query.
2056 my $tmp_prefix = '';
2057 $tmp_prefix = $OpenILS::QueryParser::parser_config{$pkg}{operators}{disallowed} if ($self->{negate});
2059 my $tmptree = $self->{plan}->{QueryParser}->new(query => $tmp_prefix.'"'.$phrase.'"')->parse->parse_tree;
2060 if ($tmptree) {
2061 # For a well-behaved phrase, we should now have only one node
2062 # in the $tmptree query plan, and that node should have an
2063 # orderly list of atoms and joiners.
2065 if ($tmptree->{query} and scalar(@{$tmptree->{query}}) == 1) {
2066 my $tmplist;
2068 eval {
2069 $tmplist = $tmptree->{query}->[0]->to_abstract_query(
2070 no_phrases => 1
2071 )->{children}->{'&'}->[0]->{children}->{'&'};
2073 next if $@;
2075 foreach (
2076 OpenILS::QueryParser::_util::find_arrays_in_abstract($abstract_query->{children})
2078 last if $self->replace_phrase_in_abstract_query(
2079 $tmplist,
2081 OpenILS::QueryParser::_util::fake_abstract_atom_from_phrase($phrase, $self->{negate}, $pkg)
2089 $abstract_query->{children} ||= { OpenILS::QueryParser::_util::default_joiner() => $kids };
2090 return $abstract_query;
2093 #-------------------------------
2094 package OpenILS::QueryParser::query_plan::node::atom;
2096 sub new {
2097 my $pkg = shift;
2098 $pkg = ref($pkg) || $pkg;
2099 my %args = @_;
2101 return bless \%args => $pkg;
2104 sub node {
2105 my $self = shift;
2106 return unless (ref $self);
2107 return $self->{node};
2110 sub content {
2111 my $self = shift;
2112 return unless (ref $self);
2113 return $self->{content};
2116 sub prefix {
2117 my $self = shift;
2118 return unless (ref $self);
2119 return $self->{prefix};
2122 sub suffix {
2123 my $self = shift;
2124 return unless (ref $self);
2125 return $self->{suffix};
2128 sub to_abstract_query {
2129 my ($self) = @_;
2131 return {
2132 (map { $_ => $self->$_ } qw/prefix suffix content/),
2133 "type" => "atom"
2136 #-------------------------------
2137 package OpenILS::QueryParser::query_plan::filter;
2139 sub new {
2140 my $pkg = shift;
2141 $pkg = ref($pkg) || $pkg;
2142 my %args = @_;
2144 return bless \%args => $pkg;
2147 sub plan {
2148 my $self = shift;
2149 return $self->{plan};
2152 sub name {
2153 my $self = shift;
2154 return $self->{name};
2157 sub negate {
2158 my $self = shift;
2159 return $self->{negate};
2162 sub args {
2163 my $self = shift;
2164 return $self->{args};
2167 sub to_abstract_query {
2168 my ($self) = @_;
2170 return {
2171 map { $_ => $self->$_ } qw/name negate args/
2175 #-------------------------------
2176 package OpenILS::QueryParser::query_plan::facet;
2178 sub new {
2179 my $pkg = shift;
2180 $pkg = ref($pkg) || $pkg;
2181 my %args = @_;
2183 return bless \%args => $pkg;
2186 sub plan {
2187 my $self = shift;
2188 return $self->{plan};
2191 sub name {
2192 my $self = shift;
2193 return $self->{name};
2196 sub negate {
2197 my $self = shift;
2198 return $self->{negate};
2201 sub values {
2202 my $self = shift;
2203 return $self->{'values'};
2206 sub to_abstract_query {
2207 my ($self) = @_;
2209 return {
2210 (map { $_ => $self->$_ } qw/name negate values/),
2211 "type" => "facet"
2215 #-------------------------------
2216 package OpenILS::QueryParser::query_plan::modifier;
2218 sub new {
2219 my $pkg = shift;
2220 $pkg = ref($pkg) || $pkg;
2221 my $modifier = shift;
2222 my $negate = shift;
2224 return bless { name => $modifier, negate => $negate } => $pkg;
2227 sub name {
2228 my $self = shift;
2229 return $self->{name};
2232 sub negate {
2233 my $self = shift;
2234 return $self->{negate};
2237 sub to_abstract_query {
2238 my ($self) = @_;
2240 return $self->name;