lib/PurpleWiki/Search/Blosxom.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 94 CC 8 SCAR 25.5
CC
8
Cov
0%
search
20.0
_fileToUrl
20.0
_parseBlosxomFile
6.0
CRAP 12.9
1 # PurpleWiki::Search::Blosxom.pm
2 #
3 # $Id: Blosxom.pm 364 2004-05-19 18:15:26Z eekim $
4 #
5 # Copyright (c) Blue Oxen Associates 2002-2004. All rights reserved.
6 #
7 # This file is part of PurpleWiki. PurpleWiki is derived from:
8 #
9 # UseModWiki v0.92 (c) Clifford A. Adams 2000-2001
10 # AtisWiki v0.3 (c) Markus Denker 1998
11 # CVWiki CVS-patches (c) Peter Merel 1997
12 # The Original WikiWikiWeb (c) Ward Cunningham
13 #
14 # PurpleWiki is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the
26 # Free Software Foundation, Inc.
27 # 59 Temple Place, Suite 330
28 # Boston, MA 02111-1307 USA
29  
30 package PurpleWiki::Search::Blosxom;
31  
32 use strict;
33 use base 'PurpleWiki::Search::Interface';
34 use PurpleWiki::Search::Result;
35 use IO::File;
36  
37 our $VERSION;
38 $VERSION = sprintf("%d", q$Id: Blosxom.pm 364 2004-05-19 18:15:26Z eekim $ =~ /\s(\d+)\s/);
39  
40 my $ENTRIES_CACHE_INDEX = '/home/eekim/www/local/blosxom/plugins/state/.entries_cache.index';
41 my $DATA_DIR = '/home/eekim/writing/blog/eekim.com';
42 my $URL = 'http://localhost:8080/cgi-bin/blosxom';
43 my $DEFAULT_FLAVOUR = 'html';
44  
45 my $COOL_URI = 1;
46  
47 sub search {
48 my $self = shift;
49 my $query = shift;
50 my %files;
51 my @results;
52  
53 my $fh = new IO::File $ENTRIES_CACHE_INDEX;
54 if (defined $fh) {
55 while (my $line = <$fh>) {
56 if ($line =~ /\s*'?(.*?)'?\s*=>\s*(\d*),?/) {
57 $files{$1} = $2;
58 }
59 }
60 $fh->close;
61 }
62 foreach my $file (sort {$files{$b} <=> $files{$a}} keys %files) {
63 my ($title, $body) = &_parseBlosxomFile($file);
64 if ($title =~ /$query/i || $body =~ /$query/i) {
65 my $result = new PurpleWiki::Search::Result;
66 $result->title($title);
67 $result->modifiedTime($files{$file});
68 $result->url(&_fileToUrl($file, $files{$file}));
69 push @results, $result;
70 }
71 }
72 return @results;
73 }
74  
75 sub _parseBlosxomFile {
76 my $fname = shift;
77 my ($title, $body);
78  
79 my $fh = new IO::File $fname;
80 if (defined $fh) {
81 $title = <$fh>;
82 local $/ = undef;
83 $body = <$fh>;
84 $fh->close;
85 }
86 return ($title, $body);
87 }
88  
89 sub _fileToUrl {
90 my ($fname, $ts) = @_;
91  
92 if ($COOL_URI) {
93 $fname =~ s/^.*\///;
94 $fname =~ s/\.txt$//;
95 my @datetime = localtime($ts);
96 my $year = 1900 + $datetime[5];
97 my $month = 1 + $datetime[4];
98 my $day = $datetime[3];
99 $month = "0$month" if ($month < 10);
100 $day = "0$day" if ($day < 10);
101 return "$URL/$year" . '/' . $month . '/' . $day . "/$fname";
102 }
103 else {
104 $fname =~ s/^$DATA_DIR\/*//;
105 $fname =~ s/\.txt$/\.$DEFAULT_FLAVOUR/;
106 $URL =~ s/\/$//;
107 return "$URL/$fname";
108 }
109 }
110  
111  
112 1;
113 __END__