aggregate.pl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright (C) 2005, 2006 Alex Schroeder <alex@emacswiki.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('aggregate.pl', 'Front Page Extension');
  18. our ($q, $bol, %Action, %Page, $OpenPageName, $UseDiff, $UsePathInfo, $RssStyleSheet, $RssLicense, $RssRights, $RssImageUrl, $CommentsPrefix, $SiteName, $ScriptName, $HomePage, $SiteDescription, @MyRules, $LastUpdate, $InterWikiMoniker);
  19. push(@MyRules, \&AggregateRule);
  20. sub AggregateRule {
  21. if ($bol && m/\G(&lt;aggregate\s+((("[^\"&]+",?\s*)+)|(sort\s+)?search\s+(.+?))&gt;)/cg) {
  22. Clean(CloseHtmlEnvironments());
  23. Dirty($1);
  24. my ($oldpos, $old_, $str, $sort, $search) = ((pos), $_, $3, $5, $6);
  25. my $master = $OpenPageName;
  26. local ($OpenPageName, %Page);
  27. print $q->start_div({class=>"aggregate journal"});
  28. my @pages = ();
  29. @pages = $str =~ m/"([^\"&]+)"/g if $str;
  30. @pages = SearchTitleAndBody($search) if $search;
  31. if ($sort) {
  32. if (defined &PageSort) {
  33. @pages = sort PageSort @pages;
  34. } else {
  35. @pages = sort(@pages);
  36. }
  37. }
  38. foreach my $id (@pages) {
  39. next if $id eq $master;
  40. my $title = $id;
  41. local $OpenPageName = FreeToNormal($id);
  42. my $page = GetPageContent($OpenPageName);
  43. my $size = length($page);
  44. my $i = index($page, "\n=");
  45. my $j = index($page, "\n----");
  46. $page = substr($page, 0, $i) if $i >= 0;
  47. $page = substr($page, 0, $j) if $j >= 0;
  48. $page =~ s/^=.*\n//; # if it starts with a header
  49. print $q->start_div({class=>"page"}),
  50. $q->h2(GetPageLink($OpenPageName, $title));
  51. ApplyRules(QuoteHtml($page), 1, 0, undef, 'p');
  52. print $q->p(GetPageLink($OpenPageName, T('Learn more...')))
  53. if length($page) < $size;
  54. print $q->end_div();
  55. }
  56. print $q->end_div();
  57. Clean(AddHtmlEnvironment('p'));
  58. ($_, pos) = ($old_, $oldpos); # restore \G (assignment order matters!)
  59. return '';
  60. }
  61. return;
  62. }
  63. $Action{aggregate} = \&DoAggregate;
  64. sub DoAggregate {
  65. print GetHttpHeader('application/xml');
  66. my $frontpage = GetParam('id', $HomePage);
  67. my $title = $frontpage;
  68. $title =~ s/_/ /g;
  69. my $source = GetPageContent($frontpage);
  70. my $url = QuoteHtml($ScriptName);
  71. my $diffPrefix = $url . QuoteHtml("?action=browse;diff=1;id=");
  72. my $historyPrefix = $url . QuoteHtml("?action=history;id=");
  73. my $date = TimeToRFC822($LastUpdate);
  74. my $rss = qq{<?xml version="1.0" encoding="utf-8"?>};
  75. if ($RssStyleSheet =~ /\.(xslt?|xml)$/) {
  76. $rss .= qq{<?xml-stylesheet type="text/xml" href="$RssStyleSheet" ?>};
  77. } elsif ($RssStyleSheet) {
  78. $rss .= qq{<?xml-stylesheet type="text/css" href="$RssStyleSheet" ?>};
  79. }
  80. $rss .= qq{<rss version="2.0"
  81. xmlns:wiki="http://purl.org/rss/1.0/modules/wiki/"
  82. xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">
  83. <channel>
  84. <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  85. };
  86. $rss .= "<title>" . QuoteHtml("$SiteName: $title") . "</title>\n";
  87. $rss .= "<link>" . $url . ($UsePathInfo ? "/" : "?") . UrlEncode($frontpage) . "</link>\n";
  88. $rss .= "<description>" . QuoteHtml($SiteDescription) . "</description>\n";
  89. $rss .= "<pubDate>" . $date. "</pubDate>\n";
  90. $rss .= "<lastBuildDate>" . $date . "</lastBuildDate>\n";
  91. $rss .= "<generator>Oddmuse</generator>\n";
  92. $rss .= "<copyright>" . $RssRights . "</copyright>\n" if $RssRights;
  93. if (ref $RssLicense eq 'ARRAY') {
  94. $rss .= join('', map {"<creativeCommons:license>$_</creativeCommons:license>\n"} @$RssLicense);
  95. } elsif ($RssLicense) {
  96. $rss .= "<creativeCommons:license>" . $RssLicense . "</creativeCommons:license>\n";
  97. }
  98. $rss .= "<wiki:interwiki>" . $InterWikiMoniker . "</wiki:interwiki>\n" if $InterWikiMoniker;
  99. if ($RssImageUrl) {
  100. $rss .= "<image>\n";
  101. $rss .= "<url>" . $RssImageUrl . "</url>\n";
  102. $rss .= "<title>" . QuoteHtml($SiteName) . "</title>\n";
  103. $rss .= "<link>" . $url . "</link>\n";
  104. $rss .= "</image>\n";
  105. }
  106. while ($source =~ m/<aggregate\s+((("[^\"&]+",?\s*)+)|(sort\s+)?search\s+(.+?))>/g) {
  107. my ($str, $sort, $search) = ($1, $5, $6);
  108. my @pages = ();
  109. @pages = $str =~ m/"([^\"&]+)"/g if $str;
  110. @pages = SearchTitleAndBody($search) if $search;
  111. if ($sort) {
  112. if (defined &PageSort) {
  113. @pages = sort PageSort @pages;
  114. } else {
  115. @pages = sort(@pages);
  116. }
  117. }
  118. foreach my $id (@pages) {
  119. my $data = ParseData(ReadFileOrDie(GetPageFile(FreeToNormal($id))));
  120. my $page = $data->{text};
  121. my $size = length($page);
  122. my $i = index($page, "\n=");
  123. my $j = index($page, "\n----");
  124. $page = substr($page, 0, $i) if $i >= 0;
  125. $page = substr($page, 0, $j) if $j >= 0;
  126. $page =~ s/^=.*\n//; # if it starts with a header
  127. my $name = $id;
  128. $name =~ s/_/ /g;
  129. my $date = TimeToRFC822($data->{ts});
  130. my $host = $data->{host};
  131. my $username = $data->{username};
  132. $username = QuoteHtml($username);
  133. $username = $host unless $username;
  134. my $minor = $data->{minor};
  135. my $revision = $data->{revision};
  136. my $cluster = GetCluster($page);
  137. my $description = ToString(sub { ApplyRules(QuoteHtml($page), 1, 0, undef, 'p') });
  138. $description .= $q->p(GetPageLink($id, T('Learn more...')))
  139. if length($page) < $size;
  140. $rss .= "\n<item>\n";
  141. $rss .= "<title>" . QuoteHtml($name) . "</title>\n";
  142. $rss .= "<link>" . $url . (GetParam("all", 0)
  143. ? "?" . GetPageParameters("browse", $id, $revision, $cluster)
  144. : ($UsePathInfo ? "/" : "?") . UrlEncode($id)) . "</link>\n";
  145. $rss .= "<description>" . QuoteHtml($description) . "</description>\n";
  146. $rss .= "<pubDate>" . $date . "</pubDate>\n";
  147. $rss .= "<comments>" . $url . ($UsePathInfo ? "/" : "?")
  148. . $CommentsPrefix . UrlEncode($id) . "</comments>\n"
  149. if $CommentsPrefix and $id !~ /^$CommentsPrefix/;
  150. $rss .= "<wiki:username>" . $username . "</wiki:username>\n";
  151. $rss .= "<wiki:status>" . (1 == $revision ? "new" : "updated") . "</wiki:status>\n";
  152. $rss .= "<wiki:importance>" . ($minor ? "minor" : "major") . "</wiki:importance>\n";
  153. $rss .= "<wiki:version>" . $revision . "</wiki:version>\n";
  154. $rss .= "<wiki:history>" . $historyPrefix . UrlEncode($id) . "</wiki:history>\n";
  155. $rss .= "<wiki:diff>" . $diffPrefix . UrlEncode($id) . "</wiki:diff>\n"
  156. if $UseDiff and GetParam("diffrclink", 1);
  157. $rss .= "</item>\n";
  158. }
  159. $rss .= "</channel>\n</rss>\n";
  160. }
  161. print $rss;
  162. }