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,33 +1,23 @@
<?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;
use Flarum\Extend as Flarum;
return [
(new Flarum\ServiceProvider())->register(Provider::class),
(new Flarum\ServiceProvider)->register(Provider::class),
(new Flarum\Frontend('forum'))
->js(__DIR__.'/js/dist/forum.js'),
->js(__DIR__ . '/js/dist/forum.js'),
(new Flarum\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js'),
->js(__DIR__ . '/js/dist/admin.js'),
(new Flarum\Locales(__DIR__.'/resources/locale')),
(new Flarum\Locales(__DIR__ . '/resources/locale')),
(new Flarum\Routes('api'))
->get('/blomstra/search/{type}', 'blomstra.search', Api\Controllers\SearchController::class)
->put('/blomstra/search/index', 'blomstra.search.index', Api\Controllers\IndexController::class),
(new Flarum\Console())
->command(Commands\BuildCommand::class),
(new Flarum\Console)
->command(Commands\BuildCommand::class)
];

View File

@ -1,15 +1,5 @@
<?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;
use Illuminate\Support\Arr;
@ -19,9 +9,7 @@ class Client extends \Flarum\Api\Client
{
public function get(string $path): ResponseInterface
{
if ($path === '/discussions' && Arr::has($this->queryParams, 'filter.q')) {
return parent::get('/blomstra/search/discussions');
}
if ($path === '/discussions' && Arr::has($this->queryParams, 'filter.q')) return parent::get("/blomstra/search/discussions");
return parent::get($path);
}

View File

@ -1,15 +1,5 @@
<?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;
use Blomstra\Search\Commands\BuildCommand;
@ -33,8 +23,7 @@ class IndexController implements RequestHandlerInterface
$command->run(
new ArrayInput([]),
new ConsoleOutput()
);
new ConsoleOutput);
return new EmptyResponse();
}

View File

@ -1,21 +1,11 @@
<?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;
use Blomstra\Search\Elasticsearch\MatchPhraseQuery;
use Blomstra\Search\Elasticsearch\MatchQuery;
use Blomstra\Search\Elasticsearch\TermsQuery;
use Blomstra\Search\Save\Document as ElasticDocument;
use Blomstra\Search\Elasticsearch\TermsQuery;
use Elasticsearch\Client;
use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Api\Serializer\DiscussionSerializer;
@ -44,12 +34,11 @@ class SearchController extends ListDiscussionsController
protected array $translateSort = [
'lastPostedAt' => 'updated_at',
'createdAt' => 'created_at',
'commentCount' => 'comment_count',
'commentCount' => 'comment_count'
];
public function __construct(protected Client $elastic, protected UrlGenerator $uri)
{
}
{}
protected function data(ServerRequestInterface $request, Document $document)
{
@ -69,14 +58,14 @@ class SearchController extends ListDiscussionsController
$filterQuery = BoolQuery::create();
if (!empty($search)) {
if (! empty($search)) {
$filterQuery
// @todo commented out to use only partial matching for now
->add($this->sentenceMatch($search))
->add($this->wordMatch($search, 'and'))
->add($this->wordMatch($search, 'or'))
// ->add($this->partialMatch($search))
;
;
}
$builder = (new Builder($this->elastic))
@ -102,7 +91,7 @@ class SearchController extends ListDiscussionsController
// If the first level of the relationship wasn't explicitly included,
// add it so the code below can look for it
if (!in_array('mostRelevantPost', $include)) {
if (! in_array('mostRelevantPost', $include)) {
$include[] = 'mostRelevantPost';
}
}
@ -117,19 +106,19 @@ class SearchController extends ListDiscussionsController
if ($type === 'posts') {
return [
'most_relevant_post_id' => $id,
'weight' => Arr::get($hit, 'sort.0'),
'weight' => Arr::get($hit, 'sort.0')
];
} else {
return [
'discussion_id' => $id,
'weight' => Arr::get($hit, 'sort.0'),
'weight' => Arr::get($hit, 'sort.0')
];
}
});
$document->addPaginationLinks(
$this->uri->to('api')->route('blomstra.search', [
'type' => 'discussions',
'type' => 'discussions'
]),
$request->getQueryParams(),
$offset,
@ -143,7 +132,7 @@ class SearchController extends ListDiscussionsController
->select('discussions.*')
->join('posts', 'posts.discussion_id', 'discussions.id')
// Extra safety to prevent leaking hidden discussion (titles) towards search results.
->when($actor->isGuest() || !$actor->hasPermission('discussion.hide'), fn ($query) => $query->whereNull('discussions.hidden_at'))
->when($actor->isGuest() || ! $actor->hasPermission('discussion.hide'), fn($query) => $query->whereNull('discussions.hidden_at'))
->where(function ($query) use ($results) {
$query
->whereIn('discussions.id', $results->pluck('discussion_id')->filter())
@ -298,6 +287,7 @@ class SearchController extends ListDiscussionsController
);
}
protected function getGroups(User $actor): Collection
{
/** @var Collection $groups */

View File

@ -1,20 +1,11 @@
<?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;
use Blomstra\Search\Jobs\Job;
use Blomstra\Search\Jobs\SavingJob;
use Blomstra\Search\Seeders\Seeder;
use Carbon\Carbon;
use Elasticsearch\Client;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Console\Command;
@ -61,14 +52,14 @@ class BuildCommand extends Command
'recipient_groups' => ['type' => 'integer'],
'recipient_users' => ['type' => 'integer'],
'comment_count' => ['type' => 'integer'],
],
]
];
if ($this->option('recreate')) {
// Flush the index.
$client->indices()->delete([
'index' => $index,
'ignore_unavailable' => true,
'ignore_unavailable' => true
]);
// Create a new index.
@ -80,35 +71,35 @@ class BuildCommand extends Command
'analysis' => [
'analyzer' => [
'flarum_analyzer' => [
'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english',
'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english'
],
'flarum_analyzer_partial' => [
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => [
'lowercase',
'partial_search_filter',
],
],
'partial_search_filter'
]
]
],
'filter' => [
'partial_search_filter' => [
'type' => 'ngram',
'min_gram' => 1,
'max_gram' => 10,
'token_chars' => ['letter', 'digit', 'symbol'],
],
],
],
],
],
'token_chars' => ['letter', 'digit', 'symbol']
]
]
]
]
]
]);
}
if ($this->option('recreate') || $this->option('mapping')) {
$client->indices()->putMapping([
'index' => $index,
'body' => $properties,
'body' => $properties
]);
}
@ -116,9 +107,7 @@ class BuildCommand extends Command
/** @var Seeder $seeder */
foreach ($seeders as $seeder) {
if ($only && $seeder->type() !== $only) {
continue;
}
if ($only && $seeder->type() !== $only) continue;
$total = 0;
@ -126,7 +115,7 @@ class BuildCommand extends Command
? ($this->continueAt($seeder->type()) ?? $seeder->query()->max('id'))
: $seeder->query()->max('id');
while ($continueAt !== null) {
while($continueAt !== null) {
/** @var Collection $collection */
$collection = $seeder->query()
->latest('id')

View File

@ -1,15 +1,5 @@
<?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;
use Spatie\ElasticsearchQueryBuilder\Queries\MatchQuery;
@ -32,7 +22,7 @@ class MatchPhraseQuery extends MatchQuery
$query[$this->field]['boost'] = $this->boost;
return [
'match_phrase' => $query,
'match_phrase' => $query
];
}
}

View File

@ -1,15 +1,5 @@
<?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;
class MatchQuery extends \Spatie\ElasticsearchQueryBuilder\Queries\MatchQuery

View File

@ -1,15 +1,5 @@
<?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;
use Spatie\ElasticsearchQueryBuilder\Queries\Query;
@ -52,8 +42,8 @@ class SimpleSearchQuery implements Query
'fields' => $this->fields,
'analyzer' => $this->analyzer,
'default_operator' => 'AND',
'boost' => $this->boost,
],
'boost' => $this->boost
]
];
}
}

