Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
39 / 39 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
resync | |
100.00% |
39 / 39 |
|
100.00% |
3 / 3 |
12 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
set_type_constraints | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
4 | |||
resync | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
7 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of 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 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\attachment; |
15 | |
16 | use phpbb\db\driver\driver_interface; |
17 | |
18 | /** |
19 | * Attachment resync class |
20 | */ |
21 | class resync |
22 | { |
23 | /** @var driver_interface */ |
24 | protected $db; |
25 | |
26 | /** @var string Attachment table SQL ID */ |
27 | private $attach_sql_id; |
28 | |
29 | /** @var string Resync table SQL ID */ |
30 | private $resync_sql_id; |
31 | |
32 | /** @var string Resync SQL table */ |
33 | private $resync_table; |
34 | |
35 | /** @var string SQL where statement */ |
36 | private $sql_where; |
37 | |
38 | /** |
39 | * Constructor for attachment resync class |
40 | * |
41 | * @param driver_interface $db Database driver |
42 | */ |
43 | public function __construct(driver_interface $db) |
44 | { |
45 | $this->db = $db; |
46 | } |
47 | |
48 | /** |
49 | * Set type constraints for attachment resync |
50 | * |
51 | * @param string $type Type of resync; can be: message|post|topic |
52 | */ |
53 | protected function set_type_constraints($type) |
54 | { |
55 | switch ($type) |
56 | { |
57 | case 'message': |
58 | $this->attach_sql_id = 'post_msg_id'; |
59 | $this->sql_where = ' AND in_message = 1 |
60 | AND is_orphan = 0'; |
61 | $this->resync_table = PRIVMSGS_TABLE; |
62 | $this->resync_sql_id = 'msg_id'; |
63 | break; |
64 | |
65 | case 'post': |
66 | $this->attach_sql_id = 'post_msg_id'; |
67 | $this->sql_where = ' AND in_message = 0 |
68 | AND is_orphan = 0'; |
69 | $this->resync_table = POSTS_TABLE; |
70 | $this->resync_sql_id = 'post_id'; |
71 | break; |
72 | |
73 | case 'topic': |
74 | $this->attach_sql_id = 'topic_id'; |
75 | $this->sql_where = ' AND is_orphan = 0'; |
76 | $this->resync_table = TOPICS_TABLE; |
77 | $this->resync_sql_id = 'topic_id'; |
78 | break; |
79 | } |
80 | } |
81 | |
82 | /** |
83 | * Resync specified type |
84 | * |
85 | * @param string $type Type of resync |
86 | * @param array $ids IDs to resync |
87 | */ |
88 | public function resync($type, $ids) |
89 | { |
90 | if (empty($type) || !is_array($ids) || !count($ids) || !in_array($type, array('post', 'topic', 'message'))) |
91 | { |
92 | return; |
93 | } |
94 | |
95 | $this->set_type_constraints($type); |
96 | |
97 | // Just check which elements are still having an assigned attachment |
98 | // not orphaned by querying the attachments table |
99 | $sql = 'SELECT ' . $this->attach_sql_id . ' |
100 | FROM ' . ATTACHMENTS_TABLE . ' |
101 | WHERE ' . $this->db->sql_in_set($this->attach_sql_id, $ids) |
102 | . $this->sql_where; |
103 | $result = $this->db->sql_query($sql); |
104 | |
105 | $remaining_ids = array(); |
106 | while ($row = $this->db->sql_fetchrow($result)) |
107 | { |
108 | $remaining_ids[] = $row[$this->attach_sql_id]; |
109 | } |
110 | $this->db->sql_freeresult($result); |
111 | |
112 | // Now only unset those ids remaining |
113 | $ids = array_diff($ids, $remaining_ids); |
114 | |
115 | if (count($ids)) |
116 | { |
117 | $sql = 'UPDATE ' . $this->resync_table . ' |
118 | SET ' . $type . '_attachment = 0 |
119 | WHERE ' . $this->db->sql_in_set($this->resync_sql_id, $ids); |
120 | $this->db->sql_query($sql); |
121 | } |
122 | } |
123 | |
124 | } |