]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - admin/wiki2omega
Allow bold, italics or underlined for numbers
[SourceForge/phpwiki.git] / admin / wiki2omega
1 #!/usr/bin/perl -w
2 # Copyright (C) 2007 Reini Urban
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2 of the
7 # License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 =pod
19
20 =head1 NAME
21
22 wiki2omega [-u URL] WIKIDB
23
24 =over 4
25
26 =item B<WIKIDB>
27
28 The first argument may be a phpwiki url, a directory or a single file.
29 If it is an url the -url option is optional, otherwise mandatory.
30
31 =item B<-url>
32
33 The url prefix for the xapian database to be able to link to the result page.
34
35 =item B<-man>
36
37 Prints the manual page and exits.
38
39 =back
40
41 =head1 DESCRIPTION
42
43 Convert a phpwiki database to a format recognizable for xapian scriptindex,
44 to prepare a fulltext index.
45
46 =head1 SYNOPSIS
47
48   # serial dump (see config.ini for DEFAULT_DUMP_DIR)
49   DEFAULT_DUMP_DIR = /var/www/wikidb/pgsrc
50   lwp-request -d http://localhost/phpwiki/?action=dumpserial
51   wiki2omega -u wiki $DEFAULT_DUMP_DIR | \
52     scriptindex /var/lib/omega/data/wiki /usr/share/omega/wiki2omega.script
53
54   # zip dump
55   wiki2omega http://localhost/phpwiki/ | \
56     scriptindex /var/lib/omega/data/wiki /usr/share/omega/wiki2omega.script
57
58   # single file
59   wiki2omega -u wiki /tmp/wikidump/HomePage | \
60     scriptindex /var/lib/omega/data/wiki /usr/share/omega/wiki2omega.script
61
62   # real life usage
63   DEFAULT_DUMP_DIR = /var/www/wikidb/pgsrc
64   nice /usr/bin/lwp-request -P -d -m GET "http://localhost/wiki/?action=dumpserial"
65   nice wiki2omega -u /wiki $DEFAULT_DUMP_DIR | \
66     scriptindex /var/lib/omega/data/wiki /var/lib/omega/scripts/wiki2index.script \
67     > /var/log/omega/updateindex-wiki.log
68
69 =cut
70
71 use strict;
72 use Getopt::Long;
73 use Pod::Usage;
74 use Digest::MD5 qw(md5_hex);
75 use constant AZ_OK => 0;
76
77 my $man = 0;
78 my $help = 0;
79 my $db;
80 my $wikiurl;
81
82 GetOptions('help|?'  => \$help,
83            'man'     => \$man,
84            'url|u=s' => \$wikiurl,
85           ) or pod2usage(2);
86 pod2usage(1) if $help;
87 pod2usage(-exitstatus => 0, -verbose => 2) if $man;
88
89 sub urldecode ($) {
90   my $s = shift;
91   $s =~ s/%([A-F0-9][A-F0-9])/chr(hex($1))/eg;
92   return $s;
93 }
94
95 sub p ($) {
96   # my $title = shift;
97   my $content = shift;
98   my $hdr = 1;
99   my $headers = '';
100   my %hdr;
101   return unless $content;
102   $hdr{md5} = md5_hex($content);
103   $hdr{size} = length($content);
104   $hdr{language} = 'english';
105   @_ = split(/\n/, $content);
106  LINE: while ($_ = shift @_) {
107     s/\r$//;
108     if ($hdr) {
109         if (/^$/) {
110           # headers finished, dump them
111           my $title = $hdr{title};
112           print "url=$wikiurl/$title\n";
113           for my $h (keys %hdr) {
114             print "$h=$hdr{$h}\n";
115           }
116           print "content";
117           $hdr = 0;
118           next LINE;
119         }
120         # ignore continuation lines
121         my $line = $_;
122         #while ($_ = shift @_) {
123         #  s/\r$//;
124         #  last unless /^[ \t]/;
125         #  $line .= $_;
126         #}
127         if ($line =~ s/^Date:\s*(.*?)\s*$/$1/i) {
128           $hdr{date} = $line if length $line;
129           #print "date=$line\n" if length $line;
130         } elsif ($line =~ s/  pagename=(.*?);$/$1/) {
131           $hdr{title} = urldecode($line) if length $line;
132         } elsif ($line =~ s/  author=(.*?);$/$1/) {
133           $hdr{author} = urldecode($line) if length $line;
134         } elsif ($line =~ s/  lastmodified=(.*?);$/$1/) {
135           $hdr{lastmod} = $line if length $line;
136           $hdr{date} = $line if length $line;
137         }
138     } else {
139       # the rest is the content:
140       # gather headers
141       if (/^\s*\!+(.+)$/) {
142         $headers .= "$1 ";
143       }
144       print "=",$_,"\n";
145     }
146   }
147   print "headers=$headers" if $headers;
148 }
149
150 unless ($wikiurl) {
151   $wikiurl = $db = shift or die "Syntax: $0 WIKIURL\n";
152 } else {
153   $db = shift or die "Syntax: $0 -u WIKIURL DATABASE\n";
154 }
155 if ($db =~ /^http/) {
156   `wget -nv -O/tmp/wikidb.zip "$db?action=zip"`;
157   $db = "/tmp/wikidb.zip";
158 }
159
160 if ($db =~ /\.zip$/i) {
161   eval "require Archive::Zip;";
162   my $zip = Archive::Zip->new();
163   die 'wikidb.zip read error' unless $zip->read( $db ) == AZ_OK;
164   foreach my $member ($zip->members()) {
165     unless ($member->isDirectory()) {
166       my $page = $zip->contents($member);
167       p ($page);
168       print "\n";
169     }
170   }
171 } elsif (-d $db) {
172   local $/;
173   @_ = glob "$db/*";
174   while (my $filename = shift @_) {
175     open IN, "< $filename";
176     my $content = <IN>;
177     p ($content);
178     print "\n";
179     close IN;
180   }
181 } elsif (-f $db) {
182   local $/;
183   open IN, "< $db";
184   my $content = <IN>;
185   p ($content);
186   close IN;
187   print "\n";
188 } else {
189   die "invalid argument";
190 }
191