Passed
Pull Request — master (#120)
by Matt
01:33
created

bbcodes_installer::first_pass_replace()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 3
rs 10
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
				continue;
99 2
			}
100 2
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 {$this->first_pass_match()}'" . $this->db->sql_escape($bbcode_data['first_pass_match']) . "'
103 2
				AND {$this->first_pass_replace()}'" . $this->db->sql_escape($bbcode_data['first_pass_replace']) . "'
104 2
				AND bbcode_id = " . (int) $bbcode['bbcode_id'];
105
106
			$this->db->sql_query($sql);
107
		}
108
109
		$this->resynchronize_bbcode_order();
110
	}
111
112 2
	/**
113
	 * Get SQL statement for first_pass_match
114 2
	 * Addresses MSSQL Error: 402 The data types ntext and varchar are incompatible in the equal to operator
115 2
	 *
116 1
	 * @return string
117 1
	 */
118
	private function first_pass_match()
119 2
	{
120
		static $first_pass_match;
121
122
		if (null === $first_pass_match)
123
		{
124
			$first_pass_match = strpos($this->db->get_sql_layer(), 'mssql') === 0 ? 'CONVERT(NVARCHAR(MAX), first_pass_match) = N' : 'first_pass_match = ';
125
		}
126
127
		return $first_pass_match;
128
	}
129 2
130
	/**
131 2
	 * Get SQL statement for first_pass_replace
132
	 * Addresses MSSQL Error: 402 The data types ntext and varchar are incompatible in the equal to operator
133 2
	 *
134 2
	 * @return string
135 2
	 */
136 2
	private function first_pass_replace()
137 2
	{
138 2
		static $first_pass_replace;
139 2
140
		if (null === $first_pass_replace)
141 2
		{
142
			$first_pass_replace = strpos($this->db->get_sql_layer(), 'mssql') === 0 ? 'CONVERT(NVARCHAR(MAX), first_pass_replace) = N' : 'first_pass_replace = ';
143
		}
144
145
		return $first_pass_replace;
146
	}
147
148
	/**
149
	 * Get the acp_bbcodes class
150 2
	 *
151
	 * @return \acp_bbcodes
152 2
	 * @access protected
153
	 */
154
	protected function get_acp_bbcodes()
155
	{
156
		if (!class_exists('acp_bbcodes'))
157
		{
158
			include $this->phpbb_root_path . 'includes/acp/acp_bbcodes.' . $this->php_ext;
159
		}
160
161
		return new \acp_bbcodes();
162
	}
163 2
164
	/**
165
	 * Build the bbcode
166 2
	 *
167 2
	 * @param array $bbcode_data Initial bbcode data
168 2
	 * @return array Complete bbcode data array
169 2
	 * @access protected
170 2
	 */
171 2
	protected function build_bbcode(array $bbcode_data)
172
	{
173 2
		$data = $this->acp_bbcodes->build_regexp($bbcode_data['bbcode_match'], $bbcode_data['bbcode_tpl']);
174
175
		return array_replace($bbcode_data, [
176
			'bbcode_tag'          => $data['bbcode_tag'],
177
			'first_pass_match'    => $data['first_pass_match'],
178
			'first_pass_replace'  => $data['first_pass_replace'],
179
			'second_pass_match'   => $data['second_pass_match'],
180
			'second_pass_replace' => $data['second_pass_replace'],
181
		]);
182
	}
183 1
184
	/**
185 1
	 * Get the max bbcode id value
186 1
	 *
187 1
	 * @return int bbcode identifier
188 1
	 * @access protected
189 1
	 */
190
	protected function get_max_bbcode_id()
191
	{
192
		return $this->get_max_column_value('bbcode_id');
193
	}
194
195
	/**
196
	 * Check if bbcode exists
197 2
	 *
198
	 * @param string $bbcode_name Name of bbcode
199 2
	 * @param string $bbcode_tag  Tag name of bbcode
200
	 * @return array|false Existing bbcode data array or false if not found
201 2
	 * @access protected
202 2
	 */
203 1
	public function bbcode_exists($bbcode_name, $bbcode_tag)
204 1
	{
205
		$sql = 'SELECT bbcode_id
206 2
			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...
207 2
			WHERE LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_name)) . "'
208 2
			OR LOWER(bbcode_tag) = '" . $this->db->sql_escape(strtolower($bbcode_tag)) . "'";
209
		$result = $this->db->sql_query($sql);
210 2
		$row = $this->db->sql_fetchrow($result);
211
		$this->db->sql_freeresult($result);
212 2
213 2
		return $row ? (array) $row : false;
214 2
	}
215
216
	/**
217
	 * Update existing bbcode
218
	 *
219
	 * @param array $old_bbcode Existing bbcode data
220
	 * @param array $new_bbcode New bbcode data
221
	 * @access protected
222
	 */
223
	protected function update_bbcode(array $old_bbcode, array $new_bbcode)
224
	{
225
		$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...
226
			SET ' . $this->db->sql_build_array('UPDATE', $new_bbcode) . '
227
			WHERE bbcode_id = ' . (int) $old_bbcode['bbcode_id'];
228
		$this->db->sql_query($sql);
229
	}
230
231
	/**
232
	 * Add new bbcode
233
	 *
234
	 * @param array $bbcode_data New bbcode data
235
	 * @access protected
236
	 */
237
	protected function add_bbcode(array $bbcode_data)
238
	{
239
		$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...
240
241
		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...
242
		{
243
			$bbcode_data['bbcode_id'] = (int) $bbcode_id;
244
			// set display_on_posting to 1 by default, so if 0 is desired, set it in our data array
245
			$bbcode_data['display_on_posting'] = (int) !array_key_exists('display_on_posting', $bbcode_data);
246
247
			$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...
248
		}
249
	}
250
}
251