User:Jeeputer/missingInFawiki.js

// Lists pages in a given category that are missing on my home wiki, Persian Wikipedia, so i can translate and transfer them to that project. // TODO: Make it less personal and usable for other users and other projects.

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, advert = ' (with script)';

function savePage(title, text, summary) { return api.postWithToken('csrf', {       action: 'edit',        title: title,        text: text,        summary: summary,        nocreate: '',        createonly: false,        minor: true    }); }

function main(catName) { api.get({       action: 'query',        list: 'categorymembers',        cmtitle: catName,        cmtype: 'page',        cmlimit: 50,        format: 'json',        formatversion: 2    }).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.substring(9) + ' ==\n'; wikitext += '* ' + articles.join('\n* ') + '\n'; savePage(               'User:Jeeputer/Needed pages on fawiki',                wikitext,                'List pages from ' + catName + ' that are missing on fawiki' + advert            ); });   }); }

$(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:Jeeputer/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:Jeeputer/Needed pages on fawiki'); }, 3000);               }            });        }    });    e.preventDefault; });