summaryrefslogtreecommitdiff
path: root/t/linkify.t
blob: 6dff0a02954a7c61b5d05fc79a994d1486d2e005 (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use Test::More tests => 32;
  5. BEGIN { use_ok("IkiWiki"); }
  6. # Initialize link plugin
  7. %config=IkiWiki::defaultconfig();
  8. IkiWiki::loadplugins();
  9. my $prefix_directives;
  10. sub linkify ($$$$) {
  11. my $lpage=shift;
  12. my $page=shift;
  13. my $content=shift;
  14. my @existing_pages=@{shift()};
  15. # This is what linkify and htmllink need set right now to work.
  16. # This could change, if so, update it..
  17. %IkiWiki::pagecase=();
  18. %links=();
  19. foreach my $p (@existing_pages) {
  20. $IkiWiki::pagecase{lc $p}=$p;
  21. $links{$p}=[];
  22. $renderedfiles{"$p.mdwn"}=[$p];
  23. $destsources{$p}="$p.mdwn";
  24. }
  25. %config=IkiWiki::defaultconfig();
  26. $config{cgiurl}="http://somehost/ikiwiki.cgi";
  27. $config{srcdir}=$config{destdir}="/dev/null"; # placate checkconfig
  28. # currently coded for non usedirs mode (TODO: check both)
  29. $config{usedirs}=0;
  30. $config{prefix_directives}=$prefix_directives;
  31. IkiWiki::checkconfig();
  32. return IkiWiki::linkify($lpage, $page, $content);
  33. }
  34. sub links_to ($$) {
  35. my $link=shift;
  36. my $content=shift;
  37. if ($content =~ m!<a href="[^"]*\Q$link\E[^"]*"\s*[^>]*>!) {
  38. return 1;
  39. }
  40. else {
  41. print STDERR "# expected link to $link in $content\n";
  42. return;
  43. }
  44. }
  45. sub not_links_to ($$) {
  46. my $link=shift;
  47. my $content=shift;
  48. if ($content !~ m!<a href="[^"]*\Q$link\E[^"]*">!) {
  49. return 1;
  50. }
  51. else {
  52. print STDERR "# expected no link to $link in $content\n";
  53. return;
  54. }
  55. }
  56. sub links_text ($$) {
  57. my $text=shift;
  58. my $content=shift;
  59. if ($content =~ m!>\Q$text\E</a>!) {
  60. return 1;
  61. }
  62. else {
  63. print STDERR "# expected link text $text in $content\n";
  64. return;
  65. }
  66. }
  67. # Tests that are the same for both styles of prefix directives.
  68. foreach $prefix_directives (0,1) {
  69. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "bar"])), "ok link");
  70. ok(links_to("bar_baz", linkify("foo", "foo", "link to [[bar_baz]] ok", ["foo", "bar_baz"])), "ok link");
  71. ok(not_links_to("bar", linkify("foo", "foo", "link to \\[[bar]] ok", ["foo", "bar"])), "escaped link");
  72. ok(links_to("page=bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo"])), "broken link");
  73. ok(links_to("bar", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  74. ok(links_to("baz", linkify("foo", "foo", "link to [[baz]] and [[bar]] ok", ["foo", "baz", "bar"])), "dual links");
  75. ok(links_to("bar", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link");
  76. ok(links_text("some page", linkify("foo", "foo", "link to [[some_page|bar]] ok", ["foo", "bar"])), "named link text");
  77. ok(links_text("0", linkify("foo", "foo", "link to [[0|bar]] ok", ["foo", "bar"])), "named link to 0");
  78. ok(links_text("Some long, & complex page name.", linkify("foo", "foo", "link to [[Some_long,_&_complex_page_name.|bar]] ok, and this is not a link]] here", ["foo", "bar"])), "complex named link text");
  79. ok(links_to("foo/bar", linkify("foo/item", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "inline page link");
  80. ok(links_to("bar", linkify("foo", "foo", "link to [[bar]] ok", ["foo", "foo/item", "foo/bar"])), "same except not inline");
  81. ok(links_to("bar#baz", linkify("foo", "foo", "link to [[bar#baz]] ok", ["foo", "bar"])), "anchor link");
  82. }
  83. $prefix_directives=0;
  84. ok(not_links_to("some_page", linkify("foo", "foo", "link to [[some page]] ok", ["foo", "bar", "some_page"])),
  85. "link with whitespace, without prefix_directives");
  86. ok(not_links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])),
  87. "named link, with whitespace, without prefix_directives");
  88. $prefix_directives=1;
  89. ok(links_to("some_page", linkify("foo", "foo", "link to [[some page]] ok", ["foo", "bar", "some_page"])),
  90. "link with whitespace");
  91. ok(links_to("bar", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])),
  92. "named link, with whitespace");
  93. ok(links_text("some page", linkify("foo", "foo", "link to [[some page|bar]] ok", ["foo", "bar"])),
  94. "named link text, with whitespace");
