tagmap.pl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 2 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, write to the
  16. # Free Software Foundation, Inc.
  17. # 59 Temple Place, Suite 330
  18. # Boston, MA 02111-1307 USA
  19. use strict;
  20. use v5.10;
  21. AddModuleDescription('tagmap.pl', 'TagMap Module');
  22. our (%Action, %Page, $OpenPageName, @MyRules, $ModuleDir, $ScriptName);
  23. our ($TagMapPage, $TagMark, $TagClass, $TagString, $TagSearchTitle);
  24. $TagMapPage = "TagMap" unless defined $TagMapPage;
  25. # Page tags are identified by this mark (input mark)
  26. $TagMark = "Tags:" unless defined $TagMark;
  27. # Page tags enclosed in DIV block of this class
  28. $TagClass = "tags" unless defined $TagClass;
  29. # This string precedes tags on page (output mark)
  30. $TagString = "Tags: " unless defined $TagString;
  31. $Action{tagmap} = \&DoTagMap;
  32. $Action{tagsearch} = \&DoTagSearch;
  33. $TagSearchTitle = "Pages with tag %s";
  34. push (@MyRules, \&TagRule);
  35. my %TagList = ();
  36. my $TagXML;
  37. sub TagRule { # Process page tags on a page
  38. if ( m/\G$TagMark\s*(.*)/cg) { # find page tags
  39. my @tags = split /,\s*/, $1; # push them in array
  40. @tags = map { # and generate html output:
  41. qq{<a href="$ScriptName?action=tagsearch;tag=$_">$_</a>}; # each tag is a link to search all pages with that tag
  42. } @tags;
  43. my $tags = join ', ', @tags;
  44. return qq{<div class="$TagClass">$TagString$tags</div>}; # tags are put in DIV block
  45. }
  46. return;
  47. }
  48. sub DoTagSearch {
  49. my $searchedtag = GetParam('tag'); # get tag parameter
  50. my $header = Ts($TagSearchTitle, $searchedtag); # modify page title with requested tag
  51. print GetHeader('',$header,''); # print title
  52. print '<div class="content">';
  53. my $SearchResult = GenerateSearchResult($searchedtag);
  54. print $SearchResult;
  55. print '</div>';
  56. PrintFooter();
  57. }
  58. sub GenerateSearchResult {
  59. my $searchedtag = shift @_;
  60. my @pages = AllPagesList();
  61. local %Page;
  62. local $OpenPageName='';
  63. my $SearchResult .= "<ul>";
  64. foreach my $page (@pages) {
  65. OpenPage($page); # open a page
  66. my @tags = GetTags($Page{text}); # collect tags in an array
  67. foreach (@tags) {
  68. if (/^$searchedtag$/) {
  69. my $name = NormalToFree($page);
  70. $SearchResult .= "<li><a href=\"$ScriptName/$page\">$name</a></li>"; # list of pages
  71. }
  72. }
  73. }
  74. $SearchResult .= "</ul>";
  75. return $SearchResult;
  76. }
  77. sub DoTagMap {
  78. print GetHeader('',$TagMapPage,'');
  79. CreateTagMap();
  80. print '<div class="content">';
  81. PrintTagMap();
  82. print '</div>';
  83. PrintFooter();
  84. }
  85. sub CreateTagMap {
  86. my @pages = AllPagesList();
  87. local %Page;
  88. local $OpenPageName='';
  89. $TagXML .= "<taglist>\n";
  90. foreach my $page (@pages) {
  91. OpenPage($page);
  92. my @tags = GetTags($Page{text});
  93. $page = FreeToNormal($page);
  94. my $count = @tags;
  95. if ($count != 0) {
  96. $TagXML .= "<object><id>$page</id>\n";
  97. foreach (@tags) {
  98. $TagXML .= "<tag>$_</tag>";
  99. $TagList{$_} = 1;
  100. }
  101. $TagXML .= "\n</object>\n";
  102. }
  103. }
  104. $TagXML .= "</taglist>\n";
  105. }
  106. sub PrintTagMap {
  107. do "$ModuleDir/TagCategorizer/TagCategorizer.pl";
  108. my $result = TagCategorizer::ProcessXML($TagXML);
  109. $result =~ s/\<tagHierarchy\>/<ul>/;
  110. $result =~ s/\<\/tagHierarchy\>/<\/ul>/;
  111. $result =~ s{
  112. <tag[ ]title="(.*?)">
  113. }{
  114. my $tag = $1;
  115. "<li>$tag</li>\n<ul>";
  116. }egsx;
  117. $result =~ s/\<\/tag\>/<\/ul>/g;
  118. $result =~ s{
  119. <object>(.*?)</object>
  120. }{
  121. my $id = $1;
  122. my $name = $id;
  123. $name =~ s/_/ /g;
  124. "<li><a href=\"$ScriptName\/$id\">$name</a></li>";
  125. }egsx;
  126. print $result;
  127. }
  128. sub GetTags {
  129. my $text = shift;
  130. my @tags;
  131. # strip [[.*?]] bits, then split on spaces
  132. if ($text =~ /^$TagMark\s*(.*)$/m) {
  133. my $tagstring = $1;
  134. @tags = split /,\s*/, $tagstring;
  135. } else {
  136. return;
  137. }
  138. return @tags;
  139. }
  140. *TagMapOldBrowseResolvedPage = \&BrowseResolvedPage;
  141. *BrowseResolvedPage = \&TagMapBrowseResolvedPage;
  142. sub TagMapBrowseResolvedPage {
  143. my $title = shift;
  144. $title =~ s/_/ /g;
  145. my $id = FreeToNormal($title);
  146. if ($id eq $TagMapPage) {
  147. DoTagMap();
  148. } else {
  149. TagMapOldBrowseResolvedPage($id);
  150. }
  151. }
  152. *TagMapOldPrintWikiToHTML = \&PrintWikiToHTML;
  153. *PrintWikiToHTML = \&TagMapPrintWikiToHTML;
  154. sub TagMapPrintWikiToHTML {
  155. my ($pageText, $savecache, $revision, $islocked) = @_;
  156. # Cause an empty page with the name $ClusterMapPage to
  157. # display a map.
  158. if (($TagMapPage eq $OpenPageName)
  159. && ($pageText =~ /^\s*$/s)){
  160. CreateTagMap();
  161. PrintTagMap();
  162. }
  163. TagMapOldPrintWikiToHTML(@_);
  164. }