journal-rss.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright (C) 2004–2023 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. $RecentLink, $SiteName, $SiteDescription, $ScriptName, $RssRights);
  20. $Action{journal} = \&DoJournalRss;
  21. # Currently RSS works like RecentChanges, which is not what bloggers
  22. # expect. Produce a RSS feed that mimicks exactly how the journal tag
  23. # works.
  24. sub DoJournalRss {
  25. return if $CollectingJournal; # avoid infinite loops
  26. local $CollectingJournal = 1;
  27. # Fake the result of GetRcLines()
  28. local *GetRcLines = \&JournalRssGetRcLines;
  29. local *RcSelfWebsite = \&JournalRssSelfWebsite;
  30. local *RcSelfAction = \&JournalRssSelfAction;
  31. local *RcPreviousAction = \&JournalRssPreviousAction;
  32. local *RcLastAction = \&JournalRssLastAction;
  33. SetParam('full', 1);
  34. if (GetParam('raw', 0)) {
  35. print GetHttpHeader('text/plain');
  36. print RcTextItem('title', $SiteName),
  37. RcTextItem('description', $SiteDescription), RcTextItem('link', $ScriptName),
  38. RcTextItem('generator', 'Oddmuse'), RcTextItem('rights', $RssRights);
  39. ProcessRcLines(sub {}, \&RcTextRevision);
  40. } else {
  41. print GetHttpHeader('application/xml') . GetRcRss();
  42. }
  43. }
  44. sub JournalRssParameters {
  45. my $more = '';
  46. foreach (@_, qw(rsslimit match search reverse monthly)) {
  47. my $val = GetParam($_, '');
  48. $more .= ";$_=" . UrlEncode($val) if $val;
  49. }
  50. return $more;
  51. }
  52. sub JournalRssSelfWebsite {
  53. my $more = '';
  54. my $search = GetParam('rcfilteronly', '');
  55. $more .= ";search=" . UrlEncode($search) if $search;
  56. my $match = GetParam('match', '');
  57. $more .= ";match=" . UrlEncode($match) if $match;
  58. return $more;
  59. }
  60. sub JournalRssSelfAction {
  61. return "action=journal" . JournalRssParameters(qw(offset));
  62. }
  63. sub JournalRssPreviousAction {
  64. my $num = GetParam('rsslimit', 10);
  65. my $offset = GetParam('offset', 0) + $num;
  66. return "action=journal;offset=$offset" . JournalRssParameters();
  67. }
  68. sub JournalRssLastAction {
  69. return "action=journal" . JournalRssParameters();
  70. }
  71. sub JournalRssGetRcLines {
  72. my $num = GetParam('rsslimit', 10);
  73. my $match = GetParam('match', '^\d\d\d\d-\d\d-\d\d');
  74. my $search = GetParam('search', '');
  75. my $reverse = GetParam('reverse', 0);
  76. my $monthly = GetParam('monthly', 0);
  77. my $offset = GetParam('offset', 0);
  78. my @pages = sort JournalSort (Matched($match, $search ? SearchTitleAndBody($search) : AllPagesList()));
  79. if ($monthly and not $match) {
  80. my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime();
  81. $match = '^' . sprintf("%04d-%02d", $year+1900, $mon+1) . '-\d\d';
  82. }
  83. if ($reverse) {
  84. @pages = reverse @pages;
  85. }
  86. # FIXME: Missing 'future' and 'past' keywords.
  87. my @result = ();
  88. my $n = 0;
  89. foreach my $id (@pages) {
  90. # Now save information required for saving the cache of the current page.
  91. local %Page;
  92. local $OpenPageName = '';
  93. OpenPage($id);
  94. # If this is a minor edit, let's keep everything as it is, but show the date
  95. # of the last major change, if possible. This is important for blogs that
  96. # get added to a Planet. A minor change doesn't mean that the page needs to
  97. # go to the front of the Planet.
  98. if ($Page{minor} and $Page{lastmajor}) {
  99. my %major = GetKeptRevision($Page{lastmajor});
  100. $Page{ts} = $major{ts} if $major{ts};
  101. }
  102. next if $Page{text} =~ /^\s*$/; # only whitespace is also to be deleted
  103. next if $DeletedPage && substr($Page{text}, 0, length($DeletedPage))
  104. eq $DeletedPage; # no regexp
  105. # OK, this is a candidate page
  106. $n++;
  107. next if $n <= $offset;
  108. # Generate artifical rows in the list to pass to GetRcRss. We need
  109. # to open every single page, because the meta-data ordinarily
  110. # available in the rc.log file is not available to us. This is why
  111. # we observe the rsslimit parameter. Without it, we would have to
  112. # open *all* date pages.
  113. my @languages = split(/,/, $Page{languages});
  114. push (@result, [$Page{ts}, $id, $Page{minor}, $Page{summary}, $Page{host},
  115. $Page{username}, $Page{revision}, \@languages,
  116. GetCluster($Page{text})]);
  117. last if @result >= $num;
  118. }
  119. return @result;
  120. }
  121. # Prevent near links from being printed as a result of the search.
  122. push(@MyInitVariables, sub {
  123. $NearLinksException{journal} = 1;
  124. });