tagging: Allow using titles in for related stories.
[matthijs/upstream/blosxom-plugins.git] / general / recentwritebacks
1 # Blosxom Plugin: RecentWritebacks
2 # Author: Fletcher T. Penney
3 # Version: 0.2
4
5 package recentwritebacks;
6
7 # --- Configurable variables -----
8
9 $writebacks_extension = "wb";
10
11 $writebacksdir = "$blosxom::plugin_state_dir/writeback";
12
13 $switch_word = "recent";        # This defines the term that needs to be added to the url
14                                 # ie "http://some.host/weblog/?recent=5"
15                                 # This finds posts with comments in last 5 days
16
17 $use_datestamp = 1;                     # Use embedded datestamp, rather than file modification date
18
19 $date_field = "date";           # What variable contains the date
20
21 $use_UK_dates = 0;                      # Default is mm/dd/yy (US)
22                                                         # Set to 1 to use dd/mm/yy (UK)
23
24 # --------------------------------
25
26
27
28 use CGI qw/:standard/;
29 use FileHandle;
30 use File::stat;
31 use Time::Local;
32
33 my $time = time();
34
35 my $fh = new FileHandle;
36
37 sub start {
38         if (CGI::param($switch_word)) {
39                 $time_window = CGI::param($switch_word);
40                 $time =  $time - $time_window*60*60*24;
41                 return 1;
42         } else {
43                 return 0;
44         }
45 }
46
47 sub filter {
48         my ($pkg, $files_ref) = @_;
49         my @files_list = keys %$files_ref;
50
51         foreach $file (@files_list) {
52                 $realfile = $file;
53                 $file =~ s/$blosxom::datadir/$writebacksdir/;
54                 $file =~ s/txt$/$writebacks_extension/;
55                 if ($fh->open("$file")) {
56                         $keep =0;
57                         
58                         $stats = stat($file)->mtime;
59
60                         if ( $use_datestamp == 1 ) {
61                                 while ( $line = <$fh> ) {
62                                         if ($line =~ /^$date_field\:\s*(.*)/) {
63                                                 $datestamp = $1;
64                                                 
65                                                 $stat2 = parsedate($datestamp);
66                                                 if ($stat2 gt $stats) {
67                                                         $stats = $stat2;
68                                                 }                               
69                                         }
70                                 }
71                         }
72
73                         if ($stats lt $time) {
74                                 delete $files_ref->{$realfile};
75                         }                               
76
77                 } else {
78                         # No writebacks available
79                         delete $files_ref->{$realfile};
80                 }
81         }
82         1;
83 }
84
85
86 sub parsedate {
87         my ($datestring) = @_;
88         #warn "Parsing $datestring\n";
89         
90         # Possible formatting
91         # Month can be 3 letter abbreviation or full name (in English)
92         # Time must be hh:mm or hh:mm:ss  in 24 hour format
93         # Year must be yyyy
94         # The remaining 1 or 2 digits are treated as date
95         # ie: May 25 2003 18:40 
96         # order is not important as long as pieces are there
97                 
98         # Convert the datestring to a time() format
99
100         # Find "Shorthand" Date
101         if ( $datestring =~ /\d\d?\/\d\d?\/\d\d\d?\d?/) {
102                 if ( $use_UK_dates eq 0) {
103                         # Use US Formatting
104                         $datestring =~ s/(\d\d?)\/(\d\d?)\/(\d\d\d?\d?)//;
105                         $mon = $1 - 1;
106                         $day = $2;
107                         $year = $3;
108                 } else {
109                         # Use UK Formatting
110                         $datestring =~ s/(\d\d?)\/(\d\d?)\/(\d\d\d?\d?)//;
111                         $mon = $2 - 1;
112                         $day = $1;
113                         $year = $3;
114                 }
115                 
116                 # Now, clean up year if 2 digit
117                 # You may change the 70 to whatever cutoff you like
118                 $year += 2000 if ($year < 70 );
119                 $year += 1900 if ($year < 100);
120         }
121         
122         # Find Month
123         $mon = 0 if ($datestring =~ s/(Jan|January)//i);
124         $mon = 1 if ($datestring =~ s/(Feb|February)//i);
125         $mon = 2 if ($datestring =~ s/(Mar|March)//i);
126         $mon = 3 if ($datestring =~ s/(Apr|April)//i);
127         $mon = 4 if ($datestring =~ s/(May)//i);
128         $mon = 5 if ($datestring =~ s/(Jun|June)//i);
129         $mon = 6 if ($datestring =~ s/(Jul|July)//i);
130         $mon = 7 if ($datestring =~ s/(Aug|August)//i);
131         $mon = 8 if ($datestring =~ s/(Sep|September)//i);
132         $mon = 9 if ($datestring =~ s/(Oct|October)//i);
133         $mon = 10 if ($datestring =~ s/(Nov|November)//i);
134         $mon = 11 if ($datestring =~ s/(Dec|December)//i);
135
136         # Find Time
137         if ($datestring =~ s/(\d\d?):(\d\d)(:\d\d)?//) {
138                 $hour = $1;
139                 $min = $2;
140                 $sec = $3;
141         }
142         
143         if ($datestring =~ s/(\d\d\d\d)//) {
144                 $year = $1;
145         }
146         
147         if ($datestring =~ s/(\d\d?)//) {
148                 $day = $1;
149         }
150         
151         return timelocal($sec,$min,$hour,$day,$mon,$year);
152         
153 }
154
155 1;
156
157 __END__
158
159 =head1 NAME
160
161 Blosxom Plug-in: recentwritebacks
162
163 =head1 DESCRIPTION
164
165 RecentWritebacks locates stories with comments posted within a specified period of time. If you are like me, you find it hard to keep up with comments posted to older articles.  This plugin is designed to assist with that.  You can also use it to make it easier for visitors to your site to locate the "Hot Topics" where writebacks are being submitted.
166
167 To use it, simply install the plugin and configure the $writebacks_extension and $writebacksdir variables.  They should match whatever you used in the writebacks plugin.
168
169 You can then use a url similar to:
170         http://some.host/weblog/index.html?recent=5
171 to locate posts with comments written in the last 5 days.  You can also create links on your site to make this easier for your visitors.
172
173 You are also welcome to change the $switch_word variable to whatever you like to fit your site.
174
175 =head1 BUGS
176
177 None known; please send bug reports and feedback to the Blosxom
178 development mailing list <blosxom-devel@lists.sourceforge.net>.
179
180 =head1 AUTHOR
181
182 Fletcher T. Penney - http://fletcher.freeshell.org
183
184 This plugin is now maintained by the Blosxom Sourceforge Team,
185 <blosxom-devel@lists.sourceforge.net>.
186
187 =head1 LICENSE
188
189 This source is submitted to the public domain.  Feel free to use and modify it.  If you like, a comment in your modified source attributing credit for my original work would be appreciated.
190
191 THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY WARRANTY OF ANY KIND.  USE AT YOUR OWN RISK!