User:ImplSearchBot/Code: Difference between revisions

From Rosetta Code
Content added Content deleted
m (ImplSearchBot:0)
Line 1: Line 1:
<lang perl>#!/usr/bin/perl
<lang perl>#!/usr/bin/perl -w

use strict;
use strict;
use MediaWiki::Bot;
use MediaWiki::Bot;
use Data::Dumper;


# Handles interaction with the wiki.
my $editor = MediaWiki::Bot->new("ImplSearchBot/1.0.0");
# Note that I had to modify HTTP::Message to make it work
# HTTP::Message silently failed when presented by MW
# with an encoding type of "application/json" or some such.


my $usage = "Usage: $0 (username) (password) [posttosite]";
print "Created editor...trying to set wiki.\n";


my $username = shift @ARGV;
$editor->set_wiki('rosettacode.org','w');
my $password = shift @ARGV;


defined $username
print "Set Wiki...trying to log in.\n";
or die $usage;


defined $password
unless("Success" == $editor->login($username,$password))
or die $usage;

my $posttosite = shift @ARGV;

print "Creating editor\n";
my $editor = MediaWiki::Bot->new('ImpleSearchBot');
$editor->{debug} = 1;

sub postpage
{
{
my $pagename = shift;
die "Unable to login: " . $editor->errstr;
my $pagedata = shift;
}
my $remark = shift;
my $minoredit = shift;


unless( defined $posttosite )
print "Logged in...getting tasks\n";
{
$pagename =~ tr/:\//_-/;
$pagename .= ".wikitxt";


print "Saving: $pagename\n";
my @alltasks = $editor->get_pages_in_category('Category:Programming Tasks');


open my $outfile, '>', $pagename
print "Got all the tasks...getting the languages.\n";
or warn "Failed to open $pagename: $!";
my @alllanguages = $editor->get_pages_in_category('Category:Programming Languages');


return unless defined $outfile;
foreach my $language (@alllanguages)

{
print $outfile $pagedata;
$language =~ s/Category://;
close $outfile;
}
else
{
print "Posting $pagename\n";
$editor->edit($pagename, $pagedata, "ImplSearchBot:$remark", $minoredit)
or warn "Failed to post page: " . $editor->{'errstr'};
}
}
}


# Tell the editor to edit Rosetta Code. I'm sure Wikipedia didn't like
print "Filling langtaskmap\n";
# my initial attempts from before I added this line.
my %langtaskmap;
print "Trying to set wiki.\n";
$editor->set_wiki('rosettacode.org','w');


# Attempt to log in.
foreach my $language (@alllanguages)
print "Trying to log in.\n";
unless("Success" == $editor->login($username, $password))
{
{
# No, it's not the "(expr) or die" syntax. This will be clearer
my %lang = map {$_, 1} $editor->get_pages_in_category("Category:$language");
# for most folks who read the code.
$langtaskmap{$language} = \%lang;
die "Unable to login: " . Dumper($editor);
}
}


# Get a complete listing of the tasks.
print "Beginning edit cycle\n";
print "Getting tasks\n";
my @alltasks = $editor->get_pages_in_category('Category:Programming Tasks');

# Get a complete listing of the languages.
print "Getting the languages.\n";
my @alllanguages = $editor->get_pages_in_category('Category:Programming Languages');

# We want the language name, not the fully-qualified wiki name.
$_ =~ s/^Category:// foreach (@alllanguages);

