Revert "Apply fixes from StyleCI"

This reverts commit a26f88a9b6.
This commit is contained in:
Ian Morland 2022-11-18 09:34:16 +00:00
parent a26f88a9b6
commit 5815144d3a
19 changed files with 106 additions and 308 deletions

View File

@ -1,21 +1,11 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search; namespace Blomstra\Search;
use Flarum\Extend as Flarum; use Flarum\Extend as Flarum;
return [ return [
(new Flarum\ServiceProvider())->register(Provider::class), (new Flarum\ServiceProvider)->register(Provider::class),
(new Flarum\Frontend('forum')) (new Flarum\Frontend('forum'))
->js(__DIR__ . '/js/dist/forum.js'), ->js(__DIR__ . '/js/dist/forum.js'),
@ -28,6 +18,6 @@ return [
->get('/blomstra/search/{type}', 'blomstra.search', Api\Controllers\SearchController::class) ->get('/blomstra/search/{type}', 'blomstra.search', Api\Controllers\SearchController::class)
->put('/blomstra/search/index', 'blomstra.search.index', Api\Controllers\IndexController::class), ->put('/blomstra/search/index', 'blomstra.search.index', Api\Controllers\IndexController::class),
(new Flarum\Console()) (new Flarum\Console)
->command(Commands\BuildCommand::class), ->command(Commands\BuildCommand::class)
]; ];

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Api; namespace Blomstra\Search\Api;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
@ -19,9 +9,7 @@ class Client extends \Flarum\Api\Client
{ {
public function get(string $path): ResponseInterface public function get(string $path): ResponseInterface
{ {
if ($path === '/discussions' && Arr::has($this->queryParams, 'filter.q')) { if ($path === '/discussions' && Arr::has($this->queryParams, 'filter.q')) return parent::get("/blomstra/search/discussions");
return parent::get('/blomstra/search/discussions');
}
return parent::get($path); return parent::get($path);
} }

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Api\Controllers; namespace Blomstra\Search\Api\Controllers;
use Blomstra\Search\Commands\BuildCommand; use Blomstra\Search\Commands\BuildCommand;
@ -33,8 +23,7 @@ class IndexController implements RequestHandlerInterface
$command->run( $command->run(
new ArrayInput([]), new ArrayInput([]),
new ConsoleOutput() new ConsoleOutput);
);
return new EmptyResponse(); return new EmptyResponse();
} }

View File

@ -1,21 +1,11 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Api\Controllers; namespace Blomstra\Search\Api\Controllers;
use Blomstra\Search\Elasticsearch\MatchPhraseQuery; use Blomstra\Search\Elasticsearch\MatchPhraseQuery;
use Blomstra\Search\Elasticsearch\MatchQuery; use Blomstra\Search\Elasticsearch\MatchQuery;
use Blomstra\Search\Elasticsearch\TermsQuery;
use Blomstra\Search\Save\Document as ElasticDocument; use Blomstra\Search\Save\Document as ElasticDocument;
use Blomstra\Search\Elasticsearch\TermsQuery;
use Elasticsearch\Client; use Elasticsearch\Client;
use Flarum\Api\Controller\ListDiscussionsController; use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Api\Serializer\DiscussionSerializer; use Flarum\Api\Serializer\DiscussionSerializer;
@ -44,12 +34,11 @@ class SearchController extends ListDiscussionsController
protected array $translateSort = [ protected array $translateSort = [
'lastPostedAt' => 'updated_at', 'lastPostedAt' => 'updated_at',
'createdAt' => 'created_at', 'createdAt' => 'created_at',
'commentCount' => 'comment_count', 'commentCount' => 'comment_count'
]; ];
public function __construct(protected Client $elastic, protected UrlGenerator $uri) public function __construct(protected Client $elastic, protected UrlGenerator $uri)
{ {}
}
protected function data(ServerRequestInterface $request, Document $document) protected function data(ServerRequestInterface $request, Document $document)
{ {
@ -117,19 +106,19 @@ class SearchController extends ListDiscussionsController
if ($type === 'posts') { if ($type === 'posts') {
return [ return [
'most_relevant_post_id' => $id, 'most_relevant_post_id' => $id,
'weight' => Arr::get($hit, 'sort.0'), 'weight' => Arr::get($hit, 'sort.0')
]; ];
} else { } else {
return [ return [
'discussion_id' => $id, 'discussion_id' => $id,
'weight' => Arr::get($hit, 'sort.0'), 'weight' => Arr::get($hit, 'sort.0')
]; ];
} }
}); });
$document->addPaginationLinks( $document->addPaginationLinks(
$this->uri->to('api')->route('blomstra.search', [ $this->uri->to('api')->route('blomstra.search', [
'type' => 'discussions', 'type' => 'discussions'
]), ]),
$request->getQueryParams(), $request->getQueryParams(),
$offset, $offset,
@ -298,6 +287,7 @@ class SearchController extends ListDiscussionsController
); );
} }
protected function getGroups(User $actor): Collection protected function getGroups(User $actor): Collection
{ {
/** @var Collection $groups */ /** @var Collection $groups */

View File

@ -1,20 +1,11 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Commands; namespace Blomstra\Search\Commands;
use Blomstra\Search\Jobs\Job; use Blomstra\Search\Jobs\Job;
use Blomstra\Search\Jobs\SavingJob; use Blomstra\Search\Jobs\SavingJob;
use Blomstra\Search\Seeders\Seeder; use Blomstra\Search\Seeders\Seeder;
use Carbon\Carbon;
use Elasticsearch\Client; use Elasticsearch\Client;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Console\Command; use Illuminate\Console\Command;
@ -61,14 +52,14 @@ class BuildCommand extends Command
'recipient_groups' => ['type' => 'integer'], 'recipient_groups' => ['type' => 'integer'],
'recipient_users' => ['type' => 'integer'], 'recipient_users' => ['type' => 'integer'],
'comment_count' => ['type' => 'integer'], 'comment_count' => ['type' => 'integer'],
], ]
]; ];
if ($this->option('recreate')) { if ($this->option('recreate')) {
// Flush the index. // Flush the index.
$client->indices()->delete([ $client->indices()->delete([
'index' => $index, 'index' => $index,
'ignore_unavailable' => true, 'ignore_unavailable' => true
]); ]);
// Create a new index. // Create a new index.
@ -80,35 +71,35 @@ class BuildCommand extends Command
'analysis' => [ 'analysis' => [
'analyzer' => [ 'analyzer' => [
'flarum_analyzer' => [ 'flarum_analyzer' => [
'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english', 'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english'
], ],
'flarum_analyzer_partial' => [ 'flarum_analyzer_partial' => [
'type' => 'custom', 'type' => 'custom',
'tokenizer' => 'standard', 'tokenizer' => 'standard',
'filter' => [ 'filter' => [
'lowercase', 'lowercase',
'partial_search_filter', 'partial_search_filter'
], ]
], ]
], ],
'filter' => [ 'filter' => [
'partial_search_filter' => [ 'partial_search_filter' => [
'type' => 'ngram', 'type' => 'ngram',
'min_gram' => 1, 'min_gram' => 1,
'max_gram' => 10, 'max_gram' => 10,
'token_chars' => ['letter', 'digit', 'symbol'], 'token_chars' => ['letter', 'digit', 'symbol']
], ]
], ]
], ]
], ]
], ]
]); ]);
} }
if ($this->option('recreate') || $this->option('mapping')) { if ($this->option('recreate') || $this->option('mapping')) {
$client->indices()->putMapping([ $client->indices()->putMapping([
'index' => $index, 'index' => $index,
'body' => $properties, 'body' => $properties
]); ]);
} }
@ -116,9 +107,7 @@ class BuildCommand extends Command
/** @var Seeder $seeder */ /** @var Seeder $seeder */
foreach ($seeders as $seeder) { foreach ($seeders as $seeder) {
if ($only && $seeder->type() !== $only) { if ($only && $seeder->type() !== $only) continue;
continue;
}
$total = 0; $total = 0;

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Elasticsearch; namespace Blomstra\Search\Elasticsearch;
use Spatie\ElasticsearchQueryBuilder\Queries\MatchQuery; use Spatie\ElasticsearchQueryBuilder\Queries\MatchQuery;
@ -32,7 +22,7 @@ class MatchPhraseQuery extends MatchQuery
$query[$this->field]['boost'] = $this->boost; $query[$this->field]['boost'] = $this->boost;
return [ return [
'match_phrase' => $query, 'match_phrase' => $query
]; ];
} }
} }

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Elasticsearch; namespace Blomstra\Search\Elasticsearch;
class MatchQuery extends \Spatie\ElasticsearchQueryBuilder\Queries\MatchQuery class MatchQuery extends \Spatie\ElasticsearchQueryBuilder\Queries\MatchQuery

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Elasticsearch; namespace Blomstra\Search\Elasticsearch;
use Spatie\ElasticsearchQueryBuilder\Queries\Query; use Spatie\ElasticsearchQueryBuilder\Queries\Query;
@ -52,8 +42,8 @@ class SimpleSearchQuery implements Query
'fields' => $this->fields, 'fields' => $this->fields,
'analyzer' => $this->analyzer, 'analyzer' => $this->analyzer,
'default_operator' => 'AND', 'default_operator' => 'AND',
'boost' => $this->boost, 'boost' => $this->boost
], ]
]; ];
} }
} }

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Elasticsearch; namespace Blomstra\Search\Elasticsearch;
use Spatie\ElasticsearchQueryBuilder\Queries\Query; use Spatie\ElasticsearchQueryBuilder\Queries\Query;

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Elasticsearch; namespace Blomstra\Search\Elasticsearch;
class WildcardQuery extends \Spatie\ElasticsearchQueryBuilder\Queries\WildcardQuery class WildcardQuery extends \Spatie\ElasticsearchQueryBuilder\Queries\WildcardQuery
@ -48,6 +38,7 @@ class WildcardQuery extends \Spatie\ElasticsearchQueryBuilder\Queries\WildcardQu
if ($this->rewrite) { if ($this->rewrite) {
$query['wildcard'][$this->field]['rewrite'] = $this->rewrite; $query['wildcard'][$this->field]['rewrite'] = $this->rewrite;
} }
return $query; return $query;

