Theory of superconducting wire networks and Josephson-junction arrays in magnetic fields