summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/goto.pm
blob: 439552f62f9d5b3fe97c33d8397a559118e25b71 (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,
  44. "404 Not Found",
  45. IkiWiki::misctemplate(gettext("missing page"),
  46. "<p>".
  47. sprintf(gettext("The page %s does not exist."),
  48. htmllink("", "", $page)).
  49. "</p>")
  50. )
  51. }
  52. else {
  53. IkiWiki::redirect($q, urlto($link, undef, 1));
  54. }
  55. exit;
  56. }
  57. sub cgi ($) {
  58. my $cgi=shift;
  59. my $do = $cgi->param('do');
  60. if (defined $do && ($do eq 'goto' || $do eq 'commenter' ||
  61. $do eq 'recentchanges_link')) {
  62. # goto is the preferred name for this; recentchanges_link and
  63. # commenter are for compatibility with any saved URLs
  64. cgi_goto($cgi);
  65. }
  66. }
  67. 1;