5 # script to search for url's in lyxfiles
6 # and testing their validity.
8 # Syntax: search_url.pl [(filesToScan|(ignored|reverted|extra|selected)URLS)={path_to_control]*
9 # Param value is a path to a file containing list of xxx:
10 # filesToScan={xxx = lyx-file-names to be scanned for}
11 # ignoredURLS={xxx = urls that are discarded from test}
12 # revertedURLS={xxx = urls that should fail, to test the test with invalid urls}
13 # extraURLS={xxx = urls which should be also checked}
15 # This file is free software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public
17 # License as published by the Free Software Foundation; either
18 # version 2 of the License, or (at your option) any later version.
20 # This software is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 # General Public License for more details.
25 # You should have received a copy of the GNU General Public
26 # License along with this software; if not, write to the Free Software
27 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 # Copyright (c) 2013 Kornel Benko <kornel@lyx.org>
30 # (c) 2013 Scott Kostyshak <skotysh@lyx.org>
36 my $p = File::Spec->rel2abs(__FILE__);
37 $p =~ s/[\/\\]?[^\/\\]+$//;
44 use POSIX qw(locale_h);
46 setlocale(LC_CTYPE, "");
47 setlocale(LC_MESSAGES, "en_US.UTF-8");
51 my %revertedURLS = ();
53 my %selectedURLS = ();
55 my $checkSelectedOnly = 0;
57 die("Bad argument \"$arg\"") if ($arg !~ /=/);
58 my ($type,$val) = split("=", $arg);
59 if ($type eq "filesToScan") {
60 #The file should be a list of files to search in
61 if (open(FLIST, $val)) {
62 while (my $l = <FLIST>) {
69 elsif ($type eq "ignoredURLS") {
70 &readUrls($val, \%ignoredURLS);
72 elsif ($type eq "revertedURLS") {
73 &readUrls($val, \%revertedURLS);
75 elsif ($type eq "extraURLS") {
76 &readUrls($val, \%extraURLS);
78 elsif ($type eq "selectedURLS") {
79 $checkSelectedOnly = 1;
80 &readUrls($val, \%selectedURLS);
83 die("Invalid argument \"$arg\"");
87 my @urls = sort keys %URLS, keys %extraURLS;
93 if (defined($selectedURLS{$u})) {
94 ${selectedURLS}{$u}->{count} += 1;
96 if (defined($ignoredURLS{$u})) {
97 $ignoredURLS{$u}->{count} += 1;
100 next if ($checkSelectedOnly && ! defined($selectedURLS{$u}));
102 print "Checking '$u': ";
105 $res = &check_url($u);
114 $prnt = "Failed, caught error: $_";
118 my $printSourceFiles = 0;
119 my $err_txt = "Error url:";
121 if ($res || $checkSelectedOnly) {
122 $printSourceFiles = 1;
124 if ($res && defined($revertedURLS{$u})) {
125 $err_txt = "Failed url:";
127 $res = ! $res if (defined($revertedURLS{$u}));
128 if ($res || $checkSelectedOnly) {
129 print "$err_txt \"$u\"\n";
131 if ($printSourceFiles) {
132 if (defined($URLS{$u})) {
133 for my $f(sort keys %{$URLS{$u}}) {
134 my $lines = ":" . join(',', @{$URLS{$u}->{$f}});
145 &printNotUsedURLS("Ignored", \%ignoredURLS);
146 &printNotUsedURLS("Selected", \%selectedURLS);
147 &printNotUsedURLS("KnownInvalid", \%extraURLS);
150 print "\n$errorcount URL-tests failed out of $URLScount\n\n";
153 ###############################################################################
155 sub printNotUsedURLS($$)
157 my ($txt, $rURLS) = @_;
159 for my $u ( sort keys %{$rURLS}) {
160 if ($rURLS->{$u}->{count} < 2) {
162 for my $f (sort keys %{$rURLS->{$u}}) {
163 next if ($f eq "count");
164 push(@submsg, "$f:" . $rURLS->{$u}->{$f});
166 push(@msg, "\n $u\n " . join("\n ", @submsg) . "\n");
170 print "\n$txt URLs not found in sources: " . join(' ',@msg) . "\n";
176 my ($file, $rUrls) = @_;
178 die("Could not read file $file") if (! open(ULIST, $file));
180 while (my $l = <ULIST>) {
182 $l =~ s/[\r\n]+$//; # remove eol
183 $l =~ s/\s*\#.*$//; # remove comment
185 if (! defined($rUrls->{$l} )) {
186 $rUrls->{$l} = {$file => $line, count => 1};
195 my $status = "out"; # outside of URL/href
197 return if ($f =~ /\/attic\//);
200 while(my $l = <FI>) {
202 $l =~ s/[\r\n]+$//; # Simulate chomp
203 if ($status eq "out") {
204 # searching for "\begin_inset Flex URL"
205 if($l =~ /^\s*\\begin_inset\s+Flex\s+URL\s*$/) {
206 $status = "inUrlInset";
208 elsif ($l =~ /^\s*\\begin_inset\s+CommandInset\s+href\s*$/) {
209 $status = "inHrefInset";
212 # Outside of url, check also
213 if ($l =~ /"((ftp|http|https):\/\/[^ ]+)"/) {
215 &handle_url($url, $f, "x$line");
220 if($l =~ /^\s*\\end_(layout|inset)\s*$/) {
223 elsif ($status eq "inUrlInset") {
224 if ($l =~ /\s*([a-z]+:\/\/.+)\s*$/) {
227 &handle_url($url, $f, "u$line");
230 elsif ($status eq "inHrefInset") {
231 if ($l =~ /^target\s+"([a-z]+:\/\/[^ ]+)"$/) {
234 &handle_url($url, $f, "h$line");
245 my($url, $f, $line) = @_;
247 if(!defined($URLS{$url})) {
249 $URLS{$url}->{$f} = [];
251 push(@{$URLS{$url}->{$f}}, $line);