-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathPlugins.php
More file actions
432 lines (375 loc) · 14 KB
/
Plugins.php
File metadata and controls
432 lines (375 loc) · 14 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
declare(strict_types=1);
namespace PhpMyAdmin;
use FilesystemIterator;
use PhpMyAdmin\ConfigStorage\Relation;
use PhpMyAdmin\Container\ContainerBuilder;
use PhpMyAdmin\Dbal\DatabaseInterface;
use PhpMyAdmin\Export\OutputHandler;
use PhpMyAdmin\Html\MySQLDocumentation;
use PhpMyAdmin\Import\Import;
use PhpMyAdmin\Import\ImportSettings;
use PhpMyAdmin\Plugins\ExportPlugin;
use PhpMyAdmin\Plugins\ExportType;
use PhpMyAdmin\Plugins\ImportPlugin;
use PhpMyAdmin\Plugins\Plugin;
use PhpMyAdmin\Plugins\PluginType;
use PhpMyAdmin\Plugins\SchemaPlugin;
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup;
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertySubgroup;
use PhpMyAdmin\Properties\Options\Items\HiddenPropertyItem;
use PhpMyAdmin\Properties\Options\OptionsPropertyGroup;
use PhpMyAdmin\Properties\Options\OptionsPropertyItem;
use PhpMyAdmin\Properties\Options\OptionsPropertyOneItem;
use SplFileInfo;
use Throwable;
use function __;
use function array_map;
use function class_exists;
use function count;
use function in_array;
use function is_string;
use function is_subclass_of;
use function mb_strtolower;
use function mb_strtoupper;
use function mb_substr;
use function sprintf;
use function str_starts_with;
use function strcasecmp;
use function usort;
class Plugins
{
/**
* Instantiates the specified plugin type for a certain format
*
* @param string $type the type of the plugin (import, export, etc)
* @param string $format the format of the plugin (sql, xml, et )
* @phpstan-param 'export'|'import'|'schema' $type
*
* @return object|null new plugin instance
*/
public static function getPlugin(
string $type,
string $format,
ExportType $exportType = ExportType::Raw,
bool $singleTable = false,
): object|null {
ExportPlugin::$exportType = $exportType;
ExportPlugin::$singleTable = $singleTable;
$pluginType = mb_strtoupper($type[0]) . mb_strtolower(mb_substr($type, 1));
$pluginFormat = mb_strtoupper($format[0]) . mb_strtolower(mb_substr($format, 1));
$class = sprintf('PhpMyAdmin\\Plugins\\%s\\%s%s', $pluginType, $pluginType, $pluginFormat);
if (! class_exists($class)) {
return null;
}
$container = ContainerBuilder::getContainer();
if ($type === 'export') {
/** @psalm-suppress MixedMethodCall */
return new $class(
$container->get(Relation::class),
$container->get(OutputHandler::class),
$container->get(Transformations::class),
$container->get(DatabaseInterface::class),
$container->get(Config::class),
);
}
if ($type === 'import') {
/** @psalm-suppress MixedMethodCall */
return new $class(
$container->get(Import::class),
$container->get(DatabaseInterface::class),
$container->get(Config::class),
);
}
/** @psalm-suppress MixedMethodCall */
return new $class();
}
/**
* @return ExportPlugin[]
* @psalm-return list<ExportPlugin>
*/
public static function getExport(ExportType $type, bool $singleTable): array
{
ExportPlugin::$exportType = $type;
ExportPlugin::$singleTable = $singleTable;
return self::getPlugins('Export');
}
/**
* @return ImportPlugin[]
* @psalm-return list<ImportPlugin>
*/
public static function getImport(): array
{
return self::getPlugins('Import');
}
/**
* @return SchemaPlugin[]
* @psalm-return list<SchemaPlugin>
*/
public static function getSchema(): array
{
return self::getPlugins('Schema');
}
/**
* Reads all plugin information
*
* @param string $type the type of the plugin (import, export, etc)
* @psalm-param 'Export'|'Import'|'Schema' $type
*
* @return Plugin[] list of plugin instances
* @psalm-return (
* $type is 'Export'
* ? list<ExportPlugin>
* : ($type is 'Import' ? list<ImportPlugin> : list<SchemaPlugin>)
* )
*/
private static function getPlugins(string $type): array
{
try {
$files = new FilesystemIterator(ROOT_PATH . 'src/Plugins/' . $type);
} catch (Throwable) {
return [];
}
$plugins = [];
/** @var SplFileInfo $fileInfo */
foreach ($files as $fileInfo) {
if (! $fileInfo->isReadable() || ! $fileInfo->isFile() || $fileInfo->getExtension() !== 'php') {
continue;
}
if (! str_starts_with($fileInfo->getFilename(), $type)) {
continue;
}
$class = sprintf('PhpMyAdmin\\Plugins\\%s\\%s', $type, $fileInfo->getBasename('.php'));
if (! class_exists($class) || ! is_subclass_of($class, Plugin::class) || ! $class::isAvailable()) {
continue;
}
$container = ContainerBuilder::getContainer();
if ($type === 'Export' && is_subclass_of($class, ExportPlugin::class)) {
$plugins[] = new $class(
$container->get(Relation::class),
$container->get(OutputHandler::class),
$container->get(Transformations::class),
$container->get(DatabaseInterface::class),
$container->get(Config::class),
);
} elseif ($type === 'Import' && is_subclass_of($class, ImportPlugin::class)) {
$plugins[] = new $class(
$container->get(Import::class),
$container->get(DatabaseInterface::class),
$container->get(Config::class),
);
} elseif ($type === 'Schema' && is_subclass_of($class, SchemaPlugin::class)) {
$plugins[] = new $class();
}
}
usort($plugins, static fn (Plugin $plugin1, Plugin $plugin2): int => strcasecmp(
$plugin1->getProperties()->getText(),
$plugin2->getProperties()->getText(),
));
return $plugins;
}
/**
* Returns html input tag option 'checked' if plugin $opt
* should be set by config or request
*
* @param string $opt name of option
*
* @return string html input tag option 'checked'
*/
public static function checkboxCheck(PluginType $pluginType, string $opt): string
{
// If the form is being repopulated using $_GET data, that is priority
if (
isset($_GET[$opt])
|| ! isset($_GET['repopulate'])
&& ((ImportSettings::$timeoutPassed && isset($_REQUEST[$opt]))
|| ! empty(Config::getInstance()->settings[$pluginType->value][$opt]))
) {
return ' checked';
}
return '';
}
/**
* Validates the plugin name and returns it, or falls back to 'sql' if invalid.
*
* @param ExportPlugin[]|ImportPlugin[] $plugins
*/
public static function validatePluginNameOrUseDefault(array $plugins, string $pluginName): string
{
// If the format is invalid, fall back to 'sql' (issue: #19891)
$validNames = array_map(static function ($plugin) {
return $plugin->getName();
}, $plugins);
if (! in_array($pluginName, $validNames, true)) {
return 'sql';
}
return $pluginName;
}
/**
* Returns default value for option $opt
*
* @param PluginType $pluginType type of the plugin
* @param string $opt name of option
*
* @return string default value for option $opt
*/
public static function getDefault(PluginType $pluginType, string $opt): string
{
if (isset($_GET[$opt]) && is_string($_GET[$opt])) {
// If the form is being repopulated using $_GET data, that is priority
return $_GET[$opt];
}
if (isset($_REQUEST[$opt]) && is_string($_REQUEST[$opt]) && ImportSettings::$timeoutPassed) {
return $_REQUEST[$opt];
}
$config = Config::getInstance();
if (isset($config->settings[$pluginType->value][$opt])) {
return (string) $config->settings[$pluginType->value][$opt];
}
return '';
}
/**
* @param ExportPlugin[]|ImportPlugin[]|SchemaPlugin[] $list
*
* @return array<int, array<string, bool|string>>
* @psalm-return list<array{name: non-empty-lowercase-string, text: string, is_selected: bool, is_binary: bool}>
*/
public static function getChoice(array $list, string $default): array
{
$return = [];
foreach ($list as $plugin) {
$pluginName = $plugin->getName();
$properties = $plugin->getProperties();
$return[] = [
'name' => $pluginName,
'text' => $plugin->getTranslatedText($properties->getText()),
'is_selected' => $pluginName === $default,
'is_binary' => $properties->getForceFile(),
];
}
return $return;
}
/**
* Returns single option in a list element
*
* @param OptionsPropertyItem $propertyGroup options property main group instance
*
* @return string table row with option
*/
private static function getOneOption(
Plugin $plugin,
PluginType $pluginType,
OptionsPropertyItem $propertyGroup,
): string {
$ret = '';
$properties = [];
if (! $propertyGroup instanceof OptionsPropertySubgroup) {
// for subgroup headers
if ($propertyGroup instanceof OptionsPropertyOneItem) {
$properties = [$propertyGroup];
} else {
// for main groups
$ret .= "\n" . '<div id="' . $propertyGroup->getName() . '">';
$text = $propertyGroup->getText();
if ($text !== '') {
$ret .= '<h5 class="card-title mt-4 mb-2">' . $plugin->getTranslatedText($text) . '</h5>';
}
$ret .= '<ul class="list-group">' . "\n";
}
}
if ($propertyGroup instanceof OptionsPropertyGroup) {
$properties = $propertyGroup->getProperties();
}
foreach ($properties as $propertyItem) {
if ($propertyItem instanceof OptionsPropertySubgroup) {
// each subgroup can have a header, which may also be a form element
$subgroupHeader = $propertyItem->getSubgroupHeader();
if ($subgroupHeader !== null) {
$ret .= self::getOneOption($plugin, $pluginType, $subgroupHeader);
}
$ret .= '<li class="list-group-item"><ul class="list-group"';
if ($subgroupHeader !== null && $subgroupHeader->getName() !== '') {
$ret .= ' id="ul_' . $subgroupHeader->getName() . '">';
} else {
$ret .= '>';
}
$ret .= "\n";
$ret .= self::getOneOption($plugin, $pluginType, $propertyItem);
continue;
}
// single property item
$ret .= self::getHtmlForProperty($plugin, $pluginType, $propertyItem);
}
if ($propertyGroup instanceof OptionsPropertySubgroup) {
// end subgroup
$ret .= '</ul>' . "\n";
} elseif ($propertyGroup instanceof OptionsPropertyGroup) {
// end main group
$ret .= '</ul></div>' . "\n";
}
return $ret;
}
private static function getHtmlForProperty(
Plugin $plugin,
PluginType $pluginType,
OptionsPropertyItem $propertyItem,
): string {
if ($propertyItem instanceof OptionsPropertyOneItem) {
return $propertyItem->getHtml($plugin, $pluginType) . "\n";
}
return '';
}
/**
* Returns html div with editable options for plugin
*
* @param ExportPlugin[]|ImportPlugin[]|SchemaPlugin[] $list array with plugin instances
*
* @return string html fieldset with plugin options
*/
public static function getOptions(PluginType $pluginType, array $list): string
{
$ret = '';
// Options for plugins that support them
foreach ($list as $plugin) {
$properties = $plugin->getProperties();
$text = $properties->getText();
$options = $properties->getOptions();
$ret .= '<div id="' . $plugin->getName() . '_options" class="format_specific_options">';
$ret .= '<h3>' . $plugin->getTranslatedText($text) . '</h3>';
$noOptions = true;
if ($options !== null) {
/** @var OptionsPropertyMainGroup $propertyMainGroup */
foreach ($options->getProperties() as $propertyMainGroup) {
// check for hidden properties
$noOptions = true;
foreach ($propertyMainGroup->getProperties() as $propertyItem) {
if (! $propertyItem instanceof HiddenPropertyItem) {
$noOptions = false;
break;
}
}
$ret .= self::getOneOption($plugin, $pluginType, $propertyMainGroup);
}
}
if ($noOptions) {
$ret .= '<p class="card-text">' . __('This format has no options') . '</p>';
}
$ret .= '</div>' . "\n\n";
}
return $ret;
}
public static function getDocumentationLinkHtml(OptionsPropertyOneItem $propertyGroup): string
{
$doc = $propertyGroup->getDoc();
if ($doc === '') {
return '';
}
if (is_string($doc)) {
return MySQLDocumentation::showDocumentation('faq', $doc);
}
if (count($doc) === 2) {
return MySQLDocumentation::show($doc[0], anchor: $doc[1]);
}
return MySQLDocumentation::show($doc[0]);
}
}