Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
fix_left_right_ids | |
0.00% |
0 / 39 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
configure | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
fix_ids_tree | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 |
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\console\command\fixup; |
15 | |
16 | use Symfony\Component\Console\Command\Command as symfony_command; |
17 | use Symfony\Component\Console\Input\InputInterface; |
18 | use Symfony\Component\Console\Output\OutputInterface; |
19 | use Symfony\Component\Console\Style\SymfonyStyle; |
20 | |
21 | class fix_left_right_ids extends \phpbb\console\command\command |
22 | { |
23 | /** @var \phpbb\user */ |
24 | protected $user; |
25 | |
26 | /** @var \phpbb\db\driver\driver_interface */ |
27 | protected $db; |
28 | |
29 | /** @var \phpbb\cache\driver\driver_interface */ |
30 | protected $cache; |
31 | |
32 | /** |
33 | * Constructor |
34 | * |
35 | * @param \phpbb\user $user User instance |
36 | * @param \phpbb\db\driver\driver_interface $db Database connection |
37 | * @param \phpbb\cache\driver\driver_interface $cache Cache instance |
38 | */ |
39 | public function __construct(\phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\cache\driver\driver_interface $cache) |
40 | { |
41 | $this->user = $user; |
42 | $this->db = $db; |
43 | $this->cache = $cache; |
44 | |
45 | parent::__construct($user); |
46 | } |
47 | |
48 | /** |
49 | * {@inheritdoc} |
50 | */ |
51 | protected function configure() |
52 | { |
53 | $this |
54 | ->setName('fixup:fix-left-right-ids') |
55 | ->setDescription($this->user->lang('CLI_DESCRIPTION_FIX_LEFT_RIGHT_IDS')) |
56 | ; |
57 | } |
58 | |
59 | /** |
60 | * Executes the command fixup:fix-left-right-ids. |
61 | * |
62 | * Repairs the tree structure of the forums and modules. |
63 | * The code is mainly borrowed from Support toolkit for phpBB Olympus |
64 | * |
65 | * @param InputInterface $input An InputInterface instance |
66 | * @param OutputInterface $output An OutputInterface instance |
67 | * |
68 | * @return int |
69 | */ |
70 | protected function execute(InputInterface $input, OutputInterface $output) |
71 | { |
72 | $io = new SymfonyStyle($input, $output); |
73 | |
74 | // Fix Left/Right IDs for the modules table |
75 | $result = $this->db->sql_query('SELECT DISTINCT(module_class) FROM ' . MODULES_TABLE); |
76 | while ($row = $this->db->sql_fetchrow($result)) |
77 | { |
78 | $i = 1; |
79 | $where = array("module_class = '" . $this->db->sql_escape($row['module_class']) . "'"); |
80 | $this->fix_ids_tree($i, 'module_id', MODULES_TABLE, 0, $where); |
81 | } |
82 | $this->db->sql_freeresult($result); |
83 | |
84 | // Fix the Left/Right IDs for the forums table |
85 | $i = 1; |
86 | $this->fix_ids_tree($i, 'forum_id', FORUMS_TABLE); |
87 | |
88 | $this->cache->purge(); |
89 | |
90 | $io->success($this->user->lang('CLI_FIXUP_FIX_LEFT_RIGHT_IDS_SUCCESS')); |
91 | return symfony_command::SUCCESS; |
92 | } |
93 | |
94 | /** |
95 | * Item's tree structure rebuild helper |
96 | * The item is either forum or ACP/MCP/UCP module |
97 | * |
98 | * @param int $i Item id offset index |
99 | * @param string $field The key field to fix, forum_id|module_id |
100 | * @param string $table The table name to perform, FORUMS_TABLE|MODULES_TABLE |
101 | * @param int $parent_id Parent item id |
102 | * @param array $where Additional WHERE clause condition |
103 | * |
104 | * @return bool True on rebuild success, false otherwise |
105 | */ |
106 | protected function fix_ids_tree(&$i, $field, $table, $parent_id = 0, $where = array()) |
107 | { |
108 | $changes_made = false; |
109 | $sql = 'SELECT * FROM ' . $table . ' |
110 | WHERE parent_id = ' . (int) $parent_id . |
111 | ((!empty($where)) ? ' AND ' . implode(' AND ', $where) : '') . ' |
112 | ORDER BY left_id ASC'; |
113 | $result = $this->db->sql_query($sql); |
114 | while ($row = $this->db->sql_fetchrow($result)) |
115 | { |
116 | // Update the left_id for the item |
117 | if ($row['left_id'] != $i) |
118 | { |
119 | $this->db->sql_query('UPDATE ' . $table . ' SET ' . $this->db->sql_build_array('UPDATE', array('left_id' => $i)) . " WHERE $field = " . (int) $row[$field]); |
120 | $changes_made = true; |
121 | } |
122 | $i++; |
123 | |
124 | // Go through children and update their left/right IDs |
125 | $changes_made = (($this->fix_ids_tree($i, $field, $table, $row[$field], $where)) || $changes_made) ? true : false; |
126 | |
127 | // Update the right_id for the item |
128 | if ($row['right_id'] != $i) |
129 | { |
130 | $this->db->sql_query('UPDATE ' . $table . ' SET ' . $this->db->sql_build_array('UPDATE', array('right_id' => $i)) . " WHERE $field = " . (int) $row[$field]); |
131 | $changes_made = true; |
132 | } |
133 | $i++; |
134 | } |
135 | $this->db->sql_freeresult($result); |
136 | |
137 | return $changes_made; |
138 | } |
139 | } |