User:AWB Alex/editing robot
From Guild Wars 2 Wiki
Jump to navigationJump to search
- Primary article: Widget:Editing robot
Editing robot[edit]
Make sure you login using your bot account.
Javascript[edit]
Code below. |
---|
// Chieftain Alex's page manipulation script function mwBulkScript () { // Make the GUI. function makeTaskGUI () { // Initialise frame with required options. var UIframe = $('<div id="fullwrapper">' +'<div id="titlebar">AJAX move/delete/restore/purge script</div>' +'<div class="contentwrapper">' +'<table><tbody>' +'<tr>' +'<td colspan="2"><input id="listsummary" type="text" placeholder="Insert edit summary here."></td>' +'</tr>' +'<tr>' +'<td id="listinput" rowspan="2"><textarea></textarea></td>' +'<td id="listsucceed"><div></div></td>' +'</tr>' +'<tr>' +'<td id="listfailed"><div></div></td>' +'</tr>' +'<tr>' +'<td id="listprogress"></td>' +'<td id="listoperators">' +'<select id="listmode" name="mode">' +'<option value="blank" selected>(blank)</option>' +'<option value="move">Move</option>' +'<option value="delete">Delete</option>' +'<option value="restore">Restore</option>' +'<option value="purge">Purge</option>' +'</select>' +'<input name="run" type="button" value="Run">' +'<input name="reset" type="button" value="Reset">' +'</td>' +'</tr>' +'</tbody></table>' +'</div>' +'</div>'); $('#widgettarget').html(UIframe); } // Extract information from the GUI. function getCurrentTaskFromGUI () { // Define variables var mode = $('#listmode').val(); var summary = ''; var content = ''; // no idea how we'd do this bit. Maybe only apply regex rules to old content? (GET required) var sourcepagetitle = ''; var targetpagetitle = ''; // Obtain the list of potential pages var allpages = []; try { allpages = $('#listinput textarea').val().split('\n'); } catch(e) { allpages = ['testpage','testpage2']; } // Take the first line away from the potential pages and add to the processing box. var currentline = allpages.shift(); $('#listprogress').text(currentline); // Return allpages back to the form without the top line $('#listinput textarea').val(allpages.join('\n')); // Check list entry isn't blank var currentpages = currentline.split('»') if (currentpages.length > 0) { sourcepagetitle = currentpages[0]; } // Check if the mode is move, if so, we need to have a target. if (mode === 'move' && currentpages.length > 1) { targetpagetitle = currentpages[1]; } else if (mode === 'move') { // If no target, reject everything and shut down. return; } // Get summary summary = $('#listsummary').val(); // Verify inprogress isn't blank if ( $('#listprogress').text() == '' ) { return; } // Need to pass a different set of options for each operation var options = {}; switch (mode) { case "move": // Move source page to target page - this works by separating lists with '»' // FIXME: Use a tab instead? options = { action: "move", reason: summary, from: sourcepagetitle, to: targetpagetitle, bot: true, noredirect: true, movetalk: true, movesubpages: true, ignorewarnings: true }; break; case "delete": // Delete source page options = { action: "delete", reason: summary, title: sourcepagetitle, bot: true }; break; case "restore": // Restore source page with last revision options = { action: "undelete", reason: summary, title: sourcepagetitle, bot: true }; break; case "purge": // Purge current page content cache options = { action: "purge", titles: sourcepagetitle, forcelinkupdate: true, bot: true }; break; default: // Do nothing and abort. return; break; } mwapiOperate(options); } // Perform an operation using the currently active account. function mwapiOperate (options) { // console.log(options); var pagetitle = options.title || options.from; api.postWithToken( "csrf", options ) .done( function( result, jqXHR ) { mw.log( pagetitle + ": Saved successfully" ); $('#listsucceed div').html( $('#listprogress').text() + '<br>' + $('#listsucceed div').html()); $('#listprogress').text(''); if ( $('#listinput textarea').val() !== '' ) { getCurrentTaskFromGUI(); } } ) .fail( function( code, result ) { $('#listfailed div').html( $('#listprogress').text() + '<br>' + $('#listfailed div').html()); $('#listprogress').text(''); if ( code === "http" ) { mw.log( pagetitle + ": HTTP error: " + result.textStatus ); } else if ( code === "ok-but-empty" ) { mw.log( pagetitle + ": Got an empty response from the server" ); } else { mw.log( pagetitle + ": API error: " + code ); } } ); } // Make the API connection, set up the GUI, respond to button pushes, loop through elements function mainTaskFunction () { // When user clicks run, pick up the current function and cycle through the list $('input[name="run"]').click(function(){ console.log('user clicked run.'); if ( $('#listmode').val() == 'blank' ) { console.log('User did not select a mode. Abort!'); return; } getCurrentTaskFromGUI(); }); // When user clicks reset, blank all fields $('input[name="reset"]').click(function(){ console.log('user clicked reset.'); $('#listmode').val('blank'); $('#listsucceed div').html(''); $('#listfailed').html(''); $('#listinput textarea').text(''); $('#listprogress').text(''); $('#listprogress').text(''); $('#listsummary').val(''); }); } var api = new mw.Api(); makeTaskGUI(); mainTaskFunction(); } RLQ.push(mwBulkScript); |