enclosure.pl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (C) 2007 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('enclosure.pl', 'Podcasting');
  18. our (%Page, %IndexHash, @MyRules, $FreeLinkPattern);
  19. our (@Enclosures);
  20. push(@MyRules, \&EnclosureRule);
  21. # [[enclosure:page name]]
  22. # [[enclosure:page name|alt text]]
  23. # [[enclosure:url|size in bytes|mime type]]
  24. sub EnclosureRule {
  25. if (m!\G\[\[enclosure:\s*$FreeLinkPattern(\|([^\]]+))?\]\]!cgi) {
  26. my $id = FreeToNormal($1);
  27. # Make sure we don't add duplicates; we will add non-existing
  28. # enclosures as well. We test for existence only when the RSS feed
  29. # is being produced.
  30. my %enclosures = map { $_ => 1 } split(' ', $Page{enclosures});
  31. $enclosures{$id} = 1;
  32. $Page{enclosures} = join(' ', keys %enclosures);
  33. return GetDownloadLink($id, undef, undef, $3);
  34. }
  35. return;
  36. }
  37. *OldEnclosurePrintWikiToHTML = \&PrintWikiToHTML;
  38. *PrintWikiToHTML = \&NewEnclosurePrintWikiToHTML;
  39. sub NewEnclosurePrintWikiToHTML {
  40. $Page{enclosures} = '';
  41. return OldEnclosurePrintWikiToHTML(@_);
  42. }
  43. *OldEnclosureRssItem = \&RssItem;
  44. *RssItem = \&NewEnclosureRssItem;
  45. sub NewEnclosureRssItem {
  46. my $id = shift;
  47. my $rss = OldEnclosureRssItem($id, @_);
  48. require MIME::Base64;
  49. my $data = ParseData(ReadFileOrDie(GetPageFile($id)));
  50. my @enclosures = split(' ', $data->{enclosures});
  51. my $enclosures = '';
  52. foreach my $enclosure (@enclosures) {
  53. # Don't add the enclosure if the page has been deleted in the mean
  54. # time (or never existed in the first place).
  55. next unless $IndexHash{$enclosure};
  56. my $url = GetDownloadLink($enclosure, 2); # just the URL
  57. my %item = ParseData(ReadFileOrDie(GetPageFile($enclosure)));
  58. my ($type) = TextIsFile($item{text});
  59. my ($data) = $item{text} =~ /^[^\n]*\n(.*)/s;
  60. my $size = length(MIME::Base64::decode($data));
  61. $enclosures .= qq{<enclosure url="$url" length="$size" type="$type" />\n};
  62. }
  63. $rss =~ s!</item>$!$enclosures</item>! if $enclosures;
  64. return $rss;
  65. }