forked from Technical-13/OneClickArchiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOneClickArchiver.js
271 lines (251 loc) · 14 KB
/
OneClickArchiver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
$(document).ready( function () {
if ( ( $( '#ca-addsection' ).length > 0 ||
$.inArray( 'Non-talk pages that are automatically signed', mw.config.get( 'wgCategories' ) ) >= 0 ) &&
mw.config.get( 'wgAction' ) === 'view' &&
$.inArray( 'Pages that should not be manually archived', mw.config.get( 'wgCategories' ) ) === -1 ) {
var OCAstate = mw.user.options.get( 'userjs-OCA-enabled', 'true' );
var pageid = mw.config.get( 'wgArticleId' );
var errorLog = {};
new mw.Api().get( {
action: 'query',
pageids: pageid,
rvsection: 0,
prop: [ 'revisions', 'info' ],
rvprop: "content",
indexpageids: 1,
continue: ''
} ).done( function ( response0 ) {
var content0 = response0.query.pages[pageid].revisions[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.counter = 'Counter<br />defaulted to<br /><br />' + counter;
} else {
counter = counter[1];
var archiveNum = counter;
}
/* archiveName *///Get the archiveName value
var archiveNameRegEx = /\| *archive *= *(.*\%\(counter\)d.*?) *(-->)?/;
var archiveName = archiveNameRegEx.exec( content0 );
var rootBase = mw.config.get( 'wgPageName' )
.replace( /\/.*/g, '' )// Chop off the subpages
.replace( /_/g, ' ' );// Replace underscores with spaces
if ( archiveName === null || archiveName === undefined ) {
archiveName = rootBase + '/Archive ' + counter;
errorLog.archiveName = 'Archive name<br />defaulted to<br /><br />' + archiveName;
} else {
var year = new Date().getFullYear();
var month = new Date().getMonth();
var archiveName = archiveName[1]
.replace( /\| *archive *= */, '' )
.replace( /\%\(year\)d/g, year)
.replace( /\%\(month\)d/g, month)
.replace( /\%\(monthname\)s/g, mw.config.get( 'wgMonthNames' )[month+1] )
.replace( /\%\(monthnameshort\)s/g, mw.config.get( 'wgMonthNamesShort' )[month+1] )
.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.archiveName = 'Archive name mismatch:<br /><br />Found: ' + archiveName;
errorLog.archiveName += '<br />Expected: ' + rootBase.replace( '_', ' ' ) + archiveSub + '<br /><br />';
}
}
/* 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.headerLevel = 'Header level<br />defaulted to<br /><br />' + headerLevel;
} else {
headerLevel = parseInt( headerLevel[1] );
}
/* 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.archiveHeader = 'Archive header<br />defaulted to<br /><br />' + archiveHeader;
} else {
archiveHeader = archiveHeader[1];
}
/* 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.maxArchiveSize = 'Maximum archive size defaulted to<br /><br />' + 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 );
}
/* debug */// Table to report the values found.
if ( mw.config.get( 'debug' ) === true ) {
var OCAreport = '<table style="width: 100%;" border="1"><tr><th>Config</th><th>value</th></tr>';
OCAreport += '<tr><td>Counter</td><td style="text-align: center;';
if ( errorLog.counter ) { OCAreport += ' background-color: #FFEEEE;">' + errorLog.counter; }
else { OCAreport += '">' + counter; }
OCAreport += '</td></tr><tr><td>Archive<br />name</td><td style="text-align: center;';
if ( errorLog.archiveName ) { OCAreport += ' background-color: #FFEEEE;">' + errorLog.archiveName; }
else { OCAreport += '">' + archiveName; }
OCAreport += '</td></tr><tr><td>Header<br />Level</td><td style="text-align: center;';
if ( errorLog.headerLevel ) { OCAreport += ' background-color: #FFEEEE;">' + errorLog.headerLevel; }
else { OCAreport += '">' + headerLevel }
OCAreport += '</td></tr><tr><td>Archive<br />header</td><td style="text-align: center;';
if ( errorLog.archiveHeader ) { OCAreport += ' background-color: #FFEEEE;">' + errorLog.archiveHeader; }
else { OCAreport += '">' + archiveHeader }
OCAreport += '</td></tr><tr><td>Max<br />archive<br />size</td><td style="text-align: center;';
if ( errorLog.maxArchiveSize ) { OCAreport += ' background-color: #FFEEEE;">' + errorLog.maxArchiveSize; }
else { OCAreport += '">' + maxArchiveSize }
OCAreport += '</td></tr></table>';
mw.notify( $( OCAreport ), { title: 'OneClickArchiver report!', tag: 'OCA', autoHide: true } );
}
if ( errorLog && OCAstate === 'true' ) {
/* Temporary extra filtering */
if ( errorLog.counter || errorLog.archiveName ) {
/* Temporary extra filtering */
var OCAerror = '<p>The following errors detected:<br />';
if ( errorLog.counter ) { OCAerror += '<b style="font-size: larger; color: #FF0000;">•</b> Unable to find <b>|counter=</b><br />'; }
if ( errorLog.archiveName && errorLog.archiveName.search( 'defaulted to' ) !== -1 ) { OCAerror += '<b style="font-size: larger; color: #FF0000;">•</b> Unable to find <b>|archive=</b><br />'; }
if ( errorLog.archiveName && errorLog.archiveName.search( 'mismatch' ) !== -1 ) { OCAerror += '<b style="font-size: larger; color: #FF0000;">•</b> Archive name mismatch detected.<br />'; }
// if ( errorLog.headerLevel ) { OCAerror += ' Unable to find <b>|headerlevel=</b><br />'; }
// if ( errorLog.archiveHeader ) { OCAerror += ' Unable to find <b>|archiveheader=</b><br />'; }
// if ( errorLog.maxArchiveSize ) { OCAerror += ' Unable to find <b>|maxarchivesize=</b><br />'; }
if ( errorLog.counter || errorLog.archiveName ) { OCAerror += '<br /><b style="font-size: larger; color: #FF0000;">•</b> Causing the script to abort.<br />'; }
OCAerror += '<br /><span style="font-size: larger;">Please, see <a href="/wiki/User:Technical_13/Scripts/OneClickArchiver" title="User:Technical_13/Scripts/OneClickArchiver">the documentation</a> for details.</span></p>';
mw.notify( $( OCAerror ), { title: 'OneClickArchiver errors!', tag: 'OCAerr', autoHide: true } );
/* Temporary extra close for extra filtering */
}
/* Temporary extra close for extra filtering */
}
if ( errorLog.counter || errorLog.archiveName ) {
/* Abort script */
} else {
$( 'h' + headerLevel + ' span.mw-headline' ).each( function( i, val ) {
var sectionName = $( this ).text();
var editSectionUrl = $( this ).parent().find( '.mw-editsection a: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( ' <div class="archiverDiv" style="font-size: 0.6em; font-weight: bold; float: right;"> | <a id="' + sectionNumber +
'" href="#archiverLink" class="archiverLink">' + 'Archive' + '</a></div>' );
$( this ).parent( 'h' + headerLevel ).find( 'a.archiverLink' ).click( function() {
var mHeaders = '<span style="color: #444;">Retrieving headers...</span>';
var mSection = 'retrieving section content...';
var mPosting = '<span style="color: #040">Content retrieved,</span> performing edits...';
var mPosted = '<span style="color: #080">Archive appended...</span>';
var mCleared = '<span style="color: #080">Section cleared...</span>';
var mReloading = '<span style="color: #008">All done! </span><a href="#archiverLink" onClick="javascript:location.reload();" title="Reload page">Reloading</a>...';
$( 'body' ).append( '<div class="overlay" style="background-color: #000; opacity: 0.4; position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; z-index: 500;"></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: 501; opacity: 1; position: fixed; width: 50%; left: 25%; top: 30%; background: #F7F7F7; border: #222 ridge 1px; padding: 20px;"></div>' );
$( '.arcProg' ).append( '<div>' + mHeaders + '</div>' );
$( '.arcProg' ).append( '<div>' + 'Archive name <span style="font-weight: normal; color: #036;">' + archiveName + '</span> <span style="color: darkgreen;">found</span>, ' + mSection + '</div>' );
new mw.Api().get( {
action: 'query',
pageids: pageid,
rvsection: sectionNumber,
prop: [ 'revisions', 'info' ],
rvprop: 'content',
indexpageids: 1,
format: 'json',
continue: ''
} ).done( function ( responseSection ) {
var sectionContent = responseSection.query.pages[pageid].revisions[0]['*'];
$( '.arcProg' ).append( '<div>' + mPosting + '</div>' );
var dnau = sectionContent.match( /<!-- \[\[User:DoNotArchiveUntil\]\] ([\d]{2}):([\d]{2}), ([\d]{1,2}) (January|February|March|April|May|June|July|August|September|October|November|December) ([\d]{4}) \(UTC\) -->/ );
if ( dnau === null || dnau === undefined ) {
var dnauDate = Date.now();
dnau = null;
} else {
dnau = dnau[1] + ':' + dnau[2] + ' ' + dnau[3] + ' ' + dnau[4] + ' ' + dnau[5];
var dnauDate = new Date( dnau );
dnauDate = dnauDate.valueOf();
}
if ( dnauDate > Date.now() ) {
$( '.arcProg' ).remove();
$( '.overlay' ).remove();
var dnauAbortMsg = '<p>This section has been marked \"Do Not Archive Until\" ' + dnau + ', so archiving was aborted.<br /><br /><span style="font-size: larger;">Please, see <a href="/wiki/User:Technical_13/1CA" title="User:Technical_13/1CA">the documentation</a> for details.</span></p>';
mw.notify( $( dnauAbortMsg ), { title: 'OneClickArchiver aborted!', tag: 'OCAdnau', autoHide: false } );
} else {
var contentSection = '\n\n{{Clear}}\n' + sectionContent;
if ( dnau != null ) {
contentSection = contentSection.replace( /<!-- \[\[User:DoNotArchiveUntil\]\] ([\d]{2}):([\d]{2}), ([\d]{1,2}) (January|February|March|April|May|June|July|August|September|October|November|December) ([\d]{4}) \(UTC\) -->/g, '' );
}
new mw.Api().postWithToken( 'edit', {
action: 'edit',
title: archiveName,
appendtext: contentSection,
summary: '[[User:Technical_13/1CA|OneClickArchive]] adding [[' + archiveName + '#' + sectionName + '|' + sectionName + ']]'
} ).done( function( archived ) {
$( '.arcProg' ).append( '<div class="archiverPosted">' + mPosted + '</div>' );
new mw.Api().postWithToken( 'edit', {
action: 'edit',
section: sectionNumber,
pageid: pageid,
text: '',
summary: '[[User:Technical_13/1CA|OneClickArchive]] archived [[Special:Diff/' + archived.edit.newrevid + '|' + sectionName + ']] to [[' + archiveName + '#' + sectionName + '|' + archiveName + ']]'
} ).done( function() {
$( '.arcProg' ).append( '<div class="archiverCleared">' + mCleared + '</div>' );
$( '.arcProg' ).append( '<div>' + mReloading + '</div>' );
location.reload();
} );
} );
}
} );
} );
}
}
} );
}
} );
var linkTextD = 'OCA - on', linkDescD = 'Disable OneClickArchiver on this page';
var linkTextE = 'OCA - off', linkDescE = 'Enable OneClickArchiver on this page';
var linkText = linkTextD, linkDesc = linkDescD;
if ( OCAstate === 'false' ) {
linkText = linkTextE, linkDesc = linkDescE;
$( 'div.archiverDiv' ).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 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';
} );
} );
}
} );