root/mt-db-convert/trunk/mt-tbfixer.cgi

Revision 173, 2.3 kB (checked in by ogawa, 3 years ago)

Little more fix for mt-tbfixer.cgi

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Rev Id
Line 
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
10use strict;
11use lib 'lib';
12use MT::Bootstrap;
13
14package TBFixer;
15use MT::App;
16@TBFixer::ISA = qw( MT::App );
17use MT::Trackback;
18use MT::TBPing;
19
20sub 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
29sub 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
78package main;
79my $app = TBFixer->new;
80$app->run;
81
821;
Note: See TracBrowser for help on using the browser.