Files
oddmuse/modules/linktagmap.pl

252 lines
8.0 KiB
Perl
Raw Normal View History

2007-04-04 07:07:28 +00:00
# Copyright (C) 2007 Alexander Uvizhev <uvizhe@yandex.ru>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.
# 59 Temple Place, Suite 330
# Boston, MA 02111-1307 USA
#
# Based on code of tagmap.pl module by Fletcher T. Penney
# and searchtags.pl module by Brock Wilcox
AddModuleDescription('linktagmap.pl', 'LinkTagMap Module');
2007-04-04 07:07:28 +00:00
2008-03-27 13:18:05 +00:00
use vars qw($LinkTagMark $LinkDescMark $LinkTagClass $LinkDescClass $LinkTagMapPage $UrlPattern $FullUrlPattern $LinkTagSearchTitle);
2007-04-04 07:07:28 +00:00
2007-04-04 07:39:19 +00:00
# Tags and descripton are embraced with this sequences
2007-04-04 07:07:28 +00:00
$LinkTagMark = '%T%' unless defined $LinkTagMark;
$LinkDescMark = '%D%' unless defined $LinkDescMark;
2007-04-04 07:39:19 +00:00
# In output html these will be values for property "class" of SPAN tag
2007-04-04 07:07:28 +00:00
$LinkTagClass = "lntag" unless defined $LinkTagClass;
$LinkDescClass = "lndesc" unless defined $LinkDescClass;
2007-04-04 07:39:19 +00:00
# Wiki page, where links will be present in a structured way
2007-04-04 07:07:28 +00:00
$LinkTagMapPage = "LinkTagMap" unless defined $LinkTagMapPage;
2007-04-04 07:39:19 +00:00
# The same output with wiki.pl?action=linktagmap
2007-04-04 07:07:28 +00:00
$Action{linktagmap} = \&DoLinkTagMap;
# Action to search and show all links with specified tag
$Action{linktagsearch} = \&DoLinkTagSearch;
# Header of a search result
$LinkTagSearchTitle = "Links with tag %s";
2007-04-04 07:07:28 +00:00
2008-03-27 13:18:05 +00:00
my $rstr = crypt($$,$$);
push (@MyRules, \&LinkTagRule, \&LinkDescriptionRule);
2007-04-04 07:07:28 +00:00
sub LinkTagRule { # Process link tags on a page
2007-04-04 07:07:28 +00:00
if ( m/\G$LinkTagMark(.*?)$LinkTagMark/gc) { # find tags
my @linktags = split /,\s*/, $1; # push them in array
@linktags = map { # and generate html output:
qq{<a href="$ScriptName?action=linktagsearch;linktag=$_">$_</a>}; # each tag is a link to search all links with that tag
2007-04-04 07:07:28 +00:00
} @linktags;
my $linktags = join ', ', @linktags;
return qq{<span class="$LinkTagClass">$linktags</span>}; # tags are put in SPAN block
2007-04-04 07:07:28 +00:00
}
return undef;
}
sub LinkDescriptionRule { # Process link descriptions on a page
2007-04-04 07:07:28 +00:00
if ( m/\G$LinkDescMark(.*?)$LinkDescMark/gc) { # find description
return qq{<span class="$LinkDescClass">$1</span>}; # put it in SPAN block
2007-04-04 07:07:28 +00:00
}
return undef;
}
sub DoLinkTagMap {
print GetHeader('',$LinkTagMapPage,'');
2007-04-04 07:07:28 +00:00
my $TagXML = GenerateLinkTagMap();
2007-04-04 07:07:28 +00:00
print '<div class="content">';
PrintLinkTagMap($TagXML);
2007-04-04 07:07:28 +00:00
print '</div>';
PrintFooter();
}
sub DoLinkTagSearch {
my $searchedtag = GetParam('linktag'); # get tag parameter
my $header = Ts($LinkTagSearchTitle, $searchedtag); # modify page title with requested tag
print GetHeader('',$header,''); # print title
print '<div class="content">';
my $SearchResult = GenerateLinkSearchResult($searchedtag);
print $SearchResult;
print '</div>';
PrintFooter();
}
sub GenerateLinkSearchResult {
my $searchedtag = shift @_;
2007-04-04 07:07:28 +00:00
my @pages = AllPagesList();
local %Page;
local $OpenPageName='';
my $SearchResult .= "<ul>";
2007-04-04 07:07:28 +00:00
foreach my $page (@pages) {
OpenPage($page); # open a page
my @links = GetLinks($Page{text}); # find links
foreach my $link (@links) {
my @tags = GetLinkTags($link->{tags}); # collect tags in an array
foreach (@tags) {
2007-08-23 09:17:39 +00:00
if (/^$searchedtag$/) {
my @linktags = split /,\s*/, $link->{tags}; # push tags in an array
@linktags = map { # and print html output:
qq{<a href="$ScriptName?action=linktagsearch;linktag=$_">$_</a>}; # each tag is a link to search all links with that tag
} @linktags;
my $linktags = join ', ', @linktags;
if ( length $link->{name} == 0 ) { $link->{name} = $link->{url}; } # if link has no name we use url instead
$SearchResult .= "<li><a href=\"$link->{url}\">$link->{name}</a><span class=\"$LinkTagClass\">$linktags</span><span class=\"$LinkDescClass\">$link->{description}</span></li>";
}
}
}
}
$SearchResult .= "</ul>";
return $SearchResult;
}
sub GenerateLinkTagMap { # Generate an input XML for TagCategorizer
my @pages = AllPagesList();
local %Page;
local $OpenPageName='';
my $TagXML .= "<taglist>\n";
foreach my $page (@pages) {
OpenPage($page); # open a page
my @links = GetLinks($Page{text}); # find links
foreach my $link (@links) {
my @tags = GetLinkTags($link->{tags}); # collect tags in an array
2008-03-27 13:18:05 +00:00
$TagXML .= "<object><id>$link->{url}\|$rstr\|$link->{name}\|$rstr\|$link->{description}</id>\n"; # put everything in 'id' block
foreach (@tags) { # except of tags
$TagXML .= "<tag>$_</tag>"; # which are in 'tag' blocks
2007-04-04 07:07:28 +00:00
}
2008-03-27 13:18:05 +00:00
$TagXML .= "\n</object>\n";
2007-04-04 07:07:28 +00:00
}
}
$TagXML .= "</taglist>\n";
return $TagXML;
2007-04-04 07:07:28 +00:00
}
sub PrintLinkTagMap {
my $TagXML = shift @_;
2007-04-04 07:07:28 +00:00
do "$ModuleDir/TagCategorizer/TagCategorizer.pl";
my $result = TagCategorizer::ProcessXML($TagXML); # get an output XML from TagCategorizer
2007-04-04 07:07:28 +00:00
$result =~ s/\<tagHierarchy\>/<ul>/; # and convert it to html
2007-04-04 07:07:28 +00:00
$result =~ s/\<\/tagHierarchy\>/<\/ul>/;
$result =~ s{
<tag[ ]title="(.*?)">
}{
my $tag = $1;
"<li id=\"$tag\">$tag</li>\n<ul>";
}xsge;
$result =~ s/\<\/tag\>/<\/ul>/g;
$result =~ s{
2008-03-27 13:18:05 +00:00
<object>$FullUrlPattern\|$rstr\|(.*?)\|$rstr\|(.*?)</object> # divide 'object' block content
2007-04-04 07:07:28 +00:00
}{
my $url = $1; # to url,
my $name = $2; if ( length $name == 0 ) { $name = $url; } # name (if not present use url instead)
my $description = $3; # and description
"<li><a href=\"$url\">$name</a> <span class=\"$LinkDescClass\">$description</span></li>";
2007-04-04 07:07:28 +00:00
}xsge;
print $result;
}
sub GetLinks { # Search a page for links
2007-04-04 07:07:28 +00:00
my $text = shift;
2008-03-27 13:18:05 +00:00
my $text1 = $text;
2007-04-04 07:07:28 +00:00
my @links;
2008-03-27 13:18:05 +00:00
while ( $text =~ /($UrlPattern)\s*($LinkTagMark(.+?)$LinkTagMark\s*($LinkDescMark(.+?)$LinkDescMark)?)/cg # simple link
or $text1 =~ /\[+$FullUrlPattern(.*?)\]+\s*($LinkTagMark(.+?)$LinkTagMark\s*($LinkDescMark(.+?)$LinkDescMark)?)/cg) { # link in brackets
push @links, { url => $1, name => $2, tags => $4, description => $6 }; # push found links' attributes to an array of hashes
2007-04-04 07:07:28 +00:00
}
return @links;
}
sub GetLinkTags { # Retrieve tags (if present) from a link
2007-04-04 07:07:28 +00:00
my $tags = shift;
2007-04-04 07:07:28 +00:00
my @tags;
@tags = split /\s*,\s*/, $tags;
2007-04-04 07:07:28 +00:00
return @tags;
}
*LinkTagMapOldBrowseResolvedPage = *BrowseResolvedPage;
*BrowseResolvedPage = *LinkTagMapBrowseResolvedPage;
sub LinkTagMapBrowseResolvedPage {
my $title = shift;
$title =~ s/_/ /g;
my $id = FreeToNormal($title);
if ($id eq $LinkTagMapPage) {
DoLinkTagMap();
} else {
LinkTagMapOldBrowseResolvedPage($id);
}
}
*LinkTagMapOldPrintWikiToHTML = *PrintWikiToHTML;
*PrintWikiToHTML = *LinkTagMapPrintWikiToHTML;
sub LinkTagMapPrintWikiToHTML {
my ($pageText) = @_;
2007-04-04 07:07:28 +00:00
# Cause an empty page with the name $LinkTagMapPage to
2007-04-04 07:07:28 +00:00
# display a map.
if (($LinkTagMapPage eq $OpenPageName)
&& ($pageText =~ /^\s*$/s)){
CreateLinkTagMap();
PrintLinkTagMap();
}
LinkTagMapOldPrintWikiToHTML(@_);
}