Skip to content

Commit

Permalink
Item1898: initial import from old tmwiki version
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/ValidateUrlsPlugin@4616 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenDowideit authored and SvenDowideit committed Aug 7, 2009
0 parents commit ee02597
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 0 deletions.
50 changes: 50 additions & 0 deletions data/System/ValidateUrlsPlugin.txt
@@ -0,0 +1,50 @@
---+!! !ValidateUrlsPlugin
<!--
One line description, required for extensions repository catalog.
* Set SHORTDESCRIPTION = Validates external Urls in Wiki topics
-->
%SHORTDESCRIPTION%

%TOC%

---++ Usage

This extension is best run from the command line, perhaps from a monthly cronjob..
<verbatim>
cd twiki/bin
./rest ValidateUrlsPlugin.getExternalLinks web Sandbox
</verbatim>
This will generate or update =Sandbox.WebExternalURLsReport= for the Sandbox web, listing all the url's it finds in the topics of the Sandbox web, listed either as broken, or working (from the point of view of the server).

If you run this rest handler from your browser, it will try to update the topic (assuming you have (admin) permission to write to that web/topic), and it will _not_ attempt to validate the urls.

The topics are set to be viewable only by the %USERSWEB%.AdminGroup, so that there is less risk of exposing url's listed on view restricted topics.
This can be changed by setting ={Plugins}{ValidateUrlsPlugin}{ReportViewPermission}= to the group that you want to allow (set to %USERSWEB%.WikiGuest if you're ok with the list being public).

---+++ existing reports
%SEARCH{
"WebExternalURLsReport"
scope="topic"
web="all"
noheader="---+++ External Url reports"
format=" * $nop$web - $web.$topic"
nonoise="on"
}%

---++ Installation Instructions



---++ Plugin Info

| Plugin Author(s): | Sven Dowideit - [[http://fosiki.com][fosiki]] |
| Copyright: | &copy; 2008,2009 SvenDowideit@fosiki.com |
| License: | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] |
| Plugin Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| Aug 2009 | initial public release for foswiki |
| Dependencies: | None |
| Plugin Home: | http://foswiki.org/Extensions/ValidateUrlsPlugin |


<!-- Do _not_ attempt to edit this topic; it is auto-generated. Please add comments/questions/remarks to the feedback topic on twiki.org instead. -->
193 changes: 193 additions & 0 deletions lib/Foswiki/Plugins/ValidateUrlsPlugin.pm
@@ -0,0 +1,193 @@
# Plugin for Foswiki Collaboration Platform, http://foswiki.org/
#
# Copyright 2008,2009 Sven Dowideit SvenDowideit@fosiki.com
#
# 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 3
# 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, published at
# http://www.gnu.org/copyleft/gpl.html

package Foswiki::Plugins::ValidateUrlsPlugin;

# Always use strict to enforce variable scoping
use strict;

require Foswiki::Func; # The plugins API
require Foswiki::Plugins; # For the API version

use vars
qw( $VERSION $RELEASE $SHORTDESCRIPTION $debug $pluginName $NO_PREFS_IN_TOPIC );
$VERSION = '$Rev: 1049 (14 Oct 2008) $';
$SHORTDESCRIPTION = 'Validates external Urls in Wiki topics';
$NO_PREFS_IN_TOPIC = 1;

# Name of this Plugin, only used in this module
$pluginName = 'ValidateUrlsPlugin';

sub initPlugin {
my ( $topic, $web, $user, $installWeb ) = @_;

Foswiki::Func::registerRESTHandler( 'getExternalLinks', \&getExternalLinks );
return 1;
}

=pod
---++ getExternalLinks($session) -> $text
searches through the specified web and lists all the explicitly listed external url's
=cut

sub getExternalLinks {

#my ($session) = @_;

#TODO: make a lease file, and only allow the first request to run..
#TODO: secure things - we don't want to be revealing url's that are on restricted access topics to everyone.

my %urlHash;
my $urls = \%urlHash;

my $web = Foswiki::Func::getCgiQuery()->param('web');
$web =~ /(.*)/;
$web = $1;

my @topics = Foswiki::Func::getTopicList($web);
my $result =
Foswiki::Func::searchInWebContent( '.*http.*', $web, \@topics,
{ type => 'regex', casesensitive => 0, files_without_match => 0 } );
foreach my $topic ( keys %$result ) {
next if ( $topic eq 'WebExternalURLsReport' );
foreach my $matching_line ( @{ $result->{$topic} } ) {

#square brackets can contain spaces, so we need to
$matching_line =~
s/\[\[($Foswiki::regex{linkProtocolPattern}:[^\]\[\n]+)\](\[([^\]\n]+)\])?\]/_externalLink($urls, $web, $topic, $1)/geo;

#regex from Foswiki::Render::getRenderedVersion
#$matching_line =~ s/(^|[-*\s(|])($Foswiki::regex{linkProtocolPattern}:([^\s<>"]+[^\s*.,!?;:"')<|]))/$1._externalLink($urls, $web, $topic, $2)/geo;
$matching_line =~
s/($Foswiki::regex{linkProtocolPattern}:([^\s<>'"\]]+[^\s*.,!?;:"')<|\]]))/_externalLink($urls, $web, $topic, $1)/geo;
}
}

