Monchoman45

FixRedlinks

Aug 12th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $('#WikiHeader').append('<a class="wikia-button" onclick="this.innerHTML = \'Processing...\'; FixRedlinks();">Fix redlinks</a>');
  2.  
  3. function FixRedlinks() {
  4.     $.getJSON('/api.php?action=query&prop=revisions&titles=' + wgPageName + '&rvprop=content&indexpageids=1&format=json', function(result) {
  5.         var text = result.query.pages[result.query.pageids[0]].revisions[0]['*'];
  6.         var redlinks = $('a.new');
  7.  
  8.         var ref = 0;
  9.         while(text.indexOf('[[', ref) != -1) {
  10.             if(text.indexOf(']]', text.indexOf('[[', ref)) != -1) {
  11.                 var open = text.indexOf('[[', ref);
  12.                 var pipe = text.indexOf('|', open);
  13.                 var close = text.indexOf(']]', open);
  14.                 if(text.indexOf('<br/>', open) != -1 && text.indexOf('<br/>', open) < close) {ref = open + 1; continue;}
  15.                 if(pipe != -1 && pipe < close) { //is [[page|display]]
  16.                     if(pipe == close - 1) {
  17.                         var title = text.substring(open + 2, pipe);
  18.                         var display = title.substring(title.indexOf(':') + 1);
  19.                     }
  20.                     else {
  21.                         var title = text.substring(open + 2, pipe);
  22.                         var display = text.substring(pipe + 1, close);
  23.                     }
  24.                 }
  25.                 else { //is [[page]]
  26.                     var title = text.substring(open + 2, close);
  27.                     var display = title;
  28.                 }
  29.                 if(!title) {ref = open + 1; continue;} //skip [[]] and [[|<anything>]]
  30.        
  31.                 for(var i = 0; i < redlinks.length; i++) {
  32.                     var utitle = title[0].toUpperCase() + title.substring(1);
  33.                     if(redlinks[i].title.indexOf(' (page does not exist)')) {var redlink = redlinks[i].title.substring(0, redlinks[i].title.length - 22);}
  34.                     else {var redlink = redlinks[i];}
  35.                     if(utitle == redlink || utitle == ':' + redlink) {
  36.                         if(pipe == close - 1) {var show = title;}
  37.                         else {var show = display;}
  38.                         text = text.substring(0, open) + show + text.substring(close + 2);
  39.                         break;
  40.                     }
  41.                 }
  42.                 ref = open + 2;
  43.             }
  44.             else {break;}
  45.         }
  46.  
  47.         $.getJSON('/api.php?action=query&prop=info&titles=' + wgPageName + '&intoken=edit&indexpageids=1&format=json', function(result) {
  48.             var token = result.query.pages[result.query.pageids[0]].edittoken;
  49.             $.post('/api.php', {action: 'edit', title: wgPageName, text: text, summary: 'Fixing redlinks', token: token}, function() {window.location.reload();});
  50.         });
  51.     });
  52. }
Advertisement
Add Comment
Please, Sign In to add comment