User:Elli/OneClickArchiver.js

/**

* Derived from Evad37's [1] version of Technical 13's version [2] of Equazcion's OneClickArchiver [3]

* [1] < https://en.wikipedia.org/wiki/User:Evad37/OneClickArchiver.js >

* [2] < https://en.wikipedia.org/wiki/User:Technical_13/Scripts/OneClickArchiver.js >

* [3] < https://en.wikipedia.org/wiki/User:Equazcion/OneClickArchiver.js >

*/

//

mw.loader.using(['mediawiki.util', 'mediawiki.api'], function() {

var config = mw.config.get([

'debug',

'wgAction',

'wgArticleId',

'wgCategories',

'wgMonthNames',

'wgNamespaceNumber',

'wgPageName',

'wgRelevantUserName'

]);

if (!window.OCALinkSize){

linkSize = 0.6;

} else{

linkSize = window.OCALinkSize;

}

function swapInDates(inputString, thisMonthNum, thisYear){

/*

year, month, month02d, monthname, and monthnameshort are commonly supported by bots

quarter is supported by Hazard-Bot

Hazard-Bot also supports isoyear, week, and isoweek, but I found no evidence of use of any of these on Wikidata, so have not implemented (since I have no page to test these with).

*/

thisMonthFullName = config.wgMonthNames[ thisMonthNum ];

monthNamesShort = [ "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

thisMonthShortName = monthNamesShort[ thisMonthNum ];

thisQuarter = Math.ceil(thisMonthNum/4);

return inputString

.replace( /\| *archive *= */, '' )

.replace( /\%\(year\)d/g, thisYear )

.replace( /\%\(month\)d/g, thisMonthNum )

.replace( /\%\(month\)02d/g, thisMonthNum.toString().padStart(2, '0') ) // Month with padding so it's always two digits. Probably fine to hard code this instead of adding generic support for padding, since it's not like this comes up often, right?

.replace( /\%\(monthname\)s/g, thisMonthFullName )

.replace( /\%\(monthnameshort\)s/g, thisMonthShortName )

.replace( /\%\(quarter\)s/g, thisQuarter );

}

$( document ).ready( function () {

if ( ( $( '#ca-addsection' ).length > 0 ||

$.inArray( 'Non-talk pages that are automatically signed', config.wgCategories ) >= 0 ) &&

config.wgAction === 'view' &&

$.inArray( 'Pages that should not be manually archived', config.wgCategories ) === -1 ) {

var OCAstate = mw.user.options.get( 'userjs-OCA-enabled', 'true' );

var pageid = config.wgArticleId;

var errorLog = { errorCount: 0 };

new mw.Api().get( {

action: 'query',

prop: [ 'revisions', 'info' ],

rvsection: 0,

rvprop: 'content',

pageids: pageid,

indexpageids: 1,

rawcontinue: ''

} ).done( function ( response0 ) {

var thisMonthNum, thisYear, archiveNum;

var content0 = response0.query.pages[ pageid ].revisions[ 0 ][ '*' ];

thisMonthNum = new Date().getMonth() + 1; // This returns something 0-11, but we want 1-12.

thisYear = new Date().getFullYear();

/* archiveme */// Find out if there is already an {{Archive me}} request, and if it is between 1-2 months old

if ( config.wgNamespaceNumber === 3 ) {

var nowOcto = parseInt( ( ( thisYear * 12 ) + thisMonthNum ), 10 );

var archiveme = content0.match( /\{\{Archive ?me(\| *date *= *(January|February|March|April|May|June|July|August|September|October|November|December) ([\d]{4}))?\}\}/i );

if ( archiveme === null || archiveme === undefined ) {

errorLog.errorCount++;

errorLog.archiveme = '{{Archiveme}} not found.';

} else {

/* Archive me found - how old is it? */

var archivemeMonth = archiveme[ 2 ];

var archivemeMonthNum = 0;

if ( typeof archivemeMonth === 'number' ) {

archivemeMonthNum = parseInt( archivemeMonth, 10 );

} else {

for ( var i in config.wgMonthNames ) {

if ( archivemeMonth === config.wgMonthNames[ i ] ) {

archivemeMonthNum = parseInt( i, 10 );

} else if ( archivemeMonth === monthNamesShort[ i ] ) {

archivemeMonthNum = parseInt( i, 10 );

}

}

}

var archivemeYear = parseInt( archiveme[ 3 ], 10 );

var archivemeOcto = parseInt( ( ( archivemeYear * 12 ) + archivemeMonthNum ), 10 );

var archivemeSafe = parseInt( ( nowOcto - 2 ), 10 );

archiveme = archiveme[ 0 ];

}

}

/* counter */// Get the counter value

var counterRegEx = new RegExp( '\\| *counter *= *(\\d+)' );

var counter = counterRegEx.exec( content0 );

if ( counter === null || counter === undefined ) {

counter = 1;

errorLog.errorCount++;

errorLog.counter = counter;

} else {

counter = counter[ 1 ];

archiveNum = counter;

}

/* archiveName */// Get the archiveName value

var archiveNameRegEx = /\| *archive *= *(.*\%\((counter|year|month|monthname|monthnameshort)\)d.*) *(-->)?/;

var archiveName = archiveNameRegEx.exec( content0 );

var rootBase = config.wgPageName

.replace( /\/.*/g, '' )// Chop off the subpages

.replace( /_/g, ' ' );// Replace underscores with spaces

if ( archiveName === null || archiveName === undefined ) {

archiveName = rootBase + '/Archive ' + counter;

errorLog.errorCount++;

errorLog.archiveName = archiveName;

} else {

archiveName = swapInDates(archiveName[ 1 ], thisMonthNum, thisYear).replace( /\%\(counter\)d/g, archiveNum );

var archiveBase = archiveName

.replace( /\/.*/, '' )// Chop off the subpages

.replace( /_/g, ' ' );// Replace underscores with spaces

var archiveSub = archiveName

.replace( /_/g, ' ' )// Replace underscores with spaces

.replace( archiveBase, '' );// Chop off the base pagename

if ( archiveBase != rootBase ) {

errorLog.errorCount++;

errorLog.archiveName = 'Archive name mismatch:

Found: ' + archiveName;

errorLog.archiveName += '
Expected: ' + rootBase.replace( '_', ' ' ) + archiveSub + '

';

}

}

/* archivepagesize */// Get the size of the destination archive from the API

new mw.Api().get( {

action: 'query',

prop: 'revisions',rvlimit: 1,

rvprop: [ 'size', 'content' ],

titles: archiveName,

list: 'usercontribs',

uclimit: 1,

ucprop: 'timestamp',

ucuser: ( ( config.wgRelevantUserName ) ?

config.wgRelevantUserName : 'Example' ),

rawcontinue: '',

} ).done( function ( archivePageData ) {

var archivePageSize = 0;

if ( archivePageData.query.pages[ -1 ] === undefined ) {

for ( var a in archivePageData.query.pages ) {

archivePageSize = parseInt( archivePageData.query.pages[ a ].revisions[ 0 ].size, 10 );

archiveName = archivePageData.query.pages[ a ].title;

}

} else {

archivePageSize = -1;

archiveName = archivePageData.query.pages[ archivePageSize ].title;

errorLog.errorCount++;

errorLog.archivePageSize = -1;

errorLog.archiveName = '' + archiveName + '';

}

/* maxarchivesize */// Get the defined max archive size from template

var maxArchiveSizeRegEx = new RegExp( '\\| *maxarchivesize *= *(\\d+K?)' );

var maxArchiveSize = maxArchiveSizeRegEx.exec( content0 );

if ( maxArchiveSize === null || maxArchiveSize[ 1 ] === undefined ) {

maxArchiveSize = parseInt( 153600, 10 );

errorLog.errorCount++;

errorLog.maxArchiveSize = maxArchiveSize;

} else if ( maxArchiveSize[ 1 ].slice( -1 ) === "K" && $.isNumeric( maxArchiveSize[ 1 ].slice( 0, maxArchiveSize[ 1 ].length-1 ) ) ) {

maxArchiveSize = parseInt( maxArchiveSize[ 1 ].slice( 0, maxArchiveSize[ 1 ].length - 1 ), 10 ) * 1024;

} else if ( $.isNumeric( maxArchiveSize[ 1 ].slice() ) ) {

maxArchiveSize = parseInt( maxArchiveSize[ 1 ].slice(), 10 );

}

/* pslimit */// If maxArchiveSize is defined, and archivePageSize >= maxArchiveSize increment counter and redfine page name.

if ( !errorLog.maxArchiveSize && archivePageSize >= maxArchiveSize ) {

counter++;

archiveName = archiveNameRegEx.exec( content0 );

archiveName = swapInDates(archiveName[ 1 ], thisMonthNum, thisYear).replace( /\%\(counter\)d/g, counter );

var oldCounter = counterRegEx.exec( content0 );

var newCounter = '|counter=1';

if ( oldCounter !== null && oldCounter !== undefined ) {

newCounter = oldCounter[ 0 ].replace( oldCounter[ 1 ], counter );

oldCounter = oldCounter[ 0 ];

} else {

errorLog.errorCount++;

errorLog.newCounter = newCounter;

}

}

/* archiveheader */// Get the defined archive header to place on archive page if it doesn't exist

var archiveHeaderRegEx = new RegExp( '\\| *archiveheader *= *(\{\{[^\r\n]*\}\})' );

var archiveHeader = archiveHeaderRegEx.exec( content0 );

if ( archiveHeader === null || archiveHeader === undefined ) {

archiveHeader = "{{Aan}}";

errorLog.errorCount++;

errorLog.archiveHeader = archiveHeader;

} else {

archiveHeader = archiveHeader[ 1 ];

}

/* headerlevel */// Get the headerlevel value or default to '2'

var headerLevelRegEx = new RegExp( '\\| *headerlevel *= *(\\d+)' );

var headerLevel = headerLevelRegEx.exec( content0 );

if ( headerLevel === null || headerLevel === undefined ) {

headerLevel = 2;

errorLog.errorCount++;

errorLog.headerLevel = headerLevel;

} else {

headerLevel = parseInt( headerLevel[ 1 ] );

}

/* debug */// Table to report the values found.

if ( config.debug === true ) {

var OCAreport = '

';

OCAreport += '

configvalue
Counter' + errorLog.counter; }

else { OCAreport += '">' + counter; }

OCAreport += '

Archive name
' + errorLog.archiveName; }

else { OCAreport += '">' + archiveName; }

OCAreport += '

Header Level' + errorLog.headerLevel; }

else { OCAreport += '">' + headerLevel; }

OCAreport += '

Archive header' + errorLog.archiveHeader; }

else { OCAreport += '">' + archiveHeader; }

OCAreport += '

Max
archive size
' + errorLog.maxArchiveSize; }

else { OCAreport += '">' + maxArchiveSize; }

OCAreport += '

Current
archive size
' + archivePageSize; }

else if ( archivePageSize >= maxArchiveSize ) { OCAreport += ' background-color: #FFEEEE;">' + archivePageSize; }

else { OCAreport += '">' + archivePageSize; }

if ( !errorLog.archiveme && archiveme !== undefined ) {

OCAreport += '

Asked to archive '; }

if ( ( nowOcto - archivemeOcto ) === 0 ) { OCAreport += 'this month'; }

else if ( ( nowOcto - archivemeOcto ) === 1 ) { OCAreport += 'last month'; }

else { OCAreport += ' background-color: #FFEEEE;">Asked to archive ' + ( nowOcto - archivemeOcto ) + ' months ago'; }

}

if ( errorLog.archiveme || archiveme !== undefined ) { OCAreport += '

' + errorLog.archiveme; }

else if ( archiveme !== undefined ) { OCAreport += '">' + archiveme; }

OCAreport += '

Documentation
';

mw.notify( $( OCAreport ), { title: 'OneClickArchiver report!', tag: 'OCA', autoHide: false } );

}

var OCAerror = '

The following errors detected:
';

if ( errorLog.counter ) { OCAerror += ' Unable to find |counter=
     Default value: 1
'; }

if ( errorLog.archiveName && errorLog.archiveName.search( 'defaulted to' ) !== -1 ) { OCAerror += ' Unable to find |archive=
     Default value: ' + archiveName + '
'; }

if ( errorLog.archiveName && errorLog.archiveName.search( 'mismatch' ) !== -1 ) { OCAerror += ' Archive name mismatch detected.
'; }

if ( errorLog.headerLevel ) { OCAerror += '  Unable to find |headerlevel=
     Default value: 2
'; }

if ( errorLog.archiveHeader ) { OCAerror += '  Unable to find |archiveheader=
     Default value: "{{Aan}}"
'; }

if ( errorLog.maxArchiveSize ) { OCAerror += '  Unable to find |maxarchivesize=
     Default value: 153600
'; }

if ( errorLog.counter || errorLog.archiveName ) { OCAerror += '
 Causing the script to abort.
'; }

OCAerror += '
Please, see the documentation for details.

';

var archiverReport = mw.util.addPortletLink(

'p-cactions',

'#archiverNoLink',

'|Archive',

'pt-OCA-report',

'Report for why there are no |Archive links on this page',

null,

null

);

$( archiverReport ).click( function ( e ) {

e.preventDefault();

mw.notify( $( OCAerror ), { title: 'OneClickArchiver errors!', tag: 'OCAerr', autoHide: false } );

} );

if ( config.wgNamespaceNumber === 3 && ( errorLog.counter || errorLog.archiveName ) &&

config.debug === true && errorLog.archiveme ) {

if ( confirm( 'Click [OK] to post {{Archiveme|{{SUBST:DATE}}}} to the top of the page and abort or\n\t[Cancel] to attempt running with default values.' ) === true ) {

new mw.Api().postWithToken( 'edit', {

action: 'edit',

section: 0,

pageid: pageid,

text: '{{Archiveme|{{SUBST:DATE}}}}\n' + content0,

summary: '{{Archiveme}} posted with OneClickArchiver.'

} ).done( function () {

alert( 'Request for user to set up archiving posted.' );

location.reload();

} );

}

} else if ( config.wgNamespaceNumber === 3 && archivemeOcto >= archivemeSafe ) {

/* Archive me request was made, give the user a chance to comply */

} else if ( config.wgNamespaceNumber === 3 && ( errorLog.counter || errorLog.archiveName ) && config.debug === true && confirm( '{{Archiveme}} found on the top of the page:\n\n\t Click [OK] abort or\n\t[Cancel] to attempt running with default values.' ) === true ) {

/* User aborted script */

} else {

// $( 'h' + headerLevel + ' span.mw-headline' ).each( function() {

$( 'div.mw-heading' + headerLevel + ' h'+headerLevel ).each( function() {

var sectionName = $( this ).text();

var editSectionUrl = $( this ).parent().find('.mw-editsection a').not('.mw-editsection-visualeditor').first().attr( 'href' );

var sectionReg = /§ion=(.*)/;

var sectionRaw = sectionReg.exec( editSectionUrl );

if ( sectionRaw != null && sectionRaw[ 1 ].indexOf( 'T' ) < 0 ) {

var sectionNumber = parseInt( sectionRaw[ 1 ] );

// if ( $( this ).parent().prop( 'tagName' ) === 'H' + headerLevel ) {

// $( this ).parent( 'h' + headerLevel ).append(

$( this ).parent( 'div.mw-heading' ).append(

'

| ' + 'Archive' + '
'

);

// $(this).parent('h' + headerLevel).find('a.archiverLink').attr('title', 'Archive to: "'+archiveName+'"');

$(this).parent('div.mw-heading').find('a.archiverLink').attr('title', 'Archive to: "'+archiveName+'"');

// $( this ).parent( 'h' + headerLevel ).find( 'a.archiverLink' ).click( function() {

$( this ).parent( 'div.mw-heading' ).find( 'a.archiverLink' ).click( function() {

var mHeaders = 'Retrieving headers...';

var mSection = 'retrieving section content...';

var mPosting = 'Content retrieved, performing edits...';

var mPosted = 'Archive appended...';

var mCleared = 'Section cleared...';

var mReloading = 'All done! Reloading...';

$( 'body' ).append( '

' );

$( 'body' ).prepend( '

' );

$( '.arcProg' ).append( '

' + mHeaders + '
' );

$( '.arcProg' ).append( '

' + 'Archive name ' + archiveName + ' found, ' + mSection + ' (' + archivePageSize + 'b)
' );

new mw.Api().get( {

action: 'query',

pageids: pageid,

rvsection: sectionNumber,

prop: [ 'revisions', 'info' ],

rvprop: 'content',

indexpageids: 1,

rawcontinue: ''

} ).done( function ( responseSection ) {

var sectionContent = responseSection.query.pages[ pageid ].revisions[ 0 ][ '*' ];

$( '.arcProg' ).append( '

' + mPosting + '
' );

var dnau = sectionContent.match( // );

var dnauDate;

if ( dnau === null || dnau === undefined ) {

dnauDate = Date.now();

dnau = null;

} else {

dnau = dnau[ 1 ] + ':' + dnau[ 2 ] + ' ' + dnau[ 3 ] + ' ' + dnau[ 4 ] + ' ' + dnau[ 5 ];

dnauDate = new Date( dnau );

dnauDate = dnauDate.valueOf();

}

if ( dnauDate > Date.now() ) {

$( '.arcProg' ).remove();

$( '.overlay' ).remove();

var dnauAbortMsg = '

This section has been marked \"Do Not Archive Until\" ' + dnau + ', so archiving was aborted.

Please, see the documentation for details.

';

mw.notify( $( dnauAbortMsg ), { title: 'OneClickArchiver aborted!', tag: 'OCAdnau', autoHide: false } );

} else {

var archiveAction = 'adding section';

if ( archivePageSize <= 0 || ( archivePageSize >= maxArchiveSize && !errorLog.maxArchiveSize ) ) {

sectionContent = archiveHeader + '\n\n' + sectionContent;

archiveAction = 'creating';

mPosted = 'Archive created...';

} else {

sectionContent = '\n\n{{Clear}}\n' + sectionContent;

}

if ( dnau != null ) {

sectionContent = sectionContent.replace( //g, '' );

}

new mw.Api().postWithToken( 'edit', {

action: 'edit',

title: archiveName,

appendtext: sectionContent,

summary: '/* '+sectionName+' */ archived using OneClickArchiver)'

} ).done( function () {

$( '.arcProg' ).append( '

' + mPosted + '
' );

new mw.Api().postWithToken( 'edit', {

action: 'edit',

section: sectionNumber,

pageid: pageid,

text: '',

summary: 'OneClickArchived "' + sectionName + '" to ' + archiveName + ''

} ).done( function () {

$( '.arcProg' ).append( '

' + mCleared + '
' );

if ( archivePageSize >= maxArchiveSize && !errorLog.maxArchiveSize ) {

var mUpdated = 'Counter updated...';

new mw.Api().postWithToken( 'edit', {

action: 'edit',

section: 0,

pageid: pageid,

text: content0.replace( oldCounter, newCounter ),

summary: 'OneClickArchiver updating counter.'

} ).done( function () {

$( '.arcProg' ).append( '

' + mUpdated + '
' );

$( '.arcProg' ).append( '

' + mReloading + '
' );

location.reload();

} );

} else {

$( '.arcProg' ).append( '

' + mReloading + '
' );

location.reload();

}

} );

} );

}

} );

} );

// }

}

} );

}

} );

} );

var linkTextD = '1CA is on', linkDescD = 'Disable OneClickArchiver';

var linkTextE = '1CA is off', linkDescE = 'Enable OneClickArchiver';

var linkText = linkTextD, linkDesc = linkDescD;

if ( OCAstate === 'false' ) {

linkText = linkTextE; linkDesc = linkDescE;

$( 'div.archiverDiv, li#pt-OCA-report' ).css( 'display', 'none' );

}

var archiverToggle = mw.util.addPortletLink(

'p-cactions',

'#archiverLink',

linkText,

'pt-OCA',

linkDesc,

'o',

null

);

$( archiverToggle ).click( function ( e ) {

e.preventDefault();

/* Toggle the archiveLinks */

$( 'div.archiverDiv' ).css( 'display', function ( _i, val ) {

return val === 'none' ? '' : 'none';

});

/* Toggle the toggle link */

$( 'li#pt-OCA a' ).html( function ( _i, val ) {

return val === linkTextD ? linkTextE : linkTextD;

});

/* Toggle the toggle description */

$( 'li#pt-OCA a' ).attr( 'title', function ( _i, val ) {

return val === linkDescD ? linkDescE : linkDescD;

});

/* Toggle the error report link */

if ( ( errorLog.counter || errorLog.archiveName ) ) {

$( 'li#pt-OCA-report' ).css( 'display', function ( _i, val ) {

return val === 'none' ? '' : 'none';

});

}

/* Toggle default state */

new mw.Api().postWithToken( 'options', {

action: 'options',

optionname: 'userjs-OCA-enabled',

optionvalue: OCAstate === 'true' ? 'false' : 'true'

} ).done( function() {

var resultMsg = 'OneClickArchiver is now ' + ( OCAstate === 'true' ? 'disabled' : 'enabled' ) + ' by default.';

mw.notify(resultMsg);

OCAstate = OCAstate === 'true' ? 'false' : 'true';

} );

} );

}

} );

});

//