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

Content deleted Content added
try to find section even if anchor is dot-encoded, load dependencies only when needed
move all strings into an object for internationalization
Line 26:
return;
}
 
var msg = {
"starter-text": "Looks like the discussion \"$1\" has been archived. ",
"finding": "Finding archived discussion...",
"click-link": "Click to see archived discussion",
divHtml"no-search-results": = '"No search results found for archived discussion \"$1\"' + mw.html.escape(sectionName) + '"';,
"search-link-found": "(or search in archives)",
"search-link-notfound": "Click to search in archives"
};
 
$('#mw-content-text').before(
$('<div>')
.text(msg['Looks like the discussion "starter-text' +].replace('$1', sectionName) + msg['" has been archived. Finding archived discussion...finding'])
.addClass('archived-section-prompt')
.css({
Line 48 ⟶ 57:
}
 
var searchQuery = sectionNameDotDecoded === sectionName ?
mw.loader.using(['mediawiki.util', 'mediawiki.api'], function() {
 
var searchQuery = sectionNameDotDecoded === sectionName ?
'"' + sectionName + '" prefix:"' + prefix + '"' :
'"' + sectionName + '" OR "' + sectionNameDotDecoded + '" prefix:"' + prefix + '"';
 
mw.loader.using(['mediawiki.util', 'mediawiki.api'], ).then(function() {
new mw.Api({
 
return new mw.Api({
ajax: { headers: { 'Api-User-Agent': 'w:en:User:SD0001/find-archived-section.js' } }
}).get({
Line 63 ⟶ 72:
srsort: 'create_timestamp_desc', // list more recent archives first
srlimit: '20'
});
}).then(function(json) {
if (!json || !json.query || !json.query.search) {
return;
}
 
}).then(function(json) {
var divHtml;
varif results(!json =|| !json.query || !json.query.search;) {
return;
if (results.length === 0) {
}
divHtml = 'No search results found for archived discussion "' + mw.html.escape(sectionName) + '"';
 
}var else {divHtml;
ifvar (!jsonresults ||= !json.query || !json.query.search) {;
var pageTitle,
if (results.length === 0) {
sectionNameFound; // will either be sectionName or sectionNameDotDecoded
divHtml = msg['no-search-results'].replace('$1', mw.html.escape(sectionName));
 
} else {
// obtain the the first exact section title match (which would be from the most recent archive)
var divHtml;pageTitle,
// this loop iterates over just one item in the vast majority of cases
varsectionNameFound; sectionName_// =will either be sectionName.replace(/ /g,or '_');sectionNameDotDecoded
var sectionNameDotDecoded_ = sectionNameDotDecoded.replace(/ /g, '_');
for (var i in results) {
var result = results[i];
// sectiontitle in API output has spaces encoded as underscores
if (result.sectiontitle) {
if (result.sectiontitle === sectionName_ ||
result.sectiontitle === sectionNameDotDecoded_) {
 
// obtain the the first exact section title match (which would be from the most recent archive)
pageTitle = result.title;
// this loop iterates over just one item in the vast majority of cases
sectionNameFound = result.sectiontitle.replace(/_/g, ' ');
var sectionName_ = sectionName.replace(/ /g, '_');
break;
var sectionNameDotDecoded_ = sectionNameDotDecoded.replace(/ /g, '_');
}
for (var i in results) {
var result = results[i];
// sectiontitle in API output has spaces encoded as underscores
if (result.sectiontitle) {
if (result.sectiontitle === sectionName_ ||
result.sectiontitle === sectionNameDotDecoded_) {
 
pageTitle = result.title;
sectionNameFound = result.sectiontitle.replace(/_/g, ' ');
break;
}
}
}
 
var searchLink = mw.util.getUrl('Special:Search', {
search: '~' + searchQuery, // ~ in the beginning forces a search even if a page of the same name exists, see [[H:FORCE]]
prefix: prefix,
sort: 'create_timestamp_desc'
});
 
divHtml = msg['Looks like the discussion "starter-text' +].replace('$1', mw.html.escape(sectionNameFound) +|| '" has been archived. 'sectionName));
 
if (pageTitle) { // if a section with the same name was found
var discussionLink = mw.util.getUrl(pageTitle) + '#' + mw.util.wikiUrlencode(sectionNameFound);
divHtml += '<b><a href="' + discussionLink + '">Click' to+ seemsg['click-link'] archived+ discussion'</a></b> ';
divHtml += '<small><a href="' + searchLink + '">(or' + msg['search-link-found'] in+ archives)'</a></small>. ';
 
} else {
divHtml += '<a href="' + searchLink + '">Click' to+ msg['search-link-notfound'] in+ archives'</a>. ';
}
}
}
 
$('.archived-section-prompt').html(divHtml);
 
}).fail(function(err) {
console.error('[find-archived-section]: ', JSON.stringify(err));
});
 
}).fail(function(err) {
console.error('[find-archived-section]: ', JSON.stringify(err));
});