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