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/[\/\\]?[^\/\\]+$//;
43 $ENV{LC_ALL} = "en_US.UTF-8";
44 $ENV{LANG} = "en_US.UTF-8";
45 $ENV{LANGUAGE} = "en_US.UTF-8";
49 my %revertedURLS = ();
51 my %selectedURLS = ();
53 my $checkSelectedOnly = 0;
55 die("Bad argument \"$arg\"") if ($arg !~ /=/);
56 my ($type,$val) = split("=", $arg);
57 if ($type eq "filesToScan") {
58 #The file should be a list of files to search in
59 if (open(FLIST, $val)) {
60 while (my $l = <FLIST>) {
67 elsif ($type eq "ignoredURLS") {
68 &readUrls($val, \%ignoredURLS);
70 elsif ($type eq "revertedURLS") {
71 &readUrls($val, \%revertedURLS);
73 elsif ($type eq "extraURLS") {
74 &readUrls($val, \%extraURLS);
76 elsif ($type eq "selectedURLS") {
77 $checkSelectedOnly = 1;
78 &readUrls($val, \%selectedURLS);
81 die("Invalid argument \"$arg\"");
85 my @urls = sort keys %URLS, keys %extraURLS;
91 if (defined($selectedURLS{$u})) {
92 ${selectedURLS}{$u}->{count} += 1;
94 if (defined($ignoredURLS{$u})) {
95 $ignoredURLS{$u}->{count} += 1;
98 next if ($checkSelectedOnly && ! defined($selectedURLS{$u}));
100 print "Checking '$u'";
101 my $res = &check_url($u);
108 my $printSourceFiles = 0;
109 my $err_txt = "Error url:";
111 if ($res || $checkSelectedOnly) {
112 $printSourceFiles = 1;
114 if ($res && defined($revertedURLS{$u})) {
115 $err_txt = "Failed url:";
117 $res = ! $res if (defined($revertedURLS{$u}));
118 if ($res || $checkSelectedOnly) {
119 print "$err_txt \"$u\"\n";
121 if ($printSourceFiles) {
122 if (defined($URLS{$u})) {
123 for my $f(sort keys %{$URLS{$u}}) {
124 my $lines = ":" . join(',', @{$URLS{$u}->{$f}});
135 &printNotUsedURLS("Ignored", \%ignoredURLS);
136 &printNotUsedURLS("Selected", \%selectedURLS);
137 &printNotUsedURLS("KnownInvalid", \%extraURLS);
140 print "\n$errorcount URL-tests failed out of $URLScount\n\n";
143 ###############################################################################
145 sub printNotUsedURLS($$)
147 my ($txt, $rURLS) = @_;
149 for my $u ( sort keys %{$rURLS}) {
150 if ($rURLS->{$u}->{count} < 2) {
152 for my $f (sort keys %{$rURLS->{$u}}) {
153 next if ($f eq "count");
154 push(@submsg, "$f:" . $rURLS->{$u}->{$f});
156 push(@msg, "\n $u\n " . join("\n ", @submsg) . "\n");
160 print "\n$txt URLs not found in sources: " . join(' ',@msg) . "\n";
166 my ($file, $rUrls) = @_;
168 die("Could not read file $file") if (! open(ULIST, $file));
170 while (my $l = <ULIST>) {
172 $l =~ s/[\r\n]+$//; # remove eol
173 $l =~ s/\s*\#.*$//; # remove comment
175 if (! defined($rUrls->{$l} )) {
176 $rUrls->{$l} = {$file => $line, count => 1};
185 my $status = "out"; # outside of URL/href
187 return if ($f =~ /\/attic\//);
190 while(my $l = <FI>) {
192 $l =~ s/[\r\n]+$//; # Simulate chomp
193 if ($status eq "out") {
194 # searching for "\begin_inset Flex URL"
195 if($l =~ /^\s*\\begin_inset\s+Flex\s+URL\s*$/) {
196 $status = "inUrlInset";
198 elsif ($l =~ /^\s*\\begin_inset\s+CommandInset\s+href\s*$/) {
199 $status = "inHrefInset";
202 # Outside of url, check also
203 if ($l =~ /"((ftp|http|https):\/\/[^ ]+)"/) {
205 &handle_url($url, $f, "x$line");
210 if($l =~ /^\s*\\end_(layout|inset)\s*$/) {
213 elsif ($status eq "inUrlInset") {
214 if ($l =~ /\s*([a-z]+:\/\/.+)\s*$/) {
217 &handle_url($url, $f, "u$line");
220 elsif ($status eq "inHrefInset") {
221 if ($l =~ /^target\s+"([a-z]+:\/\/[^ ]+)"$/) {
224 &handle_url($url, $f, "h$line");
235 my($url, $f, $line) = @_;
237 if(!defined($URLS{$url})) {
239 $URLS{$url}->{$f} = [];
241 push(@{$URLS{$url}->{$f}}, $line);