MediaWiki:UserAccountAge/code.js

/* * @name:          UserAccountAge * @description:   Displays selected account's age in the Oasis user page masthead * @author:        Count of Howard  * @attribution:   Using code by PleaseStand  */

(function {   if($('#UserProfileMasthead').length === 0) {        return;    }

var mwVariables = mw.config.get([       'wgUserLanguage',        'wgScriptPath'    ]), i18n = { 'en': { // English second: 'second', seconds: 'seconds', minute: 'minute', minutes: 'minutes', hour: 'hour', hours: 'hours', day: 'day', days: 'days', week: 'week', weeks: 'weeks', month: 'month', months: 'months', year: 'year', years: 'years' }   },    lang = i18n[mwVariables.wgUserLanguage] || i18n[mwVariables.wgUserLanguage.split('-')[0]] || i18n.en, selectedUser = mw.util.wikiUrlencode($('.UserProfileMasthead .masthead-info h1').text);

function getData(user, callback) { $.getJSON(mw.util.wikiScript('api'), {           action: 'query',            list: 'users|usercontribs',            usprop: 'registration',            ususers: user,            uclimit: 1,            ucprop: 'timestamp',            ucuser: user,            format: 'json'        }).done(function(d) {            if(!d.error) {                callback(d);            }        }); }

function processData(data) { var results = data.query, username = results.users[0].name, user, registration, missing, invalid;

// API tends to have no data on hand for users who created accounts 2006 and earlier try { user = results.users[0]; invalid = typeof user.invalid !== "undefined"; missing = typeof user.missing !== "undefined"; registration = (typeof user.registration === "string") ? new Date(user.registration) : null; } catch(e) { return; }

// Provides a link to the API JSON that shows the date of creation if (registration) { var statusText = '' + convertDate(registration) + '';

//Stole the user tag append idea from Cube  $('.masthead-info hgroup').append(' ' + statusText + ' '); }   }

// From UserInfo  function convertDate(cd) { var age = new Date.getTime - cd.getTime, ageNumber, ageRemainder, ageWords;

if (age < 60000) { ageNumber = Math.floor(age / 1000); ageWords = formatDate(ageNumber, lang.second, lang.seconds); } else if (age < 3600000) { ageNumber = Math.floor(age / 60000); ageWords = formatDate(ageNumber, lang.minute, lang.minutes); } else if (age < 86400000) { ageNumber = Math.floor(age / 3600000); ageWords = formatDate(ageNumber, lang.hour, lang.hours); ageRemainder = Math.floor((age - ageNumber * 3600000) / 60000); } else if (age < 604800000) { ageNumber = Math.floor(age / 86400000); ageWords = formatDate(ageNumber, lang.day, lang.days); } else if (age < 2592000000) { ageNumber = Math.floor(age / 604800000); ageWords = formatDate(ageNumber, lang.week, lang.weeks); } else if (age < 31536000000) { ageNumber = Math.floor(age / 2592000000); ageWords = formatDate(ageNumber, lang.month, lang.months); } else { ageNumber = Math.floor(age / 31536000000); ageWords = formatDate(ageNumber, lang.year, lang.years); ageRemainder = Math.floor((age - ageNumber * 31536000000) / 2592000000); if (ageRemainder) { ageWords += " " + formatDate(ageRemainder, lang.month, lang.months); }       }        return ageWords; }

function formatDate(q, s, p) { return String(q).replace(/\d{1,3}(?=(\d{3})+(?!\d))/g, "$&,") + "\u00a0" + (q === 1 ? s : p); }

//Do the thing getData(selectedUser, processData); });