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 AdjustEntriesSub($EntryOst) {
064 
065         $query = "SELECT entry_id, entry_atom_id FROM mt_entry ORDER BY entry_authored_on";
066         if(!$result = mysql_query($query)) {
067             die("mysql_query(ERROR): " . mysql_error());
068         }
069 
070         $EntryCnt = 0;
071         while($line = mysql_fetch_row($result)) {
072             $EntryArray[$EntryCnt]{'entry_id'} = $line[0];
073             $EntryArray[$EntryCnt]{'entry_atom_id'} = $line[1];
074             $EntryCnt++;
075         }
076 
077         for($i = 0; $i < $EntryCnt; $i++) {
078           // mt_entry . entry_id
079             UpdateTableRow("mt_entry", "entry_id", $EntryArray[$i]{'entry_id'}, ($EntryOst + $i));
080 
081           // mt_entry . entry_atom_id
082                 $pattern = sprintf("/(\S+)\.%d$/", $EntryArray[$i]{'entry_id'});
083                 $replacement = sprintf("$1.%d", ($EntryOst + $i));
084                 $string = preg_replace($pattern, $replacement, $EntryArray[$i]{'entry_atom_id'});
085             UpdateTableRow("mt_entry", "entry_atom_id", $EntryArray[$i]{'entry_atom_id'}, $string);
086 
087           // mt_entry_meta
088             UpdateTableRow("mt_entry_meta", "entry_meta_entry_id", $EntryArray[$i]{'entry_id'}, ($EntryOst + $i));
089           // mt_entry_rev
090             UpdateTableRow("mt_entry_rev", "entry_rev_entry_id", $EntryArray[$i]{'entry_id'}, ($EntryOst + $i));
091           // mt_comment
092             UpdateTableRow("mt_comment", "comment_entry_id", $EntryArray[$i]{'entry_id'}, ($EntryOst + $i));
093           // mt_log
094             UpdateTableRow("mt_log", "log_metadata", $EntryArray[$i]{'entry_id'}, ($EntryOst + $i));
095           // mt_objectasset
096             UpdateTableRow("mt_objectasset", "objectasset_object_id", $EntryArray[$i]{'entry_id'}, ($EntryOst + $i));
097           //  mt_placement
098             UpdateTableRow("mt_placement", "placement_entry_id", $EntryArray[$i]{'entry_id'}, ($EntryOst + $i));
099           //  mt_trackback
100             UpdateTableRow("mt_trackback", "trackback_entry_id", $EntryArray[$i]{'entry_id'}, ($EntryOst + $i));
101         }
102 
103         return $EntryCnt;
104     }
105 
106     function AdjustEntries() {
107 
108         SortTableOrder("mt_entry", "entry_authored_on");
109 
110         $EntryOst = GetTableIncrement("mt_entry");
111 
112         // Pass 1
113         $EntryCnt = AdjustEntriesSub($EntryOst);
114 
115         // Pass 2
116         $EntryCnt = AdjustEntriesSub(1);
117 
118         SetTableIncrement("mt_entry", ($EntryCnt + 1));
119 
120         return $EntryCnt;
121     }
122 
123 
124     function AdjustCommentsSub($CommentOst) {
125 
126         $CommentArray = GetTableRowOrder("mt_comment", "comment_id", "comment_created_on");
127 
128         $CommentCnt = count($CommentArray);
129 
130         for($i = 0; $i < $CommentCnt; $i++) {
131             UpdateTableRow("mt_comment", "comment_id", $CommentArray[$i], ($CommentOst + $i));
132             UpdateTableRow("mt_comment", "comment_parent_id", $CommentArray[$i], ($CommentOst + $i));
133         }
134 
135         return $CommentCnt;
136     }
137 
138     function AdjustComments() {
139 
140         SortTableOrder("mt_comment", "comment_created_on");
141 
142         $CommentOst = GetTableIncrement("mt_comment");
143 
144         // Pass 1
145         $CommentCnt = AdjustCommentsSub($CommentOst);
146 
147         // Pass 2
148         $CommentCnt = AdjustCommentsSub(1);
149 
150         SetTableIncrement("mt_comment", ($CommentCnt + 1));
151 
152         return $CommentCnt;
153     }
154 
155 
156     function AdjustTrackbacksSub($TrackbackOst) {
157 
158         $TrackbackArray = GetTableRowOrder("mt_trackback", "trackback_id", "trackback_entry_id");
159 
160         $TrackbackCnt = count($TrackbackArray);
161 
162         for($i = 0; $i < $TrackbackCnt; $i++) {
163             UpdateTableRow("mt_trackback", "trackback_id", $TrackbackArray[$i], ($TrackbackOst + $i));
164             UpdateTableRow("mt_tbping", "tbping_tb_id", $TrackbackArray[$i], ($TrackbackOst + $i));
165         }
166 
167         return $TrackbackCnt;
168     }
169 
170     function AdjustTrackbacks() {
171 
172         SortTableOrder("mt_trackback", "trackback_entry_id");
173 
174         $TrackbackOst = GetTableIncrement("mt_trackback");
175 
176         // Pass 1
177         $TrackbackCnt = AdjustTrackbacksSub($TrackbackOst);
178 
179         // Pass 2
180         $TrackbackCnt = AdjustTrackbacksSub(1);
181 
182         SetTableIncrement("mt_trackback", ($TrackbackCnt + 1));
183 
184         return $TrackbackCnt;
185     }
186 
187 
188     function AdjustTBPingsSub($TBPingOst) {
189 
190         $TBPingArray = GetTableRowOrder("mt_tbping", "tbping_id", "tbping_created_on");
191 
192         $TBPingCnt = count($TBPingArray);
193 
194         for($i = 0; $i < $TBPingCnt; $i++) {
195             UpdateTableRow("mt_tbping", "tbping_id", $TBPingArray[$i], ($TBPingOst + $i));
196         }
197 
198         return $TBPingCnt;
199     }
200 
201     function AdjustTBPings() {
202 
203         SortTableOrder("mt_tbping", "tbping_created_on");
204 
205         $TBPingOst = GetTableIncrement("mt_tbping");
206 
207         // Pass 1
208         $TBPingCnt = AdjustTBPingsSub($TBPingOst);
209 
210         // Pass 2
211         $TBPingCnt = AdjustTBPingsSub(1);
212 
213         SetTableIncrement("mt_tbping", ($TBPingCnt + 1));
214 
215         return $TBPingCnt;
216     }
217 
218 
219     // Main
220 
221     GetDBSettings();
222 
223     if(!$link = mysql_connect($_VARIABLE{'DBHost'}, $_VARIABLE{'DBUser'}, $_VARIABLE{'DBPassword'})) {
224         die("mysql_connect(ERROR): " . mysql_error());
225     }
226     if(!mysql_select_db($_VARIABLE{'Database'})) {
227         die("mysql_select_db(ERROR): " . mysql_error());
228     }
229 
230         $time0 = microtime(TRUE);
231     $EntryCnt = AdjustEntries();
232         $time1 = microtime(TRUE);
233         printf("<div>Adjusted %d Entries. %.3f sec.</div>\n", $EntryCnt, ($time1 - $time0));
234 
235         $time0 = microtime(TRUE);
236     $CommentCnt = AdjustComments();
237         $time1 = microtime(TRUE);
238         printf("<div>Adjusted %d Comments. %.3f sec.</div>\n", $CommentCnt, ($time1 - $time0));
239 
240         $time0 = microtime(TRUE);
241     $TrackbackCnt = AdjustTrackbacks();
242         $time1 = microtime(TRUE);
243         printf("<div>Adjusted %d Trackbacks. %.3f sec.</div>\n", $TrackbackCnt, ($time1 - $time0));
244 
245         $time0 = microtime(TRUE);
246     $TBPingCnt = AdjustTBPings();
247         $time1 = microtime(TRUE);
248         printf("<div>Adjusted %d TBPings. %.3f sec.</div>\n", $TBPingCnt, ($time1 - $time0));
249 
250     mysql_close($link);
251 ?>