1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
55 use constant HAS_BOOLEAN_PARSER => do {
57 eval { require Parse::BooleanLogic; 1 }
61 use constant VALUE => 1;
62 use constant AGGREG => 2;
64 use constant OPEN_PAREN => 8;
65 use constant CLOSE_PAREN => 16;
66 use constant KEYWORD => 32;
67 my @tokens = qw[VALUE AGGREGATOR OPERATOR OPEN_PAREN CLOSE_PAREN KEYWORD];
69 use Regexp::Common qw /delimited/;
70 my $re_aggreg = qr[(?i:AND|OR)];
71 my $re_delim = qr[$RE{delimited}{-delim=>qq{\'\"}}];
72 my $re_value = qr[[+-]?\d+|(?i:NULL)|$re_delim];
73 my $re_keyword = qr[[{}\w\.]+|$re_delim];
74 my $re_op = qr[=|!=|>=|<=|>|<|(?i:IS NOT)|(?i:IS)|(?i:NOT LIKE)|(?i:LIKE)|(?i:NOT STARTSWITH)|(?i:STARTSWITH)|(?i:NOT ENDSWITH)|(?i:ENDSWITH)]; # long to short
75 my $re_open_paren = qr[\(];
76 my $re_close_paren = qr[\)];
81 my ($tree, $node, @pnodes);
85 $callback{'OpenParen'} = sub { push @pnodes, $node; $node = []; push @{ $pnodes[-1] }, $node };
86 $callback{'CloseParen'} = sub { $node = pop @pnodes };
87 $callback{'EntryAggregator'} = sub { push @$node, $_[0] };
88 $callback{'Condition'} = sub { push @$node, { key => $_[0], op => $_[1], value => $_[2] } };
90 Parse($string, \%callback);
95 my ($string, $cb) = @_;
96 my $loc = sub {HTML::Mason::Commands::loc(@_)};
97 $string = '' unless defined $string;
99 my $want = KEYWORD | OPEN_PAREN;
103 my ($key,$op,$value) = ("","","");
105 # order of matches in the RE is important.. op should come early,
106 # because it has spaces in it. otherwise "NOT LIKE" might be parsed
107 # as a keyword or value.
120 # Highest priority is last
122 $current = OP if ($want & OP) && $match =~ /^$re_op$/io;
123 $current = VALUE if ($want & VALUE) && $match =~ /^$re_value$/io;
124 $current = KEYWORD if ($want & KEYWORD) && $match =~ /^$re_keyword$/io;
125 $current = AGGREG if ($want & AGGREG) && $match =~ /^$re_aggreg$/io;
126 $current = OPEN_PAREN if ($want & OPEN_PAREN) && $match =~ /^$re_open_paren$/io;
127 $current = CLOSE_PAREN if ($want & CLOSE_PAREN) && $match =~ /^$re_close_paren$/io;
130 unless ($current && $want & $current) {
131 my $tmp = substr($string, 0, pos($string)- length($match));
132 $tmp .= '>'. $match .'<--here'. substr($string, pos($string));
133 my $msg = $loc->("Wrong query, expecting a [_1] in '[_2]'", _BitmaskToString($want), $tmp);
134 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
140 # Parens are highest priority
141 if ( $current & OPEN_PAREN ) {
142 $cb->{'OpenParen'}->();
144 $want = KEYWORD | OPEN_PAREN;
146 elsif ( $current & CLOSE_PAREN ) {
147 $cb->{'CloseParen'}->();
150 $want |= CLOSE_PAREN if $depth;
152 elsif ( $current & AGGREG ) {
153 $cb->{'EntryAggregator'}->( $match );
154 $want = KEYWORD | OPEN_PAREN;
156 elsif ( $current & KEYWORD ) {
160 elsif ( $current & OP ) {
164 elsif ( $current & VALUE ) {
167 # Remove surrounding quotes and unescape escaped
168 # characters from $key, $match
169 for ( $key, $value ) {
170 if ( /$re_delim/o ) {
172 substr($_,-1,1) = "";
177 $cb->{'Condition'}->( $key, $op, $value );
179 ($key,$op,$value) = ("","","");
181 $want |= CLOSE_PAREN if $depth;
183 my $msg = $loc->("Query parser is lost");
184 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
191 unless( !$last || $last & (CLOSE_PAREN | VALUE) ) {
192 my $msg = $loc->("Incomplete query, last element ([_1]) is not close paren or value in '[_2]'",
193 _BitmaskToString($last),
195 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
200 my $msg = $loc->("Incomplete query, [quant,_1,unclosed paren] in '[_2]'", $depth, $string);
201 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
206 sub _BitmaskToString {
210 for( my $i = 0; $i<@tokens; $i++ ) {
211 next unless $mask & (1<<$i);
212 push @res, $tokens[$i];
215 my $tmp = join ', ', splice @res, 0, -1;
216 unshift @res, $tmp if $tmp;
217 return join ' or ', @res;
220 sub PossibleCustomFields {
221 my %args = (Query => undef, CurrentUser => undef, @_);
223 my $cfs = RT::CustomFields->new( $args{'CurrentUser'} );
224 my $ocf_alias = $cfs->_OCFAlias;
225 $cfs->LimitToLookupType( 'RT::Queue-RT::Ticket' );
228 if ( HAS_BOOLEAN_PARSER ) {
229 $tree = Parse::BooleanLogic->filter(
230 RT::SQL::ParseToArray( $args{'Query'} ),
231 sub { $_[0]->{'key'} =~ /^Queue(?:\z|\.)/ },
234 if ( $tree && @$tree ) {
235 my $clause = 'QUEUES';
236 my $queue_alias = $cfs->Join(
238 ALIAS1 => $ocf_alias,
239 FIELD1 => 'ObjectId',
243 $cfs->_OpenParen($clause);
245 SUBCLAUSE => $clause,
246 ENTRYAGGREGATOR => 'AND',
251 $cfs->_OpenParen($clause);
254 Parse::BooleanLogic->walk(
257 open_paren => sub { $cfs->_OpenParen($clause) },
258 close_paren => sub { $cfs->_CloseParen($clause) },
259 operator => sub { $ea = $_[0] },
261 my ($key, $op, $value) = @{$_[0]}{'key', 'op', 'value'};
262 my (undef, @sub) = split /\./, $key;
263 push @sub, $value =~ /\D/? 'Name' : 'id'
266 die "Couldn't handle ". join('.', 'Queue', @sub) if @sub > 1;
268 SUBCLAUSE => $clause,
269 ENTRYAGGREGATOR => $ea,
270 ALIAS => $queue_alias,
279 $cfs->_CloseParen($clause);
280 $cfs->_CloseParen($clause);
283 ENTRYAGGREGATOR => 'AND',
286 OPERATOR => 'IS NOT',
294 RT::Base->_ImportOverlays();