? optimize_drupal_lookup_patch.patch Index: includes/path.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/path.inc,v retrieving revision 1.15 diff -u -r1.15 path.inc --- includes/path.inc 26 Mar 2007 01:32:22 -0000 1.15 +++ includes/path.inc 5 Apr 2007 02:02:14 -0000 @@ -45,12 +45,12 @@ */ function drupal_lookup_path($action, $path = '', $path_language = '') { global $language; - // $map is an array with language keys, holding arrays of Drupal paths to alias relations + // $map is an array with language keys, holding arrays of Drupal paths to alias relations. static $map = array(), $no_src = array(), $count; $path_language = $path_language ? $path_language : $language->language; - // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases + // Use $count to avoid looking up paths in subsequent calls if there simply are no aliases. if (!isset($count)) { $count = db_result(db_query('SELECT COUNT(pid) FROM {url_alias}')); } @@ -60,19 +60,41 @@ $no_src = array(); } elseif ($count > 0 && $path != '') { + static $optimize_type, $blacklist, $whitelist; + + // Check if we should use the blacklist, the whitelist, or none of both. + if (!isset($optimize_type)) { + $optimize_type = variable_get('path_optimization_type', 2); + if ($optimize_type == 0 && !isset($blacklist)) { + $blacklist = variable_get('path_optimization_blacklist', "admin*\n*/add*\*/edit\n*/outline\n*/revisions\n*/track\nadmin\nadmin/*\nbook\ncomment/*\nfilter\nfilter/*\nlogout\npoll\nprofile\ntracker*\nuser\nuser/register\nuser/login"); + } + elseif ($optimize_type == 1 && !isset($whitelist)) { + $whitelist = variable_get('path_optimization_whitelist', ''); + } + } + if ($action == 'alias') { if (isset($map[$path_language][$path])) { return $map[$path_language][$path]; } - // Get the most fitting result falling back with alias without language - $alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s' AND language IN('%s', '') ORDER BY language DESC", $path, $path_language)); + // Get the most fitting result falling back with alias without language. + $query = "SELECT dst FROM {url_alias} WHERE src = '%s' AND language IN('%s', '') ORDER BY language DESC"; + if ($optimize_type == 0) { + $alias = drupal_path_match($path, $blacklist) ? FALSE : db_result(db_query($query, $path, $path_language)); + } + elseif ($optimize_type == 1) { + $alias = drupal_path_match($path, $whitelist) ? db_result(db_query($query, $path, $path_language)) : FALSE; + } + else { + $alias = db_result(db_query($query, $path, $path_language)); + } $map[$path_language][$path] = $alias; return $alias; } // Check $no_src for this $path in case we've already determined that there - // isn't a path that has this alias + // isn't a path that has this alias. elseif ($action == 'source' && !isset($no_src[$path_language][$path])) { - // Look for the value $path within the cached $map + // Look for the value $path within the cached $map. $src = ''; if (!isset($map[$path_language]) || !($src = array_search($path, $map[$path_language]))) { // Get the most fitting result falling back with alias without language @@ -222,3 +244,23 @@ // we can check it against the 'site_frontpage' variable. return $_GET['q'] == drupal_get_normal_path(variable_get('site_frontpage', 'node')); } + +/** + * Check if a path matches a set of patterns. + * + * @param $path + * The path to match. + * @param $patterns + * String containing a set of patterns separated by \n, \r or \r\n. + * + * @return + * Boolean value: TRUE if the path matches a pattern, FALSE if otherwise. + */ +function drupal_path_match($path, $patterns) { + static $regexps; + + if (!isset($regexps[$patterns])) { + $regexps[$patterns] = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($patterns, '/')) .')$/'; + } + return preg_match($regexps[$patterns], $path); +} Index: modules/block/block.module =================================================================== RCS file: /cvs/drupal/drupal/modules/block/block.module,v retrieving revision 1.254 diff -u -r1.254 block.module --- modules/block/block.module 27 Mar 2007 05:13:53 -0000 1.254 +++ modules/block/block.module 5 Apr 2007 02:02:15 -0000 @@ -691,11 +691,10 @@ if ($block->pages) { if ($block->visibility < 2) { $path = drupal_get_path_alias($_GET['q']); - $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block->pages, '/')) .')$/'; // Compare with the internal and path alias (if any). - $page_match = preg_match($regexp, $path); + $page_match = drupal_path_match($path, $block->pages); if ($path != $_GET['q']) { - $page_match = $page_match || preg_match($regexp, $_GET['q']); + $page_match = $page_match || drupal_path_match($_GET['q'], $block->pages); } // When $block->visibility has a value of 0, the block is displayed on // all pages except those listed in $block->pages. When set to 1, it Index: modules/path/path.module =================================================================== RCS file: /cvs/drupal/drupal/modules/path/path.module,v retrieving revision 1.113 diff -u -r1.113 path.module --- modules/path/path.module 27 Mar 2007 05:13:54 -0000 1.113 +++ modules/path/path.module 5 Apr 2007 02:02:15 -0000 @@ -27,7 +27,7 @@ return $output; case 'admin/build/path': case 'admin/build/path/list': - return '

'. t("Drupal provides users complete control over URLs through aliasing. This feature is typically used to make URLs human-readable or easy to remember. For example, one could map the relative URL 'node/1' onto 'about'. Each system path can have multiple aliases.") .'

'; + return '

'. t("Drupal provides users complete control over URLs through aliasing. This feature is typically used to make URLs human-readable or easy to remember. For example, one could map the relative URL 'node/1' onto 'about'. Each system path can have multiple aliases.") .'

'. t('Optimization via white/blacklisting URL alias lookups is available on the performance settings page.', array('@link' => url('admin/settings/performance'))) .'

'; case 'admin/build/path/add': return '

'. t('Enter the path you wish to create the alias for, followed by the name of the new alias.') .'

'; } Index: modules/system/system.module =================================================================== RCS file: /cvs/drupal/drupal/modules/system/system.module,v retrieving revision 1.460 diff -u -r1.460 system.module --- modules/system/system.module 27 Mar 2007 05:13:54 -0000 1.460 +++ modules/system/system.module 5 Apr 2007 02:02:17 -0000 @@ -707,6 +707,46 @@ $form['#submit']['system_settings_form_submit'] = array(); $form['#submit']['drupal_clear_css_cache'] = array(); + + // If path.module is enabled, allow for URL alias optimization via black/whitelist + if (module_exists('path')) { + $type = variable_get('path_optimization_type', 2); + + $form['optimize'] = array( + '#type' => 'fieldset', + '#title' => t('Path optimization settings'), + '#collapsible' => false, + '#collapsed' => false, + ); + $form['optimize']['path_optimization_type'] = array( + '#type' => 'radios', + '#title' => t('Optimization type'), + '#default_value' => variable_get('path_optimization_type', 2), + '#options' => array(t('Blacklist of Drupal paths'), t('Whitelist of Drupal paths'), t('No optimization')), + '#description' => t("Blacklist: Drupal will only look up aliases for paths that don't match paths in the blacklist.
Whitelist: Drupal paths must match any of the paths listed in the whitelist, otherwise Drupal won't look up the alias.
No optimization: Drupal will look up aliases for any Drupal path."), + '#prefix' => t('It is recommended to enable either the blacklist or the whitelist, and to configure it correctly. This will reduce the numer of database queries, saving system resources! For more information see
the handbook.'), + ); + switch ($type) { + case 0: + $form['optimize']['path_optimization_blacklist'] = array( + '#type' => 'textarea', + '#title' => t('Blacklist'), + '#default_value' => variable_get('path_optimization_blacklist', "admin*\n*/add*\*/edit\n*/outline\n*/revisions\n*/track\nadmin\nadmin/*\nbook\ncomment/*\nfilter\nfilter/*\nlogout\npoll\nprofile\ntracker*\nuser\nuser/register\nuser/login"), + '#rows' => 10, + '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '')), + ); + break; + case 1: + $form['optimize']['path_optimization_whitelist'] = array( + '#type' => 'textarea', + '#title' => t('Whitelist'), + '#default_value' => variable_get('path_optimization_whitelist', ''), + '#rows' => 10, + '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '')), + ); + break; + } + } return system_settings_form($form); } Index: sites/default/settings.php =================================================================== RCS file: /cvs/drupal/drupal/sites/default/settings.php,v retrieving revision 1.54 diff -u -r1.54 settings.php --- sites/default/settings.php 28 Mar 2007 14:08:22 -0000 1.54 +++ sites/default/settings.php 5 Apr 2007 02:02:17 -0000 @@ -90,7 +90,7 @@ * $db_url = 'mysqli://username:password@localhost/databasename'; * $db_url = 'pgsql://username:password@localhost/databasename'; */ -$db_url = 'mysql://username:password@localhost/databasename'; +$db_url = 'mysql://root@localhost/HEAD'; $db_prefix = ''; /**