aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/db/migration/data/v310/soft_delete_mod_convert.php
blob: 85b90da5fa50b2988c3a28d231c26947b72e1a46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\db\migration\data\v310;

use phpbb\db\migration\container_aware_migration;

/**
 * Migration to convert the Soft Delete MOD for 3.0
 *
 * https://www.phpbb.com/customise/db/mod/soft_delete/
 */
class soft_delete_mod_convert extends container_aware_migration
{
	static public function depends_on()
	{
		return array(
			'\phpbb\db\migration\data\v310\alpha3',
		);
	}

	public function effectively_installed()
	{
		return !$this->db_tools->sql_column_exists($this->table_prefix . 'posts', 'post_deleted');
	}

	public function update_data()
	{
		return array(
			array('permission.remove', array('m_harddelete', true)),
			array('permission.remove', array('m_harddelete', false)),

			array('custom', array(array($this, 'convert_posts'))),
			array('custom', array(array($this, 'convert_topics'))),
		);
	}

	public function convert_posts($start)
	{
		$content_visibility = $this->get_content_visibility();

		$limit = 250;
		$i = 0;

		$sql = 'SELECT p.*, t.topic_first_post_id, t.topic_last_post_id
			FROM ' . $this->table_prefix . 'posts p, ' . $this->table_prefix . 'topics t
			WHERE p.post_deleted > 0
				AND t.topic_id = p.topic_id';
		$result = $this->db->sql_query_limit($sql, $limit, $start);

		while ($row = $this->db->sql_fetchrow($result))
		{
			$content_visibility->set_post_visibility(
				ITEM_DELETED,
				$row['post_id'],
				$row['topic_id'],
				$row['forum_id'],
				$row['post_deleted'],
				$row['post_deleted_time'],
				'',
				($row['post_id'] == $row['topic_first_post_id']) ? true : false,
				($row['post_id'] == $row['topic_last_post_id']) ? true : false
			);

			$i++;
		}

		$this->db->sql_freeresult($result);

		if ($i == $limit)
		{
			return $start + $i;
		}
	}

	public function convert_topics($start)
	{
		$content_visibility = $this->get_content_visibility();

		$limit = 100;
		$i = 0;

		$sql = 'SELECT *
			FROM ' . $this->table_prefix . 'topics
			WHERE topic_deleted > 0';
		$result = $this->db->sql_query_limit($sql, $limit, $start);

		while ($row = $this->db->sql_fetchrow($result))
		{
			$content_visibility->set_topic_visibility(
				ITEM_DELETED,
				$row['topic_id'],
				$row['forum_id'],
				$row['topic_deleted'],
				$row['topic_deleted_time'],
				''
			);

			$i++;
		}

		$this->db->sql_freeresult($result);

		if ($i == $limit)
		{
			return $start + $i;
		}
	}

	/**
	 * @return \phpbb\content_visibility
	 */
	protected function get_content_visibility()
	{
		return $this->container->get('content.visibility');
	}
}