XWIKI-6946: Fix a security issue

 - Refactor properly the fix introduce initially in release 4.1rc1
 - Increase security by apply the fix for empty wiki as well