Completed
Push — dev2 ( 646d6a...7b2d13 )
by Gordon
02:57
created
src/SilverStripe/Elastica/QueryGenerator.php 1 patch
Spacing   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -126,12 +126,12 @@  discard block
 block discarded – undo
126 126
 		$queryTextExists = ($this->queryText != '');
127 127
 		$isMultiMatch = ($this->fields != null);
128 128
 
129
-		if ($this->selectedFilters == null) {
129
+		if($this->selectedFilters == null) {
130 130
 			$this->selectedFilters = array();
131 131
 		}
132 132
 
133 133
 		$this->manipulatorInstance = null;
134
-		if ($this->manipulator) {
134
+		if($this->manipulator) {
135 135
 			$this->manipulatorInstance = \Injector::inst()->create($this->manipulator);
136 136
 			$this->manipulatorInstance->queryGenerator = $this;
137 137
 			$this->manipulatorInstance->originalQueryString = $this->queryText;
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
 		//This is a query_string object
141 141
 		$textQuery = null;
142 142
 
143
-		if (!$isMultiMatch) {
143
+		if(!$isMultiMatch) {
144 144
 			$textQuery = $this->simpleTextQuery();
145 145
 		} else {
146 146
 			$textQuery = $this->multiMatchQuery();
@@ -159,7 +159,7 @@  discard block
 block discarded – undo
159 159
 		$query->setLimit($this->pageLength);
160 160
 		$query->setFrom($this->start);
161 161
 
162
-		if ($this->manipulatorInstance && !$queryTextExists) {
162
+		if($this->manipulatorInstance && !$queryTextExists) {
163 163
 			$sort = $this->manipulatorInstance->getDefaultSort();
164 164
 			$query->setSort($sort);
165 165
 		}
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
 			'size' => 10,
191 191
 			'query' => array(
192 192
 				'match' => array(
193
-					$field.'.autocomplete' => array(
193
+					$field . '.autocomplete' => array(
194 194
 						'query' => $this->queryText,
195 195
 						'operator' => 'and'
196 196
 					)
@@ -199,7 +199,7 @@  discard block
 block discarded – undo
199 199
 		);
200 200
 
201 201
 		// The query clause can only have one entry, so a bit of mangling
202
-		if (!empty($this->selectedFilters)) {
202
+		if(!empty($this->selectedFilters)) {
203 203
 			$filtered = array();
204 204
 			$filtered['query'] = $data['query'];
205 205
 			unset($data['query']);
@@ -228,17 +228,17 @@  discard block
 block discarded – undo
228 228
 	 * @param Elastica\Query\QueryString $textQuery A query_string representing the current query
229 229
 	 */
230 230
 	private function addFilters($textQuery) {
231
-		if ($this->manipulator) {
231
+		if($this->manipulator) {
232 232
 			$this->manipulatorInstance->updateFilters($this->selectedFilters);
233 233
 		}
234 234
 
235 235
 		$elFilters = array();
236 236
 		$rangeFilterKeys = RangedAggregation::getTitles();
237 237
 
238
-		foreach ($this->selectedFilters as $key => $value) {
239
-			if (!in_array($key, $rangeFilterKeys)) {
238
+		foreach($this->selectedFilters as $key => $value) {
239
+			if(!in_array($key, $rangeFilterKeys)) {
240 240
 				$filter = new Term();
241
-				$filter->setTerm($key,$value);
241
+				$filter->setTerm($key, $value);
242 242
 				$elFilters[] = $filter;
243 243
 			} else {
244 244
 				// get the selected range filter
@@ -251,7 +251,7 @@  discard block
 block discarded – undo
251 251
 
252 252
 		// if not facets selected, pass through null
253 253
 		$queryFilter = null;
254
-		switch (count($this->selectedFilters)) {
254
+		switch(count($this->selectedFilters)) {
255 255
 			case 0:
256 256
 				// filter already null
257 257
 				break;
@@ -261,18 +261,18 @@  discard block
 block discarded – undo
261 261
 			default:
262 262
 				$queryFilter = new BoolAnd();
263 263
 
264
-				foreach ($elFilters as $filter) {
264
+				foreach($elFilters as $filter) {
265 265
 					$queryFilter->addFilter($filter);
266 266
 				}
267 267
 				break;
268 268
 		}
269 269
 
270 270
 		// the Elastica query object
271
-		if ($queryFilter == null) {
271
+		if($queryFilter == null) {
272 272
 			$query = new Query($textQuery);
273 273
 		} else {
274 274
 			//MatchAll appears not be allowed inside a filtered query which is a bit of a pain.
275
-			if ($textQuery instanceof MatchAll) {
275
+			if($textQuery instanceof MatchAll) {
276 276
 				$textQuery = null;
277 277
 			}
278 278
 
@@ -289,7 +289,7 @@  discard block
 block discarded – undo
289 289
 
290 290
 	private function addAggregation(&$query) {
291 291
 		// aggregation (optional)
292
-		if ($this->manipulatorInstance) {
292
+		if($this->manipulatorInstance) {
293 293
 			$this->manipulatorInstance->augmentQuery($query);
294 294
 		}
295 295
 	}
@@ -316,7 +316,7 @@  discard block
 block discarded – undo
316 316
 		//Setting the lenient flag means that numeric fields can be searched for text values
317 317
 		$textQuery->setParam('lenient', true);
318 318
 
319
-		if ($this->showResultsForEmptyQuery && $this->queryText == '') {
319
+		if($this->showResultsForEmptyQuery && $this->queryText == '') {
320 320
 			$textQuery = new MatchAll();
321 321
 		}
322 322
 
@@ -329,8 +329,8 @@  discard block
 block discarded – undo
329 329
 		$textQuery = new MultiMatch();
330 330
 
331 331
 		// Differing cases for showing and not showing empty string
332
-		if ($this->queryText == '') {
333
-			if (!$this->showResultsForEmptyQuery) {
332
+		if($this->queryText == '') {
333
+			if(!$this->showResultsForEmptyQuery) {
334 334
 				$textQuery->setQuery('');
335 335
 			} else {
336 336
 				//WIP
@@ -343,7 +343,7 @@  discard block
 block discarded – undo
343 343
 			$textQuery->setQuery($this->queryText);
344 344
 		}
345 345
 
346
-		if ($textQuery instanceof MultiMatch) {
346
+		if($textQuery instanceof MultiMatch) {
347 347
 			$elasticaFields = $this->convertWeightedFieldsForElastica($this->fields);
348 348
 			$textQuery->setFields($elasticaFields);
349 349
 			$textQuery->setType('most_fields');
@@ -366,20 +366,20 @@  discard block
 block discarded – undo
366 366
 	 */
367 367
 	public function convertWeightedFieldsForElastica($fields) {
368 368
 		$result = array();
369
-		$nameToType = self::getSearchFieldsMappingForClasses($this->classes,$fields);
369
+		$nameToType = self::getSearchFieldsMappingForClasses($this->classes, $fields);
370 370
 
371
-		if (sizeof($fields) != 0) {
372
-			foreach ($fields as $fieldName => $weight) {
371
+		if(sizeof($fields) != 0) {
372
+			foreach($fields as $fieldName => $weight) {
373 373
 				$fieldCfg = "$fieldName";
374
-				if ($weight != 1) {
375
-					$fieldCfg .= '^'.$weight;
374
+				if($weight != 1) {
375
+					$fieldCfg .= '^' . $weight;
376 376
 				}
377 377
 				array_push($result, $fieldCfg);
378
-				if (isset($nameToType[$fieldName])) {
379
-					if ($nameToType[$fieldName] == 'string') {
378
+				if(isset($nameToType[$fieldName])) {
379
+					if($nameToType[$fieldName] == 'string') {
380 380
 						$fieldCfg = "{$fieldName}.*";
381
-						if ($weight != 1) {
382
-							$fieldCfg .= '^'.$weight;
381
+						if($weight != 1) {
382
+							$fieldCfg .= '^' . $weight;
383 383
 						}
384 384
 						array_push($result, $fieldCfg);
385 385
 					}
@@ -405,29 +405,29 @@  discard block
 block discarded – undo
405 405
 		// Get a array of relevant classes to search
406 406
 		$cache = QueryGenerator::getCache();
407 407
 		$csvClasses = $classes;
408
-		if (is_array($classes)) {
409
-			$csvClasses = implode(',',$classes);
408
+		if(is_array($classes)) {
409
+			$csvClasses = implode(',', $classes);
410 410
 		}
411 411
 
412 412
 		error_log("CSV CLASSES: $csvClasses");
413 413
 
414
-		$key ='SEARCHABLE_FIELDS_'.str_replace(',', '_', $csvClasses);
414
+		$key = 'SEARCHABLE_FIELDS_' . str_replace(',', '_', $csvClasses);
415 415
 
416
-		if ($fieldsAllowed) {
416
+		if($fieldsAllowed) {
417 417
 			$fieldsAllowedCSV = self::convertToQuotedCSV(array_keys($fieldsAllowed));
418
-			$key .= '_' . str_replace(',', '_', str_replace("'", '_',$fieldsAllowedCSV));
418
+			$key .= '_' . str_replace(',', '_', str_replace("'", '_', $fieldsAllowedCSV));
419 419
 			$key = str_replace('.', '_', $key);
420 420
 			$key = str_replace(' ', '_', $key);
421 421
 		}
422 422
 
423 423
 		$result = $cache->load($key);
424 424
 		error_log("RESULT:$result for key $key \n");
425
-		if (!$result) {
425
+		if(!$result) {
426 426
 			$relevantClasses = array();
427
-			if (empty($csvClasses)) {
427
+			if(empty($csvClasses)) {
428 428
 				$sql = "SELECT DISTINCT Name from SearchableClass where InSiteTree = 1 order by Name";
429 429
 				$records = \DB::query($sql);
430
-				foreach ($records as $record) {
430
+				foreach($records as $record) {
431 431
 					array_push($relevantClasses, $record['Name']);
432 432
 				}
433 433
 			} else {
@@ -435,21 +435,21 @@  discard block
 block discarded – undo
435 435
 			}
436 436
 
437 437
 			$result = array();
438
-			if (sizeof($relevantClasses) > 0) {
438
+			if(sizeof($relevantClasses) > 0) {
439 439
 				$relevantClassesCSV = self::convertToQuotedCSV($relevantClasses);
440 440
 
441 441
 				//Perform a database query to get get a list of searchable fieldnames to Elasticsearch mapping
442 442
 				$sql = "SELECT  sf.Name,sf.Type FROM SearchableClass sc  INNER JOIN SearchableField sf ON "
443 443
 					 . "sc.id = sf.SearchableClassID WHERE sc.name IN ($relevantClassesCSV)";
444
-				if ($fieldsAllowed) {
444
+				if($fieldsAllowed) {
445 445
 					$fieldsAllowedCSV = self::convertToQuotedCSV(array_keys($fieldsAllowed));
446
-					if (strlen($fieldsAllowedCSV) > 0) {
446
+					if(strlen($fieldsAllowedCSV) > 0) {
447 447
 						$sql .= " AND sf.Name IN ($fieldsAllowedCSV)";
448 448
 					}
449 449
 				}
450 450
 
451 451
 				$records = \DB::query($sql);
452
-				foreach ($records as $record) {
452
+				foreach($records as $record) {
453 453
 					$name = $record['Name'];
454 454
 					$type = $record['Type'];
455 455
 
@@ -463,11 +463,11 @@  discard block
 block discarded – undo
463 463
 					$result[$name] = $type;
464 464
 				}
465 465
 			}
466
-			$cache->save(json_encode($result),$key);
467
-		}  else {
466
+			$cache->save(json_encode($result), $key);
467
+		} else {
468 468
 			// true is necessary here to decode the array hash back to an array and not a struct
469 469
 			self::$cacheHitCtr++;
470
-			$result = json_decode($result,true);
470
+			$result = json_decode($result, true);
471 471
 		}
472 472
 
473 473
 		return $result;
@@ -488,21 +488,21 @@  discard block
 block discarded – undo
488 488
 	 */
489 489
 	public static function convertToQuotedCSV($csvOrArray) {
490 490
 		$asArray = $csvOrArray;
491
-		if (!is_array($csvOrArray)) {
492
-			if ($csvOrArray == null) {
491
+		if(!is_array($csvOrArray)) {
492
+			if($csvOrArray == null) {
493 493
 				$asArray = array();
494 494
 			} else {
495 495
 				$asArray = explode(',', $csvOrArray);
496 496
 			}
497 497
 		}
498 498
 		$quoted = array();
499
-		foreach ($asArray as $value) {
500
-			if (strlen($value) > 0) {
501
-				$item = "'".$value."'";
499
+		foreach($asArray as $value) {
500
+			if(strlen($value) > 0) {
501
+				$item = "'" . $value . "'";
502 502
 				array_push($quoted, $item);
503 503
 			}
504 504
 
505 505
 		}
506
-		return implode(',', $quoted);;
506
+		return implode(',', $quoted); ;
507 507
 	}
508 508
 }
Please login to merge, or discard this patch.