]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchMsg.pm
use both Date: and Received: times
[public-inbox.git] / lib / PublicInbox / SearchMsg.pm
index 941bfd24128ae417ed919d0db9d8c1a68d3e8d97..de1fd1313aaf28e035f4bb93d2ae9e7e8135a1a6 100644 (file)
@@ -7,14 +7,14 @@ package PublicInbox::SearchMsg;
 use strict;
 use warnings;
 use Search::Xapian;
-use Date::Parse qw/str2time/;
 use PublicInbox::MID qw/mid_clean mid_mime/;
 use PublicInbox::Address;
+use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
 
 sub new {
        my ($class, $mime) = @_;
        my $doc = Search::Xapian::Document->new;
-       $doc->add_term('T' . 'mail');
+       $doc->add_boolean_term('T' . 'mail');
 
        bless { type => 'mail', doc => $doc, mime => $mime }, $class;
 }
@@ -31,13 +31,14 @@ sub get_val ($$) {
 
 sub load_from_data ($$) {
        my ($self) = $_[0]; # data = $_[1]
-       my ($subj, $from, $refs, $to, $cc, $blob) = split(/\n/, $_[1]);
+       my ($subj, $from, $refs, $to, $cc, $blob, $mid0) = split(/\n/, $_[1]);
        $self->{subject} = $subj;
        $self->{from} = $from;
        $self->{references} = $refs;
        $self->{to} = $to;
        $self->{cc} = $cc;
        $self->{blob} = $blob;
+       $self->{mid} = $mid0;
 }
 
 sub load_expand {
@@ -45,6 +46,7 @@ sub load_expand {
        my $doc = $self->{doc};
        my $data = $doc->get_data or return;
        $self->{ts} = get_val($doc, &PublicInbox::Search::TS);
+       $self->{ds} = get_val($doc, &PublicInbox::Search::DS);
        utf8::decode($data);
        load_from_data($self, $data);
        $self;
@@ -52,18 +54,16 @@ sub load_expand {
 
 sub load_doc {
        my ($class, $doc) = @_;
-       my $data = $doc->get_data or return;
-       my $ts = get_val($doc, &PublicInbox::Search::TS);
-       utf8::decode($data);
-       my $self = bless { doc => $doc, ts => $ts }, $class;
-       load_from_data($self, $data);
-       $self
+       my $self = bless { doc => $doc }, $class;
+       $self->load_expand;
 }
 
 # :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 num ($) {
+       $_[0]->{num} ||= get_val($_[0]->{doc}, PublicInbox::Search::NUM)
+}
 
 sub __hdr ($$) {
        my ($self, $field) = @_;
@@ -88,9 +88,9 @@ my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
 
 sub date ($) {
        my ($self) = @_;
-       my $ts = $self->{ts};
-       return unless defined $ts;
-       my ($sec, $min, $hour, $mday, $mon, $year, $wday) = 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);
 
@@ -116,15 +116,20 @@ sub from_name {
 
 sub ts {
        my ($self) = @_;
-       $self->{ts} ||= eval { str2time($self->{mime}->header('Date')) } || 0;
+       $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0;
+}
+
+sub ds {
+       my ($self) = @_;
+       $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0;
 }
 
 sub to_doc_data {
-       my ($self, $blob) = @_;
+       my ($self, $oid, $mid0) = @_;
        my @rows = ($self->subject, $self->from, $self->references,
                        $self->to, $self->cc);
-       push @rows, $blob if defined $blob;
-       join("\n", @rows);
+       $oid = '' unless defined $oid;
+       join("\n", @rows, $oid, $mid0);
 }
 
 sub references {
@@ -154,7 +159,7 @@ sub mid ($;$) {
        } elsif (my $rv = $self->{mid}) {
                $rv;
        } else {
-               $self->{mid} = _get_term_val($self, 'XMID', qr/\AXMID/) ||
+               $self->{mid} = _get_term_val($self, 'Q', qr/\AQ/) ||
                                $self->_extract_mid;
        }
 }
@@ -176,4 +181,11 @@ sub path {
        $self->{path} = _get_term_val($self, 'XPATH', qr/\AXPATH/); # path
 }
 
+sub type {
+       my ($self) = @_;
+       my $type = $self->{type};
+       return $type if defined $type;
+       $self->{type} = _get_term_val($self, 'T', qr/\AT/);
+}
+
 1;