User:Lowercase sigmabot II/Source.pl
Appearance
Source as of 02:42, 10 October 2012 (UTC).
Sandbot1.pl
[edit]This task runs every minute. It checks if the following sandboxes have the {{Sandbox heading}} template;
- Wikipedia:Sandbox
- Wikipedia:Tutorial/Editing/sandbox
- Wikipedia:Tutorial/Formatting/sandbox
- Wikipedia:Tutorial/Wikipedia links/sandbox
- Wikipedia:Tutorial/Citing sources/sandbox
- Wikipedia:Tutorial/Keep in mind/sandbox
If the template is not present on the sandbox, it prepends {{Sandbox heading}} <!-- Please leave this line alone! -->
to the page.
If the template is present, it does nothing.
#!/usr/bin/perl
use strict;
use warnings;
use MediaWiki::API;
$\ = "\n";
my $bot = MediaWiki::API->new({api_url => 'http://en.wikipedia.org/w/api.php'});
my $reinsert = "{{Sandbox heading}} <!-- Please leave this line alone! -->\n\n";
my @sandboxes = ("Wikipedia:Sandbox", "Wikipedia:Tutorial/Editing/sandbox", "Wikipedia:Tutorial/Formatting/sandbox",
"Wikipedia:Tutorial/Wikipedia links/sandbox", "Wikipedia:Tutorial/Citing sources/sandbox", "Wikipedia:Tutorial/Keep in mind/sandbox");
my @templates = ("Template:Please leave this line alone (sandbox talk heading)", "Template:Please leave this line alone (Sandbox heading)",
"Template:Sandbox heading/noedit", "Template:Please leave this line alone (sandbox talk heading)/noedit",
"Template:Sandbox header (do not remove)", "Template:PLTLA (SH)",
"Template:Please leave this line alone (sandbox heading)", "Template:Please leave this line alone (sandbox heading)/noedit");
sub shut_off_check {
my $page = shift;
my $text = $bot->get_page({title=> $page})->{"*"};
if (lc($text) eq "true") {
return 1;
}
else {
return 0;
}
}
sub revision_user {
my $page = shift;
my %query = (action=> "query", prop=> "revisions", titles=> $page, rvprop=> "user");
my $res = $bot->api(\%query)->{"query"}{"pages"};
my $pageid = [keys(%$res)]->[0];
return $res->{$pageid}{"revisions"}[0]{"user"};
}
sub templates_on_page {
my @return_this = ();
my $page = shift;
my %query = (action=> "query", prop=> "templates", tllimit=> 500, tlnamespace=> 10, titles=> $page);
my %res = $bot->api(\%query)->{"query"}{"pages"};
my $pageid = [keys %res]->[0];
# You didn't see this...
my %params = map {$_=> 1} keys(%{$res{$pageid}});
unless (exists($params{"templates"})) {
foreach my $x (0..10) {
push @return_this, $x;
}
}
else {
foreach my $template ($res{$pageid}{"templates"}) {
push @return_this, $template->{"title"};
}
}
return @return_this;
}
sub heading_is_gone {
my $box = shift;
my @templates_on_sandbox = templates_on_page($box);
foreach my $template (@templates_on_sandbox) {
if ($template eq "Template:Sandbox heading") {
return 0;
}
}
return 1;
}
sub run {
foreach my $sandbox (@sandboxes) {
if (revision_user($sandbox) eq "Lowercase sigmabot II") {
print "Bot just edited $sandbox";
next;
}
elsif (heading_is_gone($sandbox)) {
print $bot->edit({action=> "edit", title=> $sandbox, prependtext=> $reinsert, summary=> "Reinserting sandbox header"});
print "$sandbox had a heading reinserted";
}
else {
print "Something exploded!";
}
}
}
my $user = "Lowercase sigmabot II";
my $password = "password";
if (shut_off_check("User:Lowercase sigmabot II/Shutoff") eq 0) {
die "!!! Emergency shut off";
}
$bot->login({lgname=> $user, lgpassword=> $password});
run();
Sandbot2.pl
[edit]This task runs once per hour. It clears the sandboxes by replacing the content with the sandbox header, which looks like this:
{{Please leave this line alone (sandbox heading)}}<!-- * Welcome to the sandbox! * * Please leave this part alone * * The page is cleared regularly * * Feel free to try your editing skills below * ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-->
#!/usr/bin/perl
use warnings;
use utf8;
use Time::Piece::ISO;
use Time::Seconds;
use MediaWiki::API;
die "This Perl is not threadsafe.\n" if not eval 'use threads; 1';
$| = 1;
$\ = "\n";
$bot = MediaWiki::API->new({api_url => 'http://en.wikipedia.org/w/api.php'});
my $reset_text = <<EOF;
{{Please leave this line alone (sandbox heading)}}<!--
* Welcome to the sandbox! *
* Please leave this part alone *
* The page is cleared regularly *
* Feel free to try your editing skills below *
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-->
EOF
my @sandboxes = ("Wikipedia:Sandbox",
"Wikipedia:Tutorial/Editing/sandbox",
"Wikipedia:Tutorial/Formatting/sandbox",
"Wikipedia:Tutorial/Wikipedia links/sandbox",
"Wikipedia:Tutorial/Citing sources/sandbox",
"Wikipedia:Tutorial/Keep in mind/sandbox"
);
my @children;
sub shut_off_check {
my $page = shift;
my $text = $bot->get_page({title=> $page})->{"*"};
if (lc($text) eq "true") {
return 1;
}
else {
return 0;
}
}
sub revision_user {
my $page = shift;
my %query = (action=> "query", prop=> "revisions", titles=> $page, rvprop=> "user");
my $res = $bot->api(\%query)->{"query"}{"pages"};
my $pageid = [keys %$res]->[0];
return $res->{$pageid}{"revisions"}[0]{"user"};
}
sub box_wait {
my $box = shift;
foreach (1..3) {
print "3 minute sleep on $box";
sleep 60 * 3;
last if &main::box_needs_reset($box);
}
print "Done with sleeping, will clear $box now.";
$main::bot->edit({action=> "edit", title=> $box, text=> $reset_text, summary=> "Clearing sandbox) (bot"});
}
sub parse_date {
my $date = substr(shift, 0, -1); # Cutting of the 'Z' at the end
return Time::Piece::ISO->strptime($date);
}
sub box_needs_reset {
my $box = shift;
my $now = gmtime;
my $three_min = Time::Seconds->new(60 * 3);
my $res = $bot->api({action=> "query", prop=> "revisions", titles=> $box, rvprop=> "timestamp", limit=> "2"});
my $str_stamp = [values $res->{"query"}->{"pages"}]->[0]->{"revisions"}->[0]->{"timestamp"};
my $stamp = parse_date $str_stamp;
my $the_past = $now - $three_min;
#print "$stamp || $the_past";
if ($stamp < $the_past) {
return 1;
}
else {
return 0;
}
}
sub run {
foreach my $sandbox (@sandboxes) {
if (revision_user($sandbox) eq "Lowercase sigmabot II") {
print "The bot just edited the sandbox.";
next;
}
if (box_needs_reset $sandbox) {
print "Clearing $sandbox";
$bot->edit({action=> "edit", title=> $sandbox, text=> $reset_text, summary=> "Clearing sandbox) (bot"});
}
else {
push @children, threads->create(\&box_wait, ($sandbox));
}
}
$_->join() foreach (@children);
}
my $user = "Lowercase sigmabot II";
my $password = "password";
if (shut_off_check("User:Lowercase sigmabot II/Shutoff") eq 0) {
die "!!! Emergency shut off";
}
$bot->login({lgname=> $user, lgpassword=> $password});
run();