=== modified file 'includes/menu.inc'
--- includes/menu.inc	2009-01-04 20:04:32 +0000
+++ includes/menu.inc	2009-01-22 02:05:47 +0000
@@ -607,8 +607,8 @@ function _menu_item_localize(&$item, $ma
  */
 function _menu_translate(&$router_item, $map, $to_arg = FALSE) {
   $path_map = $map;
-  if (!_menu_load_objects($router_item, $map)) {
-    // An error occurred loading an object.
+  if (!preg_match($router_item['mask'], implode($map, '/')) || !_menu_load_objects($router_item, $map)) {
+    // Either the path did not validate or an error occurred loading an object.
     $router_item['access'] = FALSE;
     return FALSE;
   }
@@ -690,6 +690,10 @@ function _menu_link_translate(&$item) {
     $item['localized_options'] = $item['options'];
   }
   else {
+    if (!preg_match($item['mask'], $item['link_path'])) {
+      $item['access'] = FALSE;
+      return FALSE;
+    }
     $map = explode('/', $item['link_path']);
     _menu_link_map_translate($map, $item['to_arg_functions']);
     $item['href'] = implode('/', $map);
@@ -871,6 +875,7 @@ function menu_tree_all_data($menu_name =
         'title_arguments',
         'type',
         'description',
+        'mask',
       ));
       for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
         $query->orderBy('p' . $i, 'ASC');
@@ -1041,6 +1046,7 @@ function menu_tree_page_data($menu_name 
           'title_arguments',
           'type',
           'description',
+          'mask',
         ));
         for ($i = 1; $i <= MENU_MAX_DEPTH; $i++) {
           $query->orderBy('p' . $i, 'ASC');
@@ -2098,6 +2104,7 @@ function menu_link_save(&$item) {
         'options' => serialize($item['options']),
         'customized' => $item['customized'],
         'updated' => $item['updated'],
+        'mask' => $item['mask'],
       ))
       ->execute();
   }
@@ -2169,6 +2176,7 @@ function menu_link_save(&$item) {
         'link_title' => $item['link_title'],
         'options' => serialize($item['options']),
         'customized' => $item['customized'],
+        'mask' => $item['mask'],
       ))
       ->condition('mlid', $item['mlid'])
       ->execute();
@@ -2619,6 +2627,72 @@ function _menu_router_build($callbacks) 
 }
 
 /**
+ * Build the mask for a menu item based on its definition.
+ *
+ * @param $item
+ *   The menu item to determine the mask of, as an array.
+ * @param $path
+ *   The path of the current menu item, e.g. node/%.
+ * @return
+ *   The string mask.
+ */
+function _menu_build_mask($item, $path) {
+  if (isset($item['mask'])) {
+    if (isset($item['mask']['rest'])) {
+      $rest = $item['mask']['rest'];
+      unset($item['mask']['rest']);
+    }
+    // If the mask is only for a wildcard inside the router path, we still
+    // need to fill the mask to covert all router parts.
+    $mask_length = $item['_number_parts'] - 1;
+    if (count($item['mask'])) {
+      $mask_length = max($mask_length, max(array_keys($item['mask'])));
+    }
+    $suffix = '';
+    $mask = '';
+    $parts = $item['_parts'];
+    for ($i = 0; $i <= $mask_length; $i++) {
+      $fallback = isset($parts[$i]) ? preg_quote($parts[$i], '#') : '';
+      $mask_part = isset($item['mask'][$i]) ? $item['mask'][$i] : $fallback;
+      _menu_build_mask_part($mask_part);
+      if (isset($parts[$i])) {
+        $prefix = $i ? '/' : '';
+      }
+      else {
+        // If we are past the last router path part then all the parts are
+        // optional, so we wrap them in conditional non grouping blocks.
+        $prefix = '(?:/';
+        $suffix .= ')?';
+      }
+      $mask .= $prefix . $mask_part;
+    }
+    $mask = '#^'. $mask;
+    if (isset($rest)) {
+      _menu_build_mask_part($rest);
+      $mask .= "(?:/$rest)*";
+    }
+    $mask .= $suffix . '$#';
+  }
+  else {
+    $mask = '#^'. str_replace('%', '\d+', preg_quote($path, '#')) . '$#';
+  }
+  return $mask;
+}
+
+function _menu_build_mask_part(&$mask_part) {
+  static $mask_map = array('*' => '[^/]+', '%' => '\d+', '%d' => '\d+', '%c' => '\w+');
+  if (isset($mask_map[$mask_part])) {
+    $mask_part = $mask_map[$mask_part];
+  }
+  elseif (preg_match('/^%([a-z_]+)$/', $mask_part, $matches)) {
+    $constant = strtoupper($matches[1]) . '_MASK';
+    if (defined($constant)) {
+      $mask_part = constant($constant);
+    }
+  }
+}
+
+/**
  * Returns TRUE if a path is external (e.g. http://example.com).
  */
 function menu_path_is_external($path) {

=== modified file 'modules/simpletest/tests/menu.test'
--- modules/simpletest/tests/menu.test	2008-12-28 18:27:14 +0000
+++ modules/simpletest/tests/menu.test	2009-01-22 02:10:39 +0000
@@ -51,7 +51,7 @@ class MenuRebuildTestCase extends Drupal
       'group' => t('Menu'),
     );
   }
-  
+
   /**
    * Test if the 'menu_rebuild_needed' variable triggers a menu_rebuild() call.
    */
