Completed
Push — master ( bd04ed...1014d6 )
by Maxence
01:52
created

SearchService::improveSearchRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
/**
3
 * Files_FullTextSearch - Index the content of your files
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Files_FullTextSearch\Service;
28
29
30
use Exception;
31
use OC\Share\Constants;
32
use OC\Share\Share;
33
use OCA\Files_FullTextSearch\Exceptions\FileIsNotIndexableException;
34
use OCA\Files_FullTextSearch\Model\FilesDocument;
35
use OCA\Files_FullTextSearch\Provider\FilesProvider;
36
use OCA\FullTextSearch\Exceptions\InterruptException;
37
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException;
38
use OCA\FullTextSearch\Model\DocumentAccess;
39
use OCA\FullTextSearch\Model\Index;
40
use OCA\FullTextSearch\Model\IndexDocument;
41
use OCA\FullTextSearch\Model\Runner;
42
use OCA\FullTextSearch\Model\SearchRequest;
43
use OCP\Files\File;
44
use OCP\Files\FileInfo;
45
use OCP\Files\Folder;
46
use OCP\Files\InvalidPathException;
47
use OCP\Files\IRootFolder;
48
use OCP\Files\Node;
49
use OCP\Files\NotFoundException;
50
use OCP\Files\NotPermittedException;
51
use OCP\IUserManager;
52
use OCP\Share\IManager;
53
54
class SearchService {
55
56
57
	/** @var ConfigService */
58
	private $configService;
59
60
	/** @var MiscService */
61
	private $miscService;
62
63
64
	/**
65
	 * SearchService constructor.
66
	 *
67
	 * @param ConfigService $configService
68
	 * @param MiscService $miscService
69
	 *
70
	 * @internal param IProviderFactory $factory
71
	 */
72
	public function __construct(ConfigService $configService, MiscService $miscService) {
73
		$this->configService = $configService;
74
		$this->miscService = $miscService;
75
	}
76
77
78
	/**
79
	 * @param SearchRequest $request
80
	 */
81
	public function improveSearchRequest(SearchRequest $request) {
82
		$this->searchQueryShareNames($request);
83
		$this->searchQueryFiltersExtension($request);
84
		$this->searchQueryFiltersSource($request);
85
		$this->searchQueryWithinDir($request);
86
	}
87
88
89
	private function searchQueryWithinDir(SearchRequest $request) {
90
91
		$currentDir = $request->getOption('files_within_dir');
92
		if ($currentDir === '') {
93
			return;
94
		}
95
96
		$currentDir = MiscService::noBeginSlash(MiscService::endSlash($currentDir));
97
		$request->addWildcardFilters(
98
			[
99
				['share_names.' . $request->getAuthor() => $currentDir . '*'],
100
				['title' => $currentDir . '*']
101
			]
102
		);
103
	}
104
105
106
	/**
107
	 * @param SearchRequest $request
108
	 */
109
	private function searchQueryShareNames(SearchRequest $request) {
110
		$query = [];
111
		$words = explode(' ', $request->getSearch());
112
		foreach ($words as $word) {
113
			array_push(
114
				$query, ['share_names.' . $request->getAuthor() => '*' . $word . '*']
115
			);
116
		}
117
		$request->addWildcardQueries($query);
118
	}
119
120
121
	/**
122
	 * @param SearchRequest $request
123
	 */
124
	private function searchQueryFiltersExtension(SearchRequest $request) {
125
		$extension = $request->getOption('files_extension');
126
		if ($extension === '') {
127
			return;
128
		}
129
130
		$request->addWildcardFilters(
131
			[
132
				['share_names.' . $request->getAuthor() => '*\.' . $extension],
133
				['title' => '*\.' . $extension]
134
			]
135
		);
136
	}
137
138
139
	/**
140
	 * @param SearchRequest $request
141
	 */
142
	private function searchQueryFiltersSource(SearchRequest $request) {
143
144
		$local = $request->getOption('files_local');
145
		$external = $request->getOption('files_external');
146
		$groupFolders = $request->getOption('group_folders');
147
		$federated = $request->getOption('files_federated');
0 ignored issues
show
Unused Code introduced by
$federated is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
148
149
		if (count(array_unique([$local, $external, $groupFolders])) === 1) {
150
			return;
151
		}
152
153
		$this->addTagToSearchRequest($request, 'local', $local);
154
		$this->addTagToSearchRequest($request, 'files_external', $external);
155
		$this->addTagToSearchRequest($request, 'group_folders', $groupFolders);
156
	}
157
158
159
	/**
160
	 * @param SearchRequest $request
161
	 * @param string $tag
162
	 * @param mixed $cond
163
	 */
164
	private function addTagToSearchRequest(SearchRequest $request, $tag, $cond) {
165
		if ($cond === 1 || $cond === '1') {
166
			$request->addTag($tag);
167
		}
168
	}
169
170
171
}