Modifying MediaWiki user preferences at once

Since long time ago, it's possible to change default user preferences in MediaWiki by simply adding a line of code in LocalSettings.php. Example:
$wgDefaultUserOptions['editsection'] = 0;
However, if the wiki is not new, there could be already several users who will have inherited previous default preferences before any change is performed in LocalSettings.php.
In order to solve this, in recent MediaWiki versions there is a maintenance script for changing a user preference value for all users in the wiki.
Nonetheless, in my case, I did not want to change the preference value for all users, but only for those ones belonging to a certain group. Of course, more complex filtering criteria could be devised…
For anyone who might need it, the code (in Perl) I used below:
#!/usr/bin/perl -w
use strict;
use DBI;

#Database credentials
my $db = "wikidb";
my $user = "wikiuser";
my $pw = "xxxx";

# Filter by group
my $group = shift;
# 2 options to be changed. Hash array (it could be read from a file)
my %options = ("editsection", 1, "enotifwatchlistpages", 1 );

my $dbh = DBI->connect('DBI:mysql:'.$db, $user, $pw
                   ) || die "Could not connect to database: $DBI::errstr";

$group = $dbh->quote($group);
my $qstring = "SELECT  ug_user FROM user_groups WHERE ug_group=".$group;
#print $qstring, "\n";
my $sth = $dbh->prepare($qstring);

$sth->execute();
#Commented number of matching users
#print $sth->rows();
#print "\n";
while (my $row = $sth->fetchrow_arrayref()) {

   my $ustring = "SELECT user_options FROM user WHERE user_id=".$row->[0];
   my $uth = $dbh->prepare($ustring);
   $uth->execute();
   my $uresult = $uth->fetchrow_arrayref();
   my $outcome = processpref($uresult->[0], \%options);
   #print $uresult->[0], "\n";
   if ($outcome ne $uresult->[0]) {
        #print STDERR $outcome, "\n";
        $outcome = $dbh->quote($outcome);
        #Update user options if any new param
        my $upstring = "UPDATE user SET user_options=$outcome where user_id=$row->[0]";
        my $updo = $dbh->do($upstring);
   }
   $uth->finish;
}

$sth->finish;

$dbh->disconnect();
sub processpref {

        my $result = shift;
        my $options = shift;
        my $outcome = "";
        my @exists;

        my @loptions = split(/\n/, $result);
        #Remove redundant array elements
        @loptions = uniq2( @loptions );

        foreach my $loption (@loptions) {

                my $modif = 0;
                for ( grep { $loption=~/$_\b/ } keys %{$options} ) {

                        #print STDERR $loption." - ".$_, "\n";
                        $outcome.= $_."=".$options->{$_}."\n";
                        #print STDERR " --> ".$_."=".$options->{$_}."\n";
                        #Note down if modified
                        push(@exists, $_);
                        $modif++;
                }
                unless ($modif > 0) {
                        $outcome.=$loption."\n";
                }
        }

        #Add not modified params
        foreach my $option (keys %options) {
                my $ex = 0;
                foreach my $exist (@exists) {

                        if ($option eq $exist) {$ex++;}
                }
                if ($ex == 0) {
                        $outcome.= $option."=".$options{$option}."\n";
                }

        }

        chomp($outcome);
        return($outcome);
}

# Reference: http://stackoverflow.com/questions/7651/how-do-i-remove-duplicate-items-from-an-array-in-perl
sub uniq2 {
    my %seen = ();
    my @r = ();
    foreach my $a (@_) {
        unless ($seen{$a}) {
            push @r, $a;
            $seen{$a} = 1;
        }
    }
    return @r;
}
WARNING: With these actions, we might be changing user preference values without users' explicit consent.

Comments

Using monetary the simplest way really, I made the decision to put together strategies for submitting unemployment incentives. philosophy would have been to indicate the fact that the being out of work function works out, just what the frequent is for receiving as well as rejecting promises, which usually add in books away from our ordeal about what the ideal plans as well common mistakes are working declaring redundancy benefits. =-=