Page MenuHomePhabricator

Add function to store regex match(es)
Closed, ResolvedPublic

Description

A thing that AF lacks is a function to store the match of a regex. It would be really helpful in many situations, such as the ones in which you want to determine if the value of a parameter within a template was changed. For these and other situations, here is the "getmatch" function. Syntax is getmatch(pattern, text), the result is an array with the same properties as php function preg_match's third parameter.

For instance, suppose that you have "|parameter=value" and you want to check if it's changed. You can do

old := getmatch( "\|parameter=([^|}]+)", removed_lines)[1];
new := getmatch( "\|parameter=([^|}]+)", added_lines)[1];

and check new against old, depending on the context (the example is referred to a very basic situation).
Moreover, there might be situations when you also need another match. A case might be:

olds := getmatch( "\|([^=]+)=([^|}]+)", removed_lines);
old_par := olds[1];
old_val := olds[2];

And so on. When dealing with small arrays you don't actually need a for loop or any other way to iterate over them.

Event Timeline

Daimona triaged this task as Medium priority.
Daimona moved this task from Backlog to Filtering features on the AbuseFilter board.

Change 389778 had a related patch set uploaded (by Daimona Eaytoy; owner: Daimona Eaytoy):
[mediawiki/extensions/AbuseFilter@master] Add getmatch function

https://gerrit.wikimedia.org/r/389778

Can you please update the task description and give an explicit example of how it would be used to check for changes of a template parameter's value?

@Huji task updated and replied to the comment, too.

olds := getmatch( "\|([^=]+)=([^|}]+)", removed_lines);
old_par := olds[1];

Can we actually do something like olds[1]? I was not aware that this is supported.

Yes, we do, even though arrays only have a really really tiny documentation: https://www.mediawiki.org/wiki/Extension:AbuseFilter/Rules_format#Variables
Anyway, as I was saying in the patch page, this feature was already successfully tested on private wiki.

Change 389778 merged by jenkins-bot:
[mediawiki/extensions/AbuseFilter@master] Add get_matches function

https://gerrit.wikimedia.org/r/389778

@Daimona Thank you for creating this new function! Looks like it will go out with this week's MediaWiki train :)

Could you make sure this gets reported in next week's Tech/News?

@MusikAnimal and thank you for patiently reviewing my patch and adding testunit! I'll do my best to report it, while focusing on other AbuseFilter tasks that really need to be solved.

Forgot to ask: so this will be released today with technews/46 or with 47?

Forgot to ask: so this will be released today with technews/46 or with 47?

Issue 47. On Fridays the newsletter is frozen so that translations can be made in time for when it goes out on Monday.

You can edit the issue directly, just try to use very simple English. The Tech News maintainers will copy edit as needed :)