Special:Log and the logging table -- unified logging scariness!
[lhc/web/wiklou.git] / maintenance / importPhase2.php
1 <?php
2 # MediaWiki 'phase 2' to current format import script
3 # (import format current as of 1.2.0, March 2004)
4 #
5 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
6 # Portions by Lee Daniel Crocker, 2002
7 # http://www.mediawiki.org/
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 # http://www.gnu.org/copyleft/gpl.html
23
24 die( "This import script is not currently maintained.
25 If you need it you'll have to modify it as necessary.\n");
26
27 if ( ! is_readable( "../LocalSettings.php" ) ) {
28 print "A copy of your installation's LocalSettings.php\n" .
29 "must exist in the source directory.\n";
30 exit();
31 }
32
33 $wgCommandLineMode = true;
34 ini_set("implicit_flush", 1);
35
36 $DP = "../includes";
37 require_once( "../LocalSettings.php" );
38 require_once( "../AdminSettings.php" );
39
40 $wgDBuser = $wgDBadminuser;
41 $wgDBpassword = $wgDBadminpassword;
42
43 $sep = ( DIRECTORY_SEPARATOR == "\\" ) ? ";" : ":";
44 ini_set( "include_path", "$IP$sep$include_path" );
45
46 require_once( "Setup.php" );
47
48 require_once( "../install-utils.inc" );
49 require_once( "InitialiseMessages.inc" );
50 require_once( "rebuildlinks.inc" );
51 require_once( "rebuildrecentchanges.inc" );
52 require_once( "rebuildtextindex.inc" );
53
54 class Phase2Importer {
55 var $olddb, $titleCache;
56
57 function Phase2Importer( $database ) {
58 $this->olddb = $database;
59 $this->titleCache = new TitleCache;
60 }
61
62 function importAll() {
63 $this->importCurData();
64 $this->fixCurTitles();
65
66 $this->importOldData();
67 $this->fixOldTitles();
68
69 $this->importUserData();
70 $this->fixUserOptions();
71
72 $this->importWatchlists();
73
74 $this->importLinkData();
75
76 /*
77 # For some reason this is broken. RecentChanges will just start anew...
78 rebuildRecentChangesTablePass1();
79 rebuildRecentChangesTablePass2();
80 */
81
82 print "Rebuilding search index:\n";
83 dropTextIndex();
84 rebuildTextIndex();
85 createTextIndex();
86
87 initialiseMessages();
88 }
89
90 # Simple import functions; for the most part these are pretty straightforward.
91 # MySQL copies everything over to the new database and tweaks a few things.
92 function importCurData() {
93 print "Clearing pages from default install, if any...\n";
94 wfQuery( "DELETE FROM cur", DB_MASTER );
95
96 print "Importing current revision data...\n";
97 wfQuery( "INSERT INTO cur (cur_id,cur_namespace,cur_title,cur_text,cur_comment,
98 cur_user,cur_user_text,cur_timestamp,cur_restrictions,cur_counter,
99 cur_is_redirect,cur_minor_edit,cur_is_new,cur_random,cur_touched,inverse_timestamp)
100 SELECT cur_id,0,cur_title,cur_text,cur_comment,
101 cur_user,cur_user_text,cur_timestamp,REPLACE(cur_restrictions,'is_',''),cur_counter,
102 cur_text like '#redirect%',cur_minor_edit,0,RAND(),NOW()+0,99999999999999-cur_timestamp
103 FROM {$this->olddb}.cur", DB_MASTER );
104 $n = mysql_affected_rows();
105 print "$n rows imported.\n";
106 }
107
108 function importOldData() {
109 print "Clearing old revision data from default install, if any...\n";
110 wfQuery( "DELETE FROM old", DB_MASTER );
111
112 print "Importing old revision data...\n";
113 wfQuery( "INSERT INTO old (old_id,old_namespace,old_title,old_text,old_comment,
114 old_user,old_user_text,old_timestamp,old_minor_edit,old_flags,inverse_timestamp)
115 SELECT old_id,0,old_title,old_text,old_comment,
116 old_user,old_user_text,old_timestamp,old_minor_edit,'',99999999999999-old_timestamp
117 FROM {$this->olddb}.old", DB_MASTER );
118 $n = mysql_affected_rows();
119 print "$n rows imported.\n";
120 }
121
122 function importUserData() {
123 print "Clearing users from default install, if any...\n";
124 wfQuery( "DELETE FROM user", DB_MASTER );
125
126 print "Importing user data...\n";
127 wfQuery( "INSERT INTO $newdb.user (user_id,user_name,user_rights,
128 user_password,user_newpassword,user_email,user_options,user_touched)
129 SELECT user_id,user_name,REPLACE(user_rights,'is_',''),
130 MD5(CONCAT(user_id,'-',MD5(user_password))),'',user_email,user_options,NOW()+0
131 FROM {$this->olddb}.user", DB_MASTER );
132 $n = mysql_affected_rows();
133 print "$n rows imported.\n";
134 }
135
136 # A little less clean...
137 function importWatchlists() {
138 print "Clearing watchlists from default install, if any...\n";
139 wfQuery( "DELETE FROM watchlist", DB_MASTER );
140
141 print "Importing watchlists...";
142 $res = wfQuery( "SELECT user_id,user_watch FROM {$this->olddb}.user WHERE user_watch != ''", DB_MASTER );
143 $total = wfNumRows( $res );
144 $n = 0;
145 print " ($total total)\n";
146
147 while( $row = wfFetchObject( $res ) ) {
148 $id = IntVal( $row->user_id );
149 $list = explode( "\n", $row->user_watch );
150 foreach( $list as $page ) {
151 $title = $this->titleCache->fetch( $page );
152 if( is_null( $title ) ) {
153 print "Caught bad title '{$row->title}'\n";
154 } else {
155 $ns = $title->getNamespace();
156 $t = wfStrencode( $title->getDBkey() );
157 wfQuery( "INSERT INTO watchlist(wl_user,wl_namespace,wl_title) VALUES ($id,$ns,'$t')", DB_MASTER );
158 }
159 }
160 if( ++$n % 50 == 0 ) {
161 print "$n\n";
162 }
163 }
164 wfFreeResult( $res );
165 }
166
167 function importLinkData() {
168 # MUST BE CALLED BEFORE! fixCurTitles()
169 print "Clearing links from default install, if any...\n";
170 wfQuery( "DELETE FROM links", DB_MASTER );
171 wfQuery( "DELETE FROM brokenlinks", DB_MASTER );
172
173 print "Importing live links...";
174 wfQuery( "INSERT INTO links (l_from, l_to)
175 SELECT DISTINCT linked_from,cur_id
176 FROM {$this->olddb}.linked,{$this->olddb}.cur
177 WHERE linked_to=cur_title", DB_MASTER );
178 $n = mysql_affected_rows();
179 print "$n rows imported.\n";
180
181 print "Importing broken links...";
182 wfQuery( "INSERT INTO brokenlinks (bl_from, bl_to)
183 SELECT DISTINCT cur_id,unlinked_to
184 FROM {$this->olddb}.unlinked,{$this->olddb}.cur
185 WHERE unlinked_from=cur_title", DB_MASTER );
186 $n = mysql_affected_rows();
187 print "$n rows imported.\n";
188 }
189
190 # Fixup functions: munge data that's already been brought into tables
191 function fixCurTitles() {
192 $this->fixTitles( "cur" );
193 }
194
195 function fixOldTitles() {
196 $this->fixTitles( "old" );
197 }
198
199 function fixTitles( $table ) {
200 print "Fixing titles in $table...";
201 $res = wfQuery( "SELECT DISTINCT {$table}_title AS title FROM $table", DB_MASTER );
202 $total = wfNumRows( $res );
203 $n = 0;
204 print " ($total total)\n";
205
206 while( $row = wfFetchObject( $res ) ) {
207 $xt = wfStrencode( $row->title );
208 $title = $this->titleCache->fetch( $row->title );
209 if( is_null( $title ) ) {
210 print "Caught bad title '{$row->title}'\n";
211 } else {
212 $ns = $title->getNamespace();
213 $t = wfStrencode( $title->getDBkey() );
214 wfQuery( "UPDATE $table SET {$table}_namespace=$ns,{$table}_title='$t'
215 WHERE {$table}_namespace=0 AND {$table}_title='$xt'", DB_MASTER );
216 }
217 if( ++$n % 50 == 0 ) {
218 print "$n\n";
219 }
220 }
221 wfFreeResult( $res );
222 }
223
224 function rewriteUserOptions( $in )
225 {
226 $s = urldecode( $in );
227 $a = explode( "\n", $s );
228
229 foreach ( $a as $l ) {
230 if ( preg_match( "/^([A-Za-z0-9_]+)=(.*)/", $l, $m ) ) {
231 $ops[$m[1]] = $m[2];
232 }
233 }
234 $nops = array();
235
236 $q = strtolower( $ops["quickBar"] );
237 if ( $q == "none" ) { $q = 0; }
238 else { $q = 1; } # Default to left
239 $nops["quickbar"] = $q;
240
241 if ( $ops["markupNewTopics"] == "inverse" ) {
242 $nops["highlightbroken"] = 1;
243 }
244 $sk = substr( strtolower( $ops["skin"] ), 0, 4 );
245 if ( "star" == $sk ) { $sk = 0; }
246 else if ( "nost" == $sk ) { $sk = 1; }
247 else if ( "colo" == $sk ) { $sk = 2; }
248 else { $sk = 0; }
249 $nops["skin"] = $sk;
250
251 $u = strtolower( $ops["underlineLinks"] );
252 if ( "yes" == $u || "on" == $u ) { $nops["underline"] = 1; }
253 else { $nops["underline"] = 0; }
254
255 $t = ( (int) ($ops["hourDiff"]) );
256 if ( $t < -23 || $t > 23 ) { $t = 0; }
257 if ( 0 != $t ) { $nops["timecorrection"] = $t; }
258
259 $j = strtolower( $ops["justify"] );
260 if ( "yes" == $j || "on" == $j ) { $nops["justify"] = 1; }
261 $n = strtolower( $ops["numberHeadings"] );
262 if ( "yes" == $n || "on" == $n ) { $nops["numberheadings"] = 1; }
263 $h = strtolower( $ops["hideMinor"] );
264 if ( "yes" == $h || "on" == $h ) { $nops["hideminor"] = 1; }
265 $r = strtolower( $ops["rememberPassword"] );
266 if ( "yes" == $r || "on" == $r ) { $nops["rememberpassword"] = 1; }
267 $s = strtolower( $ops["showHover"] );
268 if ( "yes" == $s || "on" == $s ) { $nops["hover"] = 1; }
269
270 $c = $ops["cols"];
271 if ( $c < 20 || c > 200 ) { $nops["cols"] = 80; }
272 else { $nops["cols"] = $c; }
273 $r = $ops["rows"];
274 if ( $r < 5 || $r > 100 ) { $nops["rows"] = 20; }
275 else { $nops["rows"] = $r; }
276 $r = $ops["resultsPerPage"];
277 if ( $r < 3 || $r > 500 ) { $nops["searchlimit"] = 20; }
278 else { $nops["searchlimit"] = $r; }
279 $r = $ops["viewRecentChanges"];
280 if ( $r < 10 || $r > 1000 ) { $nops["rclimit"] = 50; }
281 else { $nops["rclimit"] = $r; }
282 $nops["rcdays"] = 3;
283
284 $a = array();
285 foreach ( $nops as $oname => $oval ) {
286 array_push( $a, "$oname=$oval" );
287 }
288 $s = implode( "\n", $a );
289 return $s;
290 }
291
292 function fixUserOptions() {
293 print "Fixing user options...";
294 $res = wfQuery( "SELECT user_id,user_options FROM user", DB_MASTER );
295 $total = wfNumRows( $res );
296 $n = 0;
297 print " ($total total)\n";
298
299 while( $row = wfFetchObject( $res ) ) {
300 $id = IntVal( $row->user_id );
301 $option = wfStrencode( $this->rewriteUserOptions( $row->user_options ) );
302 wfQuery( "UPDATE user SET user_options='$option' WHERE user_id=$id LIMIT 1", DB_MASTER );
303 if( ++$n % 50 == 0 ) {
304 print "$n\n";
305 }
306 }
307 wfFreeResult( $res );
308 }
309
310 }
311
312 class TitleCache {
313 var $hash = array();
314
315 function &fetch( $dbkey ) {
316 if( !isset( $hash[$dbkey] ) ) {
317 $hash[$dbkey] = Title::newFromDBkey( $dbkey );
318 }
319 return $hash[$dbkey];
320 }
321
322 }
323
324 #
325 print "You should have already run the installer to create a fresh, blank database.\n";
326 print "Data will be inserted into '$wgDBname'. THIS SHOULD BE EMPTY AND ANY DATA IN IN WILL BE ERASED!\n";
327 print "\nIf that's not what you want, ABORT NOW!\n\n";
328
329 print "Please enter the name of the old 'phase 2'-format database that will be used as a source:\n";
330 print "Old database name [enciclopedia]: ";
331 $olddb = readconsole();
332 if( empty( $olddb ) ) $olddb = "enciclopedia";
333
334 if( $olddb == $wgDBname ) {
335 die( "Can't upgrade in-place! You must create a new database and copy data into it.\n" );
336 }
337
338 print "\nSource database: '$olddb'\n";
339 print " Dest database: '$wgDBname'\n";
340 print "Is this correct? Anything in '$wgDBname' WILL BE DESTROYED. [y/N] ";
341 $response = readconsole();
342 if( strtolower( $response{0} ) != 'y' ) {
343 die( "\nAborted by user.\n" );
344 }
345
346 print "Starting import....\n";
347
348 $wgTitle = Title::newFromText( "Conversion script" );
349 $importer = new Phase2Importer( $olddb );
350 $importer->importAll();
351
352 ?>