1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later
3 # version at your option.
4 # See the file README and COPYING for more information.
6 # Copyright 2007 by Don Armstrong <don@donarmstrong.com>.
8 package Debbugs::Estraier;
12 Debbugs::Estraier -- Routines for interfacing bugs to HyperEstraier
16 use Debbugs::Estraier;
30 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
31 use Exporter qw(import);
34 use Debbugs::Common qw(getbuglocation getbugcomponent make_list);
35 use Debbugs::Status qw(readbug);
36 use Debbugs::MIME qw(parse);
37 use Encode qw(encode_utf8);
40 ($VERSION) = q$Revision: 1.3 $ =~ /^Revision:\s+([^\s+])/;
41 $DEBUG = 0 unless defined $DEBUG;
44 %EXPORT_TAGS = (add => [qw(add_bug_log add_bug_message)],
47 Exporter::export_ok_tags(qw(add));
48 $EXPORT_TAGS{all} = [@EXPORT_OK];
53 my ($est,$bug_num) = @_;
55 # We want to read the entire bug log, pulling out individual
56 # messages, and shooting them through hyper estraier
58 my $location = getbuglocation($bug_num,'log');
59 my $bug_log = getbugcomponent($bug_num,'log',$location);
60 my $log_fh = new IO::File $bug_log, 'r' or
61 die "Unable to open bug log $bug_log for reading: $!";
63 my $log = Debbugs::Log->new($log_fh) or
64 die "Debbugs::Log was unable to be initialized";
69 if (my $location = getbuglocation($bug_num,'summary')) {
70 $status = readbug($bug_num,$location);
72 while (my $record = $log->read_record()) {
74 next unless $record->{type} eq 'incoming-recv';
75 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
76 next if defined $msg_id and exists $seen_msg_ids{$msg_id};
77 $seen_msg_ids{$msg_id} = 1 if defined $msg_id;
78 next if defined $msg_id and $msg_id =~ /handler\..+\.ack(?:info)?\@/;
79 add_bug_message($est,$record->{text},$bug_num,$msg_num,$status)
84 =head2 remove_old_message
86 remove_old_message($est,300000,50);
88 Removes all messages which are no longer in the log
92 sub remove_old_messages{
93 my ($est,$bug_num,$max_message) = @_;
94 # remove records which are no longer present in the log (uri > $msg_num)
95 my $cond = new Search::Estraier::Condition;
96 $cond->add_attr('@uri STRBW '.$bug_num.'/');
99 while ($nres = $est->search($cond,0) and $nres->doc_num > 0){
100 for my $rdoc (map {$nres->get_doc($_)} 0..($nres->doc_num-1)) {
101 my $uri = $rdoc->uri;
102 my ($this_message) = $uri =~ m{/(\d+)$};
103 next unless $this_message > $max_message;
104 $est->out_doc_by_uri($uri);
106 last unless $nres->doc_num >= $cond->max;
107 $cond->set_skip($cond->skip+$cond->max);
113 my ($est,$bug_message,$bug_num,
114 $msg_num,$status) = @_;
117 my $uri = "$bug_num/$msg_num";
118 $doc = $est->get_doc_by_uri($uri);
119 $doc = new Search::Estraier::Document if not defined $doc;
121 my $message = parse($bug_message);
122 $doc->add_text(encode_utf8(join("\n",make_list(values %{$message}))));
124 # * @id : the ID number determined automatically when the document is registered.
125 # * @uri : the location of a document which any document should have.
126 # * @digest : the message digest calculated automatically when the document is registered.
127 # * @cdate : the creation date.
128 # * @mdate : the last modification date.
129 # * @adate : the last access date.
130 # * @title : the title used as a headline in the search result.
131 # * @author : the author.
132 # * @type : the media type.
133 # * @lang : the language.
134 # * @genre : the genre.
135 # * @size : the size.
136 # * @weight : the scoring weight.
137 # * @misc : miscellaneous information.
138 my @attr = qw(status subject date submitter package tags severity);
140 my ($date) = $bug_message =~ /^Date:\s+(.+?)\s*$/mi;
141 $doc->add_attr('@cdate' => encode_utf8($date)) if defined $date;
143 my ($subject) = $bug_message =~ /^Subject:\s+(.+?)\s*$/mi;
144 $doc->add_attr('@title' => encode_utf8($subject)) if defined $subject;
146 my ($author) = $bug_message =~ /^From:\s+(.+?)\s*$/mi;
147 $doc->add_attr('@author' => encode_utf8($author)) if defined $author;
149 $doc->add_attr('@uri' => encode_utf8($uri));
150 foreach my $attr (@attr) {
151 $doc->add_attr($attr => encode_utf8($status->{$attr})) if defined $status->{$attr};
153 print STDERR "adding $uri\n" if $DEBUG;
154 # Try a bit harder if estraier is returning timeouts
156 while ($attempt > 0) {
157 $est->put_doc($doc) and last;
158 my $status = $est->status;
160 print STDERR "Failed to add $uri\n".$status."\n";
161 last unless $status =~ /^5/;