-
-
Save scr4bble/b472f3683b7e03200df60c7366f08b97 to your computer and use it in GitHub Desktop.
| <?php | |
| /** | |
| * Adminer plugin that displays the first CHAR/VARCHAR/ENUM column of the foreign key | |
| * | |
| * @category Plugin | |
| * @link http://www.adminer.org/plugins/#use | |
| * @author Bruno VIBERT <http://www.netapsys.fr> | |
| * @modified by Peter Hostačný <hostacny.peter AT gmail.com> | |
| * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 | |
| * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other) | |
| */ | |
| class AdminerDisplayForeignKeyName | |
| { | |
| protected static $_valueCache = array(); | |
| /** | |
| * Get a cache entry | |
| */ | |
| protected static function _getCache($key) | |
| { | |
| if (array_key_exists($key, self::$_valueCache)) { | |
| return self::$_valueCache[$key]; | |
| } | |
| return false; | |
| } | |
| /** | |
| * Set a cache entry | |
| */ | |
| protected static function _setCache($key, $value) | |
| { | |
| self::$_valueCache[$key] = $value; | |
| } | |
| /** | |
| * Render a foreign key value | |
| */ | |
| function selectVal($val, $link, $field, $original) | |
| { | |
| $return = ($val === null ? "<i>NULL</i>" : (preg_match("~char|binary~", $field["type"]) && !preg_match("~var~", $field["type"]) ? "<code>$val</code>" : $val)); | |
| if (preg_match('~blob|bytea|raw|file~', $field["type"]) && !Adminer\is_utf8($val)) { | |
| $return = Adminer\lang('%d byte(s)', strlen($original)); | |
| } else { | |
| parse_str(substr($link, 1), $params); | |
| if (true == is_array($params) && true == array_key_exists('where', $params) && !preg_match("~var~", $field["type"])) { | |
| $where = array(); | |
| foreach ($params['where'] as $param) { | |
| if (!is_array($param)) { | |
| return ($link ? "<a href='" . Adminer\h($link) . "'" . (Adminer\is_url($link) ? " rel='noreferrer'" : "") . ">$return</a>" : $return); | |
| } | |
| $where[] = join(' ', $param); | |
| } | |
| // Find the first char/varchar/enum field to display | |
| $fieldName = false; | |
| foreach (Adminer\fields($params['select']) as $field) { | |
| if (true == in_array($field['type'], array('char', 'varchar', 'enum'))) { | |
| $fieldName = $field['field']; | |
| break; | |
| } | |
| } | |
| if (false !== $fieldName) { | |
| $query = sprintf('SELECT %s FROM %s WHERE %s LIMIT 1', $fieldName, $params['select'], join(' AND ', $where)); | |
| $return = self::_getCache(md5($query)); | |
| if (false === $return) { | |
| $result = Adminer\connection()->query($query); | |
| if ($result->num_rows == 1) { | |
| $row = $result->fetch_assoc(); | |
| $value = $row[$fieldName]; | |
| $length = (isset($_GET['text_length'])) ? (int) $_GET['text_length'] : 100; | |
| if (strlen($value) > $length) | |
| $value = substr($value, 0, $length) . '...'; | |
| $return = sprintf('<strong>[%s]</strong> %s', $original, $value); | |
| self::_setCache(md5($query), $return); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return ($link ? "<a href='" . Adminer\h($link) . "'" . (Adminer\is_url($link) ? " rel='noreferrer'" : "") . ">$return</a>" : $return); | |
| } | |
| } |
Replaced. Thanks for heads-up and direct link.
I forked this to prioritize displaying fields name, code if exist
https://gist.github.com/dungsaga/4c4fea78e9d2ad7d08c5205f49dd882f
I have updated the file.
- Fixed error:
PHP Fatal error: Uncaught TypeError: join(): Argument #2 ($array) must be of type ?array, string given. - VSCode auto-linting for better consistency in style
Hi, would you kindly update this wonderful plugin to Adminer 6. It isn't working for the newest version. Thank you.
Hello, I’ll try to take a look at this sometime this week. I haven’t reviewed what’s new in Adminer 6 yet, so I’ll need to upgrade first and test whether any plugin changes are needed to get it working again. Feel free to post patch suggestions in the meantime.
There's now also the bundled select-foreign.
There's now also the bundled select-foreign.
Thanks for your reply but I can't seem to make it work. I download the script into 'adminer-plugins' directory but the column's values are empty.
Adminer 5 wrapped itself into a namespace and plugins now need to call Adminer's functions via this namespace.
Please update this Gist to this: https://gist.github.com/vrana/2106ea46f7d1c83f62058944e0f87e2e