tagmap.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # Copyright (C) 2005 Fletcher T. Penney <fletcher@freeshell.org>
  2. # Copyright (c) 2007 Alexander Uvizhev <uvizhe@yandex.ru>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. use strict;
  17. use v5.10;
  18. AddModuleDescription('tagmap.pl', 'TagMap Module');
  19. our (%Action, %Page, $OpenPageName, @MyRules, $ModuleDir, $ScriptName);
  20. our ($TagMapPage, $TagMark, $TagClass, $TagString, $TagSearchTitle);
  21. $TagMapPage = "TagMap" unless defined $TagMapPage;
  22. # Page tags are identified by this mark (input mark)
  23. $TagMark = "Tags:" unless defined $TagMark;
  24. # Page tags enclosed in DIV block of this class
  25. $TagClass = "tags" unless defined $TagClass;
  26. # This string precedes tags on page (output mark)
  27. $TagString = "Tags: " unless defined $TagString;
  28. $Action{tagmap} = \&DoTagMap;
  29. $Action{tagsearch} = \&DoTagSearch;
  30. $TagSearchTitle = "Pages with tag %s";
  31. push (@MyRules, \&TagRule);
  32. my %TagList = ();
  33. my $TagXML;
  34. sub TagRule { # Process page tags on a page
  35. if ( m/\G$TagMark\s*(.*)/cg) { # find page tags
  36. my @tags = split /,\s*/, $1; # push them in array
  37. @tags = map { # and generate html output:
  38. qq{<a href="$ScriptName?action=tagsearch;tag=$_">$_</a>}; # each tag is a link to search all pages with that tag
  39. } @tags;
  40. my $tags = join ', ', @tags;
  41. return qq{<div class="$TagClass">$TagString$tags</div>}; # tags are put in DIV block
  42. }
  43. return;
  44. }
  45. sub DoTagSearch {
  46. my $searchedtag = GetParam('tag'); # get tag parameter
  47. my $header = Ts($TagSearchTitle, $searchedtag); # modify page title with requested tag
  48. print GetHeader('',$header,''); # print title
  49. print '<div class="content">';
  50. my $SearchResult = GenerateSearchResult($searchedtag);
  51. print $SearchResult;
  52. print '</div>';
  53. PrintFooter();
  54. }
  55. sub GenerateSearchResult {
  56. my $searchedtag = shift @_;
  57. my @pages = AllPagesList();
  58. local %Page;
  59. local $OpenPageName='';
  60. my $SearchResult .= "<ul>";
  61. foreach my $page (@pages) {
  62. OpenPage($page); # open a page
  63. my @tags = GetTags($Page{text}); # collect tags in an array
  64. foreach (@tags) {
  65. if (/^$searchedtag$/) {
  66. my $name = NormalToFree($page);
  67. $SearchResult .= "<li><a href=\"$ScriptName/$page\">$name</a></li>"; # list of pages
  68. }
  69. }
  70. }
  71. $SearchResult .= "</ul>";
  72. return $SearchResult;
  73. }
  74. sub DoTagMap {
  75. print GetHeader('',$TagMapPage,'');
  76. CreateTagMap();
  77. print '<div class="content">';
  78. PrintTagMap();
  79. print '</div>';
  80. PrintFooter();
  81. }
  82. sub CreateTagMap {
  83. my @pages = AllPagesList();
  84. local %Page;
  85. local $OpenPageName='';
  86. $TagXML .= "<taglist>\n";
  87. foreach my $page (@pages) {
  88. OpenPage($page);
  89. my @tags = GetTags($Page{text});
  90. $page = FreeToNormal($page);
  91. my $count = @tags;
  92. if ($count != 0) {
  93. $TagXML .= "<object><id>$page</id>\n";
  94. foreach (@tags) {
  95. $TagXML .= "<tag>$_</tag>";
  96. $TagList{$_} = 1;
  97. }
  98. $TagXML .= "\n</object>\n";
  99. }
  100. }
  101. $TagXML .= "</taglist>\n";
  102. }
  103. sub PrintTagMap {
  104. do "$ModuleDir/TagCategorizer/TagCategorizer.pl";
  105. my $result = TagCategorizer::ProcessXML($TagXML);
  106. $result =~ s/\<tagHierarchy\>/<ul>/;
  107. $result =~ s/\<\/tagHierarchy\>/<\/ul>/;
  108. $result =~ s{
  109. <tag[ ]title="(.*?)">
  110. }{
  111. my $tag = $1;
  112. "<li>$tag</li>\n<ul>";
  113. }egsx;
  114. $result =~ s/\<\/tag\>/<\/ul>/g;
  115. $result =~ s{
  116. <object>(.*?)</object>
  117. }{
  118. my $id = $1;
  119. my $name = $id;
  120. $name =~ s/_/ /g;
  121. "<li><a href=\"$ScriptName\/$id\">$name</a></li>";
  122. }egsx;
  123. print $result;
  124. }
  125. sub GetTags {
  126. my $text = shift;
  127. my @tags;
  128. # strip [[.*?]] bits, then split on spaces
  129. if ($text =~ /^$TagMark\s*(.*)$/m) {
  130. my $tagstring = $1;
  131. @tags = split /,\s*/, $tagstring;
  132. } else {
  133. return;
  134. }
  135. return @tags;
  136. }
  137. *TagMapOldBrowseResolvedPage = \&BrowseResolvedPage;
  138. *BrowseResolvedPage = \&TagMapBrowseResolvedPage;
  139. sub TagMapBrowseResolvedPage {
  140. my $title = shift;
  141. $title =~ s/_/ /g;
  142. my $id = FreeToNormal($title);
  143. if ($id eq $TagMapPage) {
  144. DoTagMap();
  145. } else {
  146. TagMapOldBrowseResolvedPage($id);
  147. }
  148. }
  149. *TagMapOldPrintWikiToHTML = \&PrintWikiToHTML;
  150. *PrintWikiToHTML = \&TagMapPrintWikiToHTML;
  151. sub TagMapPrintWikiToHTML {
  152. my ($pageText, $savecache, $revision, $islocked) = @_;
  153. # Cause an empty page with the name $ClusterMapPage to
  154. # display a map.
  155. if (($TagMapPage eq $OpenPageName)
  156. && ($pageText =~ /^\s*$/s)){
  157. CreateTagMap();
  158. PrintTagMap();
  159. }
  160. TagMapOldPrintWikiToHTML(@_);
  161. }