[Rt-commit] rt branch, googleish-refactor, updated. rt-3.9.4-461-g8598644

Alex Vandiver alexmv at bestpractical.com
Fri Nov 12 16:13:50 EST 2010


The branch, googleish-refactor has been updated
       via  85986448f121f9bed5a5994a1cb5e106f24fa652 (commit)
       via  35f253b550c5aa9e8217c9d10cc17f453c91e59c (commit)
       via  9c8cb2b9dfa0d103eda455aca4d8b913f88d0937 (commit)
       via  98d46b0b0944961d2bf28274276ef00be168fdc8 (commit)
       via  92f8f38341d635bc573f2ae4fb9f07e2b88c0ea6 (commit)
       via  cd8d034303c9a29b261df5024a4671452d18844e (commit)
       via  6493baf544f90bf0675da5bb405c814b870049a5 (commit)
       via  1f8787779b8f27f4eb0fb2376ee28a5f158e0ba6 (commit)
       via  e87a0c504bfdfe2851581094a7a9a69b49ef4530 (commit)
      from  aaaa8fe5764afa57982da5ea8cf15a33bcd110f3 (commit)

Summary of changes:
 lib/RT/Search/Googleish.pm |   85 ++++++++++++++++++++++++--------------------
 t/web/googleish_search.t   |   31 ++++++++--------
 2 files changed, 62 insertions(+), 54 deletions(-)

- Log -----------------------------------------------------------------
commit e87a0c504bfdfe2851581094a7a9a69b49ef4530
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Nov 12 15:45:21 2010 -0500

    Add active search limits to tests

