-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUserTalkErasedSectionsDetector.js
More file actions
181 lines (157 loc) · 5.33 KB
/
UserTalkErasedSectionsDetector.js
File metadata and controls
181 lines (157 loc) · 5.33 KB
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
// <nowiki>
/*
A user script that alerts you with a yellow banner at the top of a User Talk page if more than 3% of recent user talk diffs are self-deletions, with exceptions for some edit summary keywords such as "archiving".
Useful for detecting if a WP:PERM applicant is whitewashing their User Talk page by removing warnings without archiving them.
*/
class ErasedSectionsDetector {
constructor( mw, $ ) {
this.mw = mw;
// eslint-disable-next-line no-jquery/variable-pattern
this.$ = $;
}
async execute() {
if ( !this.shouldRunOnThisPage() ) {
return;
}
const title = this.mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
this.revisions = await this.getRevisions( title );
const totalRevisionCount = this.revisions.length;
this.addDiffsToRevisions();
this.filterForRevisionsByThisEditorOnly();
this.filterForContentRemoval();
this.filterOutReasonableEditSummaries();
this.expandBlankEditSummaries();
const negativeDiffCount = this.revisions.length;
const deletionPercent = negativeDiffCount / totalRevisionCount;
// if 500 edits, this means the script will show an alert when 16 or more edits were deleted without archiving
const MINIMUM_DELETION_PERCENT = 0.03;
if ( deletionPercent > MINIMUM_DELETION_PERCENT ) {
this.addHtml( negativeDiffCount, totalRevisionCount );
this.listenForShowDiffsClick();
}
}
/**
* Add a message to blank edit summaries. This is so the hyperlink can be clicked.
*/
expandBlankEditSummaries() {
this.revisions = this.revisions.map( ( revision ) => {
if ( revision.comment === '' ) {
revision.comment = '[no edit summary]';
}
return revision;
} );
}
listenForShowDiffsClick() {
this.$( '#ErasedSectionsDetector-SeeDiffs' ).on( 'click', () => {
this.$( '#ErasedSectionsDetector-Diffs' ).toggle();
} );
}
addHtml( negativeDiffCount, totalRevisionCount ) {
let html = `
<div class="ErasedSectionsDetector">
<div style="background-color: yellow">
<span style="font-weight:bold">Warning:</span> This user has removed content from this page (probably without archiving it) in ${ negativeDiffCount } of the last ${ totalRevisionCount } revisions. <a id="ErasedSectionsDetector-SeeDiffs">Click here</a> to see diffs.
</div>
<div id="ErasedSectionsDetector-Diffs" style="border: 1px solid black; font-size: 80%; display: none;">
<ul>
`;
for ( const revision of this.revisions ) {
html += `
<li>
<a href="w/index.php?title=${ encodeURIComponent( this.mw.config.get( 'wgPageName' ) ) }&diff=prev&oldid=${ revision.revid }">
${ revision.comment }
</a>
</li>
`;
}
html += `
</ul>
</div>
</div>
`;
this.$( '#contentSub' ).after( html );
}
filterForContentRemoval() {
const MINIMUM_DIFF_SIZE = -10;
this.revisions = this.revisions.filter( ( revision ) => revision.diff < MINIMUM_DIFF_SIZE );
}
filterForRevisionsByThisEditorOnly() {
const thisEditor = this.mw.config.get( 'wgTitle' );
this.revisions = this.revisions.filter( ( revision ) => revision.user === thisEditor );
}
filterOutReasonableEditSummaries() {
const keywordsToIgnore = [
'arc', // arc, arch, archive, archiving, OneClickArchiver
'bot mes', // mesg, message
'mass mes',
'newsletter',
'wikibreak',
'out of town'
];
for ( let keyword of keywordsToIgnore ) {
this.revisions = this.revisions.filter( ( revision ) => {
keyword = keyword.toLowerCase();
const editSummary = revision.comment.toLowerCase();
return !editSummary.includes( keyword );
} );
}
}
/**
* Given the Action API output of query revisions as a JavaScript object, add to this object a field called "diff" that is the difference +/- in size of that diff compared to the next oldest diff.
*/
addDiffsToRevisions() {
const len = this.revisions.length;
let lastRevisionSize = this.revisions[ len - 1 ].size;
// need to store the OLDER revision's size in a buffer to compute a diff, so iterate BACKWARDS
for ( let i = ( len - 2 ); i >= 0; i-- ) {
const thisRevisionSize = this.revisions[ i ].size;
this.revisions[ i ].diff = thisRevisionSize - lastRevisionSize;
lastRevisionSize = thisRevisionSize;
}
}
async getRevisions( title ) {
const api = new this.mw.Api();
const response = await api.get( {
action: 'query',
format: 'json',
prop: 'revisions',
titles: title,
formatversion: '2',
rvprop: 'comment|size|user|ids',
rvslots: '',
rvlimit: '500', // get 500 revisions
rvdir: 'older' // get newest revisions (enumerate towards older entries)
} );
return response.query.pages[ 0 ].revisions;
}
shouldRunOnThisPage() {
const isViewing = this.mw.config.get( 'wgAction' ) === 'view';
if ( !isViewing ) {
return false;
}
const isDiff = this.mw.config.get( 'wgDiffNewId' );
if ( isDiff ) {
return false;
}
const isDeletedPage = !this.mw.config.get( 'wgCurRevisionId' );
if ( isDeletedPage ) {
return false;
}
const namespace = this.mw.config.get( 'wgNamespaceNumber' );
const isUserTalkNamespace = [ 3 ].includes( namespace );
if ( !isUserTalkNamespace ) {
return false;
}
const isSubPage = this.mw.config.get( 'wgPageName' ).includes( '/' );
if ( isSubPage ) {
return;
}
return true;
}
}
$( async () => {
await mw.loader.using( [ 'mediawiki.api' ], async () => {
await ( new ErasedSectionsDetector( mw, $ ) ).execute();
} );
} );
// </nowiki>