-
Notifications
You must be signed in to change notification settings - Fork 1
/
recentwritebacks_tree
181 lines (159 loc) · 3.85 KB
/
recentwritebacks_tree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Blosxom Plugin: recentwritebacks_tree
# Author(s): typester <[email protected]>
# Version: 1.0
# Blosxom Home/Docs/Licensing: http://www.blosxom.com/
package recentwritebacks_tree;
# --- Configurable variables -----------
my $data_dir = "$blosxom::plugin_state_dir/writeback";
my $db_dir = "$blosxom::datadir";
#my $data_dir = "/virtual/typester/public_html/blosxom/plugins/state/writeback";
#my $db_dir = "/virtual/typester/public_html/blosxom/db";
my $wb_extension = "wb";
my $db_extension = "txt";
# comments
my $cm_entry = 3;
my $cm_num = 5;
# trackbacks
my $tb_entry = 3;
my $tb_num = 3;
# --------------------------------------
use FileHandle;
use vars qw/$cm_list $tb_list @wb_list/;
my $fh = new FileHandle;
sub start {
#return 0 if ($blosxom::path_info =~ /\./);
@wb_list = &createlist($data_dir);
$cm_list = &getwritebacks("comment", $cm_entry, $cm_num);
$tb_list = &getwritebacks("excerpt", $tb_entry, $tb_num);
return 1;
}
sub getwritebacks {
my ($mode, $entrynum, $num) = (@_);
my $count = 0;
my $entry = 0;
my @tmp = ();
my $ret = "<ul>\n";
foreach my $file (@wb_list) {
@tmp = ();
$i = 0;
if($fh->open($file)) {
foreach (<$fh>) {
if($_ =~ /-----$/) {
$i++;
}else {
$tmp[$i] .= $_;
}
}
$fh->close;
}
$count = 0;
my @names = ();
#my @dates = ();
foreach (reverse(@tmp)) {
my ($entrytitle, $body, $url, $name, $date, @dates) = ('', '', '', '', '', ());
if($_ =~ /[\r\n]+$mode:(.+)/) {
$count++;
$body = $1;
if($count == 1) {
$entrytitle = &gettitle($file);
$entry++;
}
}
if($_ =~ /[\r\n]+blog_name:(.+)/ and $_ =~ /[\r\n]+url:(.+)/) {
$url = $1;
}
if($_ =~ /^name:(.+)/ or $_ =~ /[\r\n]+blog_name:(.+)/) {
$name = $1;
}else {
$name = "no name";
}
if($body ne '') {
if($entrytitle ne '') {
$ret .= "<li>$entrytitle\n";
}
if($_ =~ /[\r\n]+date:(\d+)/) {
@dates = localtime($1);
$date = sprintf("%02d/%02d %02d:%02d", $dates[4]+1, $dates[3], $dates[2], $dates[1]);
}
if($url ne '') {
push(@names, "<li>$date from <a href=\"$url\">$name</a></li>\n");
}else {
push(@names, "<li>$date by $name</li>\n");
}
}
last if($count >= $num);
}
if($names[0] ne '') {
$ret .= "<ul>\n";
foreach my $name (reverse(@names)) {
$ret .= $name;
}
$ret .= "</ul>\n</li>\n";
}
last if($entry >= $entrynum);
}
$ret .= "</ul>\n";
return $ret;
}
sub gettitle {
my $file = $_[0];
my $ret = '';
$file =~ s/$data_dir/$db_dir/;
$file =~ s/wb$/txt/;
if($fh->open($file)) {
$ret = <$fh>;
$fh->close;
}
chomp($ret);
my $url = $blosxom::url;
$file =~ s/$db_dir/$url/;
$file =~ s/txt$/html/;
$ret = "<a href=\"$file\" title=\"$ret\">$ret</a>";
return $ret;
}
sub getdates {
my @files = @_;
my @dates = ();
foreach my $file (@files) {
my $mtime = (stat($file))[9];
push(@dates, $mtime);
}
return @dates;
}
sub getfiles {
my $dir = $_[0];
my @files = ();
my @ret = ();
$dir .= "/" if($dir =~ /[^\/]$/);
if(opendir(DIR, $dir)) {
@files = readdir(DIR);
closedir(DIR);
}
foreach my $file (@files) {
next if($file eq '.' or $file eq '..');
if(-d "$dir$file" and $file ne '') {
my @subdir = &getfiles("$dir$file");
@ret = (@ret, @subdir);
}else {
push(@ret, "$dir$file");
}
}
return @ret;
}
sub createlist {
my @files = &getfiles($_[0]);
my @dates = &getdates(@files);
my %hash = {};
my @ret = ();
my $i = 0;
foreach my $date (@dates) {
$hash{$date} = $files[$i];
$i++;
}
@dates = sort {$b <=> $a} @dates;
foreach my $date (@dates) {
push(@ret, $hash{$date});
}
return @ret;
}
1;