Issue #1103478 by Berdir, catch: port wildcard optimization to Drupal 7, also fixes tests (except one remaining session bug.