-
Notifications
You must be signed in to change notification settings - Fork 1
/
DetectG4G5.js
178 lines (153 loc) · 4.79 KB
/
DetectG4G5.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
// <nowiki>
/*
- Displays an alert if an article may be a CSD G4 (previous AFD) or CSD G5 (created by a sockpuppet)
- Useful for new page patrolling
- Only runs on pages that have not been marked as reviewed
*/
// TODO: Code review. Is there a way to reduce the # of API queries?
class DetectG4G5 {
async execute() {
if ( !this.shouldRunOnThisPage() ) {
return;
}
const title = mw.config.get( 'wgPageName' ); // includes namespace, underscores instead of spaces
const pageID = mw.config.get( 'wgArticleId' );
if ( await this.isReviewed( pageID ) ) {
return;
}
if ( await this.afdExists( title ) && !await this.hasAFDTemplate( title ) ) {
const href = mw.config.get( 'wgArticlePath' ).replace( '$1', 'Wikipedia:Articles_for_deletion/' + title );
this.displayWarning( `<span style="font-weight:bold">CSD G4:</span> There is an <a href="${ href }">AFD page</a> for this article. It may qualify for CSD G4.` );
}
const pageCreator = await this.getPageCreator( title );
if ( await this.isBlocked( pageCreator ) ) {
this.displayWarning( '<span style="font-weight:bold">CSD G5:</span> The creator of this page is blocked. This article may qualify for CSD G5.' );
}
if ( await this.isGloballyLocked( pageCreator ) ) {
this.displayWarning( '<span style="font-weight:bold">CSD G5:</span> The creator of this page is globally locked. This article may qualify for CSD G5.' );
}
}
async getWikicode( title ) {
if ( !mw.config.get( 'wgCurRevisionId' ) ) {
return '';
} // if page is deleted, return blank
let wikicode = '';
title = encodeURIComponent( title );
await $.ajax( {
url: 'https://en.wikipedia.org/w/api.php?action=parse&page=' + title + '&prop=wikitext&formatversion=2&format=json',
success: function ( result ) {
wikicode = result.parse.wikitext;
},
dataType: 'json',
async: false
} );
return wikicode;
}
async hasAFDTemplate( title ) {
const wikicode = await this.getWikicode( title );
return wikicode.indexOf( '{{Article for deletion' ) !== -1;
}
displayWarning( html ) {
$( '#contentSub' ).before( `<div class="DetectG4G5" style="background-color: red margin-bottom: 5px;">${ html }</div>` );
}
/**
* @param {number} pageID The page ID number. A positive number with no commas.
*/
async isReviewed( pageID ) {
const api = new mw.Api();
const response = await api.get( {
action: 'query',
format: 'json',
formatversion: '2',
prop: 'isreviewed',
pageids: pageID
} );
return response.query.pages[ 0 ].isreviewed;
}
async afdExists( title ) {
title = 'Wikipedia:Articles_for_deletion/' + title;
return await this.pageExists( title );
}
async pageExists( title ) {
const api = new mw.Api();
const response = await api.get( {
action: 'query',
format: 'json',
prop: 'revisions',
titles: title
} );
const exists = typeof response.query.pages[ '-1' ] === 'undefined';
return exists;
}
async isBlocked( username ) {
const api = new mw.Api();
const response = await api.get( {
action: 'query',
format: 'json',
list: 'users',
usprop: 'blockinfo',
ususers: username
} );
const isBlocked = typeof response.query.users[ 0 ].blockid !== 'undefined';
return isBlocked;
}
async isGloballyLocked( username ) {
const api = new mw.Api();
const response = await api.get( {
action: 'query',
list: 'globalallusers',
agulimit: '1',
agufrom: username,
aguto: username,
aguprop: 'lockinfo'
} );
const isLocked = response.query.globalallusers.length !== 0 && response.query.globalallusers[ 0 ].locked === '';
return isLocked;
}
getFirstValueInObject( obj ) {
return obj[ Object.keys( obj )[ 0 ] ];
}
async getPageCreator( title ) {
const api = new mw.Api();
const response = await api.get( {
action: 'query',
format: 'json',
prop: 'revisions',
titles: title,
rvlimit: '1',
rvdir: 'newer'
} );
const page = this.getFirstValueInObject( response.query.pages );
const pageCreator = page.revisions[ 0 ].user;
return pageCreator;
}
shouldRunOnThisPage() {
// don't run when not viewing articles
const action = mw.config.get( 'wgAction' );
if ( action !== 'view' ) {
return false;
}
// don't run when viewing diffs
const isDiff = mw.config.get( 'wgDiffNewId' );
if ( isDiff ) {
return false;
}
const isDeletedPage = ( !mw.config.get( 'wgCurRevisionId' ) );
if ( isDeletedPage ) {
return false;
}
// Only run in mainspace
const namespace = mw.config.get( 'wgNamespaceNumber' );
const title = mw.config.get( 'wgPageName' ); // includes namespace, underscores instead of spaces
if ( namespace !== 0 && title !== 'User:Novem_Linguae/sandbox' ) {
return false;
}
return true;
}
}
$( async () => {
await mw.loader.using( [ 'mediawiki.api' ], async () => {
await ( new DetectG4G5() ).execute();
} );
} );
// </nowiki>