-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTemplateTaxonomyCreator.js
More file actions
357 lines (326 loc) · 11.6 KB
/
TemplateTaxonomyCreator.js
File metadata and controls
357 lines (326 loc) · 11.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// <nowiki>
/*
Adds some buttons (Cite iNaturalist, Cite WoRMS, Cite NCBI, Cite Other) to the edit screen of Template:Taxonomy subpages, and pre-fills it assuming it is a genus. Helpful for quickly creating Template:Taxonomy subpages, which is useful when getting {{Speciesbox}} to work with new species.
*/
// TODO: fix bug where button doesn't work sometimes when clicked
// TODO: if no talk page, create talk page and add correct WikiProject templates: amphibians and reptiles, animals, arthropods, beetles, birds, diptera, fishes, fungi, gastropods, insects, lepidoptera, mammals, marine life, microbiology, paleontology, plants
class TemplateTaxonomyCreator {
constructor( mw, $ ) {
this.mw = mw;
// eslint-disable-next-line no-jquery/variable-pattern
this.$ = $;
}
async addLinksAndListeners() {
const title = this.mw.config.get( 'wgPageName' );
if ( !title.startsWith( 'Template:Taxonomy/' ) ) {
return;
}
// In the title, grab everything after the slash
const taxon = title.split( '/' ).pop();
// TODO: refactor to use JSON, and a loop to add the table rows
// last to first
let html = `
<style>
#TemplateTaxonomyCreator {
margin: 1em 0;
}
#TemplateTaxonomyCreator, #TemplateTaxonomyCreator th, #TemplateTaxonomyCreator td {
border: 1px solid black;
border-collapse: collapse;
padding: 0 5px;
background-color: #ccffff;
}
</style>
<table id="TemplateTaxonomyCreator">
`;
const websites = [
{
name: 'Catalogue of Life',
id: 'taxonomy-creator-catalogue-of-life',
description: 'All',
urlLeft: 'https://www.catalogueoflife.org/data/search?facet=rank&facet=issue&facet=status&facet=nomStatus&facet=nameType&facet=field&facet=authorship&facet=extinct&facet=environment&limit=50&offset=0&q=',
urlMiddle: taxon,
urlRight: '&sortBy=taxonomic'
},
{
name: 'WoRMS',
id: 'taxonomy-creator-worms',
description: 'Marine animals, gastropods (snails and slugs)',
urlLeft: 'https://www.marinespecies.org/aphia.php?p=taxlist&searchpar=0&tComp=begins&tName=',
urlMiddle: taxon,
urlRight: '&action=search&rSkips=0&adv=0'
},
{
name: 'NCBI',
id: 'taxonomy-creator-ncbi',
description: 'Protozoa',
urlLeft: 'https://www.ncbi.nlm.nih.gov/taxonomy/?term=',
urlMiddle: taxon,
urlRight: ''
},
{
name: 'LPSN',
id: 'taxonomy-creator-lpsn',
description: 'Bacteria',
urlLeft: 'https://lpsn.dsmz.de/search?word=',
urlMiddle: taxon,
urlRight: ''
},
{
// AJAX-ish/App-ish website, can't custom search through URL
name: 'Index Fungorum',
id: 'taxonomy-creator-index-fungorum',
description: 'Fungi',
urlLeft: 'http://www.indexfungorum.org/names/names.asp',
urlMiddle: '',
urlRight: ''
},
{
// AJAX-ish/App-ish website, can't custom search through URL, also doesn't give a unique URL for the genus
name: 'MycoBank',
id: 'taxonomy-creator-mycobank',
description: 'Fungi',
urlLeft: 'https://www.mycobank.org/page/Basic%20names%20search',
urlMiddle: '',
urlRight: ''
},
{
name: 'Mindat.org',
id: 'taxonomy-creator-mindat',
description: 'Protozoa, Parasites',
urlLeft: 'https://www.mindat.org/search.php?search=',
urlMiddle: taxon,
urlRight: '#autoanchor1'
},
{
name: 'Other',
id: 'taxonomy-creator-other',
description: '',
urlLeft: '',
urlMiddle: '',
urlRight: ''
}
];
for ( const website of websites ) {
html += '<tr><td>';
if ( website.name !== 'Other' ) {
html += `<a href="${ website.urlLeft }${ website.urlMiddle }${ website.urlRight }" target="_blank">Search ${ website.name }</a>`;
}
html += '</td>';
html += `<td><a id="${ website.id }">Cite ${ website.name }</a></td>`;
html += `<td>${ website.description }</td>`;
html += '</tr>';
}
html += '</table>';
this.$( '.editpage-head-copywarn' ).after( html );
// Listeners have to be added after the elements are placed on the page
for ( const website of websites ) {
this.$( '#' + website.id ).on( 'click', () => {
this.promptThenAddWikicode( website.id );
} );
}
}
promptThenAddWikicode( websiteId ) {
const englishRank = prompt( "Enter this taxa's rank. For example, genus, tribe, or subfamily." );
const latinRank = this.englishToLatin( englishRank );
const needsItalics = this.needsItalics( latinRank );
let parent = prompt( "Enter this taxa's parent. For example, the parent of the genus homo is Hominina." );
// capitalize just the first letter
parent = parent.charAt( 0 ).toUpperCase() + parent.slice( 1 );
const url = prompt( 'Enter the URL. For example, if this is a citation to Catalogue of Life, enter the catalogueoflife.org URL that has info on this taxa.' );
let wormsId, catalogueOfLifeId;
if ( websiteId === 'taxonomy-creator-worms' ) {
wormsId = url.match( /id=(\d+)$/ )[ 1 ] || '';
} else if ( websiteId === 'taxonomy-creator-catalogue-of-life' ) {
catalogueOfLifeId = url.match( /\/([^/]+)$/ )[ 1 ] || '';
}
// TODO: extract the ID from the Catalogue of Life URL
// CC BY-SA 4.0, Mitch3091, https://stackoverflow.com/a/38148759/3480193
const date = new Date().toISOString().slice( 0, 10 ); // yyyy-mm-dd
// In the title, grab everything after the slash
const pageName = this.mw.config.get( 'wgPageName' );
const taxon = pageName.split( '/' ).pop();
let wikicode =
`{{Don't edit this line {{{machine code|}}}
|rank=${ latinRank }
|link={{subst:#titleparts:{{subst:PAGENAME}}|2|2}}
|parent=${ parent }
|refs=`;
// TODO: if title has parentheses, use the format |link=Example (parentheses)|Example
// TODO: italics only for genus
const citeTitle = needsItalics ? `''${ taxon }''` : taxon;
const refs = {
// Old: {{Cite web |access-date=${ date } |url=${ url } |title=''${ taxon }'' |website=[[Catalogue of Life]]}}`,
'taxonomy-creator-catalogue-of-life':
`{{Catalogue of Life |id=${ catalogueOfLifeId } |title=${ citeTitle } |access-date=${ date }}}`,
'taxonomy-creator-worms':
`{{Cite WoRMS |title=''${ citeTitle }'' |id=${ wormsId } |access-date=${ date }}}`,
'taxonomy-creator-ncbi':
`{{Cite web |access-date=${ date } |url=${ url } |title=''${ citeTitle }'' |website=[[NCBI]]}}`,
'taxonomy-creator-lpsn':
`{{Cite web |access-date=${ date } |url=${ url } |title=''${ citeTitle }'' |website=[[LPSN]]}}`,
'taxonomy-creator-mycobank':
`{{Cite web |access-date=${ date } |url=${ url } |title=''${ citeTitle }'' |website=[[MycoBank]]}}`,
'taxonomy-creator-index-fungorum':
`{{Cite web |access-date=${ date } |url=${ url } |title=''${ citeTitle }'' |website=[[Index Fungorum]]}}`,
'taxonomy-creator-mindat':
`{{Cite web |access-date=${ date } |url=${ url } |title=''${ citeTitle }'' |website=[[Mindat.org]]}}`,
'taxonomy-creator-other':
`{{Cite web |access-date=${ date } |url=${ url } |title=''${ citeTitle }'' |website=}}`
};
wikicode += refs[ websiteId ];
wikicode +=
`
}}
`;
// I did this in a more complicated way than normal, to enable support for CodeMirror. https://www.mediawiki.org/wiki/Extension:CodeMirror#Using_jQuery.textSelection
const $textarea = this.$( '#wpTextbox1' );
$textarea.textSelection( 'getContents' );
$textarea.textSelection( 'setContents', wikicode );
// watchlist it
this.$( '#wpWatchthis' ).prop( 'checked', true );
const isCreatingPage = this.$( '#firstHeading' ).html().startsWith( 'Creating' );
if ( isCreatingPage ) {
this.$( '#wpSummary' ).val( 'create ([[User:Novem Linguae/Scripts/TemplateTaxonomyCreator|TemplateTaxonomyCreator]])' );
} else { // editing
this.$( '#wpSummary' ).val( 'edit ([[User:Novem Linguae/Scripts/TemplateTaxonomyCreator|TemplateTaxonomyCreator]])' );
}
}
needsItalics( latinRank ) {
// Italicize anything genus or below
return [ 'genus', 'subgenus', 'sectio', 'subsectio', 'series', 'subseries' ].includes( latinRank.toLowerCase().trim() );
}
englishToLatin( rank ) {
// https://en.wikipedia.org/wiki/Wikipedia:Automated_taxobox_system/ranks#Rank_table
const rankMap = {
alliance: 'alliance',
'basic shell type': 'basic shell type',
branch: 'branch',
clade: 'cladus',
'clade?': 'possible clade',
class: 'classis',
cohort: 'cohort',
division: 'divisio',
domain: 'domain',
epifamily: 'epifamilia',
family: 'familia',
form: 'forma',
'form taxon': 'form taxon',
genus: 'genus',
grade: 'gradus',
grandorder: 'grandordo',
group: 'virus group',
hyperfamily: 'hyperfamilia',
ichnoclass: 'ichnoclassis',
ichnocohort: 'ichnocohort',
ichnodivision: 'ichnodivisio',
ichnofamily: 'ichnofamilia',
ichnogenus: 'ichnogenus',
ichnograndorder: 'ichnograndordo',
ichnoinfraclass: 'ichnoinfraclassis',
ichnoinfradivision: 'ichnoinfradivisio',
ichnoinfraorder: 'ichnoinfraordo',
ichnolegion: 'ichnolegio',
ichnomagnorder: 'ichnomagnordo',
ichnomicrorder: 'ichnomicrordo',
ichnoorder: 'ichnoordo',
ichnoparvorder: 'ichnoparvordo',
ichnospecies: 'ichnospecies',
'ichnostem-group': 'ichnostem-group',
ichnosubclass: 'ichnosubclassis',
ichnosubdivision: 'ichnosubdivisio',
ichnosubfamily: 'ichnosubfamilia',
ichnosublegion: 'ichnosublegio',
ichnosuborder: 'ichnosubordo',
ichnosuperclass: 'ichnosuperclassis',
ichnosupercohort: 'ichnosupercohort',
ichnosuperfamily: 'ichnosuperfamilia',
ichnosuperorder: 'ichnosuperordo',
'informal group': 'informal group',
infraclass: 'infraclassis',
infrakingdom: 'infraregnum',
infralegion: 'infralegio',
infraorder: 'infraordo',
infraphylum: 'infraphylum',
infratribe: 'infratribus',
kingdom: 'regnum',
legion: 'legio',
magnorder: 'magnordo',
microphylum: 'microphylum',
microrder: 'micrordo',
mirorder: 'mirordo',
morphotype: 'morphotype',
nanophylum: 'nanophylum',
nanorder: 'nanordo',
node: 'node',
ooclass: 'ooclassis',
oocohort: 'oocohort',
oofamily: 'oofamilia',
oogenus: 'oogenus',
oomagnorder: 'oomagnordo',
oorder: 'oordo',
oospecies: 'oospecies',
oosubclass: 'oosubclassis',
oosubgenus: 'oosubgenus',
oosubspecies: 'oosubspecies',
oosupercohort: 'oosupercohort',
oosuperorder: 'oosuperordo',
order: 'ordo',
parafamily: 'parafamilia',
parvclass: 'parvclassis',
parvorder: 'parvordo',
phylum: 'phylum',
plesion: 'plesion',
'plesion-group': 'plesion-group',
realm: 'realm',
section: 'sectio',
series: 'series',
serotype: 'serotype',
species: 'species',
'species complex': 'species complex',
'species group': 'species group',
'species subgroup': 'species subgroup',
'stem group': 'stem group',
strain: 'strain',
subclass: 'subclassis',
subcohort: 'subcohort',
subdivision: 'subdivisio',
subfamily: 'subfamilia',
subgenus: 'subgenus',
subkingdom: 'subregnum',
sublegion: 'sublegio',
suborder: 'subordo',
subphylum: 'subphylum',
subsection: 'subsectio',
subseries: 'subseries',
subspecies: 'subspecies',
subterclass: 'subterclassis',
subtribe: 'subtribus',
superclass: 'superclassis',
supercohort: 'supercohort',
superdivision: 'superdivisio',
superdomain: 'superdomain',
superfamily: 'superfamilia',
superkingdom: 'superregnum',
superlegion: 'superlegio',
superorder: 'superordo',
superphylum: 'superphylum',
supersection: 'supersectio',
supertribe: 'supertribus',
'total group': 'total group',
tribe: 'tribus',
variety: 'varietas',
virus: 'virus'
};
rank = rank.toLowerCase().trim();
if ( rank in rankMap ) {
return rankMap[ rank ];
}
// return original if not found
return rank;
}
}
$( async () => {
await ( new TemplateTaxonomyCreator( mw, $ ) ).addLinksAndListeners();
} );
// </nowiki>