Repository navigation
Expand file tree
/
Copy pathTemplateSubstCount.js
More file actions
99 lines (83 loc) · 2.89 KB
/
Copy pathTemplateSubstCount.js
File metadata and controls
99 lines (83 loc) · 2.89 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
// <nowiki>
/*
When the user visits a template page, does an API search query to see how many times this template has been subst'd on user pages, then displays the count to the user.
Use case: Seeing how popular a template is before adding it to Twinkle.
*/
class TemplateSubstCount {
async execute() {
if ( !this.isTemplatePage() ) {
return;
}
const templateName = this.getTemplateName();
try {
if ( !( await this.hasTemplateMarker( templateName ) ) ) {
return;
}
const count = await this.getSubstitutionCount( templateName );
this.displayCount( count, templateName );
} catch {
mw.notify( 'Could not determine how many times this template was substituted.', {
type: 'error'
} );
}
}
isTemplatePage() {
return mw.config.get( 'wgNamespaceNumber' ) === 10;
}
getTemplateName() {
return mw.config.get( 'wgTitle' );
}
async hasTemplateMarker( templateName ) {
const api = new mw.Api();
const response = await api.get( {
action: 'query',
format: 'json',
formatversion: '2',
prop: 'revisions',
rvprop: 'content',
rvslots: 'main',
titles: `Template:${ templateName }`
} );
const page = response.query.pages[ 0 ];
const revision = page && page.revisions && page.revisions[ 0 ];
const mainSlot = revision && revision.slots && revision.slots.main;
const wikitext = mainSlot && mainSlot.content;
return typeof wikitext === 'string' &&
wikitext.toLowerCase().includes( `<!-- Template:${ templateName } -->`.toLowerCase() );
}
escapeRegex( text ) {
return text.replace( /[.*+?^${}()|[\]\\/"#@&<>~-]/g, '\\$&' );
}
buildInsourceSearch( templateName ) {
return `insource:/\\<!-- Template:${ this.escapeRegex( templateName ) } --\\>/i`;
}
async getSubstitutionCount( templateName ) {
const api = new mw.Api();
const response = await api.get( {
action: 'query',
format: 'json',
formatversion: '2',
list: 'search',
srsearch: this.buildInsourceSearch( templateName ),
srnamespace: '3',
srlimit: '0'
} );
return response.query.searchinfo.totalhits;
}
buildSearchUrl( templateName ) {
const search = this.buildInsourceSearch( templateName );
return `/w/index.php?search=${ encodeURIComponent( search ) }&title=Special%3ASearch&profile=advanced&fulltext=1&ns3=1&limit=1000`;
}
displayCount( count, templateName ) {
const formattedCount = new Intl.NumberFormat( 'en-US' ).format( count );
const countText = count === 1 ? '1 time' : `${ formattedCount } times`;
const searchUrl = this.buildSearchUrl( templateName );
const html = `This template has been subst'd on user talk pages <b>${ countText }</b>. <a href="${ searchUrl }">See pages.</a>`;
$( '#contentSub' ).before( `<div class="TemplateSubstCount" style="background-color: lightgray; margin-bottom: 5px;">${ html }</div>` );
}
}
$( async () => {
await mw.loader.using( [ 'mediawiki.api' ] );
await ( new TemplateSubstCount() ).execute();
} );
// </nowiki>