print "Identifying implemented and omitted languages\n";
foreach my $language (@alllanguages)
foreach my $language (@alllanguages)
{
{
my %implemented = map {$_, 1} $editor->get_pages_in_category("Category:$language");
my %omitted = map {$_, 1} $editor->get_pages_in_category("Category:$language/Omit");
my $omitcount = scalar keys %omitted;

my $pagename = "Tasks not implemented in $language";
my $pagename = "Tasks not implemented in $language";
print "Preparing data for:$pagename\n";
print "Preparing data for:$pagename\n";

my $pagedata = "{{unimpl_header|$language}}\n";
# Language metadata
my $taskcount = scalar @alltasks;
my $unimpcount = $taskcount - scalar keys %implemented;
my $targetcount = ($taskcount - $omitcount);

# Language-specific page data.
my $unimplisting = "";
my $omitlisting = "";
my $pagedata; # Not assembled until the end.

foreach my $taskname (@alltasks)
foreach my $taskname (@alltasks)
{
{
# We want the task name, not the fully-qualified wiki name.
unless(exists $langtaskmap{$language}->{$taskname})
my $baretaskname = $taskname;
{
$pagedata .= "* [[$taskname]]\n";
$baretaskname =~ s/^Category://;

}
# Add the task to the unimplemented list, if it's unimplemented.
$unimplisting .= "* [[$baretaskname]]\n"
unless(exists $implemented{$taskname});

# Add the task to the omission list, if it's omitted.
$omitlisting .= "* [[$baretaskname]]\n"
if(exists $omitted{$taskname})
}
}


# Prepare template fields
print "Editing:$pagename\n";
my $langfield = "|$language";
$editor->edit($pagename, $pagedata, "Bot:Updating list of unimplemented tasks.", 1);
my $unimpfield = "|$unimpcount";
}</lang>
my $tcfield = "|$targetcount";

my $impperccalc = 0;
$impperccalc = (($targetcount - $unimpcount) / $targetcount) * 100
unless ($targetcount == 0);
my $imppercfield = sprintf "|%u", $impperccalc;

my $unimpltemplatename = "unimp_body_$language";
my $omittemplatename = "unimp_omit_body_$language";

# Prepare the listing page format.
$pagedata = '{{unimpl_header' . $langfield . $unimpfield . $tcfield . $imppercfield . '}}';
$pagedata .= "{{$unimpltemplatename" . "$unimpfield}}";
$pagedata .= "{{omit_header" . "$langfield}}";
$pagedata .= "{{$omittemplatename}}";
$pagedata .= "{{unimpl_footer$langfield}}";

# Post the template containing the listing of unimplemented tasks.
&postpage("Template:$unimpltemplatename", "<includeonly>$unimplisting</includeonly>", "ImplSearchBot:Updating list body of unimplemented tasks.", 1);

# Post the template containing the listing of mitted tasks.
&postpage("Template:$omittemplatename", "<includeonly>$omitlisting</includeonly>", "ImplSearchBot:Updating list body of unimplemented tasks.", 1);

# Update the layout of the listing page, because it's changed.
&postpage($pagename, $pagedata, "ImplSearchBot:Updating layout of listing page.",1);

}

print "Updating bot code page\n";

open my $sourcefile, '<', $0
or die "Finished without updating bot source page";

my $botsource;
$botsource .= $_ while <$sourcefile>;

close $sourcefile;

&postpage("User:ImplSearchBot/Code", "<lang perl>$botsource</lang>", 0);

print "Done\n";
</lang>

Revision as of 08:07, 19 February 2009

<lang perl>#!/usr/bin/perl -w

use strict; use MediaWiki::Bot; use Data::Dumper;

  1. Handles interaction with the wiki.
  2. Note that I had to modify HTTP::Message to make it work
  3. HTTP::Message silently failed when presented by MW
  4. with an encoding type of "application/json" or some such.

my $usage = "Usage: $0 (username) (password) [posttosite]";

my $username = shift @ARGV; my $password = shift @ARGV;

defined $username

 or die $usage;

defined $password

 or die $usage;

my $posttosite = shift @ARGV;

print "Creating editor\n"; my $editor = MediaWiki::Bot->new('ImpleSearchBot'); $editor->{debug} = 1;

sub postpage {

 my $pagename = shift;
 my $pagedata = shift;
 my $remark = shift;
 my $minoredit = shift;
 unless( defined $posttosite )
 {
   $pagename =~ tr/:\//_-/;
   $pagename .= ".wikitxt";
   print "Saving: $pagename\n";
   open my $outfile, '>', $pagename
     or warn "Failed to open $pagename: $!";
   return unless defined $outfile;
   print $outfile $pagedata;
   close $outfile;
 }
 else
 {
   print "Posting $pagename\n";
   $editor->edit($pagename, $pagedata, "ImplSearchBot:$remark", $minoredit)
     or warn "Failed to post page: " . $editor->{'errstr'};
 }

}

  1. Tell the editor to edit Rosetta Code. I'm sure Wikipedia didn't like
  2. my initial attempts from before I added this line.

