Files
oddmuse/scripts/graph.pl

107 lines
3.4 KiB
Perl
Raw Normal View History

2003-10-17 09:49:10 +00:00
#!/usr/bin/perl
# Copyright (C) 2003 Alex Schroeder <alex@emacswiki.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
2003-10-17 09:49:10 +00:00
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2003-10-17 09:49:10 +00:00
# Usage: perl graph.pl URL StartPage depth breadth stop-regexp
# All arguments are optional.
#
# Defaults:
2003-12-24 04:05:18 +00:00
# URL http://www.emacswiki.org/cgi-bin/wiki?action=links;exists=1;raw=1
# StartPage none -- all other options only have effect if this one is set!
2003-10-17 09:49:10 +00:00
# Depth 2
# Breadth 4
# Stop-Regexp ^(Category|SiteMap)
#
# The HTML data is cached. From then on the URL parameter has no effect.
2003-12-24 04:05:18 +00:00
# To refresh the cache, delete the 'graph.cache' file.
2003-10-17 09:49:10 +00:00
#
# Breadth selects a number of children to include. These are sorted by
# number of incoming links.
#
2003-12-24 04:05:18 +00:00
# Example usage:
# perl graph.pl -> download cache file and produce a graph.dot for the entire wiki
# perl graph.pl cache AlexSchroeder -> from the cache, start with AlexSchroeder
# springgraph < cache.dot > cache.png
#
2003-10-17 09:49:10 +00:00
$uri = $ARGV[0];
2003-12-24 04:05:18 +00:00
$uri = "http://www.emacswiki.org/cgi-bin/wiki?action=links;exists=1;raw=1" unless $uri;
2003-10-17 09:49:10 +00:00
$start = $ARGV[1];
$depth = $ARGV[2];
$depth = 2 unless $depth;
$breadth = $ARGV[3];
$breadth = 4 unless $breadth;
$stop = $ARGV[4];
$stop = "^(Category|SiteMap)" unless $stop;
2003-12-24 04:05:18 +00:00
if (-f 'graph.cache') {
print "Reusing graph.cache -- delete it if you want a fresh one.\n";
2003-10-17 09:49:10 +00:00
} else {
2003-12-24 04:05:18 +00:00
print "Downloading graph.cache and saving for reuse.\n";
$command = "wget -O graph.cache $uri";
2003-10-17 09:49:10 +00:00
print "Using $command\n";
system(split(/ /, $command)) == 0 or die "Cannot run wget\n";
}
2003-12-24 04:05:18 +00:00
if (not $start) {
open (F,'<graph.cache') or warn "Cannot read graph.cache\n";
print "Reading graph.cache...\n";
undef $/;
$data = <F>;
close (F);
open (F,'>graph.dot') or warn "Cannot write graph.dot\n";
print "Writing graph.dot...\n";
print "Using all pages...\n";
print F "digraph links {\n";
print F $data;
print F "}\n";
close (F);
exit;
2003-10-17 09:49:10 +00:00
}
2003-12-24 04:05:18 +00:00
open(F,'graph.cache') or warn "Cannot read graph.cache\n";
print "Reading graph.cache...\n";
while($_ = <F>) {
if (m/^"(.*?)" -> "(.*?)"$/) {
push (@{$page{$1}}, $2);
$score{$2}++;
2003-10-17 09:49:10 +00:00
}
}
2003-12-24 04:05:18 +00:00
close(F);
open(F,'>graph.dot') or warn "Cannot write graph.dot\n";
print "Writing graph.dot...\n";
2003-10-17 09:49:10 +00:00
print F "digraph links {\n";
2003-12-24 04:05:18 +00:00
print "Starting with $start...\n";
$count = 0;
@pages = ($start);
while ($count++ < $depth) {
@current = @pages;
foreach (@pages) {
$done{$_} = 1;
2003-10-17 09:49:10 +00:00
}
2003-12-24 04:05:18 +00:00
@pages = ();
foreach $page (@current) {
@links = @{$page{$page}};
@links = sort {$score{$a} <=> $score{$b}} @links; # only take pages with highest score
@links = @links[0..$breadth-1] if $#links >= $breadth;
next if $stop and eval "$page =~ /$stop/"; # no children for stop pages
foreach $target (sort @links) {
push(@pages, $target) unless $done{$target}; # don't cycle
2003-10-17 09:49:10 +00:00
print F "\"$page\" -> \"$target\"\n";
}
}
}
print F "}\n";
close(F);
print "Done.\n";