summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/goto.pm
blob: 669211691028b90f748f0d1489e0380a7d97b253 (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. my $link;
  36. if (! IkiWiki::isinternal($page)) {
  37. $link = bestlink("", $page);
  38. }
  39. elsif (defined $pagestate{$page}{meta}{permalink}) {
  40. # Can only redirect to an internal page if it has a
  41. # permalink.
  42. IkiWiki::redirect($q, $pagestate{$page}{meta}{permalink});
  43. }
  44. if (! length $link) {
  45. IkiWiki::cgi_custom_failure(
  46. $q,
  47. "404 Not Found",
  48. IkiWiki::misctemplate(gettext("missing page"),
  49. "<p>".
  50. sprintf(gettext("The page %s does not exist."),
  51. htmllink("", "", $page)).
  52. "</p>")
  53. )
  54. }
  55. else {
  56. IkiWiki::redirect($q, urlto($link, undef, 1));
  57. }
  58. exit;
  59. }
  60. sub cgi ($) {
  61. my $cgi=shift;
  62. my $do = $cgi->param('do');
  63. if (defined $do && ($do eq 'goto' || $do eq 'commenter' ||
  64. $do eq 'recentchanges_link')) {
  65. # goto is the preferred name for this; recentchanges_link and
  66. # commenter are for compatibility with any saved URLs
  67. cgi_goto($cgi);
  68. }
  69. }
  70. 1;