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