Jump to content

User:Quarl/location canonicalize.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Quarl (talk | contribs) at 08:28, 2 February 2006 (+ washington DC). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// [[User:Quarl/location_canonicalize.js]] - canonicalizes location WikiLinks

//  Example: [[Seattle, Washington]] becomes [[Seattle, Washington|Seattle]], [[Washington]], [[USA]].

// requires: wikipage.js, util.js, addlilink.js

// quarl 2006-01-22 initial version

//<pre><nowiki>

location_canonicalize = new Object();

location_canonicalize.run = function() {
    location_canonicalize.initData();
    wikiPage.getEditorAsync(location_canonicalize.edit);
}

location_canonicalize.edit = function(editor) {
    // make changes

    var result = '';
    var input = editor.wpTextbox1;
    var changes = [];

    // special case the first Infobox, if there is one
    if (input.match(/^(?:{{Infobox(?:.|\n)*?\n}}|{\|(?:.|\n)*?\n\|})/i)) {
        // var left = RegExp.leftContext;
        var infobox = RegExp.lastMatch;
        var right = RegExp.rightContext;

        // treat the infobox separately, so that USA links get added to main
        // article.
        result = (location_canonicalize.canonicalizeString(infobox, changes) +
                  location_canonicalize.canonicalizeString(right, changes));
    } else {
        result = location_canonicalize.canonicalizeString(input, changes);
    }

    if (changes.length) {
        editor.wpTextbox1 = result;
        editor.wpSummary = 'Location canonicalization: ' + changes.join('; ');
        editor.wpMinoredit = true;
        editor.submit('wpDiff');
    } else {
        alert("No changes to make!");
    }
}

location_canonicalize.canonicalizeString = function(input, changes) {
    var result = '';

    while (input.match(/\[\[ *(?:([^|\]]+?) *\| *)?([^\]]+?) *\]\]/)) {
        var left = RegExp.leftContext;
        var wfull = RegExp.lastMatch;
        var wlink = RegExp.$1;
        var wtext = RegExp.$2;
        var right = RegExp.rightContext;

        result += left;
        var r = location_canonicalize.wikilink((wlink||wtext), wtext, result, right);
        if (r) {
            var new_wfull = r.wfull;
            result = r.left;
            right = r.right;
            changes.push(wfull + ' → ' + new_wfull);
        } else {
            var new_wfull = wfull;
        }
        result += new_wfull;
        input = right;
    }
    result += input;
    return result;
}

location_canonicalize.initData = function() {
    if (this.init) return;
    this.init = true;

    var CountryData = function(states, link_country, regexp_country) {
        this.states = states;
        this.link_country = link_country.match(/\[/) ? link_country : '[['+link_country+']]';
        var regexp_country = regexp_country || '\\[\\['+link_country+'\\]\\]';
        this.regexp_country = new RegExp(regexp_country);
        this.regexp_country_sq = new RegExp('^, '+regexp_country);
        this.regexp_substate = (
            new RegExp('^([^,]+), (' + this.states.join('|') + ')$'));
        this.regexp_state = (
            new RegExp('^(?:' + this.states.join('|') + ')$'));
    }

    this.countries = [
        new CountryData( // USA
            ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
             'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
             'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine',
             'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
             'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
             'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio',
             'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina',
             'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia',
             'Washington', 'West Virginia', 'Wisconsin', 'Wyoming',
             'Washington, DC', 'Washington, D.C.' // not strictly a state, but needs to be qualified with country also
             ],
            '[[United States|USA]]',
            '\\[\\[(?:United[ _]States(?:[ _][^|\\\]]+?)?|USA)(?:\\|[^|\\\]]+?)?\\]\\]'),

        new CountryData( // Canada
            ['British Columbia', 'Alberta', 'Saskatchewan', 'Manitoba',
             'Ontario', 'Quebec', 'New Brunswick', 'Nova Scotia',
             'Prince Edward Island', 'Newfoundland and Labrador'],
            'Canada'),

        new CountryData( // England
            ['Bedfordshire', 'Berkshire', 'City of Bristol',
             'Buckinghamshire', 'Cambridgeshire', 'Cheshire',
             'Cornwall', 'Cumbria', 'Derbyshire', 'Devon', 'Dorset',
             'Durham', 'East Riding of Yorkshire', 'East Sussex', 'Essex',
             'Gloucestershire', 'Greater London', 'Greater Manchester',
             'Hampshire', 'Herefordshire', 'Hertfordshire', 'Isle of Wight',
             'Kent', 'Lancashire', 'Leicestershire', 'Lincolnshire',
             'City of London', 'Merseyside', 'Norfolk', 'Northamptonshire',
             'Northumberland', 'North Yorkshire', 'Nottinghamshire',
             'Oxfordshire', 'Rutland', 'Shropshire', 'Somerset',
             'South Yorkshire', 'Staffordshire', 'Suffolk', 'Surrey',
             'Tyne and Wear', 'Warwickshire', 'West Midlands', 'West Sussex',
             'West Yorkshire', 'Wiltshire', 'Worcestershire'],
            'England'),             

        ];
}

location_canonicalize.wikilink = function(wlink, wtext, left, right) {
    // non-main namespace - usually a category
    if (wtext.match(/:/)) return null;

    if (wlink != wtext) return;

    for (i in this.countries) {
        var c = this.countries[i];

        var changes = 0;
        var wfull;
        if (wtext.match(c.regexp_substate)) {
            var city = RegExp.$1, state = RegExp.$2;
            wfull = '[[' + wtext + '|' + city + ']]';
            // only add link to state if we haven't link it yet.
            if (left.match('\\[\\['+state+'\\]\\]')) {
                wfull += ', ' + state;
            } else {
                wfull += ', [['+state+']]';
            }
            ++changes;
        } else if (wtext.match(c.regexp_state)) {
            // state link -- just need to add country link as necessary
            wfull = '[['+wtext+']]';
        }

        if (!wfull) continue;

        if (left.match(c.regexp_country)) {
            // Already mentioned country.  Delete redundant subsequent
            // country links
            if (right.match(c.regexp_country_sq)) {
                right = RegExp.rightContext;
                // only count as a change if we actually delete it!
                ++changes;
            }
        } else {
            // Haven't mentioned country earlier
            if (right.match(c.regexp_country_sq)) {
                // it's there already; good.
            } else {
                // not there; add it.
                wfull += ', ' + c.link_country;
                ++changes;
            }
        }

        if (changes) {
            return { wfull: wfull, left: left, right: right};
        } else {
            return null;
        }
    }

    return null;
}

location_canonicalize.load = function() {
    if (wikiPage.nsSpecialP) return;
    addTab('javascript:location_canonicalize.run()', 'locZ', 'ca-locZ', 'Canonicalize location wikilinks');
}

addOnloadHook(location_canonicalize.load);

//</nowiki></pre>