<commit_before><?php
/**
 * ElasticsearchDocumentsDelegate.
 *
 * @author emi
 */

namespace Minds\Core\Channels\Delegates\Artifacts;

use Minds\Core\Channels\Snapshots\Repository;
use Minds\Core\Config;
use Minds\Core\Data\ElasticSearch\Client as ElasticsearchClient;
use Minds\Core\Di\Di;

class ElasticsearchDocumentsDelegate implements ArtifactsDelegateInterface
{
    /** @var Repository */
    protected $repository;

    /** @var Config */
    protected $config;

    /** @var ElasticsearchClient */
    protected $elasticsearch;

    /**
     * ElasticsearchDocumentsDelegate constructor.
     * @param Repository $repository
     * @param Config $config
     * @param ElasticsearchClient $elasticsearch
     */
    public function __construct(
        $repository = null,
        $config = null,
        $elasticsearch = null
    ) {
        $this->repository = $repository ?: new Repository();
        $this->config = $config ?: Di::_()->get('Config');
        $this->elasticsearch = $elasticsearch ?: Di::_()->get('Database\ElasticSearch');
    }

    /**
     * @param string|int $userGuid
     * @return bool
     */
    public function snapshot($userGuid)
    {
        return true;
    }

    /**
     * @param string|int $userGuid
     * @return bool
     * @throws \Exception
     */
    public function restore($userGuid)
    {
        $body = [
            'query' => [
                'bool' => [
                    'must' => [
                        [
                            'match' => [
                                'owner_guid' => (string) $userGuid,
                            ]
                        ],
                    ],
                ],
            ],
            'script' => [
                'inline' => 'ctx._source.deleted = false;',
                'lang' => 'painless',
            ],
        ];

        $query = [
            'index' => $this->config->get('elasticsearch')['index'],
            'body' => $body,
        ];

        $this->elasticsearch->getClient()->updateByQuery($query);

        return true;
    }

    /**
     * @param string|int $userGuid
     * @return bool
     */
    public function hide($userGuid)
    {
        $body = [
            'query' => [
                'bool' => [
                    'must' => [
                        [
                            'match' => [
                                'owner_guid' => (string) $userGuid,
                            ]
                        ],
                    ],
                ],
            ],
            'script' => [
                'inline' => 'ctx._source.deleted = true;',
                'lang' => 'painless',
            ],
        ];

        $query = [
            'index' => $this->config->get('elasticsearch')['index'],
            'body' => $body,<commit_msg>(fix): do not abort on conflicts<commit_after><?php
/**
 * ElasticsearchDocumentsDelegate.
 *
 * @author emi
 */

namespace Minds\Core\Channels\Delegates\Artifacts;

use Minds\Core\Channels\Snapshots\Repository;
use Minds\Core\Config;
use Minds\Core\Data\ElasticSearch\Client as ElasticsearchClient;
use Minds\Core\Di\Di;

class ElasticsearchDocumentsDelegate implements ArtifactsDelegateInterface
{
    /** @var Repository */
    protected $repository;

    /** @var Config */
    protected $config;

    /** @var ElasticsearchClient */
    protected $elasticsearch;

    /**
     * ElasticsearchDocumentsDelegate constructor.
     * @param Repository $repository
     * @param Config $config
     * @param ElasticsearchClient $elasticsearch
     */
    public function __construct(
        $repository = null,
        $config = null,
        $elasticsearch = null
    ) {
        $this->repository = $repository ?: new Repository();
        $this->config = $config ?: Di::_()->get('Config');
        $this->elasticsearch = $elasticsearch ?: Di::_()->get('Database\ElasticSearch');
    }

    /**
     * @param string|int $userGuid
     * @return bool
     */
    public function snapshot($userGuid)
    {
        return true;
    }

    /**
     * @param string|int $userGuid
     * @return bool
     * @throws \Exception
     */
    public function restore($userGuid)
    {
        $body = [
            'query' => [
                'bool' => [
                    'must' => [
                        [
                            'match' => [
                                'owner_guid' => (string) $userGuid,
                            ]
                        ],
                    ],
                ],
            ],
            'script' => [
                'inline' => 'ctx._source.deleted = false;',
                'lang' => 'painless',
            ],
        ];

        $query = [
            'index' => $this->config->get('elasticsearch')['index'],
            'body' => $body,
        ];

        $this->elasticsearch->getClient()->updateByQuery($query);

        return true;
    }

    /**
     * @param string|int $userGuid
     * @return bool
     */
    public function hide($userGuid)
    {
        $body = [
            'query' => [
                'bool' => [
                    'must' => [
                        [
                            'match' => [
                                'owner_guid' => (string) $userGuid,
                            ]
                        ],
                    ],
                ],
            ],
            'script' => [
                'inline' => 'ctx._source.deleted = true;',
                'lang' => 'painless',
            ],
        ];

        $query = [
            'index' => $this->config->get('elasticsearch')['index'],
            'body' => $body,
            'conflicts' => 'proceed'
{'fraction_non_alphanumeric': 0.1401195499296765, 'fraction_numerical': 0.0, 'mean_word_length': 1.3192009783938035, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1373508', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1495, 'n_words': 268}