1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 # <jesse@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 }}}
54 use constant HAS_BOOLEAN_PARSER => do {
56 eval { require Parse::BooleanLogic; 1 }
60 use constant VALUE => 1;
61 use constant AGGREG => 2;
63 use constant OPEN_PAREN => 8;
64 use constant CLOSE_PAREN => 16;
65 use constant KEYWORD => 32;
66 my @tokens = qw[VALUE AGGREGATOR OPERATOR OPEN_PAREN CLOSE_PAREN KEYWORD];
68 use Regexp::Common qw /delimited/;
69 my $re_aggreg = qr[(?i:AND|OR)];
70 my $re_delim = qr[$RE{delimited}{-delim=>qq{\'\"}}];
71 my $re_value = qr[\d+|NULL|$re_delim];
72 my $re_keyword = qr[[{}\w\.]+|$re_delim];
73 my $re_op = qr[=|!=|>=|<=|>|<|(?i:IS NOT)|(?i:IS)|(?i:NOT LIKE)|(?i:LIKE)]; # long to short
74 my $re_open_paren = qr[\(];
75 my $re_close_paren = qr[\)];
80 my ($tree, $node, @pnodes);
84 $callback{'OpenParen'} = sub { push @pnodes, $node; $node = []; push @{ $pnodes[-1] }, $node };
85 $callback{'CloseParen'} = sub { $node = pop @pnodes };
86 $callback{'EntryAggregator'} = sub { push @$node, $_[0] };
87 $callback{'Condition'} = sub { push @$node, { key => $_[0], op => $_[1], value => $_[2] } };
89 Parse($string, \%callback);
94 my ($string, $cb) = @_;
95 $string = '' unless defined $string;
97 my $want = KEYWORD | OPEN_PAREN;
101 my ($key,$op,$value) = ("","","");
103 # order of matches in the RE is important.. op should come early,
104 # because it has spaces in it. otherwise "NOT LIKE" might be parsed
105 # as a keyword or value.
118 # Highest priority is last
120 $current = OP if ($want & OP) && $match =~ /^$re_op$/io;
121 $current = VALUE if ($want & VALUE) && $match =~ /^$re_value$/io;
122 $current = KEYWORD if ($want & KEYWORD) && $match =~ /^$re_keyword$/io;
123 $current = AGGREG if ($want & AGGREG) && $match =~ /^$re_aggreg$/io;
124 $current = OPEN_PAREN if ($want & OPEN_PAREN) && $match =~ /^$re_open_paren$/io;
125 $current = CLOSE_PAREN if ($want & CLOSE_PAREN) && $match =~ /^$re_close_paren$/io;
128 unless ($current && $want & $current) {
129 my $tmp = substr($string, 0, pos($string)- length($match));
130 $tmp .= '>'. $match .'<--here'. substr($string, pos($string));
131 my $msg = "Wrong query, expecting a ". _BitmaskToString($want) ." in '$tmp'";
132 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
138 # Parens are highest priority
139 if ( $current & OPEN_PAREN ) {
140 $cb->{'OpenParen'}->();
142 $want = KEYWORD | OPEN_PAREN;
144 elsif ( $current & CLOSE_PAREN ) {
145 $cb->{'CloseParen'}->();
148 $want |= CLOSE_PAREN if $depth;
150 elsif ( $current & AGGREG ) {
151 $cb->{'EntryAggregator'}->( $match );
152 $want = KEYWORD | OPEN_PAREN;
154 elsif ( $current & KEYWORD ) {
158 elsif ( $current & OP ) {
162 elsif ( $current & VALUE ) {
165 # Remove surrounding quotes and unescape escaped
166 # characters from $key, $match
167 for ( $key, $value ) {
168 if ( /$re_delim/o ) {
170 substr($_,-1,1) = "";
175 $cb->{'Condition'}->( $key, $op, $value );
177 ($key,$op,$value) = ("","","");
179 $want |= CLOSE_PAREN if $depth;
181 my $msg = "Query parser is lost";
182 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
189 unless( !$last || $last & (CLOSE_PAREN | VALUE) ) {
190 my $msg = "Incomplete query, last element ("
191 . _BitmaskToString($last)
192 . ") is not CLOSE_PAREN or VALUE in '$string'";
193 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
198 my $msg = "Incomplete query, $depth paren(s) isn't closed in '$string'";
199 return $cb->{'Error'}->( $msg ) if $cb->{'Error'};
204 sub _BitmaskToString {
208 for( my $i = 0; $i<@tokens; $i++ ) {
209 next unless $mask & (1<<$i);
210 push @res, $tokens[$i];
213 my $tmp = join ', ', splice @res, 0, -1;
214 unshift @res, $tmp if $tmp;
215 return join ' or ', @res;
218 sub PossibleCustomFields {
219 my %args = (Query => undef, CurrentUser => undef, @_);
221 my $cfs = RT::CustomFields->new( $args{'CurrentUser'} );
222 my $ocf_alias = $cfs->_OCFAlias;
223 $cfs->LimitToLookupType( 'RT::Queue-RT::Ticket' );
226 if ( HAS_BOOLEAN_PARSER ) {
227 $tree = Parse::BooleanLogic->filter(
228 RT::SQL::ParseToArray( $args{'Query'} ),
229 sub { $_[0]->{'key'} =~ /^Queue(?:\z|\.)/ },
232 if ( $tree && @$tree ) {
233 my $clause = 'QUEUES';
234 my $queue_alias = $cfs->Join(
236 ALIAS1 => $ocf_alias,
237 FIELD1 => 'ObjectId',
241 $cfs->_OpenParen($clause);
243 SUBCLAUSE => $clause,
244 ENTRYAGGREGATOR => 'AND',
249 $cfs->_OpenParen($clause);
252 Parse::BooleanLogic->walk(
255 open_paren => sub { $cfs->_OpenParen($clause) },
256 close_paren => sub { $cfs->_CloseParen($clause) },
257 operator => sub { $ea = $_[0] },
259 my ($key, $op, $value) = @{$_[0]}{'key', 'op', 'value'};
260 my (undef, @sub) = split /\./, $key;
261 push @sub, $value =~ /\D/? 'Name' : 'id'
264 die "Couldn't handle ". join('.', 'Queue', @sub) if @sub > 1;
266 SUBCLAUSE => $clause,
267 ENTRYAGGREGATOR => $ea,
268 ALIAS => $queue_alias,
277 $cfs->_CloseParen($clause);
278 $cfs->_CloseParen($clause);
281 ENTRYAGGREGATOR => 'AND',
284 OPERATOR => 'IS NOT',
292 eval "require RT::SQL_Vendor";
293 if ($@ && $@ !~ qr{^Can't locate RT/SQL_Vendor.pm}) {
297 eval "require RT::SQL_Local";
298 if ($@ && $@ !~ qr{^Can't locate RT/SQL_Local.pm}) {