User:ImplSearchBot/Code: Difference between revisions

Content added Content deleted
m (ImplSearchBot:0)
m (ImplSearchBot:0)
Line 4: Line 4:
use MediaWiki::Bot;
use MediaWiki::Bot;
use JSON qw/to_json from_json/;
use JSON qw/to_json from_json/;



my $usage = "Usage: $0 --username=(username) --password=(password) [--posttosite=yes]";
my $usage = "Usage: $0 --username=(username) --password=(password) [--posttosite=yes]";
Line 17: Line 16:
my $verbosity = 2; # verbosity level. 0 is silent. 1 is error only. 2 is updates. 3 is process, more is noisy.
my $verbosity = 2; # verbosity level. 0 is silent. 1 is error only. 2 is updates. 3 is process, more is noisy.
my $post; # Is this an actual run?
my $post; # Is this an actual run?
my $cachepath = "cache/";
my $result = GetOptions(
my $result = GetOptions(
"wiki=s" => \$wiki,
"wiki=s" => \$wiki,
Line 22: Line 22:
"password=s" => \$password,
"password=s" => \$password,
"verbosity=s" => \$verbosity,
"verbosity=s" => \$verbosity,
"post" => \$post);
"post" => \$post,
"cachepath=s" => \$cachepath);
$options{'wiki'} = $wiki;
$options{'wiki'} = $wiki;


Line 39: Line 40:


$options{'verbosity'} = $verbosity;
$options{'verbosity'} = $verbosity;

$cachepath .= '/'
if('/' ne substr($cachepath, -1, 1));
$options{'cachepath'} = $cachepath;
}
}


Line 45: Line 51:
my $starttime = time;
my $starttime = time;
my $pagesedited = 0;
my $pagesedited = 0;

# Tracking for svn checkin at end.
# We *should* be the only ones writing to the cache path for now.
# Eventually, we'll keep track of the vision we last ran at, and update to that.
# In the mean time, since the structure of this is a bit unstable for the moment,
# we'll do an update to HEAD just to catch anything silly I might have done in the
# mean time.
&svn('update', $options{'cachepath'});
opendir(CACHEDIR, $options{'cachepath'})
or die "Unable to open cache directory";
my @initialcache = readdir(CACHEDIR);
closedir(CACHEDIR);

&out(scalar @initialcache . " categories initially cached\n", 3);


# Handles interaction with the wiki.
# Handles interaction with the wiki.
Line 191: Line 211:


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

&out("Updating cache\n", 3);
&commitcache();


&out("Done\n", 2);
&out("Done\n", 2);
Line 260: Line 283:
unless( exists $options{'post'} )
unless( exists $options{'post'} )
{
{
# save it to disk, and out of the way.
$pagename = &sanitizenamefs($pagename);
$pagename = "test/" . &sanitizenamefs($pagename);
$pagename .= ".wikitxt";
$pagename .= ".wikitxt";


Line 298: Line 322:
my $dataname = shift;
my $dataname = shift;
my $data = shift;
my $data = shift;
my $filename = $options{'cachepath'} . &sanitizenamefs($dataname . ".json");
&out("Caching $dataname...", 3);
my $filename = &sanitizenamefs("cache_" . $dataname . ".json");
&out("Caching $dataname to $filename...", 3);
my $outfile;
my $outfile;
unless(open $outfile, '>', $filename)
unless(open $outfile, '>', $filename)
Line 315: Line 339:
{
{
my $dataname = shift;
my $dataname = shift;
&out("Getting cached data for $dataname...", 3);
my $filename = $options{'cachepath'} . &sanitizenamefs($dataname . ".json");
my $filename = &sanitizenamefs("cache_" . $dataname . ".json");
&out("Getting cached data for $dataname from $filename...", 3);
my $infile;
my $infile;
unless (open $infile, '<', $filename)
unless (open $infile, '<', $filename)
Line 388: Line 412:
return {'impl' => \%impl,
return {'impl' => \%impl,
'omit' => \%omit };
'omit' => \%omit };
}

sub commitcache
{
# First, find out if we've added any files.
my $cachepath = $options{'cachepath'};
print "Cachepath: $cachepath\n";
opendir(CACHEDIR, $cachepath);

my @current = readdir(CACHEDIR);
close(CACHEDIR);

# We need to run svn adds if we've created any new files.
# Maybe we'll use SVN::Client some day. Not right now.
my ($added, $removed) = &diffcat(\@current, \@initialcache);
&out("Detected " . scalar @$added . " new cache files and " . scalar @$removed . " removed\n",2);
if ((scalar @$added + scalar @$removed ) > 0)
{
foreach my $cachefile (@$added)
{
print "cachepath: $cachepath, cachefile: $cachefile\n";
&svn('add', $cachepath . $cachefile);;
}

foreach my $cachefile (@$removed)
{
print "cachepath: $cachepath, cachefile: $cachefile\n";
&svn('remove', $cachepath . $cachefile);;
}
}

&svn('ci', '--message="ImplSearchBot run"', $cachepath);
&svn('update', $cachepath);
}

sub svn
{
my @args = @_;

my $string = "system 'svn'";
$string .= ", '$_'"
foreach (@args);

$string .= "\n";

&out($string, 2);

system 'svn', @args
if(exists $options{'post'});
}
}
</lang>
</lang>