use strict;
use warnings;
use PublicInbox::SearchMsg;
-use base qw/Exporter/;
use Search::Xapian qw/:standard/;
-require PublicInbox::View;
use Email::MIME;
use PublicInbox::MID qw/mid_clean mid_compressed/;
-our @EXPORT = qw/xpfx/;
+# 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
+ # 5 - subject_path drops trailing '.'
+ # 6 - preserve References: order in document data
+ SCHEMA_VERSION => 6,
QP_FLAGS => FLAG_PHRASE|FLAG_BOOLEAN|FLAG_LOVEHATE|FLAG_WILDCARD,
};
sub xpfx { $all_pfx{$_[0]} }
our %PFX2TERM_RMAP;
+my %meta_pfx = (mid => 1, thread => 1, path => 1, type => 1);
while (my ($k, $v) = each %all_pfx) {
- next if $prob_prefix{$k};
- $PFX2TERM_RMAP{$v} = $k;
+ $PFX2TERM_RMAP{$v} = $k if $meta_pfx{$k};
}
my $mail_query = Search::Xapian::Query->new(xpfx('type') . 'mail');
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);
}
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('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;
$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;
my $ts = Search::Xapian::sortable_serialise($smsg->ts);
$doc->add_value(PublicInbox::Search::TS, $ts);
- $doc->set_data($smsg->to_doc_data);
-
my $tg = $self->term_generator;
$tg->set_document($doc);
# account for filter bugs...
$ct =~ m!\btext/plain\b!i or return;
- my $enc = PublicInbox::View::enc_for($ct, $enc_msg);
my (@orig, @quot);
- foreach my $l (split(/\n/, $enc->decode($part->body))) {
+ my $body = $part->body;
+ $part->body_set('');
+ my @lines = split(/\n/, $body);
+ while (defined(my $l = shift @lines)) {
if ($l =~ /^\s*>/) {
push @quot, $l;
} else {
}
if (@quot) {
$tg->index_text(join("\n", @quot), 0);
+ @quot = ();
$tg->increase_termpos;
}
if (@orig) {
$tg->index_text(join("\n", @orig));
+ @orig = ();
$tg->increase_termpos;
}
});
if ($was_ghost) {
$doc_id = $smsg->doc_id;
$self->link_message($smsg, 0);
+ $doc->set_data($smsg->to_doc_data);
$db->replace_document($doc_id, $doc);
} else {
$self->link_message($smsg, 0);
+ $doc->set_data($smsg->to_doc_data);
$doc_id = $db->add_document($doc);
}
};
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;
}
my ($self, $query_string, $opts) = @_;
my $query = $self->qp->parse_query($query_string, QP_FLAGS);
- $query = Search::Xapian::Query->new(OP_AND, $mail_query, $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);
my ($self, $mid, $opts) = @_;
my $smsg = eval { $self->lookup_message($mid) };
- return { count => 0, msgs => [] } unless $smsg;
+ return { total => 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);
+ $self->do_enquire($query, $opts);
}
# private subs below
my ($self, $query, $opts) = @_;
my $enquire = $self->enquire;
+ $query = Search::Xapian::Query->new(OP_AND, $query, $mail_query);
$enquire->set_query($query);
$enquire->set_sort_by_relevance_then_value(TS, 0);
$opts ||= {};
PublicInbox::SearchMsg->load_doc($_->get_document);
} $mset->items;
- { count => $mset->get_matches_estimated, msgs => \@msgs }
+ { total => $mset->get_matches_estimated, msgs => \@msgs }
}
# read-write
-sub stemmer { Search::Xapian::Stem->new(LANG) }
+sub stemmer { Search::Xapian::Stem->new($LANG) }
# read-only
sub qp {
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('References');
my @refs = $refs ? ($refs =~ /<([^>]+)>/g) : ();
- my $irt = $mime->header('In-Reply-To');
+ my $irt = $mime->header_obj->header('In-Reply-To');
if ($irt) {
- if ($irt =~ /<([^>]+)>/) {
- $irt = $1;
+ $irt = mid_compressed(mid_clean($irt));
+
+ # maybe some crazies will try to make a circular reference:
+ if ($irt eq $mid) {
+ $irt = undef;
+ } else {
+ # last References should be $irt
+ # we will de-dupe later
+ push @refs, $irt;
}
- 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;
+ }
+ }
+ if (@refs) {
+ $doc->add_term(xpfx('inreplyto') . $irt) if defined $irt;
+ $smsg->{references_sorted} = '<'.join('><', @refs).'>';
my $ref_pfx = xpfx('references');
# normalize subjects so they are suitable as pathnames for URLs
sub subject_path {
- my ($subj) = @_;
-
- $subj =~ s/\A\s+//;
- $subj =~ s/\s+\z//;
- $subj =~ s/^(?:re|aw):\s*//i; # remove reply prefix (aw: German)
+ my $subj = pop;
+ $subj = subject_normalized($subj);
$subj =~ s![^a-zA-Z0-9_\.~/\-]+!_!g;
+ lc($subj);
+}
+
+sub subject_normalized {
+ my $subj = pop;
+ $subj =~ s/\A\s+//s; # no leading space
+ $subj =~ s/\s+\z//s; # no trailing space
+ $subj =~ s/\s+/ /gs; # no redundant spaces
+ $subj =~ s/\.+\z//; # no trailing '.'
+ $subj =~ s/$REPLY_RE//igo; # remove reply prefix
$subj;
}
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('Message-ID');
eval { $self->remove_message($mid) } if defined $mid;
warn "W: unindex_blob $blob: $@\n" if $@;
}
# indexes all unindexed messages
sub index_sync {
- my ($self, $git) = @_;
+ my ($self, $head) = @_;
+ 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}$!;
+ $head ||= 'HEAD';
- # 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;