Jump to content

User:FR30799386/redirectify.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.
//<nowiki>
function loadredirectify() {
	if($('.assess-ga-text').length || $('.assess-fa-text').length)
	{
		 $('#mw-content-text').prepend('<div style="border:1px solid red;">Redirectify should not be used to redirect a Good article or a featured article.'
		 +'Seek consensus on the article talk page if you plan to redirect this page.</div>');
		 return;
	}
    mw.loader.using('mediawiki.widgets', function() {
        if (window.Morebits === null) {
            importScript('MediaWiki:Gadget-morebits.js');
        }
        $('.redirectify-ui').remove();
        var api = new mw.Api();
        //Portion copied from [[User:EnterpriseyBot/AFCHRS.js]] starts
        var Rcat = [{
            label: 'None',
            selected: true,
            value: '#'
        }, {
            label: 'Frequently used',
            value: 'Frequently used',
            disabled: true
        }, {
            label: 'R from alternative language',
            value: 'R from alternative language'
        }, {
            label: 'R from alternative name',
            value: 'R from alternative name'
        }, {
            label: 'R from modification',
            value: 'R from modification'
        }, {
            label: 'R to section',
            value: 'R to section'
        }, {
            label: 'R from diacritic',
            value: 'R from diacritic'
        }, {
            label: 'R to diacritic',
            value: 'R to diacritic'
        }, {
            label: 'From – abbreviation, capitalisation, and grammar',
            value: 'From – abbreviation, capitalisation, and grammar',
            disabled: true
        }, {
            label: 'R from acronym',
            value: 'R from acronym'
        }, {
            label: 'R from initialism',
            value: 'R from initialism'
        }, {
            label: 'R from CamelCase',
            value: 'R from CamelCase'
        }, {
            label: 'R from miscapitalisation',
            value: 'R from miscapitalisation'
        }, {
            label: 'R from other capitalisation',
            value: 'R from other capitalisation'
        }, {
            label: 'R from modification',
            value: 'R from modification'
        }, {
            label: 'R from plural',
            value: 'R from plural'
        }, {
            label: 'From parts of speach',
            value: 'From parts of speach',
            disabled: true
        }, {
            label: 'R from adjective',
            value: 'R from adjective'
        }, {
            label: 'R from adverb',
            value: 'R from adverb'
        }, {
            label: 'R from common noun',
            value: 'R from common noun'
        }, {
            label: 'R from gerund',
            value: 'R from gerund'
        }, {
            label: 'R from proper noun',
            value: 'R from proper noun'
        }, {
            label: 'R from verb',
            value: 'R from verb'
        }, {
            label: 'From – spelling',
            value: 'From – spelling',
            disabled: true
        }, {
            label: 'R from alternative spelling',
            value: 'R from alternative spelling'
        }, {
            label: 'R from misspelling',
            value: 'R from misspelling'
        }, {
            label: 'R from American English',
            value: 'R from American English'
        }, {
            label: 'R from British English',
            value: 'R from British English'
        }, {
            label: 'R from ASCII-only',
            value: 'R from ASCII-only'
        }, {
            label: 'R from diacritic',
            value: 'R from diacritic'
        }, {
            label: 'R from ligature',
            value: 'R from ligature'
        }, {
            label: 'R from stylization',
            value: 'R from stylization'
        }, {
            label: 'R from alternative transliteration',
            value: 'R from alternative transliteration'
        }, {
            label: 'R from Wade–Giles romanization',
            value: 'R from Wade–Giles romanization'
        }, {
            label: 'From alternative names, general',
            value: 'From alternative names, general',
            disabled: true
        }, {
            label: 'R from alternative language',
            value: 'R from alternative language'
        }, {
            label: 'R from alternative name',
            value: 'R from alternative name'
        }, {
            label: 'R from former name',
            value: 'R from former name'
        }, {
            label: 'R from historic name',
            value: 'R from historic name'
        }, {
            label: 'R from incomplete name',
            value: 'R from incomplete name'
        }, {
            label: 'R from incorrect name',
            value: 'R from incorrect name'
        }, {
            label: 'R from letter–word combination',
            value: 'R from letter–word combination'
        }, {
            label: 'R from long name',
            value: 'R from long name'
        }, {
            label: 'R from portmanteau',
            value: 'R from portmanteau'
        }, {
            label: 'R from predecessor company name',
            value: 'R from predecessor company name'
        }, {
            label: 'R from short name',
            value: 'R from short name'
        }, {
            label: 'R from sort name',
            value: 'R from sort name'
        }, {
            label: 'R from less specific name',
            value: 'R from less specific name'
        }, {
            label: 'R from more specific name',
            value: 'R from more specific name'
        }, {
            label: 'R from antonym',
            value: 'R from antonym'
        }, {
            label: 'R from eponym',
            value: 'R from eponym'
        }, {
            label: 'R from synonym',
            value: 'R from synonym'
        }, {
            label: 'R from Roman numerals',
            value: 'R from Roman numerals'
        }, {
            label: 'From alternative names, geography',
            value: 'From alternative names, geography',
            disabled: true
        }, {
            label: 'R from Canadian settlement name',
            value: 'R from Canadian settlement name'
        }, {
            label: 'R from name and country',
            value: 'R from name and country'
        }, {
            label: 'R from city and state',
            value: 'R from city and state'
        }, {
            label: 'R from city and province',
            value: 'R from city and province'
        }, {
            label: 'R from more specific geographic name',
            value: 'R from more specific geographic name'
        }, {
            label: 'R from postal abbreviation',
            value: 'R from postal abbreviation'
        }, {
            label: 'R from postal code',
            value: 'R from postal code'
        }, {
            label: 'R from US postal abbreviation',
            value: 'R from US postal abbreviation'
        }, {
            label: 'From alternative names, organisms',
            value: 'From alternative names, organisms',
            disabled: true
        }, {
            label: 'R from scientific abbreviation',
            value: 'R from scientific abbreviation'
        }, {
            label: 'R from scientific name',
            value: 'R from scientific name'
        }, {
            label: 'R from alternative scientific name',
            value: 'R from alternative scientific name'
        }, {
            label: 'R from monotypic taxon',
            value: 'R from monotypic taxon'
        }, {
            label: 'From alternative names, people',
            value: 'From alternative names, people',
            disabled: true
        }, {
            label: 'R from birth name',
            value: 'R from birth name'
        }, {
            label: 'R from given name',
            value: 'R from given name'
        }, {
            label: 'R from married name',
            value: 'R from married name'
        }, {
            label: 'R from name with title',
            value: 'R from name with title'
        }, {
            label: 'R from non-neutral name',
            value: 'R from non-neutral name'
        }, {
            label: 'R from personal name',
            value: 'R from personal name'
        }, {
            label: 'R from pseudonym',
            value: 'R from pseudonym'
        }, {
            label: 'R from relative',
            value: 'R from relative'
        }, {
            label: 'R from spouse',
            value: 'R from spouse'
        }, {
            label: 'R from surname',
            value: 'R from surname'
        }, {
            label: 'From alternative names, technical',
            value: 'From alternative names, technical',
            disabled: true
        }, {
            label: 'R from Bluebook abbreviation',
            value: 'R from Bluebook abbreviation'
        }, {
            label: 'R from brand name',
            value: 'R from brand name'
        }, {
            label: 'R from drug trade name',
            value: 'R from drug trade name'
        }, {
            label: 'R from file name',
            value: 'R from file name'
        }, {
            label: 'R from Java package name',
            value: 'R from Java package name'
        }, {
            label: 'R from MathSciNet abbreviation',
            value: 'R from MathSciNet abbreviation'
        }, {
            label: 'R from molecular formula',
            value: 'R from molecular formula'
        }, {
            label: 'R from NLM abbreviation',
            value: 'R from NLM abbreviation'
        }, {
            label: 'R from product name',
            value: 'R from product name'
        }, {
            label: 'R from slogan',
            value: 'R from slogan'
        }, {
            label: 'R from symbol',
            value: 'R from symbol'
        }, {
            label: 'R from systematic abbreviations',
            value: 'R from systematic abbreviations'
        }, {
            label: 'R from technical name',
            value: 'R from technical name'
        }, {
            label: 'R from trademark',
            value: 'R from trademark'
        }, {
            label: 'From – navigation',
            value: 'From – navigation',
            disabled: true
        }, {
            label: 'R from file metadata link',
            value: 'R from file metadata link'
        }, {
            label: 'R mentioned in hatnote',
            value: 'R mentioned in hatnote'
        }, {
            label: 'R from shortcut',
            value: 'R from shortcut'
        }, {
            label: 'R from template shortcut',
            value: 'R from template shortcut'
        }, {
            label: 'From disambiguations',
            value: 'From disambiguations',
            disabled: true
        }, {
            label: 'R from ambiguous term',
            value: 'R from ambiguous term'
        }, {
            label: 'R from incomplete disambiguation',
            value: 'R from incomplete disambiguation'
        }, {
            label: 'R from incorrect disambiguation',
            value: 'R from incorrect disambiguation'
        }, {
            label: 'R from other disambiguation',
            value: 'R from other disambiguation'
        }, {
            label: 'R from predictable disambiguation',
            value: 'R from predictable disambiguation'
        }, {
            label: 'R from unnecessary disambiguation',
            value: 'R from unnecessary disambiguation'
        }, {
            label: 'From mergers, duplicates, and moves',
            value: 'From mergers, duplicates, and moves',
            disabled: true
        }, {
            label: 'R from duplicated article',
            value: 'R from duplicated article'
        }, {
            label: 'R with history',
            value: 'R with history'
        }, {
            label: 'R from merge',
            value: 'R from merge'
        }, {
            label: 'R from move',
            value: 'R from move'
        }, {
            label: 'R with old history',
            value: 'R with old history'
        }, {
            label: 'From fiction',
            value: 'From fiction',
            disabled: true
        }, {
            label: 'R from fictional character',
            value: 'R from fictional character'
        }, {
            label: 'R from fictional element',
            value: 'R from fictional element'
        }, {
            label: 'R from fictional location',
            value: 'R from fictional location'
        }, {
            label: 'From related info',
            value: 'From related info',
            disabled: true
        }, {
            label: 'R from album',
            value: 'R from album'
        }, {
            label: 'R from animal',
            value: 'R from animal'
        }, {
            label: 'R from book',
            value: 'R from book'
        }, {
            label: 'R from catchphrase',
            value: 'R from catchphrase'
        }, {
            label: 'R from domain name',
            value: 'R from domain name'
        }, {
            label: 'R from top-level domain',
            value: 'R from top-level domain'
        }, {
            label: 'R from film',
            value: 'R from film'
        }, {
            label: 'R from gender',
            value: 'R from gender'
        }, {
            label: 'R from legislation',
            value: 'R from legislation'
        }, {
            label: 'R from list topic',
            value: 'R from list topic'
        }, {
            label: 'R from member',
            value: 'R from member'
        }, {
            label: 'R from person',
            value: 'R from person'
        }, {
            label: 'R from phrase',
            value: 'R from phrase'
        }, {
            label: 'R from quotation',
            value: 'R from quotation'
        }, {
            label: 'R from related word',
            value: 'R from related word'
        }, {
            label: 'R from school',
            value: 'R from school'
        }, {
            label: 'R from song',
            value: 'R from song'
        }, {
            label: 'R from subtopic',
            value: 'R from subtopic'
        }, {
            label: 'R from team',
            value: 'R from team'
        }, {
            label: 'R from work',
            value: 'R from work'
        }, {
            label: 'R from writer',
            value: 'R from writer'
        }, {
            label: 'R from Unicode',
            value: 'R from Unicode'
        }, {
            label: 'To – grammar, punctuation, and spelling',
            value: 'To – grammar, punctuation, and spelling',
            disabled: true
        }, {
            label: 'R to acronym',
            value: 'R to acronym'
        }, {
            label: 'R to initialism',
            value: 'R to initialism'
        }, {
            label: 'R to ASCII-only title',
            value: 'R to ASCII-only title'
        }, {
            label: 'R to diacritic',
            value: 'R to diacritic'
        }, {
            label: 'R to ligature',
            value: 'R to ligature'
        }, {
            label: 'R to plural',
            value: 'R to plural'
        }, {
            label: 'To alternative names',
            value: 'To alternative names',
            disabled: true
        }, {
            label: 'R to former name',
            value: 'R to former name'
        }, {
            label: 'R to historic name',
            value: 'R to historic name'
        }, {
            label: 'R to joint biography',
            value: 'R to joint biography'
        }, {
            label: 'R to name with title',
            value: 'R to name with title'
        }, {
            label: 'R to monotypic taxon',
            value: 'R to scientific name'
        }, {
            label: 'R to scientific name',
            value: 'R to scientific name'
        }, {
            label: 'R to systematic name',
            value: 'R to systematic name'
        }, {
            label: 'R to technical name',
            value: 'R to technical name'
        }, {
            label: 'To – navigation and disambiguation',
            value: 'To – navigation and disambiguation',
            disabled: true
        }, {
            label: 'R to anchor',
            value: 'R to anchor'
        }, {
            label: 'R to anthroponymy page',
            value: 'R to anthroponymy page'
        }, {
            label: 'R to disambiguation page',
            value: 'R to disambiguation page'
        }, {
            label: 'R to list entry',
            value: 'R to list entry'
        }, {
            label: 'R to section',
            value: 'R to section'
        }, {
            label: 'To miscellaneous',
            value: 'To miscellaneous',
            disabled: true
        }, {
            label: 'R to decade',
            value: 'R to decade'
        }, {
            label: 'R to related topic',
            value: 'R to related topic'
        }, {
            label: 'R to subpage',
            value: 'R to subpage'
        }, {
            label: 'R to subtopic',
            value: 'R to subtopic'
        }, {
            label: 'R to TV episode list entry',
            value: 'R to TV episode list entry'
        }, {
        	label:'Custom - prompt me',
        	value: 'Custom'
        }
        ];
        function generateSelect(title, options, onchange) {
            var text = '<select name="' + title + '" id="' + title + '" ';
            text += '>';
            for (var i = 0; i < options.length; i++) {
                var o = options[i];
                text += '<option value="' + o.value + '" ';
                if (o.selected) text += 'selected="selected" ';
                if (o.disabled) text += 'disabled ';
                text += '>' + o.label + '</option>';
            }
            text += "</select>";
            return text;
        }
        //Section copied ends here
        var morebits = new Morebits.wiki.page(mw.config.get('wgPageName'));
        var wikitext = '#REDIRECT [[$1]]\n$2';
        var summaryredirect;
        if($('.redirectMsg').length)
        summaryredirect = "Managing redirects by";
        else
        summaryredirect = 'Redirecting page to [[$1]]';
        var summaryalert = 'Notification of article being redirected';
        var title = (mw.config.get('wgPageName')).replace('_', ' ');
        var messagetouser = window.messagetouser || '==Redirecting page:' + title + '==\nA page you created, [[' + title + ']], does not seem to have [[WP:SOURCE|enough reliable third party sources]] to merit [[WP:N|inclusion in the encyclopedia]] in itself. It has been [[WP:REDIRECT|redirected]] to $1 which is related to the subject of the article.~~~~';
        var titleinput = new mw.widgets.TitleInputWidget({
            required: true
        });
        if($('.redirectMsg').length)
        {
        	titleinput.setValue($('.redirectMsg a').text());
        }
        $('#mw-content-text').prepend('<div class="redirectify-ui" style="padding:1em; background:#f2f2f2;"><h3>Redirectify</h3><div class="redirectify"><label>Target :</label></div><div class="r-action-ui"></div><div id="r-error" style="width:100%; color:red;"></div>');
        var $redirectify = $('.redirectify');
        $redirectify.append(titleinput.$element);
        $redirectify.append('<br><label for="r-rcat">Redirect categorization : </label>' + generateSelect('r-rcat', Rcat)+'&nbsp;<input id="r-rcat-custom" type="text" placeholder="Add template name here..."></input>');
        $('#r-rcat-custom').hide();
        $('#r-rcat').change(function(){
        	if($(this).val() === 'Custom')
        	{
        		$('#r-rcat-custom').fadeIn();
        	}
        	else
        	$('#r-rcat-custom').fadeOut();
        });
        $redirectify.append('<br><label for="r-summary">Summary of action : </label><input type="text" id="r-summary" required></input>');
        morebits.lookupCreator(function() {
            if (!$('.redirectMsg').length) {
                $redirectify.append('<br><label for="r-message">Message : (Remove current text to prevent message for being sent)</label>\n<textarea id="r-message" rows="4" columns="20">' + messagetouser + '</textarea>\n<span><b>Note :</b>$1 denotes target page</span>');
            }
        });
        $('.r-action-ui').append('<button class="mw-ui-progressive mw-ui-button r-submit">Redirect</button>'
        +'&nbsp;<button class="mw-ui-button mw-ui-quiet mw-ui-destructive r-cancel" style="background:none; border:0px solid white">Cancel</button>');
        $('.r-cancel').click(function(){
        $('.redirectify-ui').remove();	
        });
        $('.r-submit').click(function(e) {
            e.preventDefault();
            var reason = $('#r-summary').val()||'';
            var target = titleinput.value;
            if (target) {
                api.get({
                    action: "query",
                    titles: target,
                }).then(function(ret) {
                    $.each(ret.query.pages, function() {
                        if (this.missing !== "") {
                            wikitext = wikitext.replace('$1', target);
                            if ($('#r-rcat').val() !== '#') {
                            	if($('#r-rcat-custom:visible').length){
                            		wikitext = wikitext.replace('$2', '{{' + $('#r-rcat-custom').val() + '}}');
                            	}else{
                                wikitext = wikitext.replace('$2', '{{' + $('#r-rcat').val() + '}}');
                            	}
                            }else {
                                wikitext = wikitext.replace('$2', '');
                            }
                            summaryredirect = summaryredirect.replace('$1', target);
                            messagetouser = ($('#r-message').val() || '').replace('$1', target);
                            $redirectify.empty();
                            api.postWithToken('csrf', {
                                action: 'edit',
                                title: mw.config.get('wgPageName'),
                                text: wikitext,
                                summary: summaryredirect + ' ' + reason + ' ([[User:FR30799386/redirectify.js|redirectify]])'
                            }).done(function(response) {
                                if (response.edit.result === 'Success')
                                    $redirectify.append('<li style="color:green;">Redirected page !</li>');
                                console.log(response);
                            }).fail(function(status) {
                                $redirectify.append('<span style="color:red;">Could not redirect page !</span>');
                                console.log(status);
                            });
                            if (messagetouser !== '') {
                                morebits.lookupCreator(function() {
                                    var guy = morebits.getCreator();
                                    var guyedit = new Morebits.wiki.page('User talk:' + guy);
                                    guyedit.setEditSummary(summaryalert);
                                    guyedit.setAppendText('\n' + messagetouser);
                                    guyedit.append(function() {
                                        $redirectify.append('<li style="color:green;">Messaged user !</li>');
                                    }, function() {
                                        $redirectify.append('<li style="color:red;">Could not message user !</li>');
                                    });
                                });
                            }
                            $('#r-error').html("");
                            $('.r-action-ui').html('<button class="mw-ui-button r-reload">Reload</button>');
                            $('.r-reload').click(function() {
                                location.href = '/wiki/' + mw.config.get('wgPageName') + '?redirect=no';
                            });
                        } else {
                            createerror();
                        }
                    });
                }, function(error) {
                    createerror();
                });
            } else {
                createerror();
            }
        });
    });
    function createerror(){
    	$('#r-error').html('The page specified in the target parameter does not exist. Redirecting this page to the target will result in the creation of a broken redirect.');
    }
}
$.when(mw.loader.using('mediawiki.util'), $.ready).then(function() {
	if( mw.config.get( "wgNamespaceNumber" ) < 0 )
	return;
	 if (-1 === $.inArray('extendedconfirmed', mw.config.get('wgUserGroups'))) {
        console.log("[Redirectify] User isn't extendedconfirmed. Due to the sensitive nature of the script we cannot allow non-confirmed users to use it.");
        return;
    }
    var portlet = mw.util.addPortletLink('p-cactions', 'javascript:void(0)', 'Redirect page', 'ca-redirectify', 'Make page a redirect', 'r', '');
    $('#ca-redirectify').click(function() {
        loadredirectify();
    });
});
//</nowiki>