MediaWiki REL1_33
convertLinks.php
Go to the documentation of this file.
1<?php
25
26require_once __DIR__ . '/Maintenance.php';
27
38
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription(
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' );
44
45 $this->addArg( 'logperformance', "Log performance to perfLogFilename.", false );
46 $this->addArg(
47 'perfLogFilename',
48 "Filename where performance is logged if --logperformance was set "
49 . "(defaults to 'convLinksPerf.txt').",
50 false
51 );
52 $this->addArg(
53 'keep-links-table',
54 "Don't overwrite the old links table with the new one, leave the new table at links_temp.",
55 false
56 );
57 $this->addArg(
58 'nokeys',
59 /* (What about InnoDB?) */
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.",
62 false
63 );
64 }
65
66 public function getDbType() {
68 }
69
70 public function execute() {
71 $dbw = $this->getDB( DB_MASTER );
72
73 $type = $dbw->getType();
74 if ( $type != 'mysql' ) {
75 $this->output( "Link table conversion not necessary for $type\n" );
76
77 return;
78 }
79
80 # counters etc
81 $numBadLinks = $curRowsRead = 0;
82
83 # total tuples INSERTed into links_temp
84 $totalTuplesInserted = 0;
85
86 # whether or not to give progress reports while reading IDs from cur table
87 $reportCurReadProgress = true;
88
89 # number of rows between progress reports
90 $curReadReportInterval = 1000;
91
92 # whether or not to give progress reports during conversion
93 $reportLinksConvProgress = true;
94
95 # number of rows per INSERT
96 $linksConvInsertInterval = 1000;
97
98 $initialRowOffset = 0;
99
100 # not used yet; highest row number from links table to process
101 # $finalRowOffset = 0;
102
103 $overwriteLinksTable = !$this->hasOption( 'keep-links-table' );
104 $noKeys = $this->hasOption( 'noKeys' );
105 $this->logPerformance = $this->hasOption( 'logperformance' );
106 $perfLogFilename = $this->getArg( 'perfLogFilename', "convLinksPerf.txt" );
107
108 # --------------------------------------------------------------------
109
110 list( $cur, $links, $links_temp, $links_backup ) =
111 $dbw->tableNamesN( 'cur', 'links', 'links_temp', 'links_backup' );
112
113 if ( $dbw->tableExists( 'pagelinks' ) ) {
114 $this->output( "...have pagelinks; skipping old links table updates\n" );
115
116 return;
117 }
118
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" );
122
123 return;
124 }
125
126 $res = $dbw->query( "SELECT COUNT(*) AS count FROM $links" );
127 $row = $dbw->fetchObject( $res );
128 $numRows = $row->count;
129
130 if ( $numRows == 0 ) {
131 $this->output( "Updating schema (no rows to convert)...\n" );
132 $this->createTempTable();
133 } else {
134 $fh = false;
135 if ( $this->logPerformance ) {
136 $fh = fopen( $perfLogFilename, "w" );
137 if ( !$fh ) {
138 $this->error( "Couldn't open $perfLogFilename" );
139 $this->logPerformance = false;
140 }
141 }
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" );
146 $this->performanceLog( $fh, "rows read vs seconds elapsed:\n" );
147
148 $dbw->bufferResults( false );
149 $res = $dbw->query( "SELECT cur_namespace,cur_title,cur_id FROM $cur" );
150 $ids = [];
151
152 foreach ( $res as $row ) {
153 $title = $row->cur_title;
154 if ( $row->cur_namespace ) {
155 $title = MediaWikiServices::getInstance()->getContentLanguage()->
156 getNsText( $row->cur_namespace ) . ":$title";
157 }
158 $ids[$title] = $row->cur_id;
159 $curRowsRead++;
160 if ( $reportCurReadProgress ) {
161 if ( ( $curRowsRead % $curReadReportInterval ) == 0 ) {
162 $this->performanceLog(
163 $fh,
164 $curRowsRead . " " . ( microtime( true ) - $baseTime ) . "\n"
165 );
166 $this->output( "\t$curRowsRead rows of $cur table read.\n" );
167 }
168 }
169 }
170 $dbw->bufferResults( true );
171 $this->output( "Finished loading IDs.\n\n" );
172 $this->performanceLog(
173 $fh,
174 "Took " . ( microtime( true ) - $baseTime ) . " seconds to load IDs.\n\n"
175 );
176
177 # --------------------------------------------------------------------
178
179 # Now, step through the links table (in chunks of $linksConvInsertInterval rows),
180 # convert, and write to the new table.
181 $this->createTempTable();
182 $this->performanceLog( $fh, "Resetting timer.\n\n" );
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" );
187
188 for ( $rowOffset = $initialRowOffset; $rowOffset < $numRows;
189 $rowOffset += $linksConvInsertInterval
190 ) {
191 $sqlRead = "SELECT * FROM $links ";
192 $sqlRead = $dbw->limitResult( $sqlRead, $linksConvInsertInterval, $rowOffset );
193 $res = $dbw->query( $sqlRead );
194 if ( $noKeys ) {
195 $sqlWrite = [ "INSERT INTO $links_temp (l_from,l_to) VALUES " ];
196 } else {
197 $sqlWrite = [ "INSERT IGNORE INTO $links_temp (l_from,l_to) VALUES " ];
198 }
199
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];
205 $to = $row->l_to;
206 if ( $tuplesAdded != 0 ) {
207 $sqlWrite[] = ",";
208 }
209 $sqlWrite[] = "($from,$to)";
210 $tuplesAdded++;
211 } else { # invalid title
212 $numBadLinks++;
213 }
214 }
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..." );
220 }
221 $dbw->query( implode( "", $sqlWrite ) );
222 $totalTuplesInserted += $tuplesAdded;
223 if ( $reportLinksConvProgress ) {
224 $this->output( " done. Total $totalTuplesInserted tuples inserted.\n" );
225 $this->performanceLog(
226 $fh,
227 $totalTuplesInserted . " " . ( microtime( true ) - $baseTime ) . "\n"
228 );
229 }
230 }
231 }
232 $this->output( "$totalTuplesInserted valid titles and "
233 . "$numBadLinks invalid titles were processed.\n\n" );
234 $this->performanceLog(
235 $fh,
236 "$totalTuplesInserted valid titles and $numBadLinks invalid titles were processed.\n"
237 );
238 $this->performanceLog(
239 $fh,
240 "Total execution time: " . ( microtime( true ) - $startTime ) . " seconds.\n"
241 );
242 if ( $this->logPerformance ) {
243 fclose( $fh );
244 }
245 }
246 # --------------------------------------------------------------------
247
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" );
253
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" );
258
259 $this->output( "Conversion complete. The old table remains at $links_backup;\n" );
260 $this->output( "delete at your leisure.\n" );
261 } else {
262 $this->output( "Conversion complete. The converted table is at $links_temp;\n" );
263 $this->output( "the original links table is unchanged.\n" );
264 }
265 }
266
267 private function createTempTable() {
268 $dbConn = $this->getDB( DB_MASTER );
269
270 if ( !( $dbConn->isOpen() ) ) {
271 $this->output( "Opening connection to database failed.\n" );
272
273 return;
274 }
275 $links_temp = $dbConn->tableName( 'links_temp' );
276
277 $this->output( "Dropping temporary links table if it exists..." );
278 $dbConn->query( "DROP TABLE IF EXISTS $links_temp" );
279 $this->output( " done.\n" );
280
281 $this->output( "Creating temporary links table..." );
282 if ( $this->hasOption( 'noKeys' ) ) {
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')" );
286 } else {
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), " .
291 "KEY (l_to))" );
292 }
293 $this->output( " done.\n\n" );
294 }
295
296 private function performanceLog( $fh, $text ) {
297 if ( $this->logPerformance ) {
298 fwrite( $fh, $text );
299 }
300 }
301}
302
303$maintClass = ConvertLinks::class;
304require_once RUN_MAINTENANCE_IF_MAIN;
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
addArg( $arg, $description, $required=true)
Add some args that are needed.
output( $out, $channel=null)
Throw some output to the user.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
hasOption( $name)
Checks to see if a particular option exists.
getArg( $argId=0, $default=null)
Get an argument.
addDescription( $text)
Set the description text.
MediaWikiServices is the service locator for the application scope of MediaWiki.
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
require_once RUN_MAINTENANCE_IF_MAIN
title
const DB_MASTER
Definition defines.php:26