Passed
Push — development ( 2bb3b4...48f606 )
by Emanuele
53s
created

AbstractSourceSmfImporter::codeAvatars()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 67
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 67
rs 6.6523
cc 8
eloc 38
nc 24
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @name      OpenImporter
4
 * @copyright OpenImporter contributors
5
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
6
 *
7
 * @version 2.0 Alpha
8
 */
9
10
namespace OpenImporter\Importers;
11
12
use OpenImporter\Core\Files;
13
14
/**
15
 * Class AbstractSourceSmfImporter
16
 * This abstract class is the base for any php importer file.
17
 *
18
 * It provides some common necessary methods and some default properties
19
 * so that Importer can do its job without having to test for existence
20
 * of methods every two/three lines of code.
21
 *
22
 * @package OpenImporter\Importers
23
 */
24
abstract class AbstractSourceSmfImporter extends AbstractSourceImporter
25
{
26
	protected $setting_file = '/Settings.php';
27
28
	public function getDbPrefix()
29
	{
30
		return $this->fetchSetting('db_prefix');
31
	}
32
33
	protected function fetchSetting($name)
34
	{
35
		$content = $this->readSettingsFile();
36
37
		$match = array();
38
		preg_match('~\$' . $name . '\s*=\s*\'(.*?)\';~', $content, $match);
39
40
		return isset($match[1]) ? $match[1] : '';
41
	}
42
43
	public function setDefines()
44
	{
45
		if (!defined('SMF'))
46
		{
47
			define('SMF', 1);
48
		}
49
	}
50
51
	public function dbConnectionData()
52
	{
53
		if ($this->path === null)
54
		{
55
			return false;
56
		}
57
58
		return array(
59
			'dbname' => $this->fetchSetting('db_name'),
60
			'user' => $this->fetchSetting('db_user'),
61
			'password' => $this->fetchSetting('db_passwd'),
62
			'host' => $this->fetchSetting('db_server'),
63
			'driver' => $this->fetchDriver(),
64
			'test_table' => $this->getTableTest(),
65
			'system_name' => $this->getname(),
66
		);
67
	}
68
69
	protected function fetchDriver()
70
	{
71
		$type = $this->fetchSetting('db_type');
72
		$drivers = array(
73
			'mysql' => 'pdo_mysql',
74
			'mysqli' => 'pdo_mysql',
75
			'postgresql' => 'pdo_pgsql',
76
			'sqlite' => 'pdo_sqlite',
77
		);
78
79
		return isset($drivers[$type]) ? $drivers[$type] : 'pdo_mysql';
80
	}
81
82
	protected function getTableTest()
83
	{
84
		return '{db_prefix}members';
85
	}
86
87
	public function getDbName()
88
	{
89
		return $this->fetchSetting('db_name');
90
	}
91
92
	public function preparseBoards($originalRows)
93
	{
94
		$rows = array();
95
96
		foreach ($originalRows as $row)
97
		{
98
			$memberGroups = array_filter(explode(',', $row['member_groups']));
99
			$groups = array();
100
			foreach ($memberGroups as $group)
101
			{
102
				$groups[] = $this->mapBoardsGroups($group);
103
			}
104
105
			$row['member_groups'] = implode(',', $groups);
106
107
			$rows[] = $row;
108
		}
109
110
		return $rows;
111
	}
112
113
	protected function mapBoardsGroups($group)
114
	{
115
		$known = array(
116
			-1 => -1,
117
			0 => 0,
118
			1 => 1,
119
			3 => 2,
120
		);
121
122
		return isset($known[$group]) ? $known[$group] : $group + 10;
123
	}
124
125
	public function codeSettings()
126
	{
127
		// @todo this list looks broken (I don't remember any enablePinnedTopics in SMF 1.1)
128
		$do_import = array(
129
			'news',
130
			'compactTopicPagesContiguous',
131
			'compactTopicPagesEnable',
132
			'enablePinnedTopics',
133
			'todayMod',
134
			'enablePreviousNext',
135
			'pollMode',
136
			'enableVBStyleLogin',
137
			'enableCompressedOutput',
138
			'attachmentSizeLimit',
139
			'attachmentPostLimit',
140
			'attachmentNumPerPostLimit',
141
			'attachmentDirSizeLimit',
142
			'attachmentExtensions',
143
			'attachmentCheckExtensions',
144
			'attachmentShowImages',
145
			'attachmentEnable',
146
			'attachmentEncryptFilenames',
147
			'attachmentThumbnails',
148
			'attachmentThumbWidth',
149
			'attachmentThumbHeight',
150
			'censorIgnoreCase',
151
			'mostOnline',
152
			'mostOnlineToday',
153
			'mostDate',
154
			'allow_disableAnnounce',
155
			'trackStats',
156
			'userLanguage',
157
			'titlesEnable',
158
			'topicSummaryPosts',
159
			'enableErrorLogging',
160
			'max_image_width',
161
			'max_image_height',
162
			'onlineEnable',
163
			'smtp_host',
164
			'smtp_port',
165
			'smtp_username',
166
			'smtp_password',
167
			'mail_type',
168
			'timeLoadPageEnable',
169
			'totalMembers',
170
			'totalTopics',
171
			'totalMessages',
172
			'simpleSearch',
173
			'censor_vulgar',
174
			'censor_proper',
175
			'enablePostHTML',
176
			'enableEmbeddedFlash',
177
			'xmlnews_enable',
178
			'xmlnews_maxlen',
179
			'hotTopicPosts',
180
			'hotTopicVeryPosts',
181
			'registration_method',
182
			'send_validation_onChange',
183
			'send_welcomeEmail',
184
			'allow_editDisplayName',
185
			'allow_hideOnline',
186
			'guest_hideContacts',
187
			'spamWaitTime',
188
			'pm_spam_settings',
189
			'reserveWord',
190
			'reserveCase',
191
			'reserveUser',
192
			'reserveName',
193
			'reserveNames',
194
			'autoLinkUrls',
195
			'banLastUpdated',
196
			'avatar_max_height_external',
197
			'avatar_max_width_external',
198
			'avatar_action_too_large',
199
			'avatar_max_height_upload',
200
			'avatar_max_width_upload',
201
			'avatar_resize_upload',
202
			'avatar_download_png',
203
			'failed_login_threshold',
204
			'oldTopicDays',
205
			'edit_wait_time',
206
			'edit_disable_time',
207
			'autoFixDatabase',
208
			'allow_guestAccess',
209
			'time_format',
210
			'number_format',
211
			'enableBBC',
212
			'max_messageLength',
213
			'signature_settings',
214
			'autoOptMaxOnline',
215
			'defaultMaxMessages',
216
			'defaultMaxTopics',
217
			'defaultMaxMembers',
218
			'enableParticipation',
219
			'recycle_enable',
220
			'recycle_board',
221
			'maxMsgID',
222
			'enableAllMessages',
223
			'fixLongWords',
224
			'who_enabled',
225
			'time_offset',
226
			'cookieTime',
227
			'lastActive',
228
			'requireAgreement',
229
			'unapprovedMembers',
230
			'package_make_backups',
231
			'databaseSession_enable',
232
			'databaseSession_loose',
233
			'databaseSession_lifetime',
234
			'search_cache_size',
235
			'search_results_per_page',
236
			'search_weight_frequency',
237
			'search_weight_age',
238
			'search_weight_length',
239
			'search_weight_subject',
240
			'search_weight_first_message',
241
			'search_max_results',
242
			'search_floodcontrol_time',
243
			'permission_enable_deny',
244
			'permission_enable_postgroups',
245
			'mail_next_send',
246
			'mail_recent',
247
			'settings_updated',
248
			'next_task_time',
249
			'warning_settings',
250
			'admin_features',
251
			'last_mod_report_action',
252
			'pruningOptions',
253
			'cache_enable',
254
			'reg_verification',
255
			'enable_buddylist',
256
			'birthday_email',
257
			'globalCookies',
258
			'default_timezone',
259
			'memberlist_updated',
260
			'latestMember',
261
			'latestRealName',
262
			'db_mysql_group_by_fix',
263
			'rand_seed',
264
			'mostOnlineUpdated',
265
			'search_pointer',
266
			'spider_name_cache',
267
			'modlog_enabled',
268
			'disabledBBC',
269
			'latest_member',
270
			'latest_real_name',
271
			'total_members',
272
			'total_messages',
273
			'max_msg_id',
274
			'total_topics',
275
			'disable_hash_time',
276
			'latestreal_name',
277
			'disableHashTime',
278
		);
279
280
		$request = $this->db->query("
281
			SELECT variable, value
282
			FROM {$this->config->from_prefix}settings;");
283
		$rows = array();
284
		while ($row = $this->db->fetch_assoc($request))
285
		{
286
			if (in_array($row['variable'], $do_import))
287
			{
288
				$rows[] = array(
289
					'variable' => $row['variable'],
290
					'value' => $row['value'],
291
				);
292
			}
293
		}
294
		$this->db->free_result($request);
295
296
		return $rows;
297
	}
298
299
	public function preparseMembers($originalRows)
300
	{
301
		$rows = array();
302
		foreach ($originalRows as $row)
303
		{
304
			$row['date_registered'] = date('Y-m-d G:i:s', $row['date_registered']);
305
			$rows[] = $row;
306
		}
307
308
		return $rows;
309
	}
310
311
	public function codeAvatars()
312
	{
313
		$rows = array();
314
315
		$avatarg = $this->db->query("
316
			SELECT value
317
			FROM {$this->config->from_prefix}settings
318
			WHERE variable = 'avatar_directory';");
319
		list ($smf_avatarg) = $this->db->fetch_row($avatarg);
320
		$this->db->free_result($avatarg);
321
322
		$avatar_gallery = array();
323
		if (!empty($smf_avatarg) && file_exists($smf_avatarg))
324
		{
325
			$avatar_gallery = Files::get_files_recursive($smf_avatarg);
326
		}
327
328
		foreach ($avatar_gallery as $file)
329
		{
330
			$file = str_replace('\\', '/', $file);
331
			$rows[] = array(
332
				'id_member' => 0,
333
				'basedir' => $smf_avatarg,
334
				'full_path' => dirname($file),
335
				'filename' => basename($file),
336
				'type' => 'gallery',
337
			);
338
		}
339
340
		$avatarg = $this->db->query("
341
			SELECT value
342
			FROM {$this->config->from_prefix}settings
343
			WHERE variable = 'custom_avatar_dir';");
344
		list ($smf_avatarg) = $this->db->fetch_row($avatarg);
345
		$this->db->free_result($avatarg);
346
347
		$avatar_custom = array();
348
		if (!empty($smf_avatarg) && file_exists($smf_avatarg))
349
		{
350
			$avatar_custom = Files::get_files_recursive($smf_avatarg);
351
		}
352
353
		foreach ($avatar_custom as $file)
354
		{
355
			$file = str_replace('\\', '/', $file);
356
			preg_match('~avatar_(\d+)_\d+~i', $file, $match);
357
358
			if (!empty($match[1]))
359
			{
360
				$id_member = $match[1];
361
			}
362
			else
363
			{
364
				$id_member = 0;
365
			}
366
367
			$rows[] = array(
368
				'id_member' => $id_member,
369
				'basedir' => $smf_avatarg,
370
				'full_path' => dirname($file),
371
				'filename' => basename($file),
372
				'type' => 'upload',
373
			);
374
		}
375
376
		return $rows;
377
	}
378
379
	public function codeCopysmiley()
380
	{
381
		$request = $this->db->query("
382
			SELECT value
383
			FROM {$this->config->from_prefix}settings
384
			WHERE variable = 'smileys_dir';");
385
		list ($smf_smileys_dir) = $this->db->fetch_row($request);
386
387
		if (!empty($smf_smileys_dir) && file_exists($smf_smileys_dir))
388
		{
389
			$smf_smileys_dir = str_replace('\\', '/', $smf_smileys_dir);
390
			$smiley = array();
391
			$files = Files::get_files_recursive($smf_smileys_dir);
392
393
			foreach ($files as $file)
394
			{
395
				$file = str_replace('\\', '/', $file);
396
				$smiley[] = array(
397
					'basedir' => $smf_smileys_dir,
398
					'full_path' => dirname($file),
399
					'filename' => basename($file),
400
				);
401
			}
402
403
			if (!empty($smiley))
404
			{
405
				return array($smiley);
406
			}
407
			else
408
			{
409
				return false;
410
			}
411
		}
412
		else
413
		{
414
			return false;
415
		}
416
	}
417
}