Fixes #5936 - Only call fact_name_class.maximum if necessary

This patch stops calling fact_name_class.maximum every
time facts are uploaded, doing only the query when it's
strictly necessary (basically, when there are facts to create).

This optimization reduces the number of DB queries, which
might have a big impact when the size of the fact_names
table is significantly big.