? .DS_Store ? optimize_drupal_lookup_patch.patch Index: includes/path.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/path.inc,v retrieving revision 1.15 diff -u -p -r1.15 path.inc --- includes/path.inc 26 Mar 2007 01:32:22 -0000 1.15 +++ includes/path.inc 4 Apr 2007 23:31:50 -0000 @@ -45,12 +45,12 @@ function drupal_init_path() { */ 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 @@ function drupal_lookup_path($action, $pa $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_optimize_type', 2); + if ($optimize_type == 0 && !isset($blacklist)) { + $blacklist = variable_get('path_optimize_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_optimize_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 @@ function drupal_is_front_page() { // 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 -p -r1.254 block.module --- modules/block/block.module 27 Mar 2007 05:13:53 -0000 1.254 +++ modules/block/block.module 4 Apr 2007 23:32:29 -0000 @@ -691,11 +691,10 @@ function block_list($region) { 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 -p -r1.113 path.module --- modules/path/path.module 27 Mar 2007 05:13:54 -0000 1.113 +++ modules/path/path.module 4 Apr 2007 23:32:46 -0000 @@ -67,6 +67,14 @@ function path_menu() { 'access arguments' => array('administer url aliases'), 'type' => MENU_LOCAL_TASK, ); + $items['admin/build/path/optimize'] = array( + 'title' => t('Optimize'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('path_admin_optimize'), + 'access arguments' => array('adminster url aliases'), + 'type' => MENU_LOCAL_TASK, + 'weight' => 10, + ); return $items; } @@ -110,6 +118,51 @@ function path_admin_delete_confirm($pid) } /** + * Menu callback; presents the optimize form. + */ +function path_admin_optimize() { + $type = variable_get('path_optimize_type', 2); + + $form = array(); + $form['optimize'] = array( + '#type' => 'fieldset', + '#title' => t('Path optimization settings'), + '#collapsible' => true, + '#collapsed' => false, + ); + $form['optimize']['path_optimize_type'] = array( + '#type' => 'radios', + '#title' => t('Optimization type'), + '#default_value' => variable_get('path_optimize_type', 2), + '#options' => array(t('blacklist of Drupal paths'), t('whitelist of Drupal paths'), t('no optimization')), + '#description' => t("Blacklist: Drupal dwill 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 highly recommended to either enable the blacklist or the whitelist and to configure it correctly. This will reduce the numer of database queries, and thus less waste of resourced!'), + ); + switch ($type) { + case 0: + $form['optimize']['path_optimize_blacklist'] = array( + '#type' => 'textarea', + '#title' => t('Blacklist'), + '#default_value' => variable_get('path_optimize_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."), + ); + break; + case 1: + $form['optimize']['path_optimize_whitelist'] = array( + '#type' => 'textarea', + '#title' => t('Whitelist'), + '#default_value' => variable_get('path_optimize_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."), + ); + break; + } + + return system_settings_form($form); +} + +/** * Execute URL alias deletion **/ function path_admin_delete_confirm_submit($form_id, $form_values) {