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