Passed
Branch master (e751cb)
by Matt
02:48
created

bbcodes_installer::delete_bbcodes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 15
nc 4
nop 1
dl 0
loc 28
ccs 17
cts 17
cp 1
crap 4
rs 9.7666
c 1
b 1
f 0
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2016 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\core;
12
13
use phpbb\db\driver\driver_interface;
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use phpbb\group\helper;
0 ignored issues
show
Bug introduced by
The type phpbb\group\helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use phpbb\language\language;
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use phpbb\request\request;
0 ignored issues
show
Bug introduced by
The type phpbb\request\request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
/**
19
 * Class bbcodes_installer
20
 *
21
 * @package vse\abbc3\core
22
 */
23
class bbcodes_installer extends acp_manager
24
{
25
	/** @var \acp_bbcodes */
0 ignored issues
show
Bug introduced by
The type acp_bbcodes was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
	protected $acp_bbcodes;
27
28
	/** @var string */
29
	protected $phpbb_root_path;
30
31
	/** @var string */
32
	protected $php_ext;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param driver_interface $db
38
	 * @param helper           $group_helper
39
	 * @param language         $language
40
	 * @param request          $request
41
	 * @param string           $phpbb_root_path
42
	 * @param string           $php_ext
43
	 * @access public
44
	 */
45 2
	public function __construct(driver_interface $db, helper $group_helper, language $language, request $request, $phpbb_root_path, $php_ext)
46
	{
47 2
		parent::__construct($db, $group_helper, $language, $request);
48
49 2
		$this->phpbb_root_path = $phpbb_root_path;
50 2
		$this->php_ext         = $php_ext;
51
52 2
		$this->acp_bbcodes     = $this->get_acp_bbcodes();
53
54 2
		$this->language->add_lang('acp/posting');
55 2
	}
56
57
	/**
58
	 * Installs bbcodes, used by migrations to perform add/updates
59
	 *
60
	 * @param array $bbcodes Array of bbcodes to install
61
	 * @access public
62
	 */
63 2
	public function install_bbcodes(array $bbcodes)
64
	{
65 2
		foreach ($bbcodes as $bbcode_name => $bbcode_data)
66
		{
67 2
			$bbcode_data = $this->build_bbcode($bbcode_data);
68
69 2
			if ($bbcode = $this->bbcode_exists($bbcode_name, $bbcode_data['bbcode_tag']))
70 2
			{
71 1
				$this->update_bbcode($bbcode, $bbcode_data);
72 1
				continue;
73
			}
74
75 2
			$this->add_bbcode($bbcode_data);
76 2
		}
77
78 2
		$this->resynchronize_bbcode_order();
79 2
	}
80
81
	/**
82
	 * Deletes bbcodes, used by migrations to perform add/updates.
83
	 * Don't delete just by ID. Also match replace fields. This is to ensure
84
	 * we only delete BBCodes created by ABBC3, and not if, for example
85
	 * ABBC3 BBCodes that have been altered by the user.
86
	 *
87 2
	 * @param array $bbcodes Array of bbcodes to delete
88
	 * @access public
89 2
	 */
90
	public function delete_bbcodes(array $bbcodes)
91 2
	{
92
		foreach ($bbcodes as $bbcode_name => $bbcode_data)
93 2
		{
94 2
			$bbcode_data = $this->build_bbcode($bbcode_data);
95 2
96 2
			if ($bbcode = $this->bbcode_exists($bbcode_name, $bbcode_data['bbcode_tag']))
97 2
			{
98 2
				if (strpos($this->db->get_sql_layer(), 'mssql') === 0)
99 2
				{
100 2
					// Fix for MSSQL Error: 402 The data types ntext and varchar are incompatible in the equal to operator
101 2
					$sql = 'DELETE FROM ' . BBCODES_TABLE . "
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
102
					WHERE CONVERT(NVARCHAR(MAX), first_pass_match) = N'" . $this->db->sql_escape($bbcode_data['first_pass_match']) . "'
103 2
						AND CONVERT(NVARCHAR(MAX), first_pass_replace) = N'" . $this->db->sql_escape($bbcode_data['first_pass_replace']) . "'
104 2
						AND bbcode_id = " . (int) $bbcode['bbcode_id'];
105
				}
106
				else
107
				{
108
					$sql = 'DELETE FROM ' . BBCODES_TABLE . "
109
					WHERE first_pass_match = '" . $this->db->sql_escape($bbcode_data['first_pass_match']) . "'
110
						AND first_pass_replace = '" . $this->db->sql_escape($bbcode_data['first_pass_replace']) . "'
111
						AND bbcode_id = " . (int) $bbcode['bbcode_id'];
112 2
				}
113
				$this->db->sql_query($sql);
114 2
			}
115 2
		}
116 1
117 1
		$this->resynchronize_bbcode_order();
118
	}
119 2
120
	/**
121
	 * Get the acp_bbcodes class
122
	 *
123
	 * @return \acp_bbcodes
124
	 * @access protected
125
	 */
126
	protected function get_acp_bbcodes()
127
	{
128
		if (!class_exists('acp_bbcodes'))
129 2
		{
130
			include $this->phpbb_root_path . 'includes/acp/acp_bbcodes.' . $this->php_ext;
131 2
		}
132
133 2
		return new \acp_bbcodes();
134 2
	}
135 2
136 2
	/**
137 2
	 * Build the bbcode
138 2
	 *
139 2
	 * @param array $bbcode_data Initial bbcode data
140
	 * @return array Complete bbcode data array
141 2
	 * @access protected
142
	 */
143
	protected function build_bbcode(array $bbcode_data)
144
	{
145
		$data = $this->acp_bbcodes->build_regexp($bbcode_data['bbcode_match'], $bbcode_data['bbcode_tpl']);
146
147
		$bbcode_data = array_replace($bbcode_data, [
148
			'bbcode_tag'          => $data['bbcode_tag'],
149
			'first_pass_match'    => $data['first_pass_match'],
150 2
			'first_pass_replace'  => $data['first_pass_replace'],
151
			'second_pass_match'   => $data['second_pass_match'],
152 2
			'second_pass_replace' => $data['second_pass_replace'],
153
		]);
154
155
		return $bbcode_data;
156
	}
157
158
	/**
159
	 * Get the max bbcode id value
160
	 *
161
	 * @return int bbcode identifier
162
	 * @access protected
163 2
	 */
164
	protected function get_max_bbcode_id()
165
	{
166 2
		return $this->get_max_column_value('bbcode_id');
167 2
	}
168 2
169 2
	/**
170 2
	 * Check if bbcode exists
171 2
	 *
172
	 * @param string $bbcode_name Name of bbcode
173 2
	 * @param string $bbcode_tag  Tag name of bbcode
174
	 * @return array|false Existing bbcode data array or false if not found
175
	 * @access protected
176
	 */
177
	public function bbcode_exists($bbcode_name, $bbcode_tag)
178
	{
179
		$sql = 'SELECT bbcode_id
180
			FROM ' . BBCODES_TABLE . "
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
181
			WHERE LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_name)) . "'
