2025-03-17 19:02:16 +01:00
< ? php
/** Display links to tables referencing current row , same as in Adminer Editor
* @ link https :// www . adminer . org / plugins / #use
* @ author Jakub Vrana , https :// www . vrana . cz /
* @ license https :// www . apache . org / licenses / LICENSE - 2.0 Apache License , Version 2.0
* @ license https :// www . gnu . org / licenses / gpl - 2.0 . html GNU General Public License , version 2 ( one or other )
*/
2025-04-07 16:32:57 +02:00
class AdminerBackwardKeys extends Adminer\Plugin {
2025-03-17 19:02:16 +01:00
// this is copy-pasted from Adminer Editor
function backwardKeys ( $table , $tableName ) {
$return = array ();
2025-06-18 16:51:29 +02:00
// we couldn't use the same query in MySQL and PostgreSQL because unique_constraint_name is not table-specific in MySQL and referenced_table_name is not available in PostgreSQL
2025-03-17 19:02:16 +01:00
foreach (
2025-06-18 16:51:29 +02:00
Adminer\get_rows ( " SELECT s.table_name table_name, s.constraint_name constraint_name, s.column_name column_name, " . ( Adminer\JUSH == " sql " ? " referenced_column_name " : " t.column_name " ) . " referenced_column_name
FROM information_schema . key_column_usage s " . (Adminer \ JUSH == " sql " ? "
WHERE table_schema = " . Adminer \ q(Adminer \ DB) . "
AND referenced_table_schema = " . Adminer \ q(Adminer \ DB) . "
AND referenced_table_name " : "
JOIN information_schema . referential_constraints r USING ( constraint_catalog , constraint_schema , constraint_name )
JOIN information_schema . key_column_usage t ON r . unique_constraint_catalog = t . constraint_catalog
AND r . unique_constraint_schema = t . constraint_schema
AND r . unique_constraint_name = t . constraint_name
2025-06-25 14:52:12 +02:00
AND r . constraint_catalog = t . constraint_catalog
AND r . constraint_schema = t . constraint_schema
AND r . unique_constraint_name = t . constraint_name
2025-06-18 16:51:29 +02:00
AND s . position_in_unique_constraint = t . ordinal_position
2025-06-28 11:31:05 +02:00
WHERE t . table_catalog = " . Adminer \ q(Adminer \ DB) . " AND t . table_schema = " . Adminer \ q( " $_GET [ ns ] " ) . "
2025-06-18 16:51:29 +02:00
AND t . table_name " ) . " = " . Adminer \ q( $table ) . "
ORDER BY s . ordinal_position " , null, " " ) as $row
2025-03-17 19:02:16 +01:00
) {
2025-06-18 16:51:29 +02:00
$return [ $row [ " table_name " ]][ " keys " ][ $row [ " constraint_name " ]][ $row [ " column_name " ]] = $row [ " referenced_column_name " ];
2025-03-17 19:02:16 +01:00
}
foreach ( $return as $key => $val ) {
2025-03-26 01:34:48 +01:00
$name = Adminer\adminer () -> tableName ( Adminer\table_status1 ( $key , true ));
2025-03-17 19:02:16 +01:00
if ( $name != " " ) {
$search = preg_quote ( $tableName );
$separator = '(:|\s*-)?\s+' ;
$return [ $key ][ " name " ] = ( preg_match ( " (^ $search $separator (.+)|^(.+?) $separator $search\ $ )iu " , $name , $match ) ? $match [ 2 ] . $match [ 3 ] : $name );
} else {
unset ( $return [ $key ]);
}
}
return $return ;
}
function backwardKeysPrint ( $backwardKeys , $row ) {
foreach ( $backwardKeys as $table => $backwardKey ) {
foreach ( $backwardKey [ " keys " ] as $cols ) {
$link = Adminer\ME . 'select=' . urlencode ( $table );
$i = 0 ;
foreach ( $cols as $column => $val ) {
2025-06-25 14:57:35 +02:00
if ( ! isset ( $row [ $val ])) {
continue 2 ;
}
2025-03-17 19:02:16 +01:00
$link .= Adminer\where_link ( $i ++ , $column , $row [ $val ]);
}
2025-06-19 09:25:03 +02:00
echo " <a href=' " . Adminer\h ( $link ) . " '> " . Adminer\h ( preg_replace ( '(^' . preg_quote ( $_GET [ " select " ]) . ( substr ( $_GET [ " select " ], - 1 ) == 's' ? '?' : '' ) . '_)' , '_' , $backwardKey [ " name " ])) . " </a> " ;
2025-03-17 19:02:16 +01:00
$link = Adminer\ME . 'edit=' . urlencode ( $table );
foreach ( $cols as $column => $val ) {
$link .= " &set " . urlencode ( " [ " . Adminer\bracket_escape ( $column ) . " ] " ) . " = " . urlencode ( $row [ $val ]);
}
echo " <a href=' " . Adminer\h ( $link ) . " ' title=' " . Adminer\lang ( 'New item' ) . " '>+</a> " ;
}
}
}
2025-04-07 17:00:59 +02:00
2025-04-07 19:09:53 +02:00
function screenshot () {
return " https://www.adminer.org/static/plugins/backward-keys.png " ;
}
2025-04-08 12:57:03 +02:00
protected $translations = array (
2025-04-07 17:00:59 +02:00
'cs' => array ( '' => 'Zobrazí odkazy na tabulky odkazující aktuální řádek, stejně jako Adminer Editor' ),
2025-04-08 14:33:58 +02:00
'de' => array ( '' => 'Links zu Tabellen anzeigen die auf die aktuelle Zeile verweisen, wie im Adminer Editor' ),
2025-04-08 19:34:12 +09:00
'ja' => array ( '' => 'Adminer Editor と同様に、カレント行を参照しているテーブルへのリンクを表示' ),
2025-04-08 17:03:02 +02:00
'pl' => array ( '' => 'Wyświetlaj linki do tabel odnoszących się do bieżącego wiersza, tak samo jak w Edytorze administratora' ),
2025-04-07 17:00:59 +02:00
);
2025-03-17 19:02:16 +01:00
}