blib/script/fix-link

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/script/copy-list ↑ Summary blib/script/link-report »
1 #!/usr/local/bin/perl -w
2  
3 =head1 NAME
4  
5 fix-link - identify files affected by a link change and fix them
6  
7 =head1 SYNOPSIS
8  
9 fixlink-as-text.pl [arguments] old-link new-link
10  
11 =head1 DESCRIPTION
12  
13 This program is designed to change one uri to another one throughout
14 an infostructure (group of interlinked files). Normally the program
15 uses an index built up for link testing to make this operation fast by
16 examining only those files which have links needing changed.
17  
18 B<This program is not designed to run SUID. Instead the user is meant
19 to have access to the files that are to be changed.>
20  
21  
22 =head1 FILE LOCATION MODES
23  
24 =head2 Find Files by directory
25  
26 Goes through all of the files in a directory. It changes checks all
27 files to see if they need to be changed.
28  
29 =head2 Find Files by index file
30  
31 Goes through the index file looking for all URIs that need to be
32 corrected then corrects those.
33  
34 =head1 SUBSTITUTION MODES
35  
36 =head2 Do simple textual substitutions
37  
38 Here we just substitute string for string. See "HOW IT WORKS" below.
39  
40 =head2 Do file parsing to find uris
41  
42 Not yet implemented. Here we go through each file parsing it
43 properly. This is the only way for binary formats.
44  
45 =head1 RELATIVE LINKS
46  
47 With the B<--relative> option we can search for relative links. This
48 is more dangerous (more chance of a false match) and slower, but will
49 be needed if you move files within your own web pages and need to
50 correct links to them.
51  
52 =head1 HOW IT WORKS
53  
54 This discussion covers how we do substitution in HTML documents. It's
55 a bit out of date.
56  
57 The proper way to do this is to parse each bit of the document, then
58 identify the links, convert them to the most cannonical form possible
59 and compare them to the original uri. If they match then replace them
60 with the new one (possibly in a relative form).
61  
62 This is great if:-
63 a) your document is not broken
64  
65 b) you don't have any links outside the link text (more common than
66 you would like to imagine, think of every `we have changed to a new
67 site' notice)
68  
69 So we resort to the following brute force approach:-
70  
71 a) if it looks like the original absolute link substitute it no
72 matter what.
73  
74 b) if it looks like the relative form and seems to be in an
75 attribute substitute it and warn what we are doing.
76  
77 c) if it looks like the relative form, but isn't in an attribute
78 then bitch about it, but do nothing.
79  
80 Your files should
81  
82 a) have the right level of quoting
83  
84 b) not have any uris containing [stuff]/fred/../[stuff]
85  
86 =head1 TODO
87  
88 Add extra formats.
89  
90  
91 =head1 SEE ALSO
92  
93 L<verify-link-control(1)>; L<extract-links(1)>; L<build-schedule>
94 L<link-report(1)>; L<fix-link(1)>; L<link-report.cgi(1)>; L<fix-link.cgi>
95 L<suggest(1)>; L<link-report.cgi(1)>; L<configure-link-control>
96  
97 The LinkController manual in the distribution in HTML, info, or
98 postscript formats, included in the distribution.
99  
100 http://scotclimb.org.uk/software/linkcont/ - the
101 LinkController homepage.
102  
103 =cut
104  
105 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
106  
107 8 ▶use CDB_File::BiIndex 0.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
108 0 ▶use DB_File;
Subroutine BEGIN: called CC=1 SCAR=2.6
109 use File::Find;
110 use File::Copy;
111 use Fcntl;
112 use MLDBM qw(DB_File);
113 use WWW::Link::Repair;
114 use WWW::Link::Repair::Substitutor;
115 use WWW::Link_Controller::InfoStruc;
116 use URI;
117  
118 use WWW::Link_Controller::ReadConf;
119  
120 #controls - not really configurations right now.
121  
122 #$::secret_suggestions=0; #whether the user wants to tell others about changes
123  
124 use vars qw();
125  
126 use Getopt::Function qw(maketrue makevalue);
127 $::verbose=0;
128 $::tree=0;
129 $::base=undef;
130 $::relative=undef;
131 $::opthandler = new Getopt::Function
132 [ "version V>version",
133 "usage h>usage help>usage",
134 "help-opt=s",
135 "verbose:i v>verbose",
136 "silent q>silent quiet>silent",
137 "no-warn",
138 "",
139 # "link-index=s",
140 "directory=s",
141 "",
142 "relative r>relative",
143 "tree t>tree",
144 "base=s b>base",
145 "",
146 "config-file=s",
147 ], {
148 # "link-index" => [ sub { $::link_index=$::value; },
149 # "Use the given file as the index of which file has "
150 # . "what link.",
151 # "FILENAME" ],
152 "directory" => [ \&makevalue,
153 "correct all files in the given directory.",
154 "DIRNAME" ],
155 # "mode" => [ sub { $::link_index=$::value; },
156 # "Use the given file as the index of which file has "
157 # . "what link.",
158 # "FILENAME" ],
159 "tree" => [ \&maketrue,
160 "Fix the link and any others based on it." ],
161 "no-warn" => [ sub { $::no_warn = 1; },
162 "Avoid issuing warnings about non-fatal problems." ],
163 "relative" => [ \&maketrue,
164 "Fix relative links (expensive??)." ],
165 "base" => [ \&makevalue,
166 "Base URI of the document or directory to be fixed.",
167 "FILENAME" ],
168 "config-file" => [ sub {
169 eval {require $::value};
170 die "Additional config failed: $@"
171 if $@; #if it's not there die anyway. compare Config.pm
172 },
173 "Load in an additional configuration file",
174 "FILENAME" ],
175 };
176  
177 $::opthandler->std_opts;
178  
179 $::opthandler->check_opts;
180  
181 sub usage() {
182 print <<'EOF';
183 fix-link [options] old-link new-link
184  
185 EOF
186 $::opthandler->list_opts;
187 print <<'EOF';
188  
189 Replace any occurences of OLD-LINK with NEW-LINK using link index file
190 to locate which files OLD-LINK occurs in.
191 EOF
192 }
193  
194 sub version() {
195 print <<'EOF';
196 fix-link version
197 $Id: fix-link.pl,v 1.22 2002/02/03 21:18:46 mikedlr Exp $
198 EOF
199 }
200  
201 $WWW::Link::Repair::no_warn=1 if $::no_warn;
202 $WWW::Link::Repair::verbose=0xFFF if $::verbose;
203 $WWW::Link::Repair::Substitutor::verbose=0xFFF if $::verbose;
204 $WWW::Link_Controller::InfoStruc::verbose=0xFFF if $::verbose;
205  
206 my $origuri=shift;
207 die "missing arguments, giving up\n" unless @ARGV;
208 my $newuri=shift;
209  
210 #FIXME: CHANGE EVERYTHING TO USE URI RATHER THAN URL
211 #make the uris absolute.
212  
213 if (defined $::base) {
214 my $origuri_obj = new URI $origuri;
215 my $newuri_obj = new URI $newuri;
216  
217 my $orig_abs = $origuri_obj->abs($::base);
218 my $new_abs = $newuri_obj->abs($::base);
219  
220 $origuri = $orig_abs->as_string();
221 $newuri = $new_abs->as_string();
222 }
223  
224 print STDERR "going to change $origuri to $newuri\n" if $::verbose;
225  
226 $::file_subs= WWW::Link::Repair::Substitutor::gen_file_substitutor
227 ($origuri, $newuri, ($::tree ? ( tree_mode => 1 ) : () ),
228 ($::relative ? ( relative => 1 ,
229 file_to_url => \&WWW::Link_Controller::InfoStruc::file_to_url)
230 : () ),
231 );
232  
233 $::default_infostrucs=1 unless defined $::default_infostrucs;
234  
235 die "you must define the \$::infostrucs configuration variable"
236 if $::default_infostrucs and not defined $::infostrucs ;
237  
238 WWW::Link_Controller::InfoStruc::default_infostrucs()
239 if $::default_infostrucs;
240  
241 unless ( $::directory ) {
242  
243 #we could create new links as we go along. We could delete old ones
244 #but I don't see why.. Best that I can see is to output a list of
245 #any new URLs we create..
246 #
247 # die 'you must define the $::links configuration variable'
248 # unless $::links;
249 # tie %::links, MLDBM, $::links, O_RDONLY, 0666, $DB_HASH
250 # or die $!;
251 # we go RDONLY but if we want to make suggestions that will have to change
252  
253 die 'you must define the $::link_index configuration variable'
254 unless $::link_index;
255 die 'you must define the $::page_index configuration variable'
256 unless $::page_index;
257 $::index = new CDB_File::BiIndex $::page_index, $::link_index;
258  
259 print STDERR "using index to find files\n" if $::verbose;
260 my $trans_sub=\&WWW::Link_Controller::InfoStruc::url_to_file;
261 my $fixed=WWW::Link::Repair::infostructure($origuri, $::index, $trans_sub,
262 $::file_subs, $::tree);
263 unless ( $::silent ) {
264 if ( $fixed or $::relative ) {
265 print "fix-link finished: made $fixed substitutions\n";
266 } else {
267 print "fix-link finished: made no substitutions.. try --relative?\n"
268 }
269 }
270 } else {
271 print STDERR "searching through directory $::directory\n" if $::verbose;
272 my $fixed=WWW::Link::Repair::directory($::file_subs, $::directory);
273 print "fix-link finished: made $fixed substitutions\n"
274 unless $::silent;
275 }
276  
277 exit;
« blib/script/copy-list ↑ Summary blib/script/link-report »