South Africa’s government announced the recent arrest of thousands of undocumented migrants as it sought to keep a lid on growing protests against foreigners stoked by factors including rampant crime and unemployment.