1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2015 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 }}}
57 Use the argument passed in as a "Google-style" set of keywords
63 package RT::Search::Googleish;
67 use base qw(RT::Search);
69 use Regexp::Common qw/delimited/;
71 # Only a subset of limit types AND themselves together. "queue:foo
72 # queue:bar" is an OR, but "subject:foo subject:bar" is an AND
82 $self->{'Queues'} = delete( $args{'Queues'} ) || [];
83 $self->SUPER::_Init(%args);
88 return ( $self->loc( "Keyword and intuition-based searching", ref $self ) );
93 my $tql = $self->QueryToSQL( $self->Argument );
95 $RT::Logger->debug($tql);
97 $self->TicketsObj->FromSQL($tql);
103 my $query = shift || $self->Argument;
107 while ($query =~ /^\S/) {
110 (\w+) # A straight word
111 (?:\. # With an optional .foo
112 ($RE{delimited}{-delim=>q['"]}
113 |[\w-]+ # Allow \w + dashes
114 ) # Which could be ."foo bar", too
117 : # Followed by a colon
118 ($RE{delimited}{-delim=>q['"]}
120 ) # And a possibly-quoted foo:"bar baz"
122 my ($type, $extra, $value) = ($1, $2, $3);
123 ($value, my ($quoted)) = $self->Unquote($value);
124 $extra = $self->Unquote($extra) if defined $extra;
125 $self->Dispatch(\%limits, $type, $value, $quoted, $extra);
126 } elsif ($query =~ s/^($RE{delimited}{-delim=>q['"]}|\S+)\s*//) {
127 # If there's no colon, it's just a word or quoted string
128 my($val, $quoted) = $self->Unquote($1);
129 $self->Dispatch(\%limits, $self->GuessType($val, $quoted), $val, $quoted);
132 $self->Finalize(\%limits);
135 for my $subclause (sort keys %limits) {
136 next unless @{$limits{$subclause}};
138 my $op = $AND{lc $subclause} ? "AND" : "OR";
139 push @clauses, "( ".join(" $op ", @{$limits{$subclause}})." )";
142 return join " AND ", @clauses;
147 my ($limits, $type, $contents, $quoted, $extra) = @_;
148 $contents =~ s/(['\\])/\\$1/g;
149 $extra =~ s/(['\\])/\\$1/g if defined $extra;
151 my $method = "Handle" . ucfirst(lc($type));
152 $method = "HandleDefault" unless $self->can($method);
153 my ($key, @tsql) = $self->$method($contents, $quoted, $extra);
154 push @{$limits->{$key}}, @tsql;
158 # Given a word or quoted string, unquote it if it is quoted,
159 # removing escaped quotes.
162 if ($token =~ /^$RE{delimited}{-delim=>q['"]}{-keep}$/) {
163 my $quote = $2 || $5;
164 my $value = $3 || $6;
165 $value =~ s/\\(\\|$quote)/$1/g;
166 return wantarray ? ($value, 1) : $value;
168 return wantarray ? ($token, 0) : $token;
176 # Apply default "active status" limit if we don't have any status
177 # limits ourselves, and we're not limited by id
178 if (not $limits->{status} and not $limits->{id}
179 and RT::Config->Get('OnlySearchActiveTicketsInSimpleSearch', $self->TicketsObj->CurrentUser)) {
180 $limits->{status} = [map {s/(['\\])/\\$1/g; "Status = '$_'"} RT::Queue->ActiveStatusArray()];
183 # Respect the "only search these queues" limit if we didn't
184 # specify any queues ourselves
185 if (not $limits->{queue} and not $limits->{id}) {
186 for my $queue ( @{ $self->{'Queues'} } ) {
187 my $QueueObj = RT::Queue->new( $self->TicketsObj->CurrentUser );
188 next unless $QueueObj->Load($queue);
189 my $name = $QueueObj->Name;
190 $name =~ s/(['\\])/\\$1/g;
191 push @{$limits->{queue}}, "Queue = '$name'";
197 [ 10 => sub { return "subject" if $_[1] } ],
198 [ 20 => sub { return "id" if /^#?\d+$/ } ],
199 [ 30 => sub { return "requestor" if /\w+@\w+/} ],
200 [ 35 => sub { return "domain" if /^@\w+/} ],
202 return "status" if RT::Queue->new( $_[2] )->IsValidStatus( $_ )
204 [ 40 => sub { return "status" if /^((in)?active|any)$/i } ],
206 my $q = RT::Queue->new( $_[2] );
207 return "queue" if $q->Load($_) and $q->Id and not $q->Disabled
210 my $u = RT::User->new( $_[2] );
211 return "owner" if $u->Load($_) and $u->Id and $u->Privileged
213 [ 70 => sub { return "owner" if $_ eq "me" } ],
218 my ($val, $quoted) = @_;
220 my $cu = $self->TicketsObj->CurrentUser;
221 for my $sub (map $_->[1], sort {$a->[0] <=> $b->[0]} @GUESS) {
223 my $ret = $sub->($val, $quoted, $cu);
230 # $_[1] is escaped value without surrounding single quotes
231 # $_[2] is a boolean of "was quoted by the user?"
232 # ensure this is false before you do smart matching like $_[1] eq "me"
233 # $_[3] is escaped subkey, if any (see HandleCf)
234 sub HandleDefault { return subject => "Subject LIKE '$_[1]'"; }
235 sub HandleSubject { return subject => "Subject LIKE '$_[1]'"; }
236 sub HandleFulltext { return content => "Content LIKE '$_[1]'"; }
237 sub HandleContent { return content => "Content LIKE '$_[1]'"; }
238 sub HandleId { $_[1] =~ s/^#//; return id => "Id = $_[1]"; }
240 if ($_[1] =~ /^active$/i and !$_[2]) {
241 return status => map {s/(['\\])/\\$1/g; "Status = '$_'"} RT::Queue->ActiveStatusArray();
242 } elsif ($_[1] =~ /^inactive$/i and !$_[2]) {
243 return status => map {s/(['\\])/\\$1/g; "Status = '$_'"} RT::Queue->InactiveStatusArray();
244 } elsif ($_[1] =~ /^any$/i and !$_[2]) {
247 return status => "Status = '$_[1]'";
251 if (!$_[2] and $_[1] eq "me") {
252 return owner => "Owner.id = '__CurrentUser__'";
254 elsif (!$_[2] and $_[1] =~ /\w+@\w+/) {
255 return owner => "Owner.EmailAddress = '$_[1]'";
257 return owner => "Owner = '$_[1]'";
261 return watcher => (!$_[2] and $_[1] eq "me") ? "Watcher.id = '__CurrentUser__'" : "Watcher = '$_[1]'";
263 sub HandleRequestor { return requestor => "Requestor STARTSWITH '$_[1]'"; }
264 sub HandleDomain { $_[1] =~ s/^@?/@/; return requestor => "Requestor ENDSWITH '$_[1]'"; }
265 sub HandleQueue { return queue => "Queue = '$_[1]'"; }
266 sub HandleQ { return queue => "Queue = '$_[1]'"; }
267 sub HandleCf { return "cf.$_[3]" => "'CF.{$_[3]}' LIKE '$_[1]'"; }
269 RT::Base->_ImportOverlays();