User talk:Rillke/checkCat.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Pretty cool, but I don't understand the regexp

var chCatRE = /\{\{[Cc]heck [Cc]ategories\s*\|(?:.|\n){10,51}\}\}/g;

Why the repetition from 10 to at most 51? That fails e.g. on File:"LV and Meaku at Trey Songz Ready Concert in the iE".jpg (90 characters between the initial vertical bar and the closing curly braces). I'd use a non-greedy match "*?" instead:

var chCatRE = /\{\{[Cc]heck categories\s*\|(?:.|\n)*?\}\}/g;

Or, if we can be sure that there will be no single "}" inside the template, simply:

var chCatRE = /\{\{[Cc]heck categories\s*\|[^\}]*\}\}/g;

Or am I missing something? (And of course, if the template may contain other template invocations in its parameters, a simple regexp won't be able to do the job.) Lupo 00:19, 4 February 2012 (UTC)[reply]

You are right. What about
var chCatRE = /\{\{[Cc]heck categories\s*\|((?:.|\n)*?(?:\{\{[^\}\{]+\}\})(?:.|\n)*?)*?\}\}/g;
This will allow any {{Template}} inside but ensures that it has two closing tags and then a second replace for cases without any template inside:
var chCatRE2 = /\{\{[Cc]heck categories\s*\|(?:.|\n)*?\}\}/g;
or in combination:
var chCatRE = /\{\{[Cc]heck categories\s*\|((?:.|\n)*?(?:\{\{[^\}\{]+\}\})(?:.|\n)*?)*?\}\}|\{\{[Cc]heck categories\s*\|(?:.|\n)*?\}\}/g;
And what do you think about adding this to the HotCat-definition at MediaWiki:Gadgets-definition in a separate script, of course so other wikis are not affected. -- RE rillke questions? 10:10, 4 February 2012 (UTC)[reply]
I wouldn't try handling templates inside the {{Check categories ... }}. I don't know how many such cases exist at all (I think these templates are added by a bot, so possibly there are none at all), and regexps can't handle recursion anyway. So just
var chCatRE = /\{\{[Cc]heck categories\s*\|[^\}\{]*\}\}/g;
should be good enough. (It'll fail to match if there are templates there, so it should be safe.)
Adding this to the gadget definition of HotCat is a good idea. As a separate gadget it'd also work, but I guess anybody who'd like to use this already uses HotCat, so it's perhaps simpler to combine them.
Does mw.loader.using() guarantee that document-ready has occurred when the function is executed? If not, the $('#catlinks') at the end might not always find the category bar. If so, that would need to become $(function () { $('#catlinks')... }); Lupo 22:51, 5 February 2012 (UTC)[reply]
Ok, agreed. According to Krinkle, gadgets are loaded from the bottom of the page and therefore it should work despite $(document).ready has not fired. But I don't know about the Sizzle internals. Sice HotCat waits for the readyState of the document, I think wrapping the whole thing inside $(function() {...}) (the shortcut) would be no harm. Before adding to the gadgets-definitions, I will notify people on the Village Pump if someone has objections. -- RE rillke questions? 08:10, 6 February 2012 (UTC)[reply]