]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Search.pm
search: bump SCHEMA_VERSION to 4
[public-inbox.git] / lib / PublicInbox / Search.pm
index 39b06b0afe6045fcd220eee77133a12144f4d476..b9f283fb2f5433a4839aab87b561f42d5e44f56c 100644 (file)
@@ -10,10 +10,20 @@ require PublicInbox::View;
 use Email::MIME;
 use PublicInbox::MID qw/mid_clean mid_compressed/;
 
+# This is English-only, everything else is non-standard and may be confused as
+# a prefix common in patch emails
+our $REPLY_RE = qr/^re:\s+/i;
+our $LANG = 'english';
+
 use constant {
        TS => 0,
-       SCHEMA_VERSION => 0,
-       LANG => 'english',
+       # SCHEMA_VERSION history
+       # 0 - initial
+       # 1 - subject_path is lower-cased
+       # 2 - subject_path is mid_compressed in the index, only
+       # 3 - message-ID is compressed if it includes '%' (hack!)
+       # 4 - change "Re: " normalization, avoid circular Reference ghosts
+       SCHEMA_VERSION => 4,
        QP_FLAGS => FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE|FLAG_WILDCARD,
 };
 
@@ -54,10 +64,13 @@ sub new {
 
        if ($writable) { # not used by the WWW interface
                require Search::Xapian::WritableDatabase;
-               require File::Path;
-               File::Path::mkpath($dir);
-               $db = Search::Xapian::WritableDatabase->new($dir,
-                                       Search::Xapian::DB_CREATE_OR_OPEN);
+               my $flag = Search::Xapian::DB_OPEN;
+               if ($writable == 1) {
+                       require File::Path;
+                       File::Path::mkpath($dir);
+                       $flag = Search::Xapian::DB_CREATE_OR_OPEN;
+               }
+               $db = Search::Xapian::WritableDatabase->new($dir, $flag);
        } else {
                $db = Search::Xapian::Database->new($dir);
        }
@@ -71,13 +84,12 @@ sub add_message {
        my $db = $self->{xdb};
 
        my $doc_id;
-       my $mid = mid_clean($mime->header('Message-ID'));
-       $mid = mid_compressed($mid);
+       my $mid_orig = mid_clean($mime->header_obj->header_raw('Message-ID'));
+       my $mid = mid_compressed($mid_orig);
        my $was_ghost = 0;
        my $ct_msg = $mime->header('Content-Type') || 'text/plain';
        my $enc_msg = PublicInbox::View::enc_for($ct_msg);
 
-       $db->begin_transaction;
        eval {
                my $smsg = $self->lookup_message($mid);
                my $doc;
@@ -110,7 +122,7 @@ sub add_message {
                        $doc->add_term(xpfx('subject') . $subj);
 
                        my $path = subject_path($subj);
-                       $doc->add_term(xpfx('path') . $path);
+                       $doc->add_term(xpfx('path') . mid_compressed($path));
                }
 
                my $from = $smsg->from_name;
@@ -169,33 +181,28 @@ sub add_message {
        };
 
        if ($@) {
-               warn "failed to index message <$mid>: $@\n";
-               $db->cancel_transaction;
-       } else {
-               $db->commit_transaction;
+               warn "failed to index message <$mid_orig>: $@\n";
+               return undef;
        }
        $doc_id;
 }
 
 # returns deleted doc_id on success, undef on missing
 sub remove_message {
-       my ($self, $mid) = @_;
+       my ($self, $mid_orig) = @_;
        my $db = $self->{xdb};
        my $doc_id;
-       $mid = mid_clean($mid);
-       $mid = mid_compressed($mid);
+       $mid_orig = mid_clean($mid_orig);
+       my $mid = mid_compressed($mid_orig);
 
-       $db->begin_transaction;
        eval {
                $doc_id = $self->find_unique_doc_id('mid', $mid);
                $db->delete_document($doc_id) if defined $doc_id;
        };
 
        if ($@) {
-               warn "failed to remove message <$mid>: $@\n";
-               $db->cancel_transaction;
-       } else {
-               $db->commit_transaction;
+               warn "failed to remove message <$mid_orig>: $@\n";
+               return undef;
        }
        $doc_id;
 }
@@ -209,8 +216,14 @@ sub query {
        $self->do_enquire($query, $opts);
 }
 
-# given a message ID, get replies to a message
-sub get_replies {
+sub get_subject_path {
+       my ($self, $path, $opts) = @_;
+       my $query = $self->qp->parse_query("path:".mid_compressed($path), 0);
+       $self->do_enquire($query);
+}
+
+# given a message ID, get followups to a message
+sub get_followups {
        my ($self, $mid, $opts) = @_;
        $mid = mid_clean($mid);
        $mid = mid_compressed($mid);
@@ -229,7 +242,7 @@ sub get_thread {
        return { count => 0, msgs => [] } unless $smsg;
        my $qp = $self->qp;
        my $qtid = $qp->parse_query('thread:'.$smsg->thread_id);
-       my $qsub = $qp->parse_query('path:'.$smsg->path);
+       my $qsub = $qp->parse_query('path:'.mid_compressed($smsg->path));
        my $query = Search::Xapian::Query->new(OP_OR, $qtid, $qsub);
        $self->do_enquire($query);
 }
@@ -254,7 +267,7 @@ sub do_enquire {
 }
 
 # read-write
-sub stemmer { Search::Xapian::Stem->new(LANG) }
+sub stemmer { Search::Xapian::Stem->new($LANG) }
 
 # read-only
 sub qp {
@@ -332,23 +345,40 @@ sub link_message_to_parents {
        my $doc = $smsg->{doc};
        my $mid = mid_compressed($smsg->mid);
        my $mime = $smsg->mime;
-       my $refs = $mime->header('References');
+       my $refs = $mime->header_obj->header_raw('References');
        my @refs = $refs ? ($refs =~ /<([^>]+)>/g) : ();
-       my $irt = $mime->header('In-Reply-To');
+       my $irt = $mime->header_obj->header_raw('In-Reply-To');
        if ($irt) {
                if ($irt =~ /<([^>]+)>/) {
                        $irt = $1;
                }
-               push @refs, $irt;
+
+               # maybe some crazies will try to make a circular reference:
+               if ($irt eq $mid) {
+                       $irt = undef;
+               } else {
+                       push @refs, $irt;
+               }
        }
 
        my $tid;
        if (@refs) {
-               @refs = map { mid_compressed($_) } @refs;
-               my %uniq;
-               @refs = grep { !$uniq{$_}++ } @refs; # uniq
-
-               $doc->add_term(xpfx('inreplyto') . $refs[-1]);
+               my @crefs = map { mid_compressed($_) } @refs;
+               my %uniq = ($mid => 1);
+
+               # prevent circular references via References: here:
+               @refs = ();
+               foreach my $ref (@crefs) {
+                       next if $uniq{$ref};
+                       $uniq{$ref} = 1;
+                       push @refs, $ref;
+               }
+               $irt = undef if (defined $irt && !$uniq{$irt});
+       }
+       if (@refs) {
+               if (defined $irt) {
+                       $doc->add_term(xpfx('inreplyto') . $irt);
+               }
 
                my $ref_pfx = xpfx('references');
 
@@ -461,13 +491,13 @@ sub merge_threads {
 
 # normalize subjects so they are suitable as pathnames for URLs
 sub subject_path {
-       my ($subj) = @_;
+       my $subj = pop;
 
        $subj =~ s/\A\s+//;
        $subj =~ s/\s+\z//;
-       $subj =~ s/^(?:re|aw):\s*//i; # remove reply prefix (aw: German)
+       $subj =~ s/$REPLY_RE//igo; # remove reply prefix
        $subj =~ s![^a-zA-Z0-9_\.~/\-]+!_!g;
-       $subj;
+       lc($subj);
 }
 
 sub do_cat_mail {
@@ -489,7 +519,7 @@ sub index_blob {
 sub unindex_blob {
        my ($self, $git, $blob) = @_;
        my $mime = do_cat_mail($git, $blob) or return;
-       my $mid = $mime->header('Message-ID');
+       my $mid = $mime->header_obj->header_raw('Message-ID');
        eval { $self->remove_message($mid) } if defined $mid;
        warn "W: unindex_blob $blob: $@\n" if $@;
 }
@@ -501,40 +531,47 @@ sub enquire {
 
 # indexes all unindexed messages
 sub index_sync {
-       my ($self, $git) = @_;
+       my ($self) = @_;
+       require PublicInbox::GitCatFile;
        my $db = $self->{xdb};
-       my $latest = $db->get_metadata('last_commit');
-       my $range = length $latest ? "$latest..HEAD" : 'HEAD';
-       $latest = undef;
-
        my $hex = '[a-f0-9]';
        my $h40 = $hex .'{40}';
        my $addmsg = qr!^:000000 100644 \S+ ($h40) A\t${hex}{2}/${hex}{38}$!;
        my $delmsg = qr!^:100644 000000 ($h40) \S+ D\t${hex}{2}/${hex}{38}$!;
 
-       # get indexed messages
-       my @cmd = ('git', "--git-dir=$git->{git_dir}", "log",
-                   qw/--reverse --no-notes --no-color --raw -r --no-abbrev/,
-                   $range);
-
-       my $pid = open(my $log, '-|', @cmd) or
-               die('open` '.join(' ', @cmd) . " pipe failed: $!\n");
-       my $last;
-       while (my $line = <$log>) {
-               if ($line =~ /$addmsg/o) {
-                       $self->index_blob($git, $1);
-               } elsif ($line =~ /$delmsg/o) {
-                       $self->unindex_blob($git, $1);
-               } elsif ($line =~ /^commit ($h40)/o) {
-                       my $commit = $1;
-                       if (defined $latest) {
-                               $db->set_metadata('last_commit', $latest)
+       $db->begin_transaction;
+       eval {
+               my $git = PublicInbox::GitCatFile->new($self->{git_dir});
+
+               my $latest = $db->get_metadata('last_commit');
+               my $range = length $latest ? "$latest..HEAD" : 'HEAD';
+               $latest = undef;
+
+               # get indexed messages
+               my @cmd = ('git', "--git-dir=$self->{git_dir}", "log",
+                           qw/--reverse --no-notes --no-color --raw -r
+                              --no-abbrev/, $range);
+               my $pid = open(my $log, '-|', @cmd) or
+                       die('open` '.join(' ', @cmd) . " pipe failed: $!\n");
+
+               while (my $line = <$log>) {
+                       if ($line =~ /$addmsg/o) {
+                               $self->index_blob($git, $1);
+                       } elsif ($line =~ /$delmsg/o) {
+                               $self->unindex_blob($git, $1);
+                       } elsif ($line =~ /^commit ($h40)/o) {
+                               $latest = $1;
                        }
-                       $latest = $commit;
                }
+               close $log;
+               $db->set_metadata('last_commit', $latest) if defined $latest;
+       };
+       if ($@) {
+               warn "indexing failed: $@\n";
+               $db->cancel_transaction;
+       } else {
+               $db->commit_transaction;
        }
-       close $log;
-       $db->set_metadata('last_commit', $latest) if defined $latest;
 }
 
 1;