commit dd5a60e90484969f40e84687b93d5798cbbbb657 Author: Lorenz Schori Date: Sun Nov 4 22:31:41 2012 +0100 Convert node search ranking to cmi diff --git a/core/modules/comment/comment.install b/core/modules/comment/comment.install index f47151f..08cd76e 100644 --- a/core/modules/comment/comment.install +++ b/core/modules/comment/comment.install @@ -382,6 +382,15 @@ function comment_update_8003(&$sandbox) { } /** + * Moves variable node_rank_comments to config node.settings.search_rank.comments. + */ +function comment_update_8004() { + update_variables_to_config('node.settings', array( + 'node_rank_views' => 'search_rank.views', + )); +} + +/** * @} End of "addtogroup updates-7.x-to-8.x". * The next series of updates should start at 9000. */ diff --git a/core/modules/node/config/node.settings.yml b/core/modules/node/config/node.settings.yml index 9562864..77fea07 100644 --- a/core/modules/node/config/node.settings.yml +++ b/core/modules/node/config/node.settings.yml @@ -1 +1,6 @@ items_per_page: '10' +search_rank: + promote: '0' + recent: '0' + relevance: '0' + sticky: '0' diff --git a/core/modules/node/node.install b/core/modules/node/node.install index 9e1ee1b..d8ca244 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -478,12 +478,6 @@ function node_uninstall() { config('language.settings')->clear('node. ' . $type . '.language.default_configuration')->save(); } - // Delete node search ranking variables. - variable_del('node_rank_relevance'); - variable_del('node_rank_sticky'); - variable_del('node_rank_promote'); - variable_del('node_rank_recent'); - // Delete remaining general module variables. state()->delete('node.node_access_needs_rebuild'); variable_del('node_admin_theme'); @@ -729,6 +723,30 @@ function node_update_8010() { } /** + * Moves variables node_rank_relevance, node_rank_sticky, node_rank_promote and + * node_rank_recent to config node.settings.search_rank. + */ +function node_update_8011() { + $oldprefix = 'node_rank_'; + $newprefix = 'search_rank.'; + $oldpfxlen = strlen($oldprefix); + + $rank_variables = db_select('variable', 'v') + ->fields('v', array('name')) + ->condition('v.name', db_like($oldprefix) . '%', 'LIKE') + ->execute() + ->fetchCol(); + + $config = array(); + foreach ($rank_variables as $rank_variable) { + $config[$rank_variable] = substr_replace($rank_variable, $newprefix, 0, $oldpfxlen); + } + + update_variables_to_config('node.settings', $config); +} + + +/** * @} End of "addtogroup updates-7.x-to-8.x" * The next series of updates should start at 9000. */ diff --git a/core/modules/node/node.module b/core/modules/node/node.module index fc53461..10a531a 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -1279,8 +1279,10 @@ function node_permission() { function _node_rankings(SelectExtender $query) { if ($ranking = module_invoke_all('ranking')) { $tables = &$query->getTables(); + $ranks = config('node.settings')->get('search_rank'); foreach ($ranking as $rank => $values) { - if ($node_rank = variable_get('node_rank_' . $rank, 0)) { + if (!empty($ranks[$rank])) { + $node_rank = $ranks[$rank]; // If the table defined in the ranking isn't already joined, then add it. if (isset($values['join']) && !isset($tables[$values['join']['alias']])) { $query->addJoin($values['join']['type'], $values['join']['table'], $values['join']['alias'], $values['join']['on']); @@ -1330,6 +1332,7 @@ function node_search_status() { /** * Implements hook_search_admin(). + * @see node_search_admin_submit() */ function node_search_admin() { // Output form for defining rank factor weights. @@ -1338,24 +1341,38 @@ function node_search_admin() { '#title' => t('Content ranking'), ); $form['content_ranking']['#theme'] = 'node_search_admin'; + $form['content_ranking']['#tree'] = TRUE; $form['content_ranking']['info'] = array( '#value' => '' . t('The following numbers control which properties the content search should favor when ordering the results. Higher numbers mean more influence, zero means the property is ignored. Changing these numbers does not require the search index to be rebuilt. Changes take effect immediately.') . '' ); // Note: reversed to reflect that higher number = higher ranking. $options = drupal_map_assoc(range(0, 10)); + $ranks = config('node.settings')->get('search_rank'); foreach (module_invoke_all('ranking') as $var => $values) { - $form['content_ranking']['factors']['node_rank_' . $var] = array( + $form['content_ranking']['factors'][$var] = array( '#title' => $values['title'], '#type' => 'select', '#options' => $options, - '#default_value' => variable_get('node_rank_' . $var, 0), + '#default_value' => isset($ranks[$var]) ? $ranks[$var] : 0, ); } + + $form['#submit'][] = 'node_search_admin_submit'; + return $form; } /** + * Submit callback for search admin form callback. + */ +function node_search_admin_submit($form, &$form_state) { + config('node.settings') + ->set('search_rank', $form_state['values']['content_ranking']['factors']) + ->save(); +} + +/** * Implements hook_search_execute(). */ function node_search_execute($keys = NULL, $conditions = NULL) { diff --git a/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php b/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php index a57e483..9eabd95 100644 --- a/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php +++ b/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php @@ -92,9 +92,11 @@ function testRankings() { // Test each of the possible rankings. foreach ($node_ranks as $node_rank) { // Disable all relevancy rankings except the one we are testing. + $ranks = array(); foreach ($node_ranks as $var) { - variable_set('node_rank_' . $var, $var == $node_rank ? 10 : 0); + $ranks[$var] = $var == $node_rank ? 10 : 0; } + config('node.settings')->set('search_rank', $ranks)->save(); // Do the search and assert the results. $set = node_search_execute('rocks'); @@ -149,10 +151,8 @@ function testHTMLRankings() { $this->refreshVariables(); // Disable all other rankings. - $node_ranks = array('sticky', 'promote', 'recent', 'comments', 'views'); - foreach ($node_ranks as $node_rank) { - variable_set('node_rank_' . $node_rank, 0); - } + $ranks = array('sticky' => 0, 'promote' => 0, 'recent' => 0, 'comments' => 0, 'views' => 0); + config('node.settings')->set('search_rank', $ranks)->save(); $set = node_search_execute('rocks'); // Test the ranking of each tag. @@ -219,11 +219,8 @@ function testDoubleRankings() { // Set up for ranking sticky and lots of comments; make sure others are // disabled. - $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments', 'views'); - foreach ($node_ranks as $var) { - $value = ($var == 'sticky' || $var == 'comments') ? 10 : 0; - variable_set('node_rank_' . $var, $value); - } + $ranks = array('sticky' => 10, 'promote' => 0, 'relevance' => 0, 'recent' => 0, 'comments' => 10, 'views' => 0); + config('node.settings')->set('search_rank', $ranks)->save(); // Do the search and assert the results. $set = node_search_execute('rocks'); diff --git a/core/modules/search/search.api.php b/core/modules/search/search.api.php index 0dd0a84..f120c8f 100644 --- a/core/modules/search/search.api.php +++ b/core/modules/search/search.api.php @@ -137,20 +137,25 @@ function hook_search_admin() { '#title' => t('Content ranking'), ); $form['content_ranking']['#theme'] = 'node_search_admin'; + $form['content_ranking']['#tree'] = TRUE; $form['content_ranking']['info'] = array( '#value' => '' . t('The following numbers control which properties the content search should favor when ordering the results. Higher numbers mean more influence, zero means the property is ignored. Changing these numbers does not require the search index to be rebuilt. Changes take effect immediately.') . '' ); // Note: reversed to reflect that higher number = higher ranking. $options = drupal_map_assoc(range(0, 10)); + $ranks = config('node.settings')->get('search_rank'); foreach (module_invoke_all('ranking') as $var => $values) { - $form['content_ranking']['factors']['node_rank_' . $var] = array( + $form['content_ranking']['factors'][$var] = array( '#title' => $values['title'], '#type' => 'select', '#options' => $options, - '#default_value' => variable_get('node_rank_' . $var, 0), + '#default_value' => isset($ranks[$var]) ? $ranks[$var] : 0, ); } + + $form['#submit'][] = 'node_search_admin_submit'; + return $form; } diff --git a/core/modules/statistics/statistics.install b/core/modules/statistics/statistics.install index c0d0a6f..4fd3f1c 100644 --- a/core/modules/statistics/statistics.install +++ b/core/modules/statistics/statistics.install @@ -154,3 +154,12 @@ function statistics_update_8001() { array('primary key' => array('nid')) ); } + +/** + * Moves variable node_rank_views to config node.settings.search_rank.views. + */ +function statistics_update_8002() { + update_variables_to_config('node.settings', array( + 'node_rank_views' => 'search_rank.views', + )); +} diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php index e3cb1d0..21f4083 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/SystemUpgradePathTest.php @@ -79,6 +79,11 @@ public function testVariableUpgrade() { 'cancel_method' => 'user_cancel_reassign', ); + $expected_config['node.settings'] = array( + 'search_rank.promote' => 10, + 'search_rank.custom_from_contrib' => 5, + ); + foreach ($expected_config as $file => $values) { $config = config($file); $this->verbose(print_r($config->get(), TRUE)); diff --git a/core/modules/system/tests/upgrade/drupal-7.system.database.php b/core/modules/system/tests/upgrade/drupal-7.system.database.php index e9f31d3..c67a47e 100644 --- a/core/modules/system/tests/upgrade/drupal-7.system.database.php +++ b/core/modules/system/tests/upgrade/drupal-7.system.database.php @@ -94,6 +94,14 @@ ->values(array( 'name' => 'user_cancel_method', 'value' => 's:20:"user_cancel_reassign"', + )) +->values(array( + 'name' => 'node_rank_promote', + 'value' => 'i:10;', + )) +->values(array( + 'name' => 'node_rank_custom_from_contrib', + 'value' => 'i:5;', )) ->execute();