View File

@ -1,15 +1,5 @@
<?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;
use Spatie\ElasticsearchQueryBuilder\Queries\Query;

View File

@ -1,15 +1,5 @@
<?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;
class WildcardQuery extends \Spatie\ElasticsearchQueryBuilder\Queries\WildcardQuery
@ -44,10 +34,11 @@ class WildcardQuery extends \Spatie\ElasticsearchQueryBuilder\Queries\WildcardQu
$query = parent::toArray();
$query['wildcard'][$this->field]['boost'] = $this->boost;
$query['wildcard'][$this->field]['case_insensitive'] = !$this->sensitivity;
$query['wildcard'][$this->field]['case_insensitive'] = ! $this->sensitivity;
if ($this->rewrite) {
$query['wildcard'][$this->field]['rewrite'] = $this->rewrite;
}
return $query;

View File

@ -1,22 +1,13 @@
<?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;
use Throwable;
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);
}

View File

@ -1,15 +1,5 @@
<?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;
use Elasticsearch\Client;
@ -19,23 +9,21 @@ class DeletingJob extends Job
{
public function handle(Client $client)
{
if ($this->models->isEmpty()) {
return;
}
if ($this->models->isEmpty()) return;
// Preparing body for storing.
$body = $this->models->map(function (Model $model) {
$document = $this->seeder->toDocument($model);
return [
['delete' => ['_index' => $this->index, '_id' => $document->id]],
['delete' => ['_index' => $this->index, '_id' => $document->id]]
];
})->flatten(1);
$response = $client->bulk([
'index' => $this->index,
'body' => $body->toArray(),
'refresh' => true,
'refresh' => true
]);
}
}

View File

@ -1,15 +1,5 @@
<?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;
use Blomstra\Search\Seeders\Seeder;
@ -26,8 +16,6 @@ abstract class Job extends AbstractJob
{
$this->index = resolve('blomstra.search.elastic_index');
if (static::$onQueue) {
$this->onQueue(static::$onQueue);
}
if (static::$onQueue) $this->onQueue(static::$onQueue);
}
}