#rest web response - not that useful..
my @sortedKeys =
sort { $urls->{$b}->{count} <=> $urls->{$a}->{count} } keys( %{$urls} );
my $id = 0;
my $return = join(
"<br /></div>\n",
map {
"<div id='section"
. ++$id
. "'><span id='testUrl$id' >$_</span> "
. (
defined( $urls->{$_}->{response} )
? ' (' . $urls->{$_}->{response} . ') '
: '' )
. ', '
. $urls->{$_}->{count} . ', '
. join( ', ', keys( %{ $urls->{$_}->{topics} } ) )
} @sortedKeys
);

my @brokenLinks;
my @okLinks;

foreach my $key (@sortedKeys) {
if ( $urls->{$key}->{response} eq 200 ) {
push( @okLinks, $key );
}
else {
push( @brokenLinks, $key );
}
}

my $viewPermission = $Foswiki::cfg{Plugins}{$pluginName}{ReportViewPermission} || 'AdminGroup';
if ((!defined($viewPermission)) || ($viewPermission ne '')) {
$viewPermission = "\n * Set ALLOWTOPICVIEW = %USERSWEB%.".Foswiki::Func::normalizeWebTopicName('%USERSWEB%', $viewPermission)."\n";
}
my $generatedTopic =
"\n---++ External URL's in $web"
. "\n\nthis topic is generated by %SYSTEMWEB%.$pluginName, do not edit it, your changes will be lost. \n\n"
. "\n---+++ Broken URL's\n"
. join(
"\n",
map {
" * [[$_][$_]] - "
. (
defined( $urls->{$_}->{response} )
? ' (' . $urls->{$_}->{response} . ') '
: '' )
. ', '
. $urls->{$_}->{count} . ', '
. join( ', ', keys( %{ $urls->{$_}->{topics} } ) )
} @brokenLinks
)
. "\n---+++ working URL's\n"
. join(
"\n",
map {
" * [[$_][$_]] - "
. (
defined( $urls->{$_}->{response} )
? ' (' . $urls->{$_}->{response} . ') '
: '' )
. ', '
. $urls->{$_}->{count} . ', '
. join( ', ', keys( %{ $urls->{$_}->{topics} } ) )
} @okLinks
) . "\n$viewPermission\n";
if ( Foswiki::Func::getContext()->{'command_line'} ) {

#save topic text without checking permission... mmm
Foswiki::Func::saveTopicText( $web, 'WebExternalURLsReport',
$generatedTopic, 1, 1 );
}
else {
my ( $meta, $text ) =
Foswiki::Func::readTopic( $web, 'WebExternalURLsReport' );
Foswiki::Func::saveTopic( $web, 'WebExternalURLsReport', $meta,
$generatedTopic, { forcenewrevision => 1 } );
}

return
"$web contains "
. scalar( keys( %{$urls} ) )
. " urls: <br />$return\n\n";
}

sub _externalLink {
my ( $urls, $web, $topic, $url ) = @_;
$urls->{$url} = { count => 0, topics => {} }
unless ( defined( $urls->{$url} ) );
$urls->{$url}->{count}++;
$urls->{$url}->{topics}{"$web.$topic"}++;
if ( Foswiki::Func::getContext()->{'command_line'} ) {
my $show = Foswiki::Func::getCgiQuery()->param('show') || 'all';
if ( !defined( $urls->{$url}->{response} ) ) {
my $response = Foswiki::Func::getExternalResource($url);
sleep(1); #be nice to everyone.
$urls->{$url}->{response} = $response->code();
if ( ( $show eq 'all' )
|| ( ( $show eq 'ok' ) && ( $response->code() == 200 ) )
|| ( ( $show eq 'broken' ) && ( $response->code() != 200 ) ) )
{
print STDERR $urls->{$url}->{response} . " : $url \n";
}
else {
print STDERR '.';
}
}
}
return '';
}

1;
7 changes: 7 additions & 0 deletions lib/Foswiki/Plugins/ValidateUrlsPlugin/Config.spec
@@ -0,0 +1,7 @@
# ---+ Extensions
# ---++ ValidateUrlsPlugin
# **TEXT**
# restrict the WebExternalURLsReport topics to the following users. defaults to AdminGroup for safety of possibly delicate url's
# set to WikiGuest for public Wiki's
$Foswiki::cfg{Plugins}{ValidateUrlsPlugin}{ReportViewPermission} = 'AdminGroup';

3 changes: 3 additions & 0 deletions lib/Foswiki/Plugins/ValidateUrlsPlugin/MANIFEST
@@ -0,0 +1,3 @@
data/System/ValidateUrlsPlugin.txt
lib/Foswiki/Plugins/ValidateUrlsPlugin/Config.spec
lib/Foswiki/Plugins/ValidateUrlsPlugin.pm

0 comments on commit ee02597

Please sign in to comment.