]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchThread.pm
thread: avoid incrementing undefined value
[public-inbox.git] / lib / PublicInbox / SearchThread.pm
1 # This library is free software; you can redistribute it and/or modify
2 # it under the same terms as Perl itself.
3 #
4 # This license differs from the rest of public-inbox
5 #
6 # Our own jwz-style threading class based on Mail::Thread from CPAN.
7 # Mail::Thread is unmaintained and available on some distros.
8 # We also do not want pruning or subject grouping, since we want
9 # to encourage strict threading and hopefully encourage people
10 # to use proper In-Reply-To.
11 #
12 # This includes fixes from several open bugs for Mail::Thread
13 #
14 # Avoid circular references
15 # - https://rt.cpan.org/Public/Bug/Display.html?id=22817
16 #
17 # And avoid recursion in recurse_down:
18 # - https://rt.cpan.org/Ticket/Display.html?id=116727
19 # - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833479
20 package PublicInbox::SearchThread;
21 use strict;
22 use warnings;
23
24 sub new {
25         return bless {
26                 messages => $_[1],
27                 id_table => {},
28                 rootset  => []
29         }, $_[0];
30 }
31
32 sub thread {
33         my $self = shift;
34         _add_message($self, $_) foreach @{$self->{messages}};
35         $self->{rootset} = [
36                         grep { !$_->{parent} } values %{$self->{id_table}} ];
37         delete $self->{id_table};
38 }
39
40 sub _get_cont_for_id ($$) {
41         my ($self, $mid) = @_;
42         $self->{id_table}{$mid} ||= PublicInbox::SearchThread::Msg->new($mid);
43 }
44
45 sub _add_message ($$) {
46         my ($self, $smsg) = @_;
47
48         # A. if id_table...
49         my $this = _get_cont_for_id($self, $smsg->{mid});
50         $this->{smsg} = $smsg;
51
52         # B. For each element in the message's References field:
53         my $prev;
54         if (defined(my $refs = $smsg->{references})) {
55                 foreach my $ref ($refs =~ m/<([^>]+)>/g) {
56                         # Find a Container object for the given Message-ID
57                         my $cont = _get_cont_for_id($self, $ref);
58
59                         # Link the References field's Containers together in
60                         # the order implied by the References header
61                         #
62                         # * If they are already linked don't change the
63                         #   existing links
64                         # * Do not add a link if adding that link would
65                         #   introduce a loop...
66                         if ($prev &&
67                                 !$cont->{parent} &&  # already linked
68                                 !$cont->has_descendent($prev) # would loop
69                            ) {
70                                 $prev->add_child($cont);
71                         }
72                         $prev = $cont;
73                 }
74         }
75
76         # C. Set the parent of this message to be the last element in
77         # References...
78         if ($prev && !$this->has_descendent($prev)) { # would loop
79                 $prev->add_child($this)
80         }
81 }
82
83 sub order {
84         my ($self, $ordersub) = @_;
85
86         # make a fake root
87         my $root = _get_cont_for_id($self, 'fakeroot');
88         $root->add_child( $_ ) for @{ $self->{rootset} };
89
90         # sort it
91         $root->order_children( $ordersub );
92
93         # and untangle it
94         my $kids = $root->children;
95         $self->{rootset} = $kids;
96         $root->remove_child($_) for @$kids;
97 }
98
99 package PublicInbox::SearchThread::Msg;
100 use Carp qw(croak);
101 use Scalar::Util qw(weaken);
102
103 sub new { my $self = shift; bless { id => shift }, $self; }
104
105 sub add_child {
106         my ($self, $child) = @_;
107         croak "Cowardly refusing to become my own parent: $self"
108           if $self == $child;
109
110         if (grep { $_ == $child } @{$self->children}) {
111                 # All is potentially correct with the world
112                 weaken($child->{parent} = $self);
113                 return;
114         }
115
116         my $parent = $child->{parent};
117         remove_child($parent, $child) if $parent;
118
119         $child->{next} = $self->{child};
120         $self->{child} = $child;
121         weaken($child->{parent} = $self);
122 }
123
124 sub remove_child {
125         my ($self, $child) = @_;
126
127         my $x = $self->{child} or return;
128         if ($x == $child) {  # First one's easy.
129                 $self->{child} = $child->{next};
130                 $child->{parent} = $child->{next} = undef;
131                 return;
132         }
133
134         my $prev = $x;
135         while ($x = $x->{next}) {
136                 if ($x == $child) {
137                         $prev->{next} = $x->{next}; # Unlink x
138                         $x->{next} = $x->{parent} = undef; # Deparent it
139                         return;
140                 }
141                 $prev = $x;
142         }
143         # oddly, we can get here
144         $child->{next} = $child->{parent} = undef;
145 }
146
147 sub has_descendent {
148         my $self = shift;
149         my $child = shift;
150         die "Assertion failed: $child" unless eval {$child};
151         my $there = 0;
152         $self->recurse_down(sub { $there = 1 if $_[0] == $child });
153
154         return $there;
155 }
156
157 sub children {
158         my $self = shift;
159         my @children;
160         my $visitor = $self->{child};
161         while ($visitor) {
162                 push @children, $visitor;
163                 $visitor = $visitor->{next};
164         }
165         \@children;
166 }
167
168 sub set_children {
169         my ($self, $children) = @_;
170         my $walk = $self->{child} = shift @$children;
171         do {
172                 $walk = $walk->{next} = shift @$children;
173         } while ($walk);
174 }
175
176 # non-recursive version of recurse_down to avoid stack depth warnings
177 sub recurse_down {
178         my ($self, $callback) = @_;
179         my %seen;
180         my @q = ($self);
181         while (my $cont = shift @q) {
182                 $seen{$cont} = 1;
183                 $callback->($cont);
184
185                 if (my $next = $cont->{next}) {
186                         if ($seen{$next}) {
187                                 $cont->{next} = undef;
188                         } else {
189                                 push @q, $next;
190                         }
191                 }
192                 if (my $child = $cont->{child}) {
193                         if ($seen{$child}) {
194                                 $cont->{child} = undef;
195                         } else {
196                                 push @q, $child;
197                         }
198                 }
199         }
200 }
201
202 sub order_children {
203         my ($walk, $ordersub) = @_;
204
205         my %seen;
206         my $depth = 0;
207         my @visited;
208         while ($walk) {
209                 push @visited, $walk;
210
211                 # spot/break loops
212                 $seen{$walk} = 1;
213
214                 my $child = $walk->{child};
215                 if ($child && $seen{$child}) {
216                         $walk->{child} = $child = undef;
217                 }
218
219                 my $next = $walk->{next};
220                 if ($next && $seen{$next}) {
221                         $walk->{next} = $next = undef;
222                 }
223
224                 # go down, or across
225                 if ($child) {
226                         $next = $child;
227                         ++$depth;
228                 }
229
230                 # no next?  look up
231                 if (!$next) {
232                         my $up = $walk;
233                         while ($up && !$next) {
234                                 $up = $up->{parent};
235                                 --$depth;
236                                 $next = $up->{next} if $up;
237                         }
238                 }
239                 $walk = $next;
240         }
241         foreach my $cont (@visited) {
242                 my $children = $cont->children;
243                 next if @$children < 2;
244                 $children = $ordersub->($children);
245                 $cont = $cont->{child} = shift @$children;
246                 do {
247                         $cont = $cont->{next} = shift @$children;
248                 } while ($cont);
249         }
250 }
251
252 1;