User:Brenden/revertscript

From Conservapedia
< User:Brenden
This is an old revision of this page, as edited by Brenden (talk | contribs) at 22:08, September 1, 2012. It may differ significantly from current revision.
Jump to navigation Jump to search

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.2' ) import groovyx.net.http.HTTPBuilder // created originally by the Codehaus community /**************************************************************************/ /* INPUT BY THE USER OF THE SCRIPT ****************************************/ /**************************************************************************/ names_of_vandal = [ "vandalname" , "othervandal" ] //<- replace the X's by the usernames of the vandals (an arbitrary number of user names can be specified here) name_of_admin = "adminaccount" //<- replace the string by your username my_password = "adminpassword" //<- replace the string by your password http = new HTTPBuilder("http://conservapedia.com") my_path = "/api.php" // mediawiki api. This can be changed according to the relative path of the api. /**************************************************************************/ /* END OF INPUT BY THE USER OF THE SCRIPT *********************************/ /**************************************************************************/ def page_titles_and_ids = [] //data-structure= [[name_of_vandal, page_title, page_id], ...] /**************************************************************************/ /* FUNCTION TO GET PAGEID *************************************************/ /**************************************************************************/ //get page_id: get_page_id = {page_name -> def mr; def my_keys; def pageid; mr = http.get( path:my_path, query:[ action:'query', titles:page_name, format:'json' ]); my_keys = mr.query.pages.keySet().toArray() for(i in (0..<my_keys.size()) ) { try { pageid = 1.parseInt(my_keys[i]) break; //pageid found! } catch(e) { // println "pageid not yet found at key = " + mks[i] } } return pageid } /**************************************************************************/ /* END OF FUNCTION TO GET PAGEID ******************************************/ /**************************************************************************/ /**************************************************************************/ /* FUNCTION TO REVERT VANDALISM *******************************************/ /**************************************************************************/ revert_vandalism_function = {my_page_titles_and_ids -> //first create a List with all page titles: def temp_map =[:] //a temporary variable that is only used to create the list my_page_titles. def my_page_titles; //a list of all pages that were vandalized will be stored here. This list will contain no duplicates. def first_revision; //the top revision of a page will be stored here def save_revision; //the last non-vandal revision of a page will be stored here; towards this revision -if it exists- the page will be reverted. //create the temporary map. //the page titles will be the keys, //so if a page title is added more than once, //there will still be only one key corresponding to that page title in the map. my_page_titles_and_ids.each { temp_map[""+it[1]] = 1; } //extract the list of page titles from the temporary map: my_page_titles = temp_map.keySet().toArray(); println "" //loop over all page titles: //this is the main loop of the revert_vandalism_function. for(ind2 in (0..<my_page_titles.size())) { pagetitle = my_page_titles[ind2] println pagetitle page_id = get_page_id(pagetitle); println "page_id = " + page_id //get the top revision myresult = http.get( path:my_path, query:[ action:'query', prop:'revisions', rvlimit:'1', titles:pagetitle, format:'json' ]); first_revision = myresult.query.pages[""+page_id].revisions[0] println "top revision = " println first_revision user_name = first_revision.user; println "by user: " + user_name //first check if the top revision was made by a vandal or not: if(names_of_vandal.contains(user_name)) { //here the edit was performed by a vandal: println "This page needs to be fixed (or perhaps even deleted)." println "" //loop over all older revisions until revision by non-vandal is found (and if not page must be deleted): query_continue = myresult['query-continue']; while(query_continue) { rev_id = query_continue.revisions.rvstartid println "revision id = " + rev_id; //get previous revision: myresult = http.get( path:my_path, query:[ action:'query', prop:'revisions', rvlimit:'1', rvstartid:rev_id, titles:pagetitle, format:'json' ]); temp_revision = myresult.query.pages[""+page_id].revisions[0] println "temp revision = " println temp_revision user_name = temp_revision.user; println "by user: " + user_name if(names_of_vandal.contains(user_name)) { //the revision was also by a vandal: //we must continue the while-loop //to go deeper in the revision history query_continue = myresult['query-continue'] } else { //this revision was not by a vandal: //we can exit the while-loop save_revision = myresult.query.pages[""+page_id].revisions[0] //the revision towards which the page will be restored println save_revision break; //exit the while-loop } } //the while loop has ended, //we now find the reason why: if(query_continue) { //so the while-loop has ended because of an exisiting revision by a non-vandal: //We can revert to that revision. //get token for edit: myresult = http.get( path:my_path, query:[ action:'query', prop:'info', intoken:'edit', titles:pagetitle, format:'json' ]); println myresult edittoken = myresult.query.pages[""+page_id].edittoken println edittoken first_rev_id = first_revision.revid second_rev_id = save_revision.revid println first_rev_id println second_rev_id //undo the page to the revision that was saved myresult = http.post( path:my_path, query:[ action:'edit', title:pagetitle, undo:first_rev_id, undoafter:second_rev_id, token:edittoken, summary:"automatic restoration to the last revision by a non-vandal", format:'json' ]); println myresult } else { //the while-loop ended because of running out of revisions, //and these were all made by vandals. //So we can delete the page println "page must be deleted" //get the deletion token: myresult = http.get( path:my_path, query:[ action:'query', prop:'info', intoken:'delete', titles:pagetitle, format:'json' ]); println myresult mydeletetoken = myresult.query.pages[""+page_id].deletetoken println "deletion token is: " + mydeletetoken //perform the deletion: myresult = http.post( path:my_path, query:[ action:'delete', title:pagetitle, token:mydeletetoken, reason:'automatic deletion', format:'json' ]); println myresult } } else { //the top edit was by a non-vandal. //Nothing needs to be done println "This page has already been fixed." println "" } println "" } } /**************************************************************************/ /* END OF FUNCTION TO REVERT VANDALISM ************************************/ /**************************************************************************/ /**************************************************************************/ /* LOGIN PROCEDURE ********************************************************/ /**************************************************************************/ result = http.post( path:my_path, query:[ action:'login', lgname:name_of_admin, lgpassword:my_password, format:'json' ]); println result token = result.login.token println token result = http.post( path:my_path, query:[ action:'login', lgname:name_of_admin, lgpassword:my_password, format:'json', lgtoken:token ]); println result /**************************************************************************/ /* END OF LOGIN PROCEDURE *************************************************/ /**************************************************************************/ /**************************************************************************/ /* BLOCKING THE VANDALS ***************************************************/ /**************************************************************************/ for(vandal_nr in (0..<names_of_vandal.size())) { result = http.post( path:my_path, query:[ action:'block', user:names_of_vandal[vandal_nr], reason:'automatic block. Incorrect - report it', nocreate:"", autoblock:"", noemail:"", reblock:"", format:'json' ]); println result } /**************************************************************************/ /* END OF BLOCKING THE VANDALS ********************************************/ /**************************************************************************/ /**************************************************************************/ /* HERE A LIST OF VANDAL CONTRIBUTIONS IS OBTAINED ************************/ /**************************************************************************/ for(vandal_nr in (0..<names_of_vandal.size())) { result = http.get( path:my_path, query:[ action:'query', list:'usercontribs', ucuser:names_of_vandal[vandal_nr], format:'json' ]); result.query.usercontribs.each { page_titles_and_ids.add([names_of_vandal[vandal_nr], it.title, it.pageid]) // processing_function(it.pageid, it.title) } last_time_stamp = result.query.usercontribs[-1].timestamp; println last_time_stamp more_pages = true while(more_pages) { println "get more pages:" result = http.get( path:my_path, query:[ action:'query', list:'usercontribs', ucuser:names_of_vandal[vandal_nr], format:'json', ucstart:last_time_stamp ]); for(i in (1..<result.query.usercontribs.size())) { page_titles_and_ids.add([names_of_vandal[vandal_nr], result.query.usercontribs[i].title, result.query.usercontribs[i].pageid]) //processing_function(result.query.usercontribs[i].pageid, result.query.usercontribs[i].title) } new_last_time_stamp = result.query.usercontribs[-1].timestamp; if(new_last_time_stamp == last_time_stamp) { more_pages = false } else { more_pages = true last_time_stamp = new_last_time_stamp } } } println page_titles_and_ids /**************************************************************************/ /* END OF THE OBTAINING OF A LIST OF VANDAL CONTRIBUTIONS IS OBTAINED *****/ /**************************************************************************/ //call the reverting function: revert_vandalism_function(page_titles_and_ids) /**************************************************************************/ /* LOGOUT PROCEDURE *******************************************************/ /**************************************************************************/ println http.get( path:my_path, query:[ action:'logout', format:'json' ]); /**************************************************************************/ /* END OF LOGOUT PROCEDURE ************************************************/ /**************************************************************************/ println "end of script"