3 * Convert from the old links schema (string->ID) to the new schema (ID->ID).
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script to convert from the old links schema (string->ID)
28 * to the new schema (ID->ID).
30 * The wiki should be put into read-only mode while this script executes.
32 * @ingroup Maintenance
34 class ConvertLinks
extends Maintenance
{
35 private $logPerformance;
37 public function __construct() {
38 parent
::__construct();
40 "Convert from the old links schema (string->ID) to the new schema (ID->ID)."
41 . "The wiki should be put into read-only mode while this script executes";
43 $this->addArg( 'logperformance', "Log performance to perfLogFilename.", false );
46 "Filename where performance is logged if --logperformance was set "
47 . "(defaults to 'convLinksPerf.txt').",
52 "Don't overwrite the old links table with the new one, leave the new table at links_temp.",
57 /* (What about InnoDB?) */
58 "Don't create keys, and so allow duplicates in the new links table.\n"
59 . "This gives a huge speed improvement for very large links tables which are MyISAM.",
64 public function getDbType() {
65 return Maintenance
::DB_ADMIN
;
68 public function execute() {
69 $dbw = wfGetDB( DB_MASTER
);
71 $type = $dbw->getType();
72 if ( $type != 'mysql' ) {
73 $this->output( "Link table conversion not necessary for $type\n" );
80 $numBadLinks = $curRowsRead = 0;
82 # total tuples INSERTed into links_temp
83 $totalTuplesInserted = 0;
85 # whether or not to give progress reports while reading IDs from cur table
86 $reportCurReadProgress = true;
88 # number of rows between progress reports
89 $curReadReportInterval = 1000;
91 # whether or not to give progress reports during conversion
92 $reportLinksConvProgress = true;
94 # number of rows per INSERT
95 $linksConvInsertInterval = 1000;
97 $initialRowOffset = 0;
99 # not used yet; highest row number from links table to process
100 # $finalRowOffset = 0;
102 $overwriteLinksTable = !$this->hasOption( 'keep-links-table' );
103 $noKeys = $this->hasOption( 'noKeys' );
104 $this->logPerformance
= $this->hasOption( 'logperformance' );
105 $perfLogFilename = $this->getArg( 'perfLogFilename', "convLinksPerf.txt" );
107 # --------------------------------------------------------------------
109 list( $cur, $links, $links_temp, $links_backup ) =
110 $dbw->tableNamesN( 'cur', 'links', 'links_temp', 'links_backup' );
112 if ( $dbw->tableExists( 'pagelinks' ) ) {
113 $this->output( "...have pagelinks; skipping old links table updates\n" );
117 $res = $dbw->query( "SELECT l_from FROM $links LIMIT 1" );
118 if ( $dbw->fieldType( $res, 0 ) == "int" ) {
119 $this->output( "Schema already converted\n" );
123 $res = $dbw->query( "SELECT COUNT(*) AS count FROM $links" );
124 $row = $dbw->fetchObject( $res );
125 $numRows = $row->count
;
126 $dbw->freeResult( $res );
128 if ( $numRows == 0 ) {
129 $this->output( "Updating schema (no rows to convert)...\n" );
130 $this->createTempTable();
133 if ( $this->logPerformance
) {
134 $fh = fopen ( $perfLogFilename, "w" );
136 $this->error( "Couldn't open $perfLogFilename" );
137 $this->logPerformance
= false;
140 $baseTime = $startTime = $this->getMicroTime();
141 # Create a title -> cur_id map
142 $this->output( "Loading IDs from $cur table...\n" );
143 $this->performanceLog ( $fh, "Reading $numRows rows from cur table...\n" );
144 $this->performanceLog ( $fh, "rows read vs seconds elapsed:\n" );
146 $dbw->bufferResults( false );
147 $res = $dbw->query( "SELECT cur_namespace,cur_title,cur_id FROM $cur" );
150 foreach ( $res as $row ) {
151 $title = $row->cur_title
;
152 if ( $row->cur_namespace
) {
153 $title = $wgContLang->getNsText( $row->cur_namespace
) . ":$title";
155 $ids[$title] = $row->cur_id
;
157 if ( $reportCurReadProgress ) {
158 if ( ( $curRowsRead %
$curReadReportInterval ) == 0 ) {
159 $this->performanceLog(
161 $curRowsRead . " " . ( $this->getMicroTime() - $baseTime ) . "\n"
163 $this->output( "\t$curRowsRead rows of $cur table read.\n" );
167 $dbw->freeResult( $res );
168 $dbw->bufferResults( true );
169 $this->output( "Finished loading IDs.\n\n" );
170 $this->performanceLog(
172 "Took " . ( $this->getMicroTime() - $baseTime ) . " seconds to load IDs.\n\n"
175 # --------------------------------------------------------------------
177 # Now, step through the links table (in chunks of $linksConvInsertInterval rows),
178 # convert, and write to the new table.
179 $this->createTempTable();
180 $this->performanceLog( $fh, "Resetting timer.\n\n" );
181 $baseTime = $this->getMicroTime();
182 $this->output( "Processing $numRows rows from $links table...\n" );
183 $this->performanceLog( $fh, "Processing $numRows rows from $links table...\n" );
184 $this->performanceLog( $fh, "rows inserted vs seconds elapsed:\n" );
186 for ( $rowOffset = $initialRowOffset; $rowOffset < $numRows;
187 $rowOffset +
= $linksConvInsertInterval
189 $sqlRead = "SELECT * FROM $links ";
190 $sqlRead = $dbw->limitResult( $sqlRead, $linksConvInsertInterval, $rowOffset );
191 $res = $dbw->query( $sqlRead );
193 $sqlWrite = array( "INSERT INTO $links_temp (l_from,l_to) VALUES " );
195 $sqlWrite = array( "INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES " );
198 $tuplesAdded = 0; # no tuples added to INSERT yet
199 foreach ( $res as $row ) {
200 $fromTitle = $row->l_from
;
201 if ( array_key_exists( $fromTitle, $ids ) ) { # valid title
202 $from = $ids[$fromTitle];
204 if ( $tuplesAdded != 0 ) {
207 $sqlWrite[] = "($from,$to)";
209 } else { # invalid title
213 $dbw->freeResult( $res );
214 # $this->output( "rowOffset: $rowOffset\ttuplesAdded: "
215 # . "$tuplesAdded\tnumBadLinks: $numBadLinks\n" );
216 if ( $tuplesAdded != 0 ) {
217 if ( $reportLinksConvProgress ) {
218 $this->output( "Inserting $tuplesAdded tuples into $links_temp..." );
220 $dbw->query( implode( "", $sqlWrite ) );
221 $totalTuplesInserted +
= $tuplesAdded;
222 if ( $reportLinksConvProgress ) {
223 $this->output( " done. Total $totalTuplesInserted tuples inserted.\n" );
224 $this->performanceLog(
226 $totalTuplesInserted . " " . ( $this->getMicroTime() - $baseTime ) . "\n"
231 $this->output( "$totalTuplesInserted valid titles and "
232 . "$numBadLinks invalid titles were processed.\n\n" );
233 $this->performanceLog(
235 "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n"
237 $this->performanceLog(
239 "Total execution time: " . ( $this->getMicroTime() - $startTime ) . " seconds.\n"
241 if ( $this->logPerformance
) {
245 # --------------------------------------------------------------------
247 if ( $overwriteLinksTable ) {
248 # Check for existing links_backup, and delete it if it exists.
249 $this->output( "Dropping backup links table if it exists..." );
250 $dbw->query( "DROP TABLE IF EXISTS $links_backup", __METHOD__
);
251 $this->output( " done.\n" );
253 # Swap in the new table, and move old links table to links_backup
254 $this->output( "Swapping tables '$links' to '$links_backup'; '$links_temp' to '$links'..." );
255 $dbw->query( "RENAME TABLE links TO $links_backup, $links_temp TO $links", __METHOD__
);
256 $this->output( " done.\n\n" );
258 $this->output( "Conversion complete. The old table remains at $links_backup;\n" );
259 $this->output( "delete at your leisure.\n" );
261 $this->output( "Conversion complete. The converted table is at $links_temp;\n" );
262 $this->output( "the original links table is unchanged.\n" );
266 private function createTempTable() {
267 $dbConn = wfGetDB( DB_MASTER
);
269 if ( !( $dbConn->isOpen() ) ) {
270 $this->output( "Opening connection to database failed.\n" );
273 $links_temp = $dbConn->tableName( 'links_temp' );
275 $this->output( "Dropping temporary links table if it exists..." );
276 $dbConn->query( "DROP TABLE IF EXISTS $links_temp" );
277 $this->output( " done.\n" );
279 $this->output( "Creating temporary links table..." );
280 if ( $this->hasOption( 'noKeys' ) ) {
281 $dbConn->query( "CREATE TABLE $links_temp ( " .
282 "l_from int(8) unsigned NOT NULL default '0', " .
283 "l_to int(8) unsigned NOT NULL default '0')" );
285 $dbConn->query( "CREATE TABLE $links_temp ( " .
286 "l_from int(8) unsigned NOT NULL default '0', " .
287 "l_to int(8) unsigned NOT NULL default '0', " .
288 "UNIQUE KEY l_from(l_from,l_to), " .
291 $this->output( " done.\n\n" );
294 private function performanceLog( $fh, $text ) {
295 if ( $this->logPerformance
) {
296 fwrite( $fh, $text );
300 private function getMicroTime() { # return time in seconds, with microsecond accuracy
301 list( $usec, $sec ) = explode( " ", microtime() );
302 return ( (float)$usec +
(float)$sec );
306 $maintClass = "ConvertLinks";
307 require_once RUN_MAINTENANCE_IF_MAIN
;