this *should* fix munging of non-internal links, i hope
[freeside.git] / bin / pod2x
1 #!/usr/bin/perl -w
2
3 use strict;
4 use WWW::Mediawiki::Client;
5 #sub WWW::Mediawiki::Client::pagename_to_url {
6 #    my ($self, $name, $action) = @_;
7 #    WWW::Mediawiki::Client::URLConstructionException->throw(
8 #            error => 'No action supplied.',
9 #        ) unless $action;
10 #    WWW::Mediawiki::Client::URLConstructionException->throw(
11 #            error => "Page name $name ends with '.wiki'.",
12 #        ) if $name =~ /.wiki$/;
13 #    my $char = $self->space_substitute;
14 #    $name =~ s/ /$char/;
15 #    my $lang = $self->language_code;
16 #    my $host = $self->host;
17 #    $host =~ s/__LANG__/$lang/g;
18 #    my $wiki_path = $self->wiki_path;
19 #    $wiki_path =~ s/__LANG__/$lang/g;
20 #    my $protocol = $self->protocol;
21 #    return "$protocol://$host/$wiki_path?" . ACTION . "=$action&" . TITLE . "=$name" . '&wpRecreate=1';
22 #}
23
24 my $mw_username = 'ivan';
25 chomp( my $mw_password = `cat .mw-password` );
26
27 my $site_perl = "./FS";
28 my $html = "Freeside:1.7:Documentation:Developer";
29
30 foreach my $dir (
31   $html,
32   map "$html/$_", qw( bin FS FS/UI FS/part_export FS/part_pkg
33                       FS/ClientAPI FS/Cron FS/Misc FS/Report FS/Report/Table
34                       FS/TicketSystem FS/UI
35                     )
36 ) {
37   -d $dir or mkdir $dir;
38 }
39
40 $|=1;
41
42 die "Can't find $site_perl" unless -d $site_perl;
43 #die "Can't find $catman" unless -d $catman;
44 -d $html or mkdir $html;
45
46 my $count = 0;
47
48 #make some useless links
49 foreach my $file (
50   glob("$site_perl/bin/freeside-*"),
51 ) {
52   next if $file =~ /\.pod$/;
53   #symlink $file, "$file.pod"; # or die "link $file to $file.pod: $!";
54   #system("cp $file $file.pod");
55   -e "$file.pod" or system("cp $file $file.pod");
56 }
57
58 my $mvs = WWW::Mediawiki::Client->new(
59             'host'           => 'www.sisd.com',
60             'wiki_path'      => 'mediawiki/index.php',
61             'username'       => $mw_username,
62             'password'       => $mw_password,
63             #'commit_message' => 'import from POD'
64           );
65
66 $mvs->do_login;
67
68 foreach my $file (
69   glob("$site_perl/*.pm"),
70   glob("$site_perl/*/*.pm"),
71   glob("$site_perl/*/*/*.pm"),
72   glob("$site_perl/*/*/*/*.pm"),
73   glob("$site_perl/bin/*.pod"),
74   glob("./fs_selfservice/FS-SelfService/*.pm"),
75   glob("./fs_selfservice/FS-SelfService/*/*.pm"),
76 ) {
77   next if $file =~ /(^|\/)blib\//;
78   next if $file =~ /(^|\/)CVS\//;
79   #$file =~ /\/([\w\-]+)\.pm$/ or die "oops file $file";
80   my $name;
81   if ( $file =~ /fs_\w+\/FS\-\w+\/(.*)\.pm$/ ) {
82     $name = "FS/$1";
83   } elsif ( $file =~ /$site_perl\/(.*)\.(pm|pod)$/ ) {
84     $name = $1;
85   } else {
86     die "oops file $file";
87   }
88
89   #exit if $count++ == 10;
90
91   my $htmlroot = join('/', map '..',1..(scalar($file =~ tr/\///)-2)) || '.';
92
93   system "pod2wiki  --style mediawiki $file >$html/$name.rawwiki";
94
95   if ( -e "$html/$name.rawwiki" ) {
96     print "processing $name\n";
97   } else {
98     print "skipping $name\n";
99     next;
100   };
101
102   $mvs->do_update("$html/$name.wiki");
103
104   open(RAW, "<$html/$name.rawwiki") or die $!;
105   open(WIKI,">$html/$name.wiki"   ) or die $!;
106   while (<RAW>) {
107     s/\[\[([^#\/][^\]]*)\]\]/"[[$html\/". w_e($1). "|$1]]"/ge;
108     print WIKI $_;
109   }
110   close RAW;
111   close WIKI;
112
113   print "  uploading to ". $mvs->filename_to_pagename("$html/$name.wiki"). "\n";
114   $mvs->commit_message( 'import from POD' );
115   $mvs->do_commit("$html/$name.wiki");
116
117 }
118
119 sub w_e {
120   my $s = shift;
121   $s =~ s/_/ /g;
122   $s =~ s/::/\//g;
123   $s =~ s/^freeside-/bin\/freeside-/g;
124   $s;
125 }
126
127
128 ##  system "pod2text $file >$catman/$name.txt"; 
129 ##
130 #  system "pod2html --podroot=$site_perl --podpath=./FS:./FS/UI:.:./bin --norecurse --htmlroot=$htmlroot $file >$html/$name.html";
131 #  #system "pod2html --podroot=$site_perl --htmlroot=$htmlroot $file >$html/$name.html";
132 ##  system "pod2html $file >$html/$name.html";
133 ##
134
135 #remove the useless links
136 unlink glob("$site_perl/bin/*.pod");
137