2 # BEGIN BPS TAGGED BLOCK {{{
6 # This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC
7 # <sales@bestpractical.com>
9 # (Except where explicitly superseded by other copyright notices)
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 # 02110-1301 or visit their web page on the internet at
28 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
31 # CONTRIBUTION SUBMISSION POLICY:
33 # (The following paragraph is not intended to limit the rights granted
34 # to you to modify and distribute this software under the terms of
35 # the GNU General Public License and is only of importance to you if
36 # you choose to contribute your changes and enhancements to the
37 # community by submitting them to Best Practical Solutions, LLC.)
39 # By intentionally submitting any modifications, corrections or
40 # derivatives to this work, or any other work intended for use with
41 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
42 # you are the copyright holder for those contributions and you grant
43 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
44 # royalty-free, perpetual, license to use, copy, create derivative
45 # works based on those contributions, and sublicense and distribute
46 # those contributions and any derivatives thereof.
48 # END BPS TAGGED BLOCK }}}
58 Use the argument passed in as a "Google-style" set of keywords
67 package RT::Search::Googleish;
71 use base qw(RT::Search);
73 use Regexp::Common qw/delimited/;
74 my $re_delim = qr[$RE{delimited}{-delim=>qq{\'\"}}];
81 $self->{'Queues'} = delete($args{'Queues'}) || [];
82 $self->SUPER::_Init(%args);
89 return ($self->loc("No description for [_1]", ref $self));
96 my $query = shift || $self->Argument;
98 my @keywords = grep length, map { s/^\s+//; s/\s+$//; $_ }
99 split /((?:fulltext:)?$re_delim|\s+)/o, $query;
102 @tql_clauses, @owner_clauses, @queue_clauses,
103 @user_clauses, @id_clauses, @status_clauses
105 my ( $Queue, $User );
106 for my $key (@keywords) {
108 # Is this a ticket number? If so, go to it.
109 # But look into subject as well
110 if ( $key =~ m/^\d+$/ ) {
111 push @id_clauses, "id = '$key'", "Subject LIKE '$key'";
114 # if it's quoted string then search it "as is" in subject or fulltext
115 elsif ( $key =~ /^(fulltext:)?($re_delim)$/io ) {
117 push @tql_clauses, "Content LIKE $2";
119 push @tql_clauses, "Subject LIKE $2";
123 elsif ( $key =~ /^fulltext:(.*?)$/i ) {
125 $key =~ s/['\\].*//g;
126 push @tql_clauses, "Content LIKE '$key'";
130 elsif ( $key =~ /\w+\@\w+/ ) {
131 push @user_clauses, "Requestor LIKE '$key'";
134 # Is there a status with this name?
136 $Queue = RT::Queue->new( $self->TicketsObj->CurrentUser )
137 and $Queue->IsValidStatus($key)
140 push @status_clauses, "Status = '" . $key . "'";
143 # Is there a queue named $key?
144 elsif ( $Queue = RT::Queue->new( $self->TicketsObj->CurrentUser )
145 and $Queue->Load($key)
147 and not $Queue->Disabled )
149 my $quoted_queue = $Queue->Name;
150 $quoted_queue =~ s/'/\\'/g;
151 push @queue_clauses, "Queue = '$quoted_queue'";
154 # Is there a owner named $key?
155 elsif ( $User = RT::User->new( $self->TicketsObj->CurrentUser )
156 and $User->Load($key)
158 and $User->Privileged )
160 push @owner_clauses, "Owner = '" . $User->Name . "'";
163 # Else, subject must contain $key
165 $key =~ s/['\\].*//g;
166 push @tql_clauses, "Subject LIKE '$key'";
170 # restrict to any queues requested by the caller
171 for my $queue (@{ $self->{'Queues'} }) {
172 my $QueueObj = RT::Queue->new($self->TicketsObj->CurrentUser);
173 $QueueObj->Load($queue) or next;
174 my $quoted_queue = $QueueObj->Name;
175 $quoted_queue =~ s/'/\\'/g;
176 push @queue_clauses, "Queue = '$quoted_queue'";
179 if ( ! @status_clauses
180 and ! RT->Config->Get('SimpleSearchIncludeResolved') ) {
181 # implicitly exclude resolved status
182 @status_clauses = map "Status = '$_'", RT::Queue->ActiveStatusArray();
185 push @tql_clauses, join( " OR ", sort @id_clauses );
186 push @tql_clauses, join( " OR ", sort @owner_clauses );
187 push @tql_clauses, join( " OR ", sort @status_clauses );
188 push @tql_clauses, join( " OR ", sort @user_clauses );
189 push @tql_clauses, join( " OR ", sort @queue_clauses );
190 @tql_clauses = grep { $_ ? $_ = "( $_ )" : undef } @tql_clauses;
191 return join " AND ", sort @tql_clauses;
198 my $tql = $self->QueryToSQL($self->Argument);
200 $RT::Logger->debug($tql);
202 $self->TicketsObj->FromSQL($tql);
207 eval "require RT::Search::Googleish_Vendor";
208 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Search/Googleish_Vendor.pm});
209 eval "require RT::Search::Googleish_Local";
210 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Search/Googleish_Local.pm});