The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl

=head1 USE

    perl link-checker --base='http://localhost/sites/mysite/'

Traverses the pages and checks for inner broken links.

=head1 FLAGS

=over 4

=item * --base '$URL'

The base URL of the web site (and the start URL for the traversal.

=item * --pre-skip '$REGEX'

A regular expression for matching a URL so it won't be checked for outgoing
links (but will be tried to be followed). Can be specified more than once.

=item * --before-insert-skip '$REGEX'

A regular expression for preventing URLs from being retrieved in the first
place. Can be specified more than once.

=back

=head1 LIMITATIONS

=over 4

=item * Does not save and restore the current state upon a failure.

=item * Uses a regular expression to get rid of the anchor part (#my_id)

=item * Does not handle JavaScript-generated HTML properly.

=back

=cut


use strict;
use warnings;

use Getopt::Long;
use WWW::Mechanize;

use List::MoreUtils qw( any none );

my $base_url;

my @pre_skip_texts;
my @before_insert_skips_texts;
GetOptions(
    'base=s' => \$base_url,
    'pre-skip=s' => \@pre_skip_texts,
    'before-insert-skip=s' => \@before_insert_skips_texts,
)
or die "GetOptions failed.";

my @pre_skip_regexes = map { qr/$_/ } @pre_skip_texts;
my @before_insert_skips_regexes = map { qr/$_/ } @before_insert_skips_texts;

my $start_url = $base_url;

my @stack;

push @stack, { url => $start_url, from => undef(), },;

my %encountered_urls;

$encountered_urls{$start_url} = 1;

STACK:
while (my $url_rec = pop(@stack))
{
    my $url = $url_rec->{'url'};
    print "Checking SRC URL '$url'\n";

    my $mech = WWW::Mechanize->new();
    eval {
        $mech->get( $url );
    };

    if ($@)
    {
        die "SRC URL $url_rec->{from} points to '$url'.";
    }

    if (any { $url =~ $_} @pre_skip_regexes)
    {
        next STACK;
    }

    foreach my $link ($mech->links())
    {
        my $dest_url = $link->url_abs() . "";
        $dest_url =~ s{#[^#]+\z}{}ms;
        if ((!exists($encountered_urls{$dest_url})) and
            $dest_url =~ m{\A\Q$base_url\E}ms and
            (none { $dest_url =~ $_ } @before_insert_skips_regexes)
        )
        {
            $encountered_urls{$dest_url} = 1;
            push @stack, { url => $dest_url, from => $url, };
        }
    }
}

=head1 COPYRIGHT & LICENSE

Copyright 2012 by Shlomi Fish

This program is distributed under the MIT (X11) License:
L<http://www.opensource.org/licenses/mit-license.php>

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

=cut