| 1 | #!/usr/local/bin/perl -w | ||
| 2 | #!/usr/bin/perl -w | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | copy-list - copy a list of links from one database to another | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | copy-list link-database-filename listfile .... | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | This program simply copies over a list of links from the old version | ||
| 15 | of the database to the new. It moves the old version to a file of the | ||
| 16 | same name, but with C<.bak> appended. | ||
| 17 | |||
| 18 | This program can be used to clean up the database by only keeping | ||
| 19 | links which are currently in use. This may not always be advisable | ||
| 20 | though because a page may be curently deleted but be going to be | ||
| 21 | re-instated which will mean all of it's links would need to return to | ||
| 22 | the database in which case all of the links in that page will have to | ||
| 23 | be checked again from the beginning which will take a long time. | ||
| 24 | |||
| 25 | =head1 SEE ALSO | ||
| 26 | |||
| 27 | L<verify-link-control(1)>; L<extract-links(1)>; L<build-schedule> | ||
| 28 | L<link-report(1)>; L<fix-link(1)>; L<link-report.cgi(1)>; L<fix-link.cgi> | ||
| 29 | L<suggest(1)>; L<link-report.cgi(1)>; L<configure-link-control> | ||
| 30 | |||
| 31 | The LinkController manual in the distribution in HTML, info, or | ||
| 32 | postscript formats, included in the distribution. | ||
| 33 | |||
| 34 | http://scotclimb.org.uk/software/linkcont - the | ||
| 35 | LinkController homepage. | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | require 5.001; | ||
| 40 | |||
| 41 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | |||
| 43 | 2 | ▶ | use Fcntl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | 0 | ▶ | use DB_File; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 45 | use Data::Dumper; | ||
| 46 | use MLDBM qw(DB_File); #chosen over GDBM for network byte order. | ||
| 47 | #use MLDBM; | ||
| 48 | use URI; | ||
| 49 | |||
| 50 | use WWW::Link; | ||
| 51 | use WWW::Link_Controller; | ||
| 52 | use WWW::Link_Controller::Lock; | ||
| 53 | use WWW::Link_Controller::ReadConf; | ||
| 54 | |||
| 55 | use vars qw($olddbm $linkdbm); | ||
| 56 | |||
| 57 | |||
| 58 | ############################################################################## | ||
| 59 | #start command line processing | ||
| 60 | use Getopt::Function qw(maketrue makevalue); | ||
| 61 | |||
| 62 | $::verbose=0; | ||
| 63 | $::old_database=undef; | ||
| 64 | $::new_database=undef; | ||
| 65 | $::opthandler = new Getopt::Function | ||
| 66 | [ "version V>version", | ||
| 67 | "usage h>usage help>usage", | ||
| 68 | "help-opt=s", | ||
| 69 | "verbose:i v>verbose", | ||
| 70 | "", | ||
| 71 | "old-database=s o>old-database", | ||
| 72 | "new-database=s n>new-database", | ||
| 73 | ], { | ||
| 74 | "old-database" => [ \&makevalue, | ||
| 75 | "Copy links from given old database file.", | ||
| 76 | "FILENAME" ], | ||
| 77 | "new-database" => [ \&makevalue, | ||
| 78 | "Copy links to given new database file.", | ||
| 79 | "FILENAME" ], | ||
| 80 | }; | ||
| 81 | |||
| 82 | $::opthandler->std_opts; | ||
| 83 | |||
| 84 | $::opthandler->check_opts; | ||
| 85 | |||
| 86 | sub usage() { | ||
| 87 | print <<'EOF'; | ||
| 88 | copy-list.pl [options] link_database_file list_of_links | ||
| 89 | |||
| 90 | EOF | ||
| 91 | $::opthandler->list_opts; | ||
| 92 | print <<'EOF'; | ||
| 93 | |||
| 94 | Copy the links given in the list_of_links file (or STDIN) to the new | ||
| 95 | version of the database, renaming the old version by appending .bak | ||
| 96 | (unless the -o option is used). | ||
| 97 | EOF | ||
| 98 | } | ||
| 99 | sub version() { | ||
| 100 | print <<'EOF'; | ||
| 101 | copy-list version | ||
| 102 | $Id: copy-list.pl,v 1.7 2002/01/02 22:53:15 mikedlr Exp $ | ||
| 103 | EOF | ||
| 104 | } | ||
| 105 | |||
| 106 | CASE: { | ||
| 107 | defined $::new_database and last; | ||
| 108 | |||
| 109 | @ARGV && do { | ||
| 110 | $::new_database =shift; | ||
| 111 | die "excess command line arguments" if @ARGV; | ||
| 112 | last; | ||
| 113 | }; | ||
| 114 | |||
| 115 | defined $::links && do { | ||
| 116 | $::new_database = $::links; | ||
| 117 | last; | ||
| 118 | }; | ||
| 119 | |||
| 120 | die "no idea which database to copy to"; | ||
| 121 | |||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | ############################################################################## | ||
| 126 | #end command line processing | ||
| 127 | |||
| 128 | #If your filesystem can't cope with long names REPLACE IT. Same goes | ||
| 129 | #for the operating system. | ||
| 130 | $::old_data=0; | ||
| 131 | |||
| 132 | #FIXME, locking using the old database!!!???? | ||
| 133 | WWW::Link_Controller::Lock::lock($::new_database); | ||
| 134 | if ($::old_database) { | ||
| 135 | $::olddbm = tie ( %::old_links, "MLDBM", $::old_database, O_RDONLY, | ||
| 136 | 0666, $DB_HASH ) | ||
| 137 | or die "old links failed: $!"; | ||
| 138 | } else { | ||
| 139 | #calmly and casually trash any similarly named files .. oh hell #FIXME | ||
| 140 | #and feel really superior and moral if you wish. | ||
| 141 | rename $::new_database, $::new_database . '.bak' | ||
| 142 | and ( $::olddbm = tie ( %::old_links, "MLDBM", $::new_database . '.bak' , | ||
| 143 | O_RDONLY, 0666, $DB_HASH ) | ||
| 144 | or die "old links failed: $!" ) | ||
| 145 | and $::old_data=1; | ||
| 146 | } | ||
| 147 | |||
| 148 | # FIXME what checks and safety should we have about permissions and | ||
| 149 | # validity of old database. | ||
| 150 | |||
| 151 | $::linkdbm = tie %::links, "MLDBM", $::new_database, O_CREAT|O_RDWR, 0666, $DB_HASH | ||
| 152 | or die $!; | ||
| 153 | |||
| 154 | URLLINE: while ( <> ) { | ||
| 155 | chomp; | ||
| 156 | my $url=$_; | ||
| 157 | print STDERR "acting on $url\n" | ||
| 158 | if $::verbose & 4; | ||
| 159 | unless ( defined $::links{$url} ) { | ||
| 160 | if ( $::old_data && defined $::old_links{$url} ) { | ||
| 161 | $::links{$url}=$::old_links{$url}; | ||
| 162 | next URLLINE; | ||
| 163 | } | ||
| 164 | my $newlink=new WWW::Link $url; | ||
| 165 | $::links{$url}=$newlink; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | #FIXME: this should probably be optional; | ||
| 170 | |||
| 171 | foreach my $special_key ( @WWW::Link_Controller::special_keys ) { | ||
| 172 | $::links{$special_key}=$::old_links{$special_key} | ||
| 173 | if ( defined $::old_links{$special_key} ); | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | |||
| 183 | |||