2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used with the relation and pdf feature
8 if (! defined('PHPMYADMIN')) {
13 * Executes a query as controluser if possible, otherwise as normal user
15 * @param string the query to execute
16 * @param boolean whether to display SQL error messages or not
18 * @return integer the result set, or false if no result set
23 function PMA_query_as_controluser($sql, $show_error = true, $options = 0)
26 $result = PMA_DBI_query($sql, $GLOBALS['controllink'], $options);
28 $result = @PMA_DBI_try_query
($sql, $GLOBALS['controllink'], $options);
29 } // end if... else...
36 } // end of the "PMA_query_as_controluser()" function
39 * @uses $_SESSION['relation'][$GLOBALS['server']] for caching
40 * @uses $GLOBALS['cfgRelation'] to set it
41 * @uses $GLOBALS['server'] to ensure we are using server-specific pmadb
42 * @uses PMA__getRelationsParam()
43 * @uses PMA_printRelationsParamDiagnostic()
44 * @param bool $verbose whether to print diagnostic info
45 * @return array $cfgRelation
47 function PMA_getRelationsParam($verbose = false)
49 if (empty($_SESSION['relation'][$GLOBALS['server']])) {
50 $_SESSION['relation'][$GLOBALS['server']] = PMA__getRelationsParam();
53 // just for BC but needs to be before PMA_printRelationsParamDiagnostic()
55 $GLOBALS['cfgRelation'] = $_SESSION['relation'][$GLOBALS['server']];
58 PMA_printRelationsParamDiagnostic($_SESSION['relation'][$GLOBALS['server']]);
61 return $_SESSION['relation'][$GLOBALS['server']];
65 * prints out diagnostic info for pma relation feature
67 * @uses $GLOBALS['server']
68 * @uses $GLOBALS['controllink']
69 * @uses $cfg['Server']['pmadb']
71 * @uses PMA_printDiagMessageForFeature()
72 * @uses PMA_printDiagMessageForParameter()
73 * @param array $cfgRelation
75 function PMA_printRelationsParamDiagnostic($cfgRelation)
77 $messages['error'] = '<font color="red"><strong>' . __('not OK')
78 . '</strong></font> [ <a href="Documentation.html#%s" target="documentation">'
79 . __('Documentation') . '</a> ]';
81 $messages['ok'] = '<font color="green"><strong>' . __('OK') . '</strong></font>';
82 $messages['enabled'] = '<font color="green">' . __('Enabled') . '</font>';
83 $messages['disabled'] = '<font color="red">' . __('Disabled') . '</font>';
85 if (false === $GLOBALS['cfg']['Server']['pmadb']) {
86 echo 'PMA Database ... '
87 . sprintf($messages['error'], 'pmadb')
89 . __('General relation features')
90 . ' <font color="green">' . __('Disabled')
95 echo '<table>' . "\n";
97 PMA_printDiagMessageForParameter('pmadb', $GLOBALS['cfg']['Server']['pmadb'], $messages, 'pmadb');
99 PMA_printDiagMessageForParameter('relation', isset($cfgRelation['relation']), $messages, 'relation');
101 PMA_printDiagMessageForFeature(__('General relation features'), 'relwork', $messages);
103 PMA_printDiagMessageForParameter('table_info', isset($cfgRelation['table_info']), $messages, 'table_info');
105 PMA_printDiagMessageForFeature(__('Display Features'), 'displaywork', $messages);
107 PMA_printDiagMessageForParameter('table_coords', isset($cfgRelation['table_coords']), $messages, 'table_coords');
109 PMA_printDiagMessageForParameter('pdf_pages', isset($cfgRelation['pdf_pages']), $messages, 'table_coords');
111 PMA_printDiagMessageForFeature(__('Creation of PDFs'), 'pdfwork', $messages);
113 PMA_printDiagMessageForParameter('column_info', isset($cfgRelation['column_info']), $messages, 'col_com');
115 PMA_printDiagMessageForFeature(__('Displaying Column Comments'), 'commwork', $messages, false);
117 PMA_printDiagMessageForFeature(__('Browser transformation'), 'mimework', $messages);
119 if ($cfgRelation['commwork'] && ! $cfgRelation['mimework']) {
120 echo '<tr><td colspan=2 align="left">' . __('Please see the documentation on how to update your column_comments table') . '</td></tr>' . "\n";
123 PMA_printDiagMessageForParameter('bookmarktable', isset($cfgRelation['bookmark']), $messages, 'bookmark');
125 PMA_printDiagMessageForFeature(__('Bookmarked SQL query'), 'bookmarkwork', $messages);
127 PMA_printDiagMessageForParameter('history', isset($cfgRelation['history']), $messages, 'history');
129 PMA_printDiagMessageForFeature(__('SQL history'), 'historywork', $messages);
131 PMA_printDiagMessageForParameter('designer_coords', isset($cfgRelation['designer_coords']), $messages, 'designer_coords');
133 PMA_printDiagMessageForFeature(__('Designer'), 'designerwork', $messages);
135 PMA_printDiagMessageForParameter('tracking', isset($cfgRelation['tracking']), $messages, 'tracking');
137 PMA_printDiagMessageForFeature(__('Tracking'), 'trackingwork', $messages);
139 echo '</table>' . "\n";
141 echo '<p>' . __('Quick steps to setup advanced features:') . '</p>';
143 echo '<li>' . __('Create the needed tables with the <code>script/create_tables.sql</code>.') . ' ' . PMA_showDocu('linked-tables') . '</li>';
144 echo '<li>' . __('Create a pma user and give access to these tables.') . ' ' . PMA_showDocu('pmausr') . '</li>';
145 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>';
146 echo '<li>' . __('Re-login to phpMyAdmin to load the updated configuration file.') . '</li>';
151 * prints out one diagnostic message for a feature
153 * @param string feature name in a message string
154 * @param string the $GLOBALS['cfgRelation'] parameter to check
155 * @param array utility messages
156 * @param boolean whether to skip a line after the message
158 function PMA_printDiagMessageForFeature($feature_name, $relation_parameter, $messages, $skip_line=true)
160 echo ' <tr><td colspan=2 align="right">' . $feature_name . ': '
161 . ($GLOBALS['cfgRelation'][$relation_parameter] ?
$messages['enabled'] : $messages['disabled'])
162 . '</td></tr>' . "\n";
164 echo ' <tr><td> </td></tr>' . "\n";
169 * prints out one diagnostic message for a configuration parameter
171 * @param string config parameter name to display
172 * @param boolean whether this parameter is set
173 * @param array utility messages
174 * @param string anchor in Documentation.html
176 function PMA_printDiagMessageForParameter($parameter, $relation_parameter_set, $messages, $doc_anchor)
178 echo ' <tr><th align="left">';
179 echo '$cfg[\'Servers\'][$i][\'' . $parameter . '\'] ... </th><td align="right">';
180 echo ($relation_parameter_set ?
$messages['ok'] : sprintf($messages['error'], $doc_anchor)) . '</td></tr>' . "\n";
185 * Defines the relation parameters for the current user
186 * just a copy of the functions used for relations ;-)
187 * but added some stuff to check what will work
189 * @uses $cfg['Server']['user']
190 * @uses $cfg['Server']['pmadb']
191 * @uses $cfg['Server']['verbose_check']
192 * @uses $GLOBALS['server']
193 * @uses $GLOBALS['controllink']
194 * @uses PMA_DBI_QUERY_STORE
195 * @uses PMA_DBI_select_db()
196 * @uses PMA_backquote()
197 * @uses PMA_query_as_controluser()
198 * @uses PMA_DBI_fetch_row()
199 * @uses PMA_DBI_free_result()
201 * @return array the relation parameters for the current user
203 function PMA__getRelationsParam()
205 $cfgRelation = array();
206 $cfgRelation['relwork'] = false;
207 $cfgRelation['displaywork'] = false;
208 $cfgRelation['bookmarkwork']= false;
209 $cfgRelation['pdfwork'] = false;
210 $cfgRelation['commwork'] = false;
211 $cfgRelation['mimework'] = false;
212 $cfgRelation['historywork'] = false;
213 $cfgRelation['trackingwork'] = false;
214 $cfgRelation['designerwork'] = false;
215 $cfgRelation['allworks'] = false;
216 $cfgRelation['user'] = null;
217 $cfgRelation['db'] = null;
219 if ($GLOBALS['server'] == 0 ||
empty($GLOBALS['cfg']['Server']['pmadb'])
220 ||
! PMA_DBI_select_db($GLOBALS['cfg']['Server']['pmadb'], $GLOBALS['controllink'])) {
221 // No server selected -> no bookmark table
222 // we return the array with the falses in it,
223 // to avoid some 'Unitialized string offset' errors later
224 $GLOBALS['cfg']['Server']['pmadb'] = false;
229 $cfgRelation['user'] = $GLOBALS['cfg']['Server']['user'];
230 $cfgRelation['db'] = $GLOBALS['cfg']['Server']['pmadb'];
232 // Now I just check if all tables that i need are present so I can for
233 // example enable relations but not pdf...
234 // I was thinking of checking if they have all required columns but I
235 // fear it might be too slow
237 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($GLOBALS['cfg']['Server']['pmadb']);
238 $tab_rs = PMA_query_as_controluser($tab_query, false, PMA_DBI_QUERY_STORE
);
241 // query failed ... ?
242 //$GLOBALS['cfg']['Server']['pmadb'] = false;
246 while ($curr_table = @PMA_DBI_fetch_row
($tab_rs)) {
247 if ($curr_table[0] == $GLOBALS['cfg']['Server']['bookmarktable']) {
248 $cfgRelation['bookmark'] = $curr_table[0];
249 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['relation']) {
250 $cfgRelation['relation'] = $curr_table[0];
251 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_info']) {
252 $cfgRelation['table_info'] = $curr_table[0];
253 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_coords']) {
254 $cfgRelation['table_coords'] = $curr_table[0];
255 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['designer_coords']) {
256 $cfgRelation['designer_coords'] = $curr_table[0];
257 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['column_info']) {
258 $cfgRelation['column_info'] = $curr_table[0];
259 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['pdf_pages']) {
260 $cfgRelation['pdf_pages'] = $curr_table[0];
261 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['history']) {
262 $cfgRelation['history'] = $curr_table[0];
263 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['tracking']) {
264 $cfgRelation['tracking'] = $curr_table[0];
267 PMA_DBI_free_result($tab_rs);
269 if (isset($cfgRelation['relation'])) {
270 $cfgRelation['relwork'] = true;
271 if (isset($cfgRelation['table_info'])) {
272 $cfgRelation['displaywork'] = true;
275 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
276 $cfgRelation['pdfwork'] = true;
278 if (isset($cfgRelation['column_info'])) {
279 $cfgRelation['commwork'] = true;
281 if ($GLOBALS['cfg']['Server']['verbose_check']) {
282 $mime_query = 'SHOW FIELDS FROM '
283 . PMA_backquote($cfgRelation['db']) . '.'
284 . PMA_backquote($cfgRelation['column_info']);
285 $mime_rs = PMA_query_as_controluser($mime_query, false);
287 $mime_field_mimetype = false;
288 $mime_field_transformation = false;
289 $mime_field_transformation_options = false;
290 while ($curr_mime_field = @PMA_DBI_fetch_row
($mime_rs)) {
291 if ($curr_mime_field[0] == 'mimetype') {
292 $mime_field_mimetype = true;
293 } elseif ($curr_mime_field[0] == 'transformation') {
294 $mime_field_transformation = true;
295 } elseif ($curr_mime_field[0] == 'transformation_options') {
296 $mime_field_transformation_options = true;
299 PMA_DBI_free_result($mime_rs);
301 if ($mime_field_mimetype
302 && $mime_field_transformation
303 && $mime_field_transformation_options) {
304 $cfgRelation['mimework'] = true;
307 $cfgRelation['mimework'] = true;
311 if (isset($cfgRelation['history'])) {
312 $cfgRelation['historywork'] = true;
315 if (isset($cfgRelation['tracking'])) {
316 $cfgRelation['trackingwork'] = true;
319 // we do not absolutely need that the internal relations or the PDF
320 // schema feature be activated
321 if (isset($cfgRelation['designer_coords'])) {
322 $cfgRelation['designerwork'] = true;
325 if (isset($cfgRelation['bookmark'])) {
326 $cfgRelation['bookmarkwork'] = true;
329 if ($cfgRelation['relwork'] && $cfgRelation['displaywork']
330 && $cfgRelation['pdfwork'] && $cfgRelation['commwork']
331 && $cfgRelation['mimework'] && $cfgRelation['historywork']
332 && $cfgRelation['trackingwork']
333 && $cfgRelation['bookmarkwork'] && $cfgRelation['designerwork']) {
334 $cfgRelation['allworks'] = true;
338 } // end of the 'PMA_getRelationsParam()' function
341 * Gets all Relations to foreign tables for a given table or
342 * optionally a given column in a table
345 * @uses $GLOBALS['controllink']
346 * @uses $GLOBALS['information_schema_relations']
347 * @uses PMA_getRelationsParam()
348 * @uses PMA_backquote()
349 * @uses PMA_sqlAddslashes()
350 * @uses PMA_DBI_fetch_result()
351 * @uses PMA_DBI_fetch_value()
352 * @uses PMA_SQP_analyze()
353 * @uses PMA_SQP_parse()
356 * @param string $db the name of the db to check for
357 * @param string $table the name of the table to check for
358 * @param string $column the name of the column to check for
359 * @param string $source the source for foreign key information
360 * @return array db,table,column
362 function PMA_getForeigners($db, $table, $column = '', $source = 'both')
364 $cfgRelation = PMA_getRelationsParam();
367 if ($cfgRelation['relwork'] && ($source == 'both' ||
$source == 'internal')) {
369 SELECT `master_field`,
373 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
374 WHERE `master_db` = \'' . PMA_sqlAddslashes($db) . '\'
375 AND `master_table` = \'' . PMA_sqlAddslashes($table) . '\' ';
376 if (strlen($column)) {
377 $rel_query .= ' AND `master_field` = \'' . PMA_sqlAddslashes($column) . '\'';
379 $foreign = PMA_DBI_fetch_result($rel_query, 'master_field', null, $GLOBALS['controllink']);
382 if (($source == 'both' ||
$source == 'foreign') && strlen($table)) {
383 $show_create_table_query = 'SHOW CREATE TABLE '
384 . PMA_backquote($db) . '.' . PMA_backquote($table);
385 $show_create_table = PMA_DBI_fetch_value($show_create_table_query, 0, 1);
386 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
388 foreach ($analyzed_sql[0]['foreign_keys'] as $one_key) {
389 // The analyzer may return more than one column name in the
390 // index list or the ref_index_list; if this happens,
391 // the current logic just discards the whole index; having
392 // more than one index field is currently unsupported (see FAQ 3.6)
393 if (count($one_key['index_list']) == 1) {
394 foreach ($one_key['index_list'] as $i => $field) {
395 // If a foreign key is defined in the 'internal' source (pmadb)
396 // and as a native foreign key, we won't get it twice
397 // if $source='both' because we use $field as key
399 // The parser looks for a CONSTRAINT clause just before
400 // the FOREIGN KEY clause. It finds it (as output from
401 // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
402 // versions like 3.23.58.
403 // In those cases, the FOREIGN KEY parsing will put numbers
404 // like -1, 0, 1... instead of the constraint number.
406 if (isset($one_key['constraint'])) {
407 $foreign[$field]['constraint'] = $one_key['constraint'];
410 if (isset($one_key['ref_db_name'])) {
411 $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
413 $foreign[$field]['foreign_db'] = $db;
415 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
416 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
417 if (isset($one_key['on_delete'])) {
418 $foreign[$field]['on_delete'] = $one_key['on_delete'];
420 if (isset($one_key['on_update'])) {
421 $foreign[$field]['on_update'] = $one_key['on_update'];
429 * Emulating relations for some information_schema tables
431 if ($db == 'information_schema'
432 && ($source == 'internal' ||
$source == 'both')) {
433 require_once './libraries/information_schema_relations.lib.php';
435 if (isset($GLOBALS['information_schema_relations'][$table])) {
436 foreach ($GLOBALS['information_schema_relations'][$table] as $field => $relations) {
437 if ((! strlen($column) ||
$column == $field)
438 && (! isset($foreign[$field]) ||
! strlen($foreign[$field]))) {
439 $foreign[$field] = $relations;
446 } // end of the 'PMA_getForeigners()' function
449 * Gets the display field of a table
452 * @uses $GLOBALS['controllink']
453 * @uses PMA_getRelationsParam()
454 * @uses PMA_backquote()
455 * @uses PMA_sqlAddslashes()
456 * @uses PMA_DBI_fetch_single_row()
458 * @param string $db the name of the db to check for
459 * @param string $table the name of the table to check for
460 * @return string field name
462 function PMA_getDisplayField($db, $table)
464 $cfgRelation = PMA_getRelationsParam();
467 * Try to fetch the display field from DB.
469 if ($cfgRelation['displaywork']) {
471 SELECT `display_field`
472 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info']) . '
473 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
474 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'';
476 $row = PMA_DBI_fetch_single_row($disp_query, 'ASSOC', $GLOBALS['controllink']);
477 if (isset($row['display_field'])) {
478 return $row['display_field'];
483 * Emulating the display field for some information_schema tables.
485 if ($db == 'information_schema') {
487 case 'CHARACTER_SETS': return 'DESCRIPTION';
488 case 'TABLES': return 'TABLE_COMMENT';
497 } // end of the 'PMA_getDisplayField()' function
500 * Gets the comments for all rows of a table or the db itself
503 * @uses PMA_DBI_get_fields()
504 * @uses PMA_getDbComment()
505 * @param string the name of the db to check for
506 * @param string the name of the table to check for
507 * @return array [field_name] = comment
509 function PMA_getComments($db, $table = '')
514 // MySQL native column comments
515 $fields = PMA_DBI_get_fields($db, $table);
517 foreach ($fields as $key => $field) {
518 if (! empty($field['Comment'])) {
519 $comments[$field['Field']] = $field['Comment'];
524 $comments[] = PMA_getDbComment($db);
528 } // end of the 'PMA_getComments()' function
531 * Gets the comment for a db
534 * @uses PMA_DBI_QUERY_STORE
535 * @uses PMA_DBI_num_rows()
536 * @uses PMA_DBI_fetch_assoc()
537 * @uses PMA_DBI_free_result()
538 * @uses PMA_getRelationsParam()
539 * @uses PMA_backquote()
540 * @uses PMA_sqlAddslashes()
541 * @uses PMA_query_as_controluser()
543 * @param string the name of the db to check for
544 * @return string comment
546 function PMA_getDbComment($db)
548 $cfgRelation = PMA_getRelationsParam();
551 if ($cfgRelation['commwork']) {
552 // pmadb internal db comment
555 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
556 WHERE db_name = '" . PMA_sqlAddslashes($db) . "'
558 AND column_name = '(db_comment)'";
559 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE
);
561 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
562 $row = PMA_DBI_fetch_assoc($com_rs);
563 $comment = $row['comment'];
565 PMA_DBI_free_result($com_rs);
569 } // end of the 'PMA_getDbComment()' function
572 * Gets the comment for a db
575 * @uses PMA_DBI_QUERY_STORE
576 * @uses PMA_DBI_num_rows()
577 * @uses PMA_DBI_fetch_assoc()
578 * @uses PMA_DBI_free_result()
579 * @uses PMA_getRelationsParam()
580 * @uses PMA_backquote()
581 * @uses PMA_sqlAddslashes()
582 * @uses PMA_query_as_controluser()
584 * @param string the name of the db to check for
585 * @return string comment
587 function PMA_getDbComments()
589 $cfgRelation = PMA_getRelationsParam();
592 if ($cfgRelation['commwork']) {
593 // pmadb internal db comment
595 SELECT `db_name`, `comment`
596 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
597 WHERE `column_name` = '(db_comment)'";
598 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE
);
600 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
601 while ($row = PMA_DBI_fetch_assoc($com_rs)) {
602 $comments[$row['db_name']] = $row['comment'];
605 PMA_DBI_free_result($com_rs);
609 } // end of the 'PMA_getDbComments()' function
612 * Set a database comment to a certain value.
614 * @uses PMA_getRelationsParam()
615 * @uses PMA_backquote()
616 * @uses PMA_sqlAddslashes()
617 * @uses PMA_query_as_controluser()
620 * @param string $db the name of the db
621 * @param string $comment the value of the column
622 * @return boolean true, if comment-query was made.
624 function PMA_setDbComment($db, $comment = '')
626 $cfgRelation = PMA_getRelationsParam();
628 if (! $cfgRelation['commwork']) {
632 if (strlen($comment)) {
635 " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
636 (`db_name`, `table_name`, `column_name`, `comment`)
638 '" . PMA_sqlAddslashes($db) . "',
641 '" . PMA_sqlAddslashes($comment) . "')
642 ON DUPLICATE KEY UPDATE
643 `comment` = '" . PMA_sqlAddslashes($comment) . "'";
647 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
648 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
649 AND `table_name` = \'\'
650 AND `column_name` = \'(db_comment)\'';
653 if (isset($upd_query)){
654 return PMA_query_as_controluser($upd_query);
658 } // end of 'PMA_setDbComment()' function
661 * Set a SQL history entry
663 * @uses $_SESSION['sql_history']
664 * @uses $cfg['QueryHistoryDB']
665 * @uses $cfg['QueryHistoryMax']
666 * @uses PMA_getRelationsParam()
667 * @uses PMA_query_as_controluser()
668 * @uses PMA_backquote()
669 * @uses PMA_sqlAddslashes()
672 * @uses array_shift()
673 * @param string $db the name of the db
674 * @param string $table the name of the table
675 * @param string $username the username
676 * @param string $sqlquery the sql query
679 function PMA_setHistory($db, $table, $username, $sqlquery)
681 if (strlen($sqlquery) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
685 $cfgRelation = PMA_getRelationsParam();
687 if (! isset($_SESSION['sql_history'])) {
688 $_SESSION['sql_history'] = array();
691 $key = md5($sqlquery . $db . $table);
693 if (isset($_SESSION['sql_history'][$key])) {
694 unset($_SESSION['sql_history'][$key]);
697 $_SESSION['sql_history'][$key] = array(
700 'sqlquery' => $sqlquery,
703 if (count($_SESSION['sql_history']) > $GLOBALS['cfg']['QueryHistoryMax']) {
704 // history should not exceed a maximum count
705 array_shift($_SESSION['sql_history']);
708 if (! $cfgRelation['historywork'] ||
! $GLOBALS['cfg']['QueryHistoryDB']) {
712 PMA_query_as_controluser('
714 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
721 (\'' . PMA_sqlAddslashes($username) . '\',
722 \'' . PMA_sqlAddslashes($db) . '\',
723 \'' . PMA_sqlAddslashes($table) . '\',
725 \'' . PMA_sqlAddslashes($sqlquery) . '\')');
726 } // end of 'PMA_setHistory()' function
729 * Gets a SQL history entry
731 * @uses $_SESSION['sql_history']
732 * @uses $GLOBALS['controllink']
733 * @uses PMA_getRelationsParam()
734 * @uses PMA_backquote()
735 * @uses PMA_sqlAddslashes()
736 * @uses PMA_DBI_fetch_result()
737 * @uses array_reverse()
738 * @param string $username the username
739 * @return array list of history items
742 function PMA_getHistory($username)
744 $cfgRelation = PMA_getRelationsParam();
746 if (isset($_SESSION['sql_history'])) {
747 return array_reverse($_SESSION['sql_history']);
750 if (! $cfgRelation['historywork']) {
758 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
759 WHERE `username` = \'' . PMA_sqlAddslashes($username) . '\'
762 return PMA_DBI_fetch_result($hist_query, null, null, $GLOBALS['controllink']);
763 } // end of 'PMA_getHistory()' function
768 * deletes entries that exceeds $cfg['QueryHistoryMax'], oldest first, for the
771 * @uses $cfg['QueryHistoryMax']
772 * @uses $cfg['QueryHistoryDB']
773 * @uses $GLOBALS['controllink']
774 * @uses PMA_backquote()
775 * @uses PMA_sqlAddSlashes()
776 * @uses PMA_query_as_controluser()
777 * @uses PMA_DBI_fetch_value()
778 * @param string $username the username
781 function PMA_purgeHistory($username)
783 $cfgRelation = PMA_getRelationsParam();
784 if (! $GLOBALS['cfg']['QueryHistoryDB'] ||
! $cfgRelation['historywork']) {
788 if (! $cfgRelation['historywork']) {
794 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
795 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
796 ORDER BY `timevalue` DESC
797 LIMIT ' . $GLOBALS['cfg']['QueryHistoryMax'] . ', 1';
799 if ($max_time = PMA_DBI_fetch_value($search_query, 0, 0, $GLOBALS['controllink'])) {
800 PMA_query_as_controluser('
802 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
803 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
804 AND `timevalue` <= \'' . $max_time . '\'');
806 } // end of 'PMA_purgeHistory()' function
809 * Prepares the dropdown for one mode
811 * @uses $cfg['LimitChars']
812 * @uses $cfg['NaturalOrder']
814 * @uses htmlspecialchars()
818 * @uses natcasesort()
820 * @param array $foreign the keys and values for foreigns
821 * @param string $data the current data of the dropdown
822 * @param string $mode the needed mode
824 * @return array the <option value=""><option>s
828 function PMA__foreignDropdownBuild($foreign, $data, $mode)
830 $reloptions = array();
832 if ($mode == 'id-content') {
833 // sort for id-content
834 if ($GLOBALS['cfg']['NaturalOrder']) {
835 uksort($foreign, 'strnatcasecmp');
839 } elseif ($mode == 'content-id') {
840 // sort for content-id
841 if ($GLOBALS['cfg']['NaturalOrder']) {
842 natcasesort($foreign);
848 foreach ($foreign as $key => $value) {
850 if (PMA_strlen($value) <= $GLOBALS['cfg']['LimitChars']) {
852 $value = htmlspecialchars($value);
854 $vtitle = htmlspecialchars($value);
855 $value = htmlspecialchars(substr($value, 0, $GLOBALS['cfg']['LimitChars']) . '...');
858 $reloption = ' <option value="' . htmlspecialchars($key) . '"';
860 $reloption .= ' title="' . $vtitle . '"';
863 if ((string) $key == (string) $data) {
864 $reloption .= ' selected="selected"';
867 if ($mode == 'content-id') {
868 $reloptions[] = $reloption . '>' . $value . ' - ' . htmlspecialchars($key) . '</option>' . "\n";
870 $reloptions[] = $reloption . '>' . htmlspecialchars($key) . ' - ' . $value . '</option>' . "\n";
875 } // end of 'PMA__foreignDropdownBuild' function
878 * Outputs dropdown with values of foreign fields
880 * @uses $cfg['ForeignKeyMaxLimit']
881 * @uses $cfg['ForeignKeyDropdownOrder']
882 * @uses PMA__foreignDropdownBuild()
883 * @uses PMA_isValid()
885 * @param array array of the displayed row
886 * @param string the foreign field
887 * @param string the foreign field to display
888 * @param string the current data of the dropdown (field in row)
889 * @return string the <option value=""><option>s
892 function PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, $data,
896 $max = $GLOBALS['cfg']['ForeignKeyMaxLimit'];
902 foreach ($disp_row as $relrow) {
903 $key = $relrow[$foreign_field];
905 // if the display field has been defined for this foreign table
906 if ($foreign_display) {
907 $value = $relrow[$foreign_display];
910 } // end if ($foreign_display)
912 $foreign[$key] = $value;
915 // put the dropdown sections in correct order
918 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'], 'array')) {
919 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][0])) {
920 $top = PMA__foreignDropdownBuild($foreign, $data,
921 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][0]);
923 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][1])) {
924 $bot = PMA__foreignDropdownBuild($foreign, $data,
925 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][1]);
928 $top = PMA__foreignDropdownBuild($foreign, $data, 'id-content');
929 $bot = PMA__foreignDropdownBuild($foreign, $data, 'content-id');
932 // beginning of dropdown
933 $ret = '<option value=""> </option>' . "\n";
935 $top_count = count($top);
936 if ($max == -1 ||
$top_count < $max) {
937 $ret .= implode('', $top);
938 if ($top_count > 0) {
939 $ret .= ' <option value=""> </option>' . "\n";
940 $ret .= ' <option value=""> </option>' . "\n";
943 $ret .= implode('', $bot);
946 } // end of 'PMA_foreignDropdown()' function
949 * Gets foreign keys in preparation for a drop-down selector
951 * @uses PMA_Table::countRecords()
952 * @uses PMA_backquote()
953 * @uses PMA_getDisplayField()
954 * @uses PMA_sqlAddslashes()
955 * @uses PMA_DBI_fetch_value()
956 * @uses PMA_DBI_free_result()
957 * @uses PMA_DBI_query()
958 * @uses PMA_DBI_num_rows()
959 * @uses PMA_DBI_fetch_assoc()
960 * @param array array of the foreign keys
961 * @param string the foreign field name
962 * @param bool whether to override the total
963 * @param string a possible filter
964 * @param string a possible LIMIT clause
965 * @return array data about the foreign keys
969 function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filter, $foreign_limit)
971 // we always show the foreign field in the drop-down; if a display
972 // field is defined, we show it besides the foreign field
973 $foreign_link = false;
974 if ($foreigners && isset($foreigners[$field])) {
975 $foreigner = $foreigners[$field];
976 $foreign_db = $foreigner['foreign_db'];
977 $foreign_table = $foreigner['foreign_table'];
978 $foreign_field = $foreigner['foreign_field'];
980 // Count number of rows in the foreign table. Currently we do
981 // not use a drop-down if more than 200 rows in the foreign table,
982 // for speed reasons and because we need a better interface for this.
984 // We could also do the SELECT anyway, with a LIMIT, and ensure that
985 // the current value of the field is one of the choices.
987 $the_total = PMA_Table
::countRecords($foreign_db, $foreign_table);
989 if ($override_total == true ||
$the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']) {
990 // foreign_display can be FALSE if no display field defined:
991 $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
993 $f_query_main = 'SELECT ' . PMA_backquote($foreign_field)
994 . (($foreign_display == FALSE) ?
'' : ', ' . PMA_backquote($foreign_display));
995 $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table);
996 $f_query_filter = empty($foreign_filter) ?
'' : ' WHERE ' . PMA_backquote($foreign_field)
997 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
998 . (($foreign_display == FALSE) ?
'' : ' OR ' . PMA_backquote($foreign_display)
999 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1001 $f_query_order = ($foreign_display == FALSE) ?
'' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display);
1002 $f_query_limit = isset($foreign_limit) ?
$foreign_limit : '';
1004 if (!empty($foreign_filter)) {
1005 $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter);
1007 $the_total = PMA_DBI_fetch_value($res);
1008 @PMA_DBI_free_result
($res);
1014 $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit);
1015 if ($disp && PMA_DBI_num_rows($disp) > 0) {
1016 // If a resultset has been created, pre-cache it in the $disp_row array
1017 // This helps us from not needing to use mysql_data_seek by accessing a pre-cached
1018 // PHP array. Usually those resultsets are not that big, so a performance hit should
1020 $disp_row = array();
1021 while ($single_disp_row = @PMA_DBI_fetch_assoc
($disp)) {
1022 $disp_row[] = $single_disp_row;
1024 @PMA_DBI_free_result
($disp);
1028 $foreign_link = true;
1030 } // end if $foreigners
1032 $foreignData['foreign_link'] = $foreign_link;
1033 $foreignData['the_total'] = isset($the_total) ?
$the_total : null;
1034 $foreignData['foreign_display'] = isset($foreign_display) ?
$foreign_display : null;
1035 $foreignData['disp_row'] = isset($disp_row) ?
$disp_row : null;
1036 $foreignData['foreign_field'] = isset($foreign_field) ?
$foreign_field : null;
1037 return $foreignData;
1038 } // end of 'PMA_getForeignData()' function
1041 * Finds all related tables
1043 * @uses $GLOBALS['controllink']
1044 * @uses $GLOBALS['cfgRelation']
1045 * @uses $GLOBALS['db']
1046 * @param string whether to go from master to foreign or vice versa
1047 * @return boolean always TRUE
1048 * @global array $tab_left the list of tables that we still couldn't connect
1049 * @global array $tab_know the list of allready connected tables
1050 * @global string $fromclause
1054 function PMA_getRelatives($from)
1056 global $tab_left, $tab_know, $fromclause;
1058 if ($from == 'master') {
1063 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
1064 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
1066 $rel_query = 'SELECT *'
1067 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db'])
1068 . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
1069 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1070 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1071 . ' AND ' . $from . '_table IN ' . $in_know
1072 . ' AND ' . $to . '_table IN ' . $in_left;
1073 $relations = @PMA_DBI_query
($rel_query, $GLOBALS['controllink']);
1074 while ($row = PMA_DBI_fetch_assoc($relations)) {
1075 $found_table = $row[$to . '_table'];
1076 if (isset($tab_left[$found_table])) {
1078 .= "\n" . ' LEFT JOIN '
1079 . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($row[$to . '_table']) . ' ON '
1080 . PMA_backquote($row[$from . '_table']) . '.'
1081 . PMA_backquote($row[$from . '_field']) . ' = '
1082 . PMA_backquote($row[$to . '_table']) . '.'
1083 . PMA_backquote($row[$to . '_field']) . ' ';
1084 $tab_know[$found_table] = $found_table;
1085 unset($tab_left[$found_table]);
1090 } // end of the "PMA_getRelatives()" function
1093 * Rename a field in relation tables
1095 * usually called after a field in a table was renamed in tbl_alter.php
1097 * @uses PMA_getRelationsParam()
1098 * @uses PMA_backquote()
1099 * @uses PMA_sqlAddslashes()
1100 * @uses PMA_query_as_controluser()
1102 * @param string $table
1103 * @param string $field
1104 * @param string $new_name
1106 function PMA_REL_renameField($db, $table, $field, $new_name)
1108 $cfgRelation = PMA_getRelationsParam();
1110 if ($cfgRelation['displaywork']) {
1111 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
1112 . ' SET display_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1113 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1114 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
1115 . ' AND display_field = \'' . PMA_sqlAddslashes($field) . '\'';
1116 PMA_query_as_controluser($table_query);
1119 if ($cfgRelation['relwork']) {
1120 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1121 . ' SET master_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1122 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
1123 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
1124 . ' AND master_field = \'' . PMA_sqlAddslashes($field) . '\'';
1125 PMA_query_as_controluser($table_query);
1127 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1128 . ' SET foreign_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1129 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
1130 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
1131 . ' AND foreign_field = \'' . PMA_sqlAddslashes($field) . '\'';
1132 PMA_query_as_controluser($table_query);
1139 * @uses PMA_backquote()
1140 * @uses $GLOBALS['cfgRelation']['db']
1141 * @uses PMA_sqlAddslashes()
1142 * @uses PMA_query_as_controluser()
1143 * @uses PMA_DBI_insert_id()
1144 * @uses $GLOBALS['controllink']
1145 * @param string $newpage
1146 * @param array $cfgRelation
1148 * @param string $query_default_option
1149 * @return string $pdf_page_number
1151 function PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option) {
1152 if (! isset($newpage) ||
$newpage == '') {
1153 $newpage = __('no description');
1155 $ins_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1156 . ' (db_name, page_descr)'
1157 . ' VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($newpage) . '\')';
1158 PMA_query_as_controluser($ins_query, FALSE, $query_default_option);
1159 return PMA_DBI_insert_id(isset($GLOBALS['controllink']) ?
$GLOBALS['controllink'] : '');