01 <?php
02     include "./lib/commons.php";
03 
04     function AdjustCommentsSub($CommentOst) {
05         global $_VARIABLE;
06 
07         if(!$result = mysql_query("SELECT comment_id, comment_created_on, comment_parent_id FROM mt_comment ORDER BY comment_created_on")) {
08             die("mysql_query(ERROR): " . mysql_error());
09         }
10 
11         $CommentCnt = 0;
12         while($line = mysql_fetch_row($result)) {
13             $CommentArray{$CommentCnt}{'comment_id'} = $line[0];
14             $CommentArray{$CommentCnt}{'comment_created_on'} = $line[1];
15             $CommentArray{$CommentCnt}{'comment_parent_id'} = $line[2];
16             $CommentCnt++;
17         }
18 
19         for($i = 0; $i < $CommentCnt; $i++) {
20             $query = "UPDATE mt_comment SET comment_id = '" . ($CommentOst + $i) . "' WHERE mt_comment.comment_id = '" . $CommentArray{$i}{'comment_id'} . "'";
21             if(!mysql_query($query)) {
22                 die("mysql_query(ERROR): " . mysql_error());
23             }
24             $query = "UPDATE mt_comment SET comment_parent_id = '" . ($CommentOst + $i) . "' WHERE mt_comment.comment_parent_id = '" . $CommentArray{$i}{'comment_id'} . "'";
25             if(!mysql_query($query)) {
26                 die("mysql_query(ERROR): " . mysql_error());
27             }
28         }
29 
30         return $CommentCnt;
31     }
32 
33     function AdjustComments() {
34         global $_VARIABLE;
35 
36         // Sort by `comment_created_on`
37         if(!mysql_query("ALTER TABLE mt_comment ORDER BY comment_created_on")) {
38             die("mysql_query(ERROR): " . mysql_error());
39         }
40 
41         // Get 'Auto_increment' (Offset)
42         if(!$result = mysql_query("SHOW TABLE STATUS WHERE NAME = 'mt_comment'")) {
43             die("mysql_query(ERROR): " . mysql_error());
44         }
45         while($line = mysql_fetch_assoc($result)) {
46             $CommentOst = $line{'Auto_increment'};
47         }
48 
49         // Pass 1
50         $CommentCnt = AdjustCommentsSub($CommentOst);
51 
52         // Pass 2
53         $CommentCnt = AdjustCommentsSub(1);
54 
55         // Set 'Auto_increment'
56         $query = "ALTER TABLE mt_comment AUTO_INCREMENT = " . ($CommentCnt + 1);
57         if(!mysql_query($query)) {
58             die("mysql_query(ERROR): " . mysql_error());
59         }
60 
61         return $CommentCnt;
62     }
63 
64     // Main
65 
66     $time0 = microtime(TRUE);
67 
68         GetDBSettings();
69 
70         if(!$link = mysql_connect($_VARIABLE{'DBHost'}, $_VARIABLE{'DBUser'}, $_VARIABLE{'DBPassword'})) {
71             die("mysql_connect(ERROR): " . mysql_error());
72         }
73         if(!mysql_select_db($_VARIABLE{'Database'})) {
74             die("mysql_select_db(ERROR): " . mysql_error());
75         }
76 
77         $CommentCnt = AdjustComments();
78         printf("<div>Adjusted %d Comments.</div>\n", $CommentCnt);
79 
80         mysql_close($link);
81 
82     $time1 = microtime(TRUE);
83 
84     printf("<div>%.3f sec.</div>\n", $time1 - $time0);
85 ?>