| 1 | # See bottom of file for license and copyright information |
|---|
| 2 | |
|---|
| 3 | =begin TML |
|---|
| 4 | |
|---|
| 5 | ---+ package Foswiki::Plugins::InterwikiPlugin |
|---|
| 6 | |
|---|
| 7 | Recognises and processes special links to other sites defined |
|---|
| 8 | using "inter-site syntax". |
|---|
| 9 | |
|---|
| 10 | The recognized syntax is: |
|---|
| 11 | <pre> |
|---|
| 12 | InterSiteName:TopicName |
|---|
| 13 | </pre> |
|---|
| 14 | |
|---|
| 15 | Sites must start with upper case and must be preceded by white |
|---|
| 16 | space, '-', '*' or '(', or be part of the link expression |
|---|
| 17 | in a [[link]] or [[link][text]] expression. |
|---|
| 18 | |
|---|
| 19 | =cut |
|---|
| 20 | |
|---|
| 21 | package Foswiki::Plugins::InterwikiPlugin; |
|---|
| 22 | |
|---|
| 23 | use strict; |
|---|
| 24 | use warnings; |
|---|
| 25 | |
|---|
| 26 | use Foswiki::Func (); # The plugins API |
|---|
| 27 | use Foswiki::Plugins (); # For the API version |
|---|
| 28 | |
|---|
| 29 | our $VERSION = '$Rev$'; |
|---|
| 30 | our $RELEASE = '13 Dec 2010'; |
|---|
| 31 | our $NO_PREFS_IN_TOPIC = 1; |
|---|
| 32 | our $SHORTDESCRIPTION = |
|---|
| 33 | 'Link !ExternalSite:Page text to external sites based on aliases defined in a rules topic'; |
|---|
| 34 | |
|---|
| 35 | my $interLinkFormat; |
|---|
| 36 | my $sitePattern; |
|---|
| 37 | my $pagePattern; |
|---|
| 38 | my %interSiteTable; |
|---|
| 39 | |
|---|
| 40 | BEGIN { |
|---|
| 41 | |
|---|
| 42 | # 'Use locale' for internationalisation of Perl sorting and searching - |
|---|
| 43 | if ( $Foswiki::cfg{UseLocale} ) { |
|---|
| 44 | require locale; |
|---|
| 45 | import locale(); |
|---|
| 46 | } |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | # Read preferences and get all InterWiki Site->URL mappings |
|---|
| 50 | sub initPlugin { |
|---|
| 51 | my ( $topic, $web, $user, $installWeb ) = @_; |
|---|
| 52 | |
|---|
| 53 | # Regexes for the Site:page format InterWiki reference |
|---|
| 54 | my $man = $Foswiki::regex{mixedAlphaNum}; |
|---|
| 55 | my $ua = $Foswiki::regex{upperAlpha}; |
|---|
| 56 | %interSiteTable = (); |
|---|
| 57 | $sitePattern = "([$ua][$man]+)"; |
|---|
| 58 | $pagePattern = |
|---|
| 59 | "([${man}_\/][$man" . '"\'\.\/\+\_\,\&\;\:\=\!\?\%\#\@\-\(\)]*?)'; |
|---|
| 60 | |
|---|
| 61 | # Get plugin preferences from InterwikiPlugin topic |
|---|
| 62 | $interLinkFormat = |
|---|
| 63 | Foswiki::Func::getPreferencesValue('INTERWIKIPLUGIN_INTERLINKFORMAT') |
|---|
| 64 | || '<a class="interwikiLink" href="$url" title="$tooltip"><noautolink>$label</noautolink></a>'; |
|---|
| 65 | |
|---|
| 66 | my $rulesTopicPref = |
|---|
| 67 | Foswiki::Func::getPreferencesValue('INTERWIKIPLUGIN_RULESTOPIC') |
|---|
| 68 | || 'InterWikis'; |
|---|
| 69 | my @rulesTopics = split( ',', $rulesTopicPref ); |
|---|
| 70 | foreach my $topic (@rulesTopics) { |
|---|
| 71 | $topic = _trimWhitespace($topic); |
|---|
| 72 | |
|---|
| 73 | my ( $interWeb, $interTopic ) = |
|---|
| 74 | Foswiki::Func::normalizeWebTopicName( $installWeb, $topic ); |
|---|
| 75 | |
|---|
| 76 | if ( |
|---|
| 77 | !Foswiki::Func::checkAccessPermission( |
|---|
| 78 | 'VIEW', $user, undef, $interTopic, $interWeb |
|---|
| 79 | ) |
|---|
| 80 | ) |
|---|
| 81 | { |
|---|
| 82 | Foswiki::Func::writeWarning( |
|---|
| 83 | "InterwikiPlugin: user '$user' did not have permission to read the rules topic at '$interWeb.$interTopic'" |
|---|
| 84 | ); |
|---|
| 85 | return 1; |
|---|
| 86 | } |
|---|
| 87 | my $text = |
|---|
| 88 | Foswiki::Func::readTopicText( $interWeb, $interTopic, undef, 1 ); |
|---|
| 89 | |
|---|
| 90 | # '| alias | URL | ...' table and extract into 'alias', "URL" list |
|---|
| 91 | $text =~ |
|---|
| 92 | s/^\|\s*$sitePattern\s*\|\s*(.*?)\s*\|\s*(.*?)\s*\|.*$/_map($1,$2,$3)/meg; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | $sitePattern = "(" . join( "|", keys %interSiteTable ) . ")"; |
|---|
| 96 | return 1; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | sub _map { |
|---|
| 100 | my ( $site, $url, $tooltip ) = @_; |
|---|
| 101 | if ($site) { |
|---|
| 102 | $interSiteTable{$site}{url} = $url || ''; |
|---|
| 103 | $interSiteTable{$site}{tooltip} = $tooltip || ''; |
|---|
| 104 | } |
|---|
| 105 | return ''; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | sub preRenderingHandler { |
|---|
| 109 | |
|---|
| 110 | # ref in [[ref]] or [[ref][ |
|---|
| 111 | $_[0] =~ |
|---|
| 112 | s/(\[\[)$sitePattern:$pagePattern(\]\]|\]\[[^\]]+\]\])/_link($1,$2,$3,$4)/ge; |
|---|
| 113 | |
|---|
| 114 | # ref in text |
|---|
| 115 | $_[0] =~ |
|---|
| 116 | s/(^|[\s\-\*\(])$sitePattern:$pagePattern(?=[\s\.\,\;\:\!\?\|]*(\s|$))/_link($1,$2,$3)/ge; |
|---|
| 117 | |
|---|
| 118 | return; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | sub _link { |
|---|
| 122 | my ( $prefix, $site, $page, $postfix ) = @_; |
|---|
| 123 | |
|---|
| 124 | $prefix ||= ''; |
|---|
| 125 | $site ||= ''; |
|---|
| 126 | $page ||= ''; |
|---|
| 127 | $postfix ||= ''; |
|---|
| 128 | |
|---|
| 129 | my $text = $prefix; |
|---|
| 130 | if ( defined( $interSiteTable{$site} ) ) { |
|---|
| 131 | my $tooltip = $interSiteTable{$site}{tooltip}; |
|---|
| 132 | my $url = $interSiteTable{$site}{url}; |
|---|
| 133 | $url .= $page unless ( $url =~ /\$page/ ); |
|---|
| 134 | my $label = '$site:$page'; |
|---|
| 135 | |
|---|
| 136 | if ($postfix) { |
|---|
| 137 | |
|---|
| 138 | # [[...]] or [[...][...]] interwiki link |
|---|
| 139 | $text = ''; |
|---|
| 140 | if ( $postfix =~ /^\]\[([^\]]+)/ ) { |
|---|
| 141 | $label = $1; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | my $format = $interLinkFormat; |
|---|
| 146 | $format =~ s/\$url/$url/g; |
|---|
| 147 | $format =~ s/\$tooltip/$tooltip/g; |
|---|
| 148 | $format =~ s/\$label/$label/g; |
|---|
| 149 | $format =~ s/\$site/$site/g; |
|---|
| 150 | $format =~ s/\$page/$page/g; |
|---|
| 151 | $text .= $format; |
|---|
| 152 | } |
|---|
| 153 | else { |
|---|
| 154 | $text .= "$site\:$page$postfix"; |
|---|
| 155 | } |
|---|
| 156 | return $text; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | sub _trimWhitespace { |
|---|
| 160 | my $string = shift; |
|---|
| 161 | $string =~ s/^\s+//; |
|---|
| 162 | $string =~ s/\s+$//; |
|---|
| 163 | return $string; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | 1; |
|---|
| 167 | __END__ |
|---|
| 168 | Foswiki - The Free and Open Source Wiki, http://foswiki.org/ |
|---|
| 169 | |
|---|
| 170 | Copyright (C) 2008-2010 Foswiki Contributors. Foswiki Contributors |
|---|
| 171 | are listed in the AUTHORS file in the root of this distribution. |
|---|
| 172 | NOTE: Please extend that file, not this notice. |
|---|
| 173 | |
|---|
| 174 | Additional copyrights apply to some or all of the code in this |
|---|
| 175 | file as follows: |
|---|
| 176 | Copyright (C) 2000-2003 Andrea Sterbini, a.sterbini@flashnet.it |
|---|
| 177 | Copyright (C) 2001-2007 Peter Thoeny, peter@thoeny.com |
|---|
| 178 | |
|---|
| 179 | This program is free software; you can redistribute it and/or |
|---|
| 180 | modify it under the terms of the GNU General Public License |
|---|
| 181 | as published by the Free Software Foundation; either version 2 |
|---|
| 182 | of the License, or (at your option) any later version. For |
|---|
| 183 | more details read LICENSE in the root of this distribution. |
|---|
| 184 | |
|---|
| 185 | This program is distributed in the hope that it will be useful, |
|---|
| 186 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 187 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|---|
| 188 | |
|---|
| 189 | As per the GPL, removal of this notice is prohibited. |
|---|