wedge0_1_importer::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
 * This file contains code based on:
10
 *
11
 * Simple Machines Forum (SMF)
12
 * copyright:    2011 Simple Machines (http://www.simplemachines.org)
13
 * license:    BSD, See included LICENSE.TXT for terms and conditions.
14
 *
15
 * Copyright (c) 2014, Thorsten Eurich and René-Gilles Deberdt
16
 * All rights reserved.
17
 */
18
19
/**
20
 * Class wedge0_1_importer
21
 * The class contains code that allows the Importer to obtain settings
22
 * from the Wedge installation.
23
 */
24
class wedge0_1_importer extends Importers\SmfCommonSource
25
{
26
	public $attach_extension = 'ext';
27
28
	public function getName()
29
	{
30
		return 'Wedge 0.1';
31
	}
32
}
33
34
/**
35
 * Class wedge0_1_importer_step1
36
 */
37
class wedge0_1_importer_step1 extends Importers\SmfCommonSourceStep1
38
{
39
	public function doSpecialTable($special_table, $params = null)
40
	{
41
		// If there is an IP, better convert it to "something"
42
		$params = $this->doIpConvertion($params);
43
		$params = $this->doIpPointer($params);
44
45
		return parent::doSpecialTable($special_table, $params);
46
	}
47
48
	protected function doIpConvertion($row)
49
	{
50
		$convert_ips = array('member_ip', 'member_ip2');
51
52
		foreach ($convert_ips as $ip)
53
		{
54
			if (array_key_exists($ip, $row))
55
			{
56
				$row[$ip] = $this->_prepare_ipv6($row[$ip]);
57
			}
58
		}
59
60
		return $row;
61
	}
62
63
	protected function doIpPointer($row)
64
	{
65
		$to_prefix = $this->config->to_prefix;
66
		$ips_to_pointer = array('poster_ip');
67
68
		foreach ($ips_to_pointer as $ip)
69
		{
70
			if (array_key_exists($ip, $row))
71
			{
72
				$ipv6ip = $this->_prepare_ipv6($row[$ip]);
73
74
				$request2 = $this->db->query("
75
					SELECT id_ip
76
					FROM {$to_prefix}log_ips
77
					WHERE member_ip = '" . $ipv6ip . "'
78
					LIMIT 1");
79
80
				// IP already known?
81
				if ($this->db->num_rows($request2) != 0)
82
				{
83
					list ($id_ip) = $this->db->fetch_row($request2);
84
					$row[$ip] = $id_ip;
85
				}
86
				// insert the new ip
87
				else
88
				{
89
					$this->db->query("
90
						INSERT INTO {$to_prefix}log_ips
91
							(member_ip)
92
						VALUES ('$ipv6ip')");
93
94
					$pointer = $this->db->insert_id();
95
					$row[$ip] = $pointer;
96
				}
97
98
				$this->db->free_result($request2);
99
			}
100
		}
101
102
		return $row;
103
	}
104
105
	/**
106
	 * Placeholder function to convert IPV4 to IPV6
107
	 *
108
	 * @todo convert IPV4 to IPV6
109
	 * @todo move to source file, because it depends on the source for any specific destination
110
	 *
111
	 * @param string $ip
112
	 *
113
	 * @return string $ip
114
	 */
115
	private function _prepare_ipv6($ip)
116
	{
117
		return $ip;
118
	}
119
}
120
121
/**
122
 * Class wedge0_1_importer_step2
123
 */
124
class wedge0_1_importer_step2 extends Importers\SmfCommonSourceStep2
125
{
126
	public function substep0()
127
	{
128
		$to_prefix = $this->config->to_prefix;
129
130
		// Get all members with wrong number of personal messages.
131
		$request = $this->db->query("
132
			SELECT mem.id_member, COUNT(pmr.id_pm) AS real_num, mem.instant_messages
133
			FROM {$to_prefix}members AS mem
134
				LEFT JOIN {$to_prefix}pm_recipients AS pmr ON (mem.id_member = pmr.id_member AND pmr.deleted = 0)
135
			GROUP BY mem.id_member
136
			HAVING real_num != instant_messages");
137
		while ($row = $this->db->fetch_assoc($request))
138
		{
139
			$this->db->query("
140
				UPDATE {$to_prefix}members
141
				SET instant_messages = $row[real_num]
142
				WHERE id_member = $row[id_member]
143
				LIMIT 1");
144
145
			pastTime(0);
146
		}
147
		$this->db->free_result($request);
148
149
		$request = $this->db->query("
150
			SELECT mem.id_member, COUNT(pmr.id_pm) AS real_num, mem.unread_messages
151
			FROM {$to_prefix}members AS mem
152
				LEFT JOIN {$to_prefix}pm_recipients AS pmr ON (mem.id_member = pmr.id_member AND pmr.deleted = 0 AND pmr.is_read = 0)
153
			GROUP BY mem.id_member
154
			HAVING real_num != unread_messages");
155
		while ($row = $this->db->fetch_assoc($request))
156
		{
157
			$this->db->query("
158
				UPDATE {$to_prefix}members
159
				SET unread_messages = $row[real_num]
160
				WHERE id_member = $row[id_member]
161
				LIMIT 1");
162
163
			pastTime(0);
164
		}
165
		$this->db->free_result($request);
166
	}
167
168
	public function substep12()
169
	{
170
		$to_prefix = $this->config->to_prefix;
171
172
		$indexes = array(
173
			'id_topic' => array(
174
				'name' => 'id_topic',
175
				'columns' => array('id_topic'),
176
				'type' => 'primary',
177
			),
178
			'last_message' => array(
179
				'name' => 'last_message',
180
				'columns' => array('id_last_msg', 'id_board'),
181
				'type' => 'unique',
182
			),
183
			'first_message' => array(
184
				'name' => 'first_message',
185
				'columns' => array('id_first_msg', 'id_board'),
186
				'type' => 'unique',
187
			),
188
			'poll' => array(
189
				'name' => 'poll',
190
				'columns' => array('ID_POLL', 'id_topic'),
191
				'type' => 'unique',
192
			),
193
			'is_pinned' => array(
194
				'name' => 'is_pinned',
195
				'columns' => array('is_pinned'),
196
				'type' => 'key',
197
			),
198
			'id_board' => array(
199
				'name' => 'id_board',
200
				'columns' => array('id_board'),
201
				'type' => 'key',
202
			),
203
			'member_started' => array(
204
				'name' => 'member_started',
205
				'columns' => array('id_member_started', 'id_board'),
206
				'type' => 'key',
207
			),
208
			'last_message_pinned' => array(
209
				'name' => 'last_message_pinned',
210
				'columns' => array('id_board', 'is_pinned', 'id_last_msg'),
211
				'type' => 'key',
212
			),
213
			'board_news' => array(
214
				'name' => 'board_news',
215
				'columns' => array('id_board', 'id_first_msg'),
216
				'type' => 'key',
217
			),
218
		);
219
220
		foreach ($indexes as $index_info)
221
		{
222
			$this->db->alter_table("{$to_prefix}topics", $index_info);
0 ignored issues
show
Bug introduced by
The method alter_table() does not exist on OpenImporter\Database. ( Ignorable by Annotation )

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

222
			$this->db->/** @scrutinizer ignore-call */ 
223
              alter_table("{$to_prefix}topics", $index_info);

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...
223
		}
224
225
		$_REQUEST['start'] = 0;
226
		pastTime(13);
227
	}
228
229
	public function substep13()
230
	{
231
		$indexes = array(
232
			'id_msg' => array(
233
				'name' => 'id_msg',
234
				'columns' => array('id_msg'),
235
				'type' => 'primary',
236
			),
237
			'id_topic' => array(
238
				'name' => 'id_topic',
239
				'columns' => array('id_topic', 'id_msg'),
240
				'type' => 'unique',
241
			),
242
			'id_board' => array(
243
				'name' => 'id_board',
244
				'columns' => array('id_board', 'id_msg'),
245
				'type' => 'unique',
246
			),
247
			'id_member' => array(
248
				'name' => 'id_member',
249
				'columns' => array('id_member', 'id_msg'),
250
				'type' => 'unique',
251
			),
252
			'ip_index' => array(
253
				'name' => 'ip_index',
254
				'columns' => array('poster_ip(15)', 'id_topic'),
255
				'type' => 'key',
256
			),
257
			'participation' => array(
258
				'name' => 'participation',
259
				'columns' => array('id_member', 'id_topic'),
260
				'type' => 'key',
261
			),
262
			'show_posts' => array(
263
				'name' => 'show_posts',
264
				'columns' => array('id_member', 'id_board'),
265
				'type' => 'key',
266
			),
267
			'id_topic' => array(
268
				'name' => 'id_topic',
269
				'columns' => array('id_topic'),
270
				'type' => 'key',
271
			),
272
			'id_member_msg' => array(
273
				'name' => 'id_member_msg',
274
				'columns' => array('id_member', 'approved', 'id_msg'),
275
				'type' => 'key',
276
			),
277
			'current_topic' => array(
278
				'name' => 'current_topic',
279
				'columns' => array('id_topic', 'id_msg', 'id_member', 'approved'),
280
				'type' => 'key',
281
			),
282
		);
283
284
		foreach ($indexes as $index_info)
285
		{
286
			$this->db->alter_table("{$to_prefix}messages", $index_info);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $to_prefix seems to be never defined.
Loading history...
287
		}
288
289
		$_REQUEST['start'] = 0;
290
		pastTime(14);
291
	}
292
}
293
294
/**
295
 * Class wedge0_1_importer_step3
296
 */
297
class wedge0_1_importer_step3 extends Importers\SmfCommonSourceStep3
298
{
299
	public function run($import_script)
300
	{
301
		$to_prefix = $this->config->to_prefix;
302
303
		// add some importer information.
304
		$this->db->query("
305
			REPLACE INTO {$to_prefix}settings (variable, value)
306
				VALUES ('import_time', " . time() . "),
307
					('enable_password_conversion', '1'),
308
					('imported_from', '" . $import_script . "')");
309
	}
310
}