Repository navigation
Expand file tree
/
Copy pathext.php
More file actions
195 lines (166 loc) · 5.88 KB
/
Copy pathext.php
File metadata and controls
195 lines (166 loc) · 5.88 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
*
* Mark Edited Posts Unread extension for the phpBB Forum Software package
*
* @copyright (c) 2015 RMcGirr83
* @copyright (c) 2026, phpBB Modders, https://www.phpbbmodders.com/
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace phpbbmodders\editedpostsunread;
/**
* Mark Edited Posts Unread extension base
*
* Handles the switch from the old "phpbbmodders/editedposts_unread" package name: an existing
* install's data is moved over to the new name when this extension is
* enabled, so nothing is installed twice and no settings are lost.
*/
class ext extends \phpbb\extension\base
{
/** Package name this extension used before the rename */
const OLD_EXT_NAME = 'phpbbmodders/editedposts_unread';
/** Package name this extension uses now */
const NEW_EXT_NAME = 'phpbbmodders/editedpostsunread';
/** PHP namespace prefix used before the rename */
const OLD_NAMESPACE = '\\phpbbmodders\\editedposts_unread\\';
/** PHP namespace prefix used now */
const NEW_NAMESPACE = '\\phpbbmodders\\editedpostsunread\\';
/**
* Refuse to enable below the minimum phpBB and PHP versions.
*
* Refuse to enable while the old copy is still enabled; both would run
* at once and the old one's data can't be moved while it's in use.
*
* @return bool|string|array True if enableable, otherwise a reason string or array of reasons
*/
public function is_enableable()
{
if (!$this->check_phpbb_version() || !$this->check_php_version())
{
$language = $this->container->get('language');
$language->add_lang('install_editedpostsunread', 'phpbbmodders/editedpostsunread');
return $language->lang('EDITEDPOSTSUNREAD_NOT_ENABLEABLE');
}
if ($this->container->get('ext.manager')->is_enabled(self::OLD_EXT_NAME))
{
$language = $this->container->get('language');
$language->add_lang('install_editedpostsunread', 'phpbbmodders/editedpostsunread');
return $language->lang('EDITEDPOSTSUNREAD_DISABLE_OLD', self::OLD_EXT_NAME);
}
return true;
}
/**
* Require phpBB 3.3.19
*
* @return bool
*/
public function check_phpbb_version()
{
return phpbb_version_compare(PHPBB_VERSION, '3.3.19', '>=');
}
/**
* Require PHP 7.4
*
* @return bool
*/
public function check_php_version()
{
return PHP_VERSION_ID >= 70400;
}
/**
* Move an old install's data to the new name, then enable as usual.
*
* @param mixed $old_state State returned by previous call of this method
* @return bool|string
*/
public function enable_step($old_state)
{
if ($old_state === false)
{
$this->move_old_install();
// The migrator loaded its state before this ran; reload it so the
// moved migration history counts as already installed.
$this->migrator->load_migration_state();
}
return parent::enable_step($old_state);
}
/**
* Rewrite everything the database stores under the old name.
*
* Rows are filtered in PHP rather than with LIKE, because namespace
* backslashes and underscores are special characters in LIKE patterns
* on some databases. The tables involved are small.
*/
protected function move_old_install()
{
$db = $this->container->get('dbal.conn');
$prefix = $this->container->getParameter('core.table_prefix');
$db->sql_transaction('begin');
// Migration history, including each migration's dependency list
$result = $db->sql_query('SELECT migration_name, migration_depends_on FROM ' . $prefix . 'migrations');
$rows = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);
foreach ($rows as $row)
{
// Other extensions' migrations may depend on ours, so their
// dependency lists are rewritten too, not just our own rows
$old_depends_on = unserialize($row['migration_depends_on'], ['allowed_classes' => false]);
$old_depends_on = is_array($old_depends_on) ? $old_depends_on : [];
$depends_on = array_map([$this, 'new_class_name'], $old_depends_on);
$name = $this->new_class_name($row['migration_name']);
if ($name === $row['migration_name'] && $depends_on === $old_depends_on)
{
continue;
}
$db->sql_query('UPDATE ' . $prefix . 'migrations SET ' . $db->sql_build_array('UPDATE', [
'migration_name' => $name,
'migration_depends_on' => serialize($depends_on),
]) . " WHERE migration_name = '" . $db->sql_escape($row['migration_name']) . "'");
}
// Module classes and their "ext_vendor/name" auth checks
$result = $db->sql_query('SELECT module_id, module_basename, module_auth FROM ' . $prefix . 'modules');
$rows = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);
foreach ($rows as $row)
{
$basename = $this->new_class_name($row['module_basename']);
$auth = str_replace('ext_' . self::OLD_EXT_NAME, 'ext_' . self::NEW_EXT_NAME, $row['module_auth']);
if ($basename !== $row['module_basename'] || $auth !== $row['module_auth'])
{
$db->sql_query('UPDATE ' . $prefix . 'modules SET ' . $db->sql_build_array('UPDATE', [
'module_basename' => $basename,
'module_auth' => $auth,
]) . ' WHERE module_id = ' . (int) $row['module_id']);
}
}
// The old extension's own record (only once it's disabled)
$db->sql_query('DELETE FROM ' . $prefix . "ext
WHERE ext_name = '" . $db->sql_escape(self::OLD_EXT_NAME) . "'
AND ext_active = 0");
$db->sql_transaction('commit');
}
/**
* Map an old fully qualified class name to the new namespace.
*
* @param string $class_name Class name, possibly under the old namespace
* @return string
*/
protected function new_class_name($class_name)
{
if (!is_string($class_name))
{
return $class_name;
}
// Class names may be stored with or without the leading backslash
foreach ([self::OLD_NAMESPACE, ltrim(self::OLD_NAMESPACE, '\\')] as $old)
{
if (strpos($class_name, $old) === 0)
{
$new = ($old === self::OLD_NAMESPACE) ? self::NEW_NAMESPACE : ltrim(self::NEW_NAMESPACE, '\\');
return $new . substr($class_name, strlen($old));
}
}
return $class_name;
}
}