Passed
Push — release-3.0.0 ( e3b06e...069487 )
by Daniel
02:55
created

c2_update_data::update_data()   C

Complexity

Conditions 9
Paths 26

Size

Total Lines 159
Code Lines 105

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 105
c 0
b 0
f 0
nc 26
nop 0
dl 0
loc 159
rs 6.4444

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
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\migrations\converter;
11
12
use Cocur\Slugify\Slugify;
13
14
class c2_update_data extends \phpbb\db\migration\migration
15
{
16
	/**
17
	 * Skip this migration if the content types table does not exist
18
	 *
19
	 * @return bool True to skip this migration, false to run it
20
	 * @access public
21
	 *
22
	public function effectively_installed()
23
	{
24
		return !$this->db_tools->sql_table_exists($this->table_prefix . 'content_types');
25
	}
26
	*/
27
28
	/**
29
	 * @inheritdoc
30
	 */
31
	static public function depends_on()
32
	{
33
		return array(
34
			'\blitze\content\migrations\v30x\m1_initial_schema',
35
			'\blitze\content\migrations\v30x\m2_initial_data',
36
		);
37
	}
38
39
	public function update_data()
40
	{
41
		if (!$this->db_tools->sql_table_exists($this->table_prefix . 'content_types'))
42
		{
43
			return array();
44
		}
45
46
		include($this->phpbb_root_path . 'includes/bbcode.' . $this->php_ext);
47
		include($this->phpbb_root_path . 'includes/message_parser.' . $this->php_ext);
48
49
		$slugify = new Slugify();
50
		$message_parser = new \parse_message();
51
		$message_parser->mode = 'post';
52
		$message_parser->bbcode_init();
53
54
		$last_topic_id = 0;
55
		$return_data = array();
56
		$topic_tags = array();
57
		$display_maps = array(
58
			0 => 'blitze.content.view.blog',
59
			1 => 'blitze.content.view.portal',
60
			2 => 'blitze.content.view.tiles',
61
			3 => 'blitze.content.view.tiles'
62
		);
63
64
		$sql = 'SELECT t.topic_id, t.forum_id, t.topic_title, t.topic_tag, p.post_id, p.post_text, p.bbcode_uid, p.enable_bbcode, p.enable_smilies
65
			FROM ' . FORUMS_TABLE . ' f, ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
66
			WHERE p.post_id = t.topic_first_post_id
67
				AND t.topic_tag <> ''
68
				AND t.forum_id = f.forum_id
69
				AND f.parent_id = " . (int) $this->config['content_forum_id'] . '
70
			ORDER BY t.topic_id';
71
		$result = $this->db->sql_query($sql);
72
73
		while ($row = $this->db->sql_fetchrow($result))
74
		{
75
			$forum_id	= (int) $row['forum_id'];
76
			$topic_id	= (int) $row['topic_id'];
77
			$post_id	= (int) $row['post_id'];
78
79
			// Convert bbcodes back to their normal form
80
			if ($row['enable_bbcode'])
81
			{
82
				decode_message($row['post_text'], $row['bbcode_uid']);
83
84
				$message_parser->message = $row['post_text'];
85
86
				$message_parser->prepare_bbcodes();
87
				$message_parser->parse_bbcode();
88
89
				$bitfield = $message_parser->bbcode_bitfield;
90
91
				$this->db->sql_query('UPDATE ' . POSTS_TABLE . " SET bbcode_bitfield = '" . $this->db->sql_escape($bitfield) . "' WHERE post_id = " . $post_id);
92
			}
93
			else
94
			{
95
				$this->db->sql_query('UPDATE ' . POSTS_TABLE . " SET bbcode_bitfield = '' WHERE post_id = " . $post_id);
96
			}
97
98
			if ($topic_id != $last_topic_id)
99
			{
100
				$last_topic_id = $topic_id;
101
				$topic_tags[$forum_id] = $row['topic_tag'];
102
				$slug = $slugify->slugify($row['topic_title']);
103
104
				$this->db->sql_query('UPDATE ' . TOPICS_TABLE . " SET topic_slug = '$slug' WHERE topic_id = " . $topic_id);
105
			}
106
		}
107
		$this->db->sql_freeresult($result);
108
109
		$sql = 'SELECT c.*, f.forum_name
110
			FROM ' . $this->table_prefix . 'content_types c, ' . $this->table_prefix . 'forums f
111
			WHERE f.forum_id = c.forum_id
112
				AND ' . $this->db->sql_in_set('c.forum_id', array_keys($topic_tags));
113
		$result = $this->sql_query($sql);
114
115
		$content_id = 0;
116
		$content_types = $content_fields = array();
117
		while ($row = $this->db->sql_fetchrow($result))
118
		{
119
			$forum_id = (int) $row['forum_id'];
120
			$type_name = $topic_tags[$forum_id];
121
122
			$return_data[] = array('permission.remove', array('u_content_view_' . $type_name));
123
			$return_data[] = array('permission.remove', array('u_content_post_' . $type_name));
124
			$return_data[] = array('permission.remove', array('m_content_manage_' . $type_name));
125
126
			$content_types[] = array(
127
				'content_id'			=> ++$content_id,
128
				'forum_id'				=> $forum_id,
129
				'content_name'			=> $type_name,
130
				'content_langname'		=> $row['forum_name'],
131
				'content_colour'		=> substr(md5($type_name), 0, 6),
132
				'content_desc'			=> $row['content_desc'],
133
				'content_desc_bitfield'	=> $row['content_desc_bitfield'],
134
				'content_desc_options'	=> $row['content_desc_options'],
135
				'content_desc_uid'		=> $row['content_desc_uid'],
136
				'content_view'			=> $display_maps[$row['display_type']],
137
				'content_view_settings'	=> '',
138
				'req_approval'			=> (bool) $row['req_approval'],
139
				'allow_comments'		=> (bool) $row['allow_comments'],
140
				'show_poster_info'		=> (bool) $row['show_poster_info'],
141
				'show_poster_contents'	=> (bool) $row['show_poster_contents'],
142
				'show_pagination'		=> (bool) $row['show_pagination'],
143
				'items_per_page'		=> (int) $row['items_per_page'],
144
				'summary_tpl'			=> '',
145
				'detail_tpl'			=> '',
146
			);
147
148
			$fields = unserialize($row['content_fields']);
149
			$fields = array_values($fields);
150
151
			for ($i = 0, $size = sizeof($fields); $i < $size; $i++)
152
			{
153
				$field_props = '';
154
				$field_type = $fields[$i]['name'];
155
156
				if ($field_type == 'content')
157
				{
158
					$field_type = 'textarea';
159
					$field_props = json_encode(array(
160
						'max_chars' => $row['char_limit'],
161
						'rows'		=> 25,
162
						'editor'	=> true,
163
					));
164
				}
165
				else if ($field_type == 'summary')
166
				{
167
					$field_type = 'textarea';
168
					$field_props = json_encode(array(
169
						'max_chars' => $row['char_limit'],
170
						'rows'		=> 5,
171
						'editor'	=> true,
172
					));
173
				}
174
175
				$content_fields[] = array(
176
					'content_id'			=> $content_id,
177
					'field_name'			=> strtolower(str_replace(' ', '_', $fields[$i]['label'])),
178
					'field_label'			=> $fields[$i]['label'],
179
					'field_explain'			=> '',
180
					'field_type'			=> $field_type,
181
					'field_props	'		=> $field_props,
182
					'field_mod_only'		=> !$fields[$i]['input'],
183
					'field_required'		=> $fields[$i]['required'],
184
					'field_summary_show'	=> $fields[$i]['teaser'],
185
					'field_detail_show'		=> $fields[$i]['body'],
186
					'field_order'			=> $i
187
				);
188
			}
189
		}
190
		$this->db->sql_freeresult($result);
191
192
		$this->import_data($content_types, 'sm_content_types');
193
		$this->import_data($content_fields, 'sm_content_fields');
194
195
		$return_data[] = array('config.remove', array('content_forum_id'));
196
197
		return $return_data;
198
	}
199
200
	public function import_data($import_data, $table)
201
	{
202
		// If we have data to import, let's go!! :)
203
		if (!empty($import_data))
204
		{
205
			// Load the insert buffer class to perform a buffered multi insert
206
			$insert_buffer = new \phpbb\db\sql_insert_buffer($this->db, $this->table_prefix . $table);
207
			// Insert imported data to our table
208
			foreach ($import_data as $data)
209
			{
210
				$insert_buffer->insert($data);
211
			}
212
			// Flush the buffer
213
			$insert_buffer->flush();
214
		}
215
	}
216
}
217