forked from Kunena/com_kunenaimporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.php
450 lines (405 loc) · 16.6 KB
/
import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
/**
* @version $Id$
* Kunena Forum Importer Component
* @package com_kunenaimporter
*
* Imports forum data into Kunena
*
* @Copyright (C) 2009 - 2010 Kunena Team All rights reserved
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @link http://www.kunena.com
*
*/
defined ( '_JEXEC' ) or die ();
// TODO: Better Error detection
// TODO: User Mapping
// Import Joomla! libraries
jimport ( 'joomla.application.component.model' );
// Everything else than user import can be found from here:
require_once (JPATH_COMPONENT . DS . 'models' . DS . 'kunena.php');
class KunenaimporterModelImport extends JModel {
public function __construct() {
parent::__construct ();
$this->db = JFactory::getDBO ();
$this->db->debug = 0;
}
public function getImportOptions() {
// version
$options = array ('users','mapusers','config', 'userprofile', 'categories', 'messages', 'attachments', 'favorites', 'subscriptions', 'moderation', 'ranks', 'smilies', 'announcements', 'sessions', 'whoisonline', 'avatargalleries' );
return $options;
}
protected function commitStart() {
$query = "SET autocommit=0;";
$this->db->setQuery ( $query );
$result = $this->db->query () or die ( "<br />Disabling autocommit failed:<br />$query<br />" . $this->db->errorMsg () );
}
protected function commitEnd() {
$query = "COMMIT;";
$this->db->setQuery ( $query );
$result = $this->db->query () or die ( "<br />Commit failed:<br />$query<br />" . $this->db->errorMsg () );
$query = "SET autocommit=1;";
$this->db->setQuery ( $query );
$result = $this->db->query () or die ( "<br />Enabling autocommit failed:<br />$query<br />" . $this->db->errorMsg () );
}
public function disableKeys($table) {
$query = "ALTER TABLE {$table} DISABLE KEYS";
$this->db->setQuery ( $query );
$result = $this->db->query () or die ( "<br />Disable keys failed:<br />$query<br />" . $this->db->errorMsg () );
}
public function enableKeys($table) {
$query = "ALTER TABLE {$table} ENABLE KEYS";
$this->db->setQuery ( $query );
$result = $this->db->query () or die ( "<br />Enable keys failed:<br />$query<br />" . $this->db->errorMsg () );
}
public function setAuthMethod($auth_method) {
$this->auth_method = $auth_method;
}
public function getUsername($name) {
//if ($this->auth_method == 'joomla') return $name;
return strtr ( $name, "<>\"'%;()&", '_________' );
}
public function findPotentialUsers($extuser, $all = false) {
// Check if user exists in Joomla
$query = "SELECT u.*
FROM `#__users` AS u
LEFT JOIN `#__kunenaimporter_users` AS e ON e.id=u.id
WHERE (u.username LIKE {$this->db->quote($extuser->username)}
OR u.email LIKE {$this->db->quote($extuser->email)})";
if (!$all) {
$query .= " AND e.id IS NULL";
}
else if ($extuser->id) {
$query .= " OR u.id={$this->db->quote($extuser->id)}";
}
$this->db->setQuery ( $query );
$userlist = $this->db->loadObjectList ( 'id' );
$bestpoints = 0;
$bestid = 0;
$newlist = array ();
foreach ( $userlist as $user ) {
$points = 0;
if (strtolower($extuser->username) == strtolower($user->username))
$points += 2;
if (strtolower($extuser->email) == strtolower($user->email))
$points += 1;
$user->points = $points;
$newlist [$points] = $user;
}
krsort ( $newlist );
return $newlist;
}
protected function mapUser($extuser) {
if ($extuser->id !== null)
return $extuser->id;
$userlist = $this->findPotentialUsers ( $extuser );
$best = array_shift ( $userlist );
if (!$best)
return 0;
if (!empty($userlist))
return -$best->id;
return $best->id;
}
public function truncateUsersMap() {
$query = "TRUNCATE TABLE `#__kunenaimporter_users`";
$this->db->setQuery ( $query );
$result = $this->db->query () or die ( "<br />Invalid query:<br />$query<br />" . $this->db->errorMsg () );
}
public function mapUsers($result, $limit) {
if (!$result['total']) {
$query = "SELECT COUNT(*) FROM `#__kunenaimporter_users` WHERE id IS NULL";
$this->db->setQuery ( $query );
$result['total'] = $this->db->loadResult ();
}
$query = "SELECT * FROM `#__kunenaimporter_users` WHERE id IS NULL AND extid > ".intval($result['start']);
$this->db->setQuery ( $query, 0, $limit );
$users = $this->db->loadAssocList ();
$result['now'] = 0;
foreach ( $users as $userdata ) {
$result['start'] = $userdata['extid'];
$result['now']++;
$extuser = JTable::getInstance ( 'ExtUser', 'KunenaImporterTable' );
$extuser->bind ( $userdata );
unset($userdata);
$extuser->exists ( true );
$uid = $this->mapUser ( $extuser );
$result['all']++;
if (!$uid) {
continue;
}
$userdata = array();
if ($uid > 0) {
$userdata['id'] = abs ( $uid );
$result['new']++;
} else {
$userdata['conflict'] = abs ( $uid );
$result['conflict']++;
}
if ($extuser->save ( $userdata ) === false) {
$result['failed']++;
echo "ERROR: Saving external {$extuser->username} failed: " . $extuser->getError () . "<br />";
} elseif ($uid > 0) {
$this->updateUserData(-$extuser->extid, $uid);
}
}
unset($users);
return $result;
}
public function createUsers(&$users) {
foreach ( $users as $userdata ) {
}
}
protected function UpdateCatStats() {
// Update last message time from all categories.
$query = "UPDATE `#__kunena_categories`, `#__kunena_messages` SET `#__kunena_categories`.time_last_msg=`#__kunena_messages`.time WHERE `#__kunena_categories`.id_last_msg=`#__kunena_messages`.id AND `#__kunena_categories`.id_last_msg>0";
$this->db->setQuery ( $query );
$result = $this->db->query () or die ( "<br />Invalid query:<br />$query<br />" . $this->db->errorMsg () );
unset ( $query );
}
public function truncateData($option) {
if ($option == 'config' || $option == 'avatargalleries')
return;
if ($option == 'mapusers')
return;
if ($option == 'users')
$option = 'extuser';
if ($option == 'messages')
$this->truncateData ( $option . '_text' );
$this->db = JFactory::getDBO ();
$table = JTable::getInstance ( $option, 'KunenaImporterTable' );
if (!$table) die ("<br />{$option}: Table doesn't exist!");
$query = "TRUNCATE TABLE " . $this->db->nameQuote ( $table->getTableName () );
$this->db->setQuery ( $query );
$result = $this->db->query () or die ( "<br />{$option}: Invalid query:<br />$query<br />" . $this->db->errorMsg () );
}
/*
public function truncateJoomlaUsers() {
// Leave only Super Administrators
$this->db = JFactory::getDBO();
$query="DELETE FROM #__users WHERE gid != 25";
$this->db->setQuery($query);
$result = $this->db->query() or die("<br />Invalid query:<br />$query<br />" . $this->db->errorMsg());
$query="ALTER TABLE `#__users` AUTO_INCREMENT = 0";
$this->db->setQuery($query);
$result = $this->db->query() or die("<br />Invalid query:<br />$query<br />" . $this->db->errorMsg());
$query="DELETE #__core_acl_aro AS a FROM #__core_acl_aro AS a LEFT JOIN #__users AS u ON a.value=u.id WHERE u.id IS NULL";
$this->db->setQuery($query);
$result = $this->db->query() or die("<br />Invalid query:<br />$query<br />" . $this->db->errorMsg());
$query="DELETE FROM #__core_acl_groups_aro_map WHERE group_id != 25";
$this->db->setQuery($query);
$result = $this->db->query() or die("<br />Invalid query:<br />$query<br />" . $this->db->errorMsg());
$query="ALTER TABLE `#__core_acl_aro` AUTO_INCREMENT = 0";
$this->db->setQuery($query);
$result = $this->db->query() or die("<br />Invalid query:<br />$query<br />" . $this->db->errorMsg());
}
*/
public function importData($option, &$data) {
switch ($option) {
case 'config' :
$newConfig = end ( $data );
if (is_object ( $newConfig ))
$newConfig = $newConfig->GetClassVars ();
$kunenaConfig = new KunenaImporterTableConfig ();
$kunenaConfig->save ( $newConfig );
break;
case 'messages' :
$this->importMessages ( $data );
break;
case 'attachments':
$this->importAttachments ( $data );
break;
case 'subscriptions' :
case 'favorites' :
$this->importUnique ( $option, $data );
break;
case 'userprofile':
$this->importUserProfile ( $data );
break;
case 'avatargalleries':
$this->importAvatarGalleries ( $data );
break;
case 'mapusers':
break;
case 'users':
$option = 'extuser';
default :
$this->importDefault ( $option, $data );
}
}
protected function importUnique($option, &$data) {
$table = JTable::getInstance ( $option, 'KunenaImporterTable' );
if (! $table)
die ( $option );
$extids = array();
foreach ( $data as $item ) {
if (!empty($item->userid)) $extids[$item->userid] = $item->userid;
}
$extuser = JTable::getInstance ( 'ExtUser', 'KunenaImporterTable' );
$idmap = $extuser->loadIdMap($extids);
$this->commitStart ();
foreach ( $data as $item ) {
if (!empty($item->userid)) {
$item->userid = $idmap[$item->userid]->id ? $idmap[$item->userid]->id : -$idmap[$item->userid]->extid;
}
if ($table->save ( $item ) === false) {
if (! strstr ( $table->getError (), 'Duplicate entry' ))
die ( "<br />ERROR: " . $table->getError () );
}
}
$this->commitEnd ();
}
protected function importAttachments(&$data) {
$table = JTable::getInstance ( 'attachments', 'KunenaImporterTable' );
if (! $table)
die ( $option );
$extids = array();
foreach ( $data as $item ) {
if (!empty($item->userid)) $extids[$item->userid] = $item->userid;
}
$extuser = JTable::getInstance ( 'ExtUser', 'KunenaImporterTable' );
$idmap = $extuser->loadIdMap($extids);
$this->commitStart ();
foreach ( $data as $item ) {
if (isset($idmap[$item->userid])) {
$item->userid = $idmap[$item->userid]->id ? $idmap[$item->userid]->id : -$idmap[$item->userid]->extid;
}
$item->folder = 'media/kunena/attachments/'.$item->folder;
if (file_exists($item->location)) {
$path = JPATH_ROOT."/{$item->folder}";
// Create upload folder and index.html
if (!JFolder::exists($path) && JFolder::create($path)) {
JFile::write("{$path}/index.html",'<html><body></body></html>');
}
$item->hash = md5_file ( $item->location );
JFile::copy($item->location, "{$path}/{$item->filename}");
}
if ($table->save ( $item ) === false)
die ( "ERROR: " . $table->getError () );
}
$this->commitEnd ();
}
protected function importAvatarGalleries(&$data) {
foreach ( $data as $item=>$path ) {
// Copy gallery
JFolder::copy($path, JPATH_ROOT."/media/kunena/avatars/gallery/{$item}", '', true);
// Create index.html
JFile::write(JPATH_ROOT."/media/kunena/avatars/gallery/{$item}/index.html",'<html><body></body></html>');
}
}
protected function importUserProfile(&$data) {
$table = JTable::getInstance ( 'UserProfile', 'KunenaImporterTable' );
if (! $table)
die ( $option );
$extids = array();
foreach ( $data as $item ) {
if (!empty($item->userid)) $extids[$item->userid] = $item->userid;
}
$extuser = JTable::getInstance ( 'ExtUser', 'KunenaImporterTable' );
$idmap = $extuser->loadIdMap($extids);
$this->commitStart ();
foreach ( $data as $item ) {
if (isset($idmap[$item->userid])) {
$item->userid = $idmap[$item->userid]->id ? $idmap[$item->userid]->id : -$idmap[$item->userid]->extid;
}
if (!empty($item->avatarpath) && file_exists($item->avatarpath)) {
JFile::copy($item->avatarpath, JPATH_ROOT."/media/kunena/avatars/users/{$item->avatar}");
}
if ($table->save ( $item ) === false)
die ( "ERROR: " . $table->getError () );
}
$this->commitEnd ();
}
protected function importDefault($option, &$data) {
$table = JTable::getInstance ( $option, 'KunenaImporterTable' );
if (! $table)
die ( $option );
$extids = array();
foreach ( $data as $item ) {
if (!empty($item->userid)) $extids[$item->userid] = $item->userid;
}
$extuser = JTable::getInstance ( 'ExtUser', 'KunenaImporterTable' );
$idmap = $extuser->loadIdMap($extids);
$this->commitStart ();
foreach ( $data as $item ) {
if (isset($idmap[$item->userid])) {
$item->userid = $idmap[$item->userid]->id ? $idmap[$item->userid]->id : -$idmap[$item->userid]->extid;
}
if ($table->save ( $item ) === false)
die ( "ERROR: " . $table->getError () );
}
$this->commitEnd ();
}
protected function importMessages(&$messages) {
$extids = array();
foreach ( $messages as $message ) {
if (!empty($message->userid)) $extids[$message->userid] = $message->userid;
if (!empty($message->modified_by)) $extids[$message->modified_by] = $message->modified_by;
}
$extuser = JTable::getInstance ( 'ExtUser', 'KunenaImporterTable' );
$idmap = $extuser->loadIdMap($extids);
$this->commitStart ();
foreach ( $messages as $message ) {
$msgtable = JTable::getInstance ( 'messages', 'KunenaImporterTable' );
$txttable = JTable::getInstance ( 'messages_text', 'KunenaImporterTable' );
if ($message->userid) {
if ($idmap[$message->userid]->extid && $idmap[$message->userid]->lastvisitDate < $message->time - 86400) {
// user MUST have been in the forum in the past 24 hours, update last visit..
$extuser = JTable::getInstance ( 'ExtUser', 'KunenaImporterTable' );
$extuser->load($message->userid);
$extuser->lastvisitDate = $idmap[$message->userid]->lastvisitDate =$message->time;
$extuser->save();
}
$message->userid = $idmap[$message->userid]->id ? $idmap[$message->userid]->id : -$idmap[$message->userid]->extid;
}
if ($message->modified_by) {
$message->modified_by = $idmap[$message->modified_by]->id ? $idmap[$message->modified_by]->id : -$idmap[$message->modified_by]->extid;
}
$message->mesid = $message->id;
if ($message->userid > 0 && (empty ( $message->email ) || empty ( $message->name ))) {
$user = JUser::getInstance ( $message->userid );
if (empty ( $message->email ))
$message->email = $user->email;
if (empty ( $message->name ))
$message->name = $user->username;
}
if ($msgtable->save ( $message ) === false)
die ( "ERROR: " . $msgtable->getError () );
if ($txttable->save ( $message ) === false)
die ( "ERROR: " . $txttable->getError () );
}
$this->commitEnd ();
$this->updateCatStats ();
}
public function updateUserData($oldid, $newid, $replace = false) {
if ($replace) {
$this->db->setQuery ( "DELETE FROM `#__kunena_users` WHERE `userid` = {$this->db->quote($oldid)}" );
$this->db->query ();
}
$queries[] = "UPDATE `#__kunena_users` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_attachments` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_favorites` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_messages` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_messages` SET `modified_by` = {$this->db->quote($newid)} WHERE `modified_by` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_moderation` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_polls_users` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
//$queries[] = "UPDATE `#__kunena_sessions` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_subscriptions` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_subscriptions_categories` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_thankyou` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_thankyou` SET `targetuserid` = {$this->db->quote($newid)} WHERE `targetuserid` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_users_banned` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_users_banned` SET `created_by` = {$this->db->quote($newid)} WHERE `created_by` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_users_banned` SET `modified_by` = {$this->db->quote($newid)} WHERE `modified_by` = {$this->db->quote($oldid)}";
$queries[] = "UPDATE `#__kunena_whoisonline` SET `userid` = {$this->db->quote($newid)} WHERE `userid` = {$this->db->quote($oldid)}";
foreach ($queries as $query) {
$this->db->setQuery ( $query );
$this->db->query ();
if ($this->db->getErrorNum()) {
$app = JFactory::getApplication ();
$app->enqueueMessage ( "WARNING: Userid {$newid} is already in use! Cannot map user to it." );
return false;
}
}
return true;
}
}