Passed
Push — master ( 655649...07d5b2 )
by Spuds
06:48 queued 05:03
created

SMF2_1   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 40
c 1
b 0
f 1
dl 0
loc 110
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getDbName() 0 3 1
A fetchLikes() 0 20 2
A setDefines() 0 5 2
A getVersion() 0 3 1
A getPrefix() 0 6 1
A getTableTest() 0 3 1
A fetchSetting() 0 13 3
A getName() 0 3 1
A getAttachmentDirs() 0 21 3
1
<?php
2
/**
3
 * @name      OpenImporter
4
 * @copyright OpenImporter contributors
5
 * @license   BSD https://opensource.org/licenses/BSD-3-Clause
6
 *
7
 * @version 1.0
8
 */
9
10
/**
11
 * Class SMF2_1
12
 */
13
class SMF2_1 extends Importers\AbstractSourceImporter
14
{
15
	protected $setting_file = '/Settings.php';
16
17
	protected $smf_attach_folders = null;
18
19
	public function getName()
20
	{
21
		return 'SMF2_1';
22
	}
23
24
	public function getVersion()
25
	{
26
		return 'ElkArte 1.0';
27
	}
28
29
	public function setDefines()
30
	{
31
		if (!defined('SMF'))
32
		{
33
			define('SMF', 1);
34
		}
35
	}
36
37
	public function getPrefix()
38
	{
39
		$db_name = $this->getDbName();
40
		$db_prefix = $this->fetchSetting('db_prefix');
41
42
		return '`' . $db_name . '`.' . $db_prefix;
43
	}
44
45
	public function getDbName()
46
	{
47
		return $this->fetchSetting('db_name');
48
	}
49
50
	public function getTableTest()
51
	{
52
		return 'members';
53
	}
54
55
	public function fetchSetting($name)
56
	{
57
		static $content = null;
58
59
		if ($content === null)
60
		{
61
			$content = file_get_contents($this->path . '/Settings.php');
62
		}
63
64
		$match = array();
65
		preg_match('~\$' . $name . '\s*=\s*\'(.*?)\';~', $content, $match);
66
67
		return isset($match[1]) ? $match[1] : '';
68
	}
69
70
	/**
71
	 * Read the attachment directory structure from the source db
72
	 *
73
	 * @return array|null
74
	 */
75
	public function getAttachmentDirs()
76
	{
77
		if ($this->smf_attach_folders === null)
78
		{
79
			$from_prefix = $this->config->from_prefix;
80
81
			$request = $this->db->query("
82
				SELECT value
83
				FROM {$from_prefix}settings
84
				WHERE variable='attachmentUploadDir';");
85
			list ($smf_attachments_dir) = $this->db->fetch_row($request);
86
87
			$this->smf_attach_folders = @unserialize($smf_attachments_dir);
88
89
			if (!is_array($this->smf_attach_folders))
90
			{
91
				$this->smf_attach_folders = array(1 => $smf_attachments_dir);
92
			}
93
		}
94
95
		return $this->smf_attach_folders;
96
	}
97
98
	/**
99
	 * Import likes from the 2.1 table
100
	 *
101
	 * @return array
102
	 */
103
	public function fetchLikes()
104
	{
105
		$from_prefix = $this->config->from_prefix;
106
107
		$request = $this->db->query("
108
			SELECT l.id_member, l.content_id AS id_msg, m.id_member AS id_poster, l.like_time AS like_timestamp
109
			FROM {$from_prefix}user_likes AS l
110
				INNER JOIN {$from_prefix}messages AS m ON (m.id_msg = l.content_id)
111
			WHERE content_type = 'msg'");
112
		$return = array();
113
		while ($row = $this->db->fetch_assoc($request))
114
			$return[] = array(
115
				'id_member' => $row['id_member'],
116
				'id_msg' => $row['id_msg'],
117
				'id_poster' => $row['id_poster'],
118
				'like_timestamp' => $row['like_timestamp'],
119
			);
120
		$this->db->free_result($request);
121
122
		return $return;
123
	}
124
}
125
126
/**
127
 * Copy attachments from the source to our destination
128
 *
129
 * @param array $row
130
 * @param \OpenImporter\Database $db
131
 * @param string $from_prefix
132
 * @param string $attachmentUploadDir
133
 */
134
function moveAttachment(&$row, $db, $from_prefix, $attachmentUploadDir)
135
{
136
	static $smf_folders = null;
137
138
	// We need to know where the attachments are located
139
	if ($smf_folders === null)
140
	{
141
		$request = $db->query("
142
			SELECT value
143
			FROM {$from_prefix}settings
144
			WHERE variable='attachmentUploadDir';");
145
		list ($smf_attachments_dir) = $db->fetch_row($request);
146
147
		$smf_folders = @unserialize($smf_attachments_dir);
148
149
		if (!is_array($smf_folders))
150
		{
151
			$smf_folders = array(1 => $smf_attachments_dir);
152
		}
153
	}
154
155
	// If something is broken, better try to account for it as well.
156
	if (isset($smf_folders[$row['id_folder']]))
157
	{
158
		$smf_attachments_dir = $smf_folders[$row['id_folder']];
159
	}
160
	else
161
	{
162
		$smf_attachments_dir = $smf_folders[1];
163
	}
164
165
	// Missing the file hash ... create one
166
	if (empty($row['file_hash']))
167
	{
168
		$row['file_hash'] = createAttachmentFileHash($row['filename']);
169
		$source_file = $row['filename'];
170
	}
171
	else
172
	{
173
		$source_file = $row['id_attach'] . '_' . $row['file_hash'] . '.dat';
174
	}
175
176
	// Copy it over
177
	copy_file($smf_attachments_dir . '/' . $source_file, $attachmentUploadDir . '/' . $row['id_attach'] . '_' . $row['file_hash'] . '.elk');
178
}
179
180
/**
181
 * @param array $row
182
 * @param \OpenImporter\Database $db
183
 * @param string $from_prefix
184
 */
185
function cust_fields(&$row, $db, $from_prefix)
186
{
187
	$request = $db->query("
188
		SELECT variable, value
189
		FROM {$from_prefix}themes
190
		WHERE id_member = $row[id_member]");
191
192
	while ($profile = $db->fetch_assoc($request))
193
	{
194
		if ($profile['variable'] === 'cust_loca')
195
			$row['location'] = $profile['value'];
196
197
		if ($profile['variable'] === 'cust_gender')
198
		{
199
			if ($profile['value'] === 'Male')
200
				$row['gender'] = 1;
201
			if ($profile['value'] === 'Female')
202
				$row['gender'] = 2;
203
		}
204
	}
205
	$db->free_result($request);
206
}
207