User:Bility/archiveTalkSections.js

var $ASscriptsettings = new Object({	'custom': false,	'current': 1,	'maxsize': 50,	'pagename': 'Archive 1' });

function mainArchiveSection { if (mw.config.get('wgPageName') !== 'User_talk:'+mw.config.get('wgUserName') || mw.config.get('wgUserGroups').indexOf('autoconfirmed') == -1) { return false; };	testArchiveSectionSettings; addArchiveSectionSettingsLink; addArchiveSectionLinks; addSectionRemovalLinks; };

function testArchiveSectionSettings { $.ajax({		url: '/w/index.php?title=User:'+mw.config.get('wgUserName')+'/archiveTalkSectionsSettings.js&action=raw&ctype=text/javascript',		dataType: 'script',		async: false	}); if (typeof($sectionArchiveSettings) == 'undefined') { createArchiveSectionSettings; } else { $ASscriptsettings.custom=$sectionArchiveSettings.custom; $ASscriptsettings.current=$sectionArchiveSettings.current; $ASscriptsettings.maxsize=$sectionArchiveSettings.maxsize; if ($ASscriptsettings.custom) { $ASscriptsettings.pagename=$sectionArchiveSettings.pagename; } else { $ASscriptsettings.pagename='Archive '+$sectionArchiveSettings.current; };	}; };

function addArchiveSectionSettingsLink { var $archiveSectionLink = $('Archive Section Settings') .attr( { id: 'archivesectionsettingslink', href: '#', title: 'Change or view the Archive Section settings', style: 'float:right;font-size:.8em' } ); $('#siteSub').prepend($archiveSectionLink); $('#archivesectionsettingslink').click( function {		var $archiveOptionDialog = getArchiveSectionSettingsDialog;		$archiveOptionDialog.dialog('open');		return false;	}); };

function addArchiveSectionLinks { var $archiveSectionLink = $('archive') .attr( { id: 'archivesectionlink', href: '#', title: 'Archive this section' } ); $('span.editsection').each( function { 		if ($(this).parent.get(0).tagName === 'H2') {			$(this).prepend('[',$archiveSectionLink.clone,']');		};	}); $('a#archivesectionlink').click( function {		var archiveSectionDialogHTML = 'Retrieving section information...';		var $archiveSectionDialog = getArchiveSectionDialog(1);		$archiveSectionDialog.dialog('open');		var archiveSectionTitle = $(this).parent.siblings('span.mw-headline').html;		var archiveSectionNum = $(this).siblings('a:contains("edit")').attr('href');		archiveSectionNum = archiveSectionNum.substring(archiveSectionNum.search('section=')+8);		var archiveSectionInfo = getArchiveSectionText(archiveSectionNum);		if (archiveSectionInfo.length != 2) {$archiveSectionDialog.html(archiveSectionDialogHTML+='Error!'); return false;};		$archiveSectionDialog.html(archiveSectionDialogHTML+='done. Checking archive size...');		var ASpageSize = getArchivePageSize;		if (typeof(ASpageSize)=='undefined') {			var newArchive = true;			ASpageSize = 0;		} else {			var newArchive = false;			archiveSectionInfo[1] = archiveSectionInfo[1].substring(archiveSectionInfo[1].search(/$/m));		};		if (!newArchive && parseFloat(ASpageSize)+parseFloat(archiveSectionInfo[0])>$ASscriptsettings.maxsize*1024) {			$archiveSectionDialog.html(archiveSectionDialogHTML+='max size exceeded!');			if ($ASscriptsettings.custom) {				$archiveSectionDialog.html(archiveSectionDialogHTML+=' Please enter new archive page name in settings.');				return false;			} else {				$archiveSectionDialog.html(archiveSectionDialogHTML+=' Creating new archive page...');				$ASscriptsettings.current=parseFloat($ASscriptsettings.current)+1;				$ASscriptsettings.pagename='Archive '+$ASscriptsettings.current;				saveArchiveSectionSettings;			};		} else {			$archiveSectionDialog.html(archiveSectionDialogHTML+='done. Archiving section now...');		};		archiveArchiveSection(archiveSectionInfo[1],(newArchive)?'':archiveSectionTitle);		$archiveSectionDialog.html(archiveSectionDialogHTML+='done. Removing section from talk page...');		removeArchiveSection(archiveSectionNum);		$archiveSectionDialog.html(archiveSectionDialogHTML+='done. Reloading talk page...');		$archiveSectionDialog.fadeTo(2000,100,function { $archiveSectionDialog.dialog('close'); delete $ASscriptsettings; window.location.href=window.location.href; });				return false;	}); };

