#!/usr/bin/perl use strict; topictree_find_unmentioned_pages(web=>$ARGV[0],file_containing_topictree=>$ARGV[1]); exit(0); sub topictree_find_unmentioned_pages { my %args = @_; die "file_containing_topictree does not exist: <<$args{file_containing_topictree}>>" if ! -e $args{file_containing_topictree}; die "web does not exist: <<$args{web}>>" if ! -e $args{web}; print "This file automatically generated by script topictree_find_unmentioned_pages.pl\n\n"; print "Date script last run: " . `date` . "\n\n"; print "Typically, inspect this, edit it, and copy back to TopicTree\n\n"; print "\n"; my $topictree = TopicTree::scan_topictree($args{file_containing_topictree}); my $topiclist = list_of_topics_in_web($args{web}); #print "topicslist=" . join(' ',@$topiclist); my @missing_topic_list; foreach my $f ( @$topiclist ) { #print "$f\n"; push(@missing_topic_list, $f) if ! $topictree->topic_is_mentioned($f); } if( @missing_topic_list ) { print "Topics missing from topictree:\n"; foreach my $m ( @missing_topic_list ) { print " * [[$m]]\n"; } $topictree->print; } else { print "No files missing from TopicTree\n"; } } sub list_of_topics_in_web { my $web = shift; die "web does not exist $web" if ! -e $web; my $ret = []; opendir my $webdir,$web; while(my $file=readdir($webdir)) { next unless ($file =~ /\.txt$/); my $topic = $file; $topic =~ s/\.txt$//; push @$ret, $topic; } return $ret; } { package TopicTree; sub new { my $this = {}; bless $this; } sub scan_topictree { my $file = shift; die "scan_topictree does not exist: <<$file>>" if ! -e $file; my $topictree = new TopicTree; $topictree->{links_mentioned} = {}; my $topictree_linenumber = 0; my $in_topictree = 0; open my $fh, $file; while( my $line = <$fh> ) { #print $line; $topictree->{raw_text} .= $line; $topictree_linenumber++ if $in_topictree; # Look for %BeginTopicTree% / %EndTopicTree% # TBD: provide %BeginTopicTree{Name}% if( $line =~ /%BeginTopicTree%/ ) { $in_topictree = 1; $topictree_linenumber = 0; } if( $line =~ /%EndTopicTree%/ ) { $in_topictree = 0; } # if between BeginTopicTree and EndTopicTree if( $in_topictree && $topictree_linenumber > 0 ) { # if looking at a line that looks like a TopicTree line # (other lines are just comments, as is rest of a TopicTree line) if( $line =~ m/^ *\*/ ) { # delete twiki list syntax. # TBD: remember list depth? my $line_ = $line; $line_ =~ s/^ *\*[ \t]*//; # extract first twikilink on line - that is the link in the topictree. my $link; $link = $1 if $line_ =~ m{^\[\[([^]]+)\]\]}; $link = $1 if $line_ =~ m{^\[\[([^]]+)\]\[[^\]]*\]\]}; $link = $1 if $line_ =~ m{^([A-Z][a-zA-Z0-9]*)}; my $lm = $topictree->{links_mentioned}; $$lm{$link}++; #print "DEBUG Raw line = $line\nlink extracted = $link
\n\n" # if "$link" eq ""; } } } return $topictree; } sub topic_is_mentioned { my $this = shift; my $topic = shift; return $this->{links_mentioned}->{$topic}; } sub print { my $this = shift; print $this->{raw_text}; } }