001 <?php
002     include "./lib/commons.php";
003 
004 
005     function SortTableOrder($Table, $Order) {
006 
007         $query = "ALTER TABLE $Table ORDER BY $Order";
008         if(!mysql_query($query)) {
009             die("mysql_query(ERROR): " . mysql_error());
010         }
011 
012         return;
013     }
014 
015     function GetTableIncrement($Table) {
016 
017         $query = "SHOW TABLE STATUS WHERE NAME = '$Table'";
018         if(!$result = mysql_query($query)) {
019             die("mysql_query(ERROR): " . mysql_error());
020         }
021         while($line = mysql_fetch_assoc($result)) {
022             $Increment = $line{'Auto_increment'};
023         }
024 
025         return $Increment;
026     }
027 
028     function SetTableIncrement($Table, $Increment) {
029 
030         $query = sprintf("ALTER TABLE %s AUTO_INCREMENT = %d", $Table, $Increment);
031         if(!mysql_query($query)) {
032             die("mysql_query(ERROR): " . mysql_error());
033         }
034 
035         return;
036     }
037 
038     function GetTableRowOrder($Table, $Row, $Order) {
039 
040         $query = "SELECT $Row FROM $Table ORDER BY $Order";
041         if(!$result = mysql_query($query)) {
042             die("mysql_query(ERROR): " . mysql_error());
043         }
044 
045         while($line = mysql_fetch_row($result)) {
046             $TableArray[] = $line[0];
047         }
048 
049         return $TableArray;
050     }
051 
052     function UpdateTableRow($Table, $Row, $Old, $New) {
053 
054         $query = "UPDATE $Table SET $Row = '$New' WHERE $Table.$Row = '$Old'";
055         if(!mysql_query($query)) {
056             die("mysql_query(ERROR): " . mysql_error());
057         }
058 
059         return;
060     }
061 
062 
063     function AdjustCommentsSub($CommentOst) {
064 
065         $CommentArray = GetTableRowOrder("mt_comment", "comment_id", "comment_created_on");
066         $CommentCnt = count($CommentArray);
067 
068         for($i = 0; $i < $CommentCnt; $i++) {
069             UpdateTableRow("mt_comment", "comment_id", $CommentArray[$i], ($CommentOst + $i));
070             UpdateTableRow("mt_comment", "comment_parent_id", $CommentArray[$i], ($CommentOst + $i));
071         }
072 
073         return $CommentCnt;
074     }
075 
076     function AdjustComments() {
077 
078         SortTableOrder("mt_comment", "comment_created_on");
079 
080         $CommentOst = GetTableIncrement("mt_comment");
081 
082         // Pass 1
083         $CommentCnt = AdjustCommentsSub($CommentOst);
084 
085         // Pass 2
086         $CommentCnt = AdjustCommentsSub(1);
087 
088         SetTableIncrement("mt_comment", $CommentCnt + 1);
089 
090         return $CommentCnt;
091     }
092 
093 
094     function AdjustTrackbacksSub($TrackbackOst) {
095 
096         $TrackbackArray = GetTableRowOrder("mt_trackback", "trackback_id", "trackback_entry_id");
097         $TrackbackCnt = count($TrackbackArray);
098 
099         for($i = 0; $i < $TrackbackCnt; $i++) {
100             UpdateTableRow("mt_trackback", "trackback_id", $TrackbackArray[$i], ($TrackbackOst + $i));
101             UpdateTableRow("mt_tbping", "tbping_tb_id", $TrackbackArray[$i], ($TrackbackOst + $i));
102         }
103 
104         return $TrackbackCnt;
105     }
106 
107     function AdjustTrackbacks() {
108 
109         SortTableOrder("mt_trackback", "trackback_entry_id");
110 
111         $TrackbackOst = GetTableIncrement("mt_trackback");
112 
113         // Pass 1
114         $TrackbackCnt = AdjustTrackbacksSub($TrackbackOst);
115 
116         // Pass 2
117         $TrackbackCnt = AdjustTrackbacksSub(1);
118 
119         SetTableIncrement("mt_trackback", $TrackbackCnt + 1);
120 
121         return $TrackbackCnt;
122     }
123 
124 
125     function AdjustTBPingsSub($TBPingOst) {
126 
127         $TBPingArray = GetTableRowOrder("mt_tbping", "tbping_id", "tbping_created_on");
128         $TBPingCnt = count($TBPingArray);
129 
130         for($i = 0; $i < $TBPingCnt; $i++) {
131             UpdateTableRow("mt_tbping", "tbping_id", $TBPingArray[$i], ($TBPingOst + $i));
132         }
133 
134         return $TBPingCnt;
135     }
136 
137     function AdjustTBPings() {
138 
139         SortTableOrder("mt_tbping", "tbping_created_on");
140 
141         $TBPingOst = GetTableIncrement("mt_tbping");
142 
143         // Pass 1
144         $TBPingCnt = AdjustTBPingsSub($TBPingOst);
145 
146         // Pass 2
147         $TBPingCnt = AdjustTBPingsSub(1);
148 
149         SetTableIncrement("mt_tbping", $TBPingCnt + 1);
150 
151         return $TBPingCnt;
152     }
153 
154 
155     // Main
156 
157     $time0 = microtime(TRUE);
158 
159         GetDBSettings();
160 
161         if(!$link = mysql_connect($_VARIABLE{'DBHost'}, $_VARIABLE{'DBUser'}, $_VARIABLE{'DBPassword'})) {
162             die("mysql_connect(ERROR): " . mysql_error());
163         }
164         if(!mysql_select_db($_VARIABLE{'Database'})) {
165             die("mysql_select_db(ERROR): " . mysql_error());
166         }
167 
168         $CommentCnt = AdjustComments();
169         printf("<div>Adjusted %d Comments.</div>\n", $CommentCnt);
170 
171         $TrackbackCnt = AdjustTrackbacks();
172         printf("<div>Adjusted %d Trackbacks.</div>\n", $TrackbackCnt);
173 
174         $TBPingCnt = AdjustTBPings();
175         printf("<div>Adjusted %d TBPings.</div>\n", $TBPingCnt);
176 
177         mysql_close($link);
178 
179     $time1 = microtime(TRUE);
180 
181     printf("<div>%.3f sec.</div>\n", $time1 - $time0);
182 ?>