#!/usr/local/bin/perl -w

use strict;
use MediaWiki::Bot qw(:all);
use Time::ParseDate;
use Time::Local;
use POSIX;
use LWP::Simple;

my $user = 'Hebbot';
my $pass = '*******';

# create and initialize editor

my $WPDa = MediaWiki::Bot->new();

$WPDa->set_wiki({
    protocol => 'http',
    host => 'da.wikipedia.org',
    path => 'w',
    });

$WPDa->login({
    username => $user,
    password => $pass,
    });

my $WPDa_notice = "(vha. [[:en:Wikipedia:MediaWiki::Bot|MediaWiki::Bot]] - [[Brugerdiskussion:Heb|rapporter fejl]])";

# Action

sub mytime($) {
    my $premaa = "";
    my $predag = "";

    my ($sek,$min,$time,$mdag,$maaned,$aar,$udag,$aardag,$isdst) = localtime(shift);

    $aar = $aar+1900;
    $maaned = $maaned+1;

    if ($maaned < 10) {
    
	$premaa = "0";

    }
    
    if ($mdag < 10) {
    
	$predag = "0";

    }

    return $aar."-".$premaa.$maaned."-".$predag.$mdag;
}

sub myattime($) {
    my $rawstring = shift;
    my $aar = substr($rawstring, 0, 4);
    my $maaned = substr($rawstring, 4, 2);
    my $dag = substr($rawstring, 6, 2);
    my $time = substr($rawstring, 8, 2);
    my $minut = substr($rawstring, 10, 2);
    my $sekund = substr($rawstring, 12, 2);
    $aar = $aar - 1900;
    $maaned = $maaned - 1;

    my $returntime = timelocal($sekund, $minut, $time, $dag, $maaned, $aar);
    return $returntime;
}

# action!

my $lastatraw = get("http://toolserver.org/~dungodung/cgi-bin/sysopinactivity?timestamp=&wiki=dawiki&format=csv");
if (!$lastatraw) {
    exit;
}
chomp($lastatraw);
my @lastat = split(/\n/, $lastatraw);

my @botnicks = $WPDa->get_allusers(500, "sysop");

my @parsedbots;
my $count365 = 0;
my $count180 = 0;
my $count30 = 0;
my $count14 = 0;
my $count5 = 0;
my $ccount = 0;
my $count = 0;

my $count3mdr = 0;

foreach my $botnick (@botnicks) {

    my $lastedit = $WPDa->last_active($botnick);

    my $lastepoch = parsedate($lastedit);
    
    my $nodays = floor(((time()-$lastepoch) / 86400));
    my $nodaysun = ((time()-$lastepoch) / 86400);

    if ($nodays >= 91.3125) {
    
	$count3mdr++;

    }

    if ($nodays > 365) {
    
	$count365++;
	
    } elsif (365 >= $nodays && $nodays > 180) {
    
	$count180++;
	
    } elsif (180 >= $nodays && $nodays > 30) {
    
	$count30++;
	
    } else {

	if (14 >= $nodays) {
	
	    $count14++;
	    
	}
	
	if (5 >= $nodays) {
	
	    $count5++;
	    
	}
    
	$ccount++;

    }

    $count++;

    if ($count365+$count180+$count30+$ccount != $count) {
    
	print STDERR "Count error\n";
	
    }

    push @parsedbots, $nodays . "|" . $botnick . "|" . $lastepoch;

}

my ($nusek, $numin, $nutime, $nudag,$numaaned,$nuaar,,,) = gmtime(time());
$nuaar = $nuaar + 1900;
$numaaned = $numaaned + 1;

my $prenumaaned = "";
my $prenudag = "";
my $prenutime = "";
my $prenumin = "";
my $prenusek = "";

if ($numaaned < 10) { $prenumaaned = "0"; }
if ($nudag < 10) { $prenudag = "0"; }

my $ligenu = $nuaar . "-" . $prenumaaned . $numaaned . "-" . $prenudag . $nudag . "T" . $nutime . ":" . $numin . ":" . $nusek . "[[UTC]].";

$count180 = $count180 + $count365;
$count30 = $count30 + $count180;

