Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 135 lines (121 sloc) 5.183 kb
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#!/usr/bin/perl

use v5.10;

use Git::Repository;
use MediaWiki::Bot;
use File::Slurp;
use Getopt::Long::Descriptive;
use Encode;
use utf8;

my $config_file = "$ENV{HOME}/.mwbotrc";
my $c = {do "$config_file"} if -f $config_file;

my ($opt, $usage) = describe_options(
    "$0 %o <files...>",
    [ 'username|u=s', "username for account on wikipedia", {default => $c->{username} // ""} ],
    [ 'password|p=s', "password for account on wikipedia (do not use)", {default => $c->{password} // ""} ],
    [ 'base|b=s', "base location on wikipedia where files exist (default User:AzaToth or entry in .mwbotrc)", {default => $c->{base} // "User:AzaToth"} ],
    [ 'lang=s', 'Target language', {default => 'en'} ],
    [ 'family=s', 'Target family', {default => 'wikipedia'} ],
    [ 'mode' => hidden =>
        {
            required => 1,
            one_of => [
                ["pull" => "pull changes from wikipedia"],
                ["push" => "push changes to wikipedia"],
                ["deploy" => "push changes to wikipedia as gadgets"]
            ]
        }
    ],
    [ 'strip', "strip line end spaces"],
    [],
    [ 'verbose|v', "print extra stuff" ],
    [ 'help', "print usage message and exit" ],
);

print($usage->text), exit if $opt->help || !scalar(@ARGV);

my %pages = map +("$opt->{base}/$_" => $_), @ARGV;
my %deploys = (
'moment.js' => 'MediaWiki:Gadget-moment.js',
'twinkle.js' => 'MediaWiki:Gadget-Twinkle.js',
'morebits.js' => 'MediaWiki:Gadget-morebits.js',
'morebits.css' => 'MediaWiki:Gadget-morebits.css',
'modules/twinkleprod.js' => 'MediaWiki:Gadget-twinkleprod.js',
'modules/twinkleimage.js' => 'MediaWiki:Gadget-twinkleimage.js',
'modules/twinklebatchundelete.js' => 'MediaWiki:Gadget-twinklebatchundelete.js',
'modules/twinklewarn.js' => 'MediaWiki:Gadget-twinklewarn.js',
'modules/twinklespeedy.js' => 'MediaWiki:Gadget-twinklespeedy.js',
'modules/friendlyshared.js' => 'MediaWiki:Gadget-friendlyshared.js',
'modules/twinklediff.js' => 'MediaWiki:Gadget-twinklediff.js',
'modules/twinkleunlink.js' => 'MediaWiki:Gadget-twinkleunlink.js',
'modules/twinkledelimages.js' => 'MediaWiki:Gadget-twinkledelimages.js',
'modules/friendlytag.js' => 'MediaWiki:Gadget-friendlytag.js',
'modules/twinkledeprod.js' => 'MediaWiki:Gadget-twinkledeprod.js',
'modules/friendlywelcome.js' => 'MediaWiki:Gadget-friendlywelcome.js',
'modules/twinklexfd.js' => 'MediaWiki:Gadget-twinklexfd.js',
'modules/twinklebatchdelete.js' => 'MediaWiki:Gadget-twinklebatchdelete.js',
'modules/twinklebatchprotect.js' => 'MediaWiki:Gadget-twinklebatchprotect.js',
'modules/twinkleconfig.js' => 'MediaWiki:Gadget-twinkleconfig.js',
'modules/twinklefluff.js' => 'MediaWiki:Gadget-twinklefluff.js',
'modules/twinkleprotect.js' => 'MediaWiki:Gadget-twinkleprotect.js',
'modules/twinklearv.js' => 'MediaWiki:Gadget-twinklearv.js',
'modules/friendlytalkback.js' => 'MediaWiki:Gadget-friendlytalkback.js'
);

# Config file should be an hash consisting of username and password keys

my $repo = Git::Repository->new();


my $bot = MediaWiki::Bot->new({
        assert => 'user',
        protocol => 'https',
        host => "$opt->{lang}.$opt->{family}.org",
        login_data => { username => $opt->username, password => $opt->password},
        debug => $opt->{verbose} ? 2 : 0,
maxlag => 1000000 # not a botty script, thus smash it!
    }
);

if( $opt->mode eq "pull" ) {
    my @status = $repo->run( status => '--porcelain');

    if( scalar @status ) {
        say "repository is not clean. aborting...";
#exit;
    }

    while(my($page, $file) = each %pages) {
        say "Grabbing $page";
        my $text = $bot->get_text($page);
        $text =~ s/\h+$//mg if $opt->{'strip'};
        write_file( $file, {binmode => ':raw' }, encode('UTF-8',$text));
    }
    my $cmd = $repo->command( diff => '--stat', '--color' );
    my $s = $cmd->stdout;
    while (<$s>) {
        say $_;
    }
    $cmd->close;
} elsif( $opt->mode eq "push" ) {
    while(my($page, $file) = each %pages) {
        my $tag = $repo->run(describe => '--always', '--all', '--dirty');
        my $log = $repo->run(log => '-1', '--oneline', '--no-color', $file);
        $tag =~ m{(?:heads/)?(?<branch>.+)};
        my $text = read_file($file, {binmode => ':raw' });
        $bot->edit({
                page => $page,
                text => decode("UTF-8", $text),
                summary => "$+{branch}:$log",
            });
    }
} elsif( $opt->mode eq "deploy" ) {
    foreach my $file (values %pages) {
unless(defined $deploys{$file}) {
die "file not deployable";
}
$page = $deploys{$file};
say "$file -> $opt->{lang}.$opt->{family}.org/wiki/$page";
        my $tag = $repo->run(describe => '--always', '--dirty');
        my $log = $repo->run(log => '-1', '--pretty=format:%s', '--no-color');
        my $text = read_file($file, {binmode => ':raw' });
        my $ret = $bot->edit({
                page => $page,
                text => decode("UTF-8", $text),
                summary => "$tag: $log",
            });
unless($ret) {
die "Error $bot->{error}->{code}: $bot->{error}->{details}";
}
    }
}
Something went wrong with that request. Please try again.