MediaWiki:PageCreator/code.js

//PageCreator - Count of Howard //9-12-16 - Initial revision

$(function {   //Cache mw variables    var mwVariables = mw.config.get([ 'skin', 'wgServer', 'wgArticlePath', 'wgPageName', 'wgArticleId', 'wgUserLanguage', 'wgNamespaceNumber' ]);

//Add your language here //Make sure to add the $1 and $2 variables in the string var i18n = { 'en': 'Created by $1 on $2', 'sv': 'Skapad av $1 på $2' };

var lang = mw.config.get('wgUserLanguage'); var textContent = i18n.hasOwnProperty(lang) ? i18n[lang] : i18n.en; var username, userid, creationDate, revisionURL, useavatar;

//Api call parameters var parameters = { action: 'query', prop: 'revisions', titles: mwVariables.wgPageName, rvprop: 'ids|timestamp|user|userid', rvlimit: '1', rvdir: 'newer', format: 'json' };

//Script only for Main, MediaWiki, Template, Help, and Category namespaces var namespaces = [0, 8, 10, 12, 14];

//Assembling the API url var pararray = []; for (var i in parameters) { pararray.push(mw.util.wikiUrlencode(i) + '=' + mw.util.wikiUrlencode(parameters[i])); }   pararray = '?' + pararray.join('&'); var apiURL = mw.util.wikiScript('api') + pararray;

//Making the call if( $.inArray(mwVariables.wgNamespaceNumber, namespaces) != -1) { $.get(apiURL, function(data) {           username = data.query.pages[mwVariables.wgArticleId].revisions[0].user;            userid = data.query.pages[mwVariables.wgArticleId].revisions[0].userid;            creationDate = data.query.pages[mwVariables.wgArticleId].revisions[0].timestamp;            revisionURL = wgPageName + '?oldid=' + data.query.pages[mwVariables.wgArticleId].revisions[0].revid;        }).done(function(data) {            var pageCreator = document.createElement('div');            pageCreator.setAttribute('class', 'page-creator');            pageCreator.setAttribute('id', 'page-creator');

//Placement varies depending on skin employed if (mwVariables.skin == 'oasis' || mwVariables.skin == 'wikia') { $(pageCreator).appendTo('#WikiaPageHeader .header-container .header-title'); } else if (mwVariables.skin == 'monobook') { $(pageCreator).prependTo('#bodyContent'); }

//Setting the time properly var time = new Date(creationDate).toUTCString; var formattedCreationDate = time.slice(0, 3) + ', ' + time.slice(4, 16) + ', ' + time.slice(17, 25);

//Link HTML assembled var userNameLink = '' + username + ' (talk | contribs)'; var creationDateLink = '' + formattedCreationDate + '';

//Determine whether or not to keep avatar if (typeof PCOptions !== 'undefined' && PCOptions  !== null) { useavatar = PCOptions .avatar; } else { useavatar = true; }           //Snippet modified from Slyst's LastEdited if (useavatar === true) { var XMLrequest = new XMLHttpRequest; XMLrequest.open('GET', '/api/v1/User/Details?ids=' + userid + '&size=15', true); XMLrequest.onload = function { if (XMLrequest.status == 200) { var pcavatar = document.getElementById('pc-avatar'); pcavatar.setAttribute('src', JSON.parse(XMLrequest.responseText).items[0].avatar); pcavatar.insertAdjacentHTML('afterEnd', ' '); }               };                XMLrequest.send; }

//Do the thing var pageCreatorContent = textContent.replace(/\$1/g, userNameLink).replace(/\$2/g, creationDateLink); $('.page-creator').html(pageCreatorContent); });   } else {        return;    } });