MediaWiki:Common.js: Difference between revisions
Embed Google Forms for Contact Us page |
Boost title matches and site pages in Special:Search |
||
| Line 5: | Line 5: | ||
* Split mashed CSV notes into separate bullets, then link MII/IPW team codes. | * Split mashed CSV notes into separate bullets, then link MII/IPW team codes. | ||
* Build the Main Page find-a-soldier search form (raw HTML forms are stripped from wikitext). | * Build the Main Page find-a-soldier search form (raw HTML forms are stripped from wikitext). | ||
* Reorder Special:Search hits so title matches and site pages rank above research-notes noise. | |||
*/ | */ | ||
$(function () { | $(function () { | ||
| Line 26: | Line 27: | ||
$form.append($row); | $form.append($row); | ||
$slot.find('.home-find-title').after($form); | $slot.find('.home-find-title').after($form); | ||
}()); | |||
(function boostSearchResults() { | |||
if (mw.config.get('wgCanonicalSpecialPageName') !== 'Search') { | |||
return; | |||
} | |||
var $list = $('.mw-search-results').first(); | |||
if (!$list.length || $list.data('rbSearchBoost')) { | |||
return; | |||
} | |||
$list.data('rbSearchBoost', 1); | |||
var params = new URLSearchParams(window.location.search); | |||
var raw = String(params.get('search') || '').trim(); | |||
if (!raw) { | |||
return; | |||
} | |||
var cleaned = raw | |||
.replace(/\b(intitle|incategory|prefix|deepcat)\s*:/gi, ' ') | |||
.replace(/["']/g, ' ') | |||
.replace(/\s+/g, ' ') | |||
.trim() | |||
.toLowerCase(); | |||
var tokens = cleaned.split(' ').filter(function (t) { | |||
return t.length > 1; | |||
}); | |||
if (!tokens.length) { | |||
return; | |||
} | |||
function titleFromItem(el) { | |||
var $a = $(el).find('.mw-search-result-heading a, a').first(); | |||
return $.trim($a.text() || ''); | |||
} | |||
function scoreTitle(title) { | |||
var t = title.toLowerCase(); | |||
var score = 0; | |||
var i; | |||
if (t === cleaned) { | |||
score += 200; | |||
} | |||
var allInTitle = true; | |||
for (i = 0; i < tokens.length; i++) { | |||
if (t.indexOf(tokens[i]) === -1) { | |||
allInTitle = false; | |||
break; | |||
} | |||
} | |||
if (allInTitle) { | |||
score += 80; | |||
} | |||
// Soldier titles: "Surname, Given (ASN)" — reward surname/ASN hits in the title. | |||
if (/^\d{5,9}$/.test(cleaned) && t.indexOf(cleaned) !== -1) { | |||
score += 120; | |||
} | |||
if (/\(\d{5,9}\)\s*$/.test(title)) { | |||
score += 5; | |||
} | |||
return score; | |||
} | |||
function resort(siteTitles) { | |||
var items = $list.children('li').get(); | |||
if (items.length < 2) { | |||
return; | |||
} | |||
items.forEach(function (el, idx) { | |||
$(el).data('rbOrig', idx); | |||
}); | |||
items.sort(function (a, b) { | |||
var ta = titleFromItem(a); | |||
var tb = titleFromItem(b); | |||
var sa = scoreTitle(ta) + (siteTitles[ta] ? 40 : 0); | |||
var sb = scoreTitle(tb) + (siteTitles[tb] ? 40 : 0); | |||
if (sb !== sa) { | |||
return sb - sa; | |||
} | |||
return $(a).data('rbOrig') - $(b).data('rbOrig'); | |||
}); | |||
$list.append(items); | |||
} | |||
var siteTitles = {}; | |||
if (!mw.Api) { | |||
resort(siteTitles); | |||
return; | |||
} | |||
new mw.Api() | |||
.get({ | |||
action: 'query', | |||
list: 'categorymembers', | |||
cmtitle: 'Category:Ritchie Boys Wiki', | |||
cmlimit: 'max', | |||
cmnamespace: 0 | |||
}) | |||
.done(function (data) { | |||
var members = (data.query && data.query.categorymembers) || []; | |||
members.forEach(function (m) { | |||
if (m.title) { | |||
siteTitles[m.title] = true; | |||
} | |||
}); | |||
}) | |||
.always(function () { | |||
resort(siteTitles); | |||
}); | |||
}()); | }()); | ||