Txt2wiki.pl

From PTAGISWiki

Jump to: navigation, search
#!/usr/bin/perl -w

use HTML::Entities;

my $indir = `pwd`;
chomp $indir;
my $infile;
my $outfile;
my $title;
my $id = 3000;

opendir(INDIR, $indir) || die "couldn't open $indir for reading";

while($infile = readdir(INDIR)) {
	next if ($infile =~ /^\./);
	next if ($infile =~ /xml$/);
	print "found $infile...\n";
	$outfile = $infile;
	$outfile =~ s/txt$/xml/;
	print "switch to $outfile\n";
	die unless ($outfile =~ m/xml$/);
	$title = $infile;
	$title =~ s/\.txt$//;

	open(IN, "$infile") || die "couldn't open $infile for read";
	open(OUT, ">$outfile") || die "couldn't open $outfile for write";
	&printheader();
	while (<IN>) {
		#print "read a line from $infile\n";
		print OUT encode_entities($_);
	}
	&printfooter();
	close(IN);
	close(OUT);
	print "converted $outfile...\n";
	$id++;
}
closedir(INDIR);




sub printheader {
	print OUT <<EOF;
<mediawiki lang="en" version="0.3" schemaLocation="http://www.mediawiki.org/xml/export-0.3/
http://www.mediawiki.org/xml/export-0.3.xsd" xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.mediawiki.org/xml/export-0.3/">
<siteinfo>
  <sitename>Wiki</sitename>
  <base>http://snapper/wiki/index.php/Main_Page</base>
  <generator>MediaWiki 1.5.2</generator>
  <case>first-letter</case>
  <namespaces>
    <namespace key="-2">Media</namespace>
    <namespace key="-1">Special</namespace>
    <namespace key="0"></namespace>
    <namespace key="1">Talk</namespace>
    <namespace key="2">User</namespace>
    <namespace key="3">User talk</namespace>
    <namespace key="4">Wiki</namespace>
    <namespace key="5">Wiki talk</namespace>
    <namespace key="6">Image</namespace>
    <namespace key="7">Image talk</namespace>
    <namespace key="8">MediaWiki</namespace>
    <namespace key="9">MediaWiki talk</namespace>
    <namespace key="10">Template</namespace>
    <namespace key="11">Template talk</namespace>
    <namespace key="12">Help</namespace>
    <namespace key="13">Help talk</namespace>
    <namespace key="14">Category</namespace>
    <namespace key="15">Category talk</namespace>
  </namespaces>
</siteinfo>
<page>
  <title>$title</title>
  <id>$id</id>
    <revision>
      <id>$id</id>
      <timestamp>2005-12-02T18:11:55Z</timestamp>
      <contributor><username>Root</username><id>1</id></contributor>
      <text space="preserve">
		[[Category:Documentation]]
		[[Category:System]]
EOF
}

sub printfooter {
	print OUT <<EOF;
</text>
</revision>
</page>
</mediawiki>
EOF
}
Personal tools