| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | link-report - report status of links using databases | ||
| 6 | |||
| 7 | =head1 DESCRIPTION | ||
| 8 | |||
| 9 | This program goes through an entire database of links and reports | ||
| 10 | information about those links which match given criteria. It can | ||
| 11 | optionally relate these to the locations of these links in files. It | ||
| 12 | can give the output in several formats. | ||
| 13 | |||
| 14 | No actions are carried out to get further information than that which | ||
| 15 | is in the database. This means that if a URI isn't listed in the link | ||
| 16 | database, a warning is printed, but nothing else. | ||
| 17 | |||
| 18 | Distributed along with this program is a file for emacs which can use | ||
| 19 | this to build a list of files for editing called link-report-dired. | ||
| 20 | This can be very useful for fixing all broken links. | ||
| 21 | |||
| 22 | =head1 SELECTING LINKS TO REPORT | ||
| 23 | |||
| 24 | This bit is where we really miss in the design by not doing it against | ||
| 25 | an SQL database. When (and if) that comes then we can add alot more | ||
| 26 | power here more simply. | ||
| 27 | |||
| 28 | There are three modes for selection | ||
| 29 | |||
| 30 | =over 4 | ||
| 31 | |||
| 32 | =item * | ||
| 33 | |||
| 34 | by URI | ||
| 35 | |||
| 36 | =item * | ||
| 37 | |||
| 38 | by regex on the URI --uri-include --uri-exclude | ||
| 39 | |||
| 40 | =item * | ||
| 41 | |||
| 42 | by regex on the page name --page-include --page-exclude | ||
| 43 | |||
| 44 | =back | ||
| 45 | |||
| 46 | These select which links are to be examined. Then there are options | ||
| 47 | on the state of the link which choose which links to actually put out | ||
| 48 | information about. | ||
| 49 | |||
| 50 | By default redirected and broken links are output. If any of these | ||
| 51 | options are called then only the specified links are output. | ||
| 52 | |||
| 53 | =over 4 | ||
| 54 | |||
| 55 | =item --all-links | ||
| 56 | |||
| 57 | print out links in all states | ||
| 58 | |||
| 59 | =item --not-perfect | ||
| 60 | |||
| 61 | print out all links which were broken even on one check. | ||
| 62 | |||
| 63 | =item --redirected | ||
| 64 | |||
| 65 | print out links which were redirected | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | =head1 CONTROLLING THE REPORT FORMAT | ||
| 70 | |||
| 71 | The list of broken links can be put out in several formats. By | ||
| 72 | defualt it is in plain text with a description of the problem and a | ||
| 73 | list of broken pages following under each link. Alternatively other | ||
| 74 | formats are possible. These are selected through command line | ||
| 75 | options as follows. | ||
| 76 | |||
| 77 | =over 4 | ||
| 78 | |||
| 79 | =item --long-list | ||
| 80 | |||
| 81 | Instead of listing the urls of the pages where broken links are, this | ||
| 82 | converts each link to a file name and does a listing of the file. | ||
| 83 | |||
| 84 | =item --html | ||
| 85 | |||
| 86 | This outputs a basic HTML format report which is otherwise similar to | ||
| 87 | the default text report. | ||
| 88 | |||
| 89 | =item --uri-report | ||
| 90 | |||
| 91 | This report simply lists URIs of the selected links from | ||
| 92 | LinkControllers database. This provides a better format for | ||
| 93 | interfacing to other programs. | ||
| 94 | |||
| 95 | =back | ||
| 96 | |||
| 97 | =head1 TODO | ||
| 98 | |||
| 99 | Much detailed control of the kind of links reported should be provided | ||
| 100 | (e.g. how many times detected broken etc.). This should be done as a | ||
| 101 | more advanced database system is integrated. | ||
| 102 | |||
| 103 | If there's any specific, reasonably simple, reporting option you would | ||
| 104 | like added then please send a request to the bug reporting address | ||
| 105 | explaining what this report would be used for. | ||
| 106 | |||
| 107 | =head1 BUGS | ||
| 108 | |||
| 109 | If reporting options which don't make sense together are given then | ||
| 110 | the reporting probably won't make much sense, though it should still | ||
| 111 | work. | ||
| 112 | |||
| 113 | |||
| 114 | =head1 SEE ALSO | ||
| 115 | |||
| 116 | L<verify-link-control(1)>; L<extract-links(1)>; L<build-schedule> | ||
| 117 | L<link-report(1)>; L<fix-link(1)>; L<link-report.cgi(1)>; L<fix-link.cgi> | ||
| 118 | L<suggest(1)>; L<link-report.cgi(1)>; L<configure-link-control> | ||
| 119 | |||
| 120 | The LinkController manual in the distribution in HTML, info, or | ||
| 121 | postscript formats, included in the distribution. This includes | ||
| 122 | information about using link-report to `interface' to databases. | ||
| 123 | |||
| 124 | http://scotclimb.org.uk/software/linkcont/ - the LinkController | ||
| 125 | homepage. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | |||
| 130 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 131 | 2 | ▶ | use Fcntl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 132 | 0 | ▶ | use DB_File; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 133 | use MLDBM qw(DB_File); | ||
| 134 | use CDB_File::BiIndex 0.026; | ||
| 135 | use WWW::Link; | ||
| 136 | use WWW::Link::Selector; | ||
| 137 | use WWW::Link_Controller::InfoStruc; | ||
| 138 | use vars qw($linkdbm); | ||
| 139 | |||
| 140 | |||
| 141 | $::verbose=1023; | ||
| 142 | |||
| 143 | #Configuration - we go through %ENV, so you'd better not be running SUID | ||
| 144 | #eval to ignore if a file doesn't exist.. e.g. the system config | ||
| 145 | use WWW::Link_Controller::ReadConf; | ||
| 146 | |||
| 147 | ############################################################################## | ||
| 148 | #start command line processing | ||
| 149 | use Getopt::Function qw(maketrue makevalue); | ||
| 150 | |||
| 151 | use vars qw($not_perfect $redirected $since); | ||
| 152 | |||
| 153 | $::ignore_missing=0; | ||
| 154 | $::pages=1; | ||
| 155 | $::verbose=0; | ||
| 156 | $::not_perfect=0; | ||
| 157 | $::okay=0; | ||
| 158 | $::good=0; | ||
| 159 | $::since=0; | ||
| 160 | $::html=0; | ||
| 161 | $::long_list=0; | ||
| 162 | @::uri_exc=(); | ||
| 163 | @::uri_inc=(); | ||
| 164 | @::page_exc=(); | ||
| 165 | @::page_inc=(); | ||
| 166 | @::uris=(); | ||
| 167 | $::uri_list=0; | ||
| 168 | $::uri_report=0; | ||
| 169 | |||
| 170 | $::opthandler = new Getopt::Function | ||
| 171 | [ "version V>version", | ||
| 172 | "usage h>usage help>usage", | ||
| 173 | "help-opt=s", | ||
| 174 | "verbose:i v>verbose", | ||
| 175 | "", | ||
| 176 | "uri=s U>uri", | ||
| 177 | "uri-file=s f>uri", | ||
| 178 | "uri-exclude=s E>uri-exclude", | ||
| 179 | "uri-include=s I>uri-include", | ||
| 180 | "page-exclude=s e>page-exclude", | ||
| 181 | "page-include=s i>page-include", | ||
| 182 | "", | ||
| 183 | "all-links a>all-links", | ||
| 184 | "broken b>broken", | ||
| 185 | "not-perfect n>not-perfect", | ||
| 186 | "redirected r>redirected", | ||
| 187 | "okay o>okay", | ||
| 188 | "disallowed d>disallowed", | ||
| 189 | "unsupported u>unsupported", | ||
| 190 | "ignore-missing m>ignore-missing", | ||
| 191 | "good g>good", | ||
| 192 | "", | ||
| 193 | "no-pages N>no-pages", | ||
| 194 | "config-file=s", | ||
| 195 | "link-index=s", | ||
| 196 | "link-database=s", | ||
| 197 | # "uri-base=s", | ||
| 198 | # "file-base=s", | ||
| 199 | "", | ||
| 200 | "long-list l>long-list", | ||
| 201 | "uri-report R>uri-report", | ||
| 202 | "html H>html", | ||
| 203 | ], | ||
| 204 | { | ||
| 205 | "uri-exclude" => [ sub { push @::uri_exc, $::value; }, | ||
| 206 | "Add a regular expressions for URIs to ignore.", | ||
| 207 | "EXCLUDE RE" ], | ||
| 208 | "uri-include" => [ sub { push @::uri_inc, $::value; }, | ||
| 209 | "Give regular expression for URIs to check (if this " | ||
| 210 | . "option is given others aren't checked).", | ||
| 211 | "INCLUDE RE" ], | ||
| 212 | "page-exclude" => [ sub { push @::page_exc, $::value; }, | ||
| 213 | "Add a regular expressions for pages to ignore.", | ||
| 214 | "EXCLUDE RE" ], | ||
| 215 | "page-include" => [ sub { push @::page_inc, $::value; }, | ||
| 216 | "Give regular expression for URIs to check (if this " | ||
| 217 | . "option is given others aren't checked).", | ||
| 218 | "INCLUDE RE" ], | ||
| 219 | # "since" => [ \&makevalue, #FIXME process time strings | ||
| 220 | # "Only list links who's status has changed since the " | ||
| 221 | # . "given time.", | ||
| 222 | # "TIME" ], | ||
| 223 | |||
| 224 | "all-links" => [ \&maketrue, | ||
| 225 | "Report information about every URI." ], | ||
| 226 | "broken" => [ \&maketrue, | ||
| 227 | "Report links which are considered broken." ], | ||
| 228 | "not-perfect" => [ \&maketrue, | ||
| 229 | "Report any URI which wasn't okay at last test." ], | ||
| 230 | "okay" => [ \&maketrue, | ||
| 231 | "Report links which have been tested okay." ], | ||
| 232 | "good" => [ \&maketrue, | ||
| 233 | "Report links which are probably worth listing." ], | ||
| 234 | "redirected" => [ \&maketrue, | ||
| 235 | "Report links which are redirected." ], | ||
| 236 | "disallowed" => [ \&maketrue, | ||
| 237 | "Report links for which testing isn't allowed." ], | ||
| 238 | "unsupported" => [ \&maketrue, | ||
| 239 | "Report links which we don't know how to test." ], | ||
| 240 | "ignore-missing" => [ \&maketrue, | ||
| 241 | "Don't complain about links which aren't in " | ||
| 242 | . "the database." ], | ||
| 243 | "uri-file" => [ sub { print "reading urifile $::value" if $::verbose; | ||
| 244 | open URIS, "<$::value"; my @uris=<URIS>; close URIS; | ||
| 245 | foreach (@uris) { | ||
| 246 | s/\s*$//; s/\s*//; | ||
| 247 | } | ||
| 248 | push @::uris, @uris; | ||
| 249 | $::uri_list=1; | ||
| 250 | }, | ||
| 251 | "Read all URIs in a file (one URI per line).", | ||
| 252 | "FILENAME"], | ||
| 253 | "uri" => [ sub { push @::uris, split /\s+/, $::value; $::uri_list=1; }, | ||
| 254 | "Give URIs which are to be reported on.", | ||
| 255 | "URIs"], | ||
| 256 | "no-pages" => [ \&maketrue, | ||
| 257 | "Report without page list.", | ||
| 258 | ], | ||
| 259 | "config-file" => [ sub { | ||
| 260 | eval {require $::value}; | ||
| 261 | die "Additional config failed: $@" | ||
| 262 | if $@; #if it's not there die anyway. compare Config.pm | ||
| 263 | }, | ||
| 264 | "Load in an additional configuration file", | ||
| 265 | "FILENAME" ], | ||
| 266 | "link-index" => [ sub { $::link_index=$::value; }, | ||
| 267 | "Use the given file as the index of which file has " | ||
| 268 | . "what link.", | ||
| 269 | "FILENAME" ], | ||
| 270 | "link-database" => [ sub { $::links=$::value; }, | ||
| 271 | "Use the given file as the dbm containing links.", | ||
| 272 | "FILENAME" ], | ||
| 273 | |||
| 274 | # "uri-base" => [ \&makevalue, #FIXME process time strings | ||
| 275 | # "Regex for base of the infostructure (first part of s///).", | ||
| 276 | # "REGEX" ], | ||
| 277 | # "file-base" => [ \&makevalue, #FIXME process time strings | ||
| 278 | # "The base of files to be used (second part of s///).", | ||
| 279 | # "STRING" ], | ||
| 280 | "uri-report" => [ \&maketrue, | ||
| 281 | "Print URIs on separate lines for each link." ], | ||
| 282 | "html" => [ \&maketrue, | ||
| 283 | "Report status of links in html format." ], | ||
| 284 | "long-list" => [ sub { $::long_list=1; $::pages=1; }, | ||
| 285 | "Where possible, identify the file and long list it " | ||
| 286 | . "(implies infostructure). This is used for emacs " | ||
| 287 | . "link-report-dired." ], | ||
| 288 | }; | ||
| 289 | $::opthandler->std_opts; | ||
| 290 | |||
| 291 | $::opthandler->check_opts; | ||
| 292 | |||
| 293 | sub usage() { | ||
| 294 | print <<EOF; | ||
| 295 | link-report [options] | ||
| 296 | |||
| 297 | EOF | ||
| 298 | $::opthandler->list_opts; | ||
| 299 | print <<EOF; | ||
| 300 | |||
| 301 | Report on the status of links, getting the links either from the | ||
| 302 | database, from the index file or from the command line. | ||
| 303 | EOF | ||
| 304 | } | ||
| 305 | |||
| 306 | sub version() { | ||
| 307 | print <<'EOF'; | ||
| 308 | link-report version | ||
| 309 | $Id: link-report.pl,v 1.26 2002/02/09 16:31:19 mikedlr Exp $ | ||
| 310 | EOF | ||
| 311 | } | ||
| 312 | |||
| 313 | $WWW::Link::Reporter::verbose=0; | ||
| 314 | if ($::verbose) { | ||
| 315 | $WWW::Link::Reporter::verbose=0xFF; | ||
| 316 | } | ||
| 317 | |||
| 318 | $WWW::Link_Controller::InfoStruc::verbose=0xFFF if $::verbose; | ||
| 319 | |||
| 320 | $::default_infostrucs=1 unless defined $::default_infostrucs; | ||
| 321 | |||
| 322 | die "you must define the \$::infostrucs configuration variable" | ||
| 323 | if $::default_infostrucs and not defined $::infostrucs ; | ||
| 324 | |||
| 325 | WWW::Link_Controller::InfoStruc::default_infostrucs() | ||
| 326 | if $::default_infostrucs; | ||
| 327 | |||
| 328 | |||
| 329 | ############################################################################## | ||
| 330 | #consistency checks | ||
| 331 | |||
| 332 | if (@::uris) { | ||
| 333 | die "If you specify URIs then we can't use exclude or include regexes" | ||
| 334 | if (@::uri_exc || @::uri_inc | ||
| 335 | || @::page_exc || @::page_exc); | ||
| 336 | } | ||
| 337 | |||
| 338 | die "Page and URI exclude are incompatible. Maybe you can help?" | ||
| 339 | if (@::uri_exc || @::uri_inc | ||
| 340 | and @::page_exc || @::page_exc); | ||
| 341 | |||
| 342 | ############################################################################## | ||
| 343 | #end command line processing | ||
| 344 | |||
| 345 | die 'you must define the $::links configuration variable' | ||
| 346 | unless $::links; | ||
| 347 | $::linkdbm = tie %::links, "MLDBM", $::links, O_RDONLY, 0666, $::DB_HASH | ||
| 348 | or die $!; | ||
| 349 | |||
| 350 | (@::uri_exc || @::uri_inc) and | ||
| 351 | $::include=WWW::Link::Selector::gen_include_exclude @::uri_exc, @::uri_inc; | ||
| 352 | |||
| 353 | (@::page_exc || @::page_inc) and | ||
| 354 | $::include=WWW::Link::Selector::gen_include_exclude @::page_exc, @::page_inc; | ||
| 355 | |||
| 356 | $::include=sub {return 1} unless $::include; | ||
| 357 | |||
| 358 | |||
| 359 | ($::pages) && do { | ||
| 360 | die 'you must define the $::link_index configuration variable' | ||
| 361 | unless $::link_index; | ||
| 362 | die 'you must define the $::page_index configuration variable' | ||
| 363 | unless $::page_index; | ||
| 364 | $::index = undef; | ||
| 365 | $::index = new CDB_File::BiIndex ($::page_index, $::link_index); | ||
| 366 | }; | ||
| 367 | |||
| 368 | my $form_count=0; | ||
| 369 | foreach my $opt ( \$::uri_report, \$::html, \$::long_list) { | ||
| 370 | $$opt && $form_count++; | ||
| 371 | } | ||
| 372 | $form_count > 1 && do { | ||
| 373 | die "only one format argument allowed (--uri-report --html --long-list)\n"; | ||
| 374 | }; | ||
| 375 | |||
| 376 | CASE: { | ||
| 377 | $::uri_report && do { | ||
| 378 | require WWW::Link::Reporter::URI; | ||
| 379 | $::reporter=new WWW::Link::Reporter::URI; | ||
| 380 | last CASE; | ||
| 381 | }; | ||
| 382 | |||
| 383 | $::html && do { | ||
| 384 | require WWW::Link::Reporter::HTML; | ||
| 385 | $::reporter=new WWW::Link::Reporter::HTML (\*STDOUT, $::index); | ||
| 386 | last CASE; | ||
| 387 | }; | ||
| 388 | |||
| 389 | $::long_list && do { | ||
| 390 | require WWW::Link::Reporter::LongList; | ||
| 391 | die "you need to provide an index for long listing" unless $::index; | ||
| 392 | my $trans_sub=\&WWW::Link_Controller::InfoStruc::url_to_file; | ||
| 393 | $::reporter=new WWW::Link::Reporter::LongList ($trans_sub, $::index); | ||
| 394 | last CASE; | ||
| 395 | }; | ||
| 396 | |||
| 397 | require WWW::Link::Reporter::Text; | ||
| 398 | $::reporter=new WWW::Link::Reporter::Text $::index; | ||
| 399 | } | ||
| 400 | |||
| 401 | if ($::all_links || $::broken || $::not_perfect || $::okay || $::redirected | ||
| 402 | || $::ignore_missing || $::disallowed || $::unsupported || $::good) { | ||
| 403 | $::reporter->all_reports(0); | ||
| 404 | $::all_links && $::reporter->all_reports(1); | ||
| 405 | $::not_perfect and do { | ||
| 406 | $::reporter->report_not_perfect(1); | ||
| 407 | }; | ||
| 408 | $::good and do { | ||
| 409 | $::reporter->report_good(1); | ||
| 410 | }; | ||
| 411 | $::reporter->report_broken(1) if $::broken; | ||
| 412 | $::reporter->report_redirected(1) if $::redirected; | ||
| 413 | $::reporter->report_disallowed(1) if $::disallowed; | ||
| 414 | $::reporter->report_unsupported(1) if $::unsupported; | ||
| 415 | $::reporter->report_okay(1) if $::okay; | ||
| 416 | } else { | ||
| 417 | #DWIM defaults | ||
| 418 | CASE: { | ||
| 419 | $::reporter->all_reports(0); | ||
| 420 | $::uri_report && do { | ||
| 421 | $::reporter->report_broken(1) if $::broken; | ||
| 422 | last CASE; | ||
| 423 | }; | ||
| 424 | $::reporter->default_reports(); | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | CASE: { | ||
| 429 | ($::uri_list ) && do { | ||
| 430 | print STDERR "Reporting on specific URIs:", join (" ", @::uris), "\n" | ||
| 431 | if $::verbose & 16; | ||
| 432 | $::selectfunc = | ||
| 433 | WWW::Link::Selector::generate_url_func (\%::links,$::reporter,@::uris ); | ||
| 434 | last CASE; | ||
| 435 | }; | ||
| 436 | (@::page_exc || @::page_inc ) && do { | ||
| 437 | die "page filtering only works in page mode" unless $::index; | ||
| 438 | print STDERR "Reporting on filtered pages from the database.\n" | ||
| 439 | if $::verbose & 16; | ||
| 440 | $::selectfunc = | ||
| 441 | WWW::Link::Selector::generate_index_select_func | ||
| 442 | ( \%::links, $::reporter, $::include, $::index, ); | ||
| 443 | last CASE; | ||
| 444 | }; | ||
| 445 | print STDERR "Reporting on filtered URIs direct from database.\n" | ||
| 446 | if $::verbose & 16; | ||
| 447 | $::selectfunc = | ||
| 448 | WWW::Link::Selector::generate_select_func | ||
| 449 | ( \%::links, $::reporter, $::include, $::index, ); | ||
| 450 | } | ||
| 451 | |||
| 452 | $WWW::Link::Selector::ignore_missing = $::ignore_missing; | ||
| 453 | |||
| 454 | $::reporter->heading; | ||
| 455 | &$::selectfunc; | ||
| 456 | $::reporter->footer; | ||
| 457 | |||
| 458 | print STDERR "finished reporting on links\n" | ||
| 459 | if $::verbose; | ||