diff --git a/core/modules/search/lib/Drupal/search/SearchQuery.php b/core/modules/search/lib/Drupal/search/SearchQuery.php index b49c06a..8e5f803 100644 --- a/core/modules/search/lib/Drupal/search/SearchQuery.php +++ b/core/modules/search/lib/Drupal/search/SearchQuery.php @@ -413,6 +413,7 @@ public function addScore($score, $arguments = array(), $multiply = FALSE) { */ public function execute() { + if (!$this->executedFirstPass) { $this->executeFirstPass(); } @@ -440,8 +441,13 @@ public function execute() } } + // Format the float to make sure that the decimal separator is a period + // no matter what the numeric locale is set to. This is so the database + // query will not fail because a float with a comma is handed to MySQL as a + // number to do calculations on. + $relevance = number_format((1.0 / $this->normalize), 10, '.', ''); // Replace i.relevance pseudo-field with the actual, normalized value. - $this->scores = str_replace('i.relevance', '(' . (1.0 / $this->normalize) . ' * i.score * t.count)', $this->scores); + $this->scores = str_replace('i.relevance', '(' . $relevance . ' * i.score * t.count)', $this->scores); // Convert scores to an expression. $this->addExpression('SUM(' . implode(' + ', $this->scores) . ')', 'calculated_score', $this->scoresArguments); @@ -455,7 +461,6 @@ public function execute() ->addTag('search_' . $this->type) ->addMetaData('normalize', $this->normalize) ->fields('i', array('type', 'sid')); - return $this->query->execute(); } diff --git a/core/modules/search/lib/Drupal/search/Tests/SearchSetLocaleTest.php b/core/modules/search/lib/Drupal/search/Tests/SearchSetLocaleTest.php new file mode 100644 index 0000000..6e58b8f --- /dev/null +++ b/core/modules/search/lib/Drupal/search/Tests/SearchSetLocaleTest.php @@ -0,0 +1,61 @@ + 'Search with numeric locale set', + 'description' => 'Check that search works with numeric locale settings', + 'group' => 'Search', + ); + } + + function setUp() { + parent::setUp(); + + // Create a plugin instance. + $this->nodeSearchPlugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); + // Create a node with a very simple body. + $this->drupalCreateNode(array('body' => array(array('value' => 'tapir')))); + // Update the search index. + $this->nodeSearchPlugin->updateIndex(); + search_update_totals(); + } + + /** + * Verify that search works with a numeric locale set. + */ + public function testSearchWithNumericLocale() { + // French decimal point is comma. + setlocale(LC_NUMERIC, 'fr_FR'); + $this->nodeSearchPlugin->setSearch('tapir', array(), array()); + // The call to execute will throw an exception if a float in the wrong + // format is passed in the query to the database, so an assertion is not + // necessary here. + $this->nodeSearchPlugin->execute(); + } +}