Jump to content

User:Σ/Testing facility/Archiver.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by TheDJ (talk | contribs) at 09:20, 31 July 2017 (script is also using mediawiki.api). 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.
$.when( mw.loader.using(['mediawiki.util','mediawiki.api']), $.ready).done( function () {
	var validSections = {},
		fromTos = {},
		wikiText = '',
		revStamp, startArchiveButton, overlay;

	if ( mw.config.get( 'wgNamespaceNumber' ) % 2 === 0 &&
		mw.config.get( 'wgNamespaceNumber' ) != 4 ) {
		// not a talk page and not project namespace
		return;
	}
	if ( mw.config.get( 'wgNamespaceNumber' ) == -1 ) {
		// is a special page
		return;
	}
	$( 'head' ).append(
		'<style>a.arkylink { font-weight:bold } .arkyhighlight { background-color:#D9E9FF }</style>'
	);

	startArchiveButton = mw.util.addPortletLink(
		'p-cactions',
		'#',
		'ØCA',
		'pt-oeca',
		'Enter/exit the archival process',
		null,
		null
	);
	overlay = $( document.createElement( 'button' ) );
	$( startArchiveButton ).click( function ( e ) {
		$( '.arkylink' ).click();
		$( '.arky' ).toggle();
		$( '#archivebutton' ).toggle();
	} );
	overlay.html( 'archive all the selected threads' )
		.attr( 'id', 'archivebutton' )
		.css( 'position', 'fixed' )
		.css( 'bottom', 0 )
		.css( 'height', '44px' )
		.css( 'width', '100%' )
		.css( 'font-size', '200%' );
	$( document.body ).append( overlay );
	overlay.toggle();
	overlay.click( function ( e ) {
		var numOfThreads, archiveTarget, sections, archiveThis, cutOffset,
			revisedPage;
		function cut( s, start, end ) {
			return s.substr( 0, start ) + s.substring( end );
		}
		cutOffset = numOfThreads = 0;
		revisedPage = wikiText;
		sections = $( 'a.arkylink' ).map( function () {
			return $( this ).attr( 'data-section' );
		} );
		if ( !( numOfThreads = sections.length ) ) {
			return alert( 'No threads selected, aborting' );
		}
		archiveTarget = prompt(
			'Archiving ' + numOfThreads + ' threads: where should we move them to?',
			mw.config.get( 'wgPageName' )
		);
		if ( !archiveTarget || archiveTarget == mw.config.get( 'wgPageName' ) ) {
			return alert( 'No archive target selected, aborting' );
		}
		sections.each( function ( i, n ) {
			revisedPage = cut(
				revisedPage,
				fromTos[ n ][ 0 ] - cutOffset,
				fromTos[ n ][ 1 ] - cutOffset
			);
			cutOffset += fromTos[ n ][ 1 ] - fromTos[ n ][ 0 ];
		} );
		archiveThis = sections.map( function () {
			return wikiText.substring( fromTos[ this ][ 0 ], fromTos[ this ][ 1 ] );
		} ).toArray().join( '' );
		console.log( 'archive this:' + archiveThis );
		console.log( 'revised page:' + revisedPage );
		if ( 1 ) {
			new mw.Api().postWithToken(
				'csrf',
				{
					action: 'edit',
					title: mw.config.get( 'wgPageName' ),
					text: revisedPage,
					summary: 'Removed ' + numOfThreads + ' threads to [[' + archiveTarget + ']]',
					basetimestamp: revStamp,
					starttimestamp: revStamp
				}
			).done( function ( res1 ) {
				alert( 'Successfully removed threads from talk page' );
				console.log( res1 );
				new mw.Api().postWithToken(
					'csrf',
					{
						action: 'edit',
						title: archiveTarget,
						appendtext: '\n' + archiveThis,
						summary: 'Added threads from [[' + mw.config.get( 'wgPageName' ) + ']]'
					}
				).done( function ( res2 ) {
					alert( 'Successfully added threads to archive page' );
				} ).fail( function ( res2 ) {
					alert( 'failed to add threads to archive page. manual inspection needed.' );
				} ).always( function ( res2 ) {
					console.log( res2 );
					window.location.reload();
				} );
			} ).fail( function ( res1 ) {
					alert( 'failed to remove threads from talk page. aborting archive process.' );
					console.log( res1 );
					window.location.reload();
				} );
		}
	} );
	new mw.Api().get( {
		action: 'parse',
		page: mw.config.get( 'wgPageName' )
	} ).done( function ( dataShit ) {
			var i;
			new mw.Api().get( {
				action: 'query',
				pageids: mw.config.get( 'wgArticleId' ),
				prop: [ 'revisions' ],
				rvprop: [ 'content', 'timestamp' ]
			} ).done( function ( shit ) {
				var rv;
				rv = shit.query.pages[ mw.config.get( 'wgArticleId' ) ].revisions[ 0 ];
				wikiText = rv[ '*' ];
				revStamp = rv.timestamp;
			} );
			$( dataShit.parse.sections )
				.filter( function ( i, s ) { return s.index == parseInt( s.index ) } )
				.each( function ( i, s ) { validSections[ s.index ] = s } );
			for ( i in validSections ) {
				i = parseInt( i );
				fromTos[ i ] = [
					validSections[ i ].byteoffset,
					validSections.hasOwnProperty( i + 1 ) ? validSections[ i + 1 ].byteoffset : Infinity
				];
			}
			$( '#mw-content-text' )
				.find( ':header' )
				.find( 'span.mw-headline' )
				.each( function ( i, title ) {
					var header, editSection, sectionNumber;
					header = $( this ).parent();
					editSection = header.find( '.mw-editsection' ); // 1st child
					sectionNumber = header.find( '.mw-editsection a:first' );
					if ( sectionNumber[ 0 ] ) {
						sectionNumber = sectionNumber.attr( 'href' ).match( /&section=(\d+)/ );
						if ( sectionNumber ) {
							sectionNumber = sectionNumber[ 1 ];
						} else {
							// eg <h2>not a real section</h2>
							sectionNumber = undefined;
						}
					}
					if ( validSections.hasOwnProperty( sectionNumber ) ) {
						editSection[ 0 ].innerHTML += '&nbsp;'
							+ '<span class=arky style=display:none>'
							+ '<span class=mw-editsection-bracket>[</span>'
							+ '<a data-section=' + sectionNumber + ' '
							+ 'onclick=$(this).closest(\':header\').toggleClass(\'arkyhighlight\');$(this).toggleClass(\'arkylink\');>'
							+ 'archive</a>'
							+ '<span class=mw-editsection-bracket>]</span>'
							+ '</span>';
					}
				} );
		} );
} );