Jump to content

User:Jeeputer/missingArticleFinder.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
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();
    });
});