MediaWiki:AFC-submit-wizard.js: Difference between revisions

Content deleted Content added
No edit summary
update
Line 16:
$.when(
$.ready,
mw.loader.using([
'mediawiki.util', 'mediawiki.api', 'mediawiki.Title',
'mediawiki.widgets', 'oojs-ui-core', 'oojs-ui-widgets'
])
).then(function () {
 
Line 22 ⟶ 25:
return;
}
 
if (mw.util.getParamValue('withJS') !== 'MediaWiki:AFC-submit-wizard.js') {
return;
Line 57 ⟶ 60:
classes: ['container'],
items: [
draftLayout = new OO.ui.FieldLayout(draftInput = new OOmw.uiwidgets.TextInputWidgetTitleInputWidget({
value: (mw.util.getParamValue('draft') || '').replace(/_/g, ' '),
placeholder: 'Enter the draft title, begins with "Draft:" or "User:"'
}), {
Line 66 ⟶ 69:
helpInline: true
}),
 
isBLPLayout = new OO.ui.FieldLayout(isBLPInput = new OO.ui.CheckboxInputWidget({
selected: false
Line 94 ⟶ 97:
placeholder: 'Start typing to search for tags ...',
tagLimit: 10,
autocomplete: false,
// $overlay: $('body')[0]
}), {
label: 'WikiProject classification tags',
Line 142 ⟶ 146:
 
$('.mw-ui-button').parent().replaceWith(fieldset.$element);
 
// Load a JSON page from the wiki
function getJSONPage(page) {
return $.getJSON('https://en.wikipedia.org/w/index.php?title=' + encodeURIComponent(page) + '&action=raw&ctype=text/json');
}
 
// populate talk page tags for multi-select widget
var talkTagOptionsLoaded = $.Deferred();
$.getJSON('https://en.wikipedia.org/w/index.php?title=' + encodeURIComponentgetJSONPage("User:Theo's Little Bot/afchwikiproject.js") + '&action=raw&ctype=text/json').then(function (data) {
talkTagsInput.addOptions(Object.keys(data).map(function (k) {
return {
Line 152 ⟶ 161:
};
}));
talkTagsInput.addOptions([{data: 'WikiProject Biography', label: 'Biography'}]); // not in TLB list
talkTagOptionsLoaded.resolve();
});
 
// Get mapping of infoboxes with relevant WikiProjects
var ibxmapLoaded = $.Deferred();
getJSONPage('Wikipedia:WikiProject Articles for creation/Infobox WikiProject map.json').then(function (data) {
ibxmapLoaded.resolve(data);
});
 
Line 165 ⟶ 181:
lookupApi.abort(); // abort older API requests
 
ifvar drafttitle = (!draftInput.getValue().trim()) { // empty;
if (!drafttitle) { // empty
return;
}
Line 177 ⟶ 194:
talktext = null;
pagetext = null;
 
talkTagOptionsLoaded.then(function () {
talkTagsInput.setValue([]);
});
 
lookupApi.get({
"action": "query",
"prop": "revisions|description|info",
"titles": draftInput.getValue()drafttitle,
"rvprop": "content",
"rvslots": "main"
Line 216 ⟶ 237:
// set shortdesc in form
shortdescInput.setValue(page.description || '');
 
// guess wikiproject tags from infoboxes on the page
$.when(ibxmapLoaded, talkTagOptionsLoaded).then(function (ibxmap) {
var infoboxRgx = /\{\{([Ii]nfobox [^|}]*)/g,
wikiprojects = [],
match;
while (match = infoboxRgx.exec(pagetext)) {
var ibx = match[1].trim();
if (ibxmap[ibx]) {
wikiprojects = wikiprojects.concat(ibxmap[ibx]);
}
}
console.log('wikiprojects from infobox: ', wikiprojects);
console.log('setValue1:', talkTagsInput.getValue().concat(wikiprojects));
talkTagsInput.setValue(talkTagsInput.getValue().concat(wikiprojects));
});
 
// fill ORES topics
Line 226 ⟶ 263:
oresTopics = topics;
}
}, function () {
topicsLayout.toggle(true);
});
Line 233 ⟶ 270:
 
 
var talk = mw.Title.newFromText(draftInput.getValue()drafttitle);
if (!talk) {
return;
Line 254 ⟶ 291:
 
var existingWikiProjects = extractWikiProjectTagsFromText(talktext);
var existingTags = existingWikiProjects.map(function (e) {
return e.name;
});
talkTagOptionsLoaded.then(function () {
console.log('setValue2:', talkTagsInput.setValuegetValue().concat(existingTags));
talkTagsInput.setValue(talkTagsInput.getValue().concat(existingTags));
});
 
console.log(existingTags);
 
Line 273 ⟶ 311:
// ES2020 has optional chaining, but of course on MediaWiki we're still stuck with ES5
return json &&
json.enwiki &&
json.enwiki.scores &&
json.enwiki.scores[revid] &&
Line 321 ⟶ 359:
continue;
}
existingTags.push({
wikitext: match[0],
name: tag });
});
}
return existingTags;
Line 426 ⟶ 467:
// Process text of the talk page
var alreadyExistingWikiProjects = extractWikiProjectTagsFromText(talktext);
var alreadyExistingTags = alreadyExistingWikiProjects.map(function (e) {
return e.name;
});
var tagsToAdd = talkTagsInput.getValue().filter(function (tag) {
return alreadyExistingTags.indexOf(tag) === -1;
});
var tagsToRemove = alreadyExistingTags.filter(function (tag) {
return talkTagsInput.getValue().indexOf(tag) === -1;
});
 
tagsToRemove.forEach(function (tag) {
talktext = talktext.replace(new RegExp('\\{\\{\\s*' + tag + '\\s*(\\|.*?)?\\}\\}\\n?'), '');
});
Line 451 ⟶ 492:
"text": talktext,
"summary": 'Adding WikiProject tags using [[MediaWiki:AFC-submit-wizard.js|AFC-submit-wizard]])'
}).then(function (data) {
if (data.edit && data.edit.result === 'Success') {
talkStatusArea.setType('success');
Line 468 ⟶ 509:
});
 
}
 
});