UserOptionsManager apparently makes it impossible for callers to set the user option value to an empty string, when there was no pre-existing user_properties row (when the previous value was the option's default).
See my shell.php fiddling below (note I have $wgDefaultUserOptions['userjs-foo'] = 123; in my LocalSettings.php; at the start of the experiments, there are no user_properties rows existing for userjs-foo):
I have no name!@5f2d8572a442:/var/www/html/w$ php maintenance/shell.php
*******************************************************************************
NOTE: Do not run maintenance scripts directly, use maintenance/run.php instead!
Running scripts directly has been deprecated in MediaWiki 1.40.
It may not work for some (or any) scripts in the future.
*******************************************************************************
PHP Notice: Writing to directory /.config/psysh is not allowed. in /var/www/html/w/vendor/psy/psysh/src/ConfigPaths.php on line 327
Psy Shell v0.12.0 (PHP 8.1.20 — cli) by Justin Hileman
> $uom = \MediaWiki\MediaWikiServices::getInstance()->getUserOptionsManager()
= MediaWiki\User\Options\UserOptionsManager {#5766}
> $user = User::newFromId(1)
= MediaWiki\User\User {#7123
+mId: 1,
+mName: null,
+mActorId: null,
+mRealName: null,
+mEmail: null,
+mTouched: null,
+mEmailAuthenticated: null,
+mFrom: "id",
mId: 1,
mName: null,
mActorId: null,
mRealName: null,
mEmail: null,
mTouched: null,
mEmailAuthenticated: null,
mFrom: "id",
}
> $uom->getOption($user, 'userjs-foo') # this is ok, 123 is the default value for userjs-foo
= 123
> $uom->setOption($user, 'userjs-foo', '') # i want
= null
> $uom->getOption($user, 'userjs-foo') # this claims everything worked well
= ""
> $uom->saveOptions($user)
= null
> $uom->getOption($user, 'userjs-foo') # UserOptionsManager claims the operation worked even after saving into the DB
= ""
> ^D
INFO Ctrl+D.
I have no name!@5f2d8572a442:/var/www/html/w$ php maintenance/shell.php
*******************************************************************************
NOTE: Do not run maintenance scripts directly, use maintenance/run.php instead!
Running scripts directly has been deprecated in MediaWiki 1.40.
It may not work for some (or any) scripts in the future.
*******************************************************************************
PHP Notice: Writing to directory /.config/psysh is not allowed. in /var/www/html/w/vendor/psy/psysh/src/ConfigPaths.php on line 327
Psy Shell v0.12.0 (PHP 8.1.20 — cli) by Justin Hileman
> $uom = \MediaWiki\MediaWikiServices::getInstance()->getUserOptionsManager()
= MediaWiki\User\Options\UserOptionsManager {#5766}
> $user = User::newFromId(1)
= MediaWiki\User\User {#7123
+mId: 1,
+mName: null,
+mActorId: null,
+mRealName: null,
+mEmail: null,
+mTouched: null,
+mEmailAuthenticated: null,
+mFrom: "id",
mId: 1,
mName: null,
mActorId: null,
mRealName: null,
mEmail: null,
mTouched: null,
mEmailAuthenticated: null,
mFrom: "id",
}
> $uom->getOption($user, 'userjs-foo') # despite changing userjs-foo value to '' in the previous session, the change did not persist
= 123Despite UserOptionsManager first claiming everything works correctly, in the second session, it insists on userjs-foo evaluating to 123 (the default value), which is not expected.
If I first set the option to 321 and then change it to an empty string, everything works as expected:
I have no name!@5f2d8572a442:/var/www/html/w$ php maintenance/shell.php
*******************************************************************************
NOTE: Do not run maintenance scripts directly, use maintenance/run.php instead!
Running scripts directly has been deprecated in MediaWiki 1.40.
It may not work for some (or any) scripts in the future.
*******************************************************************************
PHP Notice: Writing to directory /.config/psysh is not allowed. in /var/www/html/w/vendor/psy/psysh/src/ConfigPaths.php on line 327
Psy Shell v0.12.0 (PHP 8.1.20 — cli) by Justin Hileman
> $uom = \MediaWiki\MediaWikiServices::getInstance()->getUserOptionsManager()
= MediaWiki\User\Options\UserOptionsManager {#5766}
> $user = User::newFromId(1)
= MediaWiki\User\User {#7123
+mId: 1,
+mName: null,
+mActorId: null,
+mRealName: null,
+mEmail: null,
+mTouched: null,
+mEmailAuthenticated: null,
+mFrom: "id",
mId: 1,
mName: null,
mActorId: null,
mRealName: null,
mEmail: null,
mTouched: null,
mEmailAuthenticated: null,
mFrom: "id",
}
> $uom->getOption($user, 'userjs-foo')
= 123
> $uom->setOption($user, 'userjs-foo', 321)
= null
> $uom->saveOptions($user)
= null
> ^D
INFO Ctrl+D.
I have no name!@5f2d8572a442:/var/www/html/w$ mysql -hmariadb-main -uwikiuser -p awiki
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 637
Server version: 10.11.4-MariaDB-log Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [awiki]> select * from user_properties where up_property='userjs-foo' and up_user=1;
+---------+-------------+----------+
| up_user | up_property | up_value |
+---------+-------------+----------+
| 1 | userjs-foo | 321 |
+---------+-------------+----------+
1 row in set (0.006 sec)
MariaDB [awiki]> Bye
I have no name!@5f2d8572a442:/var/www/html/w$ php maintenance/shell.php
*******************************************************************************
NOTE: Do not run maintenance scripts directly, use maintenance/run.php instead!
Running scripts directly has been deprecated in MediaWiki 1.40.
It may not work for some (or any) scripts in the future.
*******************************************************************************
PHP Notice: Writing to directory /.config/psysh is not allowed. in /var/www/html/w/vendor/psy/psysh/src/ConfigPaths.php on line 327
Psy Shell v0.12.0 (PHP 8.1.20 — cli) by Justin Hileman
> $uom = \MediaWiki\MediaWikiServices::getInstance()->getUserOptionsManager()
= MediaWiki\User\Options\UserOptionsManager {#5766}
> $user = User::newFromId(1)
= MediaWiki\User\User {#7123
+mId: 1,
+mName: null,
+mActorId: null,
+mRealName: null,
+mEmail: null,
+mTouched: null,
+mEmailAuthenticated: null,
+mFrom: "id",
mId: 1,
mName: null,
mActorId: null,
mRealName: null,
mEmail: null,
mTouched: null,
mEmailAuthenticated: null,
mFrom: "id",
}
> $uom->getOption($user, 'userjs-foo') # this is expected, 321 is in the DB table
= "321"
> $uom->setOption($user, 'userjs-foo', '') # try setting it to an empty string now
= null
> $uom->saveOptions($user)
= null
> ^D
INFO Ctrl+D.
I have no name!@5f2d8572a442:/var/www/html/w$ mysql -hmariadb-main -uwikiuser -p awiki
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 639
Server version: 10.11.4-MariaDB-log Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [awiki]> select * from user_properties where up_property='userjs-foo' and up_user=1;
+---------+-------------+----------+
| up_user | up_property | up_value |
+---------+-------------+----------+
| 1 | userjs-foo | |
+---------+-------------+----------+
1 row in set (0.004 sec)
MariaDB [awiki]>Noticed while working at T353225: Echo: Make use of conditional user defaults.