Bugfix on closing file and clarified documentation
[matthijs/upstream/blosxom-plugins.git] / general / entriescache
1 # Blosxom Plugin: entries_cache -*- perl -*-
2 # Author(s): Fletcher T. Penney <http://fletcher.freeshell.org/> 
3 # Modified by Steve Schwarz <http://agilitynerd.com/>
4 # Version: 0.92
5 # Based on entries_index by Rael Dornfest
6 # Documentation help contributed by Iain Cheyne
7
8 package entriescache;
9
10 # --- Configurable variables -----
11
12 $delay = 0;             # How many minutes delay before entries are re-indexed?
13                         # Set to 0 to force reindexing everytime - this
14                         # will provide the same behavior as Rael's
15                         # entries_index.  Though, I am not sure why one 
16                         # would do this.  At least use a small cache time to
17                         # improve performance....
18                         # SAS - I set this to zero so comments and new files
19                         # are immediately visible.
20 $indexname = "$blosxom::plugin_state_dir/.entries_cache.index";
21 $others_indexname = "$blosxom::plugin_state_dir/.entries_cache.others";
22
23
24 $use_date_tags = 1;     # Set to 1 to enable parsing meta- keywords
25                         # for date tags
26
27 $use_UK_dates = 0;      # Default is mm/dd/yy (US)
28                         # Set to 1 to use dd/mm/yy (UK)
29
30 $update_meta_date = 0;          # Add a meta_date tag if it doesn't exist
31         # This will require that perl has write access to your story files
32         # and that you are using the meta plugin.
33         
34         # NOTE: As of version 0.91, you do not NEED the meta plugin...
35
36         # Be sure to save your text files with UNIX line endings
37         # (any decent text editor should be able to do this) or use the blok
38         # plugin (http://www.enilnomi.net/download.html#blok).
39         # There is some logic in entries_cache to fix this, but do not rely
40         # on it.
41
42         # Lastly, make sure there is at least one blank line between the
43         # entry title and the body. Again, there is some logic in
44         # entries_cache to fix this, but do not rely on it.
45         
46         # The default meta-keywords are compatible with Eric Sherman's
47         # entries_index_tagged defaults:
48         
49         # http://primitiveworker.org/blo.g/development/blosxom/entries_index_tagged/
50
51 $meta_timestamp = "meta-creation_timestamp:" unless defined $meta_timestamp;
52         # timestamp_tag is the non-human readable date stamp format
53         # used by entries_index, entries_cache, entries_index_tagged,
54         # and blosxom
55
56 $meta_date = "meta-creation_date:" unless defined $meta_date;
57         # date_tag is a human readable version
58
59 $strip_meta_dates = 1;          # Strip meta-tags from story so that 
60         # they are not displayed.  Unnecessary if you are running the 
61         # meta plugin.
62
63 $debug = 0;     # Debugging flag
64
65 # --------------------------------
66
67 use File::stat;
68 use File::Find;
69 use CGI qw/:standard/;
70 use Time::Local;
71
72 my $time = time();
73 my $reindex = 0;
74
75 sub start {
76         # Force a reindex
77         $reindex = 1 if (CGI::param('reindex'));
78         return 1;
79 }
80
81
82 sub entries {
83         
84         return sub {
85                 my(%files, %indexes, %others);
86         
87                 # Read cached index
88                 if ( open CACHE, $indexname) {
89                         while ($line = <CACHE>) {
90                                 # Improved backwards compatibility with entries_index
91                                 if ($line =~ /\s*'?(.*?)'?\s*=>\s*(\d*),?/) {
92                                         $files{$1} = $2;
93                                 }
94                         }
95                         close CACHE;
96                         # See if it's time to reindex
97                         $reindex = 1 if ( stat($indexname)->mtime lt ($time - $delay*60) );
98                 } else {
99                         # No index found, so we need to index
100                         $reindex = 1;
101                 }
102
103                 # Read cached others index
104                 if ( open CACHE, $others_indexname) {
105                         while ($line = <CACHE>) {
106                                 # Improved backwards compatibility with entries_index
107                                 if ($line =~ /\s*'?(.*)'?\s*=>\s*(.*),?/) {
108                                         $others{$1} = $2;
109                                 }
110                         }
111                         close CACHE;
112                         # See if it's time to reindex
113                         $reindex = 1 if ( stat($others_indexname)->mtime lt ($time - $delay*60) );
114                 } else {
115                         # No index found, so we need to index
116                         $reindex = 1;
117                 }
118
119
120                 # Perform reindexing if necessary
121                 # This code was originally copied from entries_index by Rael Dornfest
122                 # Check to see if previously indexed files exist, and then rescan
123                 # the datadir for any new files, while preserving the old times
124
125                 # Static mode requires some of the code in this section, and the
126                 # speed hit is unimportant for static blogs
127                 
128                 if ($blosxom::static_or_dynamic eq "static") {
129                         $reindex = 1;
130                 }
131
132
133                 if ($reindex eq 1) {
134                         
135                         # If any files not available, err on side of caution and reindex
136                         for my $file (keys %files) { -f $file or do { $reindex++; delete $files{$file} }; }
137                         for my $other (keys %others) { -f $other or do { $reindex++; delete $others{$other} }; }
138
139
140                         find(
141                                 sub {
142                                         my $d; 
143                                         my $curr_depth = $File::Find::dir =~ tr[/][]; 
144
145                                         #       if ( $blosxom::depth and $curr_depth > $blosxom::depth ) {
146                                         #       # We are beyond depth, so remove files
147                                         #       $files{$File::Find::name} and delete $files{$File::Find::name};
148                                         #       $others{$File::Find::name} and delete $others{$File::Find::name};
149                                         #       return;
150                                         #}
151     
152                                 # Adding support for %others
153                                 if (
154                                         $File::Find::name =~ 
155                                                 m!^$blosxom::datadir/(?:(.*)/)?(.+)\.$blosxom::file_extension$!
156                                                 and $2 ne 'index' and $2 !~ /^\./ and (-r $File::Find::name)
157                                         ) {
158                                     # SAS modified so if meta-creation_date or file timestamp are in the future
159                                     # and we don't want future entries the file won't be included in the file list
160                                     # the index list
161                                     if (!$blosxom::show_future_entries and 
162                                         ((extract_date($File::Find::name, $files{$File::Find::name}) > time) or
163                                          (stat($File::Find::name)->mtime > time))) {
164                                         $files{$File::Find::name} and delete $files{$File::Find::name};
165                                         $others{$File::Find::name} and delete $others{$File::Find::name};
166                                     } else {
167                                                 ( $files{$File::Find::name} || ++$reindex )
168                                                 and ( $files{$File::Find::name} = 
169                                                         extract_date($File::Find::name,$files{$File::Find::name}) ||
170                                                         $files{$File::Find::name} ||
171                                                         stat($File::Find::name)->mtime )
172                                                  
173                                                 # Static
174                                                 and (
175                                                         param('-all') 
176                                                         or !-f "$blosxom::static_dir/$1/index." . $blosxom::static_flavours[0]
177 #                                                       or stat("$blosxom::static_dir/$1/index." . $blosxom::static_flavours[0])->mtime < stat($File::Find::name)->mtime
178 # Trying to fix for static mode
179 # Barijaona's note : you may uncomment the above instruction if you want to be able to fix typos in static rendering
180                                                         or stat("$blosxom::static_dir/$1/index." . $blosxom::static_flavours[0])->mtime < $files{$File::Find::name}
181                                                 )
182                                                 and $indexes{$1} = 1
183                                                 and $d = join('/', (blosxom::nice_date($files{$File::Find::name}))[5,2,3])
184                                                 and $indexes{$d} = $d
185                                                 and $blosxom::static_entries and $indexes{ ($1 ? "$1/" : '') . "$2.$blosxom::file_extension" } = 1;
186                                             }
187                                         } else {
188                                                 !-d $File::Find::name and -r $File::Find::name and $others{$File::Find::name} = stat($File::Find::name)->mtime
189                                         }
190                                 }, $blosxom::datadir
191                         );
192
193
194                         if ( $reindex ) {
195                                 # The index was recreated, so we should record the new version
196                                 if ( open ENTRIES, "> $indexname" ) {
197                                         foreach (sort keys %files) {
198                                                 print ENTRIES "$_=>$files{$_}\n";
199                                         }
200                                         close ENTRIES;
201                                 } else {
202                                         warn "couldn't > $indexname: $!\n";
203                                 }
204
205                                 if ( open ENTRIES, "> $others_indexname" ) {
206                                         foreach (sort keys %others) {
207                                                 print ENTRIES "$_=>$others{$_}\n";
208                                         }
209                                         close ENTRIES;
210                                 } else {
211                                         warn "couldn't > $others_indexname: $!\n";
212                                 }
213                         }
214                 }
215         return (\%files, \%indexes, \%others);
216         }
217 }
218  
219  
220 sub extract_date {
221         my ($file, $indexed_date) = @_;
222         my $new_story = "";
223         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
224         
225         warn "Entries_Cache: Checking $file for meta-tag\n\tComparing to $indexed_date\n" if ($debug == 1);
226         
227         # This is an attempt for compatibility with Eric Sherman's entries_index_tagged
228         # But it does not handle as many date formats, as there are too many additional
229         # necessary modules that I am not willing to require
230         
231         if ( $use_date_tags != 0) {
232         
233                 open (FILE, $file);
234                 $line = <FILE>;         # Read first line ( ie the title)
235                 $new_story .= $line;
236                 
237                 # now, parse the story, and try to correct misformatted stories
238                 while ($line = <FILE>) {
239                         if ($line =~ /^$meta_timestamp\s*(\d+)/) {
240                                 # If present, this format is used
241                                 close File;
242                                 $result = $1;
243                                 warn "Entries_Cache: Found meta_timestamp $result for $file\n" if ($debug == 1);
244                                 return $result;
245                         }
246                         
247                         if ($line =~ /^$meta_date\s*(.*)/) {
248                                 close File;
249                                 $result = $1;
250                                 warn "Entries_Cache: Found meta-date $result for $file\n" if ($debug == 1);
251                                 return parsedate($result);
252                         }
253                         
254                         if ( $line !~ /^meta.*?:/i) {
255                                 # line doesn't start with meta...  (ie story was not formatted
256                                 # for meta-tags), or the meta-tags are finished
257                                 
258                                 if ($update_meta_date eq 1) {
259                                         # Don't mess with stories unless using UNIX line endings.
260                                         
261                                         if (($line =~ /\r/) || ($new_story =~ /\r/)) {
262                                                 warn "Entries_Cache: File $file has non-UNIX line endings; cannot update metatags...\n";
263                                                 close FILE;
264                                                 return 0;
265                                         }
266                                         
267                                         warn "Entries_Cache: Updating meta-tag for $file\n" if ($debug == 1);
268                                         if ($indexed_date eq 0 || $indexed_date == "") {
269                                                 $indexed_date = stat($file)->mtime;
270                                                 warn "Entries_Cache: No date for $file, using $indexed_date\n" if ($debug == 1);
271                                         }
272                                         
273                                         ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($indexed_date);
274                                         $year += 1900;
275                                         $mon += 1;
276                                         $hour = sprintf("%02d",$hour);
277                                         $min = sprintf("%02d",$min);
278                                         $sec = sprintf("%02d",$sec);
279
280                                         warn "Entries_Cache: Adding meta-tag to $file, using $meta_date $mday/$mon/$year $hour:$min:$sec\n" if ($debug == 1);
281                                 
282                                         if ($use_UK_dates eq 1) {
283                                                 $new_story .= "$meta_date $mday/$mon/$year $hour:$min:$sec\n\n";
284                                         } else {
285                                                 $new_story .= "$meta_date $mon/$mday/$year $hour:$min:$sec\n\n";
286                                         }
287                                         
288                                         if ( $line !~ /^\s*$/) {
289                                                 # If this line wasn't empty, then add it to story
290                                                 $new_story .= $line;
291                                         }
292                                         
293                                         while ($line = <FILE>) {
294                                                 # read remainder of story
295                                                 $new_story .= $line;
296                                         }
297                                         
298                                         close FILE;
299                                         open (FILE, "> $file") or warn "Unable to update date meta-tag on $file\n";
300                                         print FILE $new_story;
301                                         close FILE;
302                                         return 0;
303                                 } else {
304                                         close FILE;
305                                         return 0;
306                                 }
307                         }
308
309                         $new_story .= $line;
310                 }
311         }
312         return 0;       
313 }
314
315 sub parsedate {
316         my ($datestring) = @_;
317         #warn "Parsing $datestring\n";
318         
319         # Possible formatting
320         # Month can be 3 letter abbreviation or full name (in English)
321         # Time must be hh:mm or hh:mm:ss  in 24 hour format
322         # Year must be yyyy
323         # The remaining 1 or 2 digits are treated as date
324         # ie: May 25 2003 18:40 
325         # order is not important as long as pieces are there
326                 
327         # Convert the datestring to a time() format
328
329         # Find "Shorthand" Date
330         if ( $datestring =~ /\d\d?\/\d\d?\/\d\d\d?\d?/) {
331                 if ( $use_UK_dates eq 0) {
332                         # Use US Formatting
333                         $datestring =~ s/(\d\d?)\/(\d\d?)\/(\d\d\d?\d?)//;
334                         $mon = $1 - 1;
335                         $day = $2;
336                         $year = $3;
337                 } else {
338                         # Use UK Formatting
339                         $datestring =~ s/(\d\d?)\/(\d\d?)\/(\d\d\d?\d?)//;
340                         $mon = $2 - 1;
341                         $day = $1;
342                         $year = $3;
343                 }
344                 
345                 # Now, clean up year if 2 digit
346                 # You may change the 70 to whatever cutoff you like
347                 $year += 2000 if ($year < 70 );
348                 $year += 1900 if ($year < 100);
349         }
350         
351         # Find Month
352         $mon = 0 if ($datestring =~ s/(Jan|January)//i);
353         $mon = 1 if ($datestring =~ s/(Feb|February)//i);
354         $mon = 2 if ($datestring =~ s/(Mar|March)//i);
355         $mon = 3 if ($datestring =~ s/(Apr|April)//i);
356         $mon = 4 if ($datestring =~ s/(May)//i);
357         $mon = 5 if ($datestring =~ s/(Jun|June)//i);
358         $mon = 6 if ($datestring =~ s/(Jul|July)//i);
359         $mon = 7 if ($datestring =~ s/(Aug|August)//i);
360         $mon = 8 if ($datestring =~ s/(Sep|September)//i);
361         $mon = 9 if ($datestring =~ s/(Oct|October)//i);
362         $mon = 10 if ($datestring =~ s/(Nov|November)//i);
363         $mon = 11 if ($datestring =~ s/(Dec|December)//i);
364
365         # Find Time
366         if ($datestring =~ s/(\d\d?):(\d\d)(:\d\d)?//) {
367                 $hour = $1;
368                 $min = $2;
369                 $sec = $3;
370         }
371         
372         if ($datestring =~ s/(\d\d\d\d)//) {
373                 $year = $1;
374         }
375         
376         if ($datestring =~ s/(\d\d?)//) {
377                 $day = $1;
378         }
379         
380         return timelocal($sec,$min,$hour,$day,$mon,$year);
381         
382 }
383
384 sub story {
385         return 1 if (! $strip_meta_dates);
386         # This code based on Rael's meta plugin
387         my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
388         
389         # Strip date-based meta tags from body
390         $$body_ref =~ s/^$meta_timestamp.*//g;
391         $$body_ref =~ s/^$meta_date.*//g;
392         
393         
394         return 1;
395 }
396
397 1;
398
399 __END__
400
401 =head1 NAME
402
403 Blosxom Plug-in: entries_cache
404
405 =head1 SYNOPSIS
406
407 Purpose: This plugin reserves original creation timestamp on weblog 
408 entries, allowing for editing of entries without altering the original 
409 creation time. It maintains an index ($blosxom::plugin_state_dir/
410 .entries_cache.index) of filenames and their creation times. It also adds 
411 new entries to the index the first time Blosxom come across them. In 
412 addition, it is possible to rely on entries_cache inserting date-based meta 
413 tags automatically into entries - this is enabled by the $use_date_tags 
414 setting. This makes the entries more portable. Note that if this approach 
415 is used, the meta plugin is required: http://www.blosxom.com/downloads/
416 plugins/meta.  (NOTE: As of version 0.91 you can use this without the meta
417 plugin.)
418
419 Replaces the default $blosxom::entries subroutine
420
421 The entries_cache plugin is a "souped-up" version of the entries_index 
422 plugin.  It maintains file modification times in the same manner as the 
423 original plugin, but goes one-step further. It uses the modification time 
424 of the index file to determine whether to rescan the datadir.  If $delay 
425 minutes have not passed, it relies on the cached information.
426
427 You can force a manual scan by appending ?reindex=y to the end of your base 
428 url.
429
430 The reason for this change is that the original blosxom and the 
431 entries_index plugin rescan the datadir each time a page is viewed. This 
432 plugin allows you to cache the information to speed up processing times on 
433 most page views.  According to several posts on the blosxom mailing list, 
434 this is one of the big processor hogs. With a $delay setting of 60 minutes, 
435 there will only be one page view each hour that has to wait for the full 
436 directory scan. To be honest, I have not noticed much of a speed boost 
437 during my testing yet, but I imagine it would only appear for sites with a 
438 large number of files to be indexed.
439
440
441 =head1 VERSION
442
443 0.92
444
445 =head1 VERSION HISTORY
446 0.92    Checks if meta-creation_date is in the future and only displays
447         entry if $show_future_entries is set. Otherwise future articles
448         (by meta-creation_date or file modified time) are not displayed.
449         Don't think this breaks static rendering... haven't tested it.
450         
451 0.91    Improved documentation courtesy of Iain Cheyne - THANKS!!!
452                 Now, if you are not running the meta plugin, you will not see any meta tags in your stories
453
454 0.9             Added parser that detects stories that are not properly formatted for meta-tags and reformats them so that they are.
455                 Additionally, it will not update files that have improper line endings ( ie non-UNIX endings).
456  
457 0.8             Fixed typo that caused index to be rebuilt every time...  :)
458
459 0.7             Major revisions - fixed the "Year 1900" bug and an issue with statically generated blogs misbehaving
460
461 0.61    Fixed bug reading old styled cache files
462
463 0.6             Added feature to automatically create meta-tags from indexed
464                                 time/date
465                                 
466 0.52    Fixed a bug where a new index might not be written
467
468 0.51    Added dd/mm/yy(yy) and mm/dd/yy(yy) date formatting
469
470 0.5             Complete rewrite - add support for %others, meta- tags, added backwards compatibility to using the .entries_index.index file from rael's plugin
471
472 0.2             Removed reliance on Data::Dumper, making it suitable for use on Earthlink
473
474 =head1 AUTHOR
475
476 Fletcher T. Penney
477 Modified by Steve Schwarz
478 based on original code by:
479 Rael Dornfest  <rael@oreilly.com>, http://www.raelity.org/
480
481 This plugin is now maintained by the Blosxom Sourceforge Team,
482 <blosxom-devel@lists.sourceforge.net>.
483
484 =head1 SEE ALSO
485
486 Blosxom Home/Docs/Licensing: http://blosxom.sourceforge.net/
487
488 Blosxom Plugin Docs: http://blosxom.sourceforge.net/documentation/users/plugins.html
489
490 =head1 BUGS
491
492 None known; please send bug reports and feedback to the Blosxom
493 development mailing list <blosxom-devel@lists.sourceforge.net>.
494
495 =head1 LICENSE
496
497 entries_cache plugin
498 Copyright 2003, Fletcher Penney
499 except for portions copied from entries_index and entries_index_tagged
500
501 Blosxom and original entries_index plugin
502 Copyright 2003, Rael Dornfest 
503
504 Permission is hereby granted, free of charge, to any person obtaining a
505 copy of this software and associated documentation files (the "Software"),
506 to deal in the Software without restriction, including without limitation
507 the rights to use, copy, modify, merge, publish, distribute, sublicense,
508 and/or sell copies of the Software, and to permit persons to whom the
509 Software is furnished to do so, subject to the following conditions:
510
511 The above copyright notice and this permission notice shall be included
512 in all copies or substantial portions of the Software.
513
514 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
515 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
516 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
517 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
518 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
519 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
520 OTHER DEALINGS IN THE SOFTWARE.