Mean-field theory of sandpiles