]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/goto.pm
tag patch
[ikiwiki.git] / IkiWiki / Plugin / goto.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::goto;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 sub import {
9         hook(type => "cgi", id => 'goto',  call => \&cgi);
10 }
11
12 sub getsetup () {
13         return
14                 plugin => {
15                         safe => 1,
16                         rebuild => 0,
17                 }
18 }
19
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;
26
27         if (!defined $page) {
28                 $page = IkiWiki::decode_utf8($q->param("page"));
29
30                 if (!defined $page) {
31                         error("missing page parameter");
32                 }
33         }
34
35         IkiWiki::loadindex();
36
37         # If the page is internal (like a comment), see if it has a
38         # permalink. Comments do.
39         if (IkiWiki::isinternal($page) &&
40             defined $pagestate{$page}{meta}{permalink}) {
41                 IkiWiki::redirect($q, $pagestate{$page}{meta}{permalink});
42         }
43
44         my $link = bestlink("", $page);
45
46         if (! length $link) {
47                 IkiWiki::cgi_custom_failure(
48                         $q->header(-status => "404 Not Found"),
49                         IkiWiki::misctemplate(gettext("missing page"),
50                                 "<p>".
51                                 sprintf(gettext("The page %s does not exist."),
52                                         htmllink("", "", $page)).
53                                 "</p>")
54                 )
55         }
56         else {
57                 IkiWiki::redirect($q, urlto($link, undef, 1));
58         }
59
60         exit;
61 }
62
63 sub cgi ($) {
64         my $cgi=shift;
65         my $do = $cgi->param('do');
66
67         if (defined $do && ($do eq 'goto' || $do eq 'commenter' ||
68                                $do eq 'recentchanges_link')) {
69                 # goto is the preferred name for this; recentchanges_link and
70                 # commenter are for compatibility with any saved URLs
71                 cgi_goto($cgi);
72         }
73 }
74
75 1;