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

Wedge1_0::setDefines()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
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
10
/**
11
 * Class Wedge1_0
12
 */
13
class Wedge1_0 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 'Wedge 1.0';
22
	}
23
24
	public function getVersion()
25
	{
26
		return 'ElkArte 1.0';
27
	}
28
29
	public function setDefines()
30
	{
31
		if (!defined('WEDGE'))
32
			define('WEDGE', 1);
33
	}
34
35
	public function getPrefix()
36
	{
37
		$db_name = $this->getDbName();
38
		$db_prefix = $this->fetchSetting('db_prefix');
39
40
		return '`' . $db_name . '`.' . $db_prefix;
41
	}
42
43
	public function getDbName()
44
	{
45
		return $this->fetchSetting('db_name');
46
	}
47
48
	public function getTableTest()
49
	{
50
		return 'members';
51
	}
52
53
	public function fetchSetting($name)
54
	{
55
		static $content = null;
56
57
		if ($content === null)
58
			$content = file_get_contents($this->path . $this->setting_file);
59
60
		$match = array();
61
		preg_match('~\$' . $name . '\s*=\s*\'(.*?)\';~', $content, $match);
62
63
		return isset($match[1]) ? $match[1] : '';
64
	}
65
66
	/**
67
	 * Read the attachment directory structure from the source db
68
	 *
69
	 * @return array|null
70
	 */
71
	public function getAttachmentDirs()
72
	{
73
		if ($this->smf_attach_folders === null)
74
		{
75
			$from_prefix = $this->config->from_prefix;
76
77
			$request = $this->db->query("
78
				SELECT value
79
				FROM {$from_prefix}settings
80
				WHERE variable='attachmentUploadDir';");
81
			list ($smf_attachments_dir) = $this->db->fetch_row($request);
82
83
			$this->smf_attach_folders = @unserialize($smf_attachments_dir);
84
85
			if (!is_array($this->smf_attach_folders))
86
				$this->smf_attach_folders = array(1 => $smf_attachments_dir);
87
		}
88
89
		return $this->smf_attach_folders;
90
	}
91
92
	/**
93
	 * Import likes from the Wedge table
94
	 *
95
	 * @return array
96
	 */
97
	public function fetchLikes()
98
	{
99
		$from_prefix = $this->config->from_prefix;
100
101
		$request = $this->db->query("
102
			SELECT l.id_member, l.id_content AS id_msg, m.id_member AS id_poster, l.like_time AS like_timestamp
103
			FROM {$from_prefix}likes AS l
104
				INNER JOIN {$from_prefix}messages AS m ON (m.id_msg = l.id_content)
105
			WHERE content_type = 'post'");
106
		$return = array();
107
		while ($row = $this->db->fetch_assoc($request))
108
			$return[] = array(
109
				'id_member' => $row['id_member'],
110
				'id_msg' => $row['id_msg'],
111
				'id_poster' => $row['id_poster'],
112
				'like_timestamp' => $row['like_timestamp'],
113
			);
114
		$this->db->free_result($request);
115
116
		return $return;
117
	}
118
}
119
120
/**
121
 * Copy attachments from the source to our destination
122
 *
123
 * @param array $row
124
 * @param \OpenImporter\Database $db
125
 * @param string $from_prefix
126
 * @param string $attachmentUploadDir
127
 */
128
function moveAttachment(&$row, $db, $from_prefix, $attachmentUploadDir)
129
{
130
	static $wedge_folders = null;
131
132
	// We need to know where the attachments are located
133
	if ($wedge_folders === null)
134
	{
135
		$request = $db->query("
136
			SELECT value
137
			FROM {$from_prefix}settings
138
			WHERE variable='attachmentUploadDir';");
139
		list ($wedge_attachments_dir) = $db->fetch_row($request);
140
141
		$wedge_folders = @unserialize($wedge_attachments_dir);
142
143
		if (!is_array($wedge_folders))
144
			$wedge_folders = array(1 => $wedge_attachments_dir);
145
	}
146
147
	// If something is broken, better try to account for it as well.
148
	if (isset($wedge_folders[$row['id_folder']]))
149
		$wedge_attachments_dir = $wedge_folders[$row['id_folder']];
150
	else
151
		$wedge_attachments_dir = $wedge_folders[1];
152
153
	// Missing the file hash ... create one
154
	if (empty($row['file_hash']))
155
	{
156
		$row['file_hash'] = createAttachmentFileHash($row['filename']);
157
		$source_file = $row['filename'];
158
	}
159
	else
160
		$source_file = $row['id_attach'] . '_' . $row['file_hash'] . '.ext';
161
162
	// Copy it over
163
	copy_file($wedge_attachments_dir . '/' . $source_file, $attachmentUploadDir . '/' . $row['id_attach'] . '_' . $row['file_hash'] . '.elk');
164
}
165