carry over manually parts of fixes for XWIKI-9120 / XWIKI-9870 to the 5.0.4 branch