User talk:V111P/js/What Links Here link filter

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Testing[edit]

Well, it works! I had to figure out what the templates were actually called, but once I did that, it went from 2008 links to 900. (I forgot to set it to article space only.) So less than half. Still a lot of links to wade through, but more accessible than going through pages and pages of cities in Texas! I'll try it on some place in California next.

Thanks for the pointer to F12 for the console--I never knew that was there! How would I access the log if I wanted to send it to you? (This is Firefox 28, on Windows XP.) There were a bunch of errors from loading the page; would those be germane?

Thank you for this! It's like magic. :-) - Gorthian (talk) 07:20, 23 April 2014 (UTC)[reply]

You are welcome. :) No, these errors have nothing to do with my script. As long as it's working there's no need to send logs. Ideally I would print the info somewhere on the page, instead of in the console - I should change the script later to do that. --V111P (talk) 11:03, 23 April 2014 (UTC)[reply]

Bug report![edit]

Hello there V111P! Your script interested me because I'm in the process of creating a new WLH userscript that will allow users to filter and sort the results delivered by WLH. In the process of trying out your testcase, I discovered what seems to be a bug. The terminating error I get in the console is TypeError: 'log' called on an object that does not implement interface Console. and when I click on it to see where it is, I get:

                for (var i = 0; i < p.length; i++) {
                    titles.push(p[i].title);
                }
                var elsToRemove = [];

                log('using page: ' + pageNames[0]);
                log('links before removal: ' + $('ul#mw-whatlinkshere-list li').size());
                $('ul#mw-whatlinkshere-list li').each(function (i, li) {
                    var lnk = $(li).find('a').first();
                    if ($.inArray($(lnk).text(), titles) > -1) {

The only reference I see to what log may be defined as anywhere else in the script is:

    var log = (window.console && console.log) || function () {};

I'd just dump line 9 and replace all instances of log() with console.log(); I also wouldn't concatenate the pieces of the output together, I'd use the formatted method.

replacement code that works and fixes this bug
// 2014-11-30
if (mw.config.get('wgCanonicalSpecialPageName') == 'Whatlinkshere')
$(function () {
	var helpPage = '//en.wikipedia.org/wiki/User:V111P/js/What_Links_Here_extra_filter';
	var spanId = 'excludePagesLinkingTo_span';
	var inputId = 'excludePagesLinkingTo_textInput';
	var formId = 'excludePagesLinkingTo_form';
	var pageNames = [];

 	$('#' + spanId).remove();
 	$('div#mw-content-text form + fieldset')
		.append($('<span id="' + spanId + '"/>')
				.append(' | Hide pages linking to or transcluding: ')
				.append($('<form id="excludePagesLinkingTo_form" style="display:inline;">'
						  + '<input type="text" id="' + inputId + '" size="40"/> '
						  + '<input type="submit" value="Go"/> (<a href="'
						  + helpPage + '" target="_blank">?</a>)</form>')));
 	$('#' + formId).submit(function (e) {
		e.preventDefault();
		pageNames = $('#' + inputId).val().split('|');
		request();
	});
 	// add a 5000 link next to the 500 link
	var limit500Lnk = $('#mw-content-text > a')
		.filter(function() { return $.text([this]) == '500'; });
	if (limit500Lnk[0]) {
		limit500Lnk.after(' | <a title="' + limit500Lnk.attr('title') + '" href="'
				+ limit500Lnk.attr('href').replace(/limit=500/, 'limit=5000') + '">5000</a>');
	}
 	function request(blcontinue) {
		$.ajax({
			url: '/w/api.php?action=query&list=backlinks&format=json&bllimit=max&bltitle='
				+ encodeURIComponent(pageNames[0]) + (blcontinue ? '&blcontinue=' + blcontinue : ''),
			dataType: 'json',
			error: function () {
				$('div#mw-content-text form > fieldset').append(' Error. ');
			},
			success: function (result) {
				if (!result || !result.query || !result.query.backlinks) {
					console.error('What Links Here Script error 1.');
					return;
				}
 
				var p = result.query.backlinks;
				var titles = [];
				for (var i = 0; i < p.length; i++) {
					titles.push(p[i].title);
				}
				var elsToRemove = [];
 
				console.log('using page: %o', pageNames[0]);
				console.log('links before removal: %o', $('ul#mw-whatlinkshere-list li').size());
				$('ul#mw-whatlinkshere-list li').each(function (i, li) {
					var lnk = $(li).find('a').first();
					if ($.inArray($(lnk).text(), titles) > -1) {
						elsToRemove.push(li);
					}
				});
				console.log('removed: %d', elsToRemove.length);
				$(elsToRemove).remove();
				console.log('links after removal: %o', $('ul#mw-whatlinkshere-list li').size());
 
				if (result['query-continue']) {
					console.info('Continue from: %s', result['query-continue'].backlinks.blcontinue);
					request(result['query-continue'].backlinks.blcontinue);
				}
				else {
					pageNames.shift();
					if (pageNames.length > 0)
						request();
				}
 
			}
		});
	}
});

Anyways, happy scripting! — {{U|Technical 13}} (etc) 21:33, 30 November 2014 (UTC)[reply]

Thank you, Technical 13. I probably didn't test that script in all browsers as I should have. I still want to check for the console because otherwise you get an error in some browsers without a console or if the console is not opened (IE I think did that). --V111P (talk) 07:50, 1 December 2014 (UTC)[reply]

Transclusion[edit]

Hello. Is template calling transclusion? If yes, then It does not work. If not, can it be implemented? (hiding pages that use certain templates) Thanks.--Dixtosa (talk) 20:54, 19 March 2015 (UTC)[reply]

Hi, Dixtosa. Thanks for reporting this, but please give an example so I can verify it and try to understand what the problem is. --V111P (talk) 01:13, 21 March 2015 (UTC)[reply]
Is it possible to exclude pages calling (thus, using curly braces rather than square brackets) some specific templates?--Dixtosa (talk) 19:05, 22 March 2015 (UTC)[reply]
@Dixtosa: Yes, that's what transclusion is. I don't know if it will work if Lua is used to create the links in the template. But the example given in the instructions with Wichita Falls, Texas and Template:Texas|Template:Texas_counties works when I test it. --V111P (talk) 00:08, 23 March 2015 (UTC)[reply]
Doh, I found out what caused the confusion: I thought this worked for every template but it only works for {{Navbox}}-type of templates because they link to themselves (the links V·T·E in the upper-left corner). Still useful script.--Dixtosa (talk) 16:28, 23 March 2015 (UTC)[reply]
@Dixtosa: I corrected the documentation with your explanation. Hiding all pages that use a given template can also easily be implemented. Can you explain in what cases would that be useful? --V111P (talk) 04:56, 25 March 2015 (UTC)[reply]
@V111P: I can foresee its usage in English Wiktionary (where I work), but it is not vital (if it gets so important we can always change the template temporarily so that it links to itself) and, thus, it is a matter of how difficult it is to implement it. Can you hint me? I myself create user scripts too, so I can help. --Dixtosa (talk) 19:02, 25 March 2015 (UTC)[reply]
@Dixtosa: It can be done with this: [1]. You can test this script: User:V111P/js/wlhFilters.js. But this isn't a very efficient way of doing it — to get all pages where a template is embedded in (could be many thousands of pages, and you can only get 500 at a time (but the limit is 5000 for a bot)) and see if any of these pages is in your list and remove it if it is. The difference with the navigation templates is that they are not used on that many pages as some other templates are. --V111P (talk) 13:09, 26 March 2015 (UTC)[reply]