importScript('User:Smith609/toolbox.js');
/* My own attempt to write a gadget */
// Import the jQuery dialog plugin before starting the rest of this script
mw.loader.using(['jquery.ui'], function() {
function renderQuickRCDialog( pageLinks ) {
alert('renderQuickRCDialog function should not have been called.');
}
var templateRegEx = /no footnotes|New unreviewed artice|dubious|expand|histinfo|Autobiography|Contradictory|Controversial|Factual accuracy|Neutrality|Notability|Original research|Possible neologisms|Suspected hoax|Unencyclopedic|Verifiability|Citation |multiple issues|copypaste|cleanup|copy edit|attention|abbreviations|advert|buzzwords|crystal|debate|editorial|essay|resume|manual|obituary|news release|peacock|repetition|research paper|review|story|technical|tone|verbosity|condense|duplication|trivia|very long|too many see alsos|spacing|lead|copypaste|external links|MOS|non-free|spam|context|generalize|detailed|expand|lacking|expert-subject|update|confusing|contradict|incomprehensible|misleading|unclear|unfocused|bad summmary|notability|importance|cherry picked|coat rack|COI|disputed|globalize|geographical imbalance|partisan|POV|recentism|unbalanced|undue|cite check|medref|one source|original research|primary sources|refimprove|speculation|religious text primary|self-published|third-party|unreferenced|unreliable|ibid|needs|needed|requested/;
function addInfoToLinks() {
var myPageLinks = [];
// var myTitles = [];
var $listQuery = [ ];
var api = new mw.Api();
$searchResults = $("ul.mw-search-results");
console.log($searchResults);
$anchors = $searchResults.find("a");
// $anchors.length = 1; //DEBUG TO SAVE TIME
console.log($anchors);
//Traverse
$anchors.each(
function(index) {
/* It's a separate get for each one because otherwise aspects won't load properly*/
var result = {
needImage: false,
articleClass: null,
warnings: [ ],
projects: { },
protected: null,
comment: "",
toString: function () {
//Projects are more finickity.
var projectsList = [ ];
for(var projectName in this.projects ) {
projectsList.push(projectName);
}
return(
(this.articleClass ? ("Class " + this.articleClass + " ") : "" ) +
(this.needImage ? "[needs image] " : "" ) +
(this.warnings.length ?
( "[" +
this.warnings.join(" ")
+ "] "
)
: "") +
(projectsList.length?
( " WikiProject(s): " +
projectsList.join(',')
) + ". "
: "" ) +
(this.protected? "Some edit protection in place. " : "") +
(this.comment ? this.comment : " " )
);
},
categoryMatches: {
"Featured articles" : function() {
result.articleClass="FA";
},
"Good articles" : function() {
result.articleClass="GA";
},
"stub articles" : function() {
result.articleClass="stub";
},
"GA-Class" : function(){result.articleClass="GA";},
"FA-Class" : function(){result.articleClass="FA";},
"A-Class" : function(){
/* Because A-class also matches GA & FA, deprioritised match*/
if ( ! result.articleClass ) {
result.articleClass="A";
}
},
"B-Class" : function(){result.articleClass="B";},
"C-Class" : function(){result.articleClass="C";},
"stub-class" : function(){result.articleClass="stub";},
"start-class" : function(){result.articleClass="start";},
"Stub-class" : function(){result.articleClass="stub";},
"Start-class" : function(){result.articleClass="start";},
"Stub-Class" : function(){result.articleClass="stub";},
"Start-Class" : function(){result.articleClass="start";},
"WikiProject Medicine":function(){
result.projects['Medicine'] = true;
},
"WikiProject History of Science":function(){
result.projects['History of Science'] = true;
}
}
};
function checkCategories( category ) {
// console.log("checking category ", category);
for ( var match in result.categoryMatches ) {
if (category.title.indexOf(match)>-1) {
console.log("Match");
result.categoryMatches[match]();
console.log("Result now ", result);
} else {
// console.log("Not");
}
}
}
function checkTemplates( template ) {
// console.log("checking template", template);
if (templateRegEx.test(template.title)) {
console.log("Found one!", template);
result.warnings.push( template.title);
}
}
function returnResult( ) {
console.log ('Final result=',result);
var resultDiv = document.createElement("div");
var text = document.createTextNode(result.toString());
resultDiv.appendChild(text);
console.log("anchorNode", anchorNode, 'text=', text);
resultDiv.style.color = 'green';
resultDiv.style.fontFamily = 'courier';
anchorNode.parentNode.appendChild(resultDiv);
//return "<em> This is being modified </em>";
}
var anchorNode = this;
api.get( {
action: 'query',
format: 'json',
titles: this.title,
prop: ['categories', 'flagged', 'info', 'pageimages', 'templates'],
inprop: ['protection', 'talkid'],
tllimit: 100,
cllimit: 500
} ).done( function ( data ) {
console.log( "data passed from API", data );
if ( Boolean(data.warnings) ) {
return "<strong>" + data.warnings.main['*'] + "</strong>";
}
var targetPage;
var pageCount = 0;
var talkId = null;
for ( var id in data.query.pages ) {
pageCount++;
var page = data.query.pages[id];
if (pageCount > 1) {
alert('Multiple pages returned');
}
console.log("Page=",page);
// Image?
if ( ! Boolean (page.pageimage) ) {
result.needImage = true;
}
// Any interesting categories?
page.categories.forEach( checkCategories );
page.templates.forEach( checkTemplates );
if ( page.talkid ) {
talkId = page.talkid;
}
if (page.protection.length) {
console.log("PROTECTION", page.protection);
result.protected = true;
}
}
/* Talk Page */
if ( ! talkId ) {
console.log("No talkid", talkId);
returnResult();
return;
}
console.log('COMMENCING TALK PAGE');
api.get( {
action: 'query',
format: 'json',
pageids: talkId,
prop: ['categories', 'flagged', 'info', 'pageimages', 'templates'],
inprop: ['protection', 'talkid'],
tllimit: 100,
cllimit: 500
} ).done( function ( talkData ) {
pageCount = 0;
console.log("talkData", talkData);
for ( var talkId in talkData.query.pages ) {
pageCount++;
var talkPage = talkData.query.pages[talkId];
if (pageCount > 1) {
alert('Multiple pages returned');
}
console.log("Talk Page=",talkPage);
// Any interesting categories?
talkPage.categories.forEach( checkCategories );
// page.templates.forEach( checkTemplates );
}
// RETURN RESULT
returnResult();
});
} );
// This code is irrelevant, and isn't what returns from the function.
// But how do we pause until it is done?
return "TEST";
}
);
// Fetch recent changes from the API by one of jQuery's AJAX functions
if (null) jQuery.getJSON(
mw.util.wikiScript( 'api' ),
{
'format': 'json',
'action': 'query',
// 'generator': 'xxx',
// 'list': 'recentchanges',
'titles': myTitles.join("|"),
// 'rclimit' : 25
'prop' : 'categories|flagged|info|pageimages|templates'
},
function( data ) {
console.log(data);
// alert(JSON.stringify(data) );
// Build a unique array of links, using the mw.html library to format them.
/*$.
each ( data.query.recentchanges , function( index , rc ) {
// Don't link to this title if we've seen this title already
if ( $.inArray( rc.title, myTitles ) === -1 ) {
myPageLinks.push(
mw.html.element(
'a', { href: mw.util.getUrl( rc.title ) }, rc.title
)
);
}
myTitles.push( rc.title );
} ) ;
renderQuickRCDialog( myPageLinks );
*/
}
);
}
$(document).ready( function() {
// Add a link to the toolbox
var link = mw.util.addPortletLink(
'p-tb',
'#',
"Add info to links",
't-prettylinkwidget',
'TEST COMMON JS',
'/',
'#t-whatlinkshere'
);
// Create a jQuery object for this link so that we get
// to use jQuery awesomeness like .click() for binding functions to events
// and methods like e.preventDefault();
$(link).click( function( e ) {
// Avoid the browser going to '#'
e.preventDefault();
// Initiate quickRC!
addInfoToLinks();
});
});
});