summaryrefslogtreecommitdiff
path: root/helpcontent2/upload-wiki.pl
blob: 1531b216e16651b38d11d5912e9c3992d194d87c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl -w

use MediaWiki::API;
use File::Find();
use File::Slurp;

# help
sub usage {
    print <<EOM;
upload-wiki.pl - Uploads the wiki/ subdir to a real wiki installation.

You need a wikisetup.txt in this directory, to be able to authentificate you.
The content should be:

wiki=<url of the api.php on the wiki>
name=<the user name>
password=<the appropriate password>

EOM
    exit 1;
}

# first of all, read the configuration from wikisetup.txt
my ( $url, $name, $password );
open( IN, "wikisetup.txt" ) || usage();
while ( my $line = <IN> ) {
    if ( $line =~ /^([^=]*)=(.*)$/ ) {
        my $k = $1;
        my $v = $2;
        chomp $k;
        chomp $v;
        if ( $k eq 'wiki' ) {
            $url = $v;
        }
        elsif ( $k eq 'name' ) {
            $name = $v;
        }
        elsif ( $k eq 'password' ) {
            $password = $v;
        }
    }
}
close( IN );

if ( !defined( $url ) || !defined( $name ) || !defined( $password ) ) {
    usage();
}

# initialize the wiki
my $mw = MediaWiki::API->new();
$mw->{config}->{api_url} = $url;

# log in to the wiki
$mw->login( { lgname => $name, lgpassword => $password } ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};

# upload the articles
sub upload_article {
    -f || return;

    my $pagename = $File::Find::name;
    $pagename =~ s/^wiki\///;
    my $text = read_file( $_ );

    print "Uploading '$pagename'\n";
    $mw->edit( {
            action => 'edit',
            title => $pagename,
            text => $text } ) || die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
}
File::Find::find( {wanted => \&upload_article}, 'wiki/' );

# clean up
$mw->logout();

# vim:set shiftwidth=4 softtabstop=4 expandtab: