User:Opencooper/talkCount.js
// Shows the number of discussions on the talk page
// Namespace # reference: https://www.mediawiki.org/wiki/Manual:Namespace#Built-in_namespaces
// License: CC0
function setup() {
// If we're not reading an article, do nothing
if (!(mw.config.get('wgAction') === 'view'
&& mw.config.get('wgIsArticle')
&& !location.search.split('oldid=')[1] // Old revision
&& !mw.config.get("wgIsMainPage"))) {
return;
}
// Check if we're on a talk page
var namespace = mw.config.get( 'wgNamespaceNumber' );
if (namespace % 2 == 1) { // All talk namespaces have odd numbers
return;
}
// Make sure there's a talk tab
if (!$("#ca-talk").length) {
return;
}
// Check if talk page is a redlink
if ($("#ca-talk.new").length !== 0) {
return;
}
var talkUrl = $("#ca-talk a").attr("href");
var talkTitle = talkUrl.split("/wiki/")[1];
talkTitle = decodeURI(talkTitle);
// Mediawiki encodes these while decodeURI doesnt reverse it
talkTitle = talkTitle.replace(/%26/, "&").replace(/%2B/, "+").replace(/%3D/, "=").replace(/%3F/, "?");
determineTalkType(talkTitle);
}
function determineTalkType(talkTitle) {
// Find out whether the talk page is a flow page or not
// API docs: https://www.wikidata.org/w/api.php?action=help&modules=query%2Binfo
$.ajax({
url: apiUrl,
data: {
action: "query",
format: "json",
titles: talkTitle,
prop: "info"
},
success: function(queryResponse) {
var page = queryResponse.query.pages;
var pageId = Object.keys(page)[0];
var contentModel = page[pageId].contentmodel;
if (contentModel == "flow-board") {
getFlowTopics(talkTitle);
} else {
parseTalk(talkTitle);
}
}
});
}
function getFlowTopics(talkTitle) {
// Get section info from Flow API
// API docs: https://www.wikidata.org/w/api.php?action=help&modules=flow%2Bview-topiclist
$.ajax({
url: apiUrl,
data: {
action: "flow",
submodule: "view-topiclist",
vtltoconly: "true",
vtllimit: "max",
format: "json",
page: talkTitle,
},
success: parseFlow
});
}
function parseFlow(flowResponse) {
var topicList = flowResponse.flow["view-topiclist"].result.topiclist;
var roots = topicList.roots;
var posts = [];
for (let i = 0; i<roots.length; i++) {
root = roots[i];
// Assuming roots have a 1:1 mapping to posts
posts.push(topicList.posts[root][0]);
}
var revisions = topicList.revisions;
var sectionTitles = [];
for (let i = 0; i<posts.length; i++) {
var key = posts[i];
var title = revisions[key].content.content;
sectionTitles.push(title);
}
var sectionsCount = sectionTitles.length;
displayCount(sectionsCount, sectionTitles);
}
function parseTalk(talkTitle) {
// Get parsed talk page section info
// API docs: https://en.wikipedia.org/w/api.php?action=help&modules=parse
$.ajax({
url: apiUrl,
data: {
action: "parse",
format: "json",
page: talkTitle,
redirects: "yes",
prop: "sections"
},
success: parseSections
});
}
function parseSections(sectionsResponse) {
var sectionsCount = sectionsResponse.parse.sections.length;
var sectionTitles = [];
for (let i = 0; i < sectionsCount; i++) {
sectionHeader = sectionsResponse.parse.sections[i].line;
sectionTitles.push(sectionHeader);
}
displayCount(sectionsCount, sectionTitles);
}
function displayCount(sectionsCount, sectionTitles) {
var formattedTitles = "";
for (let i = 0; i < sectionsCount; i++) {
formattedTitles += i+1 + ". " + sectionTitles[i];
if (i != sectionsCount-1) {
formattedTitles += "\n";
}
}
formattedTitles = formattedTitles.replace(/'/g, "'"); // escape
var countHTML = " <small id='talkCount' title='"+ formattedTitles + "'>("
+ sectionsCount + ")</small>";
$("#ca-talk a").append(countHTML);
}
var apiUrl = location.origin + "/w/api.php";
setup();
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.