Advertisement
RenzXVI

Rollback Tool

Aug 10th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Rollback Tool
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  Rollback Tool
  6. // @author       http://dev.wikia.com/wiki/Rollback
  7. // @match        wikia.com
  8. // @grant        none
  9. // @include      
  10. // ==/UserScript==
  11. /* jshint -W097 */
  12. 'use strict';
  13.  
  14. // Your code here...
  15. /*
  16. * Rollback
  17. * Perform rollbacks without needing to be in the usergroup
  18. * @author Ozuzanna
  19. */
  20.  
  21. ;(function($, mw) {
  22.  
  23. var main = {
  24.     init: function() {
  25.         if ($('.mw-rollback-link a').length) return;
  26.        
  27.         if (mw.config.get('wgAction') == "history" && $('#pagehistory li').length > 1)
  28.             $('#pagehistory li:first .mw-history-undo a').before('<span class="mw-custom-rollback-link"><a style="cursor: pointer" title="&quot;Rollback&quot; reverts edit(s) to this page of the last contributor in one click" data-id="' + mw.config.get('wgPageName') + '">rollback</a></span> | ');
  29.         else if (mw.config.get('wgCanonicalSpecialPageName') == "Contributions") {
  30.             $('#mw-content-text ul').find('li').each(function() {
  31.                 $(this).append(' <span class="mw-custom-rollback-link">[<a style="cursor: pointer" title="&quot;Rollback&quot; reverts edit(s) to this page of the last contributor in one click" data-id="' + $(this).find('a:first').attr('title') + '">rollback</a>]</span>');
  32.             });
  33.         }
  34.         else if ($.getUrlVar('diff') || $.getUrlVar('oldid'))
  35.             $('.mw-usertoollinks:last').after('&nbsp;&nbsp;&nbsp;&nbsp;<span class="mw-custom-rollback-link">[<a style="cursor: pointer" title="&quot;Rollback&quot; reverts edit(s) to this page of the last contributor in one click" data-id="' + mw.config.get('wgPageName') + '">rollback</a>]</span>');
  36.        
  37.         $('.mw-custom-rollback-link a').click(function() {
  38.             main.getRevisionIdAndContent($(this).attr('data-id'));
  39.         });
  40.     },
  41.     getRevisionIdAndContent: function(title) {
  42.         var API = new mw.Api();
  43.         API.get({
  44.         action: 'query',
  45.         prop: 'revisions',
  46.         titles: title,
  47.         rvprop: 'user|ids',
  48.         rvlimit: 500,
  49.         cb: new Date().getTime()
  50.         })
  51.         .done(function(d) {
  52.             if (!d.error) {
  53.                 var revisions;
  54.                
  55.                 for (var i in d.query.pages) {
  56.                     revisions = d.query.pages[i].revisions;
  57.                 }
  58.                
  59.                 var currentUser = revisions[0].user, //current user rollbacking from
  60.                 lastUser,
  61.                 revId;
  62.                
  63.                 for (var i in revisions) {
  64.                     if (revisions[i].user != currentUser) {
  65.                         lastUser = revisions[i].user; //remember last author
  66.                         revId = revisions[i].revid; //get revision to revert to
  67.                         break;
  68.                     }
  69.                 }
  70.                
  71.                 if (lastUser) {
  72.                     API.get({
  73.                     action: 'query',
  74.                     prop: 'revisions',
  75.                     rvprop: 'content',
  76.                     revids: revId,
  77.                     cb: new Date().getTime()
  78.                     })
  79.                     .done(function(d) {
  80.                         if (!d.error) {
  81.                             var content = ""; //can be no content on page so initialise empty as failsafe
  82.                             for (var i in d.query.pages) {
  83.                                 if (d.query.pages[i].revisions)
  84.                                     content = d.query.pages[i].revisions[0]["*"];
  85.                             }
  86.                             main.performRollback(title,content,currentUser,lastUser);
  87.                         }
  88.                         else
  89.                             new BannerNotification('Unable to rollback (failed to get page content): ' + d.error.code,'error').show();
  90.                     })
  91.                     .fail(function() {
  92.                         new BannerNotification('Unable to rollback: failed to get page content!','error').show();              
  93.                     });
  94.                 }
  95.                 else
  96.                     new BannerNotification('Unable to rollback: no different editor found!','error').show();
  97.             }
  98.             else
  99.                 new BannerNotification('Unable to rollback (failed to get revisions): ' + d.error.code,'error').show();
  100.         })
  101.         .fail(function() {
  102.             new BannerNotification('Unable to rollback: failed to get revisions!','error').show();
  103.         });        
  104.     },
  105.     performRollback: function(page,text,user,user2) {
  106.         var API = new mw.Api();
  107.         API.post({
  108.         action: 'edit',
  109.         title: page,
  110.         text: text,
  111.         summary: 'Reverted edits by [[Special:Contributions/' + user + '|' + user + ']] ([[User_talk:' + user + '|talk]] | [[Special:Block/' + user + '|block]]) to last version by [[User:' + user2 + '|' + user2 + ']] ([[w:c:dev:Rollback|script]])',
  112.         token: mw.user.tokens.values.editToken
  113.         })
  114.         .done(function(d) {
  115.             if (!d.error) {
  116.                 new BannerNotification('Rollback successful!','confirm').show();               
  117.             }
  118.             else
  119.                 new BannerNotification('Unable to rollback (failed to publish edit): ' + d.error.code,'error').show();
  120.         })
  121.         .fail(function() {
  122.             new BannerNotification('Unable to rollback: failed to publish edit!','error').show();
  123.         });
  124.     }
  125. };
  126.  
  127. main.init();
  128.    
  129. }) (this.jQuery, this.mediaWiki);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement