User:Billinghurst/MediaWiki:Gadget-SBHandler.js

// ' + ' Blacklist  ' + ' Log  '; SBlist = document.getElementById("SBlist"); SBlog = document.getElementById("SBlog"); SBHandler.getRequest('action=query&prop=info&intoken=edit&titles=MediaWiki:Spam_blacklist', SBHandler.getBL, true); } else { content.innerHTML += 'This tool can only be used in conjunction with MediaWiki_talk:Spam-blacklist or COIBot\'s spam reports to add or remove domains. '               }            },            // Get the current text and oldid of Spam blacklist getBL : function(request) {               var xml = request.responseXML; if ( xml != null ) { pages = xml.getElementsByTagName('page'); SBHandler.edittoken = pages[0].getAttribute('edittoken'); SBHandler.getRequest('action=query&prop=revisions&titles=MediaWiki:Spam-blacklist&rvprop=ids|timestamp|user|comment|content', SBHandler.parseBL, true); }           },            // Add/remove domains from the text parseBL : function(request) {               var xml = request.responseXML; if ( xml != null ) { revs = xml.getElementsByTagName('rev') ; user = revs[0].getAttribute('user'); revid = revs[0].getAttribute('revid'); SBHandler.timestamp = revs[0].getAttribute('timestamp'); SBHandler.text = revs[0].textContent; oldlength = SBHandler.text.length; document.getElementById('SgetBL').style.color = 'black'; } else { SBlist.innerHTML += 'ERROR: ' + request.status + ' Aborting. ';                   return; }               if (SBHandler.action == SBHandler.sb_add) { urls = SBHandler.urls.replace(/\|/g,'\n'); if (SBHandler.text.length > 1000 ) { lastchars = SBHandler.text.substr(-1000); SBHandler.text = SBHandler.text.substr(0, SBHandler.text.length - 1000); if (lastchars.indexOf('## sbhandler_end') > 0) { lastchars = lastchars.replace('## sbhandler_end', urls + '\n## sbhandler_end'); } else { SBlist.innerHTML += 'ERROR: Could not find marker. Aborting. ';                           return; }                           SBHandler.text += lastchars; } else { if (SBHandler.text.indexOf('## sbhandler_end') > 0) { SBHandler.text = SBHandler.text.replace('## sbhandler_end', urls + '\n## sbhandler_end'); } else { SBlist.innerHTML += 'ERROR: Could not find marker. Please add \"## sbhandler_end\" on a single line below the blacklist\'s entry\'s and try again. ';                           return; }                   }                } else if (SBHandler.action == SBHandler.sb_rem) { urls = SBHandler.urls.split('|'); for (var i=0; i < urls.length; i++) { SBHandler.text = SBHandler.text.replace(urls[i] + '\n', ''); }               }                //Check if the length changed, if not assume nothing changed. if (oldlength == SBHandler.text.length) { SBlist.innerHTML += 'ERROR: The length of the old and new text are the same. This shouldn\'t happen. Aborting. ';                   return; }               document.getElementById('SaddBL').style.color = 'black'; params = 'action=query&prop=revisions&titles=Spam+blacklist&rvdifftotext=' + encodeURIComponent(SBHandler.text); SBHandler.postRequest('format=xml', SBHandler.parseDiff, params, true); },           // Parse and show the proposed edit parseDiff : function(request) {               var xml = request.responseXML; if ( xml != null ) { document.getElementById('SgetC').style.color = 'black'; diffSource = xml.getElementsByTagName('diff'); if (diffSource[0].childNodes[0].nodeValue) { urls = SBHandler.urls.split('|'); if (SBHandler.action == 'add') { summary = 'Adding '; } else { summary = 'Removing '; }                       if (urls.length > 1 ) { summary += urls.length + ' domains '; } else { summary += urls[0] + ' '; }                       if (SBHandler.request.substr(0, 18) == 'Wikipedia:WikiProject_Spam/Local/') { summary += 'per ' + SBHandler.request + '.'; } else { summary += 'per MediaWiki_talk:Spam-blacklist.'; }                       SBlist.innerHTML += ' ' + ' ' + '  Confirm changes Edit changes '; document.getElementById('SparseBL').style.color = 'black'; } else { SBlist.innerHTML += '<div style=\"font-weight:bold;\">ERROR: Could not show diff. Aborting. ';                       return; }               } else { SBlist.innerHTML += '<div style=\"font-weight:bold;\">ERROR: ' + request.status + ' Aborting. ';                   return; }           },            // Add a text area to change the blacklist yourself editBL : function {               BLform = document.getElementById('BLform'); BLform.innerHTML = '<textarea name=\"wpTextbox1\" id=\"wpTextbox1\" cols=\"80\" rows=\"25\" accesskey=\",\">' + SBHandler.text + ' '                                   + BLform.innerHTML; },           // Submit the edit to Spam blacklist submitBL : function {               wpTextbox = document.getElementById('wpTextbox1'); if (wpTextbox != null) { SBHandler.text = wpTextbox.value; SBHandler.custom = true; //We can't simply log the change. User needs to do that. }               document.getElementById('SconfirmBL').style.color = 'black'; summary = document.getElementById('summary').value; summary += ' Using SBHandler.'; query = 'format=xml'; params = 'action=edit&title=Spam blacklist&summary=' + encodeURIComponent(summary) + '&text=' + encodeURIComponent(SBHandler.text) + '&basetimestamp=' + SBHandler.timestamp +  '&token=' + encodeURIComponent(SBHandler.edittoken); SBHandler.postRequest(query, SBHandler.LWrapper, params, true); },           // Start logging procedure LWrapper : function(request) {               var xml = request.responseXML; if (xml != null ) { edits = xml.getElementsByTagName('edit'); if (edits.length == 0 ) { SBlist.innerHTML = '<div style=\"font-weight:bold;\">Saving might have failed. Please check if it succeeded and log the edit yourself if necessary. ';                       return; }                       result = edits[0].getAttribute('result'); SBHandler.oldid = edits[0].getAttribute('newrevid'); if (result != 'Success') { SBlist.innerHTML = '<div style=\"font-weight:bold;\">Saving failed. Please blacklist the domains yourself. ';                       return; } else { document.getElementById('SsaveBL').style.color = 'black'; SBlist.innerHTML = ' Blacklist has been updated, <a href=\"' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?oldid=' + SBHandler.oldid + '&diff=prev\" title=\"diff\">diff</a>. ';                   }                } else { SBlist.innerHTML += '<div style=\"font-weight:bold;\">ERROR: ' + request.status + ' Aborting. ';                   return; }               d = new Date; m = d.getMonth + 1; if (m < 10 ) { m = '0' + m;               } y = d.getFullYear; logtitle = 'Mediawiki_talk:Spam blacklist/log/' + y + '/' + m;               if (SBHandler.request.substr(0, 18) != 'Wikipedia:WikiProject_Spam/') { SBHandler.getRequest('action=query&prop=revisions&titles=MediaWiki_talk:Spam-blacklist&rvprop=ids|timestamp|user|comment|content', SBHandler.parseTBL, true); } else { SBHandler.getRequest('action=query&prop=revisions&titles=' + logtitle + '&rvprop=ids|timestamp|user|comment|content', SBHandler.parseL, true); }           },            // Get current oldid of Mediawiki talk:Spam-blacklist, because that's the location of the request. parseTBL : function(request) {               var xml = request.responseXML; if ( xml != null ) { revs = xml.getElementsByTagName('rev') ; tprevid = revs[0].getAttribute('revid'); } else { SBlog.innerHTML += '<div style=\"font-weight:bold;\">ERROR: ' + request.status + ' . Please log the edit yourself. ';                   return; }               SBHandler.getRequest('action=query&prop=revisions&titles=' + logtitle + '&rvprop=ids|timestamp|user|comment|content', SBHandler.parseL, true); },           // Add/remove domains to/from log parseL : function(request) {               var xml = request.responseXML; if ( xml != null ) { document.getElementById('SgetL').style.color = 'black'; revs = xml.getElementsByTagName('rev') ; if (revs.length == 0) { SBlog.innerHTML += '<div style=\"font-weight:bold;\">ERROR: Could not get log. It probably does not exist yet. Please create <a href="//en.wikipedia.org/wiki/' + logtitle + '" title="' + logtitle + '">' + logtitle + '</a> and log the edit yourself. ';                       return; }                   SBHandler.timestamp = revs[0].getAttribute('timestamp'); SBHandler.text = revs[0].textContent; } else { SBlog.innerHTML += '<div style=\"font-weight:bold;\">ERROR: ' + request.status + ' Please log the edit yourself. ';                   return; }               if (SBHandler.action == SBHandler.sb_add) { sbldiff = ''; } else { sbldiff = ''; }               urls = SBHandler.urls.split('|'); r = SBHandler.request if (r.substr(0, 18) == 'Wikipedia:WikiProject_Spam/') {                   r =  + r + ; } else { r = ''; }               spaces = '                                        '; if (urls.length == 1) { log_text = ' ' + urls[0] + spaces.substr(0, 39 - urls[0].length) + '# ' + wgUserName + ' # ' + sbldiff + '; see ' + r               } else { log_text = ' #:                                    ' + wgUserName + ' # ' + sbldiff + '; see ' + r                   for (var i=0; i < urls.length; i++) { log_text += '\n  ' + urls[i]; }               }                // User needs to confirm log edit if (SBHandler.custom) { SBlog.innerHTML += ' The following text will be added to the log. You need to update this to reflect the changes you made to the proposed edit. '                                   + '<textarea name=\"wpTextbox1\" id=\"wpTextbox1\" cols=\"80\" rows=\"10\" accesskey=\",\">' + log_text + ' '                                   + '<button onclick=\"SBHandler.submitL\">Confirm changes '; } else { SBHandler.submitL; }           },            submitL : function { wpTextbox = document.getElementById('wpTextbox1'); if (wpTextbox != null) { SBHandler.text += '\n' + wpTextbox.value; } else { SBHandler.text += '\n' + log_text; }               document.getElementById('SaddL').style.color = 'black'; query = 'format=xml'; params = 'action=edit&title=' + encodeURIComponent(logtitle) + '&summary=' + encodeURIComponent(summary) + '&text=' + encodeURIComponent(SBHandler.text) + '&token=' + encodeURIComponent(SBHandler.edittoken); SBHandler.postRequest(query, SBHandler.LEnd, params, true); },           // Confirm results LEnd : function(request) {               var xml = request.responseXML; if (xml != null ) { edits = xml.getElementsByTagName('edit'); if (edits.length == 0 ) { SBlist.innerHTML = '<div style=\"font-weight:bold;\">Saving might have failed. Please check if it succeeded and log the edit yourself if necessary. ';                       return; }                   result = edits[0].getAttribute('result'); oldid = edits[0].getAttribute('newrevid'); if (result != 'Success') { SBlog.innerHTML = ' Saving failed. Please log the edit yourself. ';                       return; } else { document.getElementById('SsaveL').style.color = 'black'; document.getElementById('Sthrobber').innerHTML = ''; SBlog.innerHTML = ' Log has been updated, <a href=\"' + mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?oldid=' + oldid + '&diff=prev\" title=\"diff\">diff</a>.<p style=\"font-style:italic\">Thanks for helping with the Spam blacklist! Return to <a href=\"//en.wikipedia.org/wiki/MediaWiki_talk:Spam-blacklist\" title=\"MediaWiki talk:Spam-blacklist\">MediaWiki talk:Spam-blacklist</a>. ';                   }                }            },            getRequest : function(query, callback, api) {               if (api) { var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&' + query; } else { var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + query; }               var request = sajax_init_object ; if (request == null) { return null; }                   request.open('GET', url, true); request.onreadystatechange = function { if(request.readyState==4) { callback(request) }               };                request.setRequestHeader('Pragma', 'cache=yes'); request.setRequestHeader('Cache-Control', 'no-transform'); request.send(null); },           postRequest : function(query, callback, params, api) {               if (api) { var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?' + query; } else { var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + query; }               var request = sajax_init_object ; if (request == null) { return null; }                   request.open('POST', url, true); request.onreadystatechange = function { if(request.readyState==4) { callback(request) }               };

request.setRequestHeader('Content-type','application/x-www-form-urlencoded'); request.setRequestHeader('Content-length', params.length); request.setRequestHeader("Pragma", "cache=yes"); request.setRequestHeader("Cache-Control", "no-transform");

request.send(params); },           setupHandler : function {               if (wgPageName == 'Special:SpamBlacklist') { SBHandler.SBWrapper; } else { SBHandler.closeRequestLinks; }           }        } // End of SBHandler addOnloadHook (SBHandler.setupHandler); } // End of sysop check } // End of idempotency check //

//