Koha/misc/cronjobs/check-url-quick.pl
Jonathan Druart 9d6d641d1f Bug 17600: Standardize our EXPORT_OK
On bug 17591 we discovered that there was something weird going on with
the way we export and use subroutines/modules.
This patch tries to standardize our EXPORT to use EXPORT_OK only.

That way we will need to explicitely define the subroutine we want to
use from a module.

This patch is a squashed version of:
Bug 17600: After export.pl
Bug 17600: After perlimport
Bug 17600: Manual changes
Bug 17600: Other manual changes after second perlimports run
Bug 17600: Fix tests

And a lot of other manual changes.

export.pl is a dirty script that can be found on bug 17600.

"perlimport" is:
git clone https://github.com/oalders/App-perlimports.git
cd App-perlimports/
cpanm --installdeps .
export PERL5LIB="$PERL5LIB:/kohadevbox/koha/App-perlimports/lib"
find . \( -name "*.pl" -o -name "*.pm" \) -exec perl App-perlimports/script/perlimports --inplace-edit --no-preserve-unused --filename {} \;

The ideas of this patch are to:
* use EXPORT_OK instead of EXPORT
* perltidy the EXPORT_OK list
* remove '&' before the subroutine names
* remove some uneeded use statements
* explicitely import the subroutines we need within the controllers or
modules

Note that the private subroutines (starting with _) should not be
exported (and not used from outside of the module except from tests).

EXPORT vs EXPORT_OK (from
https://www.thegeekstuff.com/2010/06/perl-exporter-examples/)
"""
Export allows to export the functions and variables of modules to user’s namespace using the standard import method. This way, we don’t need to create the objects for the modules to access it’s members.

@EXPORT and @EXPORT_OK are the two main variables used during export operation.

@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

@EXPORT_OK does export of symbols on demand basis.
"""

If this patch caused a conflict with a patch you wrote prior to its
push:
* Make sure you are not reintroducing a "use" statement that has been
removed
* "$subroutine" is not exported by the C4::$MODULE module
means that you need to add the subroutine to the @EXPORT_OK list
* Bareword "$subroutine" not allowed while "strict subs"
means that you didn't imported the subroutine from the module:
  - use $MODULE qw( $subroutine list );
You can also use the fully qualified namespace: C4::$MODULE::$subroutine

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2021-07-16 08:58:47 +02:00

213 lines
5.9 KiB
Perl
Executable file

#!/usr/bin/perl
# Copyright 2012 Tamil s.a.r.l.
#
# This file is part of Koha.
#
# Koha 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.
#
# Koha 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 Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Pod::Usage qw( pod2usage );
use Getopt::Long qw( GetOptions );
use Koha::Script -cron;
use C4::Context;
use C4::Biblio qw( GetMarcBiblio );
use AnyEvent;
use AnyEvent::HTTP qw( http_request );
use Encode qw( encode_utf8 );
my ( $verbose, $help, $html ) = ( 0, 0, 0 );
my ( $host, $host_intranet ) = ( '', '' );
my ( $timeout, $maxconn ) = ( 10, 200 );
my @tags;
my $uriedit = "/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=";
my $user_agent = 'Mozilla/5.0 (compatible; U; Koha checkurl)';
GetOptions(
'verbose' => \$verbose,
'html' => \$html,
'h|help' => \$help,
'host=s' => \$host,
'host-intranet=s' => \$host_intranet,
'timeout=i' => \$timeout,
'maxconn=i' => \$maxconn,
'tags=s{,}' => \@tags,
);
# Validate tags to check
{
my %h = map { $_ => undef } @tags;
@tags = sort keys %h;
my @invalids;
for (@tags) {
push @invalids, $_ unless /^\d{3}$/;
}
if (@invalids) {
say "Invalid tag(s): ", join( ' ', @invalids );
exit;
}
push @tags, '856' unless @tags;
}
sub usage {
pod2usage( -verbose => 2 );
exit;
}
sub report {
my ( $hdr, $biblionumber, $url ) = @_;
print $html
? "<tr>\n <td><a href=\""
. $host_intranet
. $uriedit
. $biblionumber
. "\">$biblionumber</a>"
. "</td>\n <td>$url</td>\n <td>"
. "$hdr->{Status} $hdr->{Reason}</td>\n</tr>\n"
: "$biblionumber\t$url\t" . "$hdr->{Status} $hdr->{Reason}\n";
}
# Check all URLs from all current Koha biblio records
sub check_all_url {
my $sth = C4::Context->dbh->prepare(
"SELECT biblionumber FROM biblioitems ORDER BY biblionumber");
$sth->execute;
my $count = 0; # Number of requested URL
my $cv = AnyEvent->condvar;
say "<html>\n<body>\n<div id=\"checkurl\">\n<table>" if $html;
my $idle = AnyEvent->timer(
interval => .3,
cb => sub {
return if $count > $maxconn;
while ( my ($biblionumber) = $sth->fetchrow ) {
my $record = GetMarcBiblio({ biblionumber => $biblionumber });
for my $tag (@tags) {
foreach my $field ( $record->field($tag) ) {
my $url = $field->subfield('u');
next unless $url;
$url = "$host/$url" unless $url =~ /^http/i;
$url = encode_utf8($url);
$count++;
http_request(
HEAD => $url,
headers => { 'user-agent' => $user_agent },
timeout => $timeout,
sub {
my ( undef, $hdr ) = @_;
$count--;
report( $hdr, $biblionumber, $url )
if $hdr->{Status} !~ /^2/ || $verbose;
},
);
}
}
return if $count > $maxconn;
}
$cv->send;
}
);
$cv->recv;
$idle = undef;
# Few more time for pending requests
$cv = AnyEvent->condvar;
my $timer = AnyEvent->timer(
after => $timeout,
interval => $timeout,
cb => sub { $cv->send if $count == 0; }
);
$cv->recv;
say "</table>\n</div>\n</body>\n</html>" if $html;
}
usage() if $help;
if ( $html && !$host_intranet ) {
if ($host) {
$host_intranet = $host;
}
else {
say
"Error: host-intranet parameter or host must be provided in html mode";
exit;
}
}
check_all_url();
=head1 NAME
check-url-quick.pl - Check URLs from biblio records
=head1 USAGE
=over
=item check-url-quick [--verbose|--help|--html] [--tags 310 856] [--host=http://default.tld]
[--host-intranet]
Scan all URLs found by default in 856$u of bib records and display if resources
are available or not. HTTP requests are sent in parallel for efficiency, and
speed. This script replaces check-url.pl script.
=back
=head1 PARAMETERS
=over
=item B<--host=http://default.tld>
Server host used when URL doesn't have one, ie doesn't begin with 'http:'.
For example, if --host=http://www.mylib.com, then when 856$u contains
'img/image.jpg', the url checked is: http://www.mylib.com/image.jpg'.
=item B<--tags>
Tags containing URLs in $u subfields. If not provided, 856 tag is checked. Multiple tags can be specified, for example:
check-url-quick.pl --tags 310 410 856
=item B<--verbose|-v>
Outputs both successful and failed URLs.
=item B<--html>
Formats output in HTML. The result can be redirected to a file
accessible by http. This way, it's possible to link directly to biblio
record in edit mode. With this parameter B<--host-intranet> is required.
=item B<--host-intranet=http://koha-pro.tld>
Server host used to link to biblio record editing page in Koha intranet
interface.
=item B<--timeout=10>
Timeout for fetching URLs. By default 10 seconds.
=item B<--maxconn=1000>
Number of simulaneous HTTP requests. By default 200 connexions.
=item B<--help|-h>
Print this help page.
=back
=cut