2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used with the relation and pdf feature
9 if (! defined('PHPMYADMIN')) {
16 require_once './libraries/Table.class.php';
19 * Executes a query as controluser if possible, otherwise as normal user
21 * @param string the query to execute
22 * @param boolean whether to display SQL error messages or not
24 * @return integer the result set, or false if no result set
29 function PMA_query_as_controluser($sql, $show_error = true, $options = 0)
32 $result = PMA_DBI_query($sql, $GLOBALS['controllink'], $options);
34 $result = @PMA_DBI_try_query
($sql, $GLOBALS['controllink'], $options);
35 } // end if... else...
42 } // end of the "PMA_query_as_controluser()" function
45 * @uses $_SESSION['relation'][$GLOBALS['server']] for caching
46 * @uses $GLOBALS['cfgRelation'] to set it
47 * @uses $GLOBALS['server'] to ensure we are using server-specific pmadb
48 * @uses PMA__getRelationsParam()
49 * @uses PMA_printRelationsParamDiagnostic()
50 * @param bool $verbose whether to print diagnostic info
51 * @return array $cfgRelation
53 function PMA_getRelationsParam($verbose = false)
55 if (empty($_SESSION['relation'][$GLOBALS['server']])) {
56 $_SESSION['relation'][$GLOBALS['server']] = PMA__getRelationsParam();
59 // just for BC but needs to be before PMA_printRelationsParamDiagnostic()
61 $GLOBALS['cfgRelation'] = $_SESSION['relation'][$GLOBALS['server']];
64 PMA_printRelationsParamDiagnostic($_SESSION['relation'][$GLOBALS['server']]);
67 return $_SESSION['relation'][$GLOBALS['server']];
71 * prints out diagnostic info for pma relation feature
73 * @uses $GLOBALS['server']
74 * @uses $GLOBALS['controllink']
76 * @uses __('Documentation')
77 * @uses __('General relation features')
78 * @uses __('Disabled')
80 * @uses __('Display Features')
81 * @uses __('Creation of PDFs')
82 * @uses __('Displaying Column Comments')
83 * @uses __('Bookmarked SQL query')
84 * @uses __('Please see the documentation on how to update your column_comments table')
85 * @uses __('SQL history')
86 * @uses __('Designer')
87 * @uses __('Tracking')
88 * @uses __('User preferences')
89 * @uses $cfg['Server']['pmadb']
91 * @uses PMA_printDiagMessageForFeature()
92 * @uses PMA_printDiagMessageForParameter()
93 * @param array $cfgRelation
95 function PMA_printRelationsParamDiagnostic($cfgRelation)
97 $messages['error'] = '<font color="red"><strong>' . __('not OK')
98 . '</strong></font> [ <a href="Documentation.html#%s" target="documentation">'
99 . __('Documentation') . '</a> ]';
101 $messages['ok'] = '<font color="green"><strong>' . __('OK') . '</strong></font>';
102 $messages['enabled'] = '<font color="green">' . __('Enabled') . '</font>';
103 $messages['disabled'] = '<font color="red">' . __('Disabled') . '</font>';
105 if (false === $GLOBALS['cfg']['Server']['pmadb']) {
106 echo 'PMA Database ... '
107 . sprintf($messages['error'], 'pmadb')
109 . __('General relation features')
110 . ' <font color="green">' . __('Disabled')
115 echo '<table>' . "\n";
117 PMA_printDiagMessageForParameter('pmadb', $GLOBALS['cfg']['Server']['pmadb'], $messages, 'pmadb');
119 PMA_printDiagMessageForParameter('relation', isset($cfgRelation['relation']), $messages, 'relation');
121 PMA_printDiagMessageForFeature(__('General relation features'), 'relwork', $messages);
123 PMA_printDiagMessageForParameter('table_info', isset($cfgRelation['table_info']), $messages, 'table_info');
125 PMA_printDiagMessageForFeature(__('Display Features'), 'displaywork', $messages);
127 PMA_printDiagMessageForParameter('table_coords', isset($cfgRelation['table_coords']), $messages, 'table_coords');
129 PMA_printDiagMessageForParameter('pdf_pages', isset($cfgRelation['pdf_pages']), $messages, 'table_coords');
131 PMA_printDiagMessageForFeature(__('Creation of PDFs'), 'pdfwork', $messages);
133 PMA_printDiagMessageForParameter('column_info', isset($cfgRelation['column_info']), $messages, 'col_com');
135 PMA_printDiagMessageForFeature(__('Displaying Column Comments'), 'commwork', $messages, false);
137 PMA_printDiagMessageForFeature(__('Browser transformation'), 'mimework', $messages);
139 if ($cfgRelation['commwork'] && ! $cfgRelation['mimework']) {
140 echo '<tr><td colspan=2 align="left">' . __('Please see the documentation on how to update your column_comments table') . '</td></tr>' . "\n";
143 PMA_printDiagMessageForParameter('bookmarktable', isset($cfgRelation['bookmark']), $messages, 'bookmark');
145 PMA_printDiagMessageForFeature(__('Bookmarked SQL query'), 'bookmarkwork', $messages);
147 PMA_printDiagMessageForParameter('history', isset($cfgRelation['history']), $messages, 'history');
149 PMA_printDiagMessageForFeature(__('SQL history'), 'historywork', $messages);
151 PMA_printDiagMessageForParameter('designer_coords', isset($cfgRelation['designer_coords']), $messages, 'designer_coords');
153 PMA_printDiagMessageForFeature(__('Designer'), 'designerwork', $messages);
155 PMA_printDiagMessageForParameter('tracking', isset($cfgRelation['tracking']), $messages, 'tracking');
157 PMA_printDiagMessageForFeature(__('Tracking'), 'trackingwork', $messages);
159 PMA_printDiagMessageForParameter('userconfig', isset($cfgRelation['userconfig']), $messages, 'userconfig');
161 PMA_printDiagMessageForFeature(__('User preferences'), 'userconfigwork', $messages);
163 echo '</table>' . "\n";
165 echo '<p>' . __('Quick steps to setup advanced features:') . '</p>';
167 echo '<li>' . __('Create the needed tables with the <code>script/create_tables.sql</code>.') . ' ' . PMA_showDocu('linked-tables') . '</li>';
168 echo '<li>' . __('Create a pma user and give access to these tables.') . ' ' . PMA_showDocu('pmausr') . '</li>';
169 echo '<li>' . __('Enable advanced features in configuration file (<code>config.inc.php</code>), for example by starting from <code>config.sample.inc.php</code>.') . ' ' . PMA_showDocu('quick_install') . '</li>';
170 echo '<li>' . __('Re-login to phpMyAdmin to load the updated configuration file.') . '</li>';
175 * prints out one diagnostic message for a feature
177 * @param string feature name in a message string
178 * @param string the $GLOBALS['cfgRelation'] parameter to check
179 * @param array utility messages
180 * @param boolean whether to skip a line after the message
182 function PMA_printDiagMessageForFeature($feature_name, $relation_parameter, $messages, $skip_line=true)
184 echo ' <tr><td colspan=2 align="right">' . $feature_name . ': '
185 . ($GLOBALS['cfgRelation'][$relation_parameter] ?
$messages['enabled'] : $messages['disabled'])
186 . '</td></tr>' . "\n";
188 echo ' <tr><td> </td></tr>' . "\n";
193 * prints out one diagnostic message for a configuration parameter
195 * @param string config parameter name to display
196 * @param boolean whether this parameter is set
197 * @param array utility messages
198 * @param string anchor in Documentation.html
200 function PMA_printDiagMessageForParameter($parameter, $relation_parameter_set, $messages, $doc_anchor)
202 echo ' <tr><th align="left">';
203 echo '$cfg[\'Servers\'][$i][\'' . $parameter . '\'] ... </th><td align="right">';
204 echo ($relation_parameter_set ?
$messages['ok'] : sprintf($messages['error'], $doc_anchor)) . '</td></tr>' . "\n";
209 * Defines the relation parameters for the current user
210 * just a copy of the functions used for relations ;-)
211 * but added some stuff to check what will work
213 * @uses $cfg['Server']['user']
214 * @uses $cfg['Server']['pmadb']
215 * @uses $cfg['Server']['verbose_check']
216 * @uses $GLOBALS['server']
217 * @uses $GLOBALS['controllink']
218 * @uses PMA_DBI_QUERY_STORE
219 * @uses PMA_DBI_select_db()
220 * @uses PMA_backquote()
221 * @uses PMA_query_as_controluser()
222 * @uses PMA_DBI_fetch_row()
223 * @uses PMA_DBI_free_result()
225 * @return array the relation parameters for the current user
227 function PMA__getRelationsParam()
229 $cfgRelation = array();
230 $cfgRelation['relwork'] = false;
231 $cfgRelation['displaywork'] = false;
232 $cfgRelation['bookmarkwork']= false;
233 $cfgRelation['pdfwork'] = false;
234 $cfgRelation['commwork'] = false;
235 $cfgRelation['mimework'] = false;
236 $cfgRelation['historywork'] = false;
237 $cfgRelation['trackingwork'] = false;
238 $cfgRelation['designerwork'] = false;
239 $cfgRelation['userconfigwork'] = false;
240 $cfgRelation['allworks'] = false;
241 $cfgRelation['user'] = null;
242 $cfgRelation['db'] = null;
244 if ($GLOBALS['server'] == 0 ||
empty($GLOBALS['cfg']['Server']['pmadb'])
245 ||
! PMA_DBI_select_db($GLOBALS['cfg']['Server']['pmadb'], $GLOBALS['controllink'])) {
246 // No server selected -> no bookmark table
247 // we return the array with the falses in it,
248 // to avoid some 'Unitialized string offset' errors later
249 $GLOBALS['cfg']['Server']['pmadb'] = false;
254 $cfgRelation['user'] = $GLOBALS['cfg']['Server']['user'];
255 $cfgRelation['db'] = $GLOBALS['cfg']['Server']['pmadb'];
257 // Now I just check if all tables that i need are present so I can for
258 // example enable relations but not pdf...
259 // I was thinking of checking if they have all required columns but I
260 // fear it might be too slow
262 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($GLOBALS['cfg']['Server']['pmadb']);
263 $tab_rs = PMA_query_as_controluser($tab_query, false, PMA_DBI_QUERY_STORE
);
266 // query failed ... ?
267 //$GLOBALS['cfg']['Server']['pmadb'] = false;
271 while ($curr_table = @PMA_DBI_fetch_row
($tab_rs)) {
272 if ($curr_table[0] == $GLOBALS['cfg']['Server']['bookmarktable']) {
273 $cfgRelation['bookmark'] = $curr_table[0];
274 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['relation']) {
275 $cfgRelation['relation'] = $curr_table[0];
276 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_info']) {
277 $cfgRelation['table_info'] = $curr_table[0];
278 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_coords']) {
279 $cfgRelation['table_coords'] = $curr_table[0];
280 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['designer_coords']) {
281 $cfgRelation['designer_coords'] = $curr_table[0];
282 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['column_info']) {
283 $cfgRelation['column_info'] = $curr_table[0];
284 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['pdf_pages']) {
285 $cfgRelation['pdf_pages'] = $curr_table[0];
286 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['history']) {
287 $cfgRelation['history'] = $curr_table[0];
288 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['tracking']) {
289 $cfgRelation['tracking'] = $curr_table[0];
290 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['userconfig']) {
291 $cfgRelation['userconfig'] = $curr_table[0];
294 PMA_DBI_free_result($tab_rs);
296 if (isset($cfgRelation['relation'])) {
297 $cfgRelation['relwork'] = true;
298 if (isset($cfgRelation['table_info'])) {
299 $cfgRelation['displaywork'] = true;
302 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
303 $cfgRelation['pdfwork'] = true;
305 if (isset($cfgRelation['column_info'])) {
306 $cfgRelation['commwork'] = true;
308 if ($GLOBALS['cfg']['Server']['verbose_check']) {
309 $mime_query = 'SHOW FIELDS FROM '
310 . PMA_backquote($cfgRelation['db']) . '.'
311 . PMA_backquote($cfgRelation['column_info']);
312 $mime_rs = PMA_query_as_controluser($mime_query, false);
314 $mime_field_mimetype = false;
315 $mime_field_transformation = false;
316 $mime_field_transformation_options = false;
317 while ($curr_mime_field = @PMA_DBI_fetch_row
($mime_rs)) {
318 if ($curr_mime_field[0] == 'mimetype') {
319 $mime_field_mimetype = true;
320 } elseif ($curr_mime_field[0] == 'transformation') {
321 $mime_field_transformation = true;
322 } elseif ($curr_mime_field[0] == 'transformation_options') {
323 $mime_field_transformation_options = true;
326 PMA_DBI_free_result($mime_rs);
328 if ($mime_field_mimetype
329 && $mime_field_transformation
330 && $mime_field_transformation_options) {
331 $cfgRelation['mimework'] = true;
334 $cfgRelation['mimework'] = true;
338 if (isset($cfgRelation['history'])) {
339 $cfgRelation['historywork'] = true;
342 if (isset($cfgRelation['tracking'])) {
343 $cfgRelation['trackingwork'] = true;
346 if (isset($cfgRelation['userconfig'])) {
347 $cfgRelation['userconfigwork'] = true;
350 // we do not absolutely need that the internal relations or the PDF
351 // schema feature be activated
352 if (isset($cfgRelation['designer_coords'])) {
353 $cfgRelation['designerwork'] = true;
356 if (isset($cfgRelation['bookmark'])) {
357 $cfgRelation['bookmarkwork'] = true;
360 if ($cfgRelation['relwork'] && $cfgRelation['displaywork']
361 && $cfgRelation['pdfwork'] && $cfgRelation['commwork']
362 && $cfgRelation['mimework'] && $cfgRelation['historywork']
363 && $cfgRelation['trackingwork'] && $cfgRelation['userconfigwork']
364 && $cfgRelation['bookmarkwork'] && $cfgRelation['designerwork']) {
365 $cfgRelation['allworks'] = true;
369 } // end of the 'PMA_getRelationsParam()' function
372 * Gets all Relations to foreign tables for a given table or
373 * optionally a given column in a table
376 * @uses $GLOBALS['controllink']
377 * @uses $GLOBALS['information_schema_relations']
378 * @uses PMA_getRelationsParam()
379 * @uses PMA_backquote()
380 * @uses PMA_sqlAddslashes()
381 * @uses PMA_DBI_fetch_result()
382 * @uses PMA_DBI_fetch_value()
383 * @uses PMA_SQP_analyze()
384 * @uses PMA_SQP_parse()
387 * @param string $db the name of the db to check for
388 * @param string $table the name of the table to check for
389 * @param string $column the name of the column to check for
390 * @param string $source the source for foreign key information
391 * @return array db,table,column
393 function PMA_getForeigners($db, $table, $column = '', $source = 'both')
395 $cfgRelation = PMA_getRelationsParam();
398 if ($cfgRelation['relwork'] && ($source == 'both' ||
$source == 'internal')) {
400 SELECT `master_field`,
404 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
405 WHERE `master_db` = \'' . PMA_sqlAddslashes($db) . '\'
406 AND `master_table` = \'' . PMA_sqlAddslashes($table) . '\' ';
407 if (strlen($column)) {
408 $rel_query .= ' AND `master_field` = \'' . PMA_sqlAddslashes($column) . '\'';
410 $foreign = PMA_DBI_fetch_result($rel_query, 'master_field', null, $GLOBALS['controllink']);
413 if (($source == 'both' ||
$source == 'foreign') && strlen($table)) {
414 $show_create_table_query = 'SHOW CREATE TABLE '
415 . PMA_backquote($db) . '.' . PMA_backquote($table);
416 $show_create_table = PMA_DBI_fetch_value($show_create_table_query, 0, 1);
417 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
419 foreach ($analyzed_sql[0]['foreign_keys'] as $one_key) {
420 // The analyzer may return more than one column name in the
421 // index list or the ref_index_list; if this happens,
422 // the current logic just discards the whole index; having
423 // more than one index field is currently unsupported (see FAQ 3.6)
424 if (count($one_key['index_list']) == 1) {
425 foreach ($one_key['index_list'] as $i => $field) {
426 // If a foreign key is defined in the 'internal' source (pmadb)
427 // and as a native foreign key, we won't get it twice
428 // if $source='both' because we use $field as key
430 // The parser looks for a CONSTRAINT clause just before
431 // the FOREIGN KEY clause. It finds it (as output from
432 // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
433 // versions like 3.23.58.
434 // In those cases, the FOREIGN KEY parsing will put numbers
435 // like -1, 0, 1... instead of the constraint number.
437 if (isset($one_key['constraint'])) {
438 $foreign[$field]['constraint'] = $one_key['constraint'];
441 if (isset($one_key['ref_db_name'])) {
442 $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
444 $foreign[$field]['foreign_db'] = $db;
446 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
447 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
448 if (isset($one_key['on_delete'])) {
449 $foreign[$field]['on_delete'] = $one_key['on_delete'];
451 if (isset($one_key['on_update'])) {
452 $foreign[$field]['on_update'] = $one_key['on_update'];
460 * Emulating relations for some information_schema tables
462 if ($db == 'information_schema'
463 && ($source == 'internal' ||
$source == 'both')) {
464 require_once './libraries/information_schema_relations.lib.php';
466 if (isset($GLOBALS['information_schema_relations'][$table])) {
467 foreach ($GLOBALS['information_schema_relations'][$table] as $field => $relations) {
468 if ((! strlen($column) ||
$column == $field)
469 && (! isset($foreign[$field]) ||
! strlen($foreign[$field]))) {
470 $foreign[$field] = $relations;
477 } // end of the 'PMA_getForeigners()' function
480 * Gets the display field of a table
483 * @uses $GLOBALS['controllink']
484 * @uses PMA_getRelationsParam()
485 * @uses PMA_backquote()
486 * @uses PMA_sqlAddslashes()
487 * @uses PMA_DBI_fetch_single_row()
489 * @param string $db the name of the db to check for
490 * @param string $table the name of the table to check for
491 * @return string field name
493 function PMA_getDisplayField($db, $table)
495 $cfgRelation = PMA_getRelationsParam();
498 * Try to fetch the display field from DB.
500 if ($cfgRelation['displaywork']) {
502 SELECT `display_field`
503 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info']) . '
504 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
505 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'';
507 $row = PMA_DBI_fetch_single_row($disp_query, 'ASSOC', $GLOBALS['controllink']);
508 if (isset($row['display_field'])) {
509 return $row['display_field'];
514 * Emulating the display field for some information_schema tables.
516 if ($db == 'information_schema') {
518 case 'CHARACTER_SETS': return 'DESCRIPTION';
519 case 'TABLES': return 'TABLE_COMMENT';
528 } // end of the 'PMA_getDisplayField()' function
531 * Gets the comments for all rows of a table or the db itself
534 * @uses PMA_DBI_get_fields()
535 * @uses PMA_getDbComment()
536 * @param string the name of the db to check for
537 * @param string the name of the table to check for
538 * @return array [field_name] = comment
540 function PMA_getComments($db, $table = '')
545 // MySQL native column comments
546 $fields = PMA_DBI_get_fields($db, $table);
548 foreach ($fields as $key => $field) {
549 if (! empty($field['Comment'])) {
550 $comments[$field['Field']] = $field['Comment'];
555 $comments[] = PMA_getDbComment($db);
559 } // end of the 'PMA_getComments()' function
562 * Gets the comment for a db
565 * @uses PMA_DBI_QUERY_STORE
566 * @uses PMA_DBI_num_rows()
567 * @uses PMA_DBI_fetch_assoc()
568 * @uses PMA_DBI_free_result()
569 * @uses PMA_getRelationsParam()
570 * @uses PMA_backquote()
571 * @uses PMA_sqlAddslashes()
572 * @uses PMA_query_as_controluser()
574 * @param string the name of the db to check for
575 * @return string comment
577 function PMA_getDbComment($db)
579 $cfgRelation = PMA_getRelationsParam();
582 if ($cfgRelation['commwork']) {
583 // pmadb internal db comment
586 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
587 WHERE db_name = '" . PMA_sqlAddslashes($db) . "'
589 AND column_name = '(db_comment)'";
590 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE
);
592 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
593 $row = PMA_DBI_fetch_assoc($com_rs);
594 $comment = $row['comment'];
596 PMA_DBI_free_result($com_rs);
600 } // end of the 'PMA_getDbComment()' function
603 * Gets the comment for a db
606 * @uses PMA_DBI_QUERY_STORE
607 * @uses PMA_DBI_num_rows()
608 * @uses PMA_DBI_fetch_assoc()
609 * @uses PMA_DBI_free_result()
610 * @uses PMA_getRelationsParam()
611 * @uses PMA_backquote()
612 * @uses PMA_sqlAddslashes()
613 * @uses PMA_query_as_controluser()
615 * @param string the name of the db to check for
616 * @return string comment
618 function PMA_getDbComments()
620 $cfgRelation = PMA_getRelationsParam();
623 if ($cfgRelation['commwork']) {
624 // pmadb internal db comment
626 SELECT `db_name`, `comment`
627 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
628 WHERE `column_name` = '(db_comment)'";
629 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE
);
631 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
632 while ($row = PMA_DBI_fetch_assoc($com_rs)) {
633 $comments[$row['db_name']] = $row['comment'];
636 PMA_DBI_free_result($com_rs);
640 } // end of the 'PMA_getDbComments()' function
643 * Set a database comment to a certain value.
645 * @uses PMA_getRelationsParam()
646 * @uses PMA_backquote()
647 * @uses PMA_sqlAddslashes()
648 * @uses PMA_query_as_controluser()
651 * @param string $db the name of the db
652 * @param string $comment the value of the column
653 * @return boolean true, if comment-query was made.
655 function PMA_setDbComment($db, $comment = '')
657 $cfgRelation = PMA_getRelationsParam();
659 if (! $cfgRelation['commwork']) {
663 if (strlen($comment)) {
666 " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
667 (`db_name`, `table_name`, `column_name`, `comment`)
669 '" . PMA_sqlAddslashes($db) . "',
672 '" . PMA_sqlAddslashes($comment) . "')
673 ON DUPLICATE KEY UPDATE
674 `comment` = '" . PMA_sqlAddslashes($comment) . "'";
678 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
679 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
680 AND `table_name` = \'\'
681 AND `column_name` = \'(db_comment)\'';
684 if (isset($upd_query)){
685 return PMA_query_as_controluser($upd_query);
689 } // end of 'PMA_setDbComment()' function
692 * Set a SQL history entry
694 * @uses $_SESSION['sql_history']
695 * @uses $cfg['QueryHistoryDB']
696 * @uses $cfg['QueryHistoryMax']
697 * @uses PMA_getRelationsParam()
698 * @uses PMA_query_as_controluser()
699 * @uses PMA_backquote()
700 * @uses PMA_sqlAddslashes()
703 * @uses array_shift()
704 * @param string $db the name of the db
705 * @param string $table the name of the table
706 * @param string $username the username
707 * @param string $sqlquery the sql query
710 function PMA_setHistory($db, $table, $username, $sqlquery)
712 if (strlen($sqlquery) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
716 $cfgRelation = PMA_getRelationsParam();
718 if (! isset($_SESSION['sql_history'])) {
719 $_SESSION['sql_history'] = array();
722 $key = md5($sqlquery . $db . $table);
724 if (isset($_SESSION['sql_history'][$key])) {
725 unset($_SESSION['sql_history'][$key]);
728 $_SESSION['sql_history'][$key] = array(
731 'sqlquery' => $sqlquery,
734 if (count($_SESSION['sql_history']) > $GLOBALS['cfg']['QueryHistoryMax']) {
735 // history should not exceed a maximum count
736 array_shift($_SESSION['sql_history']);
739 if (! $cfgRelation['historywork'] ||
! $GLOBALS['cfg']['QueryHistoryDB']) {
743 PMA_query_as_controluser('
745 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
752 (\'' . PMA_sqlAddslashes($username) . '\',
753 \'' . PMA_sqlAddslashes($db) . '\',
754 \'' . PMA_sqlAddslashes($table) . '\',
756 \'' . PMA_sqlAddslashes($sqlquery) . '\')');
757 } // end of 'PMA_setHistory()' function
760 * Gets a SQL history entry
762 * @uses $_SESSION['sql_history']
763 * @uses $GLOBALS['controllink']
764 * @uses PMA_getRelationsParam()
765 * @uses PMA_backquote()
766 * @uses PMA_sqlAddslashes()
767 * @uses PMA_DBI_fetch_result()
768 * @uses array_reverse()
769 * @param string $username the username
770 * @return array list of history items
773 function PMA_getHistory($username)
775 $cfgRelation = PMA_getRelationsParam();
777 if (isset($_SESSION['sql_history'])) {
778 return array_reverse($_SESSION['sql_history']);
781 if (! $cfgRelation['historywork']) {
789 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
790 WHERE `username` = \'' . PMA_sqlAddslashes($username) . '\'
793 return PMA_DBI_fetch_result($hist_query, null, null, $GLOBALS['controllink']);
794 } // end of 'PMA_getHistory()' function
799 * deletes entries that exceeds $cfg['QueryHistoryMax'], oldest first, for the
802 * @uses $cfg['QueryHistoryMax']
803 * @uses $cfg['QueryHistoryDB']
804 * @uses $GLOBALS['controllink']
805 * @uses PMA_backquote()
806 * @uses PMA_sqlAddSlashes()
807 * @uses PMA_query_as_controluser()
808 * @uses PMA_DBI_fetch_value()
809 * @param string $username the username
812 function PMA_purgeHistory($username)
814 $cfgRelation = PMA_getRelationsParam();
815 if (! $GLOBALS['cfg']['QueryHistoryDB'] ||
! $cfgRelation['historywork']) {
819 if (! $cfgRelation['historywork']) {
825 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
826 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
827 ORDER BY `timevalue` DESC
828 LIMIT ' . $GLOBALS['cfg']['QueryHistoryMax'] . ', 1';
830 if ($max_time = PMA_DBI_fetch_value($search_query, 0, 0, $GLOBALS['controllink'])) {
831 PMA_query_as_controluser('
833 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
834 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
835 AND `timevalue` <= \'' . $max_time . '\'');
837 } // end of 'PMA_purgeHistory()' function
840 * Prepares the dropdown for one mode
842 * @uses $cfg['LimitChars']
843 * @uses $cfg['NaturalOrder']
845 * @uses htmlspecialchars()
849 * @uses natcasesort()
851 * @param array $foreign the keys and values for foreigns
852 * @param string $data the current data of the dropdown
853 * @param string $mode the needed mode
855 * @return array the <option value=""><option>s
859 function PMA__foreignDropdownBuild($foreign, $data, $mode)
861 $reloptions = array();
863 if ($mode == 'id-content') {
864 // sort for id-content
865 if ($GLOBALS['cfg']['NaturalOrder']) {
866 uksort($foreign, 'strnatcasecmp');
870 } elseif ($mode == 'content-id') {
871 // sort for content-id
872 if ($GLOBALS['cfg']['NaturalOrder']) {
873 natcasesort($foreign);
879 foreach ($foreign as $key => $value) {
881 if (PMA_strlen($value) <= $GLOBALS['cfg']['LimitChars']) {
883 $value = htmlspecialchars($value);
885 $vtitle = htmlspecialchars($value);
886 $value = htmlspecialchars(substr($value, 0, $GLOBALS['cfg']['LimitChars']) . '...');
889 $reloption = ' <option value="' . htmlspecialchars($key) . '"';
891 $reloption .= ' title="' . $vtitle . '"';
894 if ((string) $key == (string) $data) {
895 $reloption .= ' selected="selected"';
898 if ($mode == 'content-id') {
899 $reloptions[] = $reloption . '>' . $value . ' - ' . htmlspecialchars($key) . '</option>' . "\n";
901 $reloptions[] = $reloption . '>' . htmlspecialchars($key) . ' - ' . $value . '</option>' . "\n";
906 } // end of 'PMA__foreignDropdownBuild' function
909 * Outputs dropdown with values of foreign fields
911 * @uses $cfg['ForeignKeyMaxLimit']
912 * @uses $cfg['ForeignKeyDropdownOrder']
913 * @uses PMA__foreignDropdownBuild()
914 * @uses PMA_isValid()
916 * @param array array of the displayed row
917 * @param string the foreign field
918 * @param string the foreign field to display
919 * @param string the current data of the dropdown (field in row)
920 * @return string the <option value=""><option>s
923 function PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, $data,
927 $max = $GLOBALS['cfg']['ForeignKeyMaxLimit'];
933 foreach ($disp_row as $relrow) {
934 $key = $relrow[$foreign_field];
936 // if the display field has been defined for this foreign table
937 if ($foreign_display) {
938 $value = $relrow[$foreign_display];
941 } // end if ($foreign_display)
943 $foreign[$key] = $value;
946 // put the dropdown sections in correct order
949 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'], 'array')) {
950 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][0])) {
951 $top = PMA__foreignDropdownBuild($foreign, $data,
952 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][0]);
954 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][1])) {
955 $bot = PMA__foreignDropdownBuild($foreign, $data,
956 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][1]);
959 $top = PMA__foreignDropdownBuild($foreign, $data, 'id-content');
960 $bot = PMA__foreignDropdownBuild($foreign, $data, 'content-id');
963 // beginning of dropdown
964 $ret = '<option value=""> </option>' . "\n";
966 $top_count = count($top);
967 if ($max == -1 ||
$top_count < $max) {
968 $ret .= implode('', $top);
969 if ($top_count > 0) {
970 $ret .= ' <option value=""> </option>' . "\n";
971 $ret .= ' <option value=""> </option>' . "\n";
974 $ret .= implode('', $bot);
977 } // end of 'PMA_foreignDropdown()' function
980 * Gets foreign keys in preparation for a drop-down selector
982 * @uses PMA_Table::countRecords()
983 * @uses PMA_backquote()
984 * @uses PMA_getDisplayField()
985 * @uses PMA_sqlAddslashes()
986 * @uses PMA_DBI_fetch_value()
987 * @uses PMA_DBI_free_result()
988 * @uses PMA_DBI_query()
989 * @uses PMA_DBI_num_rows()
990 * @uses PMA_DBI_fetch_assoc()
991 * @param array array of the foreign keys
992 * @param string the foreign field name
993 * @param bool whether to override the total
994 * @param string a possible filter
995 * @param string a possible LIMIT clause
996 * @return array data about the foreign keys
1000 function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filter, $foreign_limit)
1002 // we always show the foreign field in the drop-down; if a display
1003 // field is defined, we show it besides the foreign field
1004 $foreign_link = false;
1005 if ($foreigners && isset($foreigners[$field])) {
1006 $foreigner = $foreigners[$field];
1007 $foreign_db = $foreigner['foreign_db'];
1008 $foreign_table = $foreigner['foreign_table'];
1009 $foreign_field = $foreigner['foreign_field'];
1011 // Count number of rows in the foreign table. Currently we do
1012 // not use a drop-down if more than 200 rows in the foreign table,
1013 // for speed reasons and because we need a better interface for this.
1015 // We could also do the SELECT anyway, with a LIMIT, and ensure that
1016 // the current value of the field is one of the choices.
1018 $the_total = PMA_Table
::countRecords($foreign_db, $foreign_table);
1020 if ($override_total == true ||
$the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']) {
1021 // foreign_display can be FALSE if no display field defined:
1022 $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
1024 $f_query_main = 'SELECT ' . PMA_backquote($foreign_field)
1025 . (($foreign_display == FALSE) ?
'' : ', ' . PMA_backquote($foreign_display));
1026 $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table);
1027 $f_query_filter = empty($foreign_filter) ?
'' : ' WHERE ' . PMA_backquote($foreign_field)
1028 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1029 . (($foreign_display == FALSE) ?
'' : ' OR ' . PMA_backquote($foreign_display)
1030 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1032 $f_query_order = ($foreign_display == FALSE) ?
'' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display);
1033 $f_query_limit = isset($foreign_limit) ?
$foreign_limit : '';
1035 if (!empty($foreign_filter)) {
1036 $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter);
1038 $the_total = PMA_DBI_fetch_value($res);
1039 @PMA_DBI_free_result
($res);
1045 $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit);
1046 if ($disp && PMA_DBI_num_rows($disp) > 0) {
1047 // If a resultset has been created, pre-cache it in the $disp_row array
1048 // This helps us from not needing to use mysql_data_seek by accessing a pre-cached
1049 // PHP array. Usually those resultsets are not that big, so a performance hit should
1051 $disp_row = array();
1052 while ($single_disp_row = @PMA_DBI_fetch_assoc
($disp)) {
1053 $disp_row[] = $single_disp_row;
1055 @PMA_DBI_free_result
($disp);
1059 $foreign_link = true;
1061 } // end if $foreigners
1063 $foreignData['foreign_link'] = $foreign_link;
1064 $foreignData['the_total'] = isset($the_total) ?
$the_total : null;
1065 $foreignData['foreign_display'] = isset($foreign_display) ?
$foreign_display : null;
1066 $foreignData['disp_row'] = isset($disp_row) ?
$disp_row : null;
1067 $foreignData['foreign_field'] = isset($foreign_field) ?
$foreign_field : null;
1068 return $foreignData;
1069 } // end of 'PMA_getForeignData()' function
1072 * Finds all related tables
1074 * @uses $GLOBALS['controllink']
1075 * @uses $GLOBALS['cfgRelation']
1076 * @uses $GLOBALS['db']
1077 * @param string whether to go from master to foreign or vice versa
1078 * @return boolean always TRUE
1079 * @global array $tab_left the list of tables that we still couldn't connect
1080 * @global array $tab_know the list of allready connected tables
1081 * @global string $fromclause
1085 function PMA_getRelatives($from)
1087 global $tab_left, $tab_know, $fromclause;
1089 if ($from == 'master') {
1094 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
1095 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
1097 $rel_query = 'SELECT *'
1098 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db'])
1099 . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
1100 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1101 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1102 . ' AND ' . $from . '_table IN ' . $in_know
1103 . ' AND ' . $to . '_table IN ' . $in_left;
1104 $relations = @PMA_DBI_query
($rel_query, $GLOBALS['controllink']);
1105 while ($row = PMA_DBI_fetch_assoc($relations)) {
1106 $found_table = $row[$to . '_table'];
1107 if (isset($tab_left[$found_table])) {
1109 .= "\n" . ' LEFT JOIN '
1110 . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($row[$to . '_table']) . ' ON '
1111 . PMA_backquote($row[$from . '_table']) . '.'
1112 . PMA_backquote($row[$from . '_field']) . ' = '
1113 . PMA_backquote($row[$to . '_table']) . '.'
1114 . PMA_backquote($row[$to . '_field']) . ' ';
1115 $tab_know[$found_table] = $found_table;
1116 unset($tab_left[$found_table]);
1121 } // end of the "PMA_getRelatives()" function
1124 * Rename a field in relation tables
1126 * usually called after a field in a table was renamed in tbl_alter.php
1128 * @uses PMA_getRelationsParam()
1129 * @uses PMA_backquote()
1130 * @uses PMA_sqlAddslashes()
1131 * @uses PMA_query_as_controluser()
1133 * @param string $table
1134 * @param string $field
1135 * @param string $new_name
1137 function PMA_REL_renameField($db, $table, $field, $new_name)
1139 $cfgRelation = PMA_getRelationsParam();
1141 if ($cfgRelation['displaywork']) {
1142 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
1143 . ' SET display_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1144 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1145 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
1146 . ' AND display_field = \'' . PMA_sqlAddslashes($field) . '\'';
1147 PMA_query_as_controluser($table_query);
1150 if ($cfgRelation['relwork']) {
1151 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1152 . ' SET master_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1153 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
1154 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
1155 . ' AND master_field = \'' . PMA_sqlAddslashes($field) . '\'';
1156 PMA_query_as_controluser($table_query);
1158 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1159 . ' SET foreign_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1160 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
1161 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
1162 . ' AND foreign_field = \'' . PMA_sqlAddslashes($field) . '\'';
1163 PMA_query_as_controluser($table_query);
1170 * @uses __('no description')
1171 * @uses PMA_backquote()
1172 * @uses $GLOBALS['cfgRelation']['db']
1173 * @uses PMA_sqlAddslashes()
1174 * @uses PMA_query_as_controluser()
1175 * @uses PMA_DBI_insert_id()
1176 * @uses $GLOBALS['controllink']
1177 * @param string $newpage
1178 * @param array $cfgRelation
1180 * @param string $query_default_option
1181 * @return string $pdf_page_number
1183 function PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option) {
1184 if (! isset($newpage) ||
$newpage == '') {
1185 $newpage = __('no description');
1187 $ins_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1188 . ' (db_name, page_descr)'
1189 . ' VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($newpage) . '\')';
1190 PMA_query_as_controluser($ins_query, FALSE, $query_default_option);
1191 return PMA_DBI_insert_id(isset($GLOBALS['controllink']) ?
$GLOBALS['controllink'] : '');