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;
129 $dbw->freeResult(
$res );
131 if ( $numRows == 0 ) {
132 $this->
output(
"Updating schema (no rows to convert)...\n" );
136 if ( $this->logPerformance ) {
137 $fh = fopen( $perfLogFilename,
"w" );
139 $this->
error(
"Couldn't open $perfLogFilename" );
140 $this->logPerformance =
false;
143 $baseTime = $startTime = microtime(
true );
144 # Create a title -> cur_id map
145 $this->
output(
"Loading IDs from $cur table...\n" );
146 $this->
performanceLog( $fh,
"Reading $numRows rows from cur table...\n" );
149 $dbw->bufferResults(
false );
150 $res = $dbw->query(
"SELECT cur_namespace,cur_title,cur_id FROM $cur" );
153 foreach (
$res as $row ) {
154 $title = $row->cur_title;
155 if ( $row->cur_namespace ) {
156 $title = MediaWikiServices::getInstance()->getContentLanguage()->
157 getNsText( $row->cur_namespace ) .
":$title";
159 $ids[
$title] = $row->cur_id;
161 if ( $reportCurReadProgress ) {
162 if ( ( $curRowsRead % $curReadReportInterval ) == 0 ) {
165 $curRowsRead .
" " . ( microtime(
true ) - $baseTime ) .
"\n"
167 $this->
output(
"\t$curRowsRead rows of $cur table read.\n" );
171 $dbw->freeResult(
$res );
172 $dbw->bufferResults(
true );
173 $this->
output(
"Finished loading IDs.\n\n" );
176 "Took " . ( microtime(
true ) - $baseTime ) .
" seconds to load IDs.\n\n"
179 # --------------------------------------------------------------------
181 # Now, step through the links table (in chunks of $linksConvInsertInterval rows),
182 # convert, and write to the new table.
185 $baseTime = microtime(
true );
186 $this->
output(
"Processing $numRows rows from $links table...\n" );
187 $this->
performanceLog( $fh,
"Processing $numRows rows from $links table...\n" );
188 $this->
performanceLog( $fh,
"rows inserted vs seconds elapsed:\n" );
190 for ( $rowOffset = $initialRowOffset; $rowOffset < $numRows;
191 $rowOffset += $linksConvInsertInterval
193 $sqlRead =
"SELECT * FROM $links ";
194 $sqlRead = $dbw->limitResult( $sqlRead, $linksConvInsertInterval, $rowOffset );
195 $res = $dbw->query( $sqlRead );
197 $sqlWrite = [
"INSERT INTO $links_temp (l_from,l_to) VALUES " ];
199 $sqlWrite = [
"INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES " ];
202 $tuplesAdded = 0; # no tuples added to INSERT yet
203 foreach (
$res as $row ) {
204 $fromTitle = $row->l_from;
205 if ( array_key_exists( $fromTitle, $ids ) ) { # valid title
206 $from = $ids[$fromTitle];
208 if ( $tuplesAdded != 0 ) {
211 $sqlWrite[] =
"($from,$to)";
213 }
else { # invalid title
217 $dbw->freeResult(
$res );
218 # $this->output( "rowOffset: $rowOffset\ttuplesAdded: "
219 # . "$tuplesAdded\tnumBadLinks: $numBadLinks\n" );
220 if ( $tuplesAdded != 0 ) {
221 if ( $reportLinksConvProgress ) {
222 $this->
output(
"Inserting $tuplesAdded tuples into $links_temp..." );
224 $dbw->query( implode(
"", $sqlWrite ) );
225 $totalTuplesInserted += $tuplesAdded;
226 if ( $reportLinksConvProgress ) {
227 $this->
output(
" done. Total $totalTuplesInserted tuples inserted.\n" );
230 $totalTuplesInserted .
" " . ( microtime(
true ) - $baseTime ) .
"\n"
235 $this->
output(
"$totalTuplesInserted valid titles and "
236 .
"$numBadLinks invalid titles were processed.\n\n" );
239 "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n"
243 "Total execution time: " . ( microtime(
true ) - $startTime ) .
" seconds.\n"
245 if ( $this->logPerformance ) {
249 # --------------------------------------------------------------------
251 if ( $overwriteLinksTable ) {
252 # Check for existing links_backup, and delete it if it exists.
253 $this->
output(
"Dropping backup links table if it exists..." );
254 $dbw->query(
"DROP TABLE IF EXISTS $links_backup", __METHOD__ );
255 $this->
output(
" done.\n" );
257 # Swap in the new table, and move old links table to links_backup
258 $this->
output(
"Swapping tables '$links' to '$links_backup'; '$links_temp' to '$links'..." );
259 $dbw->query(
"RENAME TABLE links TO $links_backup, $links_temp TO $links", __METHOD__ );
260 $this->
output(
" done.\n\n" );
262 $this->
output(
"Conversion complete. The old table remains at $links_backup;\n" );
263 $this->
output(
"delete at your leisure.\n" );
265 $this->
output(
"Conversion complete. The converted table is at $links_temp;\n" );
266 $this->
output(
"the original links table is unchanged.\n" );