26 require_once __DIR__ .
'/Maintenance.php';
40 parent::__construct();
42 'Convert from the old links schema (string->ID) to the new schema (ID->ID). '
43 .
'The wiki should be put into read-only mode while this script executes' );
45 $this->
addArg(
'logperformance',
"Log performance to perfLogFilename.",
false );
48 "Filename where performance is logged if --logperformance was set "
49 .
"(defaults to 'convLinksPerf.txt').",
54 "Don't overwrite the old links table with the new one, leave the new table at links_temp.",
60 "Don't create keys, and so allow duplicates in the new links table.\n"
61 .
"This gives a huge speed improvement for very large links tables which are MyISAM.",
73 $type = $dbw->getType();
74 if (
$type !=
'mysql' ) {
75 $this->
output(
"Link table conversion not necessary for $type\n" );
81 $numBadLinks = $curRowsRead = 0;
83 # total tuples INSERTed into links_temp
84 $totalTuplesInserted = 0;
86 # whether or not to give progress reports while reading IDs from cur table
87 $reportCurReadProgress =
true;
89 # number of rows between progress reports
90 $curReadReportInterval = 1000;
92 # whether or not to give progress reports during conversion
93 $reportLinksConvProgress =
true;
95 # number of rows per INSERT
96 $linksConvInsertInterval = 1000;
98 $initialRowOffset = 0;
100 # not used yet; highest row number from links table to process
101 # $finalRowOffset = 0;
103 $overwriteLinksTable = !$this->
hasOption(
'keep-links-table' );
105 $this->logPerformance = $this->
hasOption(
'logperformance' );
106 $perfLogFilename = $this->
getArg(
'perfLogFilename',
"convLinksPerf.txt" );
108 # --------------------------------------------------------------------
110 list( $cur, $links, $links_temp, $links_backup ) =
111 $dbw->tableNamesN(
'cur',
'links',
'links_temp',
'links_backup' );
113 if ( $dbw->tableExists(
'pagelinks' ) ) {
114 $this->
output(
"...have pagelinks; skipping old links table updates\n" );
119 $res = $dbw->query(
"SELECT l_from FROM $links LIMIT 1" );
120 if ( $dbw->fieldType(
$res, 0 ) ==
"int" ) {
121 $this->
output(
"Schema already converted\n" );
126 $res = $dbw->query(
"SELECT COUNT(*) AS count FROM $links" );
127 $row = $dbw->fetchObject(
$res );
128 $numRows = $row->count;
130 if ( $numRows == 0 ) {
131 $this->
output(
"Updating schema (no rows to convert)...\n" );
135 if ( $this->logPerformance ) {
136 $fh = fopen( $perfLogFilename,
"w" );
138 $this->
error(
"Couldn't open $perfLogFilename" );
139 $this->logPerformance =
false;
142 $baseTime = $startTime = microtime(
true );
143 # Create a title -> cur_id map
144 $this->
output(
"Loading IDs from $cur table...\n" );
145 $this->
performanceLog( $fh,
"Reading $numRows rows from cur table...\n" );
148 $dbw->bufferResults(
false );
149 $res = $dbw->query(
"SELECT cur_namespace,cur_title,cur_id FROM $cur" );
152 foreach (
$res as $row ) {
154 if ( $row->cur_namespace ) {
155 $title = MediaWikiServices::getInstance()->getContentLanguage()->
156 getNsText( $row->cur_namespace ) .
":$title";
158 $ids[
$title] = $row->cur_id;
160 if ( $reportCurReadProgress ) {
161 if ( ( $curRowsRead % $curReadReportInterval ) == 0 ) {
164 $curRowsRead .
" " . ( microtime(
true ) - $baseTime ) .
"\n"
166 $this->
output(
"\t$curRowsRead rows of $cur table read.\n" );
170 $dbw->bufferResults(
true );
171 $this->
output(
"Finished loading IDs.\n\n" );
174 "Took " . ( microtime(
true ) - $baseTime ) .
" seconds to load IDs.\n\n"
177 # --------------------------------------------------------------------
179 # Now, step through the links table (in chunks of $linksConvInsertInterval rows),
180 # convert, and write to the new table.
183 $baseTime = microtime(
true );
184 $this->
output(
"Processing $numRows rows from $links table...\n" );
185 $this->
performanceLog( $fh,
"Processing $numRows rows from $links table...\n" );
186 $this->
performanceLog( $fh,
"rows inserted vs seconds elapsed:\n" );
188 for ( $rowOffset = $initialRowOffset; $rowOffset < $numRows;
189 $rowOffset += $linksConvInsertInterval
191 $sqlRead =
"SELECT * FROM $links ";
192 $sqlRead = $dbw->limitResult( $sqlRead, $linksConvInsertInterval, $rowOffset );
193 $res = $dbw->query( $sqlRead );
195 $sqlWrite = [
"INSERT INTO $links_temp (l_from,l_to) VALUES " ];
197 $sqlWrite = [
"INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES " ];
200 $tuplesAdded = 0; # no tuples added to INSERT yet
201 foreach (
$res as $row ) {
202 $fromTitle = $row->l_from;
203 if ( array_key_exists( $fromTitle, $ids ) ) { # valid
title
204 $from = $ids[$fromTitle];
206 if ( $tuplesAdded != 0 ) {
209 $sqlWrite[] =
"($from,$to)";
211 }
else { # invalid
title
215 # $this->output( "rowOffset: $rowOffset\ttuplesAdded: "
216 # . "$tuplesAdded\tnumBadLinks: $numBadLinks\n" );
217 if ( $tuplesAdded != 0 ) {
218 if ( $reportLinksConvProgress ) {
219 $this->
output(
"Inserting $tuplesAdded tuples into $links_temp..." );
221 $dbw->query( implode(
"", $sqlWrite ) );
222 $totalTuplesInserted += $tuplesAdded;
223 if ( $reportLinksConvProgress ) {
224 $this->
output(
" done. Total $totalTuplesInserted tuples inserted.\n" );
227 $totalTuplesInserted .
" " . ( microtime(
true ) - $baseTime ) .
"\n"
232 $this->
output(
"$totalTuplesInserted valid titles and "
233 .
"$numBadLinks invalid titles were processed.\n\n" );
236 "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n"
240 "Total execution time: " . ( microtime(
true ) - $startTime ) .
" seconds.\n"
242 if ( $this->logPerformance ) {
246 # --------------------------------------------------------------------
248 if ( $overwriteLinksTable ) {
249 # Check for existing links_backup, and delete it if it exists.
250 $this->
output(
"Dropping backup links table if it exists..." );
251 $dbw->query(
"DROP TABLE IF EXISTS $links_backup", __METHOD__ );
252 $this->
output(
" done.\n" );
254 # Swap in the new table, and move old links table to links_backup
255 $this->
output(
"Swapping tables '$links' to '$links_backup'; '$links_temp' to '$links'..." );
256 $dbw->query(
"RENAME TABLE links TO $links_backup, $links_temp TO $links", __METHOD__ );
257 $this->
output(
" done.\n\n" );
259 $this->
output(
"Conversion complete. The old table remains at $links_backup;\n" );
260 $this->
output(
"delete at your leisure.\n" );
262 $this->
output(
"Conversion complete. The converted table is at $links_temp;\n" );
263 $this->
output(
"the original links table is unchanged.\n" );
270 if ( !( $dbConn->isOpen() ) ) {
271 $this->
output(
"Opening connection to database failed.\n" );
275 $links_temp = $dbConn->tableName(
'links_temp' );
277 $this->
output(
"Dropping temporary links table if it exists..." );
278 $dbConn->query(
"DROP TABLE IF EXISTS $links_temp" );
279 $this->
output(
" done.\n" );
281 $this->
output(
"Creating temporary links table..." );
283 $dbConn->query(
"CREATE TABLE $links_temp ( " .
284 "l_from int(8) unsigned NOT NULL default '0', " .
285 "l_to int(8) unsigned NOT NULL default '0')" );
287 $dbConn->query(
"CREATE TABLE $links_temp ( " .
288 "l_from int(8) unsigned NOT NULL default '0', " .
289 "l_to int(8) unsigned NOT NULL default '0', " .
290 "UNIQUE KEY l_from(l_from,l_to), " .
293 $this->
output(
" done.\n\n" );
297 if ( $this->logPerformance ) {
298 fwrite( $fh, $text );