summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/goto.pm
blob: 2e2dc04a111a164745fb628785b509d40e024e6c (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. # It's possible that $page is not a valid page name;
  29. # if so attempt to turn it into one.
  30. if ($page !~ /$config{wiki_file_regexp}/) {
  31. $page=titlepage($page);
  32. }
  33. IkiWiki::loadindex();
  34. # If the page is internal (like a comment), see if it has a
  35. # permalink. Comments do.
  36. if (IkiWiki::isinternal($page) &&
  37. defined $pagestate{$page}{meta}{permalink}) {
  38. IkiWiki::redirect($q, $pagestate{$page}{meta}{permalink});
  39. }
  40. my $link = bestlink("", $page);
  41. if (! length $link) {
  42. IkiWiki::cgi_custom_failure(
  43. $q->header(-status => "404 Not Found"),
  44. IkiWiki::misctemplate(gettext("missing page"),
  45. "<p>".
  46. sprintf(gettext("The page %s does not exist."),
  47. htmllink("", "", $page)).
  48. "</p>")
  49. )
  50. }
  51. else {
  52. IkiWiki::redirect($q, urlto($link, undef, 1));
  53. }
  54. exit;
  55. }
  56. sub cgi ($) {
  57. my $cgi=shift;
  58. my $do = $cgi->param('do');
  59. if (defined $do && ($do eq 'goto' || $do eq 'commenter' ||
  60. $do eq 'recentchanges_link')) {
  61. # goto is the preferred name for this; recentchanges_link and
  62. # commenter are for compatibility with any saved URLs
  63. cgi_goto($cgi);
  64. }
  65. }
  66. 1;