lib/PurpleWiki/Search/Arts.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 96 CC 4 SCAR 18.3
CC
4
Cov
0%
search
12.0
_initRepository
6.0
BEGIN
2.0
CRAP 6.2
1 # PurpleWiki::Search::Arts.pm
2 # vi:ai:sm:et:sw=4:ts=4
3 #
4 # $Id: Arts.pm 364 2004-05-19 18:15:26Z eekim $
5 #
6 # A Search Module for Arts (http://arts.sourceforge.net/) files
7 # that have been formatted as PurpleWiki wikitext.
8 #
9 # FIXME: Arts configuration has serious namespace issues, so there's
10 # some silliness in _initArts to get the proper information.
11 #
12 # Copyright (c) Blue Oxen Associates 2002-2004. All rights reserved.
13 #
14 # This file is part of PurpleWiki. PurpleWiki is derived from:
15 #
16 # UseModWiki v0.92 (c) Clifford A. Adams 2000-2001
17 # AtisWiki v0.3 (c) Markus Denker 1998
18 # CVWiki CVS-patches (c) Peter Merel 1997
19 # The Original WikiWikiWeb (c) Ward Cunningham
20 #
21 # PurpleWiki is free software; you can redistribute it and/or modify
22 # it under the terms of the GNU General Public License as published by
23 # the Free Software Foundation; either version 2 of the License, or
24 # (at your option) any later version.
25 #
26 # This program is distributed in the hope that it will be useful,
27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 # GNU General Public License for more details.
30 #
31 # You should have received a copy of the GNU General Public License
32 # along with this program; if not, write to the
33 # Free Software Foundation, Inc.
34 # 59 Temple Place, Suite 330
35 # Boston, MA 02111-1307 USA
36  
37 package PurpleWiki::Search::Arts;
38  
39 use strict;
40 use base 'PurpleWiki::Search::Interface';
41 use PurpleWiki::Search::Result;
42 use IO::File;
43  
44 our $VERSION;
45 $VERSION = sprintf("%d", q$Id: Arts.pm 364 2004-05-19 18:15:26Z eekim $ =~ /\s(\d+)\s/);
46  
47 my $FILE_MATCH = '\d+\.\d+\.wiki';
48  
49 # Where the searching is done.
50 sub search {
51 my $self = shift;
52 my $query = shift;
53 my @results;
54  
55 $self->_initRepository();
56  
57 # Loop through each of the available repositories, looking
58 # at the files within for matches on the query.
59 #
60 # FIXME: this loop is much too long, it should be broken
61 # down into sub routines.
62 foreach my $repository (sort(keys(%{$self->{repositories}}))) {
63  
64 my $directory = $self->{repositories}{$repository};
65  
66 opendir(DIR, $directory) ||
67 die "unable to open dir $directory: $!\n";
68 my @files = grep(/^$self->{fileMatch}$/, (readdir(DIR)));
69 closedir(DIR);
70  
71 foreach my $file (@files) {
72 my $bodytext;
73 my $title;
74  
75 my $fileref = new IO::File;
76 $fileref->open("$directory/$file") ||
77 die "unable to open file $file: $!\n";
78  
79 # read in the file
80 while (<$fileref>) {
81 $bodytext .= $_;
82 # get the title
83 /\{title\s+([^}]*)\}/i && ($title = $1);
84 }
85  
86 $fileref->close();
87  
88 # look for the query
89 if ($bodytext =~ /$query/is) {
90 # find the nid
91 $bodytext =~ /($query[^{]*){nid\s+(\w+)}/i;
92 my $summary = $1;
93 my $nid = $2;
94 $nid = "#nid$nid" if $nid;
95  
96 # FIXME: inconsistency on need of / at end of dir name
97 my $url = $self->{repositoryConfig}{$repository}{urlprefix} .
98 '/' . $file . $nid;
99  
100 # pack the results
101 my $result = new PurpleWiki::Search::Result();
102 $result->title("$repository: $title");
103 $result->url($url);
104 $result->summary($summary);
105 $result->modifiedTime((stat("$directory/$file"))[9]);
106 push(@results, $result);
107 }
108 }
109 }
110  
111 @results = sort {$b->modifiedTime() <=> $a->modifiedTime()}
112 @results;
113  
114 return @results;
115 }
116  
117 sub _initRepository {
118 my $self = shift;
119  
120 $self->{fileMatch} = $FILE_MATCH;
121  
122 # FIXME: need these to deal with arts.pl broken namespace
123 my ($UPDATERDIR, $UPDATEREXT);
124  
125 # open the arts config file and evaluate it as perl
126 my %config;
127 my $configFile = $self->config()->ArtsDirectory() . 'arts.pl';
128  
129 my $configRef = new IO::File;
130 $configRef->open($configFile) or die "unable to open $configFile: $!";
131 my $configText = join('', $configRef->getlines());
132 $configRef->close();
133  
134 eval $configText;
135  
136 # collect information on the repository locations
137 my %repositories;
138  
139 foreach my $repository (keys(%config)) {
140 if ($config{$repository}{purpleConfig} eq
141 $self->{config}->DataDir()) {
142 $repositories{$repository} = $config{$repository}{repository};
143 }
144 }
145  
146 # FIXME: its redundant to have both of these but I wanted easy
147 # access
148 $self->{repositoryConfig} = \%config;
149 $self->{repositories} = \%repositories;
150  
151 return $self;
152 }
153  
154 1;
155  
156 __END__