View File

@ -1,15 +1,5 @@
<?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;
use Blomstra\Search\Exceptions\SeedingException;
@ -21,9 +11,7 @@ class SavingJob extends Job
{
public function handle(Client $client)
{
if ($this->models->isEmpty()) {
return;
}
if ($this->models->isEmpty()) return;
// Preparing body for storing.
$body = $this->models->map(function (Model $model) {
@ -31,7 +19,7 @@ class SavingJob extends Job
return [
['index' => ['_index' => $this->index, '_id' => $document->id]],
$document->toArray(),
$document->toArray()
];
})
->flatten(1);
@ -39,12 +27,10 @@ class SavingJob extends Job
$response = $client->bulk([
'index' => $this->index,
'body' => $body->toArray(),
'refresh' => true,
'refresh' => true
]);
if (Arr::get($response, 'errors') !== true) {
return true;
}
if (Arr::get($response, 'errors') !== true) return true;
$items = Arr::get($response, 'items');

View File

@ -1,20 +1,11 @@
<?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;
use Blomstra\Search\Jobs\DeletingJob;
use Blomstra\Search\Jobs\Job;
use Blomstra\Search\Jobs\SavingJob;
use Blomstra\Search\Seeders;
use Elasticsearch\Client as Elastic;
use Elasticsearch\ClientBuilder;
use Flarum\Api\Client;
@ -62,6 +53,7 @@ class Provider extends AbstractServiceProvider
return $builder->build();
});
$this->container->instance(
'blomstra.search.elastic_index',
$settings->get('blomstra-search.elastic-index', 'flarum')
@ -70,19 +62,19 @@ class Provider extends AbstractServiceProvider
$this->container->extend(
Client::class,
function () {
$pipe = new MiddlewarePipe();
$pipe = new MiddlewarePipe;
$exclude = resolve('flarum.api_client.exclude_middleware');
$middlewareStack = array_filter(resolve('flarum.api.middleware'), function ($middlewareClass) use ($exclude) {
return !in_array($middlewareClass, $exclude);
return ! in_array($middlewareClass, $exclude);
});
foreach ($middlewareStack as $middleware) {
$pipe->pipe(resolve($middleware));
}
$pipe->pipe(new ExecuteRoute());
$pipe->pipe(new ExecuteRoute);
return new Api\Client($pipe);
}

View File

@ -1,15 +1,5 @@
<?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;
use Carbon\Carbon;

View File

@ -1,15 +1,5 @@
<?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;
use Blomstra\Search\Save\Document;
@ -25,7 +15,7 @@ class CommentSeeder extends Seeder
{
public function type(): string
{
return resolve(PostSerializer::class)->getType(new CommentPost());
return resolve(PostSerializer::class)->getType(new CommentPost);
}
public function query(): Builder
@ -63,14 +53,13 @@ class CommentSeeder extends Seeder
/**
* @param CommentPost $model
*
* @return Document
*/
public function toDocument(Model $model): Document
{
$document = new Document([
'type' => $this->type(),
'id' => $this->type().':'.$model->id,
'id' => $this->type() . ':' . $model->id,
'content' => $model->content,
'content_partial' => $model->content,
'created_at' => $model->created_at?->toAtomString(),

View File

@ -1,15 +1,5 @@
<?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;
use Blomstra\Search\Save\Document;
@ -27,7 +17,7 @@ class DiscussionSeeder extends Seeder
{
public function type(): string
{
return resolve(DiscussionSerializer::class)->getType(new Discussion());
return resolve(DiscussionSerializer::class)->getType(new Discussion);
}
public function query(): Builder
@ -42,7 +32,6 @@ class DiscussionSeeder extends Seeder
$includes[] = 'recipientUsers';
$includes[] = 'recipientGroups';
}
return Discussion::query()
->whereNull('hidden_at')
->with($includes);
@ -64,14 +53,13 @@ class DiscussionSeeder extends Seeder
/**
* @param Discussion $model
*
* @return Document
*/
public function toDocument(Model $model): Document
{
$document = new Document([
'type' => $this->type(),
'id' => $this->type().':'.$model->id,
'id' => $this->type() . ':' . $model->id,
'content' => $model->title,
'content_partial' => $model->title,
'created_at' => $model->created_at?->toAtomString(),

View File

@ -1,15 +1,5 @@
<?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;
use Blomstra\Search\Save\Document;
@ -30,7 +20,6 @@ abstract class Seeder
abstract public function query(): Builder;
abstract public static function savingOn(Dispatcher $events, callable $callable);
abstract public static function deletingOn(Dispatcher $events, callable $callable);
abstract public function toDocument(Model $model): Document;
@ -67,7 +56,7 @@ abstract class Seeder
})->flatten();
}
if (!$discussion->is_private && $permissions->isEmpty()) {
if (! $discussion->is_private && $permissions->isEmpty()) {
$permissions = $globalPermission;
}