clustermap.pl 7.1 KB

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