<commit_before># --
# Copyright (C) 2001-2017 OTRS AG, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::System::Ticket::ArticleSearchIndex::DB;

use strict;
use warnings;

use Kernel::System::VariableCheck qw(:all);

our @ObjectDependencies = (
    'Kernel::Config',
    'Kernel::System::DB',
    'Kernel::System::Log',
    'Kernel::System::Ticket::Article',
);

=head1 NAME

Kernel::System::Ticket::ArticleSearchIndex::DB - DB based ticket article search index module

=head1 DESCRIPTION

This class provides functions to index articles for searching in the database.
The methods are currently documented in L<Kernel::System::Ticket::Article>.

=cut

sub new {
    my ( $Type, %Param ) = @_;

    my $Self = {};
    bless( $Self, $Type );

    return $Self;
}

sub ArticleSearchIndexBuild {
    my ( $Self, %Param ) = @_;

    for my $Needed (qw(TicketID ArticleID UserID)) {
        if ( !$Param{$Needed} ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => "Need $Needed!"
            );
            return;
        }
    }

    my $ArticleBackendObject = $Kernel::OM->Get('Kernel::System::Ticket::Article')->BackendForArticle(
        TicketID  => $Param{TicketID},
        ArticleID => $Param{ArticleID},
    );

    my %ArticleSearchableContent = $ArticleBackendObject->ArticleSearchableContentGet(
        TicketID  => $Param{TicketID},
        ArticleID => $Param{ArticleID},
        UserID    => $Param{UserID},
    );

    return 1 if !%ArticleSearchableContent;

    # clear old data from search index table
    my $Success = $Self->ArticleSearchIndexDelete(
        ArticleID => $Param{ArticleID},
        UserID    => $Param{UserID},
    );

    if ( !$Success ) {
        $Kernel::OM->Get('Kernel::System::Log')->Log(
            Priority => 'error',
            Message  => "Could not delete ArticleID '$Param{ArticleID}' from article search index!"
        );
        return;
    }

    my $FilterStopWords = $Kernel::OM->Get('Kernel::Config')->Get('Ticket::SearchIndex::FilterStopWords') // 1;

    my $DBObject = $Kernel::OM->Get('Kernel::System::DB');

    # determine the insert type to use
    my $DBType                 = $DBObject->GetDatabaseFunction('Type');
    my $AllowMultiple          = 1;
    my @SupportMultipleInserts = qw(mysql oracle postgresql);

    if ( !grep( /$DBType/, @SupportMultipleInserts ) ) {
        $AllowMultiple = 0;
    }

    my %MultiInsertStructure = (
        Oracle => {
            Start  => 'INSERT ALL ',
            Insert => '
                INTO article_search_index (
                    ticket_id, article_id, article_key, article_value
                )
                (?, ?, ?, ?) ',
            End  => 'SELECT * FROM DUAL',
            Join => ' ',
        },
        Other => {
            Start  => 'INSERT INTO article_search_index (ticket_id, article_id, article_key, article_value) VALUES ',
            Insert => '(?, ?, ?, ?) ',
            End    => '',
            Join   => ', ',<commit_msg>Fix oracle sql in article search index build.<commit_after># --
# Copyright (C) 2001-2017 OTRS AG, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::System::Ticket::ArticleSearchIndex::DB;

use strict;
use warnings;

use Kernel::System::VariableCheck qw(:all);

our @ObjectDependencies = (
    'Kernel::Config',
    'Kernel::System::DB',
    'Kernel::System::Log',
    'Kernel::System::Ticket::Article',
);

=head1 NAME

Kernel::System::Ticket::ArticleSearchIndex::DB - DB based ticket article search index module

=head1 DESCRIPTION

This class provides functions to index articles for searching in the database.
The methods are currently documented in L<Kernel::System::Ticket::Article>.

=cut

sub new {
    my ( $Type, %Param ) = @_;

    my $Self = {};
    bless( $Self, $Type );

    return $Self;
}

sub ArticleSearchIndexBuild {
    my ( $Self, %Param ) = @_;

    for my $Needed (qw(TicketID ArticleID UserID)) {
        if ( !$Param{$Needed} ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => "Need $Needed!"
            );
            return;
        }
    }

    my $ArticleBackendObject = $Kernel::OM->Get('Kernel::System::Ticket::Article')->BackendForArticle(
        TicketID  => $Param{TicketID},
        ArticleID => $Param{ArticleID},
    );

    my %ArticleSearchableContent = $ArticleBackendObject->ArticleSearchableContentGet(
        TicketID  => $Param{TicketID},
        ArticleID => $Param{ArticleID},
        UserID    => $Param{UserID},
    );

    return 1 if !%ArticleSearchableContent;

    # clear old data from search index table
    my $Success = $Self->ArticleSearchIndexDelete(
        ArticleID => $Param{ArticleID},
        UserID    => $Param{UserID},
    );

    if ( !$Success ) {
        $Kernel::OM->Get('Kernel::System::Log')->Log(
            Priority => 'error',
            Message  => "Could not delete ArticleID '$Param{ArticleID}' from article search index!"
        );
        return;
    }

    my $FilterStopWords = $Kernel::OM->Get('Kernel::Config')->Get('Ticket::SearchIndex::FilterStopWords') // 1;

    my $DBObject = $Kernel::OM->Get('Kernel::System::DB');

    # determine the insert type to use
    my $DBType                 = $DBObject->GetDatabaseFunction('Type');
    my $AllowMultiple          = 1;
    my @SupportMultipleInserts = qw(mysql oracle postgresql);

    if ( !grep( /$DBType/, @SupportMultipleInserts ) ) {
        $AllowMultiple = 0;
    }

    my %MultiInsertStructure = (
        Oracle => {
            Start  => 'INSERT ALL ',
            Insert => '
                INTO article_search_index (
                    ticket_id, article_id, article_key, article_value
                )
                VALUES (?, ?, ?, ?) ',
            End  => 'SELECT * FROM DUAL',
            Join => ' ',
        },
        Other => {
            Start  => 'INSERT INTO article_search_index (ticket_id, article_id, article_key, article_value) VALUES ',
            Insert => '(?, ?, ?, ?) ',
            End    => '',
            Join   => ', ',
{'fraction_non_alphanumeric': 0.1449563753252717, 'fraction_numerical': 0.0042859329557630495, 'mean_word_length': 2.4571428571428573, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5619417', 'n_tokens_mistral': 2007, 'n_tokens_neox': 1893, 'n_words': 456}