-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDontForgetG12.js
More file actions
132 lines (113 loc) · 3.6 KB
/
DontForgetG12.js
File metadata and controls
132 lines (113 loc) · 3.6 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
// <nowiki>
/*
- Check if article is unreviewed
- If so, display a giant "copyright check" button at the top, to remind you to run Earwig's copyvio detector on the article first thing.
- Many submissions are copyright violations, and catching it before you perform a bunch of other steps in the NPP/AFC flowchart saves time.
*/
class DontForgetG12 {
constructor( mw, $ ) {
this.mw = mw;
// eslint-disable-next-line no-jquery/variable-pattern
this.$ = $;
}
async execute() {
if ( !await this.shouldRunOnThisPage() ) {
return;
}
// Only run if draft is submitted
const isDraftspaceOrUserspace = [ 118, 2 ].includes( this.namespace );
const draftIsSubmitted = this.wikicode.match( /(?:{{AfC submission}}|{{AfC submission\|}}|{{AfC submission\|\|)/i ) && isDraftspaceOrUserspace;
if ( draftIsSubmitted ) {
this.insertButton( this.title );
}
// or 2) article is not marked as reviewed by NPP
this.mw.hook( 'ext.pageTriage.toolbar.ready' ).add( async () => {
const pageID = this.mw.config.get( 'wgArticleId' );
if ( !( await this.isReviewed( pageID ) ) ) {
this.insertButton( this.title );
}
} );
}
async shouldRunOnThisPage() {
// don't run when not viewing articles
const action = this.mw.config.get( 'wgAction' );
if ( action !== 'view' ) {
return false;
}
// don't run when viewing diffs
const isDiff = this.mw.config.get( 'wgDiffNewId' );
if ( isDiff ) {
return false;
}
const isDeletedPage = ( !this.mw.config.get( 'wgCurRevisionId' ) );
if ( isDeletedPage ) {
return false;
}
this.namespace = this.mw.config.get( 'wgNamespaceNumber' );
const isMainspaceDraftspaceOrUserspace = [ 0, 118, 2 ].includes( this.namespace );
if ( !isMainspaceDraftspaceOrUserspace ) {
return false;
}
this.title = this.getArticleName();
this.wikicode = await this.getWikicode( this.title );
// Don't run on redirect pages
const isRedirect = this.wikicode.match( /^#REDIRECT \[\[/i );
if ( isRedirect ) {
return false;
}
return true;
}
/**
* @return {string} pagename, including the namespace name, but with spaces replaced by underscores
*/
getArticleName() {
return this.mw.config.get( 'wgPageName' );
}
async getWikicode( title ) {
if ( !this.mw.config.get( 'wgCurRevisionId' ) ) {
return '';
}
// if page is deleted, return blank
let wikicode = '';
title = encodeURIComponent( title );
await this.$.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'
} );
return wikicode;
}
insertButton( title ) {
this.$( '#contentSub' ).before( `
<a style="display: inline-block; color: black; margin-top: 0.5em; border: 2px solid black; padding: 0.25em 3em; background-color: #FFDC00; font-size: 1.5em;" href="https://copyvios.toolforge.org/?lang=en&project=wikipedia&title=` + encodeURIComponent( title ) + `" target="_blank">
Copyvio check
</a>
` );
}
async isReviewed( pageId ) {
const api = new this.mw.Api();
const response = await api.get( {
action: 'pagetriagelist',
format: 'json',
page_id: pageId
} );
// no result
if ( response.pagetriagelist.result !== 'success' || response.pagetriagelist.pages.length === 0 ) {
return true;
// 1, 2, or 3
} else if ( parseInt( response.pagetriagelist.pages[ 0 ].patrol_status ) > 0 ) {
return true;
// 0
} else {
return false;
}
}
}
$( async () => {
await mw.loader.using( [ 'mediawiki.api' ], async () => {
await ( new DontForgetG12( mw, $ ) ).execute();
} );
} );
// </nowiki>