summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/goto.pm
blob: 3f40c5859f3d22f7d7c1fc7dfb8d9398e8a7249f (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::goto;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 3.00;
  6. sub import {
  7. hook(type => "cgi", id => 'goto', call => \&cgi);
  8. }
  9. sub getsetup () {
  10. return
  11. plugin => {
  12. safe => 1,
  13. rebuild => 0,
  14. }
  15. }
  16. # cgi_goto(CGI, [page])
  17. # Redirect to a specified page, or display "not found". If not specified,
  18. # the page param from the CGI object is used.
  19. sub cgi_goto ($;$) {
  20. my $q = shift;
  21. my $page = shift;
  22. if (!defined $page) {
  23. $page = IkiWiki::decode_utf8($q->param("page"));
  24. if (!defined $page) {
  25. error("missing page parameter");
  26. }
  27. }
  28. IkiWiki::loadindex();
  29. # If the page is internal (like a comment), see if it has a
  30. # permalink. Comments do.
  31. if (IkiWiki::isinternal($page) &&
  32. defined $pagestate{$page}{meta}{permalink}) {
  33. IkiWiki::redirect($q, $pagestate{$page}{meta}{permalink});
  34. }
  35. my $link = bestlink("", $page);
  36. if (! length $link) {
  37. IkiWiki::cgi_custom_failure(
  38. $q->header(-status => "404 Not Found"),
  39. IkiWiki::misctemplate(gettext("missing page"),
  40. "<p>".
  41. sprintf(gettext("The page %s does not exist."),
  42. htmllink("", "", $page)).
  43. "</p>")
  44. )
  45. }
  46. else {
  47. IkiWiki::redirect($q, urlto($link, undef, 1));
  48. }
  49. exit;
  50. }
  51. sub cgi ($) {
  52. my $cgi=shift;
  53. my $do = $cgi->param('do');
  54. if (defined $do && ($do eq 'goto' || $do eq 'commenter' ||
  55. $do eq 'recentchanges_link')) {
  56. # goto is the preferred name for this; recentchanges_link and
  57. # commenter are for compatibility with any saved URLs
  58. cgi_goto($cgi);
  59. }
  60. }
  61. 1;