View File

@ -1,22 +1,13 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Exceptions; namespace Blomstra\Search\Exceptions;
use Throwable; use Throwable;
class SeedingException extends \Exception class SeedingException extends \Exception
{ {
public function __construct($message = '', public array $items, $code = 0, Throwable $previous = null)
public function __construct($message = "", public array $items, $code = 0, Throwable $previous = null)
{ {
parent::__construct($message, $code, $previous); parent::__construct($message, $code, $previous);
} }

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Jobs; namespace Blomstra\Search\Jobs;
use Elasticsearch\Client; use Elasticsearch\Client;
@ -19,23 +9,21 @@ class DeletingJob extends Job
{ {
public function handle(Client $client) public function handle(Client $client)
{ {
if ($this->models->isEmpty()) { if ($this->models->isEmpty()) return;
return;
}
// Preparing body for storing. // Preparing body for storing.
$body = $this->models->map(function (Model $model) { $body = $this->models->map(function (Model $model) {
$document = $this->seeder->toDocument($model); $document = $this->seeder->toDocument($model);
return [ return [
['delete' => ['_index' => $this->index, '_id' => $document->id]], ['delete' => ['_index' => $this->index, '_id' => $document->id]]
]; ];
})->flatten(1); })->flatten(1);
$response = $client->bulk([ $response = $client->bulk([
'index' => $this->index, 'index' => $this->index,
'body' => $body->toArray(), 'body' => $body->toArray(),
'refresh' => true, 'refresh' => true
]); ]);
} }
} }

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Jobs; namespace Blomstra\Search\Jobs;
use Blomstra\Search\Seeders\Seeder; use Blomstra\Search\Seeders\Seeder;
@ -26,8 +16,6 @@ abstract class Job extends AbstractJob
{ {
$this->index = resolve('blomstra.search.elastic_index'); $this->index = resolve('blomstra.search.elastic_index');
if (static::$onQueue) { if (static::$onQueue) $this->onQueue(static::$onQueue);
$this->onQueue(static::$onQueue);
}
} }
} }

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Jobs; namespace Blomstra\Search\Jobs;
use Blomstra\Search\Exceptions\SeedingException; use Blomstra\Search\Exceptions\SeedingException;
@ -21,9 +11,7 @@ class SavingJob extends Job
{ {
public function handle(Client $client) public function handle(Client $client)
{ {
if ($this->models->isEmpty()) { if ($this->models->isEmpty()) return;
return;
}
// Preparing body for storing. // Preparing body for storing.
$body = $this->models->map(function (Model $model) { $body = $this->models->map(function (Model $model) {
@ -31,7 +19,7 @@ class SavingJob extends Job
return [ return [
['index' => ['_index' => $this->index, '_id' => $document->id]], ['index' => ['_index' => $this->index, '_id' => $document->id]],
$document->toArray(), $document->toArray()
]; ];
}) })
->flatten(1); ->flatten(1);
@ -39,12 +27,10 @@ class SavingJob extends Job
$response = $client->bulk([ $response = $client->bulk([
'index' => $this->index, 'index' => $this->index,
'body' => $body->toArray(), 'body' => $body->toArray(),
'refresh' => true, 'refresh' => true
]); ]);
if (Arr::get($response, 'errors') !== true) { if (Arr::get($response, 'errors') !== true) return true;
return true;
}
$items = Arr::get($response, 'items'); $items = Arr::get($response, 'items');

View File

@ -1,20 +1,11 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search; namespace Blomstra\Search;
use Blomstra\Search\Jobs\DeletingJob; use Blomstra\Search\Jobs\DeletingJob;
use Blomstra\Search\Jobs\Job; use Blomstra\Search\Jobs\Job;
use Blomstra\Search\Jobs\SavingJob; use Blomstra\Search\Jobs\SavingJob;
use Blomstra\Search\Seeders;
use Elasticsearch\Client as Elastic; use Elasticsearch\Client as Elastic;
use Elasticsearch\ClientBuilder; use Elasticsearch\ClientBuilder;
use Flarum\Api\Client; use Flarum\Api\Client;
@ -62,6 +53,7 @@ class Provider extends AbstractServiceProvider
return $builder->build(); return $builder->build();
}); });
$this->container->instance( $this->container->instance(
'blomstra.search.elastic_index', 'blomstra.search.elastic_index',
$settings->get('blomstra-search.elastic-index', 'flarum') $settings->get('blomstra-search.elastic-index', 'flarum')
@ -70,7 +62,7 @@ class Provider extends AbstractServiceProvider
$this->container->extend( $this->container->extend(
Client::class, Client::class,
function () { function () {
$pipe = new MiddlewarePipe(); $pipe = new MiddlewarePipe;
$exclude = resolve('flarum.api_client.exclude_middleware'); $exclude = resolve('flarum.api_client.exclude_middleware');
@ -82,7 +74,7 @@ class Provider extends AbstractServiceProvider
$pipe->pipe(resolve($middleware)); $pipe->pipe(resolve($middleware));
} }
$pipe->pipe(new ExecuteRoute()); $pipe->pipe(new ExecuteRoute);
return new Api\Client($pipe); return new Api\Client($pipe);
} }

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Save; namespace Blomstra\Search\Save;
use Carbon\Carbon; use Carbon\Carbon;

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Seeders; namespace Blomstra\Search\Seeders;
use Blomstra\Search\Save\Document; use Blomstra\Search\Save\Document;
@ -25,7 +15,7 @@ class CommentSeeder extends Seeder
{ {
public function type(): string public function type(): string
{ {
return resolve(PostSerializer::class)->getType(new CommentPost()); return resolve(PostSerializer::class)->getType(new CommentPost);
} }
public function query(): Builder public function query(): Builder
@ -63,7 +53,6 @@ class CommentSeeder extends Seeder
/** /**
* @param CommentPost $model * @param CommentPost $model
*
* @return Document * @return Document
*/ */
public function toDocument(Model $model): Document public function toDocument(Model $model): Document

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Seeders; namespace Blomstra\Search\Seeders;
use Blomstra\Search\Save\Document; use Blomstra\Search\Save\Document;
@ -27,7 +17,7 @@ class DiscussionSeeder extends Seeder
{ {
public function type(): string public function type(): string
{ {
return resolve(DiscussionSerializer::class)->getType(new Discussion()); return resolve(DiscussionSerializer::class)->getType(new Discussion);
} }
public function query(): Builder public function query(): Builder
@ -42,7 +32,6 @@ class DiscussionSeeder extends Seeder
$includes[] = 'recipientUsers'; $includes[] = 'recipientUsers';
$includes[] = 'recipientGroups'; $includes[] = 'recipientGroups';
} }
return Discussion::query() return Discussion::query()
->whereNull('hidden_at') ->whereNull('hidden_at')
->with($includes); ->with($includes);
@ -64,7 +53,6 @@ class DiscussionSeeder extends Seeder
/** /**
* @param Discussion $model * @param Discussion $model
*
* @return Document * @return Document
*/ */
public function toDocument(Model $model): Document public function toDocument(Model $model): Document

View File

@ -1,15 +1,5 @@
<?php <?php
/*
* This file is part of ianm/translate.
*
* Copyright (c) 2022 Blomstra Ltd.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/
namespace Blomstra\Search\Seeders; namespace Blomstra\Search\Seeders;
use Blomstra\Search\Save\Document; use Blomstra\Search\Save\Document;
@ -30,7 +20,6 @@ abstract class Seeder
abstract public function query(): Builder; abstract public function query(): Builder;
abstract public static function savingOn(Dispatcher $events, callable $callable); abstract public static function savingOn(Dispatcher $events, callable $callable);
abstract public static function deletingOn(Dispatcher $events, callable $callable); abstract public static function deletingOn(Dispatcher $events, callable $callable);
abstract public function toDocument(Model $model): Document; abstract public function toDocument(Model $model): Document;