| 1 | |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | use WWW::Mechanize; |
|---|
| 7 | use Getopt::Long; |
|---|
| 8 | use Pod::Usage; |
|---|
| 9 | use OpenGuides::RDF::Reader; |
|---|
| 10 | use XML::RSS; |
|---|
| 11 | use Data::Dumper; |
|---|
| 12 | use OpenGuides; |
|---|
| 13 | use OpenGuides::Config; |
|---|
| 14 | |
|---|
| 15 | our $VERSION = 0.02; |
|---|
| 16 | my $help = 0; |
|---|
| 17 | my $list_version = 0; |
|---|
| 18 | my $days=0; |
|---|
| 19 | my ($site,$config_file); |
|---|
| 20 | |
|---|
| 21 | (GetOptions( |
|---|
| 22 | 'site=s' => \$site, |
|---|
| 23 | 'config=s' => \$config_file, |
|---|
| 24 | 'days=i' => \$days, |
|---|
| 25 | 'help+' => \$help, |
|---|
| 26 | 'version!' => \$list_version, |
|---|
| 27 | ) && $site && $config_file) |
|---|
| 28 | || pod2usage( -verbose => 0 ); |
|---|
| 29 | |
|---|
| 30 | pod2usage( -verbose => $help) if $help; |
|---|
| 31 | |
|---|
| 32 | (print STDERR "$0 version $VERSION\n\n"), exit 0 |
|---|
| 33 | if $list_version; |
|---|
| 34 | |
|---|
| 35 | =head1 NAME |
|---|
| 36 | |
|---|
| 37 | mirror.pl - Replicate an OpenGuides site |
|---|
| 38 | |
|---|
| 39 | =head1 SYNOPSIS |
|---|
| 40 | |
|---|
| 41 | mirror.pl --site http://from.site.url/ --config /path/to/wiki.conf [--days 1] |
|---|
| 42 | |
|---|
| 43 | =head1 DESCRIPTION |
|---|
| 44 | |
|---|
| 45 | This is a script to mirror the contents from another OpenGuides website. |
|---|
| 46 | It can be run from a cron job to update periodically. |
|---|
| 47 | |
|---|
| 48 | To initially load the wiki, run the script without the --days option. Then, |
|---|
| 49 | the script can be run periodically with the --days option, to keep the site |
|---|
| 50 | in line. |
|---|
| 51 | |
|---|
| 52 | =head1 OPTIONS |
|---|
| 53 | |
|---|
| 54 | =over 4 |
|---|
| 55 | |
|---|
| 56 | =item C<--site | -s> |
|---|
| 57 | |
|---|
| 58 | Specify the guide website to mirror from. |
|---|
| 59 | |
|---|
| 60 | =item C<--config | -c> |
|---|
| 61 | |
|---|
| 62 | Path to the config file for the wiki on the localhost. |
|---|
| 63 | |
|---|
| 64 | =item C<--days | -d> |
|---|
| 65 | |
|---|
| 66 | Number of days back to look at in the RSS feed. Omit this option to work |
|---|
| 67 | in "hoover" mode. |
|---|
| 68 | |
|---|
| 69 | =item C<--help | -h> |
|---|
| 70 | |
|---|
| 71 | Show this list of options. |
|---|
| 72 | |
|---|
| 73 | =item C<--help --help | -h -h> |
|---|
| 74 | |
|---|
| 75 | Display man page. |
|---|
| 76 | |
|---|
| 77 | =item C<--version> |
|---|
| 78 | |
|---|
| 79 | Show the mirror script's version number |
|---|
| 80 | |
|---|
| 81 | =back |
|---|
| 82 | |
|---|
| 83 | =head1 HISTORY |
|---|
| 84 | |
|---|
| 85 | 0.01 18-Oct-2005 Initial version |
|---|
| 86 | |
|---|
| 87 | 0.02 19-Oct-2005 Exclude updates for pages that haven't changed |
|---|
| 88 | |
|---|
| 89 | =cut |
|---|
| 90 | |
|---|
| 91 | my $agent = WWW::Mechanize->new(); |
|---|
| 92 | |
|---|
| 93 | my $config = OpenGuides::Config->new( file => $config_file ); |
|---|
| 94 | my $guide = OpenGuides->new( config => $config ); |
|---|
| 95 | my $wiki = $guide->wiki; |
|---|
| 96 | |
|---|
| 97 | my @pagelist = $days ? get_recent_changes($agent, $site, $days) : |
|---|
| 98 | get_all_pages($agent, $site); |
|---|
| 99 | |
|---|
| 100 | $|=1; |
|---|
| 101 | |
|---|
| 102 | for (@pagelist) { |
|---|
| 103 | chomp; |
|---|
| 104 | print $_,":"; |
|---|
| 105 | |
|---|
| 106 | my %meta = eval { get_page_metadata($agent, $site, $_) }; |
|---|
| 107 | (print "Failed to parse metadata\n"),next if $@; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | my $text = get_page_content($agent, $site, $_); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | populate_local_wiki($wiki, $_, $text, \%meta); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | sub get_all_pages { |
|---|
| 118 | my ($ua, $url) = @_; |
|---|
| 119 | |
|---|
| 120 | $ua->get("$url?action=index;format=plain"); |
|---|
| 121 | |
|---|
| 122 | split /\n/,$ua->content; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | sub get_recent_changes { |
|---|
| 126 | my ($ua, $url, $days) = @_; |
|---|
| 127 | |
|---|
| 128 | $ua->get("$url?action=rss;days=$days"); |
|---|
| 129 | my $rss = XML::RSS->new; |
|---|
| 130 | $rss->parse($ua->content); |
|---|
| 131 | reverse map {$_->{title}} @{$rss->{items}}; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | sub get_page_metadata { |
|---|
| 135 | my ($ua, $url, $page) = @_; |
|---|
| 136 | |
|---|
| 137 | $ua->get("$url?id=$page;format=rdf"); |
|---|
| 138 | |
|---|
| 139 | my $rdf = $ua->content; |
|---|
| 140 | |
|---|
| 141 | parse_rdf($rdf); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | sub get_page_content { |
|---|
| 145 | my ($ua, $url, $page) = @_; |
|---|
| 146 | |
|---|
| 147 | $ua->get("$url?id=$page;format=raw"); |
|---|
| 148 | |
|---|
| 149 | $ua->content; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | sub populate_local_wiki { |
|---|
| 153 | my ($wiki, $page, $content, $metadata) = @_; |
|---|
| 154 | |
|---|
| 155 | my $node = $wiki->formatter->node_param_to_node_name( $page ); |
|---|
| 156 | my %old_data = $wiki->retrieve_node($node); |
|---|
| 157 | |
|---|
| 158 | if ($old_data{version}) { |
|---|
| 159 | (print "Unchanged\n"), return |
|---|
| 160 | if $old_data{metadata}{version}[0] == $metadata->{version}; |
|---|
| 161 | print "Updating... "; |
|---|
| 162 | } |
|---|
| 163 | else { |
|---|
| 164 | print "Creating... "; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | my $written = $wiki->write_node( $node, $content, $old_data{checksum}, $metadata); |
|---|
| 168 | |
|---|
| 169 | print $written ? "Done\n" : "Failed\n"; |
|---|
| 170 | } |
|---|