diff --git a/t/web/googleish_search.t b/t/web/googleish_search.t
index 0082752..57590f8 100644
--- a/t/web/googleish_search.t
+++ b/t/web/googleish_search.t
@@ -16,44 +16,44 @@ my $two_words_queue = RT::Test->load_or_create_queue(
 ok $two_words_queue && $two_words_queue->id, 'loaded or created a queue';
 
 
-diag "just test an API";
 {
     my $tickets = RT::Tickets->new( RT->SystemUser );
+    my $active = "( ".join( " OR ", map "Status = '$_'", RT::Queue->ActiveStatusArray())." )";
 
     require RT::Search::Googleish;
     my $parser = RT::Search::Googleish->new(
         TicketsObj => $tickets,
         Argument   => '',
     );
-    is $parser->QueryToSQL("foo"), "( Subject LIKE 'foo' )", "correct parsing";
+    is $parser->QueryToSQL("foo"), "$active AND ( Subject LIKE 'foo' )", "correct parsing";
     is $parser->QueryToSQL("1"), "( Id = 1 )", "correct parsing";
-    is $parser->QueryToSQL("'1'"), "( Subject LIKE '1' )", "correct parsing";
+    is $parser->QueryToSQL("'1'"), "$active AND ( Subject LIKE '1' )", "correct parsing";
 
     is $parser->QueryToSQL("foo bar"),
-        "( Subject LIKE 'foo' AND Subject LIKE 'bar' )",
+        "$active AND ( Subject LIKE 'foo' AND Subject LIKE 'bar' )",
         "correct parsing";
     is $parser->QueryToSQL("'foo bar'"),
-        "( Subject LIKE 'foo bar' )",
+        "$active AND ( Subject LIKE 'foo bar' )",
         "correct parsing";
 
     is $parser->QueryToSQL("'foo \\' bar'"),
-        "( Subject LIKE 'foo \\' bar' )",
+        "$active AND ( Subject LIKE 'foo \\' bar' )",
         "correct parsing";
     is $parser->QueryToSQL('"foo \' bar"'),
-        "( Subject LIKE 'foo \\' bar' )",
+        "$active AND ( Subject LIKE 'foo \\' bar' )",
         "correct parsing";
     is $parser->QueryToSQL('"\f\o\o"'),
-        "( Subject LIKE 'foo' )",
+        "$active AND ( Subject LIKE 'foo' )",
         "correct parsing";
 
-    is $parser->QueryToSQL("General"), "( Queue = 'General' )", "correct parsing";
-    is $parser->QueryToSQL("'Two Words'"), "( Queue = 'Two Words' )", "correct parsing";
-    is $parser->QueryToSQL("subject:'Two Words'"), "( Subject LIKE 'Two Words' )", "correct parsing";
+    is $parser->QueryToSQL("General"), "( Queue = 'General' ) AND $active", "correct parsing";
+    is $parser->QueryToSQL("'Two Words'"), "( Queue = 'Two Words' ) AND $active", "correct parsing";
+    is $parser->QueryToSQL("subject:'Two Words'"), "$active AND ( Subject LIKE 'Two Words' )", "correct parsing";
 
-    is $parser->QueryToSQL("me"), "( Watcher.id = '__CurrentUser__' )", "correct parsing";
-    is $parser->QueryToSQL("'me'"), "( Subject LIKE 'me' )", "correct parsing";
-    is $parser->QueryToSQL("owner:me"), "( Owner.id = '__CurrentUser__' )", "correct parsing";
-    is $parser->QueryToSQL("owner:'me'"), "( Owner = 'me' )", "correct parsing";
+    is $parser->QueryToSQL("me"), "$active AND ( Watcher.id = '__CurrentUser__' )", "correct parsing";
+    is $parser->QueryToSQL("'me'"), "$active AND ( Subject LIKE 'me' )", "correct parsing";
+    is $parser->QueryToSQL("owner:me"), "( Owner.id = '__CurrentUser__' ) AND $active", "correct parsing";
+    is $parser->QueryToSQL("owner:'me'"), "( Owner = 'me' ) AND $active", "correct parsing";
 }
 
 my $ticket_found_1 = RT::Ticket->new($RT::SystemUser);

commit 1f8787779b8f27f4eb0fb2376ee28a5f158e0ba6
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Nov 12 15:52:40 2010 -0500

    Quoted values are always subject searches

diff --git a/lib/RT/Search/Googleish.pm b/lib/RT/Search/Googleish.pm
index f85bc6f..bd80da4 100644
--- a/lib/RT/Search/Googleish.pm
+++ b/lib/RT/Search/Googleish.pm
@@ -120,13 +120,13 @@ sub QueryToSQL {
                         ) # And a possibly-quoted foo:"bar baz"
                         \s*//ix) {
             my ($type, $extra, $value) = ($1, $2, $3);
-            $value = $self->Unquote($value);
+            ($value, my ($quoted)) = $self->Unquote($value);
             $extra = $self->Unquote($extra) if defined $extra;
-            $self->Dispatch(\%limits, $type, $value, $extra);
+            $self->Dispatch(\%limits, $type, $value, $quoted, $extra);
         } elsif ($query =~ s/^($RE{delimited}{-delim=>q['"]}|\S+)\s*//) {
             # If there's no colon, it's just a word or quoted string
-            my $val = $self->Unquote($1);
-            $self->Dispatch(\%limits, $self->GuessType($val), $val);
+            my($val, $quoted) = $self->Unquote($1);
+            $self->Dispatch(\%limits, $self->GuessType($val, $quoted), $val, $quoted);
         }
     }
     $self->Finalize(\%limits);
@@ -142,13 +142,13 @@ sub QueryToSQL {
 
 sub Dispatch {
     my $self = shift;
-    my ($limits, $type, $contents, $extra) = @_;
+    my ($limits, $type, $contents, $quoted, $extra) = @_;
     $contents =~ s/(['\\])/\\$1/g;
     $extra    =~ s/(['\\])/\\$1/g if defined $extra;
 
     my $method = "Handle" . ucfirst(lc($type));
     $method = "HandleDefault" unless $self->can($method);
-    my ($key, $tsql) = $self->$method($contents, $extra);
+    my ($key, $tsql) = $self->$method($contents, $quoted, $extra);
     push @{$limits->{$key}}, $tsql;
 }
 
@@ -161,9 +161,9 @@ sub Unquote {
         my $quote = $2 || $5;
         my $value = $3 || $6;
         $value =~ s/\\(\\|$quote)/$1/g;
-        return $value;
+        return wantarray ? ($value, 1) : $value;
     } else {
-        return $token;
+        return wantarray ? ($token, 0) : $token;
     }
 }
 
@@ -193,7 +193,9 @@ sub Finalize {
 
 sub GuessType {
     my $self = shift;
-    my ($val) = @_;
+    my ($val, $quoted) = @_;
+
+    return "subject" if $quoted;
 
     my $Queue = RT::Queue->new( $self->TicketsObj->CurrentUser );
     my $User = RT::User->new( $self->TicketsObj->CurrentUser );
@@ -230,7 +232,7 @@ sub HandleRequestor { return requestor => "Requestor LIKE '$_[1]'";  }
 sub HandleQueue     { return queue     => "Queue = '$_[1]'";      }
 
 sub HandleNumber    { return number    => "(Id = '$_[1]' OR Subject LIKE '$_[1]')"; }
-sub HandleCf        { return "cf.$_[2]" => "CF.'$_[2]' LIKE '$_[1]'"; }
+sub HandleCf        { return "cf.$_[3]" => "CF.'$_[3]' LIKE '$_[1]'"; }
 
 RT::Base->_ImportOverlays();
 

commit 6493baf544f90bf0675da5bb405c814b870049a5
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Nov 12 15:54:30 2010 -0500

    Allow #123 as a ticket id

diff --git a/lib/RT/Search/Googleish.pm b/lib/RT/Search/Googleish.pm
index bd80da4..2a150c1 100644
--- a/lib/RT/Search/Googleish.pm
+++ b/lib/RT/Search/Googleish.pm
@@ -199,7 +199,7 @@ sub GuessType {
 
     my $Queue = RT::Queue->new( $self->TicketsObj->CurrentUser );
     my $User = RT::User->new( $self->TicketsObj->CurrentUser );
-    if ($val =~ /^\d+$/) {
+    if ($val =~ /^#?\d+$/) {
         # Simple numeric => id or subject
         return "number";
     } elsif ($val =~ /\w+\@\w+/) {
@@ -225,7 +225,7 @@ sub HandleDefault   { return subject   => "Subject LIKE '$_[1]'"; }
 sub HandleSubject   { return subject   => "Subject LIKE '$_[1]'"; }
 sub HandleFulltext  { return content   => "Content LIKE '$_[1]'"; }
 sub HandleContent   { return content   => "Content LIKE '$_[1]'"; }
-sub HandleId        { return id        => "Id = '$_[1]'";         }
+sub HandleId        { $_[1] =~ s/^#//; return id => "Id = $_[1]"; }
 sub HandleStatus    { return status    => "Status = '$_[1]'";     }
 sub HandleOwner     { return owner     => "Owner = '$_[1]'";      }
 sub HandleRequestor { return requestor => "Requestor LIKE '$_[1]'";  }
diff --git a/t/web/googleish_search.t b/t/web/googleish_search.t
index 57590f8..218b65f 100644
--- a/t/web/googleish_search.t
+++ b/t/web/googleish_search.t
@@ -27,6 +27,7 @@ ok $two_words_queue && $two_words_queue->id, 'loaded or created a queue';
     );
     is $parser->QueryToSQL("foo"), "$active AND ( Subject LIKE 'foo' )", "correct parsing";
     is $parser->QueryToSQL("1"), "( Id = 1 )", "correct parsing";
+    is $parser->QueryToSQL("#1"), "( Id = 1 )", "correct parsing";
     is $parser->QueryToSQL("'1'"), "$active AND ( Subject LIKE '1' )", "correct parsing";
 
     is $parser->QueryToSQL("foo bar"),

commit cd8d034303c9a29b261df5024a4671452d18844e
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Nov 12 16:00:37 2010 -0500

    Make 123 always mean Id = 123; users can use "123" to search subjects

diff --git a/lib/RT/Search/Googleish.pm b/lib/RT/Search/Googleish.pm
index 2a150c1..eb2c903 100644
--- a/lib/RT/Search/Googleish.pm
+++ b/lib/RT/Search/Googleish.pm
@@ -201,7 +201,7 @@ sub GuessType {
     my $User = RT::User->new( $self->TicketsObj->CurrentUser );
     if ($val =~ /^#?\d+$/) {
         # Simple numeric => id or subject
-        return "number";
+        return "id";
     } elsif ($val =~ /\w+\@\w+/) {
         # email => requestor address
         return "requestor";
@@ -230,8 +230,6 @@ sub HandleStatus    { return status    => "Status = '$_[1]'";     }
 sub HandleOwner     { return owner     => "Owner = '$_[1]'";      }
 sub HandleRequestor { return requestor => "Requestor LIKE '$_[1]'";  }
 sub HandleQueue     { return queue     => "Queue = '$_[1]'";      }
-
-sub HandleNumber    { return number    => "(Id = '$_[1]' OR Subject LIKE '$_[1]')"; }
 sub HandleCf        { return "cf.$_[3]" => "CF.'$_[3]' LIKE '$_[1]'"; }
 
 RT::Base->_ImportOverlays();

commit 92f8f38341d635bc573f2ae4fb9f07e2b88c0ea6
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Nov 12 15:55:30 2010 -0500

    Searches by id are exempt from queue and status limiting, for obvious reasons

diff --git a/lib/RT/Search/Googleish.pm b/lib/RT/Search/Googleish.pm
index eb2c903..b64d6a7 100644
--- a/lib/RT/Search/Googleish.pm
+++ b/lib/RT/Search/Googleish.pm
@@ -172,15 +172,15 @@ sub Finalize {
     my ($limits) = @_;
 
     # Apply default "active status" limit if we don't have any status
-    # limits ourselves
-    if (not $limits->{status}
+    # limits ourselves, and we're not limited by id
+    if (not $limits->{status} and not $limits->{id}
         and RT::Config->Get('OnlySearchActiveTicketsInSimpleSearch', $self->TicketsObj->CurrentUser)) {
         $limits->{status} = [map {s/(['\\])/\\$1/g; "Status = '$_'"} RT::Queue->ActiveStatusArray()];
     }
 
     # Respect the "only search these queues" limit if we didn't
     # specify any queues ourselves
-    if (not $limits->{queue}) {
+    if (not $limits->{queue} and not $limits->{id}) {
         for my $queue ( @{ $self->{'Queues'} } ) {
             my $QueueObj = RT::Queue->new( $self->TicketsObj->CurrentUser );
             next unless $QueueObj->Load($queue);

commit 98d46b0b0944961d2bf28274276ef00be168fdc8
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Nov 12 15:57:48 2010 -0500

    Refactor GuessType into iterating over a list of check subs, and picking the first one

diff --git a/lib/RT/Search/Googleish.pm b/lib/RT/Search/Googleish.pm
index b64d6a7..25afde5 100644
--- a/lib/RT/Search/Googleish.pm
+++ b/lib/RT/Search/Googleish.pm
@@ -191,34 +191,34 @@ sub Finalize {
     }
 }
 
+our @GUESS = (
+    [ 10 => sub { return "subject" if $_[1] } ],
+    [ 20 => sub { return "id" if /^#?\d+$/ } ],
+    [ 30 => sub { return "requestor" if /\w+@\w+/} ],
+    [ 40 => sub {
+          return "status" if RT::Queue->new( $_[2] )->IsValidStatus( $_ )
+      }],
+    [ 50 => sub {
+          my $q = RT::Queue->new( $_[2] );
+          return "queue" if $q->Load($_) and $q->Id
+      }],
+    [ 60 => sub {
+          my $u = RT::User->new( $_[2] );
+          return "owner" if $u->Load($_) and $u->Id and $u->Privileged
+      }],
+);
+
 sub GuessType {
     my $self = shift;
     my ($val, $quoted) = @_;
 
-    return "subject" if $quoted;
-
-    my $Queue = RT::Queue->new( $self->TicketsObj->CurrentUser );
-    my $User = RT::User->new( $self->TicketsObj->CurrentUser );
-    if ($val =~ /^#?\d+$/) {
-        # Simple numeric => id or subject
-        return "id";
-    } elsif ($val =~ /\w+\@\w+/) {
-        # email => requestor address
-        return "requestor";
-    } elsif ( $Queue->IsValidStatus($val) ) {
-        # a valid status
-        return "status";
-    } elsif ($Queue->Load($val) && $Queue->Id ) {
-        # Matches a queue name (or id, but we dealt with that above)
-        return "queue";
-    } elsif ($User->Load($val) && $User->Id && $User->Privileged ) {
-        # Matches a privileged username
-        return "owner";
-    } else {
-        # Fall through to the default
-        return "default";
+    my $cu = $self->TicketsObj->CurrentUser;
+    for my $sub (map $_->[1], sort {$a->[0] <=> $b->[0]} @GUESS) {
+        local $_ = $val;
+        my $ret = $sub->($val, $quoted, $cu);
+        return $ret if $ret;
     }
-
+    return "default";
 }
 
 sub HandleDefault   { return subject   => "Subject LIKE '$_[1]'"; }

commit 9c8cb2b9dfa0d103eda455aca4d8b913f88d0937
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Nov 12 15:59:11 2010 -0500

    Allow "owner:me" to match tickets I own, and "me" to do that as well

diff --git a/lib/RT/Search/Googleish.pm b/lib/RT/Search/Googleish.pm
index 25afde5..4c451ed 100644
--- a/lib/RT/Search/Googleish.pm
+++ b/lib/RT/Search/Googleish.pm
@@ -206,6 +206,7 @@ our @GUESS = (
           my $u = RT::User->new( $_[2] );
           return "owner" if $u->Load($_) and $u->Id and $u->Privileged
       }],
+    [ 70 => sub { return "owner" if $_ eq "me" } ],
 );
 
 sub GuessType {
@@ -227,7 +228,9 @@ sub HandleFulltext  { return content   => "Content LIKE '$_[1]'"; }
 sub HandleContent   { return content   => "Content LIKE '$_[1]'"; }
 sub HandleId        { $_[1] =~ s/^#//; return id => "Id = $_[1]"; }
 sub HandleStatus    { return status    => "Status = '$_[1]'";     }
-sub HandleOwner     { return owner     => "Owner = '$_[1]'";      }
+sub HandleOwner     {
+    return owner  => (!$_[2] and $_[1] eq "me") ? "Owner.id = '__CurrentUser__'" : "Owner = '$_[1]'";
+}
 sub HandleRequestor { return requestor => "Requestor LIKE '$_[1]'";  }
 sub HandleQueue     { return queue     => "Queue = '$_[1]'";      }
 sub HandleCf        { return "cf.$_[3]" => "CF.'$_[3]' LIKE '$_[1]'"; }

commit 35f253b550c5aa9e8217c9d10cc17f453c91e59c
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Nov 12 15:59:29 2010 -0500

    For completeness, add watcher:

diff --git a/lib/RT/Search/Googleish.pm b/lib/RT/Search/Googleish.pm
index 4c451ed..6503cc6 100644
--- a/lib/RT/Search/Googleish.pm
+++ b/lib/RT/Search/Googleish.pm
@@ -231,6 +231,9 @@ sub HandleStatus    { return status    => "Status = '$_[1]'";     }
 sub HandleOwner     {
     return owner  => (!$_[2] and $_[1] eq "me") ? "Owner.id = '__CurrentUser__'" : "Owner = '$_[1]'";
 }
+sub HandleWatcher     {
+    return watcher => (!$_[2] and $_[1] eq "me") ? "Watcher.id = '__CurrentUser__'" : "Watcher = '$_[1]'";
+}
 sub HandleRequestor { return requestor => "Requestor LIKE '$_[1]'";  }
 sub HandleQueue     { return queue     => "Queue = '$_[1]'";      }
 sub HandleCf        { return "cf.$_[3]" => "CF.'$_[3]' LIKE '$_[1]'"; }

commit 85986448f121f9bed5a5994a1cb5e106f24fa652
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Fri Nov 12 16:01:22 2010 -0500

    Add q:queuename as an alias for queue:queuename

diff --git a/lib/RT/Search/Googleish.pm b/lib/RT/Search/Googleish.pm
index 6503cc6..f8ca0bd 100644
--- a/lib/RT/Search/Googleish.pm
+++ b/lib/RT/Search/Googleish.pm
@@ -236,6 +236,7 @@ sub HandleWatcher     {
 }
 sub HandleRequestor { return requestor => "Requestor LIKE '$_[1]'";  }
 sub HandleQueue     { return queue     => "Queue = '$_[1]'";      }
+sub HandleQ         { return queue     => "Queue = '$_[1]'";      }
 sub HandleCf        { return "cf.$_[3]" => "CF.'$_[3]' LIKE '$_[1]'"; }
 
 RT::Base->_ImportOverlays();

-----------------------------------------------------------------------


More information about the Rt-commit mailing list