From aaf712fbbbea9bb5fd64cd971757efb749ae306c Mon Sep 17 00:00:00 2001 From: DariusIII Date: Wed, 30 Apr 2025 17:11:24 +0200 Subject: [PATCH] CS fixes --- .../Commands/CreateManticoreIndexes.php | 279 +++++++++--------- .../Commands/NntmuxPopulateSearchIndexes.php | 6 +- 2 files changed, 141 insertions(+), 144 deletions(-) diff --git a/app/Console/Commands/CreateManticoreIndexes.php b/app/Console/Commands/CreateManticoreIndexes.php index d024a508b..0b4b3e88c 100644 --- a/app/Console/Commands/CreateManticoreIndexes.php +++ b/app/Console/Commands/CreateManticoreIndexes.php @@ -1,168 +1,165 @@ info('Creating Manticore Search indexes...'); + /** + * Execute the console command. + */ + public function handle(): int + { + $this->info('Creating Manticore Search indexes...'); - $dropExisting = $this->option('drop'); + $dropExisting = $this->option('drop'); - // Get connection details from config - $host = config('sphinxsearch.host', '127.0.0.1'); - $port = config('sphinxsearch.port', 9308); + // Get connection details from config + $host = config('sphinxsearch.host', '127.0.0.1'); + $port = config('sphinxsearch.port', 9308); - // Create client - $this->client = new Client([ - 'host' => $host, - 'port' => $port - ]); + // Create client + $this->client = new Client([ + 'host' => $host, + 'port' => $port, + ]); - // We'll skip checking for data_dir this way since it may not be accessible via API - // but instead provide better error handling during index creation + // We'll skip checking for data_dir this way since it may not be accessible via API + // but instead provide better error handling during index creation - // If you encounter data_dir errors, ensure it's properly set in manticore.conf: - // data_dir = /path/to/data - // And make sure the path exists and has proper permissions + // If you encounter data_dir errors, ensure it's properly set in manticore.conf: + // data_dir = /path/to/data + // And make sure the path exists and has proper permissions - try { - $this->client->nodes()->status(); - } catch (\Exception $e) { - $this->error('Failed to connect to Manticore Search: ' . $e->getMessage()); - $this->info('Please check if Manticore Search is running and properly configured.'); - return 1; - } + try { + $this->client->nodes()->status(); + } catch (\Exception $e) { + $this->error('Failed to connect to Manticore Search: '.$e->getMessage()); + $this->info('Please check if Manticore Search is running and properly configured.'); - // Define indexes and their schema - $indexes = [ - 'releases_rt' => [ - 'settings' => [ - 'min_prefix_len' => 0, - 'min_infix_len' => 2, - ], - 'columns' => [ - 'name' => ['type' => 'text'], - 'searchname' => ['type' => 'text'], - 'fromname' => ['type' => 'text'], - 'filename' => ['type' => 'text'], - 'categories_id' => ['type' => 'text'], - 'dummy' => ['type' => 'integer', 'attribute' => true] - ] - ], - 'predb_rt' => [ - 'settings' => [ - 'min_prefix_len' => 0, - 'min_infix_len' => 2, - ], - 'columns' => [ - 'title' => ['type' => 'text', 'attribute' => true], - 'filename' => ['type' => 'text', 'attribute' => true], - 'dummy' => ['type' => 'integer', 'attribute' => true], - 'source' => ['type' => 'string', 'attribute' => true] - ] - ] - ]; + return 1; + } - $hasErrors = false; + // Define indexes and their schema + $indexes = [ + 'releases_rt' => [ + 'settings' => [ + 'min_prefix_len' => 0, + 'min_infix_len' => 2, + ], + 'columns' => [ + 'name' => ['type' => 'text'], + 'searchname' => ['type' => 'text'], + 'fromname' => ['type' => 'text'], + 'filename' => ['type' => 'text'], + 'categories_id' => ['type' => 'text'], + 'dummy' => ['type' => 'integer', 'attribute' => true], + ], + ], + 'predb_rt' => [ + 'settings' => [ + 'min_prefix_len' => 0, + 'min_infix_len' => 2, + ], + 'columns' => [ + 'title' => ['type' => 'text', 'attribute' => true], + 'filename' => ['type' => 'text', 'attribute' => true], + 'dummy' => ['type' => 'integer', 'attribute' => true], + 'source' => ['type' => 'string', 'attribute' => true], + ], + ], + ]; - // Create each index - foreach ($indexes as $indexName => $schema) { - if (!$this->createIndex($indexName, $schema, $dropExisting)) { - $hasErrors = true; - } - } + $hasErrors = false; - if ($hasErrors) { - $this->error('Some errors occurred during index creation.'); - return 1; - } + // Create each index + foreach ($indexes as $indexName => $schema) { + if (! $this->createIndex($indexName, $schema, $dropExisting)) { + $hasErrors = true; + } + } - $this->info('All Manticore Search indexes created successfully!'); - return 0; - } + if ($hasErrors) { + $this->error('Some errors occurred during index creation.'); - /** - * Create a single index with error handling. - * - * @param string $indexName - * @param array $schema - * @param bool $dropExisting - * @return bool - */ - protected function createIndex(string $indexName, array $schema, bool $dropExisting): bool - { - $this->info("Creating {$indexName} index..."); - $indices = $this->client->tables(); + return 1; + } - try { - // Optionally drop existing index - if ($dropExisting) { - try { - $this->info("Dropping existing {$indexName} index..."); - $indices->drop(['index' => $indexName, 'body' => ['silent' => true]]); - $this->info("Successfully dropped {$indexName} index."); - } catch (ResponseException $e) { - if (!str_contains($e->getMessage(), 'unknown index')) { - $this->warn("Warning when dropping {$indexName} index: " . $e->getMessage()); - } - } - } + $this->info('All Manticore Search indexes created successfully!'); - // Instead of checking if index exists (which doesn't work), - // try to create it directly and handle any errors - // that might occur if it already exists - $response = $indices->create([ - 'index' => $indexName, - 'body' => $schema - ]); + return 0; + } - $this->info("Successfully created {$indexName} index."); - $this->line('Response: ' . json_encode($response, JSON_PRETTY_PRINT)); - return true; - } catch (ResponseException $e) { - // Check if the error is because the index already exists - if (str_contains($e->getMessage(), 'already exists')) { - $this->warn("Index {$indexName} already exists. Use --drop option to recreate it."); - return true; - } + /** + * Create a single index with error handling. + */ + protected function createIndex(string $indexName, array $schema, bool $dropExisting): bool + { + $this->info("Creating {$indexName} index..."); + $indices = $this->client->tables(); - $this->error("Failed to create {$indexName} index: " . $e->getMessage()); - return false; - } catch (\Exception $e) { - $this->error("Failed to create {$indexName} index: " . $e->getMessage()); - return false; + try { + // Optionally drop existing index + if ($dropExisting) { + try { + $this->info("Dropping existing {$indexName} index..."); + $indices->drop(['index' => $indexName, 'body' => ['silent' => true]]); + $this->info("Successfully dropped {$indexName} index."); + } catch (ResponseException $e) { + if (! str_contains($e->getMessage(), 'unknown index')) { + $this->warn("Warning when dropping {$indexName} index: ".$e->getMessage()); } } } + + // Instead of checking if index exists (which doesn't work), + // try to create it directly and handle any errors + // that might occur if it already exists + $response = $indices->create([ + 'index' => $indexName, + 'body' => $schema, + ]); + + $this->info("Successfully created {$indexName} index."); + $this->line('Response: '.json_encode($response, JSON_PRETTY_PRINT)); + + return true; + } catch (ResponseException $e) { + // Check if the error is because the index already exists + if (str_contains($e->getMessage(), 'already exists')) { + $this->warn("Index {$indexName} already exists. Use --drop option to recreate it."); + + return true; + } + + $this->error("Failed to create {$indexName} index: ".$e->getMessage()); + + return false; + } catch (\Exception $e) { + $this->error("Failed to create {$indexName} index: ".$e->getMessage()); + + return false; + } + } +} diff --git a/app/Console/Commands/NntmuxPopulateSearchIndexes.php b/app/Console/Commands/NntmuxPopulateSearchIndexes.php index f4a72150e..e368ff5e1 100644 --- a/app/Console/Commands/NntmuxPopulateSearchIndexes.php +++ b/app/Console/Commands/NntmuxPopulateSearchIndexes.php @@ -58,7 +58,7 @@ class NntmuxPopulateSearchIndexes extends Command /** * Run releases. */ - private function manticoreReleases(): void + private function manticoreReleases(): void { $manticore = new ManticoreSearch; $manticore->truncateRTIndex(Arr::wrap('releases_rt')); @@ -97,7 +97,7 @@ class NntmuxPopulateSearchIndexes extends Command ]; $bar->advance(); } - if (!empty($data)) { + if (! empty($data)) { $manticore->manticoreSearch->table('releases_rt')->replaceDocuments($data); } }); @@ -143,7 +143,7 @@ class NntmuxPopulateSearchIndexes extends Command ]; $bar->advance(); } - if (!empty($data)) { + if (! empty($data)) { $manticore->manticoreSearch->table('predb_rt')->replaceDocuments($data); } });