Skip to content

Commit

Permalink
Item14693: modernized code
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDaum committed Jun 2, 2018
1 parent 51cd248 commit 18c519f
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 102 deletions.
15 changes: 8 additions & 7 deletions .gitignore
@@ -1,8 +1,9 @@
*.swp
WebLinkPlugin.md5
WebLinkPlugin.sha1
WebLinkPlugin.tgz
WebLinkPlugin.txt
WebLinkPlugin.zip
WebLinkPlugin_installer
WebLinkPlugin_installer.pl
*,v
/WebLinkPlugin.md5
/WebLinkPlugin.sha1
/WebLinkPlugin.tgz
/WebLinkPlugin.txt
/WebLinkPlugin.zip
/WebLinkPlugin_installer
/WebLinkPlugin_installer.pl
3 changes: 2 additions & 1 deletion data/System/VarWEBLINK.txt
@@ -1,4 +1,5 @@
%META:TOPICINFO{author="ProjectContributor" comment="" date="1291041934" format="1.1" version="2"}%
%META:TOPICINFO{author="ProjectContributor" comment="" date="1527934709" format="1.1" version="1"}%
%META:TOPICPARENT{name="WebLinkPlugin"}%
#VarWEBLINK
---+++ WEBLINK
* create a link to a web
Expand Down
35 changes: 18 additions & 17 deletions data/System/WebLinkPlugin.txt
@@ -1,6 +1,6 @@
%META:TOPICINFO{author="ProjectContributor" comment="" date="1291041934" format="1.1" version="2"}%
%META:TOPICINFO{author="ProjectContributor" comment="" date="1527934709" format="1.1" version="1"}%
---+!! %TOPIC%
%SHORTDESCRIPTION%
%FORMFIELD{"Description"}%

%TOC%

Expand All @@ -17,24 +17,25 @@ Otherwise the normal topic name will be displayed as link text.
%INCLUDE{"VarWEBLINK"}%

---++ Installation Instructions

%$INSTALL_INSTRUCTIONS%

---++ Info
<!--
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
-->

| Author(s): | Michael Daum|
| Copyright: | &copy; 2010-2014 Michael Daum http://michaeldaumconsulting.com |
| License: | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
---++ Dependencies
%$DEPENDENCIES%

---++ Change History
| 02 Jun 2018 | modernized code; depend on new !TopicTitlePlugin |
| 14 Mar 2013 | removed use of !DBCachePlugin |
| 07 May 2012 | added web summary as defined in the WEBSUMMARY preference variable |
| 30 Sep 2011 | made dependency to Foswiki:Extensions/DBCachePlugin optional |
| 29 Nov 2010 | externalized from !NatSkinPlugin |
| Dependencies: | %$DEPENDENCIES% |
| Home page: | Foswiki:Extensions/%TOPIC% |
| Support: | Foswiki:Support/%TOPIC% |

%META:FORM{name="PackageForm"}%
%META:FIELD{name="Author" title="Author" value="Michael Daum"}%
%META:FIELD{name="Copyright" title="Copyright" value="&copy; 2010-2018 Michael Daum http://michaeldaumconsulting.com"}%
%META:FIELD{name="Description" title="Description" value="%25$SHORTDESCRIPTION%25"}%
%META:FIELD{name="Home" title="Home" value="Foswiki:Extensions/%TOPIC%"}%
%META:FIELD{name="License" title="License" value="[[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]]"}%
%META:FIELD{name="Release" title="Release" value="%$RELEASE%"}%
%META:FIELD{name="Repository" title="Repository" value="https://github.com/foswiki/%TOPIC%"}%
%META:FIELD{name="Support" title="Support" value="Foswiki:Support/%TOPIC%"}%
%META:FIELD{name="Version" title="Version" value="%$VERSION%"}%
31 changes: 20 additions & 11 deletions lib/Foswiki/Plugins/WebLinkPlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# WebLinkPlugin is Copyright (C) 2010-2014 Michael Daum http://michaeldaumconsulting.com
# WebLinkPlugin is Copyright (C) 2010-2018 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -26,13 +26,11 @@ use warnings;

use Foswiki::Func ();

our $VERSION = '1.31';
our $RELEASE = '1.31';
our $VERSION = '2.00';
our $RELEASE = '2 Jun 2018';
our $SHORTDESCRIPTION = 'A parametrized %WEB macro';
our $NO_PREFS_IN_TOPIC = 1;
our $baseWeb;
our $baseTopic;
our $doneInit;
our $core;

=begin TML
Expand All @@ -41,16 +39,27 @@ our $doneInit;
=cut

sub initPlugin {
($baseTopic, $baseWeb) = @_;

Foswiki::Func::registerTagHandler('WEBLINK', sub {
require Foswiki::Plugins::WebLinkPlugin::Core;
Foswiki::Plugins::WebLinkPlugin::Core::init($baseWeb, $baseTopic);
return Foswiki::Plugins::WebLinkPlugin::Core::WEBLINK(@_);
return getCore()->handleWEBLINK(@_);
});

$doneInit = 0;
return 1;
}

sub finishPlugin {
undef $core;
}

sub getCore {

unless (defined $core) {
require Foswiki::Plugins::WebLinkPlugin::Core;
$core = Foswiki::Plugins::WebLinkPlugin::Core->new();
}

return $core;
}


