User:MiszaBot/Source/archivePage.pl

archivePage.pl
This program archives the first section of a talk page to a specified archive if it is old enough. I didn't make any config files for it, so just replace whatever text applies.


 * 1) !/usr/bin/env perl

use strict; use warnings; use Encode; use Time::Local; use Wiki;

my $targetpage = "User talk:Example"; #TODO: NAME OF THE PAGE TO ARCHIVE HERE

my %months = qw; $months{January} = 1; $months{February} = 2; $months{March} = 3; $months{April} = 4; $months{May} = 5; $months{June} = 6; $months{July} = 7; $months{August} = 8; $months{September} = 9; $months{October} = 10; $months{November} = 11; $months{December} = 12;

my $wiki = Wiki->new; $wiki->setPrefix('http://en.wikipedia.org/w/index.php'); $wiki->userLogin("YourBot","youllneverguess"); #TODO: YOUR LOGIN INFO HERE my $ARCHIVETRESHOLD = 7; #TODO: HOW MANY DAYS SHOULD A THREAD HAVE TO QUALIFY FOR ARCHIVING? $wiki->editPage($targetpage,1); my $content = $wiki->{form}->value("wpTextbox1");

my ($DAY,$MONTH,$YEAR,$DATE,$cDATE,$DAYSAGO);
 * 09:38, 9 March 2006 (UTC)

if ($content =~ /(\d\d):(\d\d), (\d\d?) (.*?) (\d\d\d\d) \(UTC\)/) { #print "Date: $1:$2, $3 $4 $5 (UTC)\n"; $DAY = $3; $MONTH = $months{$4}; $YEAR = $5; $DATE = timegm(0,$2,$1,$3,$months{$4}-1,$5-1900); $cDATE = timegm(gmtime); $DAYSAGO = int(($cDATE-$DATE)/3600/24); } else { #$DAYSAGO = 1000; die "We shouldn't get here!"; #TODO: Actually, we'll get here if there are no timestamps. }

if ($DAYSAGO > $ARCHIVETRESHOLD) { my $title = "MISZABOT:TITLE ERROR"; if ($content =~ /== *(.*?) *==/) { $title = $1; }   print "Thread titled \"$title\" started more than $ARCHIVETRESHOLD days ago.\n"; my $archive = $targetpage."/Archives/$YEAR/".sprintf("%02d",$MONTH); if ($content =~ //) { my $directive = $1; if ($directive =~ /ARCHIVETO:(.*)/) { $archive = $1; }       if ($directive =~ /NOARCHIVE/) { $archive = ""; }       $content =~ s///; }   print "Removing section...\n"; $content =~ s/==(.*)==\n*//; #print $content; $wiki->{form}->value("wpTextbox1",""); my $summary = $wiki->{form}->value("wpSummary"); if ($archive) { $wiki->{form}->value("wpSummary","$summary. Archiving section to $archive."); print "Archiving section to $archive.\n"; } else { $wiki->{form}->value("wpSummary","$summary Removing section without archiving."); print "Removing section without archiving.\n"; }   $wiki->submitEdit; if ($archive) { $wiki->editPage($archive); $wiki->{form}->value("wpSummary","/* $title */ Archiving"); my $oldarchive = $wiki->{form}->value("wpTextbox1"); $wiki->{form}->value("wpTextbox1",encode_utf8($oldarchive."\n== $title ==\n\n$content\n")); $wiki->submitEdit; } }