print "Trying to set wiki.\n"; $editor->set_wiki('rosettacode.org','w');

  1. Attempt to log in.

print "Trying to log in.\n"; unless("Success" == $editor->login($username, $password)) {

 # No, it's not the "(expr) or die" syntax.  This will be clearer
 # for most folks who read the code.
 die "Unable to login: " . Dumper($editor);

}

  1. Get a complete listing of the tasks.

print "Getting tasks\n"; my @alltasks = $editor->get_pages_in_category('Category:Programming Tasks');

  1. Get a complete listing of the languages.

print "Getting the languages.\n"; my @alllanguages = $editor->get_pages_in_category('Category:Programming Languages');

  1. We want the language name, not the fully-qualified wiki name.

$_ =~ s/^Category:// foreach (@alllanguages);

print "Identifying implemented and omitted languages\n"; foreach my $language (@alllanguages) {

 my %implemented = map {$_, 1} $editor->get_pages_in_category("Category:$language");
 my %omitted = map {$_, 1} $editor->get_pages_in_category("Category:$language/Omit");
 my $omitcount = scalar keys %omitted;
 my $pagename = "Tasks not implemented in $language";
 print "Preparing data for:$pagename\n";
 # Language metadata
 my $taskcount = scalar @alltasks;
 my $unimpcount =  $taskcount - scalar keys %implemented;
 my $targetcount = ($taskcount - $omitcount);
 # Language-specific page data.
 my $unimplisting = "";
 my $omitlisting = "";
 my $pagedata; # Not assembled until the end.
 foreach my $taskname (@alltasks)
 {
   # We want the task name, not the fully-qualified wiki name.
   my $baretaskname = $taskname;
   $baretaskname =~ s/^Category://;
   # Add the task to the unimplemented list, if it's unimplemented.
   $unimplisting .= "* $baretaskname\n"
      unless(exists $implemented{$taskname});
   # Add the task to the omission list, if it's omitted.
   $omitlisting .= "* $baretaskname\n"
     if(exists $omitted{$taskname})
 }
 # Prepare template fields
 my $langfield = "|$language";
 my $unimpfield = "|$unimpcount";
 my $tcfield = "|$targetcount";
 my $impperccalc = 0;
 $impperccalc = (($targetcount - $unimpcount) / $targetcount) * 100
   unless ($targetcount == 0);
 
 my $imppercfield = sprintf "|%u", $impperccalc;
 my $unimpltemplatename = "unimp_body_$language";
 my $omittemplatename = "unimp_omit_body_$language";
 # Prepare the listing page format.
 $pagedata = 'Template:Unimpl header' . $langfield . $unimpfield . $tcfield . $imppercfield . '';
 $pagedata .= "Template:$unimpltemplatename" . "$unimpfield";
 $pagedata .= "Template:Omit header" . "$langfield";
 $pagedata .= "Template:$omittemplatename";
 $pagedata .= "Template:Unimpl footer$langfield";
 # Post the template containing the listing of unimplemented tasks.
 &postpage("Template:$unimpltemplatename", "", "ImplSearchBot:Updating list body of unimplemented tasks.", 1);
 # Post the template containing the listing of mitted tasks.
 &postpage("Template:$omittemplatename", "", "ImplSearchBot:Updating list body of unimplemented tasks.", 1);
 # Update the layout of the listing page, because it's changed.
 &postpage($pagename, $pagedata, "ImplSearchBot:Updating layout of listing page.",1);

}

print "Updating bot code page\n";

open my $sourcefile, '<', $0

 or die "Finished without updating bot source page";

my $botsource; $botsource .= $_ while <$sourcefile>;

close $sourcefile;

&postpage("User:ImplSearchBot/Code", "<lang perl>$botsource</lang>", 0);

print "Done\n"; </lang>