User:Jeeputer/missingArticleFinder.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:Jeeputer/missingArticleFinder. |
mw.loader.using(['oojs-ui-core', 'oojs-ui-windows', 'oojs-ui-widgets']).done(function () {
var portlet = mw.util.addPortletLink('p-cactions', 'javascript:void(0)', 'List missing pages', 'ca-lmp', 'Create a list of pages which are placed in this category and are missing on fawiki'),
api = new mw.Api();
function savePage(title, text, summary) {
return api.post({
action: 'edit',
title: title,
text: text,
summary: summary,
nocreate: false,
createonly: false,
minor: true,
token: mw.user.tokens.get('csrfToken')
});
}
function main(catName) {
api.get({
action: 'query',
list: 'categorymembers',
cmtitle: catName,
cmtype: 'page',
cmlimit: 'max',
format: 'json'
}).done(function (data) {
var pages = data.query.categorymembers,
page,
text = '',
titles = [];
for (page in pages) {
titles.push(pages[page].title);
}
api.get({
action: 'query',
prop: 'pageterms',
titles: titles.join('|'),
wbptterms: 'label',
wbptlanguage: 'fa',
formatversion: '2'
}).done( function (data) {
var articles = [];
var pages = data.query.pages
var len = pages.length
for (var i = 0; i < len; i++) {
if (pages[i].terms === undefined) {
articles.push(pages[i].title)
} else {
continue;
}
}
var wikitext = '\n== [[:' + catName + '|' + catName.substring(9) + ']] ==\n';
wikitext += '* [[' + articles.join(']]\n* [[') + ']]\n';
savePage(
'User:' + mw.config.get('wgUserName') + '/Needed pages on fawiki',
wikitext,
'List pages from [[' + catName + ']] that are missing on fawiki (using [[User:Jeeputer/missingArticleFinder.js|script]])'
);
});
});
}
$(portlet).on('click', function(e) {
var catName;
OO.ui.confirm('Use page name to make the list?').done( function (confirmed) {
if (confirmed) {
catName = mw.config.get('wgPageName').replace(/\_/g, ' ')
main(catName);
mw.notify('Listed missing pages from this category', {title: 'Done!', type: 'success'})
setTimeout(function () {
window.open(mw.config.get('wgServer') + mw.config.get('wgScript') + '/User:' + mw.config.get('wgUserName') + '/Needed pages on fawiki')
}, 3000);
} else {
OO.ui.prompt('Enter category name', {
textInput: {
placeholder: 'Prefixed category title'
}
}).done(function(re) {
if (re === null || re === '') {
mw.notify('Aborted...', {title: 'Not done!', type: 'error'})
} else {
catName = re;
main(catName);
mw.notify('Listed pages from ' + catName, {title: 'Done!', type: 'success'})
setTimeout(function () {
window.open(mw.config.get('wgServer') + mw.config.get('wgScript') + '/User:' + mw.config.get('wgUserName') + '/Needed pages on fawiki')
}, 3000);
}
});
}
});
e.preventDefault();
});
});