Widget:Autorenbox: Unterschied zwischen den Versionen
Aus ZUM-Unterrichten
KKeine Bearbeitungszusammenfassung Markierung: 2017-Quelltext-Bearbeitung |
KKeine Bearbeitungszusammenfassung Markierung: 2017-Quelltext-Bearbeitung |
||
Zeile 8: | Zeile 8: | ||
</div> | </div> | ||
<script type="text/javascript"> | <script type="text/javascript"> | ||
(function(){ | (function () { | ||
const paramArtikel = "<!--{$artikel|escape:'javascript'}-->"||null; | const paramArtikel = "<!--{$artikel|escape:'javascript'}-->" || null; | ||
const paramKategorie = "<!--{$kategorie|escape:'javascript'}-->"||null; | const paramKategorie = "<!--{$kategorie|escape:'javascript'}-->" || null; | ||
const element = document.currentScript && document.currentScript.previousElementSibling; | |||
const element = document.currentScript && document.currentScript.previousElementSibling; | if (element) { | ||
if(element){ | window.RLQ.push(function () { | ||
window.RLQ.push(function(){ | mw.loader.enqueue(['mediawiki.api'], function () { | ||
const getCurrentTitle = () => mw.Title.makeTitle( | |||
mw.config.get('wgNamespaceNumber'), | |||
mw.config.get('wgTitle') | |||
).getPrefixedText() | |||
; | |||
const userlistEl = $(element).find('[data-zum-hook="userlist"]'); | |||
const lastModifiedEl = $(element).find('[data-zum-hook="last-modified"]'); | |||
const api = new mw.Api(); | |||
const query = paramKategorie ? { | |||
action: 'query', | |||
prop: ['contributors', 'info'], | |||
generator: 'categorymembers', | |||
gcmtitle: `Kategorie:${paramKategorie}`, | |||
gcmnamespace: 0, | |||
gcmtype: 'page', | |||
gcmlimit: 'max' | |||
} : { | |||
action: 'query', | |||
prop: ['revisions', 'contributors', 'info'], | |||
titles: paramArtikel || getCurrentTitle(), | |||
rvprop: ['userid', 'user', 'flags', 'timestamp'], | |||
rvlimit: 150 | |||
}; | |||
const response = jQuery.Deferred(); | |||
function continueQueryOrResolve(result, query, pages, lastContinue) { | |||
api.get({...query, ...lastContinue}) | |||
.then((response) => { | |||
const newPages = Object.values(response.query.pages).reduce((pages, page) => { | |||
pages[page.pageid] = {...pages[page.pageid], ...page}; | |||
return pages; | |||
}, pages); | |||
if (response.continue !== undefined) { | |||
continueQueryOrResolve(result, query, newPages, response.continue); | |||
} else if (response.batchcomplete !== undefined) { | |||
result.resolve(newPages); | |||
} | |||
}); | |||
} | |||
continueQueryOrResolve(response, query, {}, null); | |||
response | |||
.then((pages) => Object.values(pages)) | |||
.then((pages) => { | |||
const userCounts = pages.reduce((acc, pageData) => { | |||
acc.touched = Math.max(acc.touched || -1, Date.parse(pageData.touched)); | |||
acc.userChanges = (pageData.contributors || []) | |||
.reduce((userChanges, c) => { | |||
userChanges[c.userid] = userChanges[c.userid] || { | |||
count: 0, | |||
last: -1, | |||
userid: c.userid, | |||
username: c.name | |||
}; | |||
userChanges[c.userid].count += 1; | |||
return userChanges; | |||
}, acc.userChanges || {}); | |||
acc.userChanges = (pageData.revisions || []) | |||
.reduce((userChanges, r) => { | |||
userChanges[r.userid] = userChanges[r.userid] || { | |||
count: 0, | |||
last: -1, | |||
userid: r.userid, | |||
username: r.user | |||
}; | |||
userChanges[r.userid].count += r.minor !== undefined ? 0.1 : 1; | |||
userChanges[r.userid].last = Math.max(userChanges[r.userid].last, r.revid); | |||
return userChanges; | |||
}, acc.userChanges || {}); | |||
return acc; | |||
}, {}); | |||
const usernames = Object.values(userCounts.userChanges).sort((a, b) => { | |||
let diff = b.count - a.count; | |||
if (diff !== 0) { | |||
return diff; | |||
} | |||
return b.last - a.last; | |||
}).map((user) => user.username); | |||
$(lastModifiedEl).append(new Date(userCounts.touched).toLocaleDateString()); | |||
const userdatas = usernames.map((user) => api | |||
.parse(`{{#avatar:${user}|l}}`) | |||
.then((imgTag) => $(imgTag)) | |||
.then((img$) => img$.find('img').addBack('img').attr('src')) | |||
.then((imgSrc) => ({user, imgSrc})) | |||
.fail((ignored) => null) | |||
); | ); | ||
$.when(...userdatas) | |||
.then((...userdatas) => userdatas.filter((ud) => ud !== null)) | |||
.then((userdatas) => { | |||
userdatas.forEach(({user, imgSrc}) => { | |||
$('<img>') | |||
.attr('src', imgSrc) | |||
.wrap('<a>') | |||
.parent() | |||
.attr('href', mw.Title.makeTitle(mw.config.get('wgNamespaceIds')['benutzer'], user).getUrl()) | |||
.wrap('<div class="uk-border-circle uk-text-center" style="overflow:hidden;width:60%;margin:auto;">') | |||
.parent() | |||
.wrap('<div class="uk-panel uk-panel-border uk-text-small" style="max-width:12ch">') | |||
.parent() | |||
.append($('<a>').append(user).attr('href', mw.Title.makeTitle(mw.config.get('wgNamespaceIds')['benutzer'], user).getUrl()).wrap('<div class="uk-text-center uk-text-truncate">').parent()).appendTo($(userlistEl)); | |||
.append($('<a>').append(user).attr('href', | }); | ||
}) | |||
.then((ignored) => $(element).removeClass('uk-hidden')); | |||
});//end response.then | |||
});//end loader.enqueue | |||
});//end RLQ.push | |||
});//end RLQ.push | }//end if | ||
}//end if | |||
})(); | })(); | ||
</script> | </script> | ||
</includeonly> | </includeonly> |