linktagmap.pl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # Copyright (C) 2007 Alexander Uvizhev <uvizhe@yandex.ru>
  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('linktagmap.pl', 'LinkTagMap Module');
  18. our (%Action, %Page, $OpenPageName, $ModuleDir, @MyRules, $ScriptName);
  19. our ($LinkTagMark, $LinkDescMark, $LinkTagClass, $LinkDescClass, $LinkTagMapPage, $UrlPattern, $FullUrlPattern, $LinkTagSearchTitle);
  20. # Tags and descripton are embraced with this sequences
  21. $LinkTagMark = '%T%' unless defined $LinkTagMark;
  22. $LinkDescMark = '%D%' unless defined $LinkDescMark;
  23. # In output html these will be values for property "class" of SPAN tag
  24. $LinkTagClass = "lntag" unless defined $LinkTagClass;
  25. $LinkDescClass = "lndesc" unless defined $LinkDescClass;
  26. # Wiki page, where links will be present in a structured way
  27. $LinkTagMapPage = "LinkTagMap" unless defined $LinkTagMapPage;
  28. # The same output with wiki.pl?action=linktagmap
  29. $Action{linktagmap} = \&DoLinkTagMap;
  30. # Action to search and show all links with specified tag
  31. $Action{linktagsearch} = \&DoLinkTagSearch;
  32. # Header of a search result
  33. $LinkTagSearchTitle = "Links with tag %s";
  34. my $rstr = crypt($$,$$);
  35. push (@MyRules, \&LinkTagRule, \&LinkDescriptionRule);
  36. sub LinkTagRule { # Process link tags on a page
  37. if ( m/\G$LinkTagMark(.*?)$LinkTagMark/cg) { # find tags
  38. my @linktags = split /,\s*/, $1; # push them in array
  39. @linktags = map { # and generate html output:
  40. qq{<a href="$ScriptName?action=linktagsearch;linktag=$_">$_</a>}; # each tag is a link to search all links with that tag
  41. } @linktags;
  42. my $linktags = join ', ', @linktags;
  43. return qq{<span class="$LinkTagClass">$linktags</span>}; # tags are put in SPAN block
  44. }
  45. return;
  46. }
  47. sub LinkDescriptionRule { # Process link descriptions on a page
  48. if ( m/\G$LinkDescMark(.*?)$LinkDescMark/cg) { # find description
  49. return qq{<span class="$LinkDescClass">$1</span>}; # put it in SPAN block
  50. }
  51. return;
  52. }
  53. sub DoLinkTagMap {
  54. print GetHeader('',$LinkTagMapPage,'');
  55. my $TagXML = GenerateLinkTagMap();
  56. print '<div class="content">';
  57. PrintLinkTagMap($TagXML);
  58. print '</div>';
  59. PrintFooter();
  60. }
  61. sub DoLinkTagSearch {
  62. my $searchedtag = GetParam('linktag'); # get tag parameter
  63. my $header = Ts($LinkTagSearchTitle, $searchedtag); # modify page title with requested tag
  64. print GetHeader('',$header,''); # print title
  65. print '<div class="content">';
  66. my $SearchResult = GenerateLinkSearchResult($searchedtag);
  67. print $SearchResult;
  68. print '</div>';
  69. PrintFooter();
  70. }
  71. sub GenerateLinkSearchResult {
  72. my $searchedtag = shift @_;
  73. my @pages = AllPagesList();
  74. local %Page;
  75. local $OpenPageName='';
  76. my $SearchResult .= "<ul>";
  77. foreach my $page (@pages) {
  78. OpenPage($page); # open a page
  79. my @links = GetLinks($Page{text}); # find links
  80. foreach my $link (@links) {
  81. my @tags = GetLinkTags($link->{tags}); # collect tags in an array
  82. foreach (@tags) {
  83. if (/^$searchedtag$/) {
  84. my @linktags = split /,\s*/, $link->{tags}; # push tags in an array
  85. @linktags = map { # and print html output:
  86. qq{<a href="$ScriptName?action=linktagsearch;linktag=$_">$_</a>}; # each tag is a link to search all links with that tag
  87. } @linktags;
  88. my $linktags = join ', ', @linktags;
  89. if ( length $link->{name} == 0 ) { $link->{name} = $link->{url}; } # if link has no name we use url instead
  90. $SearchResult .= "<li><a href=\"$link->{url}\">$link->{name}</a><span class=\"$LinkTagClass\">$linktags</span><span class=\"$LinkDescClass\">$link->{description}</span></li>";
  91. }
  92. }
  93. }
  94. }
  95. $SearchResult .= "</ul>";
  96. return $SearchResult;
  97. }
  98. sub GenerateLinkTagMap { # Generate an input XML for TagCategorizer
  99. my @pages = AllPagesList();
  100. local %Page;
  101. local $OpenPageName='';
  102. my $TagXML .= "<taglist>\n";
  103. foreach my $page (@pages) {
  104. OpenPage($page); # open a page
  105. my @links = GetLinks($Page{text}); # find links
  106. foreach my $link (@links) {
  107. my @tags = GetLinkTags($link->{tags}); # collect tags in an array
  108. $TagXML .= "<object><id>$link->{url}\|$rstr\|$link->{name}\|$rstr\|$link->{description}</id>\n"; # put everything in 'id' block
  109. foreach (@tags) { # except of tags
  110. $TagXML .= "<tag>$_</tag>"; # which are in 'tag' blocks
  111. }
  112. $TagXML .= "\n</object>\n";
  113. }
  114. }
  115. $TagXML .= "</taglist>\n";
  116. return $TagXML;
  117. }
  118. sub PrintLinkTagMap {
  119. my $TagXML = shift @_;
  120. do "$ModuleDir/TagCategorizer/TagCategorizer.pl";
  121. my $result = TagCategorizer::ProcessXML($TagXML); # get an output XML from TagCategorizer
  122. $result =~ s/\<tagHierarchy\>/<ul>/; # and convert it to html
  123. $result =~ s/\<\/tagHierarchy\>/<\/ul>/;
  124. $result =~ s{
  125. <tag[ ]title="(.*?)">
  126. }{
  127. my $tag = $1;
  128. "<li id=\"$tag\">$tag</li>\n<ul>";
  129. }egsx;
  130. $result =~ s/\<\/tag\>/<\/ul>/g;
  131. $result =~ s{
  132. <object>$FullUrlPattern\|$rstr\|(.*?)\|$rstr\|(.*?)</object> # divide 'object' block content
  133. }{
  134. my $url = $1; # to url,
  135. my $name = $2; if ( length $name == 0 ) { $name = $url; } # name (if not present use url instead)
  136. my $description = $3; # and description
  137. "<li><a href=\"$url\">$name</a> <span class=\"$LinkDescClass\">$description</span></li>";
  138. }egsx;
  139. print $result;
  140. }
  141. sub GetLinks { # Search a page for links
  142. my $text = shift;
  143. my $text1 = $text;
  144. my @links;
  145. while ( $text =~ /($UrlPattern)\s*($LinkTagMark(.+?)$LinkTagMark\s*($LinkDescMark(.+?)$LinkDescMark)?)/cg # simple link
  146. or $text1 =~ /\[+$FullUrlPattern(.*?)\]+\s*($LinkTagMark(.+?)$LinkTagMark\s*($LinkDescMark(.+?)$LinkDescMark)?)/cg) { # link in brackets
  147. push @links, { url => $1, name => $2, tags => $4, description => $6 }; # push found links' attributes to an array of hashes
  148. }
  149. return @links;
  150. }
  151. sub GetLinkTags { # Retrieve tags (if present) from a link
  152. my $tags = shift;
  153. my @tags;
  154. @tags = split /\s*,\s*/, $tags;
  155. return @tags;
  156. }
  157. *LinkTagMapOldBrowseResolvedPage = \&BrowseResolvedPage;
  158. *BrowseResolvedPage = \&LinkTagMapBrowseResolvedPage;
  159. sub LinkTagMapBrowseResolvedPage {
  160. my $title = shift;
  161. $title =~ s/_/ /g;
  162. my $id = FreeToNormal($title);
  163. if ($id eq $LinkTagMapPage) {
  164. DoLinkTagMap();
  165. } else {
  166. LinkTagMapOldBrowseResolvedPage($id);
  167. }
  168. }
  169. *LinkTagMapOldPrintWikiToHTML = \&PrintWikiToHTML;
  170. *PrintWikiToHTML = \&LinkTagMapPrintWikiToHTML;
  171. sub LinkTagMapPrintWikiToHTML {
  172. my ($pageText) = @_;
  173. # Cause an empty page with the name $LinkTagMapPage to
  174. # display a map.
  175. if (($LinkTagMapPage eq $OpenPageName)
  176. && ($pageText =~ /^\s*$/s)){
  177. CreateLinkTagMap();
  178. PrintLinkTagMap();
  179. }
  180. LinkTagMapOldPrintWikiToHTML(@_);
  181. }