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