clustermap.pl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # Copyright (C) 2004, 2005 Fletcher T. Penney <fletcher@freeshell.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('clustermap.pl', 'ClusterMap Module');
  18. our ($q, %Action, %Page, $OpenPageName, @MyRules, @MyAdminCode, $HomePage, $DeletedPage, $RCName, $InterMap, $BannedContent, $BannedHosts, %AdminPages, $RssExclude, @AdminPages, $NearMap);
  19. our ($ClusterMapPage, %ClusterMap, $ClusterMapTOC, $FilterUnclusteredRegExp, @ClusterMapAdminPages, $PrintTOCAnchor);
  20. my %Unclustered = ();
  21. $ClusterMapPage = "Site_Map" unless defined $ClusterMapPage;
  22. # Don't list the following pages as unclustered
  23. # By default, journal pages and Comment pages
  24. $FilterUnclusteredRegExp = '\d\d\d\d-\d\d-\d\d|\d* *Comments on .*'
  25. unless defined $FilterUnclusteredRegExp;
  26. # The following pages are added to the AdminPage list and
  27. # are not classified as unclustered.
  28. # They are also added to the Important Pages list on the administration page
  29. @ClusterMapAdminPages = ( $HomePage, $DeletedPage, $BannedContent,
  30. $BannedHosts, $InterMap, $NearMap, $RCName, $RssExclude)
  31. unless @ClusterMapAdminPages;
  32. $ClusterMapTOC = 1 unless defined $ClusterMapTOC;
  33. $PrintTOCAnchor = 0;
  34. %ClusterMap = ();
  35. *OldPrintRcHtml = \&PrintRcHtml;
  36. *PrintRcHtml = \&ClusterMapPrintRcHtml;
  37. push(@MyAdminCode, \&ClusterMapAdminRule);
  38. $Action{clustermap} = \&DoClusterMap;
  39. $Action{unclustered} = \&DoUnclustered;
  40. push(@MyRules, \&ClusterMapRule);
  41. foreach (@ClusterMapAdminPages){
  42. $AdminPages{$_} = 1;
  43. }
  44. sub ClusterMapRule {
  45. if (/\G^([\n\r]*\<\s*clustermap\s*\>\s*)$/cgm) {
  46. Dirty($1);
  47. my $oldpos = pos;
  48. my $oldstr = $_;
  49. CreateClusterMap();
  50. print "</p>"; # Needed to clean up, but could cause problems
  51. # if <clustermap> isn't put into a new paragraph
  52. PrintClusterMap();
  53. pos = $oldpos;
  54. $oldstr =~ s/.*?\<\s*clustermap\s*\>//s;
  55. $_ = $oldstr;
  56. return '';
  57. }
  58. return;
  59. }
  60. sub DoClusterMap {
  61. # Get list of all clusters
  62. # For each cluster, get list of all pages in that cluster
  63. # Create map, using body of cluster pages, followed by titles of pages
  64. # within that cluster
  65. print GetHeader('',$ClusterMapPage,'');
  66. CreateClusterMap();
  67. if ($ClusterMapTOC) {
  68. my $TOCCount = 0;
  69. print '<div class="toc"><h2>Categories</h2><ol>';
  70. foreach my $cluster ( sort keys %ClusterMap) {
  71. $cluster =~ s/_/ /g;
  72. print "<li><a href=\"#toc$TOCCount\">$cluster</a></li>";
  73. $TOCCount++;
  74. }
  75. print '</ol></div>';
  76. $PrintTOCAnchor = 1;
  77. }
  78. print '<div class="content">';
  79. PrintClusterMap();
  80. print '</div>';
  81. PrintFooter();
  82. }
  83. sub DoUnclustered {
  84. print GetHeader('','Pages without a Cluster','');
  85. print '<div class="content">';
  86. CreateClusterMap();
  87. PrintUnclusteredMap();
  88. print '</div>';
  89. PrintFooter();
  90. }
  91. sub PrintClusterMap {
  92. my $TOCCount = 0;
  93. foreach my $cluster (sort keys %ClusterMap) {
  94. local %Page;
  95. local $OpenPageName='';
  96. my $free = $cluster;
  97. $free =~ s/_/ /g;
  98. OpenPage($cluster);
  99. if ( FreeToNormal(GetCluster($Page{text})) eq $cluster ) {
  100. # Don't display the page name twice if the cluster page is also
  101. # a member of the cluster
  102. $Page{text} =~ s/^\[*($cluster|$free)\]*\n*//s;
  103. }
  104. if ($PrintTOCAnchor) {
  105. print $q->h1("<a id=\"toc$TOCCount\"></a>" . GetPageOrEditLink($free, $free));
  106. $TOCCount++;
  107. } else {
  108. print $q->h1(GetPageOrEditLink($free, $free));
  109. }
  110. PrintWikiToHTML($Page{text}, 0);
  111. print "<ul>";
  112. foreach my $page (sort keys %{$ClusterMap{$cluster}}) {
  113. my $title = $page;
  114. $title =~ s/_/ /g;
  115. print "<li>" . ScriptLink($page, $title, 'local') . "</li>";
  116. }
  117. print "</ul>";
  118. }
  119. }
  120. sub CreateClusterMap {
  121. my @pages = AllPagesList();
  122. local %Page;
  123. local $OpenPageName='';
  124. foreach my $page ( @pages) {
  125. OpenPage($page);
  126. my $cluster = FreeToNormal(GetCluster($Page{text}));
  127. next if ($cluster eq $DeletedPage); # Don't map Deleted Pages
  128. next if (TextIsFile($Page{text})); # Don't map files
  129. if ($cluster eq "") { # Grab Unclustered Pages
  130. $Unclustered{$page} = 1;
  131. next;
  132. }
  133. if ($cluster ne FreeToNormal($page)) { # Create Cluster Map
  134. $ClusterMap{$cluster}{$page} = 1;
  135. }
  136. }
  137. # Strip out Admin Pages
  138. foreach my $page (@AdminPages) {
  139. delete($Unclustered{$page});
  140. }
  141. }
  142. sub ClusterMapPrintRcHtml {
  143. my ( @options ) = @_;
  144. my $cluster = GetParam('rcclusteronly');
  145. if ($cluster ne "") {
  146. CreateClusterMap();
  147. print "Pages in this cluster:";
  148. print "<ul>";
  149. foreach my $page (sort keys %{$ClusterMap{$cluster}}) {
  150. my $title = $page;
  151. $title =~ s/_/ /g;
  152. print "<li>" . ScriptLink($page, $title, 'local') . "</li>";
  153. }
  154. print "</ul>";
  155. }
  156. OldPrintRcHtml(@options);
  157. }
  158. sub PrintUnclusteredMap {
  159. print "<ul>";
  160. foreach my $page (sort keys %Unclustered) {
  161. my $title = $page;
  162. $title =~ s/_/ /g;
  163. if ($title !~ /^($FilterUnclusteredRegExp)$/) {
  164. print "<li>" . ScriptLink($page, $title, 'local') . "</li>";
  165. }
  166. }
  167. print "</ul>";
  168. }
  169. sub ClusterMapAdminRule {
  170. my ($id, $menuref) = @_;
  171. push(@$menuref, ScriptLink('action=clustermap', T('Clustermap'), 'clustermap'));
  172. push(@$menuref, ScriptLink('action=unclustered', T('Pages without a Cluster'), 'unclustered'));
  173. }
  174. *OldBrowseResolvedPage = \&BrowseResolvedPage;
  175. *BrowseResolvedPage = \&ClusterMapBrowseResolvedPage;
  176. sub ClusterMapBrowseResolvedPage {
  177. my $title = shift;
  178. $title =~ s/_/ /g;
  179. my $id = FreeToNormal($title);
  180. if ($id eq $ClusterMapPage) {
  181. CreateClusterMap();
  182. print GetHeader('',$title,'');
  183. print '<div class="content">';
  184. if ($ClusterMapTOC) {
  185. my $TOCCount = 0;
  186. print '<div class="toc"><h2>Categories</h2><ol>';
  187. foreach my $cluster ( sort keys %ClusterMap) {
  188. $cluster =~ s/_/ /g;
  189. print "<li><a href=\"#toc$TOCCount\">$cluster</a></li>";
  190. $TOCCount++;
  191. }
  192. print '</ol></div>';
  193. $PrintTOCAnchor = 1;
  194. }
  195. PrintClusterMap();
  196. print '</div>';
  197. PrintFooter();
  198. } else {
  199. OldBrowseResolvedPage($id);
  200. }
  201. }
  202. *OldPrintWikiToHTML = \&PrintWikiToHTML;
  203. *PrintWikiToHTML = \&ClusterMapPrintWikiToHTML;
  204. sub ClusterMapPrintWikiToHTML {
  205. my ($pageText, $savecache, $revision, $islocked) = @_;
  206. # Cause an empty page with the name $ClusterMapPage to
  207. # display a map.
  208. if (($ClusterMapPage eq $OpenPageName)
  209. && ($pageText =~ /^\s*$/s)){
  210. SetParam('rcclusteronly',0);
  211. CreateClusterMap();
  212. print '<div class="content">';
  213. if ($ClusterMapTOC) {
  214. my $TOCCount = 0;
  215. print '<div class="toc"><h2>Contents</h2><ol>';
  216. foreach my $cluster ( sort keys %ClusterMap) {
  217. print "<li><a href=\"#toc$TOCCount\">$cluster</a></li>";
  218. $TOCCount++;
  219. }
  220. print '</ol></div>';
  221. $PrintTOCAnchor = 1;
  222. }
  223. PrintClusterMap();
  224. print '</div>';
  225. }
  226. OldPrintWikiToHTML(@_);
  227. }