]> git.lyx.org Git - lyx.git/blob - development/checkurls/search_url.pl
Cmake url tests: Use try - catch mechanism while testing urls.
[lyx.git] / development / checkurls / search_url.pl
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3 #
4 # file search_url.pl
5 # script to search for url's in lyxfiles
6 # and testing their validity.
7 #
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}
14 #
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.
19 #
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.
24 #
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
28 #
29 # Copyright (c) 2013 Kornel Benko <kornel@lyx.org>
30 #           (c) 2013 Scott Kostyshak <skotysh@lyx.org>
31
32 use strict;
33
34 BEGIN  {
35   use File::Spec;
36   my $p = File::Spec->rel2abs(__FILE__);
37   $p =~ s/[\/\\]?[^\/\\]+$//;
38   unshift(@INC, "$p");
39 }
40
41 use CheckURL;
42 use Try::Tiny;
43 use locale;
44 use POSIX qw(locale_h);
45
46 setlocale(LC_CTYPE, "");
47 setlocale(LC_MESSAGES, "en_US.UTF-8");
48
49 my %URLS = ();
50 my %ignoredURLS = ();
51 my %revertedURLS = ();
52 my %extraURLS = ();
53 my %selectedURLS = ();
54
55 my $checkSelectedOnly = 0;
56 for my $arg (@ARGV) {
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>) {
63         chomp($l);
64         &parse_file($l);
65       }
66       close(FLIST);
67     }
68   }
69   elsif ($type eq "ignoredURLS") {
70     &readUrls($val, \%ignoredURLS);
71   }
72   elsif ($type eq "revertedURLS") {
73     &readUrls($val, \%revertedURLS);
74   }
75   elsif ($type eq "extraURLS") {
76     &readUrls($val,  \%extraURLS);
77   }
78   elsif ($type eq "selectedURLS") {
79     $checkSelectedOnly = 1;
80     &readUrls($val,  \%selectedURLS);
81   }
82   else {
83     die("Invalid argument \"$arg\"");
84   }
85 }
86
87 my @urls = sort keys %URLS, keys %extraURLS;
88 my $errorcount = 0;
89
90 my $URLScount = 0;
91
92 for my $u (@urls) {
93   if (defined($selectedURLS{$u})) {
94     ${selectedURLS}{$u}->{count} += 1;
95   }
96   if (defined($ignoredURLS{$u})) {
97     $ignoredURLS{$u}->{count} += 1;
98     next;
99   }
100   next if ($checkSelectedOnly && ! defined($selectedURLS{$u}));
101   $URLScount++;
102   print "Checking '$u': ";
103   my ($res, $prnt);
104   try {
105     $res = &check_url($u);
106     if ($res) {
107      $prnt = "Failed";
108     }
109     else {
110       $prnt = "OK";
111     }
112   }
113   catch {
114     $prnt = "Failed, caught error: $_";
115     $res = 700;
116   };
117   print "$prnt\n";
118   my $printSourceFiles = 0;
119   my $err_txt = "Error url:";
120
121   if ($res || $checkSelectedOnly) {
122     $printSourceFiles = 1;
123   }
124   if ($res && defined($revertedURLS{$u})) {
125     $err_txt = "Failed url:";
126   }
127   $res = ! $res if (defined($revertedURLS{$u}));
128   if ($res || $checkSelectedOnly) {
129     print "$err_txt \"$u\"\n";
130   }
131   if ($printSourceFiles) {
132     if (defined($URLS{$u})) {
133       for my $f(sort keys %{$URLS{$u}}) {
134         my $lines = ":" . join(',', @{$URLS{$u}->{$f}});
135         print "  $f$lines\n";
136       }
137     }
138     if ($res ) {
139       $errorcount++;
140     }
141   }
142 }
143
144 if (%URLS) {
145   &printNotUsedURLS("Ignored", \%ignoredURLS);
146   &printNotUsedURLS("Selected", \%selectedURLS);
147   &printNotUsedURLS("KnownInvalid", \%extraURLS);
148 }
149
150 print "\n$errorcount URL-tests failed out of $URLScount\n\n";
151 exit($errorcount);
152
153 ###############################################################################
154
155 sub printNotUsedURLS($$)
156 {
157   my ($txt, $rURLS) = @_;
158   my @msg = ();
159   for my $u ( sort keys %{$rURLS}) {
160     if ($rURLS->{$u}->{count} < 2) {
161       my @submsg = ();
162       for my $f (sort keys %{$rURLS->{$u}}) {
163         next if ($f eq "count");
164         push(@submsg, "$f:" . $rURLS->{$u}->{$f});
165       }
166       push(@msg, "\n  $u\n    " . join("\n    ", @submsg) . "\n");
167     }
168   }
169   if (@msg) {
170     print "\n$txt URLs not found in sources: " . join(' ',@msg) . "\n";
171   }
172 }
173
174 sub readUrls($$)
175 {
176   my ($file, $rUrls) = @_;
177
178   die("Could not read file $file") if (! open(ULIST, $file));
179   my $line = 0;
180   while (my $l = <ULIST>) {
181     $line++;
182     $l =~ s/[\r\n]+$//;         # remove eol
183     $l =~ s/\s*\#.*$//;         # remove comment
184     next if ($l eq "");
185     if (! defined($rUrls->{$l} )) {
186       $rUrls->{$l} = {$file => $line, count => 1};
187     }
188   }
189   close(ULIST);
190 }
191
192 sub parse_file($)
193 {
194   my($f) = @_;
195   my $status = "out";           # outside of URL/href
196
197   return if ($f =~ /\/attic\//);
198   if(open(FI, $f)) {
199     my $line = 0;
200     while(my $l = <FI>) {
201       $line++;
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";
207         }
208         elsif ($l =~ /^\s*\\begin_inset\s+CommandInset\s+href\s*$/) {
209           $status = "inHrefInset";
210         }
211         else {
212           # Outside of url, check also
213           if ($l =~ /"((ftp|http|https):\/\/[^ ]+)"/) {
214             my $url = $1;
215             &handle_url($url, $f, "x$line");
216           }
217         }
218       }
219       else {
220         if($l =~ /^\s*\\end_(layout|inset)\s*$/) {
221           $status = "out";
222         }
223         elsif ($status eq "inUrlInset") {
224           if ($l =~ /\s*([a-z]+:\/\/.+)\s*$/) {
225             my $url = $1;
226             $status = "out";
227             &handle_url($url, $f, "u$line");
228           }
229         }
230         elsif ($status eq "inHrefInset") {
231           if ($l =~ /^target\s+"([a-z]+:\/\/[^ ]+)"$/) {
232             my $url = $1;
233             $status = "out";
234             &handle_url($url, $f, "h$line");
235           }
236         }
237       }
238     }
239     close(FI);
240   }
241 }
242
243 sub handle_url($$$)
244 {
245   my($url, $f, $line) = @_;
246
247   if(!defined($URLS{$url})) {
248     $URLS{$url} = {};
249     $URLS{$url}->{$f} = [];
250   }
251   push(@{$URLS{$url}->{$f}}, $line);
252 }