Skip to content

Commit 0642ad3

Browse files
SvenDowideitSvenDowideit
authored andcommitted
import PhpWikiToTWikiAddOn
1 parent 3007a22 commit 0642ad3

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

PhpWikiToTWikiAddOn/bin/php2twiki

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/perl -w
2+
#
3+
# Migrate from phpwiki to twiki
4+
#
5+
# Use phpwiki dump tool, dump as raw text
6+
#
7+
# Juan Grigera <[email protected]>
8+
# ver 0.1 - 20050502
9+
10+
use strict;
11+
$VERSION = '0.1';
12+
13+
use constant DEBUG => 0;
14+
15+
my %HEADERS;
16+
17+
my $PHPWIKI_DUMP = "/tmp/wikidump/";
18+
my $DEST_DIR = "/var/lib/twiki/data/TPing/";
19+
20+
# Iterate dump
21+
my $file;
22+
opendir(DIR, $PHPWIKI_DUMP) || die "can't opendir $PHPWIKI_DUMP: $!";
23+
while($file = readdir(DIR)) {
24+
convertFile("$file") if -f "$PHPWIKI_DUMP/$file";
25+
}
26+
closedir DIR;
27+
28+
29+
#
30+
# convert a File
31+
sub convertFile
32+
{
33+
my ($file) = @_;
34+
my $flag_header = 1;
35+
36+
my ($HANDLE, $OUTPUT);
37+
open($HANDLE, "$PHPWIKI_DUMP/$file") || die "can't open read file $file: $!";;
38+
39+
$file = "WebHome" if ($file eq "HomePage");
40+
open($OUTPUT, "> $DEST_DIR/$file.txt") || die "can't open dest file $file $!";
41+
42+
43+
while(<$HANDLE>) {
44+
# Read headers
45+
if($flag_header) {
46+
if($_ =~ /^\r?$/) {
47+
$flag_header = 0;
48+
49+
# Print header
50+
print $OUTPUT "\%META:TOPICINFO{author=\"" .
51+
$HEADERS{'author'} . '" date="' .
52+
$HEADERS{'lastmodified'} . '" format="1.0" version="1.1"}%' . "\n";
53+
} else {
54+
readHeader($_);
55+
}
56+
} else {
57+
filterWiki($_, $OUTPUT);
58+
}
59+
}
60+
close($OUTPUT);
61+
close($HANDLE);
62+
}
63+
64+
#
65+
#
66+
# parse phpwiki Header
67+
sub readHeader
68+
{
69+
my ($line) = @_;
70+
my $key;
71+
my $value;
72+
73+
$line =~ m/^([^:]+): (.*)\r?$/;
74+
if(defined $1) {
75+
$key = $1;
76+
$value = $2;
77+
} else {
78+
$line =~ m/^\s*([^=]+)=(.*)\r?$/;
79+
$key = $1 if (defined $1);
80+
$value = $2;
81+
}
82+
$value =~ s/\r|\n//g;
83+
$HEADERS{$key} = $value;
84+
}
85+
86+
#
87+
#
88+
# Filter php-Wiki format
89+
my $verbatim = 0;
90+
sub filterWiki
91+
{
92+
my ($line, $OUTPUT) = @_;
93+
94+
95+
# check verbatim
96+
if($line =~ m/<pre>/) {
97+
$line =~ s/<pre>/<verbatim>/;
98+
$verbatim = 1;
99+
}
100+
if($verbatim && not($line =~ m/<\/pre>/)) {
101+
print $OUTPUT $line;
102+
return;
103+
}
104+
105+
if($line =~ m/<\/pre>/) {
106+
$line =~ s/<\/pre>/<\/verbatim>/;
107+
$verbatim = 0;
108+
}
109+
110+
# Lists: * for bullet lists, # for numbered lists,
111+
$line =~ s/^\s*\*/\t*/;
112+
$line =~ s/^#/ 1 /;
113+
114+
# Titulos
115+
$line =~ s/^!!!/---+/;
116+
$line =~ s/^!!/---++/;
117+
$line =~ s/^!/---+++/;
118+
119+
# Tables
120+
if($line =~ m/\|\s*$/) {
121+
$line =~ s/\r\n//;
122+
$line = "|" . $line ;
123+
}
124+
# Forced links
125+
$line =~ s/\[//g;
126+
$line =~ s/\]//g;
127+
print $OUTPUT $line;
128+
}
129+

0 commit comments

Comments
 (0)