clean_old_ideas   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 8
eloc 38
c 3
b 1
f 0
dl 0
loc 102
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_records_by_range_query() 0 19 2
A get_max_id() 0 8 1
A __construct() 0 7 1
A get_columns() 0 5 1
A reparse_record() 0 22 3
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
	public function get_max_id()
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, bool $force_bbcode_reparsing = false)
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