Completed
Push — master ( c40e0f...de120c )
by
unknown
01:51 queued 12s
created

clean_old_ideas::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
1
<?php
2
/**
3
 *
4
 * Ideas extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ideas\textreparser\plugins;
12
13
class clean_old_ideas extends \phpbb\textreparser\row_based_plugin
14
{
15
	/** @var \phpbb\textformatter\s9e\utils */
16
	protected $text_formatter_utils;
17
18
	/** @var string */
19
	protected $ideas_table;
20
21
	/** @var string  */
22
	protected $topics_table;
23
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param \phpbb\db\driver\driver_interface $db                   Database connection
28
	 * @param \phpbb\textformatter\s9e\utils    $text_formatter_utils Text formatter utilities object
29
	 * @param string                            $table                Posts Table
30
	 * @param string                            $topics_table         Topics Table
31
	 * @param string                            $ideas_table          Ideas Table
32
	 */
33
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\textformatter\s9e\utils $text_formatter_utils, $table, $topics_table, $ideas_table)
34
	{
35
		parent::__construct($db, $table);
36
37
		$this->text_formatter_utils = $text_formatter_utils;
38
		$this->ideas_table = $ideas_table;
39
		$this->topics_table = $topics_table;
40
	}
41
42
	/**
43
	 * {@inheritdoc}
44
	 */
45
	public function get_columns()
46
	{
47
		return [
48
			'id'   => 'post_id',
49
			'text' => 'post_text',
50
		];
51
	}
52
53
	/**
54
	 * {@inheritdoc}
55
	 */
56 View Code Duplication
	public function get_max_id()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
	{
58
		$sql = 'SELECT MAX(idea_id) AS max_id FROM ' . $this->ideas_table;
59
		$result = $this->db->sql_query($sql);
60
		$max_id = (int) $this->db->sql_fetchfield('max_id');
61
		$this->db->sql_freeresult($result);
62
63
		return $max_id;
64
	}
65
66
	/**
67
	 * {@inheritdoc}
68
	 */
69
	protected function get_records_by_range_query($min_id, $max_id)
70
	{
71
		$columns = $this->get_columns();
72
73
		$fields = [];
74
		foreach ($columns as $field_name => $column_name)
75
		{
76
			$fields[] = 'p.' . $column_name . ' AS ' . $field_name;
77
		}
78
79
		// Query the first post's text for ideas created prior to Sep. 2017
80
		return 'SELECT ' . implode(', ', $fields) . '
81
			FROM ' . $this->table . ' p
82
			INNER JOIN ' . $this->ideas_table . ' i
83
				ON i.topic_id = p.topic_id
84
			INNER JOIN ' . $this->topics_table . ' t
85
				ON p.' . $columns['id'] . ' = t.topic_first_post_id
86
			WHERE p.post_time < ' . strtotime('September 1, 2017') . '
87
				AND  i.idea_id BETWEEN ' . $min_id . ' AND ' . $max_id;
88
	}
89
90
	/**
91
	 * {@inheritdoc}
92
	 */
93
	protected function reparse_record(array $record)
94
	{
95
		$text = $record['text'];
96
97
		// Remove the USER bbcode from the idea post
98
		$text = $this->text_formatter_utils->remove_bbcode($text, 'user');
99
100
		// Remove the IDEA bbcode from the idea post
101
		$text = $this->text_formatter_utils->remove_bbcode($text, 'idea');
102
103
		// Remove old strings from the idea post
104
		$text = str_replace([
105
			"<br/>\n<br/>\n----------",
106
			"<br/>\n<br/>\nView idea at: ",
107
			"<br/>\n<br/>\nPosted by "
108
		], ['', '', ''], $text);
109
110
		// Save the new text if it has changed and it's not a dry run
111
		if ($text !== $record['text'] && $this->save_changes)
112
		{
113
			$record['text'] = $text;
114
			$this->save_record($record);
115
		}
116
	}
117
}
118