function addSectionRemovalLinks { var $deleteSectionLink = $('x') .attr( { id: 'deletesectionlink', href: '#', title: 'Delete this section' } ); $('span.editsection').each( function { 		if ($(this).parent.get(0).tagName === 'H2') {			$(this).prepend('[',$deleteSectionLink .clone,']');		};	}); $('a#deletesectionlink').click( function {		var archiveSectionDialogHTML = 'Retrieving section information...';		var $archiveSectionDialog = getArchiveSectionDialog(0);		$archiveSectionDialog.dialog('open');		var archiveSectionNum = $(this).siblings('a:contains("edit")').attr('href');		archiveSectionNum = archiveSectionNum.substring(archiveSectionNum.search('section=')+8);		$archiveSectionDialog.html(archiveSectionDialogHTML+='done. Deleting section...');		removeArchiveSection(archiveSectionNum);		$archiveSectionDialog.html(archiveSectionDialogHTML+='done. Reloading talk page...');		$archiveSectionDialog.fadeTo(2000,100,function { $archiveSectionDialog.dialog('close'); delete $ASscriptsettings; window.location.href=window.location.href; });				return false;	}); };

function getArchiveSectionSettingsDialog { var $dialogBox = $(' ') .html('Current archive page:  '			+' Auto-archive number:  '			+'  Use custom page '			+'Max archive size in kilobytes (K): ') .dialog({			autoOpen: false,			buttons: { 'Save settings': function {				$('.ui-button-text').html('Saving...');				$ASscriptsettings.custom=$('#ASDPageCustom').attr('checked');				if (!$('#ASDPageCustom').attr('checked')) {$ASscriptsettings.current=$('#ASDPageCurrent').attr('value');};				$ASscriptsettings.maxsize=$('#ASDPageSize').attr('value');				$ASscriptsettings.pagename=$('#ASDPageName').attr('value');				saveArchiveSectionSettings;				$(this).dialog('close');			}},			title: 'Archive Settings',			modal: true		}); if(!$ASscriptsettings.custom) { $dialogBox.find('#ASDPageCustom').attr('checked',false); $dialogBox.find('#ASDPageName').attr('disabled',true); $dialogBox.find('#ASDPageCurrent').attr('disabled',false); } else { $dialogBox.find('#ASDPageCustom').attr('checked',true); $dialogBox.find('#ASDPageName').attr('disabled',false); $dialogBox.find('#ASDPageCurrent').attr('disabled',true); };	$dialogBox.find('#ASDPageCurrent').attr('value',$ASscriptsettings.current); $dialogBox.find('#ASDPageName').attr('value',$ASscriptsettings.pagename); $dialogBox.find('#ASDPageSize').attr('value',$ASscriptsettings.maxsize); $dialogBox.find('#ASDPageCustom').change(function {		if ($(this).attr('checked')) {			$('#ASDPageName').attr( { value: $ASscriptsettings.pagename, disabled: false } );			$('#ASDPageCurrent').attr('disabled',true);		} else {			$('#ASDPageName').attr( { value: 'Archive '+$('#ASDPageCurrent').attr('value'), disabled: true } );			$('#ASDPageCurrent').attr('disabled',false);		};	}); $dialogBox.find('#ASDPageCurrent').change(function {		if ($(this).attr('value').search(/^\d+$/) == -1) {			$(this).attr('value',$ASscriptsettings.current);		};		if (!$('#ASDPageCustom').attr('checked')) {			$('#ASDPageName').attr('value', 'Archive '+$(this).attr('value') );		};	}); $dialogBox.find('#ASDPageSize').change(function {		if ($(this).attr('value').search(/^\d+$/) == -1) {			$(this).attr('value',$ASscriptsettings.maxsize);		};	}); return $dialogBox; };

