various changes aimed at making a bare-bones 1.3 command line installer, for wikimedia
[lhc/web/wiklou.git] / maintenance / convertLinks.inc
1 <?php
2
3 function convertLinks() {
4 global $wgLang, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
5
6 $numRows = $tuplesAdded = $numBadLinks = $curRowsRead = 0; #counters etc
7 $totalTuplesInserted = 0; # total tuples INSERTed into links_temp
8
9 $reportCurReadProgress = true; #whether or not to give progress reports while reading IDs from cur table
10 $curReadReportInterval = 1000; #number of rows between progress reports
11
12 $reportLinksConvProgress = true; #whether or not to give progress reports during conversion
13 $linksConvInsertInterval = 1000; #number of rows per INSERT
14
15 $initialRowOffset = 0;
16 #$finalRowOffset = 0; # not used yet; highest row number from links table to process
17
18 # Overwrite the old links table with the new one. If this is set to false,
19 # the new table will be left at links_temp.
20 $overwriteLinksTable = true;
21
22 # Don't create keys, and so allow duplicates in the new links table.
23 # This gives a huge speed improvement for very large links tables which are MyISAM. (What about InnoDB?)
24 $noKeys = false;
25
26
27 $logPerformance = false; # output performance data to a file
28 $perfLogFilename = "convLinksPerf.txt";
29 #--------------------------------------------------------------------
30
31 $res = wfQuery( "SELECT COUNT(*) AS count FROM links", DB_WRITE );
32 $row = wfFetchObject($res);
33 $numRows = $row->count;
34 wfFreeResult( $res );
35
36 if ( $numRows == 0 ) {
37 print "No rows to convert. Updating schema...\n";
38 createTempTable();
39 } else {
40 $row = wfFetchObject( $res );
41 if ( is_numeric( $row->l_from ) ) {
42 print "Schema already converted\n";
43 exit;
44 }
45
46 if ( $logPerformance ) { $fh = fopen ( $perfLogFilename, "w" ); }
47 $baseTime = $startTime = getMicroTime();
48 # Create a title -> cur_id map
49 print "Loading IDs from cur table...\n";
50 performanceLog ( "Reading $numRows rows from cur table...\n" );
51 performanceLog ( "rows read vs seconds elapsed:\n" );
52 wfBufferSQLResults( false );
53 $res = wfQuery( "SELECT cur_namespace,cur_title,cur_id FROM cur", DB_WRITE );
54 $ids = array();
55
56 while ( $row = wfFetchObject( $res ) ) {
57 $title = $row->cur_title;
58 if ( $row->cur_namespace ) {
59 $title = $wgLang->getNsText( $row->cur_namespace ) . ":$title";
60 }
61 $ids[$title] = $row->cur_id;
62 $curRowsRead++;
63 if ($reportCurReadProgress) {
64 if (($curRowsRead % $curReadReportInterval) == 0) {
65 performanceLog( $curRowsRead . " " . (getMicroTime() - $baseTime) . "\n" );
66 print "\t$curRowsRead rows of cur table read.\n";
67 }
68 }
69 }
70 wfFreeResult( $res );
71 wfBufferSQLResults( true );
72 print "Finished loading IDs.\n\n";
73 performanceLog( "Took " . (getMicroTime() - $baseTime) . " seconds to load IDs.\n\n" );
74 #--------------------------------------------------------------------
75
76 # Now, step through the links table (in chunks of $linksConvInsertInterval rows),
77 # convert, and write to the new table.
78 createTempTable();
79 performanceLog( "Resetting timer.\n\n" );
80 $baseTime = getMicroTime();
81 print "Processing $numRows rows from links table...\n";
82 performanceLog( "Processing $numRows rows from links table...\n" );
83 performanceLog( "rows inserted vs seconds elapsed:\n" );
84
85 for ($rowOffset = $initialRowOffset; $rowOffset < $numRows; $rowOffset += $linksConvInsertInterval) {
86 $sqlRead = "SELECT * FROM links LIMIT $linksConvInsertInterval OFFSET $rowOffset";
87 $res = wfQuery($sqlRead, DB_READ);
88 if ( $noKeys ) {
89 $sqlWrite = array("INSERT INTO links_temp(l_from,l_to) VALUES ");
90 } else {
91 $sqlWrite = array("INSERT IGNORE INTO links_temp(l_from,l_to) VALUES ");
92 }
93
94 $tuplesAdded = 0; # no tuples added to INSERT yet
95 while ( $row = wfFetchObject($res) ) {
96 $fromTitle = $row->l_from;
97 if ( array_key_exists( $fromTitle, $ids ) ) { # valid title
98 $from = $ids[$fromTitle];
99 $to = $row->l_to;
100 if ( $tuplesAdded != 0 ) {
101 $sqlWrite[] = ",";
102 }
103 $sqlWrite[] = "($from,$to)";
104 $tuplesAdded++;
105 } else { # invalid title
106 $numBadLinks++;
107 }
108 }
109 wfFreeResult($res);
110 #print "rowOffset: $rowOffset\ttuplesAdded: $tuplesAdded\tnumBadLinks: $numBadLinks\n";
111 if ( $tuplesAdded != 0 ) {
112 if ($reportLinksConvProgress) {
113 print "Inserting $tuplesAdded tuples into links_temp...";
114 }
115 wfQuery( implode("",$sqlWrite) , DB_WRITE );
116 $totalTuplesInserted += $tuplesAdded;
117 if ($reportLinksConvProgress)
118 print " done. Total $totalTuplesInserted tuples inserted.\n";
119 performanceLog( $totalTuplesInserted . " " . (getMicroTime() - $baseTime) . "\n" );
120 }
121 }
122 print "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n\n";
123 performanceLog( "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n" );
124 performanceLog( "Total execution time: " . (getMicroTime() - $startTime) . " seconds.\n" );
125 if ( $logPerformance ) { fclose ( $fh ); }
126 }
127 #--------------------------------------------------------------------
128
129 if ( $overwriteLinksTable ) {
130 $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
131 if (!($dbConn->isOpen())) {
132 print "Opening connection to database failed.\n";
133 exit;
134 }
135 # Check for existing links_backup, and delete it if it exists.
136 print "Dropping backup links table if it exists...";
137 $dbConn->query( "DROP TABLE IF EXISTS links_backup", DB_WRITE);
138 print " done.\n";
139
140 # Swap in the new table, and move old links table to links_backup
141 print "Swapping tables 'links' to 'links_backup'; 'links_temp' to 'links'...";
142 $dbConn->query( "RENAME TABLE links TO links_backup, links_temp TO links", DB_WRITE );
143 print " done.\n\n";
144
145 $dbConn->close();
146 print "Conversion complete. The old table remains at links_backup;\n";
147 print "delete at your leisure.\n";
148 } else {
149 print "Conversion complete. The converted table is at links_temp;\n";
150 print "the original links table is unchanged.\n";
151 }
152
153 #--------------------------------------------------------------------
154
155 function createTempTable() {
156 global $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname;
157 global $noKeys;
158 $dbConn = Database::newFromParams( $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname );
159
160 if (!($dbConn->isOpen())) {
161 print "Opening connection to database failed.\n";
162 exit;
163 }
164
165 print "Dropping temporary links table if it exists...";
166 $dbConn->query( "DROP TABLE IF EXISTS links_temp", DB_WRITE);
167 print " done.\n";
168
169 print "Creating temporary links table...";
170 if ( $noKeys ) {
171 $dbConn->query( "CREATE TABLE links_temp ( " .
172 "l_from int(8) unsigned NOT NULL default '0', " .
173 "l_to int(8) unsigned NOT NULL default '0')", DB_WRITE);
174 } else {
175 $dbConn->query( "CREATE TABLE links_temp ( " .
176 "l_from int(8) unsigned NOT NULL default '0', " .
177 "l_to int(8) unsigned NOT NULL default '0', " .
178 "UNIQUE KEY l_from(l_from,l_to), " .
179 "KEY (l_to))", DB_WRITE);
180 }
181 print " done.\n\n";
182 }
183
184 function performanceLog( $text ) {
185 global $logPerformance, $fh;
186 if ( $logPerformance ) {
187 fwrite( $fh, $text );
188 }
189 }
190
191 function getMicroTime() { # return time in seconds, with microsecond accuracy
192 list($usec, $sec) = explode(" ", microtime());
193 return ((float)$usec + (float)$sec);
194 }
195
196
197
198 ?>