]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki.pm
* Add a unit test for globlist_match().
[ikiwiki.git] / IkiWiki.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4 use warnings;
5 use strict;
6 use File::Spec;
7 use HTML::Template;
8
9 use vars qw{%config %links %oldlinks %oldpagemtime %pagectime
10             %renderedfiles %pagesources %depends %plugins};
11
12 sub checkconfig () { #{{{
13         if ($config{cgi} && ! length $config{url}) {
14                 error("Must specify url to wiki with --url when using --cgi\n");
15         }
16         if ($config{rss} && ! length $config{url}) {
17                 error("Must specify url to wiki with --url when using --rss\n");
18         }
19         if ($config{hyperestraier} && ! length $config{url}) {
20                 error("Must specify --url when using --hyperestraier\n");
21         }
22         
23         $config{wikistatedir}="$config{srcdir}/.ikiwiki"
24                 unless exists $config{wikistatedir};
25         
26         if ($config{svn}) {
27                 require IkiWiki::Rcs::SVN;
28                 $config{rcs}=1;
29         }
30         else {
31                 require IkiWiki::Rcs::Stub;
32                 $config{rcs}=0;
33         }
34
35         foreach my $plugin (@{$config{plugin}}) {
36                 my $mod="IkiWiki::Plugin::".possibly_foolish_untaint($plugin);
37                 eval qq{use $mod};
38                 if ($@) {
39                         error("Failed to load plugin $mod: $@");
40                 }
41         }
42 } #}}}
43
44 sub error ($) { #{{{
45         if ($config{cgi}) {
46                 print "Content-type: text/html\n\n";
47                 print misctemplate("Error", "<p>Error: @_</p>");
48         }
49         die @_;
50 } #}}}
51
52 sub debug ($) { #{{{
53         return unless $config{verbose};
54         if (! $config{cgi}) {
55                 print "@_\n";
56         }
57         else {
58                 print STDERR "@_\n";
59         }
60 } #}}}
61
62 sub possibly_foolish_untaint ($) { #{{{
63         my $tainted=shift;
64         my ($untainted)=$tainted=~/(.*)/;
65         return $untainted;
66 } #}}}
67
68 sub basename ($) { #{{{
69         my $file=shift;
70
71         $file=~s!.*/+!!;
72         return $file;
73 } #}}}
74
75 sub dirname ($) { #{{{
76         my $file=shift;
77
78         $file=~s!/*[^/]+$!!;
79         return $file;
80 } #}}}
81
82 sub pagetype ($) { #{{{
83         my $page=shift;
84         
85         if ($page =~ /\.mdwn$/) {
86                 return ".mdwn";
87         }
88         else {
89                 return "unknown";
90         }
91 } #}}}
92
93 sub pagename ($) { #{{{
94         my $file=shift;
95
96         my $type=pagetype($file);
97         my $page=$file;
98         $page=~s/\Q$type\E*$// unless $type eq 'unknown';
99         return $page;
100 } #}}}
101
102 sub htmlpage ($) { #{{{
103         my $page=shift;
104
105         return $page.".html";
106 } #}}}
107
108 sub srcfile ($) { #{{{
109         my $file=shift;
110
111         return "$config{srcdir}/$file" if -e "$config{srcdir}/$file";
112         return "$config{underlaydir}/$file" if -e "$config{underlaydir}/$file";
113         error("internal error: $file cannot be found");
114 } #}}}
115
116 sub readfile ($;$) { #{{{
117         my $file=shift;
118         my $binary=shift;
119
120         if (-l $file) {
121                 error("cannot read a symlink ($file)");
122         }
123         
124         local $/=undef;
125         open (IN, $file) || error("failed to read $file: $!");
126         binmode(IN) if $binary;
127         my $ret=<IN>;
128         close IN;
129         return $ret;
130 } #}}}
131
132 sub writefile ($$$;$) { #{{{
133         my $file=shift; # can include subdirs
134         my $destdir=shift; # directory to put file in
135         my $content=shift;
136         my $binary=shift;
137         
138         my $test=$file;
139         while (length $test) {
140                 if (-l "$destdir/$test") {
141                         error("cannot write to a symlink ($test)");
142                 }
143                 $test=dirname($test);
144         }
145
146         my $dir=dirname("$destdir/$file");
147         if (! -d $dir) {
148                 my $d="";
149                 foreach my $s (split(m!/+!, $dir)) {
150                         $d.="$s/";
151                         if (! -d $d) {
152                                 mkdir($d) || error("failed to create directory $d: $!");
153                         }
154                 }
155         }
156         
157         open (OUT, ">$destdir/$file") || error("failed to write $destdir/$file: $!");
158         binmode(OUT) if $binary;
159         print OUT $content;
160         close OUT;
161 } #}}}
162
163 sub bestlink ($$) { #{{{
164         # Given a page and the text of a link on the page, determine which
165         # existing page that link best points to. Prefers pages under a
166         # subdirectory with the same name as the source page, failing that
167         # goes down the directory tree to the base looking for matching
168         # pages.
169         my $page=shift;
170         my $link=lc(shift);
171         
172         my $cwd=$page;
173         do {
174                 my $l=$cwd;
175                 $l.="/" if length $l;
176                 $l.=$link;
177
178                 if (exists $links{$l}) {
179                         #debug("for $page, \"$link\", use $l");
180                         return $l;
181                 }
182         } while $cwd=~s!/?[^/]+$!!;
183
184         #print STDERR "warning: page $page, broken link: $link\n";
185         return "";
186 } #}}}
187
188 sub isinlinableimage ($) { #{{{
189         my $file=shift;
190         
191         $file=~/\.(png|gif|jpg|jpeg)$/i;
192 } #}}}
193
194 sub pagetitle ($) { #{{{
195         my $page=shift;
196         $page=~s/__(\d+)__/&#$1;/g;
197         $page=~y/_/ /;
198         return $page;
199 } #}}}
200
201 sub titlepage ($) { #{{{
202         my $title=shift;
203         $title=~y/ /_/;
204         $title=~s/([^-[:alnum:]_:+\/.])/"__".ord($1)."__"/eg;
205         return $title;
206 } #}}}
207
208 sub cgiurl (@) { #{{{
209         my %params=@_;
210
211         return $config{cgiurl}."?".join("&amp;", map "$_=$params{$_}", keys %params);
212 } #}}}
213
214 sub styleurl (;$) { #{{{
215         my $page=shift;
216
217         return "$config{url}/style.css" if ! defined $page;
218         
219         $page=~s/[^\/]+$//;
220         $page=~s/[^\/]+\//..\//g;
221         return $page."style.css";
222 } #}}}
223
224 sub htmllink ($$;$$$) { #{{{
225         my $page=shift;
226         my $link=shift;
227         my $noimageinline=shift; # don't turn links into inline html images
228         my $forcesubpage=shift; # force a link to a subpage
229         my $linktext=shift; # set to force the link text to something
230
231         my $bestlink;
232         if (! $forcesubpage) {
233                 $bestlink=bestlink($page, $link);
234         }
235         else {
236                 $bestlink="$page/".lc($link);
237         }
238
239         $linktext=pagetitle(basename($link)) unless defined $linktext;
240         
241         return $linktext if length $bestlink && $page eq $bestlink;
242         
243         # TODO BUG: %renderedfiles may not have it, if the linked to page
244         # was also added and isn't yet rendered! Note that this bug is
245         # masked by the bug mentioned below that makes all new files
246         # be rendered twice.
247         if (! grep { $_ eq $bestlink } values %renderedfiles) {
248                 $bestlink=htmlpage($bestlink);
249         }
250         if (! grep { $_ eq $bestlink } values %renderedfiles) {
251                 return "<span><a href=\"".
252                         cgiurl(do => "create", page => $link, from =>$page).
253                         "\">?</a>$linktext</span>"
254         }
255         
256         $bestlink=File::Spec->abs2rel($bestlink, dirname($page));
257         
258         if (! $noimageinline && isinlinableimage($bestlink)) {
259                 return "<img src=\"$bestlink\" alt=\"$linktext\" />";
260         }
261         return "<a href=\"$bestlink\">$linktext</a>";
262 } #}}}
263
264 sub indexlink () { #{{{
265         return "<a href=\"$config{url}\">$config{wikiname}</a>";
266 } #}}}
267
268 sub lockwiki () { #{{{
269         # Take an exclusive lock on the wiki to prevent multiple concurrent
270         # run issues. The lock will be dropped on program exit.
271         if (! -d $config{wikistatedir}) {
272                 mkdir($config{wikistatedir});
273         }
274         open(WIKILOCK, ">$config{wikistatedir}/lockfile") ||
275                 error ("cannot write to $config{wikistatedir}/lockfile: $!");
276         if (! flock(WIKILOCK, 2 | 4)) {
277                 debug("wiki seems to be locked, waiting for lock");
278                 my $wait=600; # arbitrary, but don't hang forever to 
279                               # prevent process pileup
280                 for (1..600) {
281                         return if flock(WIKILOCK, 2 | 4);
282                         sleep 1;
283                 }
284                 error("wiki is locked; waited $wait seconds without lock being freed (possible stuck process or stale lock?)");
285         }
286 } #}}}
287
288 sub unlockwiki () { #{{{
289         close WIKILOCK;
290 } #}}}
291
292 sub loadindex () { #{{{
293         open (IN, "$config{wikistatedir}/index") || return;
294         while (<IN>) {
295                 $_=possibly_foolish_untaint($_);
296                 chomp;
297                 my %items;
298                 $items{link}=[];
299                 foreach my $i (split(/ /, $_)) {
300                         my ($item, $val)=split(/=/, $i, 2);
301                         push @{$items{$item}}, $val;
302                 }
303
304                 next unless exists $items{src}; # skip bad lines for now
305
306                 my $page=pagename($items{src}[0]);
307                 if (! $config{rebuild}) {
308                         $pagesources{$page}=$items{src}[0];
309                         $oldpagemtime{$page}=$items{mtime}[0];
310                         $oldlinks{$page}=[@{$items{link}}];
311                         $links{$page}=[@{$items{link}}];
312                         $depends{$page}=join(" ", @{$items{depends}})
313                                 if exists $items{depends};
314                         $renderedfiles{$page}=$items{dest}[0];
315                 }
316                 $pagectime{$page}=$items{ctime}[0];
317         }
318         close IN;
319 } #}}}
320
321 sub saveindex () { #{{{
322         if (! -d $config{wikistatedir}) {
323                 mkdir($config{wikistatedir});
324         }
325         open (OUT, ">$config{wikistatedir}/index") || 
326                 error("cannot write to $config{wikistatedir}/index: $!");
327         foreach my $page (keys %oldpagemtime) {
328                 next unless $oldpagemtime{$page};
329                 my $line="mtime=$oldpagemtime{$page} ".
330                         "ctime=$pagectime{$page} ".
331                         "src=$pagesources{$page} ".
332                         "dest=$renderedfiles{$page}";
333                 $line.=" link=$_" foreach @{$links{$page}};
334                 if (exists $depends{$page}) {
335                         $line.=" depends=$_" foreach split " ", $depends{$page};
336                 }
337                 print OUT $line."\n";
338         }
339         close OUT;
340 } #}}}
341
342 sub misctemplate ($$) { #{{{
343         my $title=shift;
344         my $pagebody=shift;
345         
346         my $template=HTML::Template->new(
347                 filename => "$config{templatedir}/misc.tmpl"
348         );
349         $template->param(
350                 title => $title,
351                 indexlink => indexlink(),
352                 wikiname => $config{wikiname},
353                 pagebody => $pagebody,
354                 styleurl => styleurl(),
355                 baseurl => "$config{url}/",
356         );
357         return $template->output;
358 }#}}}
359
360 sub glob_match ($$) { #{{{
361         my $page=shift;
362         my $glob=shift;
363
364         # turn glob into safe regexp
365         $glob=quotemeta($glob);
366         $glob=~s/\\\*/.*/g;
367         $glob=~s/\\\?/./g;
368         $glob=~s!\\/!/!g;
369         
370         $page=~/^$glob$/i;
371 } #}}}
372
373 sub globlist_match ($$) { #{{{
374         my $page=shift;
375         my @globlist=split(" ", shift);
376
377         # check any negated globs first
378         foreach my $glob (@globlist) {
379                 return 0 if $glob=~/^!(.*)/ && glob_match($page, $1);
380         }
381
382         foreach my $glob (@globlist) {
383                 return 1 if glob_match($page, $glob);
384         }
385         
386         return 0;
387 } #}}}
388
389 sub register_plugin ($$$) { # {{{
390         my $type=shift;
391         my $command=shift;
392         my $function=shift;
393         
394         $plugins{$type}{$command}=$function;
395 } # }}}
396
397 1