User:Yamaha5/linkinfo.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Yamaha5/linkinfo. |
/*jslint browser: true, regexp: true, unparam: true, indent: 2*/
/*global jQuery: false, mw: false*/
/*
+ Default option
+ Size Query
+ Last User Query
+ Last edit time Query
**********************************************************************************
Options: put this variable to your /common.js. each item sets to 0 doesn't show! *
var pageInfoConf = {
'disambigQuery': true,
'firstUserQuery': true,
'lastUserQuery': true,
'firstTimeQuery': true,
'lastTimeQuery': true,
'redirectQuery': true,
'sizeQuery': true,
'redQuery': true,
'wikidataLink': true,
'wikidataWikiCount': true,
'wikidataP31': true,
'Score': true,
'LinkHits': true,
'NoTalk': true,
'TargetRedirectPage': true,
'TargetSubject': true,
'TargetRemoveCache': true
};
**********************************************************************************
*/
(function ($, mw) {
'use strict';
function addInfoToNode(node, info, color) {
$(node).after('<span class="infoAddedcontent">[<span style="color: ' + color + ';">' + info + '</span>]</span>');
}
var conf = {
'disambigQuery': true,
'firstUserQuery': true,
'lastUserQuery': true,
'firstTimeQuery': true,
'lastTimeQuery': true,
'redirectQuery': true,
'sizeQuery': true,
'redQuery': true,
'wikidataLink': true,
'wikidataWikiCount': true,
'wikidataP31': true,
'Score': true,
'LinkHits': true,
'NoTalk': true,
'TargetRedirectPage': true,
'TargetSubject': true,
'TargetRemoveCache': true
}, api = new mw.Api();
$.extend(conf, window.pageInfoConf);
function getSelectedTextLinks() {
// borrowed from: http://stackoverflow.com/questions/4220478 :)
var selection,
selectionAncestor,
range,enableTargetSubject, enableTargetRemoveCache,
allWithinRangeParent,
allSelected,
i,
el;
// if `window.getSelection` is not defined (on IE) return nothing.
if (window.getSelection === undefined) {
return [];
}
selection = window.getSelection();
// if nothing is selected, return empty array
if (selection.isCollapsed) {
return [];
}
range = selection.getRangeAt(0);
selectionAncestor = range.commonAncestorContainer;
if (selectionAncestor.getElementsByTagName === undefined) { // if it is not a formal HTML selection
return [];
}
allWithinRangeParent = selectionAncestor.getElementsByTagName('a');
allSelected = [];
for (i = 0; allWithinRangeParent[i] !== undefined; i += 1) {
el = allWithinRangeParent[i];
// The second parameter says to include the element
// even if it's not fully selected
if (selection.containsNode(el, true)) {
allSelected.push(el);
}
}
return allSelected;
}
function getArticleLinks() {
var allSelected = getSelectedTextLinks();
if (allSelected.length === 0) {
return $('#bodyContent a');
}
return allSelected;
}
function parseUrl(url) {
if (url === undefined) {
return undefined;
}
var match = url.match(/\/wiki\/([^#]*)/);
if (match === null) {
match = url.match(/\/w\/index\.php\?title=([^&#]*).*redlink=1/);
}
if (match !== null) {
return decodeURI(match[1]); // returns () matched text
}
return undefined;
}
function getLinkTitle(link) {
return parseUrl(link.href);
}
function firstUserAndTime(data) {
var res;
$.each(data.query.pages, function (key, value) { // for retrieving first object index
if (value.revisions !== undefined) {
res = [value.revisions[0].user, value.revisions[0].timestamp];
}
});
return res;
}
function lastUserAndTime(data) {
var res;
$.each(data.query.pages, function (key, value) { // for retrieving first object index
if (value.revisions !== undefined) {
res = [value.revisions[0].user, value.revisions[0].timestamp];
}
});
return res;
}
function pageSize(data) {
var res;
$.each(data.query.pages, function (key, value) { // for retrieving first object index
if (value !== undefined) {
res = value.length;
}
});
return res;
}
function detectHasTemplate(data) {
var hasTemplate = false;
$.each(data.query.pages, function (key, value) { // for retrieving first object index
hasTemplate = value.templates === undefined ? false : true;
});
return hasTemplate;
}
function infoAdder(infoType) {
var enableNoTalk = $('#enableNoTalk').is(':checked'),
enableDisambigDetection = $('#enableDisambigDetection').is(':checked'),
enableFirstUserName = $('#enableFirstUserName').is(':checked'),
enableFirstUserTime = $('#enableFirstUserTime').is(':checked'),
enableLastUserName = $('#enableLastUserName').is(':checked'),
enableLastUserTime = $('#enableLastUserTime').is(':checked'),
enablePageSize = $('#enablePageSize').is(':checked'),
enableRedirectDetection = $('#enableRedirectDetection').is(':checked'),
enableRedLinkDetection = $('#enableRedLinkDetection').is(':checked'),
enableWikidataLink = $('#enableWikidataLink').is(':checked'),
enableWikidataWikiCount = $('#enableWikidataWikiCount').is(':checked'),
enableWikidataP31 = $('#enableWikidataP31').is(':checked'),
enableScore = $('#enableScore').is(':checked'),
enableLinkHits = $('#enableLinkHits').is(':checked'),
enableTargetRedirectPage = $('#enableTargetRedirectPage').is(':checked'),
enableTargetSubject = $('#enableTargetSubject').is(':checked'),
enableTargetRemoveCache = $('#enableTargetRemoveCache').is(':checked'),
links = getArticleLinks();
if (enableRedLinkDetection) {
$(links).filter('a.new').each(function (key, value) {
// value.innerHTML = value.innerHTML + ' @';
addInfoToNode(value, 'ناموجود', 'red');
});
}
$(links).each(function (key, value) {
var jValue = $(value),
href = jValue.attr('href'),
name;
if (href === undefined) {
return;
}
name = getLinkTitle(value);
if (name === undefined) {
return;
}
if (enableNoTalk){
name=name.replace(/بحث\:/g, "");
}
if (enableRedirectDetection && jValue.is('a.mw-redirect')) {
addInfoToNode(value, 'تغییر مسیر', 'green');
}
if (enableDisambigDetection && jValue.is('a.mw-disambig')) {
addInfoToNode(value, 'ابهام\u200cزدایی', 'aqua');
}
if (enableTargetRedirectPage) {
if (mw.config.get('wgAction') === 'edit') {
var links = $('#wpTextbox1').val().match(/\[\[.*?\]\]/g);
var i;
for (i = 0; i < links.length; i = i + 1) {
var title = links[i].replace(/\[\[:?([^\]\|]*)\|?.*?\]\]/g, "$1");
api.get({ action: 'query', titles: title, redirects: '', format: 'json' }).then(function (data) {
var redirect_title = data.query.redirects;
if (typeof redirect_title !== typeof undefined) {
$('#wpTextbox1').html($('#wpTextbox1').html().replace('[[' + redirect_title[0].from + ']]', '[[' + redirect_title[0].to + ']]'));
}
});
}
}/*else{
alert('اصلاح تغییرمسیر فقط در حالت ویرایش کار میکند! لطفاً دکمهٔ ویرایش را بزنید بعد از ابزار استفاده کنید.')
}*/
}
if (enableTargetSubject) {
api.get({
format: 'json',
action: 'query',
titles: name,
prop:'categories'
}).then(function (data) {
var categories = data.query.pages[Object.keys(data.query.pages)[0]].categories;
var articleCatz = [];
for (i in categories) {
if (categories[i].title) {
var cat_a = categories[i].title;
if (cat_a.search('رده:مقالههای ویکیپروژه نسخه آفلاین درباره') > -1) {
articleCatz.push(cat_a.replace('رده:مقالههای ویکیپروژه نسخه آفلاین درباره', '').trim());
}
}
}
if(articleCatz.length > 0){
addInfoToNode(value, articleCatz.join('، '), '#99f');
}
});
}
if (enableTargetRemoveCache) {
api.get({
action: 'query',
prop: 'revisions',
titles: name,
rvprop: 'content',
format: 'json'
}).then(function (data) {
return $.map(data.query.pages, function (value) {
return value;
})[0].revisions[0]['*'];
}).then(function (content) {
if (content !== undefined) {
new mw.Api().post( { action: 'purge', forcelinkupdate: 1, titles: name } )
addInfoToNode(value, '✓', 'green');
}
})
}
if (enableFirstUserName || enableFirstUserTime) {
api.get({
action: 'query',
prop: 'revisions',
titles: name,
rvlimit: '1',
rvprop: 'user|timestamp',
rvdir: 'newer'
}).then(function (data) {
var fut = firstUserAndTime(data);
if (fut !== undefined) {
if (enableFirstUserName) {
addInfoToNode(value, 'کاربر:' + fut[0], 'gray');
}
if (enableFirstUserTime) {
addInfoToNode(value, 'در ' + new Date(fut[1]).toDateString(), 'orange');
}
}
});
}
if (enablePageSize) {
api.get({
action: 'query',
prop: 'info',
titles: name
}).then(function (data) {
var size = pageSize(data),
num;
if (size !== undefined) {
num = size / 1000;
if (num < 4) {
addInfoToNode(value, 'کیلوبایت ~ ' + num, '#D715C1');
} else if (10 > num && num > 4) {
addInfoToNode(value, 'کیلوبایت ~ ' + num, '#039839');
} else if (30 > num && num > 10) {
addInfoToNode(value, 'کیلوبایت ~ ' + num, 'red');
} else if (num > 30) {
addInfoToNode(value, 'کیلوبایت ~ ' + num, '#FF6B00');
}
}
});
}
if (enableLastUserName || enableLastUserTime) {
api.get({
action: 'query',
prop: 'revisions',
titles: name,
rvlimit: '1',
rvprop: 'user|timestamp',
rvdir: 'older'
}).then(function (data) {
var Lut = lastUserAndTime(data);
if (Lut !== undefined) {
if (enableLastUserName) {
addInfoToNode(value, 'کاربر:' + Lut[0], 'gray');
}
if (enableLastUserTime) {
addInfoToNode(value, 'در ' + new Date(Lut[1]).toDateString(), 'orange');
}
}
});
}
if (enableScore || enableLinkHits) {
var newname=name.replace(/بحث\:/g, "");
if (newname!==name){
newname=name
}else{
newname='بحث:' + name
}
api.get({
action: 'query',
prop: 'revisions',
titles: newname,
rvprop: 'content',
format: 'json'
}).then(function (data) {
return $.map(data.query.pages, function (value) {
return value;
})[0].revisions[0]['*'];
}).then(function (content) {
if (content !== undefined) {
if (enableScore) {
var score = content.match(/\|امتیاز\=(.*?)\|/g)[0]
score = score.replace(/\|امتیاز\=/g, "").replace(/\|/g, "")
addInfoToNode(value, 'اهمیت=' + score, 'gray');
}
if (enableLinkHits) {
var linkhits = content.match(/\|تعداد بازدید\=(.*?)\|/g)[0]
linkhits = linkhits.replace(/\|تعداد بازدید\=/g, "").replace(/\|/g, "")
addInfoToNode(value, 'بازدید=' + linkhits, 'orange');
}
}
})
}
if (enableWikidataLink || enableWikidataWikiCount || enableWikidataP31) {
$.ajax({
url: '//www.wikidata.org/w/api.php',
data: {
action: 'wbgetentities',
sites: value.href.replace(/^.*\/\/([^\.]*)\.(wiki)pedia\.org.*$/, "$1$2"),
prop: 'revisions',
titles: name,
format: 'json'
},
dataType: 'jsonp'
}).then(function (data) {
var itemId = Object.keys(data.entities)[0];
if (itemId === '-1') {
return;
}
if (enableWikidataLink) {
addInfoToNode(value, 'ویکیداده: <a href="//www.wikidata.org/wiki/' + itemId + '">' + itemId + '</a>', 'black');
}
if (enableWikidataWikiCount) {
addInfoToNode(value, 'تعداد میانویکی: ' + Object.keys(data.entities[itemId].sitelinks).length, 'yellowgreen');
}
if (enableWikidataP31 &&
data.entities[itemId].claims !== undefined &&
data.entities[itemId].claims.P31 !== undefined) {
$.ajax({
url: '//www.wikidata.org/w/api.php',
dataType: 'jsonp',
data: {
action: 'wbgetentities',
ids: data.entities[itemId].claims.P31.map(function (x) {
return 'Q' + x.mainsnak.datavalue.value['numeric-id'];
}).join('|'),
format: 'json'
}
}).then(function (data) {
if (Object.keys(data.entities) === undefined) {
return;
}
addInfoToNode(value, 'یک ...: ' +
Object.keys(data.entities).map(function (x) {
var labels = data.entities[x].labels;
if (labels.en === undefined) {
return labels.en.value;
}
return labels.en.value;
}).join(', '),
'green'
);
});
}
});
}
});
}
$(function () {
$('#articlesLinksInfo').remove();
// HTML Builder
var hb = [];
hb.push('<span style="font-size: 40%; margin: 0 2em; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none;" id="articlesLinksInfo" class="noprint">');
hb.push('<a id="allInfo-button" href="#">اطلاعات پیوندها</a>');
if (conf.NoTalk) {
hb.push(' <input type="checkbox" name="enableNoTalk" id="enableNoTalk"><label for="enableNoTalk">آمار صفحهٔ غیربحث</label>');
}
if (conf.disambigQuery) {
hb.push(' <input type="checkbox" name="enableDisambigDetection" id="enableDisambigDetection"><label for="enableDisambigDetection">ابهام\u200cزدایی؟</label>');
}
if (conf.firstUserQuery) {
hb.push(' <input type="checkbox" name="enableFirstUserName" id="enableFirstUserName"><label for="enableFirstUserName">اولین ویرایشگر؟</label>');
}
if (conf.lastUserQuery) {
hb.push(' <input type="checkbox" name="enableLastUserName" id="enableLastUserName"><label for="enableLastUserName">آخرین ویرایشگر؟</label>');
}
if (conf.firstTimeQuery) {
hb.push(' <input type="checkbox" name="enableFirstUserTime" id="enableFirstUserTime"><label for="enableFirstUserTime">تاریخ ایجاد؟</label>');
}
if (conf.lastTimeQuery) {
hb.push(' <input type="checkbox" name="enableLastUserTime" id="enableLastUserTime"><label for="enableLastUserTime">تاریخ آخرین ویرایش؟</label>');
}
if (conf.redirectQuery) {
hb.push(' <input type="checkbox" name="enableRedirectDetection" id="enableRedirectDetection"><label for="enableRedirectDetection">تغییر مسیر؟</label>');
}
if (conf.sizeQuery) {
hb.push(' <input type="checkbox" name="enablePageSize" id="enablePageSize"><label for="enablePageSize">حجم؟</label>');
}
if (conf.redQuery) {
hb.push(' <input type="checkbox" name="enableRedLinkDetection" id="enableRedLinkDetection"><label for="enableRedLinkDetection">ناموجود؟</label>');
}
if (conf.wikidataLink) {
hb.push(' <input type="checkbox" name="enableWikidataLink" id="enableWikidataLink"><label for="enableWikidataLink">ویکیداده؟</label>');
}
if (conf.wikidataWikiCount) {
hb.push(' <input type="checkbox" name="enableWikidataWikiCount" id="enableWikidataWikiCount"><label for="enableWikidataWikiCount">تعداد میانویکی؟</label>');
}
if (conf.wikidataP31) {
hb.push(' <input type="checkbox" name="enableWikidataP31" id="enableWikidataP31"><label for="enableWikidataP31">ویکیداده یک ...؟</label>');
}
if (conf.LinkHits) {
hb.push(' <input type="checkbox" name="enableLinkHits" id="enableLinkHits"><label for="enableLinkHits">تعداد بازدید</label>');
}
if (conf.Score) {
hb.push(' <input type="checkbox" name="enableScore" id="enableScore"><label for="enableScore">درجهٔ اهمیت</label>');
}
if (conf.TargetRedirectPage) {
hb.push(' <input type="checkbox" name="enableTargetRedirectPage" id="enableTargetRedirectPage"><label for="enableTargetRedirectPage">اصلاح تغییرمسیر</label>');
}
if (conf.TargetSubject) {
hb.push(' <input type="checkbox" name="enableTargetSubject" id="enableTargetSubject"><label for="enableTargetSubject">موضوع مقاله</label>');
}
if (conf.TargetRemoveCache) {
hb.push(' <input type="checkbox" name="enableTargetRemoveCache" id="enableTargetRemoveCache"><label for="enableTargetRemoveCache">خالی کردن کاشه</label>');
}
hb.push('</span>');
$('h1:first').append(hb.join(''));
$('#allInfo-button').click(function (event) {
event.preventDefault();
infoAdder('all');
});
});
}(jQuery, mediaWiki));