From 958039b18b5f677e4f634dc117a5675ff607007d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Chapeaux?= Date: Fri, 24 May 2002 09:05:41 +0000 Subject: [PATCH] Patch #554925 - Search into database, thanks to Thomas Chaumeny --- ChangeLog | 5 + Documentation.html | 6 + config.inc.php3 | 25 ++- db_details_links.php3 | 14 +- db_search.php3 | 409 ++++++++++++++++++++++++++++++++++++++++++ lang/english.inc.php3 | 15 +- lang/french.inc.php3 | 15 +- 7 files changed, 467 insertions(+), 22 deletions(-) create mode 100644 db_search.php3 diff --git a/ChangeLog b/ChangeLog index 9d576f827..00ad9a0fc 100755 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,11 @@ phpMyAdmin - Changelog $Id$ $Source$ +2002-05-24 Loïc Chapeaux + * config.inc.php3; db_details_links.php3; db_search.php3; + Documentation.html; lang/*: patch #554925 - Search into database, thanks + to Thomas Chaumeny . + 2002-05-23 Marc Delisle * Documentation.html, config.inc.php3, db_details_structure.php3, pdf_schema.php3: new 'table_coords' because a table can appear diff --git a/Documentation.html b/Documentation.html index 3f804cfad..b0ce2297a 100755 --- a/Documentation.html +++ b/Documentation.html @@ -910,6 +910,12 @@ $cfg['PmaAbsoluteUri'] = (!empty($HTTPS) ? 'https' : 'http') . '://'

+
$cfg['UseDbSearch'] boolean
+
+ Define whether the "search string inside database" is enabled or not. +

