MediaWiki:Gadget-find-archived-section.js: Difference between revisions

Content deleted Content added
+case "Wikipedia:Teahouse":
 
(7 intermediate revisions by 3 users not shown)
Line 5:
* | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
* |_____________________________________________________________________________|
*
* Gadget to navigate to an archived section after following its broken link.
*
* Author: SD0001
* Documentation: [[User:SD0001/find-archived-section]]
*
*/
 
Line 16:
// START EDITING HERE FOR LOCALISATION
// Messages: translate these strings if porting to a non-English language wiki
mw.messages.set({
var msg = {
"starterfas-textfinding": "'Looks like the discussion \"$1\" has been archived. "Finding archived discussion...',
"fas-exact-match": 'Looks like the discussion "$1" has been archived. <b><a href="$2">Click to see archived discussion</a></b> <small>(<a href="$3">or search in archives</a>)</small>',
"finding": "Finding archived discussion...",
"fas-inexact-match": 'Looks like the discussion "$1" has been archived. <a href="$2">Click to search in archives</a>.',
"click-link": "Click to see archived discussion",
"fas-no-search-results": "'No search results found for section \"$1\" in archives. It may have been removed or renamed, or you may have followed a malformed link."',
});
"search-link-found": "(or search in archives)", // link to search results when an exact match was obtained.
 
"search-link-notfound": "Click to search in archives" // link to search results when an exact match was NOT obtained.
};
var config = {
// Function to introduce arbitrary changes to prefix.
Line 35 ⟶ 34:
case "Wikipedia:Administrators' noticeboard":
return "Wikipedia:Administrators' noticeboard/Archive";
case "Wikipedia:Reference desk/Science":
default:
return "Wikipedia:Reference desk/Archives/Science";
case "Wikipedia:Reference desk/Miscellaneous":
return "Wikipedia:Reference desk/Archives/Miscellaneous";
case "Wikipedia:Reference desk/Mathematics":
return "Wikipedia:Reference desk/Archives/Mathematics";
case "Wikipedia:Reference desk/Language":
return "Wikipedia:Reference desk/Archives/Language";
case "Wikipedia:Reference desk/Humanities":
return "Wikipedia:Reference desk/Archives/Humanities";
case "Wikipedia:Reference desk/Entertainment":
return "Wikipedia:Reference desk/Archives/Entertainment";
case "Wikipedia:Reference desk/Computing":
return "Wikipedia:Reference desk/Archives/Computing";
case "Wikipedia:Reference desk":
return "Wikipedia:Reference desk/Archives";
case "Wikipedia:Teahouse":
return "Wikipedia:Teahouse/Questions";
default:
return prefix;
}
Line 49 ⟶ 66:
 
// Show only on discussion pages (pages with "add section" button)
// On minerva skin (which doesn't use the add section button) show on all talk & project space pages
if (!addsection && (!correctNs || !minerva)) {
return;
Line 56 ⟶ 73:
var sectionName = decodeURIComponent(
window.___location.hash.slice(1) // to remove the leading #
.replace(/_/g, ' ')
);
 
Line 66 ⟶ 83:
if (!sectionName || // no section name in URL
sectionName.indexOf('/media/') === 0 || // URLs used by MediaViewer
/^c-/.test(sectionName) || //URLs used by DiscussionTools
/^\d{12} /.test(sectionName) || // URLs used by convenientDiscussions
/^noticeApplied-/.test(sectionName) || // URLs used by RedWarn
document.getElementById(sectionName.replace(/ /g, '_')) !== null) { // section exists on page
return;
}
 
var sanitiseStringescapeQuotes = function(str, escapeHtml) {
return (escapeHtml ? mw.html.escape(str) : str).replace(/\$"/g, '$$$$\\"');
// if str contains $ signs, we don't want String.prototype.replace to treat them with special meaning
return (escapeHtml ? mw.html.escape(str) : str).replace(/\$/g, '$$$$');
};
 
$('#mw-content-text').before(
$('<div>')
.text(mw.msg[('starterfas-text'].replace('$1finding', sanitiseString(sectionName, false)) + msg['finding'])
.addClass('archived-section-prompt')
.css({
Line 93 ⟶ 112:
 
var searchQuery = sectionNameDotDecoded === sectionName ?
'"' + escapeQuotes(sectionName) + '" prefix:"' + prefix + '"' :
'"' + escapeQuotes(sectionName) + '" OR "' + escapeQuotes(sectionNameDotDecoded) + '" prefix:"' + prefix + '"';
 
mw.loader.using(['mediawiki.util', 'mediawiki.api']).then(function() {
 
return new mw.Api({
ajax: { headers: { 'Api-User-Agent': 'w:en:UserMediaWiki:SD0001/Gadget-find-archived-section.js' } }
}).get({
action: 'query',
Line 114 ⟶ 133:
}
 
var divHtml;
var results = json.query.search;
if (results.length === 0) {
divHtml = msg[$('no.archived-searchsection-resultsprompt']).replacehtml(mw.msg('$1fas-no-results', sanitiseStringmw.html.escape(sectionName, true)));
 
} else {
Line 125 ⟶ 143:
// obtain the the first exact section title match (which would be from the most recent archive)
// this loop iterates over just one item in the vast majority of cases
var sectionName_ = sectionName.replace(/ /g, '_');
var sectionNameDotDecoded_ = sectionNameDotDecoded.replace(/ /g, '_');
for (var i in results) {
var result = results[i];
}if (
// sectiontitle in API output has spaces encoded as underscores
if ( result.sectiontitle) {&&
if (result.sectiontitle === sectionName_sectionName || result.sectiontitle === sectionNameDotDecoded)
) {
result.sectiontitle === sectionNameDotDecoded_) {
pageTitle = result.title;
 
pageTitlesectionNameFound = result.titlesectiontitle;
break;
sectionNameFound = result.sectiontitle.replace(/_/g, ' ');
break;
}
}
}
Line 147 ⟶ 161:
});
 
divHtmlvar escapedSectionName = msg['starter-text']mw.replace('$1', sanitiseStringhtml.escape(sectionNameFound || sectionName, true));
 
if (pageTitle) { // if a section with the same name was found
var discussionLink = mw.util.getUrl(pageTitle) + '#' + mw.util.wikiUrlencode(sectionNameFound);
$('.archived-section-prompt').html(mw.msg('fas-exact-match', escapedSectionName, discussionLink, searchLink));
divHtml += '<b><a href="' + discussionLink + '">' + msg['click-link'] + '</a></b> ';
divHtml += '<small><a href="' + searchLink + '">' + msg['search-link-found'] + '</a></small>. ';
 
} else {
$('.archived-section-prompt').html(mw.msg('fas-inexact-match', escapedSectionName, searchLink));
divHtml += '<a href="' + searchLink + '">' + msg['search-link-notfound'] + '</a>. ';
}
}
 
$('.archived-section-prompt').html(divHtml);
 
}).catch(function(err) {