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");
50 sub printNotUsedURLS($\%);
58 my %revertedURLS = ();
60 my %selectedURLS = ();
61 my $summaryFile = undef;
63 my $checkSelectedOnly = 0;
65 die("Bad argument \"$arg\"") if ($arg !~ /=/);
66 my ($type,$val) = split("=", $arg);
67 if ($type eq "filesToScan") {
68 #The file should be a list of files to search in
69 if (open(FLIST, $val)) {
70 while (my $l = <FLIST>) {
77 elsif ($type eq "ignoredURLS") {
78 readUrls($val, %ignoredURLS);
80 elsif ($type eq "revertedURLS") {
81 readUrls($val, %revertedURLS);
83 elsif ($type eq "extraURLS") {
84 readUrls($val, %extraURLS);
86 elsif ($type eq "selectedURLS") {
87 $checkSelectedOnly = 1;
88 readUrls($val, %selectedURLS);
90 elsif ($type eq "summaryFile") {
91 if (open(SFO, '>', "$val")) {
96 die("Invalid argument \"$arg\"");
100 my @urls = sort keys %URLS, keys %extraURLS;
106 if (defined($selectedURLS{$u})) {
107 ${selectedURLS}{$u}->{count} += 1;
109 if (defined($ignoredURLS{$u})) {
110 $ignoredURLS{$u}->{count} += 1;
113 next if ($checkSelectedOnly && ! defined($selectedURLS{$u}));
115 print "Checking '$u': ";
116 my ($res, $prnt, $outSum);
118 $res = check_url($u);
130 $prnt = "Failed, caught error: $_\n";
134 printx("$prnt", $outSum);
135 my $printSourceFiles = 0;
136 my $err_txt = "Error url:";
138 if ($res || $checkSelectedOnly) {
139 $printSourceFiles = 1;
141 if ($res && defined($revertedURLS{$u})) {
142 $err_txt = "Failed url:";
144 $res = ! $res if (defined($revertedURLS{$u}));
145 if ($res || $checkSelectedOnly) {
146 printx("$err_txt \"$u\"\n", $outSum);
148 if ($printSourceFiles) {
149 if (defined($URLS{$u})) {
150 for my $f(sort keys %{$URLS{$u}}) {
151 my $lines = ":" . join(',', @{$URLS{$u}->{$f}});
152 printx(" $f$lines\n", $outSum);
162 printNotUsedURLS("Ignored", %ignoredURLS);
163 printNotUsedURLS("Selected", %selectedURLS);
164 printNotUsedURLS("KnownInvalid", %extraURLS);
167 print "\n$errorcount URL-tests failed out of $URLScount\n\n";
168 if (defined($summaryFile)) {
173 ###############################################################################
176 my ($txt, $outSum) = @_;
178 if ($outSum && defined($summaryFile)) {
183 sub printNotUsedURLS($\%)
185 my ($txt, $rURLS) = @_;
187 for my $u ( sort keys %{$rURLS}) {
188 if ($rURLS->{$u}->{count} < 2) {
190 for my $f (sort keys %{$rURLS->{$u}}) {
191 next if ($f eq "count");
192 push(@submsg, "$f:" . $rURLS->{$u}->{$f});
194 push(@msg, "\n $u\n " . join("\n ", @submsg) . "\n");
198 print "\n$txt URLs not found in sources: " . join(' ',@msg) . "\n";
204 my ($file, $rUrls) = @_;
206 die("Could not read file $file") if (! open(ULIST, $file));
208 while (my $l = <ULIST>) {
210 $l =~ s/[\r\n]+$//; # remove eol
211 $l =~ s/\s*\#.*$//; # remove comment
213 if (! defined($rUrls->{$l} )) {
214 $rUrls->{$l} = {$file => $line, count => 1};
223 my $status = "out"; # outside of URL/href
225 return if ($f =~ /\/attic\//);
228 while(my $l = <FI>) {
230 $l =~ s/[\r\n]+$//; # Simulate chomp
231 if ($status eq "out") {
232 # searching for "\begin_inset Flex URL"
233 if($l =~ /^\s*\\begin_inset\s+Flex\s+URL\s*$/) {
234 $status = "inUrlInset";
236 elsif ($l =~ /^\s*\\begin_inset\s+CommandInset\s+href\s*$/) {
237 $status = "inHrefInset";
240 # Outside of url, check also
241 if ($l =~ /"((ftp|http|https):\/\/[^ ]+)"/) {
243 handle_url($url, $f, "x$line");
248 if($l =~ /^\s*\\end_(layout|inset)\s*$/) {
251 elsif ($status eq "inUrlInset") {
252 if ($l =~ /\s*([a-z]+:\/\/.+)\s*$/) {
255 handle_url($url, $f, "u$line");
258 elsif ($status eq "inHrefInset") {
259 if ($l =~ /^target\s+"([a-z]+:\/\/[^ ]+)"$/) {
262 handle_url($url, $f, "h$line");
273 my($url, $f, $line) = @_;
275 if(!defined($URLS{$url})) {
278 if(!defined($URLS{$url}->{$f})) {
279 $URLS{$url}->{$f} = [];
281 push(@{$URLS{$url}->{$f}}, $line);