+
+
$cfg['ShowStats'] boolean
Defines whether to display space usage and statistics about databases diff --git a/config.inc.php3 b/config.inc.php3 index 1f75bcee1..58138e92e 100755 --- a/config.inc.php3 +++ b/config.inc.php3 @@ -50,21 +50,14 @@ $cfg['Servers'][$i]['bookmarkdb'] = ''; // Bookmark db - leave blank $cfg['Servers'][$i]['bookmarktable'] = ''; // Bookmark table - leave blank for no bookmark support $cfg['Servers'][$i]['relation'] = ''; // table to describe the relation between links (see doc) // - leave blank for no relation-links support -$cfg['Servers'][$i]['table_info'] - = ''; // table to describe the - // display fields - // - leave blank - // for no display fields - // support -$cfg['Servers'][$i]['table_coords'] - = ''; // table to describe the - // tables position for the - // PDF schema - leave blank - // for no PDF schema support -$cfg['Servers'][$i]['AllowDeny']['order'] - = ''; // Host authentication order, leave blank to not use -$cfg['Servers'][$i]['AllowDeny']['rules'] - = array(); // Host authentication rules, leave blank for defaults +$cfg['Servers'][$i]['table_info'] = ''; // table to describe the display fields + // - leave blank for no display fields support +$cfg['Servers'][$i]['table_coords'] = ''; // table to describe the tables position for the PDF + // schema - leave blank for no PDF schema support +$cfg['Servers'][$i]['AllowDeny']['order'] // Host authentication order, leave blank to not use + = ''; +$cfg['Servers'][$i]['AllowDeny']['rules'] // Host authentication rules, leave blank for defaults + = array(); $i++; @@ -133,6 +126,8 @@ $cfg['ShowSQL'] = TRUE; // show SQL queries as run $cfg['AllowUserDropDatabase'] = FALSE; // show a 'Drop database' link to normal users $cfg['Confirm'] = TRUE; // confirm 'DROP TABLE' & 'DROP DATABASE' $cfg['LoginCookieRecall'] = TRUE; // recall previous login in cookie auth. mode or not +$cfg['UseDbSearch'] = TRUE; // whether to enable the "database search" feature + // or not // Left frame setup $cfg['LeftFrameLight'] = TRUE; // use a select-based menu and display only the diff --git a/db_details_links.php3 b/db_details_links.php3 index cffbba656..5d36cdacf 100644 --- a/db_details_links.php3 +++ b/db_details_links.php3 @@ -8,9 +8,11 @@ // Export link if there is at least one table if ($num_tables > 0) { $lnk3 = ''; + $lnk4 = ''; } else { $lnk3 = ''; + $lnk4 = ''; } // Drop link if allowed if (!$cfg['AllowUserDropDatabase']) { @@ -19,7 +21,7 @@ if (!$cfg['AllowUserDropDatabase']) { $cfg['AllowUserDropDatabase'] = (!mysql_error()); } if ($cfg['AllowUserDropDatabase']) { - $lnk4 = ''; } else { - $lnk4 = ''; + $lnk5 = ''; } @@ -43,14 +45,16 @@ else {  | - '; echo "\n"; ?> + ' ?> | + + '; echo "\n"; ?>  ]    [  - +  ] + */ + + +/** + * Gets some core libraries and send headers + */ +require('./db_details_common.php3'); +// If config variable $cfg['Usedbsearch'] is on FALSE : exit. +if (!$cfg['UseDbSearch']) { + PMA_mysqlDie($strAccessDenied, '', FALSE, $err_url); +} // end if +$url_query .= '&goto=db_search.php3'; + + +/** + * Get the list of tables from the current database + */ +$list_tables = mysql_list_tables($db); +$num_tables = ($list_tables ? mysql_num_rows($list_tables) : 0); +for ($i = 0; $i < $num_tables; $i++) { + $tables[] = mysql_tablename($list_tables, $i); +} +if ($num_tables) { + mysql_free_result($list_tables); +} + + +/** + * Displays top links + */ +$sub_part = ''; +require('./db_details_links.php3'); + + +/** + * 1. Main search form has been submitted + */ +if (isset($submit_search)) { + + /** + * Builds the SQL search query + * + * @param string the table name + * @param string the string to search + * @param integer type of search (1 -> 1 word at least, 2 -> all words, + * 3 -> exact string, 4 -> regexp) + * + * @return array 3 SQL querys (for count, display and delete results) + * + * @global string the url to retun to in case of errors + */ + function PMA_getSearchSqls($table, $search_str, $search_option) + { + global $err_url; + + // Statement types + $sqlstr_select = 'SELECT'; + $sqlstr_delete = 'DELETE'; + + // Fields to select + $local_query = 'SHOW FIELDS FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table); + $res = @mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url); + $res_cnt = ($res ? mysql_num_rows($res) : 0); + for ($i = 0; $i < $res_cnt; $i++) { + $tblfields[] = PMA_backquote(mysql_result($res, $i, 'field')); + } // end if + $sqlstr_fieldstoselect = ' ' . implode(', ', $tblfields); + $tblfields_cnt = count($tblfields); + if ($res) { + mysql_free_result($res); + } + + // Table to use + $sqlstr_from = ' FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($table); + + // Beginning of WHERE clause + $sqlstr_where = ' WHERE'; + + $search_words = (($search_option > 2) ? array($search_str) : explode(' ', $search_str)); + $search_wds_cnt = count($search_words); + + $like_or_regex = (($search_option == 4) ? 'REGEXP' : 'LIKE'); + + for ($i = 0; $i < $search_wds_cnt; $i++) { + // Elimines empty values + if (!empty($search_words[$i])) { + for ($j = 0; $j < $tblfields_cnt; $j++) { + $thefieldlikevalue[] = $tblfields[$j] + . ' ' . $like_or_regex + . ' \'' . $search_words[$i] . '\''; + } // end for + + $fieldslikevalues[] = ($search_wds_cnt > 1) + ? '(' . implode(' OR ', $thefieldlikevalue) . ')' + : implode(' OR ', $thefieldlikevalue); + unset($thefieldlikevalue); + } // end if + } // end for + + $implode_str = ($search_option == 1 ? ' OR ' : ' AND '); + $sqlstr_where .= ' ' . implode($implode_str, $fieldslikevalues); + unset($fieldslikevalues); + + // Builds complete queries + $sql['select_fields'] = $sqlstr_select . $sqlstr_fieldstoselect . $sqlstr_from . $sqlstr_where; + $sql['select_count'] = $sqlstr_select . ' COUNT(*) AS count' . $sqlstr_from . $sqlstr_where; + $sql['delete'] = $sqlstr_delete . $sqlstr_from . $sqlstr_where; + + return $sql; + } // end of the "PMA_getSearchSqls()" function + + + /** + * Strip slashes if necessary + */ + if (get_magic_quotes_gpc()) { + $search_str = stripslashes($search_str); + if (isset($table)) { + $table = stripslashes($table); + } + else if (isset($table_select)) { + $table_select_cnt = count($table_select); + reset($table_select); + for ($i = 0; $i < $table_select_cnt; $i++) { + $table_select[$i] = stripslashes($table_select[$i]); + } // end for + } // end if... else if... + } // end if + + + /** + * Displays the results + */ + if (!empty($search_str) && !empty($search_option)) { + + $original_search_str = $search_str; + $search_str = PMA_sqlAddslashes($search_str, TRUE); + + // Get the true string to display as option's comment + switch ($search_option) { + case 1: + $option_str = ' (' . $strSearchOption1 . ')'; + break; + case 2: + $option_str = ' (' . $strSearchOption2 . ')'; + break; + case 3: + $option_str = ' (' . $strSearchOption3 . ')'; + break; + case 4: + $option_str = ' (' . $strSearchOption4 . ')'; + break; + } // end switch + + // If $table is defined or if there is only one table in $table_select + // set $onetable to the table's name (display is different if there is + // only one table). + // + // Recall: + // $tables is an array with all tables in database $db + // $num_tables is the size of $tables + if (isset($table)) { + $onetable = $table; + } + else if (isset($table_select)) { + $num_selectedtables = count($table_select); + if ($num_selectedtables == 1) { + $onetable = $table_select[0]; + } + } + else if ($num_tables == 1) { + $onetable = $tables[0]; + } + else { + for ($i = 0; $i < $num_tables; $i++) { + $table_select[] = $tables[$i]; + } + $num_selectedtables = $num_tables; + } // end if... else if... else + ?> +
+ + +
+ + + + + + + ' . "\n"; + + // Gets the SQL statements + $newsearchsqls = PMA_getSearchSqls($onetable, $search_str, $search_option); + + // Executes the "COUNT" statement + $local_query = $newsearchsqls['select_count']; + $res = @mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url); + if ($res) { + $res_cnt = mysql_result($res, 0, 'count'); + mysql_free_result($res); + } else { + $res_cnt = 0; + } // end if... else ... + $num_search_result_total = $res_cnt; + + echo ' ' . "\n" + . '
' . "\n" + . ' ' . sprintf($strNumSearchResultsInTable, $res_cnt, htmlspecialchars($onetable)) . "\n"; + + if ($res_cnt > 0) { + echo ' ' . "\n"; + echo ' ' . "\n"; + echo ' ' . "\n"; + echo ' ' . "\n"; + } // end if + } // end only one table + + // Several tables defined in the array $table_select + else if (isset($table_select)) { + // Displays search string + echo ' ' . sprintf($strSearchResultsFor, htmlspecialchars($original_search_str), $option_str) . "\n"; + echo '
    ' . "\n"; + + $num_search_result_total = 0; + for ($i = 0; $i < $num_selectedtables; $i++) { + // Gets the SQL statements + $newsearchsqls = PMA_getSearchSqls($table_select[$i], $search_str, $search_option); + + // Executes the "COUNT" statement + $local_query = $newsearchsqls['select_count']; + $res = @mysql_query($local_query) or PMA_mysqlDie('', $local_query, FALSE, $err_url); + if ($res) { + $res_cnt = mysql_result($res, 0, 'count'); + mysql_free_result($res); + } else { + $res_cnt = 0; + } // end if... else ... + $num_search_result_total += $res_cnt; + + echo ' ' . "\n" + . '
  • ' . "\n" + . ' ' . sprintf($strNumSearchResultsInTable, $res_cnt, htmlspecialchars($table_select[$i])) . "\n"; + + if ($res_cnt > 0) { + echo ' ' . "\n"; + echo ' ' . "\n"; + echo ' ' . "\n"; + echo ' ' . "\n"; + } // end if + + echo '
  • ' . "\n"; + } // end for + + echo '
