]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/goto.pm
add section information
[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                         section => "web",
18                 }
19 }
21 # cgi_goto(CGI, [page])
22 # Redirect to a specified page, or display "not found". If not specified,
23 # the page param from the CGI object is used.
24 sub cgi_goto ($;$) {
25         my $q = shift;
26         my $page = shift;
28         if (!defined $page) {
29                 $page = IkiWiki::decode_utf8($q->param("page"));
31                 if (!defined $page) {
32                         error("missing page parameter");
33                 }
34         }
36         # It's possible that $page is not a valid page name;
37         # if so attempt to turn it into one.
38         if ($page !~ /$config{wiki_file_regexp}/) {
39                 $page=titlepage($page);
40         }
42         IkiWiki::loadindex();
44         # If the page is internal (like a comment), see if it has a
45         # permalink. Comments do.
46         if (IkiWiki::isinternal($page) &&
47             defined $pagestate{$page}{meta}{permalink}) {
48                 IkiWiki::redirect($q, $pagestate{$page}{meta}{permalink});
49         }
51         my $link = bestlink("", $page);
53         if (! length $link) {
54                 IkiWiki::cgi_custom_failure(
55                         $q,
56                         "404 Not Found",
57                         IkiWiki::misctemplate(gettext("missing page"),
58                                 "<p>".
59                                 sprintf(gettext("The page %s does not exist."),
60                                         htmllink("", "", $page)).
61                                 "</p>")
62                 )
63         }
64         else {
65                 IkiWiki::redirect($q, urlto($link, undef, 1));
66         }
68         exit;
69 }
71 sub cgi ($) {
72         my $cgi=shift;
73         my $do = $cgi->param('do');
75         if (defined $do && ($do eq 'goto' || $do eq 'commenter' ||
76                                $do eq 'recentchanges_link')) {
77                 # goto is the preferred name for this; recentchanges_link and
78                 # commenter are for compatibility with any saved URLs
79                 cgi_goto($cgi);
80         }
81 }
83 1;