Adding extra search text to a MediaWiki search que

2019-02-19 14:16发布

问题:

We are using the InputBox extension. We want to have a search function using two checkboxes like this:

So, a user can select neither, one or both checkboxes which refer to specific text on the page. That is, if the user selects "Platform 1.0" the search will return pages with "Platform 1.0" on the page. (Actually: they will return pages using a specific semantic property which results in text on the page.)

So we want a way to search for the platform text and the text entered in the search box.

We've tried:

  • putting these pages into categories, but we did this using templates (categories in the templates) which does not make them searchable using incategory.
  • writing new PHP code for InputBox using the namespaces / prefix code, but these both use specific words in the search API, apparently. We can't add specific words to e.g. the search URL.
  • using Semantic's search by property, but we can't add custom text (entered by the user) to that query.

Ideas very welcome!

回答1:

Tough one, you could try utilizing JavaScript to add a keyword you want when each of the checkboxes is selected. Something like so: (jQuery is used for comfort, but normal JS can be used as well if not an option):

$("#search").submit(function(e) {
    var search_value = $('input[type="text"]', this).val();
    if ($("#checkbox1").checked) { search_value = "SomeValue " + search_value; }
    if ($("#checkbox2").checked) { search_value = "SomeOtherValue " + search_value; }

    $('#search input[type="text"]').val(search_value);
    $(this).submit();
    e.preventDefault();
});

Note, should the user disable JavaScript for some reason, your functionality will be damaged, which is always a bad practice. Only use this in case you've really extracted all other possibilities.