Content deleted Content added
Trim out the code for generating the WikiProject List and the next random page, and used the pre-compiled list from User:Ahechtbot and the randomincategory tool on toolforge instead. |
var api = new mw.Api( { userAgent: 'draft-sorter/0.0.1' } ); |
||
(11 intermediate revisions by the same user not shown) | |||
Line 6:
( function ( $, mw ) { mw.loader.using( ["mediawiki.api", "jquery.chosen", "oojs-ui-core"], function () {
mw.loader.load( "mediawiki.ui.input", "text/css" );
var api = new mw.Api( { userAgent: 'draft-sorter/0.0.1' } );
if ( mw.config.get( "wgNamespaceNumber" ) !== 118 ) {
Line 26 ⟶ 27:
// Configure defaults
//var templateCache = mw.config.get("wgFormattedNamespaces")[2]+":"+mw.config.get("wgUserName")+"/Scripts/draft-sorter.json";
var templateCache = "
// Define the form
var form = $( "<div>" )
Line 46 ⟶ 47:
);
// Add the form to the page
form.insertAfter( "#
var select = $( "<select>" )
Line 74 ⟶ 75:
var wikiprojects = {};
action: "query",
titles: "Draft talk:" + mw.config.get( "wgTitle" ),
Line 117 ⟶ 118:
try {
listData = await
} catch (jsonerror) {
console.warn("Unable to fetch contents of " + listName + ":");
Line 136 ⟶ 137:
function nextDraft() {
// Special:RandomInCategory isn't random,
if (nextButton) {
nextButton.setLabel( "Loading..." ).setDisabled( true );
}
window.___location.href = "https://randomincategory.toolforge.org/Pending_AfC_submissions?draftsorttrigger=y&cmnamespace=118&cmtype=page&returntype=subject&server=" + mw.config.get("wgServerName");
}
Line 149 ⟶ 147:
$( "#draft-sorter-status" ).append( "<li>Suggested categories from <a href=\"https://www.mediawiki.org/wiki/ORES#Topic_routing\">ORES</a>:<ul id=\"draft-sorter-suggest\"></ul></li>" );
predicts.forEach( function(item) {
function addWithLink(p) {
$( "#draft-sorter-suggest" ).append(
$( "<li>" ).
$(
function() {
$( select ).val(
$( select ).val().concat( [ "WikiProject " + p ] )
).trigger("chosen:updated");
}
)
).append( ")" )
);
}
var singularItem = item.replace(/s$/, '');
if( !existingProjects.includes( "WikiProject " + item )
&& wikiprojects[item]
) { //Prediction matches a WikiProject and doesn't already exist
addWithLink(item);
} else if( singularItem != item
&& !existingProjects.includes( "WikiProject " + singularItem )
&& wikiprojects[singularItem]
) { //Singular form of prediction matches a WikiProject and doesn't exist
addWithLink(singularItem);
} else { //Prediction doesn't match a WikiProject or already exists
$( "#draft-sorter-suggest" ).append(
Line 189 ⟶ 181:
function getPredicts() {
var
if (lang.length == 1) return;
"Content-Type": "application/json",
"User-Agent": "draft-sorter (https://en.wikipedia.org/wiki/User:Ahecht/Scripts/draft-sorter.js)"
});
var revID = mw.config.get( "wgCurRevisionId" );
var model = (lang[0] == "en") ? "enwiki-drafttopic" : "outlink-topic-model";
var postBody = JSON.stringify({
"rev_id": revID,
"lang": lang[0],
"page_title": mw.config.get("wgPageName")
});
fetch(liftWingExternalEndpoint + model + ":predict", {
method: "POST",
headers: new Headers({
"Content-Type": "application/json",
"User-Agent": "draft-sorter (https://en.wikipedia.org/wiki/User:Ahecht/Scripts/draft-sorter.js)"
}),
body: postBody
}).then(response => response.json()).then(data => {
var prediction = [];
var dbName = mw.config.get("wgDBname");
if(data && data[dbName] && data[dbName].scores &&
data[dbName].scores[revID] &&
data[dbName].scores[revID].drafttopic &&
data[dbName].scores[revID].drafttopic.score &&
data[dbName].scores[revID].drafttopic.score.prediction) {
prediction = data[dbName].scores[revID].drafttopic.score.prediction;
} else if (data && data.prediction && data.prediction.results) {
data.prediction.results.forEach( p => {
if (p && p.topic) prediction.push(p.topic);
} );
}
if (prediction.length) {
console.log("Got ORES response! Raw predictions:");
console.log(prediction);
prediction.forEach( function (item) {
var last = item.split(".")[item.split(".").length-1];
var penultimate = item.split(".")[item.split(".").length-2];
if ( last.substr(-1) == "*" ) {
// Filter out redundant starred predictions
if (prediction.find(element => (
element.split(".")[element.split(".").length-1] != last &&
element.split(".")[element.split(".").length-2] == penultimate
) ) ) {
console.log("Prediction \"" + last + "\" excluded.");
last = null;
} else {
last = penultimate;
}
}
if ( wikiprojects[last] ) {
// WikiProject found, no need to try splitting
} else if ( last ) {
// Can't find wikiProject, try splitting
var
+ splitLast[i].slice(1);
}
console.log("Filtered predictions:");
} else {
console.warn("Error finding predictions in ORES response:");
console.warn(data);
}
} ).catch( e => console.warn("Error retrieving ORES data: " + e) );
return;
}
Line 329 ⟶ 347:
function editTalk(text, prefix) {
var params = {
action: "edit", section: "0",
title: "Draft talk:" + mw.config.get( "wgTitle" ),
summary: "Tagging draft: +" + newTags.join(", +") +
" ([[User:Ahecht/Scripts/draft-sorter
};
params[prefix + "text"] = text;
if ( data && data.edit && data.edit.result && data.edit.result === "Success" ) {
showStatus( "Edit saved successfully! (" )
Line 359 ⟶ 377:
}
action: "query",
titles: "Draft talk:" + mw.config.get( 'wgTitle' ),
Line 375 ⟶ 393:
var talkText = data.parse.wikitext["*"];
if (typeof(talkText) == "string") {
var pattern = /(\{\{\s*(?:Wiki[ _]?Project[ _]?banners?[ _]?shell(?:\/redirect)?|(?:(?:WP)?[ _]?Banner|(?:Wiki)?Project|Scope)[ _]?shell|Multiple[ _]wikiprojects|WikiProject[ _]?Banners?|WPBS?)\s*\|(?:\s*[a-z1]+\s*=[^\{\}]*)*\s*(?:\\n)*?)/im;
if (talkText.search(pattern) >= 0) {
newText = talkText.replace( pattern, ("$1" + newText) );
|