Content deleted Content added
→top: itals |
→Change log: the |
||
(20 intermediate revisions by the same user not shown) | |||
Line 1:
{{User:The Transhumanist/Workshop boilerplate/Lead hatnote}}
: '''''This script is operational
[[User:The Transhumanist/StripSearchSorted.js|StripSearchSorted.js]]: provides a menu item to strip search results down to bare page names, sort them alphabetically, and add bullet list wikicode formatting for easy copying and pasting into articles. The menu item is a toggle switch that turns this function on and off, and remembers its status for all searches. By default, just by being installed, the script removes from search results the redirected entries and members of matching categories (as they don't match the search string), even if you don't use the menu item. For Vector skin only.
= Script's workshop =
Line 10:
== Description / instruction manual ==
: '''''This script is operational''''', ''but there is a quirk in wikEd: When results are copied/pasted into wikEd, the results are erroneously double spaced. Clicking on undo in wikEd reverts it to single spaced as initially intended. (I don't know why. If you do, please tell me.)''
[[User:The Transhumanist/StripSearchSorted.js|StripSearchSorted.js]]: provides a menu item to strip search results down to bare page names, sort them alphabetically, and add bullet list wikicode formatting for easy copying and pasting into articles. The menu item is a toggle switch that turns this function on and off, and remembers its status for all searches. By default, just by being installed, the script removes from search results the redirected entries and members of matching categories (as they don't match the search string), even if you don't use the menu item. For Vector skin only.
In other words, when the menu item is turned on, this script reduces the search results to a list of links. It strips out the data between the page names, including that annoying "from redirect" note. It adds <code>* [[]]</code> to each entry and sorts them so they look like this:
Line 26:
Once installed, the menu item "SR sort" will appear in the side bar tools menu, specifying what action it is ready to perform (either "turn on" or "turn off").
{{User:The Transhumanist/Workshop boilerplate/Install}}
=== Known issues ===
Line 36 ⟶ 32:
Quirk in wikEd: When results are copied/pasted into wikEd, the results are erroneously double spaced. Clicking on undo in wikEd reverts it to single spaced as initially intended. (I don't know why. If you do, please tell me.)
=== General approach ===
Line 44 ⟶ 38:
The script uses the jQuery method .hide() for stripping the elements by class name. Here's an example of stripping out elements with the class name "searchalttitle":
<syntaxhighlight lang="javascript">
$( ".searchalttitle" ).hide();
</syntaxhighlight>
Learn about methods at https://www.w3schools.com/js/js_object_methods.asp
Line 58 ⟶ 54:
=== Activation filters ===
I didn't know what else to call these. I wanted the program to only work when intended, and only on intended pages (search result pages). So, I applied the [https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals conditional, '''''if'''''], as follows...
==== Vector skin activation filter ====
I use the Vector skin, and haven't tested the script on any other skin, so the script basically says "''if'' the vector skin is in use, do what's between the curly brackets". (Which includes the
<syntaxhighlight lang="javascript">
Line 71 ⟶ 67:
</syntaxhighlight>
===== <div style="font-size:90%">mw.config.get ( 'skin' )</div> =====
This looks up the value for skin (the internal name of the currently used skin) saved in MediaWiki's configuration file.
* [https://www.w3schools.com/jquery/ajax_get.asp jQuery get() Method]
* [https://www.mediawiki.org/wiki/Manual:Interface/JavaScript#mw.config mw.config]
===== <div style="font-size:90%">logical operators</div> =====
"<code>===</code>" means "equal value and equal type"
* [https://www.w3schools.com/js/js_comparisons.asp JavaScript Comparison and Logical Operators]
==== Page title activation filter ====
Line 80 ⟶ 88:
}
</syntaxhighlight>
=== Prep work ===
Line 147 ⟶ 153:
* 2017-10-27
** Forked script from a copy of [[User:The Transhumanist/StripSearchInWikicode.js]], and forked this workshop from a copy of [[User talk:The Transhumanist/StripSearchInWikicode.js]].
* 2017-
** Evad37 provided sequence for sorting the search results
* 2017-11-09
** Add toggle switch (dual menu item)
** Apply class of "Stripped" to the modified results, so that they can be removed to make way for original results
** Make switch swap out results between original and modified, and vice versa
* 2018-01-20
** Added TrueMatch function (intitle bug workaround)
*** Evad37 provided the 2 key lines
== Task list ==
===
=== Desired/completed features ===
Line 516 ⟶ 530:
== Adding a filter to StripSearchSorted.js ==
:''Originally posted to Evad37's talk page:''
There's a really annoying design flaw in WP's search's intitle feature. Common words like "of" are ignored, even though they are included within a quoted phrase. So, intitle:"of Boston" is interpreted as just intitle:Boston. And the search results are filled with non-matching results. To make matters worse, the search results include matches of the phrase in the contents of pages, watering the results down even more to inlcude pages that don't even have "Boston" in the title. What I need is for results to strictly match the term provided after "intitle:".
Line 600 ⟶ 614:
:::: So, there is no way to match null in regex? So you can't match null or whatever the string is, using the pipe character? ''[[User talk:The Transhumanist|The Transhumanist]]'' 04:26, 11 December 2017 (UTC)
:::::If you want to check if a variable is null or undefined, just do <code>someVar == null</code> (gives true if someVar is null/undefined, false otherwise). You can combine this with other logical tests using <code>||</code> , <code>&&</code> , <code>!</code> as usual. - <u>'''[[User:Evad37|Evad]]''37'''''</u> <span style="font-size:95%;">[[[d:w:User talk:Evad37|talk]]]</span> 04:37, 11 December 2017 (UTC)
== Adding TrueMatch to StripSearchSorted ==
:''Originally posted to Evad37's talk page''
I'm in the process of trying to fix the intitle bug in Wikipedia's search, by providing the solution as a function within [[User:The Transhumanist/StripSearchSorted.js|StripSearchSorted.js]].
The intitle bug is that when you enter a search phrase in WP's search box with a common word (like this: intitle:"in Germany"), the titles in the search results don't match.
I'm almost done, but I can't figure out how to get :contains to accept a variable:
<syntaxhighlight lang="javascript">
function TrueMatch() {
// The purpose of this function is to filter out non-matches
// Activation filter:
// Run this function only if 'intitle:"' is in the page title
// Notice the lone " after intitle:
if (document.title.indexOf('intitle:"') != -1) {
// Body of function
// Create variable with page title
var docTitle = document.title;
// Display on screen for checking
//alert ( docTitle );
// Extract the intitle search string from the html page title
// We want the part between the quotation marks
var regexIntitle = new RegExp('intitle:"(.+?)(")(.*)','i');
var intitle;
intitle = docTitle.replace(regexIntitle,"$1");
//alert ( intitle );
// Filter out search results that do not match
$( "li" ).not( 'li:contains(" + intitle + ")' ).remove();
}
}
</syntaxhighlight>
It works fine up until that last line. I want to remove all li elements that do not contain the text in the intitle variable. ''[[User talk:The Transhumanist|The Transhumanist]]'' 07:51, 19 January 2018 (UTC)
:Instead of passing a single string for the selector, you need to build a string up around the variable:
:<syntaxhighlight lang="javascript">$( "li" ).not( 'li:contains("' + intitle + '")' ).remove();</syntaxhighlight>
:The <code>'li:contains("' + intitle + '")'</code> gets processed first, and the result is passed through to <code>.not()</code>. Or if you wanted to be a bit more explicit, you could do something like
:<syntaxhighlight lang="javascript">
var intitle_selector = 'li:contains("' + intitle + '")';
$( "li" ).not( intitle_selector ).remove();
</syntaxhighlight>
: - <u>'''[[User:Evad37|Evad]]''37'''''</u> <span style="font-size:95%;">[[[d:w:User talk:Evad37|talk]]]</span> 13:59, 19 January 2018 (UTC)
:: I tried both methods you posted above. I tested it on intitle:"of Australia". The script runs, and strips out the details as it is supposed to. And it is sorting the results. But it isn't removing the non-matches. It's like it's matching everything, and therefore removing nothing. (When it matches nothing, like in my version above, it removes everything, leaving the results blank). I reactivated the alerts, and those show up fine. It's still the last line that isn't working. When you replace it with <code>$( "li" ).remove();</code>, it removes all results. ''[[User talk:The Transhumanist|The Transhumanist]]'' 02:16, 20 January 2018 (UTC)
:: Testing further on the "of Australia" search...
:: <code>$( "li").not('li:contains( "of" )').remove();</code> resulted in blank results (ie, none).
:: <code>$( "li").not('li:contains( of )').remove();</code> resulted in no matches (ie results unaffected).
:: So, it looks like the first one is matching nothing, causing all li elements to be removed, while the second one is matching everything, causing no li elements to be removed. ''[[User talk:The Transhumanist|The Transhumanist]]'' 03:11, 20 January 2018 (UTC)
:: I stared at the current page source, and discovered spans with the class "searchmatch", the contents of which appear to have been causing false matches. So, I blasted those with:
::<syntaxhighlight lang="javascript">
// First, strip out the searchmatch class elements (they match).
$( 'li').find( '.searchmatch').remove();
</syntaxhighlight>
:: Then, with the above line in place, I tested your solution again, but it didn't work:
::<syntaxhighlight lang="javascript">
$( "li" ).not( 'li:contains("' + intitle + '")' ).remove();
</syntaxhighlight>
:: The results turned up blank, which means it removed everything.
:: Ironically, doing the opposite works:
::<syntaxhighlight lang="javascript">
$( 'li:contains("' + intitle + '")').remove();
</syntaxhighlight>
:: Unfortunately, this removes precisely the entries the user wants to keep. ''[[User talk:The Transhumanist|The Transhumanist]]'' 08:26, 20 January 2018 (UTC)
:::I think we need to be more specific, and target the main link of each result - since the value of <code>intitle</code> will be somewhere within the <code>li</code>, just not neccesarily in the title. Plus we can limit the searching of <code>li</code>s to just the search results, rather than the whole page:
:::<syntaxhighlight lang="javascript">
// Mark true results with a class
$('.mw-search-results').find('li').has( 'div > a:contains("' + intitle + '")' ).addClass('truematch');
// Remove other results
$('.mw-search-results').find('li').not('.truematch').remove();
</syntaxhighlight>
:::Which basically means: In the <code>mw-search-results</code>, find the <code>li</code>s which have a <code>div</code> that itself has (as a direct child element) an <code>a</code> that contains the text <code>intitle</code>, and add the class <code>truematch</code> to those <code>li</code>s. Then, in the <code>mw-search-results</code>, find the <code>li</code>s which do not have the class <code>truematch</code>, and remove them. - <u>'''[[User:Evad37|Evad]]''37'''''</u> <span style="font-size:95%;">[[[d:w:User talk:Evad37|talk]]]</span> 09:25, 20 January 2018 (UTC)
:::: That did the trick. It works beautifully. Thank you.
:::: This leads the way to the development of two related programs:
::::# StripSearchFilter.js – will allow the user to enter additional search terms to filter down the results, including a term to keep or a term to discard. Can use it multiple times to further refine the result.
::::# SearchSuite.js – will put selected features on their own switches so they can be turned on and off. Like the details stripping, and the inserted wikicode. It will also include the search filter feature mentioned above.
:::: I'll keep you posted. ''[[User talk:The Transhumanist|The Transhumanist]]'' 11:28, 20 January 2018 (UTC)
|