my $final = qq{Oversigt over tidspunkt for seneste redigeringer fra [[Speciel:Brugerliste/sysop|registrerede]] "[[WP:ADM|administratorer]]" p den danske Wikipedia pr. ''$ligenu''.

Ud af $count administratorer har
* '''$count365''' ikke foretaget redigeringer inden for de sidste '''365 d¯gn''',
* '''$count180''' ikke foretaget redigeringer inden for de sidste '''180 d¯gn''', 
* '''$count30''' ikke foretaget redigeringer inden for de sidste '''30 d¯gn''' mens
* '''$ccount''' har foretaget redigeringer inden for de sidste 30 d¯gn,
** heraf '''$count14''' inden for de sidste '''14 d¯gn''', og
** '''$count5''' inden for de sidste '''5 d¯gn'''.

\{| class="wikitable sortable" border="1"
! Brugernavn
! Sidste adminhandling<br /><small>Via [http://toolserver.org/~dungodung/cgi-bin/sysopinactivity dungodungs sysopinactivity tool]</small>
! Sidste redigering
};


@parsedbots = sort { (split(/\|/, $a))[2] <=> (split(/\|/, $b))[2] } @parsedbots;

foreach my $parsedbot (@parsedbots) {

    my $editwarnl = "";
    my $admwarnl = "";
    my $editwarnr = "";
    my $admwarnr = "";

    my ($nodays, $botnick, $lastepoch) = split(/\|/, $parsedbot);

    my $attimer = 0;

    foreach my $attime (@lastat) {
	my ($atsnick, $atstime) = split(/\t/, $attime);
	if ($atsnick eq $botnick) {
	    $attimer = myattime($atstime);
	}
    }


    my $nodyears = floor($nodays / 365.25);
    my $noddays = floor($nodays - ($nodyears*365.25));
    my $nodreturn;

    if ($nodyears > 0) {
    
	$nodreturn = $nodyears . " Âr, " . $noddays . " dage <!-- $nodays (a) -->";
	
    } else {

	if ($noddays == -1) { $noddays++; }

	$nodreturn = $noddays . " dage <!-- $nodays (b) -->";
	
    }

    my $atdays = (time() - $attimer) / 86400;

#    print STDOUT "$atdays - $attimer\n";
    
    my $atdyears = floor($atdays / 365.25);
    my $atddays = floor($atdays - ($atdyears*365.25));
    my $atdreturn;

    if ($atdyears > 0) {
    
	$atdreturn = $atdyears . " Âr, " . $atddays . " dage <!-- $attimer (c) -->";
	
    } else {

	if ($atddays == 365) {
	
	    if ((time() - $attimer) < 86400) { $atddays = 0; }
	    
	}
    
	$atdreturn = $atddays . " dage <!-- $attimer (d) -->";
	
    }

    if ((time() - $attimer) > 15778800) { $admwarnl = "<font color=\"red\">"; $admwarnr = "</font>"; }
    if ((time() - $lastepoch) > 15778800) { $editwarnl = "<font color=\"red\">"; $editwarnr = "</font>"; }

    $final .= "|-\n";
    $final .= "| " . "[[Bruger:" . $botnick . "|" . $botnick . "]]     <small>([[Brugerdiskussion:" . $botnick . "|disk]])</small>\n| " . mytime($attimer) . " (" . $admwarnl . $atdreturn . " siden" . $admwarnr . ")\n| [[Speciel:Bidrag/" . $botnick . "|" . mytime($lastepoch) . "]] (" .$editwarnl . $nodreturn . " siden" . $editwarnr . ")\n";

}

$final .= "|}\n\n";

$final .= "''Denne liste opdateres automatisk en gang i timen af [[Bruger:Hebbot|Hebbot]].\n[[Kategori:Wikipedia statistik]]\n\n";



my $includeline = "heraf har $count5 vÊret aktive inden for de sidste 5 d¯gn, $count14 inden for de sidste 14 d¯gn mens $count3mdr ikke har foretaget redigeringer inden for de sidste 3 mÂneder";

# print $final . "\n";

# $WPDa->edit({
#    page    => "Bruger:Hebbot/AdmStatus/inc",
#    text    => $includeline,
#    summary => "Hyphest",
#    is_minor=> 0,
# });
		

$WPDa->edit({
    page => "Wikipedia:Wikipedia administratorer efter sidste redigeringstidspunkt/inc",
    text => $includeline,
    summary => "Opdateret status over [[WP:ADM|administratorers]] seneste redigeringer $WPDa_notice",
    });

$WPDa->edit({
    page => "Wikipedia:Wikipedia administratorer efter sidste redigeringstidspunkt",
    text => $final,
    summary => "Opdateret status over [[WP:ADM|administratorers]] seneste redigeringer $WPDa_notice",
    });