| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * FullTextSearch - Full text search framework for Nextcloud | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * later. See the COPYING file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author Maxence Lange <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @copyright 2018 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @license GNU AGPL version 3 or any later version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * it under the terms of the GNU Affero General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * published by the Free Software Foundation, either version 3 of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * License, or (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | namespace OCA\FullTextSearch\Search; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use daita\MySmallPhpTools\Traits\TArrayTools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | use OCA\FullTextSearch\Model\SearchRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | use OCA\FullTextSearch\Service\ConfigService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | use OCA\FullTextSearch\Service\MiscService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | use OCA\FullTextSearch\Service\SearchService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | use OCP\FullTextSearch\Model\ISearchRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | use OCP\FullTextSearch\Model\ISearchResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | use OCP\IL10N; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | use OCP\IURLGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | use OCP\IUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | use OCP\Search\IProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | use OCP\Search\ISearchQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | use OCP\Search\SearchResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  * Class UnifiedSearchProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  * @package OCA\FullTextSearch\Search | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | class UnifiedSearchProvider implements IProvider { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	const PROVIDER_ID = 'fulltextsearch'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	const ORDER = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	use TArrayTools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	/** @var IL10N */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	private $l10n; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	/** @var IURLGenerator */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	private $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	/** @var SearchService */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	private $searchService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	/** @var ConfigService */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	private $configService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	/** @var MiscService */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	private $miscService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * UnifiedSearchProvider constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 * @param IL10N $l10n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * @param IURLGenerator $urlGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * @param SearchService $searchService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * @param ConfigService $configService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * @param MiscService $miscService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 89 |  | View Code Duplication | 	public function __construct( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		IL10N $l10n, IURLGenerator $urlGenerator, SearchService $searchService, ConfigService $configService, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		MiscService $miscService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		$this->l10n = $l10n; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		$this->urlGenerator = $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$this->searchService = $searchService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		$this->configService = $configService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		$this->miscService = $miscService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 * return unique id of the provider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	public function getId(): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		return self::PROVIDER_ID; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	public function getName(): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		return $this->l10n->t('Full Text Search'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * @param string $route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * @param array $routeParameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	 * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	public function getOrder(string $route, array $routeParameters): int { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 		return self::ORDER; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 * @param IUser $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 * @param ISearchQuery $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 * @return SearchResult | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  | 	public function search(IUser $user, ISearchQuery $query): SearchResult { | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  | 		$result = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  | 		$searchRequest = $this->generateSearchRequest($query); | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  | 		try { | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  | 			$ftsResult = $this->searchService->search($user->getUID(), $searchRequest); | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  | 			$result = $this->convertSearchResult($ftsResult); | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  | 		} catch (Exception $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  | 		return SearchResult::paginated( | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  | 			$this->l10n->t('Full Text Search'), $result, ($query->getCursor() ?? 0) + $query->getLimit() | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 * @param $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	 * @return ISearchRequest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	private function generateSearchRequest(ISearchQuery $query): ISearchRequest { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 		$searchRequest = new SearchRequest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		list($app, $controller, $method) = explode('.', $query->getRoute()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		$searchRequest->setProviders([$app]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		$searchRequest->setSearch($query->getTerm()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		$searchRequest->setPage((int)floor(($query->getCursor() ?? 0) / $query->getLimit()) + 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 		$searchRequest->setParts([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		$searchRequest->setSize($query->getLimit()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		$searchRequest->setOptions([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		$searchRequest->setTags([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 		$searchRequest->setSubTags([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		$searchRequest->setSize($query->getLimit()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 		return $searchRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	 * @param ISearchResult[] $searchResult | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	 * @return UnifiedSearchResult[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	private function convertSearchResult(array $searchResult): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		$result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 		foreach ($searchResult as $ftsSearch) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 			foreach ($ftsSearch->getDocuments() as $document) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 				$excerpts = $document->getExcerpts(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 				if (empty($excerpts)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 					$title = $document->getTitle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 					$subline = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 				} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 					$title = (sizeof($excerpts) > 0) ? $excerpts[0]['excerpt'] : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 					$subline = $document->getTitle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 				} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  | 				$unified = $document->getInfoArray('unified'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  | 				$result[] = new UnifiedSearchResult( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  | 					$this->get('thumbUrl', $unified, ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  | 					$this->get('title', $unified, $title), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  | 					$this->get('subline', $unified, $subline), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  | 					$this->get('link', $unified, $document->getLink()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  | 					$this->get('icon', $unified, '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  | 				); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 208 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.