Nothing Special   »   [go: up one dir, main page]

User:Equazcion/OneClickArchiver.js

This is an old revision of this page, as edited by Equazcion (talk | contribs) at 06:44, 22 October 2013. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
if ($('#ca-addsection').length > 0){
	$('span.mw-headline').each(function(index, value){
		var section = index;
		if ($(this).parent().prop("tagName") == 'H2'){
		 
			$(this).parent('h2').append(' <div style="font-size:.6em;font-weight:bold;float:right"> | <a id="' + section + 
				'" href="#archiverLink" class="archiverLink">' + 'Archive' + '</a></div>');
				
			$(this).parent('h2').find('a.archiverLink').click(function(){
				
				var mHeaders = '<span style="color:#454545;">Retrieving headers...</span>';
				var mSection = 'retreiving section content...';
				var mPosting = '<span style="color:#004000">Content retrieved,</span> appending to archive...';
				var mClearing = '<span style="color:#008000">Appended,</span> removing section...';
				var mReloading = '<span style="color:#00008C">All done! </span>Reloading...';
				
				$('body').append('<div class="overlay" style="background-color:#000;opacity:.4;position:fixed;' + 
					'top:0;left:0;width:100%;height:100%;z-index:10;"></div>');					
					
				$('body').prepend('<div class="arcProg" style="font-weight:bold;box-shadow: 7px 7px 5px #000;font-size:0.9em;line-height:1.5em;' + 
					'z-index:500;opacity:1;position:fixed;width:50%;left:30%;top:30%;background:#F7F7F7;border:#222 ridge 1px;padding:20px;"></div>');
					
				$('.arcProg').append('<div>' + mHeaders + '</div>');
				
				var request4 = {
					action:"query", 
					titles: wgPageName, 
					rvsection: 0,
					prop: "revisions|info", 
					intoken: "edit", 
					rvprop: "content",
					indexpageids: 1,
					dataType: "xml",
					format: "xml"
				};
		 
				$.get(mw.config.get("wgScriptPath")+"/api.php", request4, function(response4){
					
					var content = $(response4).find('rev').text();
					
					var regexObject3 = new RegExp('\\|counter ?= ?(\\d+)');
					var counter = regexObject3.exec(content);
					if ((counter == null) || (typeof counter == 'undefined')){
						$('.arcProg').remove();
						$('.overlay').remove();
						alert("No archive counter was detected on this page, so archiving was aborted. See User:Equazcion/OneClickArchiver for details.");
					} else {
						var archiveNum = counter[1];
						
						var archiveNameReg = /(\|archive ?= ?.*\%\(counter\)d.*)/;
						var archiveNameRegMatch = archiveNameReg.exec(content);
						if ((archiveNameRegMatch == null) || (typeof archiveNameRegMatch == 'undefined')){
							$('.arcProg').remove();
							$('.overlay').remove();
							alert("No archive name was detected on this page, so archiving was aborted. See User:Equazcion/OneClickArchiver for details.");
						} else {
							
							var monthNames = ["january", "february", "march", "april", "may", "june", 
								"july", "august", "september", "october", "november", "december"];
								
							var shortMonthNames = ["jan", "feb", "mar", "apr", "may", "jun", 
								"jul", "aug", "sep", "oct", "nov", "dec"];
								
							var year = new Date().getFullYear();
							var month = new Date().getMonth();
							
							var archiveName = archiveNameRegMatch[1]
								.replace(/\|archive ?= ?/, '')
								.replace(/\%\(year\)d/g, year)
								.replace(/\%\(month\)d/g, month)
								.replace(/\%\(monthname\)s/g, monthNames[month])
								.replace(/\%\(monthnameshort\)s/g, shortMonthNames[month])
								.replace(/\%\(counter\)d/g, archiveNum);

							$('.arcProg').append('<div>' + 'Archive name <span style="font-weight:normal;color:#003366;">' + archiveName + 
								'</span> <span style="darkgreen">found</span>, ' + mSection + '</div>');

							var request5 = {
								action:"query", 
								titles: wgPageName, 
								rvsection: section + 1,
								prop: "revisions|info", 
								intoken: "edit", 
								rvprop: "content",
								indexpageids: 1,
								dataType: "xml",
								format: "xml"
							};
					 
							$.get(mw.config.get("wgScriptPath")+"/api.php", request5, function(response5){
								
								$('.arcProg').append('<div>' + mPosting + '</div>');
							
								var contentSection = '\r\r' + $(response5).find('rev').text();

								var request1 = {
									action:"edit", 
									title: archiveName,
									appendtext: contentSection, 
									summary: '[[User:Equazcion/OneClickArchiver|OneClickArchiver]] adding 1 discussion',
									token: mw.user.tokens.get("editToken")
								};
				
								$.post(mw.config.get("wgScriptPath")+"/api.php", request1, function(response1){
									
									$('.arcProg').append('<div>' + mClearing + '</div>');
									
									var request2 = {
										action:"edit", 
										section: section + 1, 
										title: wgPageName, 
										text: "",
										summary: '[[User:Equazcion/OneClickArchiver|OneClickArchiver]] archived 1 discussion to [[' + archiveName + ']]',
										token: mw.user.tokens.get("editToken")
									};
							 
									$.post(mw.config.get("wgScriptPath")+"/api.php", request2, function(response1){
										$('.arcProg').append('<div>' + mReloading + '</div>');
										
										location.reload();	
									});
								});
							});
						}
					}
				});
			});
		}
	});
}