MediaWiki:PageCreator/code2.js

/* * PageCreator * @author: Count of Howard (http://dev.wikia.com/wiki/User:Count_of_Howard) * Displays information related to page's creator * 9-12-16 - Initial revision * 12-12-16 - User inputted namespaces accepted * 22-12-16 - Removed Help namespace in favor of Project/Sitename * 23-12-16 - Local/UTC timezone options available * 04-02-17 - Slight cleanup/expansion of user options */

(function {   if ($('#page-creator').length) {        return;    }    //Cache mw variables    var mwVariables = mw.config.get([ 'skin', 'wgPageName', 'wgArticleId', 'wgUserLanguage', 'wgNamespaceNumber' ]),   i18n = {//Make sure to add the $1 and $2 variables in the string        'en': {            main: 'Created by $1',            on: 'on $2'        },        'be': {            main: 'Створана удзельнікам $1',            on: '$2'        },        'nl': {            main: 'Aangemaakt door $1',            on: 'op $2'        },        'pl': {            main: 'Utworzone przez $1',            on: 'dnia $2'        },        'pt': {            main: 'Criada por $1',            on: 'em $2'        },        'pt-br': {            main: 'Criada por $1',            on: 'em $2'        },        'sv': {            main: 'Skapad av $1',            on: 'på $2'        },        'es': {            main: 'Creado por $1',            on: 'el $2'        },        'ru': {            main: 'Создано участником $1',            on: '$2'        },        'uk': {            main: 'Створено користувачем $1',            on: '$2' }   },    lang = i18n[mwVariables.wgUserLanguage] || i18n[mwVariables.wgUserLanguage.split('-')[0]] || i18n.en;

// User input stuff // Default settings: No avatar, timestamp, or GMT time // Default ns: Main, Project, MediaWiki, Template, and Category var namespaces = window.pageCreatorNamespaces || [0, 4, 8, 10, 14], useAvatar = window.pageCreatorAvatar || false, useTimestamp = window.pageCreatorTimestamp || false, useUTC = window.pageCreatorUTC || false;

var pageCreator = mw.html.element('div', {       id: 'page-creator',        class: 'page-creator'    });

function getData(callback) { new mw.Api.get({           action: 'query',            prop: 'revisions',            titles: mwVariables.wgPageName,            rvprop: 'ids|timestamp|user|userid',            rvlimit: '1',            rvdir: 'newer',            format: 'json'        }).done(function(d) {            if(!d.error) {                callback(d);            }        }); }

function useData(result) { var data = result.query.pages, username = data[mwVariables.wgArticleId].revisions[0].user, userid = data[mwVariables.wgArticleId].revisions[0].userid, creationDate = data[mwVariables.wgArticleId].revisions[0].timestamp, revisionURL = mwVariables.wgPageName + '?oldid=' + data[mwVariables.wgArticleId].revisions[0].revid, userNameLink = '' + username + ' (talk | contribs)', time, formattedCreationDate, creationDateLink;

//Placement of PC before LE if possible if ($('#lastEdited').length) { $(pageCreator).insertBefore('#lastEdited'); } else { switch (mwVariables.skin) { case 'oasis': case 'wikia': $(pageCreator).appendTo('#WikiaPageHeader .header-container .header-title'); break; case 'monobook': case 'uncyclopedia': case 'wowwiki': $(pageCreator).prependTo('#bodyContent'); break; }       }        //Assorted user input options if (useAvatar === true) { getAvatar(userid); }

if (useTimestamp === true) { if (useUTC === true) { time = new Date(creationDate).toUTCString; formattedCreationDate = time.slice(0, 3) + ', ' + time.slice(4, 16) + ', ' + time.slice(17, 25) + ' (' + time.slice(26) + ')'; } else { time = new Date(creationDate).toString; formattedCreationDate = time.slice(0, 3) + ', ' + time.slice(4, 15) + ', ' + time.slice(16, 24) + ' ' + time.slice(34); }           creationDateLink = '' + formattedCreationDate + ''; $('.page-creator').html(lang.main.replace(/\$1/g, userNameLink) + ' ' + lang.on.replace(/\$2/g, creationDateLink)); } else { $('.page-creator').html(lang.main.replace(/\$1/g, userNameLink)); }   }

function getAvatar(id) { var XMLrequest = new XMLHttpRequest; XMLrequest.open('GET', '/api/v1/User/Details?ids=' + id + '&size=15', true); XMLrequest.onload = function { if (XMLrequest.status == 200) { $('#pc-avatar') .attr('src', JSON.parse(XMLrequest.responseText).items[0].avatar) .after(' '); }       };        XMLrequest.send; }

function init { if($.inArray(mwVariables.wgNamespaceNumber, namespaces) != -1) { getData(useData); }   }

init; });