g{amazon_s3_prefix} ||
  • ! defined $config{amazon_s3_prefix}) {
  • $config{amazon_s3_prefix}="wiki/";
  • }
  • } #}}}
  • {
  • my $bucket;
  • sub getbucket { #{{{
  • return $bucket if defined $bucket;
  • open(IN, "<", $config{amazon_s3_key_file}) || error($config{amazon_s3_key_file}.": ".$!);
  • my $key=<IN>;
  • chomp $key;
  • close IN;
  • my $s3=Net::Amazon::S3->new({
  • aws_access_key_id => $config{amazon_s3_key_id},
  • aws_secret_access_key => $key,
  • retry => 1,
  • });
  • # make sure the bucket exists
  • if (exists $config{amazon_s3_location}) {
  • $bucket=$s3->add_bucket({
  • bucket => $config{amazon_s3_bucket},
  • location_constraint => $config{amazon_s3_location},
  • });
  • }
  • else {
  • $bucket=$s3->add_bucket({
  • bucket => $config{amazon_s3_bucket},
  • });
  • }
  • if (! $bucket) {
  • error(gettext("Failed to create bucket in S3: ").
  • $s3->err.": ".$s3->errstr."\n");
  • }
  • return $bucket;
  • } #}}}
  • }
  • # Given a file, return any S3 keys associated with it.
  • sub file2keys ($) { #{{{
  • my $file=shift;
  • my @keys;
  • if ($file =~ /^\Q$config{destdir}\/\E(.*)/) {
  • push @keys, $config{amazon_s3_prefix}.$1;
  • # Munge foo/index.html to foo/
  • if ($keys[0]=~/(^|.*\/)index.$config{htmlext}$/) {
  • # A duplicate might need to be stored under the
  • # unmunged name too.
  • if (!$config{usedirs} || $config{amazon_s3_dupindex}) {
  • push @keys, $1;
  • }
  • else {
  • @keys=($1);
  • }
  • }
  • }
  • return @keys;
  • } #}}}
  • package IkiWiki;
  • use File::MimeInfo;
  • use Encode;
  • # This is a wrapper around the real writefile.
  • sub writefile ($$$;$$) { #{{{
  • my $file=shift;
  • my $destdir=shift;
  • my $content=shift;
  • my $binary=shift;
  • my $writer=shift;
  • # First, write the file to disk.
  • my $ret=$IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::writefile'}->($file, $destdir, $content, $binary, $writer);
  • my @keys=IkiWiki::Plugin::amazon_s3::file2keys("$destdir/$file");
  • # Store the data in S3.
  • if (@keys) {
  • my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
  • # The http layer tries to downgrade utf-8
  • # content, but that can fail (see
  • # http://rt.cpan.org/Ticket/Display.html?id=35710),
  • # so force convert it to bytes.
  • $content=encode_utf8($content) if defined $content;
  • my %opts=(
  • acl_short => 'public-read',
  • content_type => mimetype("$destdir/$file"),
  • );
  • # If there are multiple keys to write, data is sent
  • # multiple times.
  • # TODO: investigate using the new copy operation.
  • # (It may not be robust enough.)
  • foreach my $key (@keys) {
  • my $res;
  • if (! $writer) {
  • $res=$bucket->add_key($key, $content, \%opts);
  • }
  • else {
  • # This test for empty files is a workaround
  • # for this bug:
  • # http://rt.cpan.org//Ticket/Display.html?id=35731
  • if (-z "$destdir/$file") {
  • $res=$bucket->add_key($key, "", \%opts);
  • }
  • else {
  • # read back in the file that the writer emitted
  • $res=$bucket->add_key_filename($key, "$destdir/$file", \%opts);
  • }
  • }
  • if (! $res) {
  • error(gettext("Failed to save file to S3: ").
  • $bucket->err.": ".$bucket->errstr."\n");
  • }
  • }
  • }
  • return $ret;
  • } #}}}
  • # This is a wrapper around the real prune.
  • sub prune ($) { #{{{
  • my $file=shift;
  • my @keys=IkiWiki::Plugin::amazon_s3::file2keys($file);
  • # Prune files out of S3 too.
  • if (@keys) {
  • my $bucket=IkiWiki::Plugin::amazon_s3::getbucket();
  • foreach my $key (@keys) {
  • my $res=$bucket->delete_key($key);
  • if (! $res) {
  • error(gettext("Failed to delete file from S3: ").
  • $bucket->err.": ".$bucket->errstr."\n");
  • }
  • }
  • }
  • return $IkiWiki::Plugin::amazon_s3::subs{'IkiWiki::prune'}->($file);
  • } #}}}
  • 1