2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Generic plugin interface.
10 * Includes and instantiates the specified plugin type for a certain format
12 * @param string $plugin_type the type of the plugin (import, export, etc)
13 * @param string $plugin_format the format of the plugin (sql, xml, et )
14 * @param string $plugins_dir directrory with plugins
15 * @param mixed $plugin_param parameter to plugin by which they can
16 * decide whether they can work
18 * @return object new plugin instance
20 function PMA_getPlugin(
26 $GLOBALS['plugin_param'] = $plugin_param;
27 $class_name = strtoupper($plugin_type[0])
28 . strtolower(substr($plugin_type, 1))
29 . strtoupper($plugin_format[0])
30 . strtolower(substr($plugin_format, 1));
31 $file = $class_name . ".class.php";
32 if (is_file($plugins_dir . $file)) {
33 include_once $plugins_dir . $file;
34 return new $class_name;
37 // by default, return SQL plugin
38 return PMA_getPlugin($plugin_type, 'sql', $plugins_dir, $plugin_param);
42 * Reads all plugin information from directory $plugins_dir
44 * @param string $plugin_type the type of the plugin (import, export, etc)
45 * @param string $plugins_dir directrory with plugins
46 * @param mixed $plugin_param parameter to plugin by which they can
47 * decide whether they can work
49 * @return array list of plugin instances
51 function PMA_getPlugins($plugin_type, $plugins_dir, $plugin_param)
53 $GLOBALS['plugin_param'] = $plugin_param;
54 /* Scan for plugins */
55 $plugin_list = array();
56 if (!($handle = @opendir
($plugins_dir))) {
61 while ($file = @readdir
($handle)) {
62 // In some situations, Mac OS creates a new file for each file
63 // (for example ._csv.php) so the following regexp
64 // matches a file which does not start with a dot but ends
66 $class_type = mb_strtoupper($plugin_type[0], 'UTF-8')
67 . mb_strtolower(substr($plugin_type, 1), 'UTF-8');
68 if (is_file($plugins_dir . $file)
70 '@^' . $class_type . '(.+)\.class\.php$@i',
75 $GLOBALS['skip_import'] = false;
76 include_once $plugins_dir . $file;
77 if (! $GLOBALS['skip_import']) {
78 $class_name = $class_type . $matches[1];
79 $plugin = new $class_name;
80 if (null !== $plugin->getProperties()) {
81 $plugin_list[] = $plugin;
92 * Returns locale string for $name or $name if no locale is found
94 * @param string $name for local string
96 * @return string locale string for $name
98 function PMA_getString($name)
100 return isset($GLOBALS[$name]) ?
$GLOBALS[$name] : $name;
104 * Returns html input tag option 'checked' if plugin $opt
105 * should be set by config or request
107 * @param string $section name of config section in
108 * $GLOBALS['cfg'][$section] for plugin
109 * @param string $opt name of option
111 * @return string hmtl input tag option 'checked'
113 function PMA_pluginCheckboxCheck($section, $opt)
115 // If the form is being repopulated using $_GET data, that is priority
116 if (isset($_GET[$opt])
117 ||
! isset($_GET['repopulate'])
118 && ((isset($GLOBALS['timeout_passed'])
119 && $GLOBALS['timeout_passed']
120 && isset($_REQUEST[$opt]))
121 ||
(isset($GLOBALS['cfg'][$section][$opt])
122 && $GLOBALS['cfg'][$section][$opt]))
124 return ' checked="checked"';
130 * Returns default value for option $opt
132 * @param string $section name of config section in
133 * $GLOBALS['cfg'][$section] for plugin
134 * @param string $opt name of option
136 * @return string default value for option $opt
138 function PMA_pluginGetDefault($section, $opt)
140 if (isset($_GET[$opt])) {
141 // If the form is being repopulated using $_GET data, that is priority
142 return htmlspecialchars($_GET[$opt]);
145 if (isset($GLOBALS['timeout_passed'])
146 && $GLOBALS['timeout_passed']
147 && isset($_REQUEST[$opt])
149 return htmlspecialchars($_REQUEST[$opt]);
152 if (!isset($GLOBALS['cfg'][$section][$opt])) {
157 /* Possibly replace localised texts */
159 '/(str[A-Z][A-Za-z0-9]*)/',
160 $GLOBALS['cfg'][$section][$opt],
163 return htmlspecialchars($GLOBALS['cfg'][$section][$opt]);
166 $val = $GLOBALS['cfg'][$section][$opt];
167 foreach ($matches[0] as $match) {
168 if (isset($GLOBALS[$match])) {
169 $val = str_replace($match, $GLOBALS[$match], $val);
172 return htmlspecialchars($val);
176 * Returns html select form element for plugin choice
177 * and hidden fields denoting whether each plugin must be exported as a file
179 * @param string $section name of config section in
180 * $GLOBALS['cfg'][$section] for plugin
181 * @param string $name name of select element
182 * @param array &$list array with plugin instances
183 * @param string $cfgname name of config value, if none same as $name
185 * @return string html select tag
187 function PMA_pluginGetChoice($section, $name, &$list, $cfgname = null)
189 if (! isset($cfgname)) {
192 $ret = '<select id="plugins" name="' . $name . '">';
193 $default = PMA_pluginGetDefault($section, $cfgname);
194 foreach ($list as $plugin) {
195 $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
197 // If the form is being repopulated using $_GET data, that is priority
198 if (isset($_GET[$name])
199 && $plugin_name == $_GET[$name]
200 ||
! isset($_GET[$name])
201 && $plugin_name == $default
203 $ret .= ' selected="selected"';
206 $properties = $plugin->getProperties();
208 if ($properties != null) {
209 $text = $properties->getText();
211 $ret .= ' value="' . $plugin_name . '">'
212 . PMA_getString($text)
213 . '</option>' . "\n";
215 $ret .= '</select>' . "\n";
217 // Whether each plugin has to be saved as a file
218 foreach ($list as $plugin) {
219 $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
220 $ret .= '<input type="hidden" id="force_file_' . $plugin_name
222 $properties = $plugin->getProperties();
223 if ( ! strcmp($section, 'Import')
224 ||
($properties != null && $properties->getForceFile() != null)
230 $ret .= '" />' . "\n";
237 * Returns single option in a list element
239 * @param string $section name of config section in
240 * $GLOBALS['cfg'][$section] for plugin
241 * @param string $plugin_name unique plugin name
242 * @param array &$propertyGroup options property main group instance
243 * @param boolean $is_subgroup if this group is a subgroup
245 * @return string table row with option
247 function PMA_pluginGetOneOption(
255 if (! $is_subgroup) {
256 // for subgroup headers
257 if (strpos(get_class($propertyGroup), "PropertyItem")) {
258 $properties = array($propertyGroup);
261 $ret .= '<div class="export_sub_options" id="' . $plugin_name . '_'
262 . $propertyGroup->getName() . '">';
264 if (method_exists($propertyGroup, 'getText')) {
265 $text = $propertyGroup->getText();
269 $ret .= '<h4>' . PMA_getString($text) . '</h4>';
275 if (! isset($properties)) {
276 $not_subgroup_header = true;
277 if (method_exists($propertyGroup, 'getProperties')) {
278 $properties = $propertyGroup->getProperties();
282 if (isset($properties)) {
283 foreach ($properties as $propertyItem) {
284 $property_class = get_class($propertyItem);
285 // if the property is a subgroup, we deal with it recursively
286 if (strpos($property_class, "Subgroup")) {
288 // each subgroup can have a header, which may also be a form element
289 $subgroup_header = $propertyItem->getSubgroupHeader();
290 if (isset($subgroup_header)) {
291 $ret .= PMA_pluginGetOneOption(
298 $ret .= '<li class="subgroup"><ul';
299 if (isset($subgroup_header)) {
300 $ret .= ' id="ul_' . $subgroup_header->getName() . '">';
305 $ret .= PMA_pluginGetOneOption(
314 // single property item
315 switch ($property_class) {
316 case "BoolPropertyItem":
317 $ret .= '<li>' . "\n";
318 $ret .= '<input type="checkbox" name="' . $plugin_name . '_'
319 . $propertyItem->getName() . '"'
320 . ' value="something" id="checkbox_' . $plugin_name . '_'
321 . $propertyItem->getName() . '"'
323 . PMA_pluginCheckboxCheck(
325 $plugin_name . '_' . $propertyItem->getName()
328 if ($propertyItem->getForce() != null) {
329 // Same code is also few lines lower, update both if needed
330 $ret .= ' onclick="if (!this.checked && '
331 . '(!document.getElementById(\'checkbox_' . $plugin_name
332 . '_' . $propertyItem->getForce() . '\') '
333 . '|| !document.getElementById(\'checkbox_'
334 . $plugin_name . '_' . $propertyItem->getForce()
336 . 'return false; else return true;"';
339 $ret .= '<label for="checkbox_' . $plugin_name . '_'
340 . $propertyItem->getName() . '">'
341 . PMA_getString($propertyItem->getText()) . '</label>';
343 case "DocPropertyItem":
344 echo "DocPropertyItem";
346 case "HiddenPropertyItem":
347 $ret .= '<li><input type="hidden" name="' . $plugin_name . '_'
348 . $propertyItem->getName() . '"'
349 . ' value="' . PMA_pluginGetDefault(
351 $plugin_name . '_' . $propertyItem->getName()
355 case "MessageOnlyPropertyItem":
356 $ret .= '<li>' . "\n";
357 $ret .= '<p>' . PMA_getString($propertyItem->getText()) . '</p>';
359 case "RadioPropertyItem":
360 $default = PMA_pluginGetDefault(
362 $plugin_name . '_' . $propertyItem->getName()
364 foreach ($propertyItem->getValues() as $key => $val) {
365 $ret .= '<li><input type="radio" name="' . $plugin_name
366 . '_' . $propertyItem->getName() . '" value="' . $key
367 . '" id="radio_' . $plugin_name . '_'
368 . $propertyItem->getName() . '_' . $key . '"';
369 if ($key == $default) {
370 $ret .= ' checked="checked"';
372 $ret .= ' />' . '<label for="radio_' . $plugin_name . '_'
373 . $propertyItem->getName() . '_' . $key . '">'
374 . PMA_getString($val) . '</label></li>';
377 case "SelectPropertyItem":
378 $ret .= '<li>' . "\n";
379 $ret .= '<label for="select_' . $plugin_name . '_'
380 . $propertyItem->getName() . '" class="desc">'
381 . PMA_getString($propertyItem->getText()) . '</label>';
382 $ret .= '<select name="' . $plugin_name . '_'
383 . $propertyItem->getName() . '"'
384 . ' id="select_' . $plugin_name . '_'
385 . $propertyItem->getName() . '">';
386 $default = PMA_pluginGetDefault(
388 $plugin_name . '_' . $propertyItem->getName()
390 foreach ($propertyItem->getValues() as $key => $val) {
391 $ret .= '<option value="' . $key . '"';
392 if ($key == $default) {
393 $ret .= ' selected="selected"';
395 $ret .= '>' . PMA_getString($val) . '</option>';
399 case "TextPropertyItem":
400 case "NumberPropertyItem":
401 $ret .= '<li>' . "\n";
402 $ret .= '<label for="text_' . $plugin_name . '_'
403 . $propertyItem->getName() . '" class="desc">'
404 . PMA_getString($propertyItem->getText()) . '</label>';
405 $ret .= '<input type="text" name="' . $plugin_name . '_'
406 . $propertyItem->getName() . '"'
407 . ' value="' . PMA_pluginGetDefault(
409 $plugin_name . '_' . $propertyItem->getName()
411 . ' id="text_' . $plugin_name . '_'
412 . $propertyItem->getName() . '"'
413 . ($propertyItem->getSize() != null
414 ?
' size="' . $propertyItem->getSize() . '"'
416 . ($propertyItem->getLen() != null
417 ?
' maxlength="' . $propertyItem->getLen() . '"'
428 $ret .= '</ul></li>';
431 if (! empty($not_subgroup_header)) {
432 $ret .= '</ul></div>';
436 if (method_exists($propertyGroup, "getDoc")) {
437 $doc = $propertyGroup->getDoc();
439 if (count($doc) == 3) {
440 $ret .= PMA_Util
::showMySQLDocu(
445 } elseif (count($doc) == 1) {
446 $ret .= PMA_Util
::showDocu('faq', $doc[0]);
448 $ret .= PMA_Util
::showMySQLDocu(
455 // Close the list element after $doc link is displayed
456 if (isset($property_class)) {
457 if ($property_class == 'BoolPropertyItem'
458 ||
$property_class == 'MessageOnlyPropertyItem'
459 ||
$property_class == 'SelectPropertyItem'
460 ||
$property_class == 'TextPropertyItem'
470 * Returns html div with editable options for plugin
472 * @param string $section name of config section in $GLOBALS['cfg'][$section]
473 * @param array &$list array with plugin instances
475 * @return string html fieldset with plugin options
477 function PMA_pluginGetOptions($section, &$list)
480 // Options for plugins that support them
481 foreach ($list as $plugin) {
482 $properties = $plugin->getProperties();
483 if ($properties != null) {
484 $text = $properties->getText();
485 $options = $properties->getOptions();
488 $plugin_name = strtolower(substr(get_class($plugin), strlen($section)));
489 $ret .= '<div id="' . $plugin_name
490 . '_options" class="format_specific_options">';
491 $ret .= '<h3>' . PMA_getString($text) . '</h3>';
494 if ($options != null && count($options) > 0) {
495 foreach ($options->getProperties()
496 as $propertyMainGroup
498 // check for hidden properties
500 foreach ($propertyMainGroup->getProperties() as $propertyItem) {
501 if (strcmp("HiddenPropertyItem", get_class($propertyItem))) {
507 $ret .= PMA_pluginGetOneOption(
516 $ret .= '<p>' . __('This format has no options') . '</p>';