function getArchiveSectionDialog(dialogType) { var $dialogBox = $(' ') .html('Retrieving section information...') .dialog({			autoOpen: false,			title: (dialogType==1)?'Archiving section':'Deleting section',			modal: true		}); return $dialogBox; };

function getArchivePageSize { var url = '/w/api.php?action=query&prop=revisions&titles=User talk:'+mw.config.get('wgUserName')+'/'+$ASscriptsettings.pagename+'&rvprop=size&format=json'; var ASpageSize; $.ajax({		url: url,		dataType: 'json',		async: false,		success: function(data) { ASpageSize = data.query.pages[Object.keys(data.query.pages)].revisions[0].size; }	}); return ASpageSize; };

function createArchiveSectionSettings { var $archiveOptionDialog = getArchiveSectionSettingsDialog; $archiveOptionDialog.dialog('open'); };

function saveArchiveSectionSettings { var ASpageName = 'User:'+mw.config.get('wgUserName')+'/archiveTalkSectionsSettings.js'; var AStoken = getArchiveSectionToken(ASpageName); var AStext = "var $sectionArchiveSettings = new Object({\n"			+"	'custom': "+$ASscriptsettings.custom+",\n"			+"	'current': "+$ASscriptsettings.current+",\n"			+"	'maxsize': "+$ASscriptsettings.maxsize+",\n"			+"	'pagename': '"+$ASscriptsettings.pagename+"'\n"			+"});"; var ASsummary = 'Automated settings edit for ATS'; $.ajax({		type: 'POST',		url: '/w/api.php?action=edit&title='+ASpageName+'&summary='+ASsummary+'&minor=1&recreate=1&text='+AStext+'&token='+AStoken,		async: false	}); };

function archiveArchiveSection(sectionText,sectionTitle) { var ASpageName = 'User talk:'+mw.config.get('wgUserName')+'/'+$ASscriptsettings.pagename; var AStoken = getArchiveSectionToken(ASpageName); var ASsummary = (sectionTitle=='') ? 'Automated talk page archiving with ATS' : sectionTitle; var ASnewSection = (sectionTitle=='') ? '' : '&section=new'; var url = '/w/api.php?action=edit&title='+ASpageName+'&summary='+ASsummary+'&minor=1&recreate=1&text='+encodeURIComponent(sectionText)+ASnewSection+'&token='+AStoken $.ajax({		type: 'POST',		url: url,		async: false	}); };

function getArchiveSectionToken(tokenPageName) { var url = '/w/api.php?action=query&prop=info&intoken=edit&titles='+tokenPageName+'&format=json'; var token; $.ajax({		url: url,		success: function(data) { token = data.query.pages[Object.keys(data.query.pages)].edittoken.replace(/\+\\$/g,'%2B%5C'); },		dataType: 'json',		async: false	}); return token; };

function getArchiveSectionText(sectionNumber) { var url = '/w/api.php?action=query&prop=revisions&titles=User talk:'+mw.config.get('wgUserName')+'&rvprop=content|size&rvsection='+sectionNumber+'&format=json'; var sectionInfo = []; $.ajax({		url: url,		dataType: 'json',		async: false,		success: function(data) {			var counter = 0;			for (var i in data.query.pages) {				for (var j in data.query.pages[i].revisions[0]) {					sectionInfo[counter] = data.query.pages[i].revisions[0][j];					counter++;				};			};		}	}); return sectionInfo; };

function removeArchiveSection(sectionNumber) { var ASpageName = 'User talk:'+mw.config.get('wgUserName'); var AStoken = getArchiveSectionToken(ASpageName); var ASsummary = 'Automated talk page archiving with ATS'; $.ajax({		type: 'POST',		url: '/w/api.php?action=edit&title='+ASpageName+'&summary='+ASsummary+'&minor=1&recreate=1&text=&section='+sectionNumber+'&token='+AStoken,		async: false	}); };

$(document).ready(mainArchiveSection);