User:ImplSearchBot/Code

From Rosetta Code
Revision as of 08:07, 19 February 2009 by rosettacode>ImplSearchBot (ImplSearchBot:0)

<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>