Bug #3153409 [core] 0 row(s) affected
[phpmyadmin-themes.git] / libraries / relation.lib.php
blobb98fb6ad6c161d06e6987cdd49bb4c0cb203fd0e
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
28 * @author Mike Beck <mikebeck@users.sourceforge.net>
30 function PMA_query_as_controluser($sql, $show_error = true, $options = 0)
32 // Avoid caching of the number of rows affected; for example, this function
33 // is called for tracking purposes but we want to display the correct number
34 // of rows affected by the original query, not by the query generated for
35 // tracking.
36 $cache_affected_rows = false;
38 if ($show_error) {
39 $result = PMA_DBI_query($sql, $GLOBALS['controllink'], $options, $cache_affected_rows);
40 } else {
41 $result = @PMA_DBI_try_query($sql, $GLOBALS['controllink'], $options, $cache_affected_rows);
42 } // end if... else...
44 if ($result) {
45 return $result;
46 } else {
47 return false;
49 } // end of the "PMA_query_as_controluser()" function
51 /**
52 * @uses $_SESSION['relation'][$GLOBALS['server']] for caching
53 * @uses $GLOBALS['cfgRelation'] to set it
54 * @uses $GLOBALS['server'] to ensure we are using server-specific pmadb
55 * @uses PMA__getRelationsParam()
56 * @uses PMA_printRelationsParamDiagnostic()
57 * @param bool $verbose whether to print diagnostic info
58 * @return array $cfgRelation
60 function PMA_getRelationsParam($verbose = false)
62 if (empty($_SESSION['relation'][$GLOBALS['server']])) {
63 $_SESSION['relation'][$GLOBALS['server']] = PMA__getRelationsParam();
66 // just for BC but needs to be before PMA_printRelationsParamDiagnostic()
67 // which uses it
68 $GLOBALS['cfgRelation'] = $_SESSION['relation'][$GLOBALS['server']];
70 if ($verbose) {
71 PMA_printRelationsParamDiagnostic($_SESSION['relation'][$GLOBALS['server']]);
74 return $_SESSION['relation'][$GLOBALS['server']];
77 /**
78 * prints out diagnostic info for pma relation feature
80 * @uses $GLOBALS['server']
81 * @uses $GLOBALS['controllink']
82 * @uses $GLOBALS['strNotOK']
83 * @uses $GLOBALS['strDocu']
84 * @uses $GLOBALS['strGeneralRelationFeat']
85 * @uses $GLOBALS['strDisabled']
86 * @uses $GLOBALS['strEnabled']
87 * @uses $GLOBALS['strDisplayFeat']
88 * @uses $GLOBALS['strCreatePdfFeat']
89 * @uses $GLOBALS['strColComFeat']
90 * @uses $GLOBALS['strBookmarkQuery']
91 * @uses $GLOBALS['strUpdComTab']
92 * @uses $GLOBALS['strQuerySQLHistory']
93 * @uses $GLOBALS['strDesigner']
94 * @uses $cfg['Server']['pmadb']
95 * @uses sprintf()
96 * @uses PMA_printDiagMessageForFeature()
97 * @uses PMA_printDiagMessageForParameter()
98 * @param array $cfgRelation
100 function PMA_printRelationsParamDiagnostic($cfgRelation)
102 $messages['error'] = '<font color="red"><strong>' . $GLOBALS['strNotOK']
103 . '</strong></font> [ <a href="Documentation.html#%s" target="documentation">'
104 . $GLOBALS['strDocu'] . '</a> ]';
106 $messages['ok'] = '<font color="green"><strong>' . $GLOBALS['strOK'] . '</strong></font>';
107 $messages['enabled'] = '<font color="green">' . $GLOBALS['strEnabled'] . '</font>';
108 $messages['disabled'] = '<font color="red">' . $GLOBALS['strDisabled'] . '</font>';
110 if (false === $GLOBALS['cfg']['Server']['pmadb']) {
111 echo 'PMA Database ... '
112 . sprintf($messages['error'], 'pmadb')
113 . '<br />' . "\n"
114 . $GLOBALS['strGeneralRelationFeat']
115 . ' <font color="green">' . $GLOBALS['strDisabled']
116 . '</font>' . "\n";
117 return;
120 echo '<table>' . "\n";
122 PMA_printDiagMessageForParameter('pmadb', $GLOBALS['cfg']['Server']['pmadb'], $messages, 'pmadb');
124 PMA_printDiagMessageForParameter('relation', isset($cfgRelation['relation']), $messages, 'relation');
126 PMA_printDiagMessageForFeature('strGeneralRelationFeat', 'relwork', $messages);
128 PMA_printDiagMessageForParameter('table_info', isset($cfgRelation['table_info']), $messages, 'table_info');
130 PMA_printDiagMessageForFeature('strDisplayFeat', 'displaywork', $messages);
132 PMA_printDiagMessageForParameter('table_coords', isset($cfgRelation['table_coords']), $messages, 'table_coords');
134 PMA_printDiagMessageForParameter('pdf_pages', isset($cfgRelation['pdf_pages']), $messages, 'table_coords');
136 PMA_printDiagMessageForFeature('strCreatePdfFeat', 'pdfwork', $messages);
138 PMA_printDiagMessageForParameter('column_info', isset($cfgRelation['column_info']), $messages, 'col_com');
140 PMA_printDiagMessageForFeature('strColComFeat', 'commwork', $messages, false);
142 PMA_printDiagMessageForFeature('strMIME_transformation', 'mimework', $messages);
144 if ($cfgRelation['commwork'] && ! $cfgRelation['mimework']) {
145 echo '<tr><td colspan=2 align="left">' . $GLOBALS['strUpdComTab'] . '</td></tr>' . "\n";
148 PMA_printDiagMessageForParameter('bookmarktable', isset($cfgRelation['bookmark']), $messages, 'bookmark');
150 PMA_printDiagMessageForFeature('strBookmarkQuery', 'bookmarkwork', $messages);
152 PMA_printDiagMessageForParameter('history', isset($cfgRelation['history']), $messages, 'history');
154 PMA_printDiagMessageForFeature('strQuerySQLHistory', 'historywork', $messages);
156 PMA_printDiagMessageForParameter('designer_coords', isset($cfgRelation['designer_coords']), $messages, 'designer_coords');
158 PMA_printDiagMessageForFeature('strDesigner', 'designerwork', $messages);
160 PMA_printDiagMessageForParameter('tracking', isset($cfgRelation['tracking']), $messages, 'tracking');
162 PMA_printDiagMessageForFeature('strTracking', 'trackingwork', $messages);
164 echo '</table>' . "\n";
168 * prints out one diagnostic message for a feature
170 * @param string feature name in a message string
171 * @param string the $GLOBALS['cfgRelation'] parameter to check
172 * @param array utility messages
173 * @param boolean whether to skip a line after the message
175 function PMA_printDiagMessageForFeature($feature_name, $relation_parameter, $messages, $skip_line=true)
177 echo ' <tr><td colspan=2 align="right">' . $GLOBALS[$feature_name] . ': '
178 . ($GLOBALS['cfgRelation'][$relation_parameter] ? $messages['enabled'] : $messages['disabled'])
179 . '</td></tr>' . "\n";
180 if ($skip_line) {
181 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
186 * prints out one diagnostic message for a configuration parameter
188 * @param string config parameter name to display
189 * @param boolean whether this parameter is set
190 * @param array utility messages
191 * @param string anchor in Documentation.html
193 function PMA_printDiagMessageForParameter($parameter, $relation_parameter_set, $messages, $doc_anchor)
195 echo ' <tr><th align="left">';
196 echo '$cfg[\'Servers\'][$i][\'' . $parameter . '\'] ... </th><td align="right">';
197 echo ($relation_parameter_set ? $messages['ok'] : sprintf($messages['error'], $doc_anchor)) . '</td></tr>' . "\n";
202 * Defines the relation parameters for the current user
203 * just a copy of the functions used for relations ;-)
204 * but added some stuff to check what will work
206 * @uses $cfg['Server']['user']
207 * @uses $cfg['Server']['pmadb']
208 * @uses $cfg['Server']['verbose_check']
209 * @uses $GLOBALS['server']
210 * @uses $GLOBALS['controllink']
211 * @uses PMA_DBI_QUERY_STORE
212 * @uses PMA_DBI_select_db()
213 * @uses PMA_backquote()
214 * @uses PMA_query_as_controluser()
215 * @uses PMA_DBI_fetch_row()
216 * @uses PMA_DBI_free_result()
217 * @access protected
218 * @author Mike Beck <mikebeck@users.sourceforge.net>
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 * @author Mike Beck <mikebeck@users.sourceforge.net>
363 * @author Marc Delisle
364 * @access public
365 * @uses $GLOBALS['controllink']
366 * @uses $GLOBALS['information_schema_relations']
367 * @uses PMA_getRelationsParam()
368 * @uses PMA_backquote()
369 * @uses PMA_sqlAddslashes()
370 * @uses PMA_DBI_fetch_result()
371 * @uses PMA_DBI_fetch_value()
372 * @uses PMA_SQP_analyze()
373 * @uses PMA_SQP_parse()
374 * @uses count()
375 * @uses strlen()
376 * @param string $db the name of the db to check for
377 * @param string $table the name of the table to check for
378 * @param string $column the name of the column to check for
379 * @param string $source the source for foreign key information
380 * @return array db,table,column
382 function PMA_getForeigners($db, $table, $column = '', $source = 'both')
384 $cfgRelation = PMA_getRelationsParam();
385 $foreign = array();
387 if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
388 $rel_query = '
389 SELECT `master_field`,
390 `foreign_db`,
391 `foreign_table`,
392 `foreign_field`
393 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
394 WHERE `master_db` = \'' . PMA_sqlAddslashes($db) . '\'
395 AND `master_table` = \'' . PMA_sqlAddslashes($table) . '\' ';
396 if (strlen($column)) {
397 $rel_query .= ' AND `master_field` = \'' . PMA_sqlAddslashes($column) . '\'';
399 $foreign = PMA_DBI_fetch_result($rel_query, 'master_field', null, $GLOBALS['controllink']);
402 if (($source == 'both' || $source == 'foreign') && strlen($table)) {
403 $show_create_table_query = 'SHOW CREATE TABLE '
404 . PMA_backquote($db) . '.' . PMA_backquote($table);
405 $show_create_table = PMA_DBI_fetch_value($show_create_table_query, 0, 1);
406 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
408 foreach ($analyzed_sql[0]['foreign_keys'] as $one_key) {
409 // The analyzer may return more than one column name in the
410 // index list or the ref_index_list; if this happens,
411 // the current logic just discards the whole index; having
412 // more than one index field is currently unsupported (see FAQ 3.6)
413 if (count($one_key['index_list']) == 1) {
414 foreach ($one_key['index_list'] as $i => $field) {
415 // If a foreign key is defined in the 'internal' source (pmadb)
416 // and as a native foreign key, we won't get it twice
417 // if $source='both' because we use $field as key
419 // The parser looks for a CONSTRAINT clause just before
420 // the FOREIGN KEY clause. It finds it (as output from
421 // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
422 // versions like 3.23.58.
423 // In those cases, the FOREIGN KEY parsing will put numbers
424 // like -1, 0, 1... instead of the constraint number.
426 if (isset($one_key['constraint'])) {
427 $foreign[$field]['constraint'] = $one_key['constraint'];
430 if (isset($one_key['ref_db_name'])) {
431 $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
432 } else {
433 $foreign[$field]['foreign_db'] = $db;
435 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
436 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
437 if (isset($one_key['on_delete'])) {
438 $foreign[$field]['on_delete'] = $one_key['on_delete'];
440 if (isset($one_key['on_update'])) {
441 $foreign[$field]['on_update'] = $one_key['on_update'];
449 * Emulating relations for some information_schema tables
451 if ($db == 'information_schema'
452 && ($source == 'internal' || $source == 'both')) {
453 require_once './libraries/information_schema_relations.lib.php';
455 if (isset($GLOBALS['information_schema_relations'][$table])) {
456 foreach ($GLOBALS['information_schema_relations'][$table] as $field => $relations) {
457 if ((! strlen($column) || $column == $field)
458 && (! isset($foreign[$field]) || ! strlen($foreign[$field]))) {
459 $foreign[$field] = $relations;
465 return $foreign;
466 } // end of the 'PMA_getForeigners()' function
469 * Gets the display field of a table
471 * @access public
472 * @author Mike Beck <mikebeck@users.sourceforge.net>
473 * @uses $GLOBALS['controllink']
474 * @uses PMA_getRelationsParam()
475 * @uses PMA_backquote()
476 * @uses PMA_sqlAddslashes()
477 * @uses PMA_DBI_fetch_single_row()
478 * @uses trim()
479 * @param string $db the name of the db to check for
480 * @param string $table the name of the table to check for
481 * @return string field name
483 function PMA_getDisplayField($db, $table)
485 $cfgRelation = PMA_getRelationsParam();
488 * Try to fetch the display field from DB.
490 if ($cfgRelation['displaywork']) {
491 $disp_query = '
492 SELECT `display_field`
493 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info']) . '
494 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
495 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'';
497 $row = PMA_DBI_fetch_single_row($disp_query, 'ASSOC', $GLOBALS['controllink']);
498 if (isset($row['display_field'])) {
499 return $row['display_field'];
504 * Emulating the display field for some information_schema tables.
506 if ($db == 'information_schema') {
507 switch ($table) {
508 case 'CHARACTER_SETS': return 'DESCRIPTION';
509 case 'TABLES': return 'TABLE_COMMENT';
514 * No Luck...
516 return false;
518 } // end of the 'PMA_getDisplayField()' function
521 * Gets the comments for all rows of a table or the db itself
523 * @author Mike Beck <mikebeck@users.sourceforge.net>
524 * @author lem9
525 * @access public
526 * @uses PMA_DBI_get_fields()
527 * @uses PMA_getDbComment()
528 * @param string the name of the db to check for
529 * @param string the name of the table to check for
530 * @return array [field_name] = comment
532 function PMA_getComments($db, $table = '')
534 $comments = array();
536 if ($table != '') {
537 // MySQL native column comments
538 $fields = PMA_DBI_get_fields($db, $table);
539 if ($fields) {
540 foreach ($fields as $key => $field) {
541 if (! empty($field['Comment'])) {
542 $comments[$field['Field']] = $field['Comment'];
546 } else {
547 $comments[] = PMA_getDbComment($db);
550 return $comments;
551 } // end of the 'PMA_getComments()' function
554 * Gets the comment for a db
556 * @author Mike Beck <mikebeck@users.sourceforge.net>
557 * @author lem9
558 * @access public
559 * @uses PMA_DBI_QUERY_STORE
560 * @uses PMA_DBI_num_rows()
561 * @uses PMA_DBI_fetch_assoc()
562 * @uses PMA_DBI_free_result()
563 * @uses PMA_getRelationsParam()
564 * @uses PMA_backquote()
565 * @uses PMA_sqlAddslashes()
566 * @uses PMA_query_as_controluser()
567 * @uses strlen()
568 * @param string the name of the db to check for
569 * @return string comment
571 function PMA_getDbComment($db)
573 $cfgRelation = PMA_getRelationsParam();
574 $comment = '';
576 if ($cfgRelation['commwork']) {
577 // pmadb internal db comment
578 $com_qry = "
579 SELECT `comment`
580 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
581 WHERE db_name = '" . PMA_sqlAddslashes($db) . "'
582 AND table_name = ''
583 AND column_name = '(db_comment)'";
584 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE);
586 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
587 $row = PMA_DBI_fetch_assoc($com_rs);
588 $comment = $row['comment'];
590 PMA_DBI_free_result($com_rs);
593 return $comment;
594 } // end of the 'PMA_getDbComment()' function
597 * Gets the comment for a db
599 * @author Mike Beck <mikebeck@users.sourceforge.net>
600 * @author lem9
601 * @access public
602 * @uses PMA_DBI_QUERY_STORE
603 * @uses PMA_DBI_num_rows()
604 * @uses PMA_DBI_fetch_assoc()
605 * @uses PMA_DBI_free_result()
606 * @uses PMA_getRelationsParam()
607 * @uses PMA_backquote()
608 * @uses PMA_sqlAddslashes()
609 * @uses PMA_query_as_controluser()
610 * @uses strlen()
611 * @param string the name of the db to check for
612 * @return string comment
614 function PMA_getDbComments()
616 $cfgRelation = PMA_getRelationsParam();
617 $comments = array();
619 if ($cfgRelation['commwork']) {
620 // pmadb internal db comment
621 $com_qry = "
622 SELECT `db_name`, `comment`
623 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
624 WHERE `column_name` = '(db_comment)'";
625 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE);
627 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
628 while ($row = PMA_DBI_fetch_assoc($com_rs)) {
629 $comments[$row['db_name']] = $row['comment'];
632 PMA_DBI_free_result($com_rs);
635 return $comments;
636 } // end of the 'PMA_getDbComments()' function
639 * Set a database comment to a certain value.
641 * @uses PMA_getRelationsParam()
642 * @uses PMA_backquote()
643 * @uses PMA_sqlAddslashes()
644 * @uses PMA_query_as_controluser()
645 * @uses strlen()
646 * @access public
647 * @param string $db the name of the db
648 * @param string $comment the value of the column
649 * @return boolean true, if comment-query was made.
651 function PMA_setDbComment($db, $comment = '')
653 $cfgRelation = PMA_getRelationsParam();
655 if (! $cfgRelation['commwork']) {
656 return false;
659 if (strlen($comment)) {
660 $upd_query = "
661 INSERT INTO
662 " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
663 (`db_name`, `table_name`, `column_name`, `comment`)
664 VALUES (
665 '" . PMA_sqlAddslashes($db) . "',
667 '(db_comment)',
668 '" . PMA_sqlAddslashes($comment) . "')
669 ON DUPLICATE KEY UPDATE
670 `comment` = '" . PMA_sqlAddslashes($comment) . "'";
671 } else {
672 $upd_query = '
673 DELETE FROM
674 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
675 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
676 AND `table_name` = \'\'
677 AND `column_name` = \'(db_comment)\'';
680 if (isset($upd_query)){
681 return PMA_query_as_controluser($upd_query);
684 return false;
685 } // end of 'PMA_setDbComment()' function
688 * Set a SQL history entry
690 * @uses $_SESSION['sql_history']
691 * @uses $cfg['QueryHistoryDB']
692 * @uses $cfg['QueryHistoryMax']
693 * @uses PMA_getRelationsParam()
694 * @uses PMA_query_as_controluser()
695 * @uses PMA_backquote()
696 * @uses PMA_sqlAddslashes()
697 * @uses count()
698 * @uses md5()
699 * @uses array_shift()
700 * @param string $db the name of the db
701 * @param string $table the name of the table
702 * @param string $username the username
703 * @param string $sqlquery the sql query
704 * @access public
706 function PMA_setHistory($db, $table, $username, $sqlquery)
708 if (strlen($sqlquery) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
709 return;
712 $cfgRelation = PMA_getRelationsParam();
714 if (! isset($_SESSION['sql_history'])) {
715 $_SESSION['sql_history'] = array();
718 $key = md5($sqlquery . $db . $table);
720 if (isset($_SESSION['sql_history'][$key])) {
721 unset($_SESSION['sql_history'][$key]);
724 $_SESSION['sql_history'][$key] = array(
725 'db' => $db,
726 'table' => $table,
727 'sqlquery' => $sqlquery,
730 if (count($_SESSION['sql_history']) > $GLOBALS['cfg']['QueryHistoryMax']) {
731 // history should not exceed a maximum count
732 array_shift($_SESSION['sql_history']);
735 if (! $cfgRelation['historywork'] || ! $GLOBALS['cfg']['QueryHistoryDB']) {
736 return;
739 PMA_query_as_controluser('
740 INSERT INTO
741 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
742 (`username`,
743 `db`,
744 `table`,
745 `timevalue`,
746 `sqlquery`)
747 VALUES
748 (\'' . PMA_sqlAddslashes($username) . '\',
749 \'' . PMA_sqlAddslashes($db) . '\',
750 \'' . PMA_sqlAddslashes($table) . '\',
751 NOW(),
752 \'' . PMA_sqlAddslashes($sqlquery) . '\')');
753 } // end of 'PMA_setHistory()' function
756 * Gets a SQL history entry
758 * @uses $_SESSION['sql_history']
759 * @uses $GLOBALS['controllink']
760 * @uses PMA_getRelationsParam()
761 * @uses PMA_backquote()
762 * @uses PMA_sqlAddslashes()
763 * @uses PMA_DBI_fetch_result()
764 * @uses array_reverse()
765 * @param string $username the username
766 * @return array list of history items
767 * @access public
769 function PMA_getHistory($username)
771 $cfgRelation = PMA_getRelationsParam();
773 if (isset($_SESSION['sql_history'])) {
774 return array_reverse($_SESSION['sql_history']);
777 if (! $cfgRelation['historywork']) {
778 return false;
781 $hist_query = '
782 SELECT `db`,
783 `table`,
784 `sqlquery`
785 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
786 WHERE `username` = \'' . PMA_sqlAddslashes($username) . '\'
787 ORDER BY `id` DESC';
789 return PMA_DBI_fetch_result($hist_query, null, null, $GLOBALS['controllink']);
790 } // end of 'PMA_getHistory()' function
793 * purges SQL history
795 * deletes entries that exceeds $cfg['QueryHistoryMax'], oldest first, for the
796 * given user
798 * @uses $cfg['QueryHistoryMax']
799 * @uses $cfg['QueryHistoryDB']
800 * @uses $GLOBALS['controllink']
801 * @uses PMA_backquote()
802 * @uses PMA_sqlAddSlashes()
803 * @uses PMA_query_as_controluser()
804 * @uses PMA_DBI_fetch_value()
805 * @param string $username the username
806 * @access public
808 function PMA_purgeHistory($username)
810 $cfgRelation = PMA_getRelationsParam();
811 if (! $GLOBALS['cfg']['QueryHistoryDB'] || ! $cfgRelation['historywork']) {
812 return;
815 if (! $cfgRelation['historywork']) {
816 return;
819 $search_query = '
820 SELECT `timevalue`
821 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
822 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
823 ORDER BY `timevalue` DESC
824 LIMIT ' . $GLOBALS['cfg']['QueryHistoryMax'] . ', 1';
826 if ($max_time = PMA_DBI_fetch_value($search_query, 0, 0, $GLOBALS['controllink'])) {
827 PMA_query_as_controluser('
828 DELETE FROM
829 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
830 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
831 AND `timevalue` <= \'' . $max_time . '\'');
833 } // end of 'PMA_purgeHistory()' function
836 * Prepares the dropdown for one mode
838 * @uses $cfg['LimitChars']
839 * @uses $cfg['NaturalOrder']
840 * @uses PMA_strlen()
841 * @uses htmlspecialchars()
842 * @uses substr()
843 * @uses uksort()
844 * @uses ksort()
845 * @uses natcasesort()
846 * @uses asort()
847 * @param array $foreign the keys and values for foreigns
848 * @param string $data the current data of the dropdown
849 * @param string $mode the needed mode
851 * @return array the <option value=""><option>s
853 * @access protected
855 function PMA__foreignDropdownBuild($foreign, $data, $mode)
857 $reloptions = array();
859 if ($mode == 'id-content') {
860 // sort for id-content
861 if ($GLOBALS['cfg']['NaturalOrder']) {
862 uksort($foreign, 'strnatcasecmp');
863 } else {
864 ksort($foreign);
866 } elseif ($mode == 'content-id') {
867 // sort for content-id
868 if ($GLOBALS['cfg']['NaturalOrder']) {
869 natcasesort($foreign);
870 } else {
871 asort($foreign);
875 foreach ($foreign as $key => $value) {
877 if (PMA_strlen($value) <= $GLOBALS['cfg']['LimitChars']) {
878 $vtitle = '';
879 $value = htmlspecialchars($value);
880 } else {
881 $vtitle = htmlspecialchars($value);
882 $value = htmlspecialchars(substr($value, 0, $GLOBALS['cfg']['LimitChars']) . '...');
885 $reloption = ' <option value="' . htmlspecialchars($key) . '"';
886 if ($vtitle != '') {
887 $reloption .= ' title="' . $vtitle . '"';
890 if ((string) $key == (string) $data) {
891 $reloption .= ' selected="selected"';
894 if ($mode == 'content-id') {
895 $reloptions[] = $reloption . '>' . $value . '&nbsp;-&nbsp;' . htmlspecialchars($key) . '</option>' . "\n";
896 } else {
897 $reloptions[] = $reloption . '>' . htmlspecialchars($key) . '&nbsp;-&nbsp;' . $value . '</option>' . "\n";
899 } // end foreach
901 return $reloptions;
902 } // end of 'PMA__foreignDropdownBuild' function
905 * Outputs dropdown with values of foreign fields
907 * @uses $cfg['ForeignKeyMaxLimit']
908 * @uses $cfg['ForeignKeyDropdownOrder']
909 * @uses PMA__foreignDropdownBuild()
910 * @uses PMA_isValid()
911 * @uses implode()
912 * @param array array of the displayed row
913 * @param string the foreign field
914 * @param string the foreign field to display
915 * @param string the current data of the dropdown (field in row)
916 * @return string the <option value=""><option>s
917 * @access public
919 function PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, $data,
920 $max = null)
922 if (null === $max) {
923 $max = $GLOBALS['cfg']['ForeignKeyMaxLimit'];
926 $foreign = array();
928 // collect the data
929 foreach ($disp_row as $relrow) {
930 $key = $relrow[$foreign_field];
932 // if the display field has been defined for this foreign table
933 if ($foreign_display) {
934 $value = $relrow[$foreign_display];
935 } else {
936 $value = '';
937 } // end if ($foreign_display)
939 $foreign[$key] = $value;
940 } // end foreach
942 // put the dropdown sections in correct order
943 $top = array();
944 $bot = array();
945 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'], 'array')) {
946 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][0])) {
947 $top = PMA__foreignDropdownBuild($foreign, $data,
948 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][0]);
950 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][1])) {
951 $bot = PMA__foreignDropdownBuild($foreign, $data,
952 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][1]);
954 } else {
955 $top = PMA__foreignDropdownBuild($foreign, $data, 'id-content');
956 $bot = PMA__foreignDropdownBuild($foreign, $data, 'content-id');
959 // beginning of dropdown
960 $ret = '<option value="">&nbsp;</option>' . "\n";
962 $top_count = count($top);
963 if ($max == -1 || $top_count < $max) {
964 $ret .= implode('', $top);
965 if ($top_count > 0) {
966 $ret .= ' <option value="">&nbsp;</option>' . "\n";
967 $ret .= ' <option value="">&nbsp;</option>' . "\n";
970 $ret .= implode('', $bot);
972 return $ret;
973 } // end of 'PMA_foreignDropdown()' function
976 * Gets foreign keys in preparation for a drop-down selector
977 * Thanks to <markus@noga.de>
979 * @uses PMA_Table::countRecords()
980 * @uses PMA_backquote()
981 * @uses PMA_getDisplayField()
982 * @uses PMA_sqlAddslashes()
983 * @uses PMA_DBI_fetch_value()
984 * @uses PMA_DBI_free_result()
985 * @uses PMA_DBI_query()
986 * @uses PMA_DBI_num_rows()
987 * @uses PMA_DBI_fetch_assoc()
988 * @param array array of the foreign keys
989 * @param string the foreign field name
990 * @param bool whether to override the total
991 * @param string a possible filter
992 * @param string a possible LIMIT clause
993 * @return array data about the foreign keys
994 * @access public
997 function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filter, $foreign_limit)
999 // we always show the foreign field in the drop-down; if a display
1000 // field is defined, we show it besides the foreign field
1001 $foreign_link = false;
1002 if ($foreigners && isset($foreigners[$field])) {
1003 $foreigner = $foreigners[$field];
1004 $foreign_db = $foreigner['foreign_db'];
1005 $foreign_table = $foreigner['foreign_table'];
1006 $foreign_field = $foreigner['foreign_field'];
1008 // Count number of rows in the foreign table. Currently we do
1009 // not use a drop-down if more than 200 rows in the foreign table,
1010 // for speed reasons and because we need a better interface for this.
1012 // We could also do the SELECT anyway, with a LIMIT, and ensure that
1013 // the current value of the field is one of the choices.
1015 $the_total = PMA_Table::countRecords($foreign_db, $foreign_table);
1017 if ($override_total == true || $the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']) {
1018 // foreign_display can be FALSE if no display field defined:
1019 $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
1021 $f_query_main = 'SELECT ' . PMA_backquote($foreign_field)
1022 . (($foreign_display == FALSE) ? '' : ', ' . PMA_backquote($foreign_display));
1023 $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table);
1024 $f_query_filter = empty($foreign_filter) ? '' : ' WHERE ' . PMA_backquote($foreign_field)
1025 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1026 . (($foreign_display == FALSE) ? '' : ' OR ' . PMA_backquote($foreign_display)
1027 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1029 $f_query_order = ($foreign_display == FALSE) ? '' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display);
1030 $f_query_limit = isset($foreign_limit) ? $foreign_limit : '';
1032 if (!empty($foreign_filter)) {
1033 $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter);
1034 if ($res) {
1035 $the_total = PMA_DBI_fetch_value($res);
1036 @PMA_DBI_free_result($res);
1037 } else {
1038 $the_total = 0;
1042 $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit);
1043 if ($disp && PMA_DBI_num_rows($disp) > 0) {
1044 // If a resultset has been created, pre-cache it in the $disp_row array
1045 // This helps us from not needing to use mysql_data_seek by accessing a pre-cached
1046 // PHP array. Usually those resultsets are not that big, so a performance hit should
1047 // not be expected.
1048 $disp_row = array();
1049 while ($single_disp_row = @PMA_DBI_fetch_assoc($disp)) {
1050 $disp_row[] = $single_disp_row;
1052 @PMA_DBI_free_result($disp);
1054 } else {
1055 $disp_row = null;
1056 $foreign_link = true;
1058 } // end if $foreigners
1060 $foreignData['foreign_link'] = $foreign_link;
1061 $foreignData['the_total'] = isset($the_total) ? $the_total : null;
1062 $foreignData['foreign_display'] = isset($foreign_display) ? $foreign_display : null;
1063 $foreignData['disp_row'] = isset($disp_row) ? $disp_row : null;
1064 $foreignData['foreign_field'] = isset($foreign_field) ? $foreign_field : null;
1065 return $foreignData;
1066 } // end of 'PMA_getForeignData()' function
1069 * Finds all related tables
1071 * @uses $GLOBALS['controllink']
1072 * @uses $GLOBALS['cfgRelation']
1073 * @uses $GLOBALS['db']
1074 * @param string whether to go from master to foreign or vice versa
1075 * @return boolean always TRUE
1076 * @global array $tab_left the list of tables that we still couldn't connect
1077 * @global array $tab_know the list of allready connected tables
1078 * @global string $fromclause
1080 * @access private
1082 function PMA_getRelatives($from)
1084 global $tab_left, $tab_know, $fromclause;
1086 if ($from == 'master') {
1087 $to = 'foreign';
1088 } else {
1089 $to = 'master';
1091 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
1092 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
1094 $rel_query = 'SELECT *'
1095 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db'])
1096 . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
1097 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1098 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1099 . ' AND ' . $from . '_table IN ' . $in_know
1100 . ' AND ' . $to . '_table IN ' . $in_left;
1101 $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']);
1102 while ($row = PMA_DBI_fetch_assoc($relations)) {
1103 $found_table = $row[$to . '_table'];
1104 if (isset($tab_left[$found_table])) {
1105 $fromclause
1106 .= "\n" . ' LEFT JOIN '
1107 . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($row[$to . '_table']) . ' ON '
1108 . PMA_backquote($row[$from . '_table']) . '.'
1109 . PMA_backquote($row[$from . '_field']) . ' = '
1110 . PMA_backquote($row[$to . '_table']) . '.'
1111 . PMA_backquote($row[$to . '_field']) . ' ';
1112 $tab_know[$found_table] = $found_table;
1113 unset($tab_left[$found_table]);
1115 } // end while
1117 return true;
1118 } // end of the "PMA_getRelatives()" function
1121 * Rename a field in relation tables
1123 * usually called after a field in a table was renamed in tbl_alter.php
1125 * @uses PMA_getRelationsParam()
1126 * @uses PMA_backquote()
1127 * @uses PMA_sqlAddslashes()
1128 * @uses PMA_query_as_controluser()
1129 * @param string $db
1130 * @param string $table
1131 * @param string $field
1132 * @param string $new_name
1134 function PMA_REL_renameField($db, $table, $field, $new_name)
1136 $cfgRelation = PMA_getRelationsParam();
1138 if ($cfgRelation['displaywork']) {
1139 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
1140 . ' SET display_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1141 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1142 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
1143 . ' AND display_field = \'' . PMA_sqlAddslashes($field) . '\'';
1144 PMA_query_as_controluser($table_query);
1147 if ($cfgRelation['relwork']) {
1148 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1149 . ' SET master_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1150 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
1151 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
1152 . ' AND master_field = \'' . PMA_sqlAddslashes($field) . '\'';
1153 PMA_query_as_controluser($table_query);
1155 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1156 . ' SET foreign_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1157 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
1158 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
1159 . ' AND foreign_field = \'' . PMA_sqlAddslashes($field) . '\'';
1160 PMA_query_as_controluser($table_query);
1161 } // end if relwork
1165 * Create a PDF page
1167 * @uses $GLOBALS['strNoDescription']
1168 * @uses PMA_backquote()
1169 * @uses $GLOBALS['cfgRelation']['db']
1170 * @uses PMA_sqlAddslashes()
1171 * @uses PMA_query_as_controluser()
1172 * @uses PMA_DBI_insert_id()
1173 * @uses $GLOBALS['controllink']
1174 * @param string $newpage
1175 * @param array $cfgRelation
1176 * @param string $db
1177 * @param string $query_default_option
1178 * @return string $pdf_page_number
1180 function PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option) {
1181 if (! isset($newpage) || $newpage == '') {
1182 $newpage = $GLOBALS['strNoDescription'];
1184 $ins_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1185 . ' (db_name, page_descr)'
1186 . ' VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($newpage) . '\')';
1187 PMA_query_as_controluser($ins_query, FALSE, $query_default_option);
1188 return PMA_DBI_insert_id(isset($GLOBALS['controllink']) ? $GLOBALS['controllink'] : '');