182
			OR LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_tag)) . "'";
183 1
		$result = $this->db->sql_query($sql);
184
		$row = $this->db->sql_fetchrow($result);
185 1
		$this->db->sql_freeresult($result);
186 1
187 1
		return $row ? (array) $row : false;
188 1
	}
189 1
190
	/**
191
	 * Update existing bbcode
192
	 *
193
	 * @param array $old_bbcode Existing bbcode data
194
	 * @param array $new_bbcode New bbcode data
195
	 * @access protected
196
	 */
197 2
	protected function update_bbcode(array $old_bbcode, array $new_bbcode)
198
	{
199 2
		$sql = 'UPDATE ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
200
			SET ' . $this->db->sql_build_array('UPDATE', $new_bbcode) . '
201 2
			WHERE bbcode_id = ' . (int) $old_bbcode['bbcode_id'];
202 2
		$this->db->sql_query($sql);
203 1
	}
204 1
205
	/**
206 2
	 * Add new bbcode
207 2
	 *
208 2
	 * @param array $bbcode_data New bbcode data
209
	 * @access protected
210 2
	 */
211
	protected function add_bbcode(array $bbcode_data)
212 2
	{
213 2
		$bbcode_id = max($this->get_max_bbcode_id(), NUM_CORE_BBCODES) + 1;
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\NUM_CORE_BBCODES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
214 2
215
		if ($bbcode_id <= BBCODE_LIMIT)
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODE_LIMIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
216
		{
217
			$bbcode_data['bbcode_id'] = (int) $bbcode_id;
218
			// set display_on_posting to 1 by default, so if 0 is desired, set it in our data array
219
			$bbcode_data['display_on_posting'] = (int) !array_key_exists('display_on_posting', $bbcode_data);
220
221
			$this->db->sql_query('INSERT INTO ' . BBCODES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $bbcode_data));
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
222
		}
223
	}
224
}
225