1;
65 changes: 14 additions & 51 deletions lib/Foswiki/Plugins/WebLinkPlugin/Core.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# WebLinkPlugin is Copyright (C) 2010-2014 Michael Daum http://michaeldaumconsulting.com
# WebLinkPlugin is Copyright (C) 2010-2018 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -17,18 +17,14 @@ package Foswiki::Plugins::WebLinkPlugin::Core;

use strict;
use warnings;
use Foswiki::Func ();

=begin TML
---+ package WebLinkPlugin::Core
=cut

use Foswiki::Func ();

our $baseWeb;
our $baseTopic;

use constant TRACE => 0; # toggle me

=begin TML
Expand All @@ -45,14 +41,18 @@ sub writeDebug {

=begin TML
---++ init($baseWeb, $baseTopic)
---++ new
initializer for the plugin core; called before any macro hanlder is executed
constructor
=cut

sub init {
($baseWeb, $baseTopic) = @_;
sub new {
my $class = shift;

my $this = bless({@_}, $class);

return $this;
}

=begin TML
Expand All @@ -63,8 +63,8 @@ implementation of this macro
=cut

sub WEBLINK {
my ($session, $params, $topic, $web) = @_;
sub handleWEBLINK {
my ($this, $session, $params, $topic, $web) = @_;

writeDebug("called WEBLINK()");

Expand Down Expand Up @@ -94,7 +94,7 @@ sub WEBLINK {
$session->getScriptUrl(0, 'view', $theWeb, $homeTopic);

# unset the marker if this is not the current web
$theMarker = '' unless $theWeb eq $baseWeb;
$theMarker = '' unless $theWeb eq $session->{webName};

# normalize web name
$theWeb =~ s/\//\./go;
Expand All @@ -107,7 +107,7 @@ sub WEBLINK {

my $title = '';
if ($theFormat =~ /\$title/) {
$title = getTopicTitle($theWeb, $homeTopic);
$title = Foswiki::Func::getTopicTitle($theWeb, $homeTopic);
$title = $theName if $title eq $homeTopic;
}

Expand All @@ -126,41 +126,4 @@ sub WEBLINK {
return Foswiki::Func::decodeFormatTokens($result);
}

sub getTopicTitle {
my ($web, $topic) = @_;

my ($meta, $text) = Foswiki::Func::readTopic($web, $topic);

if ($Foswiki::cfg{SecureTopicTitles}) {
my $wikiName = Foswiki::Func::getWikiName();
return $topic
unless Foswiki::Func::checkAccessPermission('VIEW', $wikiName, $text, $topic, $web, $meta);
}

# read the formfield value
my $title = $meta->get('FIELD', 'TopicTitle');
$title = $title->{value} if $title;

# read the topic preference
unless ($title) {
$title = $meta->get('PREFERENCE', 'TOPICTITLE');
$title = $title->{value} if $title;
}

# read the preference
unless ($title) {
Foswiki::Func::pushTopicContext($web, $topic);
$title = Foswiki::Func::getPreferencesValue('TOPICTITLE');
Foswiki::Func::popTopicContext();
}

# default to topic name
$title ||= $topic;

$title =~ s/\s*$//;
$title =~ s/^\s*//;

return $title;
}

1;
2 changes: 2 additions & 0 deletions lib/Foswiki/Plugins/WebLinkPlugin/DEPENDENCIES
@@ -1 +1,3 @@
Foswiki::Plugins::DBCachePlugin,>=3.70,perl,Optional
ONLYIF ( $Foswiki::Plugins::VERSION < 2.6)
Foswiki::Plugins::TopicTitlePlugin,>1.00,perl,Required for Foswiki < 2.2
1 change: 1 addition & 0 deletions lib/Foswiki/Plugins/WebLinkPlugin/MANIFEST
@@ -1,3 +1,4 @@
!noci
data/System/WebLinkPlugin.txt 0644
data/System/VarWEBLINK.txt 0644
lib/Foswiki/Plugins/WebLinkPlugin/Core.pm 0644
Expand Down
16 changes: 1 addition & 15 deletions lib/Foswiki/Plugins/WebLinkPlugin/build.pl
@@ -1,24 +1,10 @@
#!/usr/bin/perl -w
#!/usr/bin/env perl
BEGIN { unshift @INC, split( /:/, $ENV{FOSWIKI_LIBS} ); }
use Foswiki::Contrib::Build;

# Create the build object
$build = new Foswiki::Contrib::Build('WebLinkPlugin');

# (Optional) Set the details of the repository for uploads.
# This can be any web on any accessible Foswiki installation.
# These defaults will be used when expanding tokens in .txt
# files, but be warned, they can be overridden at upload time!

# name of web to upload to
$build->{UPLOADTARGETWEB} = 'Extensions';
# Full URL of pub directory
$build->{UPLOADTARGETPUB} = 'http://foswiki.org/pub';
# Full URL of bin directory
$build->{UPLOADTARGETSCRIPT} = 'http://foswiki.org/bin';
# Script extension
$build->{UPLOADTARGETSUFFIX} = '';

# Build the target on the command line, or the default target
$build->build($build->{target});

0 comments on commit 18c519f

Please sign in to comment.