Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / relation.lib.php
blobc02a8b1e88bde8b7cc1f1cd215c48f31bc19931e
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used with the relation and pdf feature
6 * @version $Id$
7 * @package phpMyAdmin
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
16 require_once './libraries/Table.class.php';
18 /**
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
26 * @access public
29 function PMA_query_as_controluser($sql, $show_error = true, $options = 0)
31 if ($show_error) {
32 $result = PMA_DBI_query($sql, $GLOBALS['controllink'], $options);
33 } else {
34 $result = @PMA_DBI_try_query($sql, $GLOBALS['controllink'], $options);
35 } // end if... else...
37 if ($result) {
38 return $result;
39 } else {
40 return false;
42 } // end of the "PMA_query_as_controluser()" function
44 /**
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()
60 // which uses it
61 $GLOBALS['cfgRelation'] = $_SESSION['relation'][$GLOBALS['server']];
63 if ($verbose) {
64 PMA_printRelationsParamDiagnostic($_SESSION['relation'][$GLOBALS['server']]);
67 return $_SESSION['relation'][$GLOBALS['server']];
70 /**
71 * prints out diagnostic info for pma relation feature
73 * @uses $GLOBALS['server']
74 * @uses $GLOBALS['controllink']
75 * @uses __('not OK')
76 * @uses __('Documentation')
77 * @uses __('General relation features')
78 * @uses __('Disabled')
79 * @uses __('Enabled')
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 $cfg['Server']['pmadb']
88 * @uses sprintf()
89 * @uses PMA_printDiagMessageForFeature()
90 * @uses PMA_printDiagMessageForParameter()
91 * @param array $cfgRelation
93 function PMA_printRelationsParamDiagnostic($cfgRelation)
95 $messages['error'] = '<font color="red"><strong>' . __('not OK')
96 . '</strong></font> [ <a href="Documentation.html#%s" target="documentation">'
97 . __('Documentation') . '</a> ]';
99 $messages['ok'] = '<font color="green"><strong>' . __('OK') . '</strong></font>';
100 $messages['enabled'] = '<font color="green">' . __('Enabled') . '</font>';
101 $messages['disabled'] = '<font color="red">' . __('Disabled') . '</font>';
103 if (false === $GLOBALS['cfg']['Server']['pmadb']) {
104 echo 'PMA Database ... '
105 . sprintf($messages['error'], 'pmadb')
106 . '<br />' . "\n"
107 . __('General relation features')
108 . ' <font color="green">' . __('Disabled')
109 . '</font>' . "\n";
110 return;
113 echo '<table>' . "\n";
115 PMA_printDiagMessageForParameter('pmadb', $GLOBALS['cfg']['Server']['pmadb'], $messages, 'pmadb');
117 PMA_printDiagMessageForParameter('relation', isset($cfgRelation['relation']), $messages, 'relation');
119 PMA_printDiagMessageForFeature('strGeneralRelationFeat', 'relwork', $messages);
121 PMA_printDiagMessageForParameter('table_info', isset($cfgRelation['table_info']), $messages, 'table_info');
123 PMA_printDiagMessageForFeature('strDisplayFeat', 'displaywork', $messages);
125 PMA_printDiagMessageForParameter('table_coords', isset($cfgRelation['table_coords']), $messages, 'table_coords');
127 PMA_printDiagMessageForParameter('pdf_pages', isset($cfgRelation['pdf_pages']), $messages, 'table_coords');
129 PMA_printDiagMessageForFeature('strCreatePdfFeat', 'pdfwork', $messages);
131 PMA_printDiagMessageForParameter('column_info', isset($cfgRelation['column_info']), $messages, 'col_com');
133 PMA_printDiagMessageForFeature('strColComFeat', 'commwork', $messages, false);
135 PMA_printDiagMessageForFeature('strMIME_transformation', 'mimework', $messages);
137 if ($cfgRelation['commwork'] && ! $cfgRelation['mimework']) {
138 echo '<tr><td colspan=2 align="left">' . __('Please see the documentation on how to update your column_comments table') . '</td></tr>' . "\n";
141 PMA_printDiagMessageForParameter('bookmarktable', isset($cfgRelation['bookmark']), $messages, 'bookmark');
143 PMA_printDiagMessageForFeature('strBookmarkQuery', 'bookmarkwork', $messages);
145 PMA_printDiagMessageForParameter('history', isset($cfgRelation['history']), $messages, 'history');
147 PMA_printDiagMessageForFeature('strQuerySQLHistory', 'historywork', $messages);
149 PMA_printDiagMessageForParameter('designer_coords', isset($cfgRelation['designer_coords']), $messages, 'designer_coords');
151 PMA_printDiagMessageForFeature('strDesigner', 'designerwork', $messages);
153 PMA_printDiagMessageForParameter('tracking', isset($cfgRelation['tracking']), $messages, 'tracking');
155 PMA_printDiagMessageForFeature('strTracking', 'trackingwork', $messages);
157 echo '</table>' . "\n";
159 echo '<p>' . __('Quick steps to setup advanced features:') . '</p>';
160 echo '<ul>';
161 echo '<li>' . __('Create the needed tables with the <code>script/create_tables.sql</code>.') . ' ' . PMA_showDocu('linked-tables') . '</li>';
162 echo '<li>' . __('Create a pma user and give access to these tables.') . ' ' . PMA_showDocu('pmausr') . '</li>';
163 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>';
164 echo '<li>' . __('Re-login to phpMyAdmin to load the updated configuration file.') . '</li>';
165 echo '</ul>';
169 * prints out one diagnostic message for a feature
171 * @param string feature name in a message string
172 * @param string the $GLOBALS['cfgRelation'] parameter to check
173 * @param array utility messages
174 * @param boolean whether to skip a line after the message
176 function PMA_printDiagMessageForFeature($feature_name, $relation_parameter, $messages, $skip_line=true)
178 echo ' <tr><td colspan=2 align="right">' . $GLOBALS[$feature_name] . ': '
179 . ($GLOBALS['cfgRelation'][$relation_parameter] ? $messages['enabled'] : $messages['disabled'])
180 . '</td></tr>' . "\n";
181 if ($skip_line) {
182 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
187 * prints out one diagnostic message for a configuration parameter
189 * @param string config parameter name to display
190 * @param boolean whether this parameter is set
191 * @param array utility messages
192 * @param string anchor in Documentation.html
194 function PMA_printDiagMessageForParameter($parameter, $relation_parameter_set, $messages, $doc_anchor)
196 echo ' <tr><th align="left">';
197 echo '$cfg[\'Servers\'][$i][\'' . $parameter . '\'] ... </th><td align="right">';
198 echo ($relation_parameter_set ? $messages['ok'] : sprintf($messages['error'], $doc_anchor)) . '</td></tr>' . "\n";
203 * Defines the relation parameters for the current user
204 * just a copy of the functions used for relations ;-)
205 * but added some stuff to check what will work
207 * @uses $cfg['Server']['user']
208 * @uses $cfg['Server']['pmadb']
209 * @uses $cfg['Server']['verbose_check']
210 * @uses $GLOBALS['server']
211 * @uses $GLOBALS['controllink']
212 * @uses PMA_DBI_QUERY_STORE
213 * @uses PMA_DBI_select_db()
214 * @uses PMA_backquote()
215 * @uses PMA_query_as_controluser()
216 * @uses PMA_DBI_fetch_row()
217 * @uses PMA_DBI_free_result()
218 * @access protected
219 * @return array the relation parameters for the current user
221 function PMA__getRelationsParam()
223 $cfgRelation = array();
224 $cfgRelation['relwork'] = false;
225 $cfgRelation['displaywork'] = false;
226 $cfgRelation['bookmarkwork']= false;
227 $cfgRelation['pdfwork'] = false;
228 $cfgRelation['commwork'] = false;
229 $cfgRelation['mimework'] = false;
230 $cfgRelation['historywork'] = false;
231 $cfgRelation['trackingwork'] = false;
232 $cfgRelation['designerwork'] = false;
233 $cfgRelation['allworks'] = false;
234 $cfgRelation['user'] = null;
235 $cfgRelation['db'] = null;
237 if ($GLOBALS['server'] == 0 || empty($GLOBALS['cfg']['Server']['pmadb'])
238 || ! PMA_DBI_select_db($GLOBALS['cfg']['Server']['pmadb'], $GLOBALS['controllink'])) {
239 // No server selected -> no bookmark table
240 // we return the array with the falses in it,
241 // to avoid some 'Unitialized string offset' errors later
242 $GLOBALS['cfg']['Server']['pmadb'] = false;
243 return $cfgRelation;
247 $cfgRelation['user'] = $GLOBALS['cfg']['Server']['user'];
248 $cfgRelation['db'] = $GLOBALS['cfg']['Server']['pmadb'];
250 // Now I just check if all tables that i need are present so I can for
251 // example enable relations but not pdf...
252 // I was thinking of checking if they have all required columns but I
253 // fear it might be too slow
255 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($GLOBALS['cfg']['Server']['pmadb']);
256 $tab_rs = PMA_query_as_controluser($tab_query, false, PMA_DBI_QUERY_STORE);
258 if (! $tab_rs) {
259 // query failed ... ?
260 //$GLOBALS['cfg']['Server']['pmadb'] = false;
261 return $cfgRelation;
264 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
265 if ($curr_table[0] == $GLOBALS['cfg']['Server']['bookmarktable']) {
266 $cfgRelation['bookmark'] = $curr_table[0];
267 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['relation']) {
268 $cfgRelation['relation'] = $curr_table[0];
269 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_info']) {
270 $cfgRelation['table_info'] = $curr_table[0];
271 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_coords']) {
272 $cfgRelation['table_coords'] = $curr_table[0];
273 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['designer_coords']) {
274 $cfgRelation['designer_coords'] = $curr_table[0];
275 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['column_info']) {
276 $cfgRelation['column_info'] = $curr_table[0];
277 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['pdf_pages']) {
278 $cfgRelation['pdf_pages'] = $curr_table[0];
279 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['history']) {
280 $cfgRelation['history'] = $curr_table[0];
281 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['tracking']) {
282 $cfgRelation['tracking'] = $curr_table[0];
284 } // end while
285 PMA_DBI_free_result($tab_rs);
287 if (isset($cfgRelation['relation'])) {
288 $cfgRelation['relwork'] = true;
289 if (isset($cfgRelation['table_info'])) {
290 $cfgRelation['displaywork'] = true;
293 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
294 $cfgRelation['pdfwork'] = true;
296 if (isset($cfgRelation['column_info'])) {
297 $cfgRelation['commwork'] = true;
299 if ($GLOBALS['cfg']['Server']['verbose_check']) {
300 $mime_query = 'SHOW FIELDS FROM '
301 . PMA_backquote($cfgRelation['db']) . '.'
302 . PMA_backquote($cfgRelation['column_info']);
303 $mime_rs = PMA_query_as_controluser($mime_query, false);
305 $mime_field_mimetype = false;
306 $mime_field_transformation = false;
307 $mime_field_transformation_options = false;
308 while ($curr_mime_field = @PMA_DBI_fetch_row($mime_rs)) {
309 if ($curr_mime_field[0] == 'mimetype') {
310 $mime_field_mimetype = true;
311 } elseif ($curr_mime_field[0] == 'transformation') {
312 $mime_field_transformation = true;
313 } elseif ($curr_mime_field[0] == 'transformation_options') {
314 $mime_field_transformation_options = true;
317 PMA_DBI_free_result($mime_rs);
319 if ($mime_field_mimetype
320 && $mime_field_transformation
321 && $mime_field_transformation_options) {
322 $cfgRelation['mimework'] = true;
324 } else {
325 $cfgRelation['mimework'] = true;
329 if (isset($cfgRelation['history'])) {
330 $cfgRelation['historywork'] = true;
333 if (isset($cfgRelation['tracking'])) {
334 $cfgRelation['trackingwork'] = true;
337 // we do not absolutely need that the internal relations or the PDF
338 // schema feature be activated
339 if (isset($cfgRelation['designer_coords'])) {
340 $cfgRelation['designerwork'] = true;
343 if (isset($cfgRelation['bookmark'])) {
344 $cfgRelation['bookmarkwork'] = true;
347 if ($cfgRelation['relwork'] && $cfgRelation['displaywork']
348 && $cfgRelation['pdfwork'] && $cfgRelation['commwork']
349 && $cfgRelation['mimework'] && $cfgRelation['historywork']
350 && $cfgRelation['trackingwork']
351 && $cfgRelation['bookmarkwork'] && $cfgRelation['designerwork']) {
352 $cfgRelation['allworks'] = true;
355 return $cfgRelation;
356 } // end of the 'PMA_getRelationsParam()' function
359 * Gets all Relations to foreign tables for a given table or
360 * optionally a given column in a table
362 * @access public
363 * @uses $GLOBALS['controllink']
364 * @uses $GLOBALS['information_schema_relations']
365 * @uses PMA_getRelationsParam()
366 * @uses PMA_backquote()
367 * @uses PMA_sqlAddslashes()
368 * @uses PMA_DBI_fetch_result()
369 * @uses PMA_DBI_fetch_value()
370 * @uses PMA_SQP_analyze()
371 * @uses PMA_SQP_parse()
372 * @uses count()
373 * @uses strlen()
374 * @param string $db the name of the db to check for
375 * @param string $table the name of the table to check for
376 * @param string $column the name of the column to check for
377 * @param string $source the source for foreign key information
378 * @return array db,table,column
380 function PMA_getForeigners($db, $table, $column = '', $source = 'both')
382 $cfgRelation = PMA_getRelationsParam();
383 $foreign = array();
385 if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
386 $rel_query = '
387 SELECT `master_field`,
388 `foreign_db`,
389 `foreign_table`,
390 `foreign_field`
391 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
392 WHERE `master_db` = \'' . PMA_sqlAddslashes($db) . '\'
393 AND `master_table` = \'' . PMA_sqlAddslashes($table) . '\' ';
394 if (strlen($column)) {
395 $rel_query .= ' AND `master_field` = \'' . PMA_sqlAddslashes($column) . '\'';
397 $foreign = PMA_DBI_fetch_result($rel_query, 'master_field', null, $GLOBALS['controllink']);
400 if (($source == 'both' || $source == 'foreign') && strlen($table)) {
401 $show_create_table_query = 'SHOW CREATE TABLE '
402 . PMA_backquote($db) . '.' . PMA_backquote($table);
403 $show_create_table = PMA_DBI_fetch_value($show_create_table_query, 0, 1);
404 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
406 foreach ($analyzed_sql[0]['foreign_keys'] as $one_key) {
407 // The analyzer may return more than one column name in the
408 // index list or the ref_index_list; if this happens,
409 // the current logic just discards the whole index; having
410 // more than one index field is currently unsupported (see FAQ 3.6)
411 if (count($one_key['index_list']) == 1) {
412 foreach ($one_key['index_list'] as $i => $field) {
413 // If a foreign key is defined in the 'internal' source (pmadb)
414 // and as a native foreign key, we won't get it twice
415 // if $source='both' because we use $field as key
417 // The parser looks for a CONSTRAINT clause just before
418 // the FOREIGN KEY clause. It finds it (as output from
419 // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
420 // versions like 3.23.58.
421 // In those cases, the FOREIGN KEY parsing will put numbers
422 // like -1, 0, 1... instead of the constraint number.
424 if (isset($one_key['constraint'])) {
425 $foreign[$field]['constraint'] = $one_key['constraint'];
428 if (isset($one_key['ref_db_name'])) {
429 $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
430 } else {
431 $foreign[$field]['foreign_db'] = $db;
433 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
434 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
435 if (isset($one_key['on_delete'])) {
436 $foreign[$field]['on_delete'] = $one_key['on_delete'];
438 if (isset($one_key['on_update'])) {
439 $foreign[$field]['on_update'] = $one_key['on_update'];
447 * Emulating relations for some information_schema tables
449 if ($db == 'information_schema'
450 && ($source == 'internal' || $source == 'both')) {
451 require_once './libraries/information_schema_relations.lib.php';
453 if (isset($GLOBALS['information_schema_relations'][$table])) {
454 foreach ($GLOBALS['information_schema_relations'][$table] as $field => $relations) {
455 if ((! strlen($column) || $column == $field)
456 && (! isset($foreign[$field]) || ! strlen($foreign[$field]))) {
457 $foreign[$field] = $relations;
463 return $foreign;
464 } // end of the 'PMA_getForeigners()' function
467 * Gets the display field of a table
469 * @access public
470 * @uses $GLOBALS['controllink']
471 * @uses PMA_getRelationsParam()
472 * @uses PMA_backquote()
473 * @uses PMA_sqlAddslashes()
474 * @uses PMA_DBI_fetch_single_row()
475 * @uses trim()
476 * @param string $db the name of the db to check for
477 * @param string $table the name of the table to check for
478 * @return string field name
480 function PMA_getDisplayField($db, $table)
482 $cfgRelation = PMA_getRelationsParam();
485 * Try to fetch the display field from DB.
487 if ($cfgRelation['displaywork']) {
488 $disp_query = '
489 SELECT `display_field`
490 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info']) . '
491 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
492 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'';
494 $row = PMA_DBI_fetch_single_row($disp_query, 'ASSOC', $GLOBALS['controllink']);
495 if (isset($row['display_field'])) {
496 return $row['display_field'];
501 * Emulating the display field for some information_schema tables.
503 if ($db == 'information_schema') {
504 switch ($table) {
505 case 'CHARACTER_SETS': return 'DESCRIPTION';
506 case 'TABLES': return 'TABLE_COMMENT';
511 * No Luck...
513 return false;
515 } // end of the 'PMA_getDisplayField()' function
518 * Gets the comments for all rows of a table or the db itself
520 * @access public
521 * @uses PMA_DBI_get_fields()
522 * @uses PMA_getDbComment()
523 * @param string the name of the db to check for
524 * @param string the name of the table to check for
525 * @return array [field_name] = comment
527 function PMA_getComments($db, $table = '')
529 $comments = array();
531 if ($table != '') {
532 // MySQL native column comments
533 $fields = PMA_DBI_get_fields($db, $table);
534 if ($fields) {
535 foreach ($fields as $key => $field) {
536 if (! empty($field['Comment'])) {
537 $comments[$field['Field']] = $field['Comment'];
541 } else {
542 $comments[] = PMA_getDbComment($db);
545 return $comments;
546 } // end of the 'PMA_getComments()' function
549 * Gets the comment for a db
551 * @access public
552 * @uses PMA_DBI_QUERY_STORE
553 * @uses PMA_DBI_num_rows()
554 * @uses PMA_DBI_fetch_assoc()
555 * @uses PMA_DBI_free_result()
556 * @uses PMA_getRelationsParam()
557 * @uses PMA_backquote()
558 * @uses PMA_sqlAddslashes()
559 * @uses PMA_query_as_controluser()
560 * @uses strlen()
561 * @param string the name of the db to check for
562 * @return string comment
564 function PMA_getDbComment($db)
566 $cfgRelation = PMA_getRelationsParam();
567 $comment = '';
569 if ($cfgRelation['commwork']) {
570 // pmadb internal db comment
571 $com_qry = "
572 SELECT `comment`
573 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
574 WHERE db_name = '" . PMA_sqlAddslashes($db) . "'
575 AND table_name = ''
576 AND column_name = '(db_comment)'";
577 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE);
579 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
580 $row = PMA_DBI_fetch_assoc($com_rs);
581 $comment = $row['comment'];
583 PMA_DBI_free_result($com_rs);
586 return $comment;
587 } // end of the 'PMA_getDbComment()' function
590 * Gets the comment for a db
592 * @access public
593 * @uses PMA_DBI_QUERY_STORE
594 * @uses PMA_DBI_num_rows()
595 * @uses PMA_DBI_fetch_assoc()
596 * @uses PMA_DBI_free_result()
597 * @uses PMA_getRelationsParam()
598 * @uses PMA_backquote()
599 * @uses PMA_sqlAddslashes()
600 * @uses PMA_query_as_controluser()
601 * @uses strlen()
602 * @param string the name of the db to check for
603 * @return string comment
605 function PMA_getDbComments()
607 $cfgRelation = PMA_getRelationsParam();
608 $comments = array();
610 if ($cfgRelation['commwork']) {
611 // pmadb internal db comment
612 $com_qry = "
613 SELECT `db_name`, `comment`
614 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
615 WHERE `column_name` = '(db_comment)'";
616 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE);
618 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
619 while ($row = PMA_DBI_fetch_assoc($com_rs)) {
620 $comments[$row['db_name']] = $row['comment'];
623 PMA_DBI_free_result($com_rs);
626 return $comments;
627 } // end of the 'PMA_getDbComments()' function
630 * Set a database comment to a certain value.
632 * @uses PMA_getRelationsParam()
633 * @uses PMA_backquote()
634 * @uses PMA_sqlAddslashes()
635 * @uses PMA_query_as_controluser()
636 * @uses strlen()
637 * @access public
638 * @param string $db the name of the db
639 * @param string $comment the value of the column
640 * @return boolean true, if comment-query was made.
642 function PMA_setDbComment($db, $comment = '')
644 $cfgRelation = PMA_getRelationsParam();
646 if (! $cfgRelation['commwork']) {
647 return false;
650 if (strlen($comment)) {
651 $upd_query = "
652 INSERT INTO
653 " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
654 (`db_name`, `table_name`, `column_name`, `comment`)
655 VALUES (
656 '" . PMA_sqlAddslashes($db) . "',
658 '(db_comment)',
659 '" . PMA_sqlAddslashes($comment) . "')
660 ON DUPLICATE KEY UPDATE
661 `comment` = '" . PMA_sqlAddslashes($comment) . "'";
662 } else {
663 $upd_query = '
664 DELETE FROM
665 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
666 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
667 AND `table_name` = \'\'
668 AND `column_name` = \'(db_comment)\'';
671 if (isset($upd_query)){
672 return PMA_query_as_controluser($upd_query);
675 return false;
676 } // end of 'PMA_setDbComment()' function
679 * Set a SQL history entry
681 * @uses $_SESSION['sql_history']
682 * @uses $cfg['QueryHistoryDB']
683 * @uses $cfg['QueryHistoryMax']
684 * @uses PMA_getRelationsParam()
685 * @uses PMA_query_as_controluser()
686 * @uses PMA_backquote()
687 * @uses PMA_sqlAddslashes()
688 * @uses count()
689 * @uses md5()
690 * @uses array_shift()
691 * @param string $db the name of the db
692 * @param string $table the name of the table
693 * @param string $username the username
694 * @param string $sqlquery the sql query
695 * @access public
697 function PMA_setHistory($db, $table, $username, $sqlquery)
699 if (strlen($sqlquery) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
700 return;
703 $cfgRelation = PMA_getRelationsParam();
705 if (! isset($_SESSION['sql_history'])) {
706 $_SESSION['sql_history'] = array();
709 $key = md5($sqlquery . $db . $table);
711 if (isset($_SESSION['sql_history'][$key])) {
712 unset($_SESSION['sql_history'][$key]);
715 $_SESSION['sql_history'][$key] = array(
716 'db' => $db,
717 'table' => $table,
718 'sqlquery' => $sqlquery,
721 if (count($_SESSION['sql_history']) > $GLOBALS['cfg']['QueryHistoryMax']) {
722 // history should not exceed a maximum count
723 array_shift($_SESSION['sql_history']);
726 if (! $cfgRelation['historywork'] || ! $GLOBALS['cfg']['QueryHistoryDB']) {
727 return;
730 PMA_query_as_controluser('
731 INSERT INTO
732 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
733 (`username`,
734 `db`,
735 `table`,
736 `timevalue`,
737 `sqlquery`)
738 VALUES
739 (\'' . PMA_sqlAddslashes($username) . '\',
740 \'' . PMA_sqlAddslashes($db) . '\',
741 \'' . PMA_sqlAddslashes($table) . '\',
742 NOW(),
743 \'' . PMA_sqlAddslashes($sqlquery) . '\')');
744 } // end of 'PMA_setHistory()' function
747 * Gets a SQL history entry
749 * @uses $_SESSION['sql_history']
750 * @uses $GLOBALS['controllink']
751 * @uses PMA_getRelationsParam()
752 * @uses PMA_backquote()
753 * @uses PMA_sqlAddslashes()
754 * @uses PMA_DBI_fetch_result()
755 * @uses array_reverse()
756 * @param string $username the username
757 * @return array list of history items
758 * @access public
760 function PMA_getHistory($username)
762 $cfgRelation = PMA_getRelationsParam();
764 if (isset($_SESSION['sql_history'])) {
765 return array_reverse($_SESSION['sql_history']);
768 if (! $cfgRelation['historywork']) {
769 return false;
772 $hist_query = '
773 SELECT `db`,
774 `table`,
775 `sqlquery`
776 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
777 WHERE `username` = \'' . PMA_sqlAddslashes($username) . '\'
778 ORDER BY `id` DESC';
780 return PMA_DBI_fetch_result($hist_query, null, null, $GLOBALS['controllink']);
781 } // end of 'PMA_getHistory()' function
784 * purges SQL history
786 * deletes entries that exceeds $cfg['QueryHistoryMax'], oldest first, for the
787 * given user
789 * @uses $cfg['QueryHistoryMax']
790 * @uses $cfg['QueryHistoryDB']
791 * @uses $GLOBALS['controllink']
792 * @uses PMA_backquote()
793 * @uses PMA_sqlAddSlashes()
794 * @uses PMA_query_as_controluser()
795 * @uses PMA_DBI_fetch_value()
796 * @param string $username the username
797 * @access public
799 function PMA_purgeHistory($username)
801 $cfgRelation = PMA_getRelationsParam();
802 if (! $GLOBALS['cfg']['QueryHistoryDB'] || ! $cfgRelation['historywork']) {
803 return;
806 if (! $cfgRelation['historywork']) {
807 return;
810 $search_query = '
811 SELECT `timevalue`
812 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
813 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
814 ORDER BY `timevalue` DESC
815 LIMIT ' . $GLOBALS['cfg']['QueryHistoryMax'] . ', 1';
817 if ($max_time = PMA_DBI_fetch_value($search_query, 0, 0, $GLOBALS['controllink'])) {
818 PMA_query_as_controluser('
819 DELETE FROM
820 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
821 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
822 AND `timevalue` <= \'' . $max_time . '\'');
824 } // end of 'PMA_purgeHistory()' function
827 * Prepares the dropdown for one mode
829 * @uses $cfg['LimitChars']
830 * @uses $cfg['NaturalOrder']
831 * @uses PMA_strlen()
832 * @uses htmlspecialchars()
833 * @uses substr()
834 * @uses uksort()
835 * @uses ksort()
836 * @uses natcasesort()
837 * @uses asort()
838 * @param array $foreign the keys and values for foreigns
839 * @param string $data the current data of the dropdown
840 * @param string $mode the needed mode
842 * @return array the <option value=""><option>s
844 * @access protected
846 function PMA__foreignDropdownBuild($foreign, $data, $mode)
848 $reloptions = array();
850 if ($mode == 'id-content') {
851 // sort for id-content
852 if ($GLOBALS['cfg']['NaturalOrder']) {
853 uksort($foreign, 'strnatcasecmp');
854 } else {
855 ksort($foreign);
857 } elseif ($mode == 'content-id') {
858 // sort for content-id
859 if ($GLOBALS['cfg']['NaturalOrder']) {
860 natcasesort($foreign);
861 } else {
862 asort($foreign);
866 foreach ($foreign as $key => $value) {
868 if (PMA_strlen($value) <= $GLOBALS['cfg']['LimitChars']) {
869 $vtitle = '';
870 $value = htmlspecialchars($value);
871 } else {
872 $vtitle = htmlspecialchars($value);
873 $value = htmlspecialchars(substr($value, 0, $GLOBALS['cfg']['LimitChars']) . '...');
876 $reloption = ' <option value="' . htmlspecialchars($key) . '"';
877 if ($vtitle != '') {
878 $reloption .= ' title="' . $vtitle . '"';
881 if ((string) $key == (string) $data) {
882 $reloption .= ' selected="selected"';
885 if ($mode == 'content-id') {
886 $reloptions[] = $reloption . '>' . $value . '&nbsp;-&nbsp;' . htmlspecialchars($key) . '</option>' . "\n";
887 } else {
888 $reloptions[] = $reloption . '>' . htmlspecialchars($key) . '&nbsp;-&nbsp;' . $value . '</option>' . "\n";
890 } // end foreach
892 return $reloptions;
893 } // end of 'PMA__foreignDropdownBuild' function
896 * Outputs dropdown with values of foreign fields
898 * @uses $cfg['ForeignKeyMaxLimit']
899 * @uses $cfg['ForeignKeyDropdownOrder']
900 * @uses PMA__foreignDropdownBuild()
901 * @uses PMA_isValid()
902 * @uses implode()
903 * @param array array of the displayed row
904 * @param string the foreign field
905 * @param string the foreign field to display
906 * @param string the current data of the dropdown (field in row)
907 * @return string the <option value=""><option>s
908 * @access public
910 function PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, $data,
911 $max = null)
913 if (null === $max) {
914 $max = $GLOBALS['cfg']['ForeignKeyMaxLimit'];
917 $foreign = array();
919 // collect the data
920 foreach ($disp_row as $relrow) {
921 $key = $relrow[$foreign_field];
923 // if the display field has been defined for this foreign table
924 if ($foreign_display) {
925 $value = $relrow[$foreign_display];
926 } else {
927 $value = '';
928 } // end if ($foreign_display)
930 $foreign[$key] = $value;
931 } // end foreach
933 // put the dropdown sections in correct order
934 $top = array();
935 $bot = array();
936 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'], 'array')) {
937 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][0])) {
938 $top = PMA__foreignDropdownBuild($foreign, $data,
939 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][0]);
941 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][1])) {
942 $bot = PMA__foreignDropdownBuild($foreign, $data,
943 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][1]);
945 } else {
946 $top = PMA__foreignDropdownBuild($foreign, $data, 'id-content');
947 $bot = PMA__foreignDropdownBuild($foreign, $data, 'content-id');
950 // beginning of dropdown
951 $ret = '<option value="">&nbsp;</option>' . "\n";
953 $top_count = count($top);
954 if ($max == -1 || $top_count < $max) {
955 $ret .= implode('', $top);
956 if ($top_count > 0) {
957 $ret .= ' <option value="">&nbsp;</option>' . "\n";
958 $ret .= ' <option value="">&nbsp;</option>' . "\n";
961 $ret .= implode('', $bot);
963 return $ret;
964 } // end of 'PMA_foreignDropdown()' function
967 * Gets foreign keys in preparation for a drop-down selector
968 * Thanks to <markus@noga.de>
970 * @uses PMA_Table::countRecords()
971 * @uses PMA_backquote()
972 * @uses PMA_getDisplayField()
973 * @uses PMA_sqlAddslashes()
974 * @uses PMA_DBI_fetch_value()
975 * @uses PMA_DBI_free_result()
976 * @uses PMA_DBI_query()
977 * @uses PMA_DBI_num_rows()
978 * @uses PMA_DBI_fetch_assoc()
979 * @param array array of the foreign keys
980 * @param string the foreign field name
981 * @param bool whether to override the total
982 * @param string a possible filter
983 * @param string a possible LIMIT clause
984 * @return array data about the foreign keys
985 * @access public
988 function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filter, $foreign_limit)
990 // we always show the foreign field in the drop-down; if a display
991 // field is defined, we show it besides the foreign field
992 $foreign_link = false;
993 if ($foreigners && isset($foreigners[$field])) {
994 $foreigner = $foreigners[$field];
995 $foreign_db = $foreigner['foreign_db'];
996 $foreign_table = $foreigner['foreign_table'];
997 $foreign_field = $foreigner['foreign_field'];
999 // Count number of rows in the foreign table. Currently we do
1000 // not use a drop-down if more than 200 rows in the foreign table,
1001 // for speed reasons and because we need a better interface for this.
1003 // We could also do the SELECT anyway, with a LIMIT, and ensure that
1004 // the current value of the field is one of the choices.
1006 $the_total = PMA_Table::countRecords($foreign_db, $foreign_table);
1008 if ($override_total == true || $the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']) {
1009 // foreign_display can be FALSE if no display field defined:
1010 $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
1012 $f_query_main = 'SELECT ' . PMA_backquote($foreign_field)
1013 . (($foreign_display == FALSE) ? '' : ', ' . PMA_backquote($foreign_display));
1014 $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table);
1015 $f_query_filter = empty($foreign_filter) ? '' : ' WHERE ' . PMA_backquote($foreign_field)
1016 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1017 . (($foreign_display == FALSE) ? '' : ' OR ' . PMA_backquote($foreign_display)
1018 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1020 $f_query_order = ($foreign_display == FALSE) ? '' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display);
1021 $f_query_limit = isset($foreign_limit) ? $foreign_limit : '';
1023 if (!empty($foreign_filter)) {
1024 $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter);
1025 if ($res) {
1026 $the_total = PMA_DBI_fetch_value($res);
1027 @PMA_DBI_free_result($res);
1028 } else {
1029 $the_total = 0;
1033 $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit);
1034 if ($disp && PMA_DBI_num_rows($disp) > 0) {
1035 // If a resultset has been created, pre-cache it in the $disp_row array
1036 // This helps us from not needing to use mysql_data_seek by accessing a pre-cached
1037 // PHP array. Usually those resultsets are not that big, so a performance hit should
1038 // not be expected.
1039 $disp_row = array();
1040 while ($single_disp_row = @PMA_DBI_fetch_assoc($disp)) {
1041 $disp_row[] = $single_disp_row;
1043 @PMA_DBI_free_result($disp);
1045 } else {
1046 $disp_row = null;
1047 $foreign_link = true;
1049 } // end if $foreigners
1051 $foreignData['foreign_link'] = $foreign_link;
1052 $foreignData['the_total'] = isset($the_total) ? $the_total : null;
1053 $foreignData['foreign_display'] = isset($foreign_display) ? $foreign_display : null;
1054 $foreignData['disp_row'] = isset($disp_row) ? $disp_row : null;
1055 $foreignData['foreign_field'] = isset($foreign_field) ? $foreign_field : null;
1056 return $foreignData;
1057 } // end of 'PMA_getForeignData()' function
1060 * Finds all related tables
1062 * @uses $GLOBALS['controllink']
1063 * @uses $GLOBALS['cfgRelation']
1064 * @uses $GLOBALS['db']
1065 * @param string whether to go from master to foreign or vice versa
1066 * @return boolean always TRUE
1067 * @global array $tab_left the list of tables that we still couldn't connect
1068 * @global array $tab_know the list of allready connected tables
1069 * @global string $fromclause
1071 * @access private
1073 function PMA_getRelatives($from)
1075 global $tab_left, $tab_know, $fromclause;
1077 if ($from == 'master') {
1078 $to = 'foreign';
1079 } else {
1080 $to = 'master';
1082 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
1083 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
1085 $rel_query = 'SELECT *'
1086 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db'])
1087 . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
1088 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1089 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1090 . ' AND ' . $from . '_table IN ' . $in_know
1091 . ' AND ' . $to . '_table IN ' . $in_left;
1092 $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']);
1093 while ($row = PMA_DBI_fetch_assoc($relations)) {
1094 $found_table = $row[$to . '_table'];
1095 if (isset($tab_left[$found_table])) {
1096 $fromclause
1097 .= "\n" . ' LEFT JOIN '
1098 . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($row[$to . '_table']) . ' ON '
1099 . PMA_backquote($row[$from . '_table']) . '.'
1100 . PMA_backquote($row[$from . '_field']) . ' = '
1101 . PMA_backquote($row[$to . '_table']) . '.'
1102 . PMA_backquote($row[$to . '_field']) . ' ';
1103 $tab_know[$found_table] = $found_table;
1104 unset($tab_left[$found_table]);
1106 } // end while
1108 return true;
1109 } // end of the "PMA_getRelatives()" function
1112 * Rename a field in relation tables
1114 * usually called after a field in a table was renamed in tbl_alter.php
1116 * @uses PMA_getRelationsParam()
1117 * @uses PMA_backquote()
1118 * @uses PMA_sqlAddslashes()
1119 * @uses PMA_query_as_controluser()
1120 * @param string $db
1121 * @param string $table
1122 * @param string $field
1123 * @param string $new_name
1125 function PMA_REL_renameField($db, $table, $field, $new_name)
1127 $cfgRelation = PMA_getRelationsParam();
1129 if ($cfgRelation['displaywork']) {
1130 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
1131 . ' SET display_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1132 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1133 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
1134 . ' AND display_field = \'' . PMA_sqlAddslashes($field) . '\'';
1135 PMA_query_as_controluser($table_query);
1138 if ($cfgRelation['relwork']) {
1139 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1140 . ' SET master_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1141 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
1142 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
1143 . ' AND master_field = \'' . PMA_sqlAddslashes($field) . '\'';
1144 PMA_query_as_controluser($table_query);
1146 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1147 . ' SET foreign_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1148 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
1149 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
1150 . ' AND foreign_field = \'' . PMA_sqlAddslashes($field) . '\'';
1151 PMA_query_as_controluser($table_query);
1152 } // end if relwork
1156 * Create a PDF page
1158 * @uses __('no description')
1159 * @uses PMA_backquote()
1160 * @uses $GLOBALS['cfgRelation']['db']
1161 * @uses PMA_sqlAddslashes()
1162 * @uses PMA_query_as_controluser()
1163 * @uses PMA_DBI_insert_id()
1164 * @uses $GLOBALS['controllink']
1165 * @param string $newpage
1166 * @param array $cfgRelation
1167 * @param string $db
1168 * @param string $query_default_option
1169 * @return string $pdf_page_number
1171 function PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option) {
1172 if (! isset($newpage) || $newpage == '') {
1173 $newpage = __('no description');
1175 $ins_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1176 . ' (db_name, page_descr)'
1177 . ' VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($newpage) . '\')';
1178 PMA_query_as_controluser($ins_query, FALSE, $query_default_option);
1179 return PMA_DBI_insert_id(isset($GLOBALS['controllink']) ? $GLOBALS['controllink'] : '');