More fixes for issue 30282

There is still an error with the final insert statement (line 564-569) giving an error of:

Database query failed (error # %s): %s SQL=INSERT INTO "jos_messages"

But can't see the wood for the trees at the moment so will come back to it tomorrow if no one has already