journal-rss.pl 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2004–2014 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('journal-rss.pl', 'Journal RSS Extension');
  18. our ($OpenPageName, $CollectingJournal, %Page, %Action, @MyInitVariables, $DeletedPage, %NearLinksException);
  19. $Action{journal} = \&DoJournalRss;
  20. # Currently RSS works like RecentChanges, which is not what bloggers
  21. # expect. Produce a RSS feed that mimicks exactly how the journal tag
  22. # works.
  23. sub DoJournalRss {
  24. return if $CollectingJournal; # avoid infinite loops
  25. local $CollectingJournal = 1;
  26. # Fake the result of GetRcLines()
  27. local *GetRcLines = \&JournalRssGetRcLines;
  28. print GetHttpHeader('application/xml') . GetRcRss();
  29. }
  30. sub JournalRssGetRcLines {
  31. my $num = GetParam('rsslimit', 10);
  32. my $match = GetParam('match', '^\d\d\d\d-\d\d-\d\d');
  33. my $search = GetParam('search', '');
  34. my $reverse = GetParam('reverse', 0);
  35. my $monthly = GetParam('monthly', 0);
  36. my @pages = sort JournalSort (grep(/$match/, $search ? SearchTitleAndBody($search) : AllPagesList()));
  37. if ($monthly and not $match) {
  38. my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime();
  39. $match = '^' . sprintf("%04d-%02d", $year+1900, $mon+1) . '-\d\d';
  40. }
  41. if ($reverse) {
  42. @pages = reverse @pages;
  43. }
  44. # FIXME: Missing 'future' and 'past' keywords.
  45. # FIXME: Do we need 'offset'? I don't think so.
  46. my @result = ();
  47. foreach my $id (@pages) {
  48. # Now save information required for saving the cache of the current page.
  49. local %Page;
  50. local $OpenPageName = '';
  51. OpenPage($id);
  52. # If this is a minor edit, ignore it. Load the last major revision
  53. # instead, if you can.
  54. if ($Page{minor}) {
  55. # Perhaps the old kept revision is gone due to $KeepMajor=0 or
  56. # admin.pl or because a page was created as a minor change and
  57. # never edited. Reading kept revisions in this case results in
  58. # an error.
  59. eval {
  60. %Page = GetKeptRevision($Page{lastmajor});
  61. };
  62. next if $@;
  63. }
  64. next if $Page{text} =~ /^\s*$/; # only whitespace is also to be deleted
  65. next if $DeletedPage && substr($Page{text}, 0, length($DeletedPage))
  66. eq $DeletedPage; # no regexp
  67. # Generate artifical rows in the list to pass to GetRcRss. We need
  68. # to open every single page, because the meta-data ordinarily
  69. # available in the rc.log file is not available to us. This is why
  70. # we observe the rsslimit parameter. Without it, we would have to
  71. # open *all* date pages.
  72. my @languages = split(/,/, $Page{languages});
  73. push (@result, [$Page{ts}, $id, $Page{minor}, $Page{summary}, $Page{host},
  74. $Page{username}, $Page{revision}, \@languages,
  75. GetCluster($Page{text})]);
  76. last if $num ne 'all' and $#result + 1 >= $num;
  77. }
  78. return @result;
  79. }
  80. # Prevent near links from being printed as a result of the search.
  81. push(@MyInitVariables, sub {
  82. $NearLinksException{journal} = 1;
  83. });