24 require_once __DIR__ .
'/Maintenance.php';
38 parent::__construct();
39 $this->mDescription =
"Convert from the old links schema (string->ID) to the new schema (ID->ID).
40 The wiki should be put into read-only mode while this script executes";
42 $this->
addArg(
'logperformance',
"Log performance to perfLogFilename.",
false );
43 $this->
addArg(
'perfLogFilename',
"Filename where performance is logged if --logperformance was set (defaults to 'convLinksPerf.txt').",
false );
44 $this->
addArg(
'keep-links-table',
"Don't overwrite the old links table with the new one, leave the new table at links_temp.",
false );
45 $this->
addArg(
'nokeys',
"Don't create keys, and so allow duplicates in the new links table.\n
46 This gives a huge speed improvement for very large links tables which are MyISAM." ,
false );
56 $type = $dbw->getType();
57 if (
$type !=
'mysql' ) {
58 $this->
output(
"Link table conversion not necessary for $type\n" );
64 $numBadLinks = $curRowsRead = 0; # counters
etc
65 $totalTuplesInserted = 0; # total tuples INSERTed into links_temp
67 $reportCurReadProgress =
true; # whether or not to give progress reports
while reading IDs
from cur
table
68 $curReadReportInterval = 1000; # number
of rows between progress reports
70 $reportLinksConvProgress =
true; # whether or not to give progress reports during
conversion
71 $linksConvInsertInterval = 1000; # number
of rows per INSERT
73 $initialRowOffset = 0;
74 # $finalRowOffset = 0; # not used yet; highest row number from links table to process
76 $overwriteLinksTable = !$this->
hasOption(
'keep-links-table' );
78 $this->logPerformance = $this->
hasOption(
'logperformance' );
79 $perfLogFilename = $this->
getArg(
'perfLogFilename',
"convLinksPerf.txt" );
81 # --------------------------------------------------------------------
83 list( $cur, $links, $links_temp, $links_backup ) = $dbw->tableNamesN(
'cur',
'links',
'links_temp',
'links_backup' );
85 if ( $dbw->tableExists(
'pagelinks' ) ) {
86 $this->
output(
"...have pagelinks; skipping old links table updates\n" );
90 $res = $dbw->query(
"SELECT l_from FROM $links LIMIT 1" );
91 if ( $dbw->fieldType(
$res, 0 ) ==
"int" ) {
92 $this->
output(
"Schema already converted\n" );
96 $res = $dbw->query(
"SELECT COUNT(*) AS count FROM $links" );
97 $row = $dbw->fetchObject(
$res );
98 $numRows = $row->count;
99 $dbw->freeResult(
$res );
101 if ( $numRows == 0 ) {
102 $this->
output(
"Updating schema (no rows to convert)...\n" );
106 if ( $this->logPerformance ) {
107 $fh = fopen ( $perfLogFilename,
"w" );
109 $this->
error(
"Couldn't open $perfLogFilename" );
110 $this->logPerformance =
false;
114 # Create a title -> cur_id map
115 $this->
output(
"Loading IDs from $cur table...\n" );
116 $this->
performanceLog ( $fh,
"Reading $numRows rows from cur table...\n" );
119 $dbw->bufferResults(
false );
120 $res = $dbw->query(
"SELECT cur_namespace,cur_title,cur_id FROM $cur" );
123 foreach (
$res as $row ) {
125 if ( $row->cur_namespace ) {
128 $ids[
$title] = $row->cur_id;
130 if ( $reportCurReadProgress ) {
131 if ( ( $curRowsRead % $curReadReportInterval ) == 0 ) {
133 $this->
output(
"\t$curRowsRead rows of $cur table read.\n" );
137 $dbw->freeResult(
$res );
138 $dbw->bufferResults(
true );
139 $this->
output(
"Finished loading IDs.\n\n" );
142 # --------------------------------------------------------------------
144 # Now, step through the links table (in chunks of $linksConvInsertInterval rows),
145 # convert, and write to the new table.
149 $this->
output(
"Processing $numRows rows from $links table...\n" );
150 $this->
performanceLog( $fh,
"Processing $numRows rows from $links table...\n" );
151 $this->
performanceLog( $fh,
"rows inserted vs seconds elapsed:\n" );
153 for ( $rowOffset = $initialRowOffset; $rowOffset < $numRows; $rowOffset += $linksConvInsertInterval ) {
154 $sqlRead =
"SELECT * FROM $links ";
155 $sqlRead = $dbw->limitResult( $sqlRead, $linksConvInsertInterval, $rowOffset );
156 $res = $dbw->query( $sqlRead );
158 $sqlWrite =
array(
"INSERT INTO $links_temp (l_from,l_to) VALUES " );
160 $sqlWrite =
array(
"INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES " );
163 $tuplesAdded = 0; # no tuples added to INSERT yet
164 foreach (
$res as $row ) {
165 $fromTitle = $row->l_from;
166 if ( array_key_exists( $fromTitle, $ids ) ) { # valid
title
167 $from = $ids[$fromTitle];
169 if ( $tuplesAdded != 0 ) {
172 $sqlWrite[] =
"($from,$to)";
174 }
else { # invalid
title
178 $dbw->freeResult(
$res );
179 # $this->output( "rowOffset: $rowOffset\ttuplesAdded: $tuplesAdded\tnumBadLinks: $numBadLinks\n" );
180 if ( $tuplesAdded != 0 ) {
181 if ( $reportLinksConvProgress ) {
182 $this->
output(
"Inserting $tuplesAdded tuples into $links_temp..." );
184 $dbw->query( implode(
"", $sqlWrite ) );
185 $totalTuplesInserted += $tuplesAdded;
186 if ( $reportLinksConvProgress ) {
187 $this->
output(
" done. Total $totalTuplesInserted tuples inserted.\n" );
192 $this->
output(
"$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n\n" );
193 $this->
performanceLog( $fh,
"$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n" );
195 if ( $this->logPerformance ) {
199 # --------------------------------------------------------------------
201 if ( $overwriteLinksTable ) {
202 # Check for existing links_backup, and delete it if it exists.
203 $this->
output(
"Dropping backup links table if it exists..." );
204 $dbw->query(
"DROP TABLE IF EXISTS $links_backup", __METHOD__ );
205 $this->
output(
" done.\n" );
207 # Swap in the new table, and move old links table to links_backup
208 $this->
output(
"Swapping tables '$links' to '$links_backup'; '$links_temp' to '$links'..." );
209 $dbw->query(
"RENAME TABLE links TO $links_backup, $links_temp TO $links", __METHOD__ );
210 $this->
output(
" done.\n\n" );
212 $this->
output(
"Conversion complete. The old table remains at $links_backup;\n" );
213 $this->
output(
"delete at your leisure.\n" );
215 $this->
output(
"Conversion complete. The converted table is at $links_temp;\n" );
216 $this->
output(
"the original links table is unchanged.\n" );
223 if ( !( $dbConn->isOpen() ) ) {
224 $this->
output(
"Opening connection to database failed.\n" );
227 $links_temp = $dbConn->tableName(
'links_temp' );
229 $this->
output(
"Dropping temporary links table if it exists..." );
230 $dbConn->query(
"DROP TABLE IF EXISTS $links_temp" );
231 $this->
output(
" done.\n" );
233 $this->
output(
"Creating temporary links table..." );
235 $dbConn->query(
"CREATE TABLE $links_temp ( " .
236 "l_from int(8) unsigned NOT NULL default '0', " .
237 "l_to int(8) unsigned NOT NULL default '0')" );
239 $dbConn->query(
"CREATE TABLE $links_temp ( " .
240 "l_from int(8) unsigned NOT NULL default '0', " .
241 "l_to int(8) unsigned NOT NULL default '0', " .
242 "UNIQUE KEY l_from(l_from,l_to), " .
245 $this->
output(
" done.\n\n" );
249 if ( $this->logPerformance ) {
250 fwrite( $fh, $text );
254 private function getMicroTime() { #
return time
in seconds, with microsecond accuracy
255 list( $usec, $sec ) = explode(
" ", microtime() );
256 return ( (
float)$usec + (
float)$sec );