' . "\n"; + echo '

' . sprintf($strNumSearchResultsTotal, $num_search_result_total) . '

' . "\n"; + } // end several tables + + // Submit button if string found + if ($num_search_result_total) { + ?> + + + +
+
+ + +

+ +

+ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+   + + +
 
+   + + /> +  *
+ /> +  *
+ /> +
+ /> +
 
+   + + 1) { + $i = 0; + + echo ' ' . "\n"; + ?> +
+ +  /  + + ' . "\n"; +} // end if... else... + +echo"\n"; +?> +
 
+
+ + + \ No newline at end of file diff --git a/lang/english.inc.php3 b/lang/english.inc.php3 index 1d6928b03..502a881c9 100644 --- a/lang/english.inc.php3 +++ b/lang/english.inc.php3 @@ -64,6 +64,7 @@ $strCantRenameIdxToPrimary = 'Can\'t rename index to PRIMARY!'; $strCardinality = 'Cardinality'; $strCarriage = 'Carriage return: \\r'; $strChange = 'Change'; +$strChangeDisplay = 'Choose Field to display'; $strChangePassword = 'Change password'; $strCheckAll = 'Check All'; $strCheckDbPriv = 'Check Database Privileges'; @@ -216,6 +217,8 @@ $strNotNumber = 'This is not a number!'; $strNotValidNumber = ' is not a valid row number!'; $strNoUsersFound = 'No user(s) found.'; $strNull = 'Null'; +$strNumSearchResultsInTable = '%s match(es) inside table %s'; +$strNumSearchResultsTotal = 'Total: %s match(es)'; $strOftenQuotation = 'Often quotation marks. OPTIONALLY means that only char and varchar fields are enclosed by the "enclosed by"-character.'; $strOperations = 'Operations'; @@ -284,6 +287,16 @@ $strRunSQLQuery = 'Run SQL query/queries on database %s'; $strSave = 'Save'; $strScaleFactorSmall = 'The scale factor is too small to fit the schema on one page'; +$strSearch = 'Search'; +$strSearchFormTitle = 'Search in database'; +$strSearchInTables = 'Inside table(s):'; +$strSearchNeedle = 'Word(s) or value(s) to search for (wildcard: "%"):'; +$strSearchOption1 = 'at least one of the words'; +$strSearchOption2 = 'all words'; +$strSearchOption3 = 'the exact phrase'; +$strSearchOption4 = 'as regular expression'; +$strSearchResultsFor = 'Search results for "%s" %s:'; +$strSearchType = 'Find:'; $strSelect = 'Select'; $strSelectADb = 'Please select a database'; $strSelectAll = 'Select All'; @@ -307,6 +320,7 @@ $strSingly = '(singly)'; $strSize = 'Size'; $strSort = 'Sort'; $strSpaceUsage = 'Space usage'; +$strSplitWordsWithSpace = 'Words are splitted by a space character (" ").'; $strSQL = 'SQL'; $strSQLQuery = 'SQL-query'; $strStatement = 'Statements'; @@ -364,5 +378,4 @@ $strYes = 'Yes'; $strZip = '"zipped"'; -$strChangeDisplay = 'Choose Field to display'; //to translate ?> diff --git a/lang/french.inc.php3 b/lang/french.inc.php3 index 767565703..2c9549a60 100644 --- a/lang/french.inc.php3 +++ b/lang/french.inc.php3 @@ -64,6 +64,7 @@ $strCantRenameIdxToPrimary = 'La clef ne peut $strCardinality = 'Cardinalité'; $strCarriage = 'Retour de chariot : \\r'; $strChange = 'Modifier'; +$strChangeDisplay = 'Champ à afficher'; $strChangePassword = 'Modifier le mot de passe'; $strCheckAll = 'Tout cocher'; $strCheckDbPriv = 'Afficher les privilèges sur'; @@ -216,6 +217,8 @@ $strNotNumber = 'Ce n\'est pas un nombre !'; $strNotValidNumber = ' n\'est pas un nombre valide !'; $strNoUsersFound = 'Il n\'y a aucun utilisateur'; $strNull = 'Null'; +$strNumSearchResultsInTable = '%s occurence(s) dans la table %s'; +$strNumSearchResultsTotal = 'Total : %s occurence(s)'; $strOftenQuotation = 'Souvent des guillemets. OPTIONNEL signifie que seuls les champs de type char et varchar sont entourés par ce caractère.'; $strOperations = 'Opérations'; @@ -284,6 +287,16 @@ $strRunSQLQuery = 'Ex $strSave = 'Sauvegarder'; $strScaleFactorSmall = 'Veuillez augmenter l\'échelle car le schéma déborde la page'; +$strSearch = 'Rechercher'; +$strSearchFormTitle = 'Effectuer une nouvelle recherche dans la base de données'; +$strSearchInTables = 'Dans la(les) table(s) :'; +$strSearchNeedle = 'Mot(s) ou Valeur à rechercher (passe-partout: "%") :'; +$strSearchOption1 = 'au moins un mot'; +$strSearchOption2 = 'tous les mots'; +$strSearchOption3 = 'phrase exacte'; +$strSearchOption4 = 'expression réguliére'; +$strSearchResultsFor = 'Résultats de la recherche de "%s" %s :'; +$strSearchType = 'Type de recherche :'; $strSelect = 'Sélectionner'; $strSelectADb = 'Choisissez une base de données'; $strSelectAll = 'Tout sélectionner'; @@ -307,6 +320,7 @@ $strSingly = '( $strSize = 'Taille'; $strSort = 'Tri'; $strSpaceUsage = 'Espace utilisé'; +$strSplitWordsWithSpace = 'Séparer les mots par un espace (" ").'; $strSQL = 'SQL'; $strSQLQuery = 'requête SQL'; $strStatement = 'Information'; @@ -365,5 +379,4 @@ $strYes = 'Oui'; $strZip = '"zippé"'; // To translate -$strChangeDisplay = 'Champ à afficher'; ?>