| 1 | #!/usr/bin/perl -w |
|---|
| 2 | # mt-tbfixer.cgi |
|---|
| 3 | # This script allows you to remove duplicated MT::Trackback objects, |
|---|
| 4 | # which can be often caused by mt-db2sql.cgi and previous versions of |
|---|
| 5 | # mt-db-convert.cgi. And it also maintains MT::TBPing objects to |
|---|
| 6 | # associate with the proper MT::Trackback object consistently. |
|---|
| 7 | # |
|---|
| 8 | # $Id$ |
|---|
| 9 | |
|---|
| 10 | use strict; |
|---|
| 11 | use lib 'lib'; |
|---|
| 12 | use MT::Bootstrap; |
|---|
| 13 | |
|---|
| 14 | package TBFixer; |
|---|
| 15 | use MT::App; |
|---|
| 16 | @TBFixer::ISA = qw( MT::App ); |
|---|
| 17 | use MT::Trackback; |
|---|
| 18 | use MT::TBPing; |
|---|
| 19 | |
|---|
| 20 | sub init { |
|---|
| 21 | my $app = shift; |
|---|
| 22 | $app->SUPER::init(@_) or return; |
|---|
| 23 | $app->add_methods(check => \&fixer, fix => \&fixer); |
|---|
| 24 | $app->{charset} = $app->{cfg}->PublishCharset; |
|---|
| 25 | $app->{default_mode} = 'check'; |
|---|
| 26 | $app; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | sub fixer { |
|---|
| 30 | my $app = shift; |
|---|
| 31 | my $mode = $app->mode; |
|---|
| 32 | my $html = '<html><body><pre>'; |
|---|
| 33 | |
|---|
| 34 | my $conflicted_tb = 0; |
|---|
| 35 | my $conflicted_tbping = 0; |
|---|
| 36 | my @trackbacks = MT::Trackback->load(undef, { sort => 'id', direction => 'descend' }); |
|---|
| 37 | my %tbs; |
|---|
| 38 | for my $tb (@trackbacks) { |
|---|
| 39 | my $eid = $tb->entry_id or next; |
|---|
| 40 | unless (defined $tbs{$eid} && $tbs{$eid}->isa('MT::Trackback')) { |
|---|
| 41 | $tbs{$eid} = $tb; |
|---|
| 42 | next; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | my $tb_id = $tbs{$eid}->id; |
|---|
| 46 | $html .= "MT::Trackback(id=" . $tb->id . ") conflicts with (id=" . $tb_id . ") for entry " . $eid . "\n"; |
|---|
| 47 | $conflicted_tb++; |
|---|
| 48 | |
|---|
| 49 | my @pings = MT::TBPing->load({ tb_id => $tb->id }); |
|---|
| 50 | for my $ping (@pings) { |
|---|
| 51 | if ($mode eq 'fix') { |
|---|
| 52 | $ping->tb_id($tb_id); |
|---|
| 53 | $ping->save or die $ping->errstr; |
|---|
| 54 | } |
|---|
| 55 | $html .= " MT::TBPing(id=" . $ping->id . ") " . ($mode eq 'fix' ? 'is' : 'can be') . " moved from MT::Trackback(id=" . $tb->id . ") to (id=" . $tb_id . ")\n"; |
|---|
| 56 | $conflicted_tbping++; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | $html .= " MT::Trackback(id=" . $tb->id . ") " . ($mode eq 'fix' ? 'is' : 'can be') . " removed\n"; |
|---|
| 60 | |
|---|
| 61 | if ($mode eq 'fix') { |
|---|
| 62 | $tb->remove or die $tb->errstr; |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | $html .= "\n"; |
|---|
| 66 | if ($mode eq 'fix') { |
|---|
| 67 | $html .= "$conflicted_tb conflicts are found and fixed in MT::Trackback!\n"; |
|---|
| 68 | $html .= "$conflicted_tbping conflicts are found and fixed in MT::TBPing!\n"; |
|---|
| 69 | $html .= "\nYou should perform rebuilding for all MT contents.\n" |
|---|
| 70 | if $conflicted_tb || $conflicted_tbping; |
|---|
| 71 | } else { |
|---|
| 72 | $html .= "$conflicted_tb conflicts are found in MT::Trackback!\n"; |
|---|
| 73 | $html .= "$conflicted_tbping conflicts are found in MT::TBPing!\n"; |
|---|
| 74 | } |
|---|
| 75 | $html . '</pre></body></html>'; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | package main; |
|---|
| 79 | my $app = TBFixer->new; |
|---|
| 80 | $app->run; |
|---|
| 81 | |
|---|
| 82 | 1; |
|---|