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
28 * @author Mike Beck <mikebeck@users.sourceforge.net>
30 function PMA_query_as_controluser($sql, $show_error = true, $options = 0)
33 $result = PMA_DBI_query($sql, $GLOBALS['controllink'], $options);
35 $result = @PMA_DBI_try_query
($sql, $GLOBALS['controllink'], $options);
36 } // end if... else...
43 } // end of the "PMA_query_as_controluser()" function
46 * @uses $_SESSION['relation'][$GLOBALS['server']] for caching
47 * @uses $GLOBALS['cfgRelation'] to set it
48 * @uses $GLOBALS['server'] to ensure we are using server-specific pmadb
49 * @uses PMA__getRelationsParam()
50 * @uses PMA_printRelationsParamDiagnostic()
51 * @param bool $verbose whether to print diagnostic info
52 * @return array $cfgRelation
54 function PMA_getRelationsParam($verbose = false)
56 if (empty($_SESSION['relation'][$GLOBALS['server']])) {
57 $_SESSION['relation'][$GLOBALS['server']] = PMA__getRelationsParam();
60 // just for BC but needs to be before PMA_printRelationsParamDiagnostic()
62 $GLOBALS['cfgRelation'] = $_SESSION['relation'][$GLOBALS['server']];
65 PMA_printRelationsParamDiagnostic($_SESSION['relation'][$GLOBALS['server']]);
68 return $_SESSION['relation'][$GLOBALS['server']];
72 * prints out diagnostic info for pma relation feature
74 * @uses $GLOBALS['server']
75 * @uses $GLOBALS['controllink']
76 * @uses $GLOBALS['strNotOK']
77 * @uses $GLOBALS['strDocu']
78 * @uses $GLOBALS['strGeneralRelationFeat']
79 * @uses $GLOBALS['strDisabled']
80 * @uses $GLOBALS['strEnabled']
81 * @uses $GLOBALS['strDisplayFeat']
82 * @uses $GLOBALS['strCreatePdfFeat']
83 * @uses $GLOBALS['strColComFeat']
84 * @uses $GLOBALS['strBookmarkQuery']
85 * @uses $GLOBALS['strUpdComTab']
86 * @uses $GLOBALS['strQuerySQLHistory']
87 * @uses $GLOBALS['strDesigner']
88 * @uses $cfg['Server']['pmadb']
90 * @uses PMA_printDiagMessageForFeature()
91 * @uses PMA_printDiagMessageForParameter()
92 * @param array $cfgRelation
94 function PMA_printRelationsParamDiagnostic($cfgRelation)
96 $messages['error'] = '<font color="red"><strong>' . $GLOBALS['strNotOK']
97 . '</strong></font> [ <a href="Documentation.html#%s" target="documentation">'
98 . $GLOBALS['strDocu'] . '</a> ]';
100 $messages['ok'] = '<font color="green"><strong>' . $GLOBALS['strOK'] . '</strong></font>';
101 $messages['enabled'] = '<font color="green">' . $GLOBALS['strEnabled'] . '</font>';
102 $messages['disabled'] = '<font color="red">' . $GLOBALS['strDisabled'] . '</font>';
104 if (false === $GLOBALS['cfg']['Server']['pmadb']) {
105 echo 'PMA Database ... '
106 . sprintf($messages['error'], 'pmadb')
108 . $GLOBALS['strGeneralRelationFeat']
109 . ' <font color="green">' . $GLOBALS['strDisabled']
114 echo '<table>' . "\n";
116 PMA_printDiagMessageForParameter('pmadb', $GLOBALS['cfg']['Server']['pmadb'], $messages, 'pmadb');
118 PMA_printDiagMessageForParameter('relation', isset($cfgRelation['relation']), $messages, 'relation');
120 PMA_printDiagMessageForFeature('strGeneralRelationFeat', 'relwork', $messages);
122 PMA_printDiagMessageForParameter('table_info', isset($cfgRelation['table_info']), $messages, 'table_info');
124 PMA_printDiagMessageForFeature('strDisplayFeat', 'displaywork', $messages);
126 PMA_printDiagMessageForParameter('table_coords', isset($cfgRelation['table_coords']), $messages, 'table_coords');
128 PMA_printDiagMessageForParameter('pdf_pages', isset($cfgRelation['pdf_pages']), $messages, 'table_coords');
130 PMA_printDiagMessageForFeature('strCreatePdfFeat', 'pdfwork', $messages);
132 PMA_printDiagMessageForParameter('column_info', isset($cfgRelation['column_info']), $messages, 'col_com');
134 PMA_printDiagMessageForFeature('strColComFeat', 'commwork', $messages, false);
136 PMA_printDiagMessageForFeature('strMIME_transformation', 'mimework', $messages);
138 if ($cfgRelation['commwork'] && ! $cfgRelation['mimework']) {
139 echo '<tr><td colspan=2 align="left">' . $GLOBALS['strUpdComTab'] . '</td></tr>' . "\n";
142 PMA_printDiagMessageForParameter('bookmarktable', isset($cfgRelation['bookmark']), $messages, 'bookmark');
144 PMA_printDiagMessageForFeature('strBookmarkQuery', 'bookmarkwork', $messages);
146 PMA_printDiagMessageForParameter('history', isset($cfgRelation['history']), $messages, 'history');
148 PMA_printDiagMessageForFeature('strQuerySQLHistory', 'historywork', $messages);
150 PMA_printDiagMessageForParameter('designer_coords', isset($cfgRelation['designer_coords']), $messages, 'designer_coords');
152 PMA_printDiagMessageForFeature('strDesigner', 'designerwork', $messages);
154 PMA_printDiagMessageForParameter('tracking', isset($cfgRelation['tracking']), $messages, 'tracking');
156 PMA_printDiagMessageForFeature('strTracking', 'trackingwork', $messages);
158 echo '</table>' . "\n";
162 * prints out one diagnostic message for a feature
164 * @param string feature name in a message string
165 * @param string the $GLOBALS['cfgRelation'] parameter to check
166 * @param array utility messages
167 * @param boolean whether to skip a line after the message
169 function PMA_printDiagMessageForFeature($feature_name, $relation_parameter, $messages, $skip_line=true)
171 echo ' <tr><td colspan=2 align="right">' . $GLOBALS[$feature_name] . ': '
172 . ($GLOBALS['cfgRelation'][$relation_parameter] ?
$messages['enabled'] : $messages['disabled'])
173 . '</td></tr>' . "\n";
175 echo ' <tr><td> </td></tr>' . "\n";
180 * prints out one diagnostic message for a configuration parameter
182 * @param string config parameter name to display
183 * @param boolean whether this parameter is set
184 * @param array utility messages
185 * @param string anchor in Documentation.html
187 function PMA_printDiagMessageForParameter($parameter, $relation_parameter_set, $messages, $doc_anchor)
189 echo ' <tr><th align="left">';
190 echo '$cfg[\'Servers\'][$i][\'' . $parameter . '\'] ... </th><td align="right">';
191 echo ($relation_parameter_set ?
$messages['ok'] : sprintf($messages['error'], $doc_anchor)) . '</td></tr>' . "\n";
196 * Defines the relation parameters for the current user
197 * just a copy of the functions used for relations ;-)
198 * but added some stuff to check what will work
200 * @uses $cfg['Server']['user']
201 * @uses $cfg['Server']['pmadb']
202 * @uses $cfg['Server']['verbose_check']
203 * @uses $GLOBALS['server']
204 * @uses $GLOBALS['controllink']
205 * @uses PMA_DBI_QUERY_STORE
206 * @uses PMA_DBI_select_db()
207 * @uses PMA_backquote()
208 * @uses PMA_query_as_controluser()
209 * @uses PMA_DBI_fetch_row()
210 * @uses PMA_DBI_free_result()
212 * @author Mike Beck <mikebeck@users.sourceforge.net>
213 * @return array the relation parameters for the current user
215 function PMA__getRelationsParam()
217 $cfgRelation = array();
218 $cfgRelation['relwork'] = false;
219 $cfgRelation['displaywork'] = false;
220 $cfgRelation['bookmarkwork']= false;
221 $cfgRelation['pdfwork'] = false;
222 $cfgRelation['commwork'] = false;
223 $cfgRelation['mimework'] = false;
224 $cfgRelation['historywork'] = false;
225 $cfgRelation['trackingwork'] = false;
226 $cfgRelation['designerwork'] = false;
227 $cfgRelation['allworks'] = false;
228 $cfgRelation['user'] = null;
229 $cfgRelation['db'] = null;
231 if ($GLOBALS['server'] == 0 ||
empty($GLOBALS['cfg']['Server']['pmadb'])
232 ||
! PMA_DBI_select_db($GLOBALS['cfg']['Server']['pmadb'], $GLOBALS['controllink'])) {
233 // No server selected -> no bookmark table
234 // we return the array with the falses in it,
235 // to avoid some 'Unitialized string offset' errors later
236 $GLOBALS['cfg']['Server']['pmadb'] = false;
241 $cfgRelation['user'] = $GLOBALS['cfg']['Server']['user'];
242 $cfgRelation['db'] = $GLOBALS['cfg']['Server']['pmadb'];
244 // Now I just check if all tables that i need are present so I can for
245 // example enable relations but not pdf...
246 // I was thinking of checking if they have all required columns but I
247 // fear it might be too slow
249 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($GLOBALS['cfg']['Server']['pmadb']);
250 $tab_rs = PMA_query_as_controluser($tab_query, false, PMA_DBI_QUERY_STORE
);
253 // query failed ... ?
254 //$GLOBALS['cfg']['Server']['pmadb'] = false;
258 while ($curr_table = @PMA_DBI_fetch_row
($tab_rs)) {
259 if ($curr_table[0] == $GLOBALS['cfg']['Server']['bookmarktable']) {
260 $cfgRelation['bookmark'] = $curr_table[0];
261 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['relation']) {
262 $cfgRelation['relation'] = $curr_table[0];
263 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_info']) {
264 $cfgRelation['table_info'] = $curr_table[0];
265 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_coords']) {
266 $cfgRelation['table_coords'] = $curr_table[0];
267 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['designer_coords']) {
268 $cfgRelation['designer_coords'] = $curr_table[0];
269 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['column_info']) {
270 $cfgRelation['column_info'] = $curr_table[0];
271 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['pdf_pages']) {
272 $cfgRelation['pdf_pages'] = $curr_table[0];
273 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['history']) {
274 $cfgRelation['history'] = $curr_table[0];
275 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['tracking']) {
276 $cfgRelation['tracking'] = $curr_table[0];
279 PMA_DBI_free_result($tab_rs);
281 if (isset($cfgRelation['relation'])) {
282 $cfgRelation['relwork'] = true;
283 if (isset($cfgRelation['table_info'])) {
284 $cfgRelation['displaywork'] = true;
287 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
288 $cfgRelation['pdfwork'] = true;
290 if (isset($cfgRelation['column_info'])) {
291 $cfgRelation['commwork'] = true;
293 if ($GLOBALS['cfg']['Server']['verbose_check']) {
294 $mime_query = 'SHOW FIELDS FROM '
295 . PMA_backquote($cfgRelation['db']) . '.'
296 . PMA_backquote($cfgRelation['column_info']);
297 $mime_rs = PMA_query_as_controluser($mime_query, false);
299 $mime_field_mimetype = false;
300 $mime_field_transformation = false;
301 $mime_field_transformation_options = false;
302 while ($curr_mime_field = @PMA_DBI_fetch_row
($mime_rs)) {
303 if ($curr_mime_field[0] == 'mimetype') {
304 $mime_field_mimetype = true;
305 } elseif ($curr_mime_field[0] == 'transformation') {
306 $mime_field_transformation = true;
307 } elseif ($curr_mime_field[0] == 'transformation_options') {
308 $mime_field_transformation_options = true;
311 PMA_DBI_free_result($mime_rs);
313 if ($mime_field_mimetype
314 && $mime_field_transformation
315 && $mime_field_transformation_options) {
316 $cfgRelation['mimework'] = true;
319 $cfgRelation['mimework'] = true;
323 if (isset($cfgRelation['history'])) {
324 $cfgRelation['historywork'] = true;
327 if (isset($cfgRelation['tracking'])) {
328 $cfgRelation['trackingwork'] = true;
331 // we do not absolutely need that the internal relations or the PDF
332 // schema feature be activated
333 if (isset($cfgRelation['designer_coords'])) {
334 $cfgRelation['designerwork'] = true;
337 if (isset($cfgRelation['bookmark'])) {
338 $cfgRelation['bookmarkwork'] = true;
341 if ($cfgRelation['relwork'] && $cfgRelation['displaywork']
342 && $cfgRelation['pdfwork'] && $cfgRelation['commwork']
343 && $cfgRelation['mimework'] && $cfgRelation['historywork']
344 && $cfgRelation['trackingwork']
345 && $cfgRelation['bookmarkwork'] && $cfgRelation['designerwork']) {
346 $cfgRelation['allworks'] = true;
350 } // end of the 'PMA_getRelationsParam()' function
353 * Gets all Relations to foreign tables for a given table or
354 * optionally a given column in a table
356 * @author Mike Beck <mikebeck@users.sourceforge.net>
357 * @author Marc Delisle
359 * @uses $GLOBALS['controllink']
360 * @uses $GLOBALS['information_schema_relations']
361 * @uses PMA_getRelationsParam()
362 * @uses PMA_backquote()
363 * @uses PMA_sqlAddslashes()
364 * @uses PMA_DBI_fetch_result()
365 * @uses PMA_DBI_fetch_value()
366 * @uses PMA_SQP_analyze()
367 * @uses PMA_SQP_parse()
370 * @param string $db the name of the db to check for
371 * @param string $table the name of the table to check for
372 * @param string $column the name of the column to check for
373 * @param string $source the source for foreign key information
374 * @return array db,table,column
376 function PMA_getForeigners($db, $table, $column = '', $source = 'both')
378 $cfgRelation = PMA_getRelationsParam();
381 if ($cfgRelation['relwork'] && ($source == 'both' ||
$source == 'internal')) {
383 SELECT `master_field`,
387 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
388 WHERE `master_db` = \'' . PMA_sqlAddslashes($db) . '\'
389 AND `master_table` = \'' . PMA_sqlAddslashes($table) . '\' ';
390 if (strlen($column)) {
391 $rel_query .= ' AND `master_field` = \'' . PMA_sqlAddslashes($column) . '\'';
393 $foreign = PMA_DBI_fetch_result($rel_query, 'master_field', null, $GLOBALS['controllink']);
396 if (($source == 'both' ||
$source == 'foreign') && strlen($table)) {
397 $show_create_table_query = 'SHOW CREATE TABLE '
398 . PMA_backquote($db) . '.' . PMA_backquote($table);
399 $show_create_table = PMA_DBI_fetch_value($show_create_table_query, 0, 1);
400 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
402 foreach ($analyzed_sql[0]['foreign_keys'] as $one_key) {
403 // The analyzer may return more than one column name in the
404 // index list or the ref_index_list; if this happens,
405 // the current logic just discards the whole index; having
406 // more than one index field is currently unsupported (see FAQ 3.6)
407 if (count($one_key['index_list']) == 1) {
408 foreach ($one_key['index_list'] as $i => $field) {
409 // If a foreign key is defined in the 'internal' source (pmadb)
410 // and as a native foreign key, we won't get it twice
411 // if $source='both' because we use $field as key
413 // The parser looks for a CONSTRAINT clause just before
414 // the FOREIGN KEY clause. It finds it (as output from
415 // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
416 // versions like 3.23.58.
417 // In those cases, the FOREIGN KEY parsing will put numbers
418 // like -1, 0, 1... instead of the constraint number.
420 if (isset($one_key['constraint'])) {
421 $foreign[$field]['constraint'] = $one_key['constraint'];
424 if (isset($one_key['ref_db_name'])) {
425 $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
427 $foreign[$field]['foreign_db'] = $db;
429 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
430 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
431 if (isset($one_key['on_delete'])) {
432 $foreign[$field]['on_delete'] = $one_key['on_delete'];
434 if (isset($one_key['on_update'])) {
435 $foreign[$field]['on_update'] = $one_key['on_update'];
443 * Emulating relations for some information_schema tables
445 if ($db == 'information_schema'
446 && ($source == 'internal' ||
$source == 'both')) {
447 require_once './libraries/information_schema_relations.lib.php';
449 if (isset($GLOBALS['information_schema_relations'][$table])) {
450 foreach ($GLOBALS['information_schema_relations'][$table] as $field => $relations) {
451 if ((! strlen($column) ||
$column == $field)
452 && (! isset($foreign[$field]) ||
! strlen($foreign[$field]))) {
453 $foreign[$field] = $relations;
460 } // end of the 'PMA_getForeigners()' function
463 * Gets the display field of a table
466 * @author Mike Beck <mikebeck@users.sourceforge.net>
467 * @uses $GLOBALS['controllink']
468 * @uses PMA_getRelationsParam()
469 * @uses PMA_backquote()
470 * @uses PMA_sqlAddslashes()
471 * @uses PMA_DBI_fetch_single_row()
473 * @param string $db the name of the db to check for
474 * @param string $table the name of the table to check for
475 * @return string field name
477 function PMA_getDisplayField($db, $table)
479 $cfgRelation = PMA_getRelationsParam();
482 * Try to fetch the display field from DB.
484 if ($cfgRelation['displaywork']) {
486 SELECT `display_field`
487 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info']) . '
488 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
489 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'';
491 $row = PMA_DBI_fetch_single_row($disp_query, 'ASSOC', $GLOBALS['controllink']);
492 if (isset($row['display_field'])) {
493 return $row['display_field'];
498 * Emulating the display field for some information_schema tables.
500 if ($db == 'information_schema') {
502 case 'CHARACTER_SETS': return 'DESCRIPTION';
503 case 'TABLES': return 'TABLE_COMMENT';
512 } // end of the 'PMA_getDisplayField()' function
515 * Gets the comments for all rows of a table or the db itself
517 * @author Mike Beck <mikebeck@users.sourceforge.net>
520 * @uses PMA_DBI_get_fields()
521 * @uses PMA_getDbComment()
522 * @param string the name of the db to check for
523 * @param string the name of the table to check for
524 * @return array [field_name] = comment
526 function PMA_getComments($db, $table = '')
531 // MySQL native column comments
532 $fields = PMA_DBI_get_fields($db, $table);
534 foreach ($fields as $key => $field) {
535 if (! empty($field['Comment'])) {
536 $comments[$field['Field']] = $field['Comment'];
541 $comments[] = PMA_getDbComment($db);
545 } // end of the 'PMA_getComments()' function
548 * Gets the comment for a db
550 * @author Mike Beck <mikebeck@users.sourceforge.net>
553 * @uses PMA_DBI_QUERY_STORE
554 * @uses PMA_DBI_num_rows()
555 * @uses PMA_DBI_fetch_assoc()
556 * @uses PMA_DBI_free_result()
557 * @uses PMA_getRelationsParam()
558 * @uses PMA_backquote()
559 * @uses PMA_sqlAddslashes()
560 * @uses PMA_query_as_controluser()
562 * @param string the name of the db to check for
563 * @return string comment
565 function PMA_getDbComment($db)
567 $cfgRelation = PMA_getRelationsParam();
570 if ($cfgRelation['commwork']) {
571 // pmadb internal db comment
574 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
575 WHERE db_name = '" . PMA_sqlAddslashes($db) . "'
577 AND column_name = '(db_comment)'";
578 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE
);
580 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
581 $row = PMA_DBI_fetch_assoc($com_rs);
582 $comment = $row['comment'];
584 PMA_DBI_free_result($com_rs);
588 } // end of the 'PMA_getDbComment()' function
591 * Gets the comment for a db
593 * @author Mike Beck <mikebeck@users.sourceforge.net>
596 * @uses PMA_DBI_QUERY_STORE
597 * @uses PMA_DBI_num_rows()
598 * @uses PMA_DBI_fetch_assoc()
599 * @uses PMA_DBI_free_result()
600 * @uses PMA_getRelationsParam()
601 * @uses PMA_backquote()
602 * @uses PMA_sqlAddslashes()
603 * @uses PMA_query_as_controluser()
605 * @param string the name of the db to check for
606 * @return string comment
608 function PMA_getDbComments()
610 $cfgRelation = PMA_getRelationsParam();
613 if ($cfgRelation['commwork']) {
614 // pmadb internal db comment
616 SELECT `db_name`, `comment`
617 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
618 WHERE `column_name` = '(db_comment)'";
619 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE
);
621 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
622 while ($row = PMA_DBI_fetch_assoc($com_rs)) {
623 $comments[$row['db_name']] = $row['comment'];
626 PMA_DBI_free_result($com_rs);
630 } // end of the 'PMA_getDbComments()' function
633 * Set a database comment to a certain value.
635 * @uses PMA_getRelationsParam()
636 * @uses PMA_backquote()
637 * @uses PMA_sqlAddslashes()
638 * @uses PMA_query_as_controluser()
641 * @param string $db the name of the db
642 * @param string $comment the value of the column
643 * @return boolean true, if comment-query was made.
645 function PMA_setDbComment($db, $comment = '')
647 $cfgRelation = PMA_getRelationsParam();
649 if (! $cfgRelation['commwork']) {
653 if (strlen($comment)) {
656 " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
657 (`db_name`, `table_name`, `column_name`, `comment`)
659 '" . PMA_sqlAddslashes($db) . "',
662 '" . PMA_sqlAddslashes($comment) . "')
663 ON DUPLICATE KEY UPDATE
664 `comment` = '" . PMA_sqlAddslashes($comment) . "'";
668 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
669 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
670 AND `table_name` = \'\'
671 AND `column_name` = \'(db_comment)\'';
674 if (isset($upd_query)){
675 return PMA_query_as_controluser($upd_query);
679 } // end of 'PMA_setDbComment()' function
682 * Set a SQL history entry
684 * @uses $_SESSION['sql_history']
685 * @uses $cfg['QueryHistoryDB']
686 * @uses $cfg['QueryHistoryMax']
687 * @uses PMA_getRelationsParam()
688 * @uses PMA_query_as_controluser()
689 * @uses PMA_backquote()
690 * @uses PMA_sqlAddslashes()
693 * @uses array_shift()
694 * @param string $db the name of the db
695 * @param string $table the name of the table
696 * @param string $username the username
697 * @param string $sqlquery the sql query
700 function PMA_setHistory($db, $table, $username, $sqlquery)
702 if (strlen($sqlquery) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
706 $cfgRelation = PMA_getRelationsParam();
708 if (! isset($_SESSION['sql_history'])) {
709 $_SESSION['sql_history'] = array();
712 $key = md5($sqlquery . $db . $table);
714 if (isset($_SESSION['sql_history'][$key])) {
715 unset($_SESSION['sql_history'][$key]);
718 $_SESSION['sql_history'][$key] = array(
721 'sqlquery' => $sqlquery,
724 if (count($_SESSION['sql_history']) > $GLOBALS['cfg']['QueryHistoryMax']) {
725 // history should not exceed a maximum count
726 array_shift($_SESSION['sql_history']);
729 if (! $cfgRelation['historywork'] ||
! $GLOBALS['cfg']['QueryHistoryDB']) {
733 PMA_query_as_controluser('
735 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
742 (\'' . PMA_sqlAddslashes($username) . '\',
743 \'' . PMA_sqlAddslashes($db) . '\',
744 \'' . PMA_sqlAddslashes($table) . '\',
746 \'' . PMA_sqlAddslashes($sqlquery) . '\')');
747 } // end of 'PMA_setHistory()' function
750 * Gets a SQL history entry
752 * @uses $_SESSION['sql_history']
753 * @uses $GLOBALS['controllink']
754 * @uses PMA_getRelationsParam()
755 * @uses PMA_backquote()
756 * @uses PMA_sqlAddslashes()
757 * @uses PMA_DBI_fetch_result()
758 * @uses array_reverse()
759 * @param string $username the username
760 * @return array list of history items
763 function PMA_getHistory($username)
765 $cfgRelation = PMA_getRelationsParam();
767 if (isset($_SESSION['sql_history'])) {
768 return array_reverse($_SESSION['sql_history']);
771 if (! $cfgRelation['historywork']) {
779 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
780 WHERE `username` = \'' . PMA_sqlAddslashes($username) . '\'
783 return PMA_DBI_fetch_result($hist_query, null, null, $GLOBALS['controllink']);
784 } // end of 'PMA_getHistory()' function
789 * deletes entries that exceeds $cfg['QueryHistoryMax'], oldest first, for the
792 * @uses $cfg['QueryHistoryMax']
793 * @uses $cfg['QueryHistoryDB']
794 * @uses $GLOBALS['controllink']
795 * @uses PMA_backquote()
796 * @uses PMA_sqlAddSlashes()
797 * @uses PMA_query_as_controluser()
798 * @uses PMA_DBI_fetch_value()
799 * @param string $username the username
802 function PMA_purgeHistory($username)
804 $cfgRelation = PMA_getRelationsParam();
805 if (! $GLOBALS['cfg']['QueryHistoryDB'] ||
! $cfgRelation['historywork']) {
809 if (! $cfgRelation['historywork']) {
815 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
816 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
817 ORDER BY `timevalue` DESC
818 LIMIT ' . $GLOBALS['cfg']['QueryHistoryMax'] . ', 1';
820 if ($max_time = PMA_DBI_fetch_value($search_query, 0, 0, $GLOBALS['controllink'])) {
821 PMA_query_as_controluser('
823 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
824 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
825 AND `timevalue` <= \'' . $max_time . '\'');
827 } // end of 'PMA_purgeHistory()' function
830 * Prepares the dropdown for one mode
832 * @uses $cfg['LimitChars']
833 * @uses $cfg['NaturalOrder']
835 * @uses htmlspecialchars()
839 * @uses natcasesort()
841 * @param array $foreign the keys and values for foreigns
842 * @param string $data the current data of the dropdown
843 * @param string $mode the needed mode
845 * @return array the <option value=""><option>s
849 function PMA__foreignDropdownBuild($foreign, $data, $mode)
851 $reloptions = array();
853 if ($mode == 'id-content') {
854 // sort for id-content
855 if ($GLOBALS['cfg']['NaturalOrder']) {
856 uksort($foreign, 'strnatcasecmp');
860 } elseif ($mode == 'content-id') {
861 // sort for content-id
862 if ($GLOBALS['cfg']['NaturalOrder']) {
863 natcasesort($foreign);
869 foreach ($foreign as $key => $value) {
871 if (PMA_strlen($value) <= $GLOBALS['cfg']['LimitChars']) {
873 $value = htmlspecialchars($value);
875 $vtitle = htmlspecialchars($value);
876 $value = htmlspecialchars(substr($value, 0, $GLOBALS['cfg']['LimitChars']) . '...');
879 $reloption = ' <option value="' . htmlspecialchars($key) . '"';
881 $reloption .= ' title="' . $vtitle . '"';
884 if ((string) $key == (string) $data) {
885 $reloption .= ' selected="selected"';
888 if ($mode == 'content-id') {
889 $reloptions[] = $reloption . '>' . $value . ' - ' . htmlspecialchars($key) . '</option>' . "\n";
891 $reloptions[] = $reloption . '>' . htmlspecialchars($key) . ' - ' . $value . '</option>' . "\n";
896 } // end of 'PMA__foreignDropdownBuild' function
899 * Outputs dropdown with values of foreign fields
901 * @uses $cfg['ForeignKeyMaxLimit']
902 * @uses $cfg['ForeignKeyDropdownOrder']
903 * @uses PMA__foreignDropdownBuild()
904 * @uses PMA_isValid()
906 * @param array array of the displayed row
907 * @param string the foreign field
908 * @param string the foreign field to display
909 * @param string the current data of the dropdown (field in row)
910 * @return string the <option value=""><option>s
913 function PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, $data,
917 $max = $GLOBALS['cfg']['ForeignKeyMaxLimit'];
923 foreach ($disp_row as $relrow) {
924 $key = $relrow[$foreign_field];
926 // if the display field has been defined for this foreign table
927 if ($foreign_display) {
928 $value = $relrow[$foreign_display];
931 } // end if ($foreign_display)
933 $foreign[$key] = $value;
936 // put the dropdown sections in correct order
939 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'], 'array')) {
940 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][0])) {
941 $top = PMA__foreignDropdownBuild($foreign, $data,
942 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][0]);
944 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][1])) {
945 $bot = PMA__foreignDropdownBuild($foreign, $data,
946 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][1]);
949 $top = PMA__foreignDropdownBuild($foreign, $data, 'id-content');
950 $bot = PMA__foreignDropdownBuild($foreign, $data, 'content-id');
953 // beginning of dropdown
954 $ret = '<option value=""> </option>' . "\n";
956 $top_count = count($top);
957 if ($max == -1 ||
$top_count < $max) {
958 $ret .= implode('', $top);
959 if ($top_count > 0) {
960 $ret .= ' <option value=""> </option>' . "\n";
961 $ret .= ' <option value=""> </option>' . "\n";
964 $ret .= implode('', $bot);
967 } // end of 'PMA_foreignDropdown()' function
970 * Gets foreign keys in preparation for a drop-down selector
971 * Thanks to <markus@noga.de>
973 * @uses PMA_Table::countRecords()
974 * @uses PMA_backquote()
975 * @uses PMA_getDisplayField()
976 * @uses PMA_sqlAddslashes()
977 * @uses PMA_DBI_fetch_value()
978 * @uses PMA_DBI_free_result()
979 * @uses PMA_DBI_query()
980 * @uses PMA_DBI_num_rows()
981 * @uses PMA_DBI_fetch_assoc()
982 * @param array array of the foreign keys
983 * @param string the foreign field name
984 * @param bool whether to override the total
985 * @param string a possible filter
986 * @param string a possible LIMIT clause
987 * @return array data about the foreign keys
991 function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filter, $foreign_limit)
993 // we always show the foreign field in the drop-down; if a display
994 // field is defined, we show it besides the foreign field
995 $foreign_link = false;
996 if ($foreigners && isset($foreigners[$field])) {
997 $foreigner = $foreigners[$field];
998 $foreign_db = $foreigner['foreign_db'];
999 $foreign_table = $foreigner['foreign_table'];
1000 $foreign_field = $foreigner['foreign_field'];
1002 // Count number of rows in the foreign table. Currently we do
1003 // not use a drop-down if more than 200 rows in the foreign table,
1004 // for speed reasons and because we need a better interface for this.
1006 // We could also do the SELECT anyway, with a LIMIT, and ensure that
1007 // the current value of the field is one of the choices.
1009 $the_total = PMA_Table
::countRecords($foreign_db, $foreign_table);
1011 if ($override_total == true ||
$the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']) {
1012 // foreign_display can be FALSE if no display field defined:
1013 $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
1015 $f_query_main = 'SELECT ' . PMA_backquote($foreign_field)
1016 . (($foreign_display == FALSE) ?
'' : ', ' . PMA_backquote($foreign_display));
1017 $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table);
1018 $f_query_filter = empty($foreign_filter) ?
'' : ' WHERE ' . PMA_backquote($foreign_field)
1019 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1020 . (($foreign_display == FALSE) ?
'' : ' OR ' . PMA_backquote($foreign_display)
1021 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1023 $f_query_order = ($foreign_display == FALSE) ?
'' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display);
1024 $f_query_limit = isset($foreign_limit) ?
$foreign_limit : '';
1026 if (!empty($foreign_filter)) {
1027 $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter);
1029 $the_total = PMA_DBI_fetch_value($res);
1030 @PMA_DBI_free_result
($res);
1036 $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit);
1037 if ($disp && PMA_DBI_num_rows($disp) > 0) {
1038 // If a resultset has been created, pre-cache it in the $disp_row array
1039 // This helps us from not needing to use mysql_data_seek by accessing a pre-cached
1040 // PHP array. Usually those resultsets are not that big, so a performance hit should
1042 $disp_row = array();
1043 while ($single_disp_row = @PMA_DBI_fetch_assoc
($disp)) {
1044 $disp_row[] = $single_disp_row;
1046 @PMA_DBI_free_result
($disp);
1050 $foreign_link = true;
1052 } // end if $foreigners
1054 $foreignData['foreign_link'] = $foreign_link;
1055 $foreignData['the_total'] = isset($the_total) ?
$the_total : null;
1056 $foreignData['foreign_display'] = isset($foreign_display) ?
$foreign_display : null;
1057 $foreignData['disp_row'] = isset($disp_row) ?
$disp_row : null;
1058 $foreignData['foreign_field'] = isset($foreign_field) ?
$foreign_field : null;
1059 return $foreignData;
1060 } // end of 'PMA_getForeignData()' function
1063 * Finds all related tables
1065 * @uses $GLOBALS['controllink']
1066 * @uses $GLOBALS['cfgRelation']
1067 * @uses $GLOBALS['db']
1068 * @param string whether to go from master to foreign or vice versa
1069 * @return boolean always TRUE
1070 * @global array $tab_left the list of tables that we still couldn't connect
1071 * @global array $tab_know the list of allready connected tables
1072 * @global string $fromclause
1076 function PMA_getRelatives($from)
1078 global $tab_left, $tab_know, $fromclause;
1080 if ($from == 'master') {
1085 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
1086 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
1088 $rel_query = 'SELECT *'
1089 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db'])
1090 . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
1091 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1092 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1093 . ' AND ' . $from . '_table IN ' . $in_know
1094 . ' AND ' . $to . '_table IN ' . $in_left;
1095 $relations = @PMA_DBI_query
($rel_query, $GLOBALS['controllink']);
1096 while ($row = PMA_DBI_fetch_assoc($relations)) {
1097 $found_table = $row[$to . '_table'];
1098 if (isset($tab_left[$found_table])) {
1100 .= "\n" . ' LEFT JOIN '
1101 . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($row[$to . '_table']) . ' ON '
1102 . PMA_backquote($row[$from . '_table']) . '.'
1103 . PMA_backquote($row[$from . '_field']) . ' = '
1104 . PMA_backquote($row[$to . '_table']) . '.'
1105 . PMA_backquote($row[$to . '_field']) . ' ';
1106 $tab_know[$found_table] = $found_table;
1107 unset($tab_left[$found_table]);
1112 } // end of the "PMA_getRelatives()" function
1115 * Rename a field in relation tables
1117 * usually called after a field in a table was renamed in tbl_alter.php
1119 * @uses PMA_getRelationsParam()
1120 * @uses PMA_backquote()
1121 * @uses PMA_sqlAddslashes()
1122 * @uses PMA_query_as_controluser()
1124 * @param string $table
1125 * @param string $field
1126 * @param string $new_name
1128 function PMA_REL_renameField($db, $table, $field, $new_name)
1130 $cfgRelation = PMA_getRelationsParam();
1132 if ($cfgRelation['displaywork']) {
1133 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
1134 . ' SET display_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1135 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1136 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
1137 . ' AND display_field = \'' . PMA_sqlAddslashes($field) . '\'';
1138 PMA_query_as_controluser($table_query);
1141 if ($cfgRelation['relwork']) {
1142 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1143 . ' SET master_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1144 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
1145 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
1146 . ' AND master_field = \'' . PMA_sqlAddslashes($field) . '\'';
1147 PMA_query_as_controluser($table_query);
1149 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1150 . ' SET foreign_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1151 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
1152 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
1153 . ' AND foreign_field = \'' . PMA_sqlAddslashes($field) . '\'';
1154 PMA_query_as_controluser($table_query);
1161 * @uses $GLOBALS['strNoDescription']
1162 * @uses PMA_backquote()
1163 * @uses $GLOBALS['cfgRelation']['db']
1164 * @uses PMA_sqlAddslashes()
1165 * @uses PMA_query_as_controluser()
1166 * @uses PMA_DBI_insert_id()
1167 * @uses $GLOBALS['controllink']
1168 * @param string $newpage
1169 * @param array $cfgRelation
1171 * @param string $query_default_option
1172 * @return string $pdf_page_number
1174 function PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option) {
1175 if (! isset($newpage) ||
$newpage == '') {
1176 $newpage = $GLOBALS['strNoDescription'];
1178 $ins_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1179 . ' (db_name, page_descr)'
1180 . ' VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($newpage) . '\')';
1181 PMA_query_as_controluser($ins_query, FALSE, $query_default_option);
1182 return PMA_DBI_insert_id(isset($GLOBALS['controllink']) ?
$GLOBALS['controllink'] : '');