Home » Php » php – Mediawiki rollback bot (Mass undo troll actions!)

php – Mediawiki rollback bot (Mass undo troll actions!)

Posted by: admin July 12, 2020 Leave a comment


I want to rollback every page a certain IP address has edited, and delete any pages they’ve made.

How can I do this with either a bot or a plugin or even default functionality to do this? I’ve found the bot documentation (here) but haven’t been able to find any source codes with getting user contributions and rolling back.

Thanks for your help! This should preferably be in PHP.

How to&Answers:

We had the same problem and I ended up creating a SQL script. See here. I tried it only two times and it worked for me. Below is the link to script.


And here is the script itself.
Change database properties before to use it. First command line argument should be name or IP of the user.

echo off

echo "For all pages affected by $USER activities set  last revision to the last untouched one"
mysql $DB_NAME --user=$DB_USR --password=$DB_PWD  --execute="update page p set p.page_latest=( SELECT max(r.rev_id) FROM revision r WHERE r.rev_page=p.page_id and r.rev_user_text!='$USER' ) where p.page_id in (select distinct  r2.rev_page from revision r2 where r2.rev_user_text='$USER')"

echo "For all pages make sure that page len is set equal to revision len"
mysql $DB_NAME --user=$DB_USR --password=$DB_PWD  --execute="update page p set p.page_len=( SELECT r.rev_len FROM revision r WHERE r.rev_page=p.page_id and r.rev_id=p.page_latest )"

echo "Delete revisions done by $USER"
mysql $DB_NAME --user=$DB_USR --password=$DB_PWD --execute="delete from revision where rev_user_text='$USER'"

echo Delete $USER from the recent changes
mysql $DB_NAME --user=$DB_USR --password=$DB_PWD --execute="delete from recentchanges where rc_user_text='$USER'"

# Optional - clear cache
# mysql $DB_NAME --user=$DB_USR --password=$DB_PWD --execute="delete FROM `objectcache`"
# mysql $DB_NAME --user=$DB_USR --password=$DB_PWD --execute="delete FROM `querycache`"
# mysql $DB_NAME --user=$DB_USR --password=$DB_PWD --execute="delete FROM `querycachetwo`"

After script is run you have to hold SHIFT key when reloading page in the browser. Otherwise cached version will be rendered and you will not notice that rollback actually happened.


The Extension:Nuke is explicitly written to delete all pages created by a given user in one click.

I don’t think there’s a tool to delete or revert all contributions made by one user, but there are scripts to rollback edits with one click. See also Manual:Combating vandalism.


Based on SQL queries by Stan Sokolov, I managed to clean up an awful mess in my wiki – I haven’t monitored it for half an year and it was full of spam.
I had to clean it from specific date.

If you are going to try same, please backup first – those sql queries can destroy your wiki, kill kittens and cause pregnancy.

In this example, “tr_” is my prefix, “189” is last good page id, “41” is last good user id, and “20130215152547” is first spam entry date.

#Update page state to last good before the date
UPDATE tr_page p SET p.page_latest=( SELECT MAX(r.rev_id) FROM tr_revision r 
WHERE r.rev_page=p.page_id 
AND rev_timestamp <20130215152547) WHERE p.page_id IN 
(SELECT  r2.rev_page FROM tr_revision r2 WHERE rev_timestamp >=20130215152547);

#Update page length to match actual
UPDATE tr_page p SET p.page_len=( SELECT r.rev_len FROM tr_revision r WHERE
r.rev_page=p.page_id AND r.rev_id=p.page_latest );

#Clean up spam revisions

DELETE FROM tr_revision WHERE rev_timestamp >=20130215152547;

#Clear recent changes

DELETE FROM tr_recentchanges WHERE rc_timestamp >=20130215152547;

#Block all new bad users from ever entering wiki with this login
UPDATE tr_user SET
  user_password    = '',
  user_newpassword = '',
  user_email       = '',
  user_token       = ''
WHERE user_id>41;

#Delete pages, staring from the last good id
DELETE FROM tr_page WHERE page_id>189;

#Also, you will need TO clean TABLE tr_pagelinks.
#You will have to do it manually. It's easy, use some editor
#like PHPMyAdmin or SQLyog

#Clean cache

DELETE FROM `tr_objectcache`;
DELETE FROM `tr_querycache`;
DELETE FROM `tr_querycachetwo`;


you could defintly get pywikipediabot to do this, shouldn’t be too hard.


If you don’t want to write code for the bot, you could use the Navigation Popups, it has one click vandal revert, should be pretty fast to revert 50 edits.