]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/goto.pm
fix bugs in `find_src_files()`.
[git.ikiwiki.info.git] / IkiWiki / Plugin / goto.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::goto;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
8 sub import {
9         hook(type => "cgi", id => 'goto',  call => \&cgi);
10 }
12 sub getsetup () {
13         return
14                 plugin => {
15                         safe => 1,
16                         rebuild => 0,
17                 }
18 }
20 # cgi_goto(CGI, [page])
21 # Redirect to a specified page, or display "not found". If not specified,
22 # the page param from the CGI object is used.
23 sub cgi_goto ($;$) {
24         my $q = shift;
25         my $page = shift;
27         if (!defined $page) {
28                 $page = IkiWiki::decode_utf8($q->param("page"));
30                 if (!defined $page) {
31                         error("missing page parameter");
32                 }
33         }
35         # It's possible that $page is not a valid page name;
36         # if so attempt to turn it into one.
37         if ($page !~ /$config{wiki_file_regexp}/) {
38                 $page=titlepage($page);
39         }
41         IkiWiki::loadindex();
43         # If the page is internal (like a comment), see if it has a
44         # permalink. Comments do.
45         if (IkiWiki::isinternal($page) &&
46             defined $pagestate{$page}{meta}{permalink}) {
47                 IkiWiki::redirect($q, $pagestate{$page}{meta}{permalink});
48         }
50         my $link = bestlink("", $page);
52         if (! length $link) {
53                 IkiWiki::cgi_custom_failure(
54                         $q,
55                         "404 Not Found",
56                         IkiWiki::misctemplate(gettext("missing page"),
57                                 "<p>".
58                                 sprintf(gettext("The page %s does not exist."),
59                                         htmllink("", "", $page)).
60                                 "</p>")
61                 )
62         }
63         else {
64                 IkiWiki::redirect($q, urlto($link, undef, 1));
65         }
67         exit;
68 }
70 sub cgi ($) {
71         my $cgi=shift;
72         my $do = $cgi->param('do');
74         if (defined $do && ($do eq 'goto' || $do eq 'commenter' ||
75                                $do eq 'recentchanges_link')) {
76                 # goto is the preferred name for this; recentchanges_link and
77                 # commenter are for compatibility with any saved URLs
78                 cgi_goto($cgi);
79         }
80 }
82 1;