User:HighInBC/source
This page is for code snippets related to improving and maintaining Wikipedia. These are not bots but standalone problems. All of these are released under the GFDL.
Rollback checker
This tool scans a user's last 500(5000 if a username/password of a bot or admin is given) contributions and returns diff urls for every usage of rollback seen.
If you have a bot or admin account you can set your username in the "login" command and create a file called "password" and put just your password in it, do not press enter after the password. This will let it scan 5000 revisions instead of 500.
- Usage
- ./rollbackChecker.pl some user name
#!/usr/bin/perl
use strict;
use LWP::UserAgent;;
use HTTP::Request;
use HTTP::Request::Common;
use XML::Simple;
# Create ua object
my $ua = LWP::UserAgent->new('agent' => 'Rollback Checker 0.02 - http://en.wikipedia.org/wiki/User:Chillum/source#Rollback_checker');
$ua->default_header('Accept-Encoding' => 'gzip, deflate');
$ua->cookie_jar({ });
my $bite = 500;
$bite = login('SomeUser'); # Only useful if your account is a bot or admin. This will do nothing unless there is a "password" file containing only your password
print "Using bite size of: $bite\n";
my $resp = api(action=>'query',list=>'usercontribs',ucprop=>'ids|comment',uclimit=>$bite,ucuser=>join(' ', @ARGV)) || die "API call failed\n";
foreach my $rh_contrib (@{${$resp}{query}{usercontribs}{item}}) {
next unless (${$rh_contrib}{comment} =~ m/\[\[Help:Reverting\|Reverted\]\] edits by .*? to last version by .*$/);
print "http://en.wikipedia.org/w/index.php?title=-&diff=prev&oldid=${$rh_contrib}{revid}\n";
}
sub login {
open(PASS,'password') || return 500; sysread(PASS, my $password, -s(PASS)); close(PASS);
my $xml = api(action=>'login',lgname=>shift,lgpassword=>$password);
print "The log in result was '${$xml}{login}{result}'. ";
return ((${$xml}{login}{result} eq 'Success') ? (5000) : (500));
}
sub api {return XMLin($ua->request(POST 'http://en.wikipedia.org/w/api.php', [@_,format=>'xml'])->decoded_content());}
CGI version:
See: http://wiki.highinbc.com/cgi-bin/rollback
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common;
use XML::Simple;
use Cache::FileCache;
use CGI qw/:standard/;
use CGI::Carp 'fatalsToBrowser';
$CGI::POST_MAX=1024; # max 1K posts
$CGI::DISABLE_UPLOADS = 1; # no uploads
my $username = param('wpusername');
my $cache = new Cache::FileCache( );
print header('text/html'),
start_html('Rollback scanner'),
h1('Rollback scanner'),
p('Enter the username of a Wikipedian and I will scan the last 500 edits for rollback usage and give you links.'),
start_form,
textfield('wpusername'),
submit,
end_form,
hr(),
$username
? ('Most recent rollbacks by user '.a({href=>'http://en.wikipedia.org/wiki/User:'.$username},$username).' '.sup(small(a({href => self_url()},'(Link to this query)'))))
: '',
br(),
br();
my $ua;
if($username) {
# Create ua object
$ua = LWP::UserAgent->new('agent' => 'Rollback Checker 0.02 - http://en.wikipedia.org/wiki/User:Chillum/source#Rollback_checker');
$ua->default_header('Accept-Encoding' => 'gzip, deflate');
my $resp = api(action=>'query',list=>'usercontribs',ucprop=>'ids|comment|title|timestamp',uclimit=>500,ucuser=>$username);
foreach my $rh_contrib (@{${$resp}{query}{usercontribs}{item}}) {
next unless (${$rh_contrib}{comment} =~ m/\[\[Help:Reverting\|Reverted\]\] edits by .*? to last version by .*$/);
my $link = "http://en.wikipedia.org/w/index.php?diff=prev&oldid=${$rh_contrib}{revid}";
my $label = "${$rh_contrib}{timestamp} - ${$rh_contrib}{title}";
${$rh_contrib}{comment} =~ s/\[\[.*?\|(.*?)\]\]/$1/g;
print a({href=>$link},$label),
b(": ${$rh_contrib}{comment}"),
br();
}
}
print end_html();
sub api {
my @params = @_;
my $apiCacheKey = "$0-lastAPICall";
my $cacheKey = join(':', @params);
my $xml = $cache->get( $cacheKey );
if ( not defined $xml ) {
my $throttle = 5; # One request every 5 seconds
my $lastRun = $cache->get( $apiCacheKey ) || 0;
if ((time() - $lastRun) < $throttle) {
my $waitFor = $throttle - (time() - $lastRun);
print h1('Too much traffic'),
p('Please '.a({href => self_url()},'try again').' in '.$waitFor.' seconds'),
end_html();
exit;
}
$cache->set( $apiCacheKey, time(), "never" );
$xml = $ua->request(POST 'http://en.wikipedia.org/w/api.php', [@_,format=>'xml'])->decoded_content();
$cache->set( $cacheKey, $xml, "5 minutes" );
}
return XMLin($xml);
}
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.