@@ -75,5 +75,104 @@ class MenuRebuildTestCase extends Drupal
     $admin_exists = db_result(db_query("SELECT path from {menu_router} WHERE path = 'admin'"));
     $this->assertEqual($admin_exists, 'admin', t("The menu has been rebuilt, the path 'admin' now exists again."));
   }
-  
+}
+
+class MenuMasksTestCase extends DrupalWebTestCase {
+
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Menu masks'),
+      'description' => t('Tests the menu item masking functionality, making sure it works as expected in all cases.'),
+      // Don't use 'Menu' as the group in order to keep it separate from the
+      // menu module tests.
+      'group' => t('Menu system'),
+    );
+  }
+
+  /**
+   * Tests a set of masks to make sure they work properly.
+   */
+  function testMenuMasks() {
+    // This constant is used for the %testing_test placeholder in tests 8 - 12.
+    define('TESTING_TEST_MASK', '(test|testing)');
+
+    $items = array();
+
+    $items['testing'] = array(
+      'expected' => '#^testing$#',
+    );
+
+    $items['test#ing2'] = array(
+      'expected' => '#^test\#ing2$#',
+    );
+
+    $items['testing3/%'] = array(
+      'expected' => '#^testing3/\d+$#',
+    );
+
+    $items['testing4/%'] = array(
+      'mask' => array(1 => '*'),
+      'expected' => '#^testing4/[^/]+$#',
+    );
+
+    $items['testing5/%'] = array(
+      'mask' => array(1 => '%d'),
+      'expected' => '#^testing5/\d+$#',
+    );
+
+    $items['testing6/%'] = array(
+      'mask' => array(1 => '%c'),
+      'expected' => '#^testing6/\w+$#',
+    );
+
+    $items['testing7'] = array(
+      'mask' => array(1 => '[0-9a-f]{32}'),
+      'expected' => '#^testing7(?:/[0-9a-f]{32})?$#',
+    );
+
+    $items['testing8'] = array(
+      'mask' => array(1 => '%testing_test'),
+      'expected' => '#^testing8(?:/(test|testing))?$#',
+    );
+
+    $items['testing9'] = array(
+      'mask' => array('testing9', '%d', '%testing_test'),
+      'expected' => '#^testing9(?:/\d+(?:/(test|testing))?)?$#',
+    );
+
+    $items['testing10/%'] = array(
+      'mask' => array(1 => '%d', '%testing_test'),
+      'expected' => '#^testing10/\d+(?:/(test|testing))?$#',
+    );
+
+    $items['testing11/%/%'] = array(
+      'mask' => array(1 => '%d', '%testing_test'),
+      'expected' => '#^testing11/\d+/(test|testing)$#',
+    );
+
+    $items['testing12/%/%'] = array(
+      'mask' => array(2 => '%testing_test'),
+      'expected' => '#^testing12/\d+/(test|testing)$#',
+    );
+
+    $items['testing13'] = array(
+      'mask' => array('rest' => '%d'),
+      'expected' => '#^testing13(?:/\d+)*$#',
+    );
+
+    $items['testing14'] = array(
+      'mask' => array(1 => '%d', 'rest' => '%d'),
+      'expected' => '#^testing14(?:/\d+(?:/\d+)*)? #',
+    );
+
+    foreach ($items as $path => $item) {
+      $item['_parts'] = explode('/', $path);
+      $item['_number_parts'] = count($item['_parts']);
+      $result = _menu_build_mask($item, $path);
+      $this->assertEqual($result, $item['expected'], t('Expecting menu mask to be @expected, got @result.', array('@expected' => $item['expected'], '@result' => $result)));
+    }
+  }
 }

=== modified file 'modules/system/system.install'
--- modules/system/system.install	2009-01-21 16:58:41 +0000
+++ modules/system/system.install	2009-01-22 02:11:23 +0000
@@ -3197,6 +3197,15 @@ function system_update_7017() {
 }
 
 /**
+ * Add the 'mask' column to the {menu_router} table.
+ */
+function system_update_7018() {
+  $ret = array();
+  db_add_field($ret, 'menu_router', 'mask', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
+  return $ret;
+}
+
+/**
  * @} End of "defgroup updates-6.x-to-7.x"
  * The next series of updates should start at 8000.
  */

=== modified file 'modules/taxonomy/taxonomy.module'
--- modules/taxonomy/taxonomy.module	2009-01-14 21:16:19 +0000
+++ modules/taxonomy/taxonomy.module	2009-01-22 01:30:12 +0000
@@ -129,6 +129,7 @@ function taxonomy_menu() {
     'page callback' => 'taxonomy_term_page',
     'page arguments' => array(2),
     'access arguments' => array('access content'),
+    'mask' =>  array(2 => '(?:(?:(?:(?:\d+[+ ])+)|(?:(?:\d+,)*))\d+)', '%d', '(?:feed|page)'),
     'type' => MENU_CALLBACK,
   );
 

=== modified file 'modules/taxonomy/taxonomy.pages.inc'
--- modules/taxonomy/taxonomy.pages.inc	2009-01-14 21:16:19 +0000
+++ modules/taxonomy/taxonomy.pages.inc	2009-01-22 01:30:12 +0000
@@ -66,9 +66,6 @@ function taxonomy_term_page($terms, $dep
 
           node_feed($items, $channel);
           break;
-
-        default:
-          drupal_not_found();
       }
     }
     else {

