Porting https://github.com/joomla/joomla-cms/pull/3914 to 2.5.x