Passed
Pull Request — master (#117)
by Spuds
07:11
created

SMF2_0   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 176
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1
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_0
12
 */
13
class SMF2_0 extends Importers\AbstractSourceImporter
14
{
15
	protected $setting_file = '/Settings.php';
16
17
	protected $smf_attach_folders = null;
18
19
	protected $_is_nibogo_like = null;
20
21
	public function getName()
22
	{
23
		return 'SMF2_0';
24
	}
25
26
	public function getVersion()
27
	{
28
		return 'ElkArte 1.0';
29
	}
30
31
	public function setDefines()
32
	{
33
		if (!defined('SMF'))
34
			define('SMF', 1);
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
			$content = file_get_contents($this->path . '/Settings.php');
61
62
		$match = array();
63
		preg_match('~\$' . $name . '\s*=\s*\'(.*?)\';~', $content, $match);
64
65
		return isset($match[1]) ? $match[1] : '';
66
	}
67
68
	/**
69
	 * Read the attachment directory structure from the source db
70
	 *
71
	 * @return array|null
72
	 */
73
	public function getAttachmentDirs()
74
	{
75
		if ($this->smf_attach_folders === null)
0 ignored issues
show
Bug Best Practice introduced by
The property smf_attach_folders does not exist on SMF2_0. Did you maybe forget to declare it?
Loading history...
76
		{
77
			$from_prefix = $this->config->from_prefix;
78
79
			$request = $this->db->query("
80
				SELECT value
81
				FROM {$from_prefix}settings
82
				WHERE variable='attachmentUploadDir';");
83
			list ($smf_attachments_dir) = $this->db->fetch_row($request);
84
85
			$this->smf_attach_folders = @unserialize($smf_attachments_dir);
86
87
			if (!is_array($this->smf_attach_folders))
88
				$this->smf_attach_folders = array(1 => $smf_attachments_dir);
89
		}
90
91
		return $this->smf_attach_folders;
92
	}
93
94
	/**
95
	 * Import likes from one of the known addons
96
	 *
97
	 * @return array
98
	 */
99
	public function fetchLikes()
100
	{
101
		if ($this->isNibogo())
0 ignored issues
show
Bug introduced by
The method isNibogo() does not exist on SMF2_0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
		if ($this->/** @scrutinizer ignore-call */ isNibogo())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
			return $this->fetchNibogo();
0 ignored issues
show
Bug introduced by
The method fetchNibogo() does not exist on SMF2_0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
			return $this->/** @scrutinizer ignore-call */ fetchNibogo();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
		else
104
			return $this->fetchIllori();
0 ignored issues
show
Bug introduced by
The method fetchIllori() does not exist on SMF2_0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
			return $this->/** @scrutinizer ignore-call */ fetchIllori();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
	}
106
107
	/**
108
	 * Nibogo version of likes
109
	 *
110
	 * @return array
111
	 */
112
	protected function fetchNibogo()
113
	{
114
		$from_prefix = $this->config->from_prefix;
115
116
		$request = $this->db->query("
117
			SELECT l.id_member, t.id_first_msg, t.id_member_started
118
			FROM {$from_prefix}likes
119
				INNER JOIN {$from_prefix}topics AS t ON (t.id_topic = l.id_topic)");
120
		$return = array();
121
		while ($row = $this->db->fetch_assoc($request))
122
			$return[] = array(
123
				'id_member' => $row['id_member'],
124
				'id_msg' => $row['id_first_msg'],
125
				'id_poster' => $row['id_member_started'],
126
				'like_timestamp' => 0,
127
			);
128
		$this->db->free_result($request);
129
130
		return $return;
131
	}
132
133
	/**
134
	 * Illori version of likes
135
	 *
136
	 * @return array
137
	 */
138
	protected function fetchIllori()
139
	{
140
		$from_prefix = $this->config->from_prefix;
141
142
		$request = $this->db->query("
143
			SELECT l.id_member, l.id_message, m.id_member as id_poster
144
			FROM {$from_prefix}likes AS l
145
				INNER JOIN {$from_prefix}messages AS m ON (m.id_msg = l.id_message)");
146
		$return = array();
147
		while ($row = $this->db->fetch_assoc($request))
148
			$return[] = array(
149
				'id_member' => $row['id_member'],
150
				'id_msg' => $row['id_message'],
151
				'id_poster' => $row['id_poster'],
152
				'like_timestamp' => 0,
153
			);
154
		$this->db->free_result($request);
155
156
		return $return;
157
	}
158
159
	/**
160
	 * Figure out which likes system may be in use
161
	 *
162
	 * @return bool|null
163
	 */
164
	protected function isNibogo()
165
	{
166
		$from_prefix = $this->config->from_prefix;
167
168
		if ($this->_is_nibogo_like !== null)
0 ignored issues
show
Bug Best Practice introduced by
The property _is_nibogo_like does not exist on SMF2_0. Did you maybe forget to declare it?
Loading history...
169
			return $this->_is_nibogo_like;
170
171
		$request = $this->db->query("
172
			SHOW COLUMNS
173
			FROM {$from_prefix}likes");
174
		while ($row = $this->db->fetch_assoc($request))
175
		{
176
			// This is Nibogo
177
			if ($row['Field'] == 'id_topic')
178
			{
179
				$this->_is_nibogo_like = true;
180
				return $this->_is_nibogo_like;
181
			}
182
		}
183
184
		// Not Nibogo means Illori
185
		$this->_is_nibogo_like = false;
186
		return $this->_is_nibogo_like;
187
	}
188
}
189
190
/**
191
 * Copy attachments from the source to our destination
192
 *
193
 * @param array $row
194
 * @param \OpenImporter\Database $db
195
 * @param string $from_prefix
196
 * @param string $attachmentUploadDir
197
 */
198
function moveAttachment(&$row, $db, $from_prefix, $attachmentUploadDir)
199
{
200
	static $smf_folders = null;
201
202
	// We need to know where the attachments are located
203
	if ($smf_folders === null)
204
	{
205
		$request = $db->query("
206
			SELECT value
207
			FROM {$from_prefix}settings
208
			WHERE variable='attachmentUploadDir';");
209
		list ($smf_attachments_dir) = $db->fetch_row($request);
210
211
		$smf_folders = @unserialize($smf_attachments_dir);
212
213
		if (!is_array($smf_folders))
214
			$smf_folders = array(1 => $smf_attachments_dir);
215
	}
216
217
	// If something is broken, better try to account for it as well.
218
	if (isset($smf_folders[$row['id_folder']]))
219
		$smf_attachments_dir = $smf_folders[$row['id_folder']];
220
	else
221
		$smf_attachments_dir = $smf_folders[1];
222
223
	// Missing the file hash ... create one
224
	if (empty($row['file_hash']))
225
	{
226
		$row['file_hash'] = createAttachmentFileHash($row['filename']);
227
		$source_file = $row['filename'];
228
	}
229
	else
230
		$source_file = $row['id_attach'] . '_' . $row['file_hash'];
231
232
	// Copy it over
233
	copy_file($smf_attachments_dir . '/' . $source_file, $attachmentUploadDir . '/' . $row['id_attach'] . '_' . $row['file_hash'] . '.elk');
234
}
235