-# Copyright (C) 2015-2016 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
# based on notmuch, but with no concept of folders, files or flags
#
# Wraps a document inside our Xapian search index.
+# There may be many of these objects loaded in memory at once
+# for large threads in our WWW UI.
package PublicInbox::SearchMsg;
use strict;
use warnings;
-use Search::Xapian;
-use POSIX qw//;
-use Date::Parse qw/str2time/;
-use PublicInbox::MID qw/mid_clean/;
+use base qw(Exporter);
+our @EXPORT_OK = qw(subject_normalized);
+use PublicInbox::MID qw/mid_clean mid_mime/;
use PublicInbox::Address;
-our $PFX2TERM_RE = undef;
-use constant EPOCH_822 => 'Thu, 01 Jan 1970 00:00:00 +0000';
-use POSIX qw(strftime);
+use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
+use Time::Local qw(timegm);
sub new {
my ($class, $mime) = @_;
- my $doc = Search::Xapian::Document->new;
- $doc->add_term(PublicInbox::Search::xpfx('type') . 'mail');
-
- bless { type => 'mail', doc => $doc, mime => $mime }, $class;
+ bless { mime => $mime }, $class;
}
sub wrap {
- my ($class, $doc, $mid) = @_;
- bless { doc => $doc, mime => undef, mid => $mid }, $class;
+ my ($class, $mid) = @_;
+ bless { mid => $mid }, $class;
+}
+
+sub get {
+ my ($class, $head, $db, $mid) = @_;
+ my $doc_id = $head->get_docid;
+ load_expand(wrap($class, $mid), $db->get_document($doc_id));
}
sub get_val ($$) {
Search::Xapian::sortable_unserialise($doc->get_value($col));
}
-sub load_doc {
- my ($class, $doc) = @_;
+sub to_doc_data {
+ my ($self, $oid, $mid0) = @_;
+ $oid = '' unless defined $oid;
+ join("\n",
+ $self->subject,
+ $self->from,
+ $self->references,
+ $self->to,
+ $self->cc,
+ $oid,
+ $mid0,
+ $self->{bytes} || '',
+ $self->{lines} || ''
+ );
+}
+
+sub load_from_data ($$) {
+ my ($self) = $_[0]; # data = $_[1]
+ (
+ $self->{subject},
+ $self->{from},
+ $self->{references},
+
+ # To: and Cc: are stored to optimize HDR/XHDR in NNTP since
+ # some NNTP clients will use that for message displays.
+ # NNTP only, and only stored in Over(view), not Xapian
+ $self->{to},
+ $self->{cc},
+
+ $self->{blob},
+ $self->{mid},
+
+ # NNTP only
+ $self->{bytes},
+ $self->{lines}
+ ) = split(/\n/, $_[1]);
+}
+
+sub load_expand {
+ my ($self, $doc) = @_;
my $data = $doc->get_data or return;
- my $ts = get_val($doc, &PublicInbox::Search::TS);
+ $self->{ts} = get_val($doc, PublicInbox::Search::TS());
+ my $dt = get_val($doc, PublicInbox::Search::DT());
+ my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $dt);
+ $self->{ds} = timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
utf8::decode($data);
- my ($subj, $from, $refs, $to, $cc) = split(/\n/, $data);
- bless {
- doc => $doc,
- subject => $subj,
- ts => $ts,
- from => $from,
- references => $refs,
- to => $to,
- cc => $cc,
- }, $class;
+ load_from_data($self, $data);
+ $self;
+}
+
+sub psgi_cull ($) {
+ my ($self) = @_;
+ from_name($self); # fill in {from_name} so we can delete {from}
+
+ # drop NNTP-only fields which aren't relevant to PSGI results:
+ # saves ~80K on a 200 item search result:
+ delete @$self{qw(from ts to cc bytes lines)};
+ $self;
+}
+
+# Only called by PSGI interface, not NNTP
+sub load_doc {
+ my ($class, $doc) = @_;
+ my $self = bless {}, $class;
+ psgi_cull(load_expand($self, $doc));
}
# :bytes and :lines metadata in RFC 3977
-sub bytes ($) { get_val($_[0]->{doc}, &PublicInbox::Search::BYTES) }
-sub lines ($) { get_val($_[0]->{doc}, &PublicInbox::Search::LINES) }
-sub num ($) { get_val($_[0]->{doc}, &PublicInbox::Search::NUM) }
+sub bytes ($) { $_[0]->{bytes} }
+sub lines ($) { $_[0]->{lines} }
sub __hdr ($$) {
my ($self, $field) = @_;
my $mime = $self->{mime} or return;
$val = $mime->header($field);
$val = '' unless defined $val;
- $val =~ tr/\n/ /;
+ $val =~ tr/\t\n/ /;
$val =~ tr/\r//d;
$self->{$field} = $val;
}
sub to ($) { __hdr($_[0], 'to') }
sub cc ($) { __hdr($_[0], 'cc') }
+# no strftime, that is locale-dependent and not for RFC822
+my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
+my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
+
sub date ($) {
my ($self) = @_;
- my $date = __hdr($self, 'date');
- return $date if defined $date;
- my $ts = $self->{ts};
- return unless defined $ts;
- $self->{date} = strftime('%a, %d %b %Y %T +0000', gmtime($ts));
+ my $ds = $self->{ds};
+ return unless defined $ds;
+ my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
+ "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
+ $mday, $year+1900, $hour, $min, $sec);
+
}
sub from ($) {
sub ts {
my ($self) = @_;
- $self->{ts} ||= eval { str2time($self->mime->header('Date')) } || 0;
+ $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0;
}
-sub to_doc_data {
+sub ds {
my ($self) = @_;
- join("\n", $self->subject, $self->from, $self->references,
- $self->to, $self->cc);
+ $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0;
}
sub references {
defined $x ? $x : '';
}
-sub ensure_metadata {
- my ($self) = @_;
- my $doc = $self->{doc};
- my $end = $doc->termlist_end;
-
- unless (defined $PFX2TERM_RE) {
- my $or = join('|', keys %PublicInbox::Search::PFX2TERM_RMAP);
- $PFX2TERM_RE = qr/\A($or)/;
- }
-
- while (my ($pfx, $field) = each %PublicInbox::Search::PFX2TERM_RMAP) {
- # ideally we'd move this out of the loop:
- my $i = $doc->termlist_begin;
-
- $i->skip_to($pfx);
- if ($i != $end) {
- my $val = $i->get_termname;
-
- if ($val =~ s/$PFX2TERM_RE//o) {
- $self->{$field} = $val;
- }
- }
- }
-}
-
-# for threading only
-sub mini_mime {
- my ($self) = @_;
- $self->ensure_metadata;
- my @h = (
- 'Subject' => $self->subject,
- 'X-PI-From' => $self->from_name,
- # prevent Email::Simple::Creator from running,
- # this header is useless for threading as we use X-PI-TS
- # for sorting and display:
- 'Date' => EPOCH_822,
- 'Message-ID' => "<$self->{mid}>",
- 'X-PI-TS' => $self->ts,
- );
- if (my $refs = $self->{references}) {
- push @h, References => $refs;
- }
- my $mime = Email::MIME->create(header => \@h);
- my $h = $mime->header_obj;
-
- # set these headers manually since Encode::encode('MIME-Q', ...)
- # will add spaces to long values when using header_str above.
-
- # drop useless headers Email::MIME set for us
- $h->header_set('Date');
- $h->header_set('MIME-Version');
- $mime;
-}
-
sub mid ($;$) {
my ($self, $mid) = @_;
if (defined $mid) {
$self->{mid} = $mid;
- } elsif (my $rv = $self->{mid}) {
+ } elsif (defined(my $rv = $self->{mid})) {
$rv;
} else {
- $self->ensure_metadata; # needed for ghosts
- $self->{mid} ||= $self->_extract_mid;
+ die "NO {mime} for mid\n" unless $self->{mime};
+ $self->_extract_mid; # v1 w/o Xapian
}
}
-sub _extract_mid { mid_clean(mid_mime($_[0]->mime)) }
+sub _extract_mid { mid_clean(mid_mime($_[0]->{mime})) }
-sub mime {
- my ($self, $mime) = @_;
- if (defined $mime) {
- $self->{mime} = $mime;
- } else {
- # TODO load from git
- $self->{mime};
- }
-}
+our $REPLY_RE = qr/^re:\s+/i;
-sub doc_id {
- my ($self, $doc_id) = @_;
- if (defined $doc_id) {
- $self->{doc_id} = $doc_id;
- } else {
- # TODO load from xapian
- $self->{doc_id};
- }
-}
-
-sub thread_id {
- my ($self) = @_;
- my $tid = $self->{thread};
- return $tid if defined $tid;
- $self->ensure_metadata;
- $self->{thread};
-}
-
-sub path {
- my ($self) = @_;
- my $path = $self->{path};
- return $path if defined $path;
- $self->ensure_metadata;
- $self->{path};
+sub subject_normalized ($) {
+ my ($subj) = @_;
+ $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;
}
1;