[Rt-commit] rt branch, 4.4/rt-munge-attachments, repushed

Craig Kaiser craig at bestpractical.com
Mon Aug 6 15:44:37 EDT 2018


The branch 4.4/rt-munge-attachments was deleted and repushed:
       was df998852a1ae395f5a184af5f02d01108df00fe2
       now af8d8bf49cc388e9a304b02c7ea85c9d3666dd88

1: 9e335698a ! 1: 86ca17d02 Create rt-munge-attachments executable
    @@ -1,6 +1,18 @@
     Author: Craig Kaiser <craig at bestpractical.com>
     
         Create rt-munge-attachments executable
    +
    +diff --git a/.gitignore b/.gitignore
    +--- a/.gitignore
    ++++ b/.gitignore
    +@@
    + /sbin/rt-ldapimport
    + /sbin/rt-passwd
    + /sbin/standalone_httpd
    ++/sbin/rt-munge-attachments
    + /var/mason_data/
    + /autom4te.cache/
    + /configure
     
     diff --git a/Makefile.in b/Makefile.in
     --- a/Makefile.in
    @@ -13,147 +25,6 @@
      				rt-clean-sessions \
      				rt-dump-metadata \
      				rt-email-dashboards \
    -
    -diff --git a/sbin/rt-munge-attachments b/sbin/rt-munge-attachments
    -new file mode 100755
    ---- /dev/null
    -+++ b/sbin/rt-munge-attachments
    -@@
    -+#!/Users/craigkaiser/perl5/perlbrew/perls/perl-5.24.3/bin/perl
    -+# BEGIN BPS TAGGED BLOCK {{{
    -+#
    -+# COPYRIGHT:
    -+#
    -+# This software is Copyright (c) 1996-2018 Best Practical Solutions, LLC
    -+#                                          <sales at bestpractical.com>
    -+#
    -+# (Except where explicitly superseded by other copyright notices)
    -+#
    -+#
    -+# LICENSE:
    -+#
    -+# This work is made available to you under the terms of Version 2 of
    -+# the GNU General Public License. A copy of that license should have
    -+# been provided with this software, but in any event can be snarfed
    -+# from www.gnu.org.
    -+#
    -+# This work is distributed in the hope that it will be useful, but
    -+# WITHOUT ANY WARRANTY; without even the implied warranty of
    -+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    -+# General Public License for more details.
    -+#
    -+# You should have received a copy of the GNU General Public License
    -+# along with this program; if not, write to the Free Software
    -+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    -+# 02110-1301 or visit their web page on the internet at
    -+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
    -+#
    -+#
    -+# CONTRIBUTION SUBMISSION POLICY:
    -+#
    -+# (The following paragraph is not intended to limit the rights granted
    -+# to you to modify and distribute this software under the terms of
    -+# the GNU General Public License and is only of importance to you if
    -+# you choose to contribute your changes and enhancements to the
    -+# community by submitting them to Best Practical Solutions, LLC.)
    -+#
    -+# By intentionally submitting any modifications, corrections or
    -+# derivatives to this work, or any other work intended for use with
    -+# Request Tracker, to Best Practical Solutions, LLC, you confirm that
    -+# you are the copyright holder for those contributions and you grant
    -+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
    -+# royalty-free, perpetual, license to use, copy, create derivative
    -+# works based on those contributions, and sublicense and distribute
    -+# those contributions and any derivatives thereof.
    -+#
    -+# END BPS TAGGED BLOCK }}}
    -+use strict;
    -+use warnings;
    -+
    -+# fix lib paths, some may be relative
    -+BEGIN {    # BEGIN RT CMD BOILERPLATE
    -+    require File::Spec;
    -+    require Cwd;
    -+    my @libs = ( "./lib", "./local/lib" );
    -+    my $bin_path;
    -+
    -+    for my $lib (@libs) {
    -+        unless ( File::Spec->file_name_is_absolute($lib) ) {
    -+            $bin_path
    -+                ||= ( File::Spec->splitpath( Cwd::abs_path(__FILE__) ) )[1];
    -+            $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
    -+        }
    -+        unshift @INC, $lib;
    -+    }
    -+
    -+}
    -+
    -+# Read in the options
    -+my %opts;
    -+use Getopt::Long;
    -+GetOptions( \%opts, "help|h", "verbose|v", "search=s", "replace=s", );
    -+
    -+if ( $opts{'help'} ) {
    -+    require Pod::Usage;
    -+    print Pod::Usage::pod2usage( -verbose => 2 );
    -+    exit;
    -+}
    -+
    -+use RT -init;
    -+
    -+RT::Logger->info("Running RT-Munge-Attachments");
    -+
    -+if ( $opts{'search'} ) {
    -+    my $replacement = $opts{'replacement'} || undef;
    -+
    -+    my $search = $opts{'search'};
    -+
    -+    my $attachments = RT::Attachments->new( RT->SystemUser );
    -+    $attachments->Limit(
    -+        FIELD    => 'Headers',
    -+        OPERATOR => 'LIKE',
    -+        VALUE    => $search,
    -+    );
    -+
    -+    while ( my $attachment = $attachments->Next ) {
    -+        foreach my $header ($attachment->Headers) {
    -+            if ( $header =~ /$search/ ) {
    -+                (my $tag) = ($header =~ /^([^\:]+)/);
    -+                if ($replacement) {
    -+                    my ( $ret, $msg ) = $attachment->SetHeader($tag, $replacement);
    -+                    RT::Logger->info($msg);
    -+                } else {
    -+                    my ( $ret, $msg ) = $attachment->DelHeader($tag);
    -+                    RT::Logger->info($msg);
    -+                }
    -+            }
    -+        }
    -+    }
    -+
    -+}
    -+
    -+=head1 rt-munge-attachments
    -+
    -+rt-munge-attachments - Remove references to provided user from attachments table
    -+
    -+=head1 DESCRIPTION
    -+
    -+rt-munge-attachments is used to remove references to a search string.
    -+
    -+=head1 OPTIONS
    -+
    -+=over 8
    -+
    -+=item --search=SEARCH
    -+
    -+Provide a string to search the header column in the attachments table, if a match
    -+is found the header will be deleted.
    -+
    -+=item --replace=REPLACEMENT
    -+
    -+Provide a string to replace strings matching the search string in headers from the
    -+attachments table. Then the Content will be checked for the attachments table where
    -+a replacement will be used on matching content.
     
     diff --git a/sbin/rt-munge-attachments.in b/sbin/rt-munge-attachments.in
     new file mode 100644
    @@ -232,7 +103,7 @@
     +# Read in the options
     +my %opts;
     +use Getopt::Long;
    -+GetOptions( \%opts, "help|h", "search=s", "replace=s", );
    ++GetOptions( \%opts, "help|h", "search=s", "replacement=s", );
     +
     +if ( $opts{'help'} ) {
     +    require Pod::Usage;
    @@ -243,19 +114,15 @@
     +use RT -init;
     +
     +if ( $opts{'search'} ) {
    -+    my $replace = $opts{'replace'} || "";
    ++    my $replacement = $opts{'replacement'} || undef;
    ++
     +    my $search = $opts{'search'};
     +
    -+    my $attachments = RT::Attachments->new(RT->SystemUser);
    ++    my $attachments = RT::Attachments->new( RT->SystemUser );
    ++    my ($ret, $msg) = $attachments->ReplaceAttachments(Search => $search, Replacement => $replacement);
     +
    -+    my ($ret, $msg) = $attachments->ReplaceAttachments( Search => $search );
    -+    if ( $ret ) {
    -+        RT::Logger->info($msg);
    -+    } else {
    -+        RT::Logger->error($msg);
    -+    }
    -+} else {
    -+    print "Error: Provide search sting with --SEARCH flag \n";
    ++    RT::Logger->error($msg) unless $ret;
    ++    RT::Logger->info($msg) unless !$ret;
     +}
     +
     +=head1 rt-munge-attachments
    @@ -283,5 +150,4 @@
     +attachments table.
     +
     +=back
    -\ No newline at end of file
     
2: 88d0cdd84 ! 2: af8d8bf49 Create ReplaceAttachments, ReplaceHeader and ReplaceContent Methods
    @@ -1,9 +1,9 @@
     Author: Craig Kaiser <craig at bestpractical.com>
     
    -    Add methods ReplaceHeaders ReplaceContent to Attachment.pm
    +    Create ReplaceAttachments, ReplaceHeader and ReplaceContent Methods
         
    -    Methods for searching the Headers and Content columns in the
    -    attachments table and replacing any matches that are found.
    +    Quickly search on the attachments table and remove or replace matching
    +    data found.
     
     diff --git a/lib/RT/Attachment.pm b/lib/RT/Attachment.pm
     --- a/lib/RT/Attachment.pm
    @@ -31,7 +31,7 @@
     +    return (0, 'Provide search string for ReplaceHeaders() to search on') unless $args{Search};
     +
     +    foreach my $header ($self->SplitHeaders) {
    -+        if ( $header =~ /$args{Search}/ig ) {
    ++        if ( $header =~ /\Q$args{Search}\E/ig ) {
     +            (my $tag) = ($header =~ /^([^\:]+)/);
     +            my ( $ret, $msg ) = $self->SetHeader($tag, $args{Replacement});
     +            return ($ret, $msg) unless $ret;
    @@ -51,7 +51,7 @@
     +sub ReplaceContent {
     +    my $self = shift;
     +    my %args = (
    -+        Search   => undef,
    ++        Search      => undef,
     +        Replacement => "",
     +        @_,
     +    );
    @@ -60,10 +60,12 @@
     +
     +    my $content = $self->Content;
     +
    -+    $content =~ s/\Q$args{Search}\E/$args{Replacement}/ig;
    -+
    -+    my ($ret, $msg) = $self->_Set( Field => 'Content', Value => $content );
    -+    return ($ret, $msg);
    ++    if ( $content ) {
    ++        $content =~ s/\Q$args{Search}\E/\Q$args{Replacement}\E/ig;
    ++        my ($ret, $msg) = $self->_Set( Field => 'Content', Value => $content );
    ++        return ($ret, $msg);
    ++    }
    ++    return(1, 'No matches found in Attachments table Content column');
     +}
     +
     +
    @@ -71,3 +73,62 @@
          my $self  = shift;
          my $value = shift;
     
    +diff --git a/lib/RT/Attachments.pm b/lib/RT/Attachments.pm
    +--- a/lib/RT/Attachments.pm
    ++++ b/lib/RT/Attachments.pm
    +@@
    +     return $self->SUPER::AddRecord( $record );
    + }
    + 
    ++=head2 ReplaceAttachments ( Search => 'SEARCH', Replacement => 'Replacement', Header => 1, Content => 1 )
    ++
    ++Provide a search string to search the attachments table for, by default the Headers and Content
    ++columns will both be searched for matches.
    ++
    ++=cut
    ++
    ++sub ReplaceAttachments {
    ++    my $self = shift;
    ++    my %args = (
    ++        Search      => undef,
    ++        Replacement => '',
    ++        Headers     => 1,
    ++        Content     => 1,
    ++        @_,
    ++    );
    ++
    ++    return (0, 'Provide a search string to search on') unless $args{Search};
    ++
    ++    $self->Limit(
    ++        ENTRYAGGREGATOR => 'OR',
    ++        FIELD           => 'Headers',
    ++        OPERATOR        => 'LIKE',
    ++        VALUE           => $args{Search},
    ++        SUBCLAUSE       => 'Attachments',
    ++    ) unless ! $args{Headers};
    ++
    ++    $self->Limit(
    ++        ENTRYAGGREGATOR => 'OR',
    ++        FIELD           => 'Content',
    ++        OPERATOR        => 'LIKE',
    ++        VALUE           => $args{Search},
    ++        SUBCLAUSE       => 'Attachments',
    ++    ) unless ! $args{Content};
    ++
    ++    while (my $attachment = $self->Next) {
    ++        if ( $args{Headers} ) {
    ++            my ($ret, $msg) = $attachment->ReplaceHeaders(Search => $args{Search}, Replacement => $args{Replacement});
    ++            return ($ret, $msg) unless $ret;
    ++        }
    ++        if ( $args{Content} ) {
    ++            my ($ret, $msg) = $attachment->ReplaceContent(Search => $args{Search}, Replacement => $args{Replacement});
    ++            return ($ret, $msg) unless $ret;
    ++        }
    ++    }
    ++    return (1, "No instances of $args{Search} where found in the attachments table") unless $self->Count;
    ++    return (1, "$args{Search} has been replaced with \"$args{Replacement}\"");
    ++}
    ++
    + RT::Base->_ImportOverlays();
    + 
    + 1;
3: df998852a < -:  ------- Add method ReplaceAttachments to Attachments.pm



More information about the rt-commit mailing list