aggregate.pl 7.0 KB

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