Started a second PR at https://github.com/xwiki/xwiki-platform/pull/3946
When this second PR and its backport are merged, this issue can be closed.