]> git.lyx.org Git - features.git/blob - development/checkurls/search_url.pl
ctests for broken URLs in URL insets of LyX docs
[features.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
43 $ENV{LANG} = "en";
44 $ENV{LANGUAGE} = "en";
45
46 my %URLS = ();
47 my %ignoredURLS = ();
48 my %revertedURLS = ();
49 my %extraURLS = ();
50 my %selectedURLS = ();
51
52 my $checkSelectedOnly = 0;
53 for my $arg (@ARGV) {
54   die("Bad argument \"$arg\"") if ($arg !~ /=/);
55   my ($type,$val) = split("=", $arg);
56   if ($type eq "filesToScan") {
57     #The file should be a list of files to search in
58     if (open(FLIST, $val)) {
59       while (my $l = <FLIST>) {
60         chomp($l);
61         &parse_file($l);
62       }
63       close(FLIST);
64     }
65   }
66   elsif ($type eq "ignoredURLS") {
67     &readUrls($val, \%ignoredURLS);
68   }
69   elsif ($type eq "revertedURLS") {
70     &readUrls($val, \%revertedURLS);
71   }
72   elsif ($type eq "extraURLS") {
73     &readUrls($val,  \%extraURLS);
74   }
75   elsif ($type eq "selectedURLS") {
76     $checkSelectedOnly = 1;
77     &readUrls($val,  \%selectedURLS);
78   }
79   else {
80     die("Invalid argument \"$arg\"");
81   }
82 }
83
84 my @urls = sort keys %URLS, keys %extraURLS;
85 my $errorcount = 0;
86
87 my $URLScount = 0;
88
89 for my $u (@urls) {
90   next if (defined($ignoredURLS{$u}));
91   next if ($checkSelectedOnly && ! defined(${selectedURLS}{$u}));
92   $URLScount++;
93   print "Checking '$u'";
94   my $res = &check_url($u);
95   if ($res) {
96     print ": Failed\n";
97   }
98   else {
99     print ": OK\n";
100   }
101   my $printSourceFiles = 0;
102   my $err_txt = "Error url:";
103
104   if ($res || $checkSelectedOnly) {
105     $printSourceFiles = 1;
106   }
107   if ($res && defined($revertedURLS{$u})) {
108     $err_txt = "Failed url:";
109   }
110   $res = ! $res if (defined($revertedURLS{$u}));
111   if ($res || $checkSelectedOnly) {
112     print "$err_txt \"$u\"\n";
113   }
114   if ($printSourceFiles) {
115     if (defined($URLS{$u})) {
116       for my $f(sort keys %{$URLS{$u}}) {
117         print "  $f\n";
118       }
119     }
120     if ($res ) {
121       $errorcount++;
122     }
123   }
124 }
125
126 print "\n$errorcount URL-tests failed out of $URLScount\n\n";
127 exit($errorcount);
128
129 ###############################################################################
130
131 sub readUrls($$)
132 {
133   my ($file, $rUrls) = @_;
134
135   die("Could not read file $file") if (! open(ULIST, $file));
136   while (my $l = <ULIST>) {
137     $l =~ s/[\r\n]+$//;         # remove eol
138     $l =~ s/\s*\#.*$//;         # remove comment
139     next if ($l eq "");
140     $rUrls->{$l} = 1;
141   }
142   close(ULIST);
143 }
144
145 sub parse_file($)
146 {
147   my($f) = @_;
148   my $status = "out";           # outside of URL
149
150   return if ($f =~ /\/attic\//);
151   if(open(FI, $f)) {
152     while(my $l = <FI>) {
153       $l =~ s/[\r\n]+$//;       #  Simulate chomp
154       if($status eq "out") {
155         # searching for "\begin_inset Flex URL"
156         if($l =~ /^\s*\\begin_inset\s+Flex\s+URL\s*$/) {
157           $status = "ininset";
158         }
159       }
160       else {
161         if($l =~ /^\s*\\end_(layout|inset)\s*$/) {
162           $status = "out";
163         }
164         else {
165           if($l =~ /\s*([a-z]+:\/\/.+)\s*$/) {
166             my $url = $1;
167             $status = "out";
168             &handle_url($url, $f);
169           }
170         }
171       }
172     }
173     close(FI);
174   }
175 }
176
177 sub handle_url($$)
178 {
179   my($url, $f) = @_;
180
181   if(!defined($URLS{$url})) {
182     $URLS{$url} = {};
183   }
184   $URLS{$url}->{$f} = 1;
185 }