Bug 16031: sitemap.pl shouldn't append protocol to OPACBaseURL
[koha.git] / misc / cronjobs / sitemap.pl
1 #!/usr/bin/perl
2
3 # Copyright 2015 Tamil s.a.r.l.
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 package Main;
21
22 use Modern::Perl;
23 use utf8;
24 use Pod::Usage;
25 use Getopt::Long;
26 use C4::Biblio;
27 use Koha::Sitemapper;
28
29
30 my ($verbose, $help, $url, $dir, $short) = (0, 0, '', '.', 1);
31 GetOptions(
32     'verbose'   => \$verbose,
33     'help'      => \$help,
34     'url=s'     => \$url,
35     'dir=s'     => \$dir,
36     'short!'    => \$short,
37 );
38
39 sub usage {
40     pod2usage( -verbose => 2 );
41     exit;
42 }
43
44 usage() if $help;
45
46 unless ($url) {
47     $url = C4::Context->preference("OPACBaseURL");
48     unless ($url) {
49         say "OPACBaseURL syspref isn't defined. You can use --url parameter.";
50         exit;
51     }
52 }
53 $url =~ s/\/*$//g;
54
55 my $sitemaper = Koha::Sitemapper->new(
56     verbose => $verbose,
57     url     => $url,
58     dir     => $dir,
59     short   => $short,
60 );
61 $sitemaper->run();
62
63
64 =head1 USAGE
65
66 =over
67
68 =item sitemap.pl [--verbose|--help|--short|--noshort|--url|--dir]
69
70 =back
71
72 =head1 SYNOPSIS
73
74   sitemap.pl --verbose
75   sitemap.pl --noshort --url /home/koha/mylibrary/www
76
77 =head1 DESCRIPTION
78
79 Process all biblio records from a Koha instance and generate Sitemap files
80 complying with this protocol as described on L<http://sitemaps.org>. The goal of
81 this script is to be able to provide to search engines direct access to biblio
82 records. It avoid leaving search engine browsing Koha OPAC and so generating
83 a lot of traffic, and workload, for a bad result.
84
85 A file name F<sitemapindex.xml> is generated. It contains references to Sitemap
86 multiples files. Each file contains at most 50,000 urls, and is named
87 F<sitemapXXXX.xml>.
88
89 The files must be stored on Koha OPAC root directory, ie
90 F<<koha-root>/koha-tmpl/>. Place also in this directory a F<robots.txt> file
91 like this one:
92
93  Sitemap: sitemapindex.xml
94  User-agent: *
95  Disallow: /cgi-bin/
96
97 =head1 PARAMETERS
98
99 =over
100
101 =item B<--url=Koha OPAC base URL>
102
103 If omitted, OPACBaseURL syspref is used.
104
105 =item B<--short|noshort>
106
107 By default, --short. With --short, URL to bib record ends with
108 /bib/biblionumber. With --noshort, URL ends with
109 /cgi-bin/koha/opac-detail.pl?biblionumber=bibnum
110
111 =item B<--dir>
112
113 Directory where to write sitemap files. By default, the current directory.
114
115 =item B<--verbose|-v>
116
117 Enable script verbose mode: a message is displayed for each 10,000 biblio
118 records processed.
119
120 =item B<--help|-h>
121
122 Print this help page.
123
124 =back
125
126 =cut