Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / relation.lib.php
blob74800feb313d5299ae8a489bea7e14fe25c2388b
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 * @package phpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
13 * Executes a query as controluser if possible, otherwise as normal user
15 * @param string the query to execute
16 * @param boolean whether to display SQL error messages or not
18 * @return integer the result set, or false if no result set
20 * @access public
23 function PMA_query_as_controluser($sql, $show_error = true, $options = 0)
25 if ($show_error) {
26 $result = PMA_DBI_query($sql, $GLOBALS['controllink'], $options);
27 } else {
28 $result = @PMA_DBI_try_query($sql, $GLOBALS['controllink'], $options);
29 } // end if... else...
31 if ($result) {
32 return $result;
33 } else {
34 return false;
36 } // end of the "PMA_query_as_controluser()" function
38 /**
39 * @uses $_SESSION['relation'][$GLOBALS['server']] for caching
40 * @uses $GLOBALS['cfgRelation'] to set it
41 * @uses $GLOBALS['server'] to ensure we are using server-specific pmadb
42 * @uses PMA__getRelationsParam()
43 * @uses PMA_printRelationsParamDiagnostic()
44 * @param bool $verbose whether to print diagnostic info
45 * @return array $cfgRelation
47 function PMA_getRelationsParam($verbose = false)
49 if (empty($_SESSION['relation'][$GLOBALS['server']])) {
50 $_SESSION['relation'][$GLOBALS['server']] = PMA__getRelationsParam();
53 // just for BC but needs to be before PMA_printRelationsParamDiagnostic()
54 // which uses it
55 $GLOBALS['cfgRelation'] = $_SESSION['relation'][$GLOBALS['server']];
57 if ($verbose) {
58 PMA_printRelationsParamDiagnostic($_SESSION['relation'][$GLOBALS['server']]);
61 return $_SESSION['relation'][$GLOBALS['server']];
64 /**
65 * prints out diagnostic info for pma relation feature
67 * @uses $GLOBALS['server']
68 * @uses $GLOBALS['controllink']
69 * @uses $cfg['Server']['pmadb']
70 * @uses sprintf()
71 * @uses PMA_printDiagMessageForFeature()
72 * @uses PMA_printDiagMessageForParameter()
73 * @param array $cfgRelation
75 function PMA_printRelationsParamDiagnostic($cfgRelation)
77 $messages['error'] = '<font color="red"><strong>' . __('not OK')
78 . '</strong></font> [ <a href="Documentation.html#%s" target="documentation">'
79 . __('Documentation') . '</a> ]';
81 $messages['ok'] = '<font color="green"><strong>' . __('OK') . '</strong></font>';
82 $messages['enabled'] = '<font color="green">' . __('Enabled') . '</font>';
83 $messages['disabled'] = '<font color="red">' . __('Disabled') . '</font>';
85 if (false === $GLOBALS['cfg']['Server']['pmadb']) {
86 echo 'PMA Database ... '
87 . sprintf($messages['error'], 'pmadb')
88 . '<br />' . "\n"
89 . __('General relation features')
90 . ' <font color="green">' . __('Disabled')
91 . '</font>' . "\n";
92 return;
95 echo '<table>' . "\n";
97 PMA_printDiagMessageForParameter('pmadb', $GLOBALS['cfg']['Server']['pmadb'], $messages, 'pmadb');
99 PMA_printDiagMessageForParameter('relation', isset($cfgRelation['relation']), $messages, 'relation');
101 PMA_printDiagMessageForFeature(__('General relation features'), 'relwork', $messages);
103 PMA_printDiagMessageForParameter('table_info', isset($cfgRelation['table_info']), $messages, 'table_info');
105 PMA_printDiagMessageForFeature(__('Display Features'), 'displaywork', $messages);
107 PMA_printDiagMessageForParameter('table_coords', isset($cfgRelation['table_coords']), $messages, 'table_coords');
109 PMA_printDiagMessageForParameter('pdf_pages', isset($cfgRelation['pdf_pages']), $messages, 'table_coords');
111 PMA_printDiagMessageForFeature(__('Creation of PDFs'), 'pdfwork', $messages);
113 PMA_printDiagMessageForParameter('column_info', isset($cfgRelation['column_info']), $messages, 'col_com');
115 PMA_printDiagMessageForFeature(__('Displaying Column Comments'), 'commwork', $messages, false);
117 PMA_printDiagMessageForFeature(__('Browser transformation'), 'mimework', $messages);
119 if ($cfgRelation['commwork'] && ! $cfgRelation['mimework']) {
120 echo '<tr><td colspan=2 align="left">' . __('Please see the documentation on how to update your column_comments table') . '</td></tr>' . "\n";
123 PMA_printDiagMessageForParameter('bookmarktable', isset($cfgRelation['bookmark']), $messages, 'bookmark');
125 PMA_printDiagMessageForFeature(__('Bookmarked SQL query'), 'bookmarkwork', $messages);
127 PMA_printDiagMessageForParameter('history', isset($cfgRelation['history']), $messages, 'history');
129 PMA_printDiagMessageForFeature(__('SQL history'), 'historywork', $messages);
131 PMA_printDiagMessageForParameter('designer_coords', isset($cfgRelation['designer_coords']), $messages, 'designer_coords');
133 PMA_printDiagMessageForFeature(__('Designer'), 'designerwork', $messages);
135 PMA_printDiagMessageForParameter('tracking', isset($cfgRelation['tracking']), $messages, 'tracking');
137 PMA_printDiagMessageForFeature(__('Tracking'), 'trackingwork', $messages);
139 PMA_printDiagMessageForParameter('userconfig', isset($cfgRelation['userconfig']), $messages, 'userconfig');
141 PMA_printDiagMessageForFeature(__('User preferences'), 'userconfigwork', $messages);
143 echo '</table>' . "\n";
145 echo '<p>' . __('Quick steps to setup advanced features:') . '</p>';
146 echo '<ul>';
147 echo '<li>' . __('Create the needed tables with the <code>script/create_tables.sql</code>.') . ' ' . PMA_showDocu('linked-tables') . '</li>';
148 echo '<li>' . __('Create a pma user and give access to these tables.') . ' ' . PMA_showDocu('pmausr') . '</li>';
149 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>';
150 echo '<li>' . __('Re-login to phpMyAdmin to load the updated configuration file.') . '</li>';
151 echo '</ul>';
155 * prints out one diagnostic message for a feature
157 * @param string feature name in a message string
158 * @param string the $GLOBALS['cfgRelation'] parameter to check
159 * @param array utility messages
160 * @param boolean whether to skip a line after the message
162 function PMA_printDiagMessageForFeature($feature_name, $relation_parameter, $messages, $skip_line=true)
164 echo ' <tr><td colspan=2 align="right">' . $feature_name . ': '
165 . ($GLOBALS['cfgRelation'][$relation_parameter] ? $messages['enabled'] : $messages['disabled'])
166 . '</td></tr>' . "\n";
167 if ($skip_line) {
168 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
173 * prints out one diagnostic message for a configuration parameter
175 * @param string config parameter name to display
176 * @param boolean whether this parameter is set
177 * @param array utility messages
178 * @param string anchor in Documentation.html
180 function PMA_printDiagMessageForParameter($parameter, $relation_parameter_set, $messages, $doc_anchor)
182 echo ' <tr><th align="left">';
183 echo '$cfg[\'Servers\'][$i][\'' . $parameter . '\'] ... </th><td align="right">';
184 echo ($relation_parameter_set ? $messages['ok'] : sprintf($messages['error'], $doc_anchor)) . '</td></tr>' . "\n";
189 * Defines the relation parameters for the current user
190 * just a copy of the functions used for relations ;-)
191 * but added some stuff to check what will work
193 * @uses $cfg['Server']['user']
194 * @uses $cfg['Server']['pmadb']
195 * @uses $cfg['Server']['verbose_check']
196 * @uses $GLOBALS['server']
197 * @uses $GLOBALS['controllink']
198 * @uses PMA_DBI_QUERY_STORE
199 * @uses PMA_DBI_select_db()
200 * @uses PMA_backquote()
201 * @uses PMA_query_as_controluser()
202 * @uses PMA_DBI_fetch_row()
203 * @uses PMA_DBI_free_result()
204 * @access protected
205 * @return array the relation parameters for the current user
207 function PMA__getRelationsParam()
209 $cfgRelation = array();
210 $cfgRelation['relwork'] = false;
211 $cfgRelation['displaywork'] = false;
212 $cfgRelation['bookmarkwork']= false;
213 $cfgRelation['pdfwork'] = false;
214 $cfgRelation['commwork'] = false;
215 $cfgRelation['mimework'] = false;
216 $cfgRelation['historywork'] = false;
217 $cfgRelation['trackingwork'] = false;
218 $cfgRelation['designerwork'] = false;
219 $cfgRelation['userconfigwork'] = false;
220 $cfgRelation['allworks'] = false;
221 $cfgRelation['user'] = null;
222 $cfgRelation['db'] = null;
224 if ($GLOBALS['server'] == 0 || empty($GLOBALS['cfg']['Server']['pmadb'])
225 || ! PMA_DBI_select_db($GLOBALS['cfg']['Server']['pmadb'], $GLOBALS['controllink'])) {
226 // No server selected -> no bookmark table
227 // we return the array with the falses in it,
228 // to avoid some 'Unitialized string offset' errors later
229 $GLOBALS['cfg']['Server']['pmadb'] = false;
230 return $cfgRelation;
234 $cfgRelation['user'] = $GLOBALS['cfg']['Server']['user'];
235 $cfgRelation['db'] = $GLOBALS['cfg']['Server']['pmadb'];
237 // Now I just check if all tables that i need are present so I can for
238 // example enable relations but not pdf...
239 // I was thinking of checking if they have all required columns but I
240 // fear it might be too slow
242 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($GLOBALS['cfg']['Server']['pmadb']);
243 $tab_rs = PMA_query_as_controluser($tab_query, false, PMA_DBI_QUERY_STORE);
245 if (! $tab_rs) {
246 // query failed ... ?
247 //$GLOBALS['cfg']['Server']['pmadb'] = false;
248 return $cfgRelation;
251 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
252 if ($curr_table[0] == $GLOBALS['cfg']['Server']['bookmarktable']) {
253 $cfgRelation['bookmark'] = $curr_table[0];
254 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['relation']) {
255 $cfgRelation['relation'] = $curr_table[0];
256 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_info']) {
257 $cfgRelation['table_info'] = $curr_table[0];
258 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_coords']) {
259 $cfgRelation['table_coords'] = $curr_table[0];
260 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['designer_coords']) {
261 $cfgRelation['designer_coords'] = $curr_table[0];
262 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['column_info']) {
263 $cfgRelation['column_info'] = $curr_table[0];
264 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['pdf_pages']) {
265 $cfgRelation['pdf_pages'] = $curr_table[0];
266 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['history']) {
267 $cfgRelation['history'] = $curr_table[0];
268 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['tracking']) {
269 $cfgRelation['tracking'] = $curr_table[0];
270 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['userconfig']) {
271 $cfgRelation['userconfig'] = $curr_table[0];
273 } // end while
274 PMA_DBI_free_result($tab_rs);
276 if (isset($cfgRelation['relation'])) {
277 $cfgRelation['relwork'] = true;
278 if (isset($cfgRelation['table_info'])) {
279 $cfgRelation['displaywork'] = true;
282 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
283 $cfgRelation['pdfwork'] = true;
285 if (isset($cfgRelation['column_info'])) {
286 $cfgRelation['commwork'] = true;
288 if ($GLOBALS['cfg']['Server']['verbose_check']) {
289 $mime_query = 'SHOW FIELDS FROM '
290 . PMA_backquote($cfgRelation['db']) . '.'
291 . PMA_backquote($cfgRelation['column_info']);
292 $mime_rs = PMA_query_as_controluser($mime_query, false);
294 $mime_field_mimetype = false;
295 $mime_field_transformation = false;
296 $mime_field_transformation_options = false;
297 while ($curr_mime_field = @PMA_DBI_fetch_row($mime_rs)) {
298 if ($curr_mime_field[0] == 'mimetype') {
299 $mime_field_mimetype = true;
300 } elseif ($curr_mime_field[0] == 'transformation') {
301 $mime_field_transformation = true;
302 } elseif ($curr_mime_field[0] == 'transformation_options') {
303 $mime_field_transformation_options = true;
306 PMA_DBI_free_result($mime_rs);
308 if ($mime_field_mimetype
309 && $mime_field_transformation
310 && $mime_field_transformation_options) {
311 $cfgRelation['mimework'] = true;
313 } else {
314 $cfgRelation['mimework'] = true;
318 if (isset($cfgRelation['history'])) {
319 $cfgRelation['historywork'] = true;
322 if (isset($cfgRelation['tracking'])) {
323 $cfgRelation['trackingwork'] = true;
326 if (isset($cfgRelation['userconfig'])) {
327 $cfgRelation['userconfigwork'] = true;
330 // we do not absolutely need that the internal relations or the PDF
331 // schema feature be activated
332 if (isset($cfgRelation['designer_coords'])) {
333 $cfgRelation['designerwork'] = true;
336 if (isset($cfgRelation['bookmark'])) {
337 $cfgRelation['bookmarkwork'] = true;
340 if ($cfgRelation['relwork'] && $cfgRelation['displaywork']
341 && $cfgRelation['pdfwork'] && $cfgRelation['commwork']
342 && $cfgRelation['mimework'] && $cfgRelation['historywork']
343 && $cfgRelation['trackingwork'] && $cfgRelation['userconfigwork']
344 && $cfgRelation['bookmarkwork'] && $cfgRelation['designerwork']) {
345 $cfgRelation['allworks'] = true;
348 return $cfgRelation;
349 } // end of the 'PMA_getRelationsParam()' function
352 * Gets all Relations to foreign tables for a given table or
353 * optionally a given column in a table
355 * @access public
356 * @uses $GLOBALS['controllink']
357 * @uses $GLOBALS['information_schema_relations']
358 * @uses PMA_getRelationsParam()
359 * @uses PMA_backquote()
360 * @uses PMA_sqlAddslashes()
361 * @uses PMA_DBI_fetch_result()
362 * @uses PMA_DBI_fetch_value()
363 * @uses PMA_SQP_analyze()
364 * @uses PMA_SQP_parse()
365 * @uses count()
366 * @uses strlen()
367 * @param string $db the name of the db to check for
368 * @param string $table the name of the table to check for
369 * @param string $column the name of the column to check for
370 * @param string $source the source for foreign key information
371 * @return array db,table,column
373 function PMA_getForeigners($db, $table, $column = '', $source = 'both')
375 $cfgRelation = PMA_getRelationsParam();
376 $foreign = array();
378 if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
379 $rel_query = '
380 SELECT `master_field`,
381 `foreign_db`,
382 `foreign_table`,
383 `foreign_field`
384 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
385 WHERE `master_db` = \'' . PMA_sqlAddslashes($db) . '\'
386 AND `master_table` = \'' . PMA_sqlAddslashes($table) . '\' ';
387 if (strlen($column)) {
388 $rel_query .= ' AND `master_field` = \'' . PMA_sqlAddslashes($column) . '\'';
390 $foreign = PMA_DBI_fetch_result($rel_query, 'master_field', null, $GLOBALS['controllink']);
393 if (($source == 'both' || $source == 'foreign') && strlen($table)) {
394 $show_create_table_query = 'SHOW CREATE TABLE '
395 . PMA_backquote($db) . '.' . PMA_backquote($table);
396 $show_create_table = PMA_DBI_fetch_value($show_create_table_query, 0, 1);
397 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
399 foreach ($analyzed_sql[0]['foreign_keys'] as $one_key) {
400 // The analyzer may return more than one column name in the
401 // index list or the ref_index_list; if this happens,
402 // the current logic just discards the whole index; having
403 // more than one index field is currently unsupported (see FAQ 3.6)
404 if (count($one_key['index_list']) == 1) {
405 foreach ($one_key['index_list'] as $i => $field) {
406 // If a foreign key is defined in the 'internal' source (pmadb)
407 // and as a native foreign key, we won't get it twice
408 // if $source='both' because we use $field as key
410 // The parser looks for a CONSTRAINT clause just before
411 // the FOREIGN KEY clause. It finds it (as output from
412 // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
413 // versions like 3.23.58.
414 // In those cases, the FOREIGN KEY parsing will put numbers
415 // like -1, 0, 1... instead of the constraint number.
417 if (isset($one_key['constraint'])) {
418 $foreign[$field]['constraint'] = $one_key['constraint'];
421 if (isset($one_key['ref_db_name'])) {
422 $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
423 } else {
424 $foreign[$field]['foreign_db'] = $db;
426 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
427 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
428 if (isset($one_key['on_delete'])) {
429 $foreign[$field]['on_delete'] = $one_key['on_delete'];
431 if (isset($one_key['on_update'])) {
432 $foreign[$field]['on_update'] = $one_key['on_update'];
440 * Emulating relations for some information_schema tables
442 if ($db == 'information_schema'
443 && ($source == 'internal' || $source == 'both')) {
444 require_once './libraries/information_schema_relations.lib.php';
446 if (isset($GLOBALS['information_schema_relations'][$table])) {
447 foreach ($GLOBALS['information_schema_relations'][$table] as $field => $relations) {
448 if ((! strlen($column) || $column == $field)
449 && (! isset($foreign[$field]) || ! strlen($foreign[$field]))) {
450 $foreign[$field] = $relations;
456 return $foreign;
457 } // end of the 'PMA_getForeigners()' function
460 * Gets the display field of a table
462 * @access public
463 * @uses $GLOBALS['controllink']
464 * @uses PMA_getRelationsParam()
465 * @uses PMA_backquote()
466 * @uses PMA_sqlAddslashes()
467 * @uses PMA_DBI_fetch_single_row()
468 * @uses trim()
469 * @param string $db the name of the db to check for
470 * @param string $table the name of the table to check for
471 * @return string field name
473 function PMA_getDisplayField($db, $table)
475 $cfgRelation = PMA_getRelationsParam();
478 * Try to fetch the display field from DB.
480 if ($cfgRelation['displaywork']) {
481 $disp_query = '
482 SELECT `display_field`
483 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info']) . '
484 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
485 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'';
487 $row = PMA_DBI_fetch_single_row($disp_query, 'ASSOC', $GLOBALS['controllink']);
488 if (isset($row['display_field'])) {
489 return $row['display_field'];
494 * Emulating the display field for some information_schema tables.
496 if ($db == 'information_schema') {
497 switch ($table) {
498 case 'CHARACTER_SETS': return 'DESCRIPTION';
499 case 'TABLES': return 'TABLE_COMMENT';
504 * No Luck...
506 return false;
508 } // end of the 'PMA_getDisplayField()' function
511 * Gets the comments for all rows of a table or the db itself
513 * @access public
514 * @uses PMA_DBI_get_fields()
515 * @uses PMA_getDbComment()
516 * @param string the name of the db to check for
517 * @param string the name of the table to check for
518 * @return array [field_name] = comment
520 function PMA_getComments($db, $table = '')
522 $comments = array();
524 if ($table != '') {
525 // MySQL native column comments
526 $fields = PMA_DBI_get_fields($db, $table);
527 if ($fields) {
528 foreach ($fields as $key => $field) {
529 if (! empty($field['Comment'])) {
530 $comments[$field['Field']] = $field['Comment'];
534 } else {
535 $comments[] = PMA_getDbComment($db);
538 return $comments;
539 } // end of the 'PMA_getComments()' function
542 * Gets the comment for a db
544 * @access public
545 * @uses PMA_DBI_QUERY_STORE
546 * @uses PMA_DBI_num_rows()
547 * @uses PMA_DBI_fetch_assoc()
548 * @uses PMA_DBI_free_result()
549 * @uses PMA_getRelationsParam()
550 * @uses PMA_backquote()
551 * @uses PMA_sqlAddslashes()
552 * @uses PMA_query_as_controluser()
553 * @uses strlen()
554 * @param string the name of the db to check for
555 * @return string comment
557 function PMA_getDbComment($db)
559 $cfgRelation = PMA_getRelationsParam();
560 $comment = '';
562 if ($cfgRelation['commwork']) {
563 // pmadb internal db comment
564 $com_qry = "
565 SELECT `comment`
566 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
567 WHERE db_name = '" . PMA_sqlAddslashes($db) . "'
568 AND table_name = ''
569 AND column_name = '(db_comment)'";
570 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE);
572 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
573 $row = PMA_DBI_fetch_assoc($com_rs);
574 $comment = $row['comment'];
576 PMA_DBI_free_result($com_rs);
579 return $comment;
580 } // end of the 'PMA_getDbComment()' function
583 * Gets the comment for a db
585 * @access public
586 * @uses PMA_DBI_QUERY_STORE
587 * @uses PMA_DBI_num_rows()
588 * @uses PMA_DBI_fetch_assoc()
589 * @uses PMA_DBI_free_result()
590 * @uses PMA_getRelationsParam()
591 * @uses PMA_backquote()
592 * @uses PMA_sqlAddslashes()
593 * @uses PMA_query_as_controluser()
594 * @uses strlen()
595 * @param string the name of the db to check for
596 * @return string comment
598 function PMA_getDbComments()
600 $cfgRelation = PMA_getRelationsParam();
601 $comments = array();
603 if ($cfgRelation['commwork']) {
604 // pmadb internal db comment
605 $com_qry = "
606 SELECT `db_name`, `comment`
607 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
608 WHERE `column_name` = '(db_comment)'";
609 $com_rs = PMA_query_as_controluser($com_qry, true, PMA_DBI_QUERY_STORE);
611 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
612 while ($row = PMA_DBI_fetch_assoc($com_rs)) {
613 $comments[$row['db_name']] = $row['comment'];
616 PMA_DBI_free_result($com_rs);
619 return $comments;
620 } // end of the 'PMA_getDbComments()' function
623 * Set a database comment to a certain value.
625 * @uses PMA_getRelationsParam()
626 * @uses PMA_backquote()
627 * @uses PMA_sqlAddslashes()
628 * @uses PMA_query_as_controluser()
629 * @uses strlen()
630 * @access public
631 * @param string $db the name of the db
632 * @param string $comment the value of the column
633 * @return boolean true, if comment-query was made.
635 function PMA_setDbComment($db, $comment = '')
637 $cfgRelation = PMA_getRelationsParam();
639 if (! $cfgRelation['commwork']) {
640 return false;
643 if (strlen($comment)) {
644 $upd_query = "
645 INSERT INTO
646 " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
647 (`db_name`, `table_name`, `column_name`, `comment`)
648 VALUES (
649 '" . PMA_sqlAddslashes($db) . "',
651 '(db_comment)',
652 '" . PMA_sqlAddslashes($comment) . "')
653 ON DUPLICATE KEY UPDATE
654 `comment` = '" . PMA_sqlAddslashes($comment) . "'";
655 } else {
656 $upd_query = '
657 DELETE FROM
658 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
659 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
660 AND `table_name` = \'\'
661 AND `column_name` = \'(db_comment)\'';
664 if (isset($upd_query)){
665 return PMA_query_as_controluser($upd_query);
668 return false;
669 } // end of 'PMA_setDbComment()' function
672 * Set a SQL history entry
674 * @uses $_SESSION['sql_history']
675 * @uses $cfg['QueryHistoryDB']
676 * @uses $cfg['QueryHistoryMax']
677 * @uses PMA_getRelationsParam()
678 * @uses PMA_query_as_controluser()
679 * @uses PMA_backquote()
680 * @uses PMA_sqlAddslashes()
681 * @uses count()
682 * @uses md5()
683 * @uses array_shift()
684 * @param string $db the name of the db
685 * @param string $table the name of the table
686 * @param string $username the username
687 * @param string $sqlquery the sql query
688 * @access public
690 function PMA_setHistory($db, $table, $username, $sqlquery)
692 if (strlen($sqlquery) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
693 return;
696 $cfgRelation = PMA_getRelationsParam();
698 if (! isset($_SESSION['sql_history'])) {
699 $_SESSION['sql_history'] = array();
702 $key = md5($sqlquery . $db . $table);
704 if (isset($_SESSION['sql_history'][$key])) {
705 unset($_SESSION['sql_history'][$key]);
708 $_SESSION['sql_history'][$key] = array(
709 'db' => $db,
710 'table' => $table,
711 'sqlquery' => $sqlquery,
714 if (count($_SESSION['sql_history']) > $GLOBALS['cfg']['QueryHistoryMax']) {
715 // history should not exceed a maximum count
716 array_shift($_SESSION['sql_history']);
719 if (! $cfgRelation['historywork'] || ! $GLOBALS['cfg']['QueryHistoryDB']) {
720 return;
723 PMA_query_as_controluser('
724 INSERT INTO
725 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
726 (`username`,
727 `db`,
728 `table`,
729 `timevalue`,
730 `sqlquery`)
731 VALUES
732 (\'' . PMA_sqlAddslashes($username) . '\',
733 \'' . PMA_sqlAddslashes($db) . '\',
734 \'' . PMA_sqlAddslashes($table) . '\',
735 NOW(),
736 \'' . PMA_sqlAddslashes($sqlquery) . '\')');
737 } // end of 'PMA_setHistory()' function
740 * Gets a SQL history entry
742 * @uses $_SESSION['sql_history']
743 * @uses $GLOBALS['controllink']
744 * @uses PMA_getRelationsParam()
745 * @uses PMA_backquote()
746 * @uses PMA_sqlAddslashes()
747 * @uses PMA_DBI_fetch_result()
748 * @uses array_reverse()
749 * @param string $username the username
750 * @return array list of history items
751 * @access public
753 function PMA_getHistory($username)
755 $cfgRelation = PMA_getRelationsParam();
757 if (isset($_SESSION['sql_history'])) {
758 return array_reverse($_SESSION['sql_history']);
761 if (! $cfgRelation['historywork']) {
762 return false;
765 $hist_query = '
766 SELECT `db`,
767 `table`,
768 `sqlquery`
769 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
770 WHERE `username` = \'' . PMA_sqlAddslashes($username) . '\'
771 ORDER BY `id` DESC';
773 return PMA_DBI_fetch_result($hist_query, null, null, $GLOBALS['controllink']);
774 } // end of 'PMA_getHistory()' function
777 * purges SQL history
779 * deletes entries that exceeds $cfg['QueryHistoryMax'], oldest first, for the
780 * given user
782 * @uses $cfg['QueryHistoryMax']
783 * @uses $cfg['QueryHistoryDB']
784 * @uses $GLOBALS['controllink']
785 * @uses PMA_backquote()
786 * @uses PMA_sqlAddSlashes()
787 * @uses PMA_query_as_controluser()
788 * @uses PMA_DBI_fetch_value()
789 * @param string $username the username
790 * @access public
792 function PMA_purgeHistory($username)
794 $cfgRelation = PMA_getRelationsParam();
795 if (! $GLOBALS['cfg']['QueryHistoryDB'] || ! $cfgRelation['historywork']) {
796 return;
799 if (! $cfgRelation['historywork']) {
800 return;
803 $search_query = '
804 SELECT `timevalue`
805 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
806 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
807 ORDER BY `timevalue` DESC
808 LIMIT ' . $GLOBALS['cfg']['QueryHistoryMax'] . ', 1';
810 if ($max_time = PMA_DBI_fetch_value($search_query, 0, 0, $GLOBALS['controllink'])) {
811 PMA_query_as_controluser('
812 DELETE FROM
813 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
814 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
815 AND `timevalue` <= \'' . $max_time . '\'');
817 } // end of 'PMA_purgeHistory()' function
820 * Prepares the dropdown for one mode
822 * @uses $cfg['LimitChars']
823 * @uses $cfg['NaturalOrder']
824 * @uses PMA_strlen()
825 * @uses htmlspecialchars()
826 * @uses substr()
827 * @uses uksort()
828 * @uses ksort()
829 * @uses natcasesort()
830 * @uses asort()
831 * @param array $foreign the keys and values for foreigns
832 * @param string $data the current data of the dropdown
833 * @param string $mode the needed mode
835 * @return array the <option value=""><option>s
837 * @access protected
839 function PMA__foreignDropdownBuild($foreign, $data, $mode)
841 $reloptions = array();
843 if ($mode == 'id-content') {
844 // sort for id-content
845 if ($GLOBALS['cfg']['NaturalOrder']) {
846 uksort($foreign, 'strnatcasecmp');
847 } else {
848 ksort($foreign);
850 } elseif ($mode == 'content-id') {
851 // sort for content-id
852 if ($GLOBALS['cfg']['NaturalOrder']) {
853 natcasesort($foreign);
854 } else {
855 asort($foreign);
859 foreach ($foreign as $key => $value) {
861 if (PMA_strlen($value) <= $GLOBALS['cfg']['LimitChars']) {
862 $vtitle = '';
863 $value = htmlspecialchars($value);
864 } else {
865 $vtitle = htmlspecialchars($value);
866 $value = htmlspecialchars(substr($value, 0, $GLOBALS['cfg']['LimitChars']) . '...');
869 $reloption = ' <option value="' . htmlspecialchars($key) . '"';
870 if ($vtitle != '') {
871 $reloption .= ' title="' . $vtitle . '"';
874 if ((string) $key == (string) $data) {
875 $reloption .= ' selected="selected"';
878 if ($mode == 'content-id') {
879 $reloptions[] = $reloption . '>' . $value . '&nbsp;-&nbsp;' . htmlspecialchars($key) . '</option>' . "\n";
880 } else {
881 $reloptions[] = $reloption . '>' . htmlspecialchars($key) . '&nbsp;-&nbsp;' . $value . '</option>' . "\n";
883 } // end foreach
885 return $reloptions;
886 } // end of 'PMA__foreignDropdownBuild' function
889 * Outputs dropdown with values of foreign fields
891 * @uses $cfg['ForeignKeyMaxLimit']
892 * @uses $cfg['ForeignKeyDropdownOrder']
893 * @uses PMA__foreignDropdownBuild()
894 * @uses PMA_isValid()
895 * @uses implode()
896 * @param array array of the displayed row
897 * @param string the foreign field
898 * @param string the foreign field to display
899 * @param string the current data of the dropdown (field in row)
900 * @return string the <option value=""><option>s
901 * @access public
903 function PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, $data,
904 $max = null)
906 if (null === $max) {
907 $max = $GLOBALS['cfg']['ForeignKeyMaxLimit'];
910 $foreign = array();
912 // collect the data
913 foreach ($disp_row as $relrow) {
914 $key = $relrow[$foreign_field];
916 // if the display field has been defined for this foreign table
917 if ($foreign_display) {
918 $value = $relrow[$foreign_display];
919 } else {
920 $value = '';
921 } // end if ($foreign_display)
923 $foreign[$key] = $value;
924 } // end foreach
926 // put the dropdown sections in correct order
927 $top = array();
928 $bot = array();
929 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'], 'array')) {
930 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][0])) {
931 $top = PMA__foreignDropdownBuild($foreign, $data,
932 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][0]);
934 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][1])) {
935 $bot = PMA__foreignDropdownBuild($foreign, $data,
936 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][1]);
938 } else {
939 $top = PMA__foreignDropdownBuild($foreign, $data, 'id-content');
940 $bot = PMA__foreignDropdownBuild($foreign, $data, 'content-id');
943 // beginning of dropdown
944 $ret = '<option value="">&nbsp;</option>' . "\n";
946 $top_count = count($top);
947 if ($max == -1 || $top_count < $max) {
948 $ret .= implode('', $top);
949 if ($top_count > 0) {
950 $ret .= ' <option value="">&nbsp;</option>' . "\n";
951 $ret .= ' <option value="">&nbsp;</option>' . "\n";
954 $ret .= implode('', $bot);
956 return $ret;
957 } // end of 'PMA_foreignDropdown()' function
960 * Gets foreign keys in preparation for a drop-down selector
962 * @uses PMA_Table::countRecords()
963 * @uses PMA_backquote()
964 * @uses PMA_getDisplayField()
965 * @uses PMA_sqlAddslashes()
966 * @uses PMA_DBI_fetch_value()
967 * @uses PMA_DBI_free_result()
968 * @uses PMA_DBI_query()
969 * @uses PMA_DBI_num_rows()
970 * @uses PMA_DBI_fetch_assoc()
971 * @param array array of the foreign keys
972 * @param string the foreign field name
973 * @param bool whether to override the total
974 * @param string a possible filter
975 * @param string a possible LIMIT clause
976 * @return array data about the foreign keys
977 * @access public
980 function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filter, $foreign_limit)
982 // we always show the foreign field in the drop-down; if a display
983 // field is defined, we show it besides the foreign field
984 $foreign_link = false;
985 if ($foreigners && isset($foreigners[$field])) {
986 $foreigner = $foreigners[$field];
987 $foreign_db = $foreigner['foreign_db'];
988 $foreign_table = $foreigner['foreign_table'];
989 $foreign_field = $foreigner['foreign_field'];
991 // Count number of rows in the foreign table. Currently we do
992 // not use a drop-down if more than 200 rows in the foreign table,
993 // for speed reasons and because we need a better interface for this.
995 // We could also do the SELECT anyway, with a LIMIT, and ensure that
996 // the current value of the field is one of the choices.
998 $the_total = PMA_Table::countRecords($foreign_db, $foreign_table);
1000 if ($override_total == true || $the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']) {
1001 // foreign_display can be FALSE if no display field defined:
1002 $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
1004 $f_query_main = 'SELECT ' . PMA_backquote($foreign_field)
1005 . (($foreign_display == FALSE) ? '' : ', ' . PMA_backquote($foreign_display));
1006 $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table);
1007 $f_query_filter = empty($foreign_filter) ? '' : ' WHERE ' . PMA_backquote($foreign_field)
1008 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1009 . (($foreign_display == FALSE) ? '' : ' OR ' . PMA_backquote($foreign_display)
1010 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1012 $f_query_order = ($foreign_display == FALSE) ? '' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display);
1013 $f_query_limit = isset($foreign_limit) ? $foreign_limit : '';
1015 if (!empty($foreign_filter)) {
1016 $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter);
1017 if ($res) {
1018 $the_total = PMA_DBI_fetch_value($res);
1019 @PMA_DBI_free_result($res);
1020 } else {
1021 $the_total = 0;
1025 $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit);
1026 if ($disp && PMA_DBI_num_rows($disp) > 0) {
1027 // If a resultset has been created, pre-cache it in the $disp_row array
1028 // This helps us from not needing to use mysql_data_seek by accessing a pre-cached
1029 // PHP array. Usually those resultsets are not that big, so a performance hit should
1030 // not be expected.
1031 $disp_row = array();
1032 while ($single_disp_row = @PMA_DBI_fetch_assoc($disp)) {
1033 $disp_row[] = $single_disp_row;
1035 @PMA_DBI_free_result($disp);
1037 } else {
1038 $disp_row = null;
1039 $foreign_link = true;
1041 } // end if $foreigners
1043 $foreignData['foreign_link'] = $foreign_link;
1044 $foreignData['the_total'] = isset($the_total) ? $the_total : null;
1045 $foreignData['foreign_display'] = isset($foreign_display) ? $foreign_display : null;
1046 $foreignData['disp_row'] = isset($disp_row) ? $disp_row : null;
1047 $foreignData['foreign_field'] = isset($foreign_field) ? $foreign_field : null;
1048 return $foreignData;
1049 } // end of 'PMA_getForeignData()' function
1052 * Finds all related tables
1054 * @uses $GLOBALS['controllink']
1055 * @uses $GLOBALS['cfgRelation']
1056 * @uses $GLOBALS['db']
1057 * @param string whether to go from master to foreign or vice versa
1058 * @return boolean always TRUE
1059 * @global array $tab_left the list of tables that we still couldn't connect
1060 * @global array $tab_know the list of allready connected tables
1061 * @global string $fromclause
1063 * @access private
1065 function PMA_getRelatives($from)
1067 global $tab_left, $tab_know, $fromclause;
1069 if ($from == 'master') {
1070 $to = 'foreign';
1071 } else {
1072 $to = 'master';
1074 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
1075 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
1077 $rel_query = 'SELECT *'
1078 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db'])
1079 . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
1080 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1081 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1082 . ' AND ' . $from . '_table IN ' . $in_know
1083 . ' AND ' . $to . '_table IN ' . $in_left;
1084 $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']);
1085 while ($row = PMA_DBI_fetch_assoc($relations)) {
1086 $found_table = $row[$to . '_table'];
1087 if (isset($tab_left[$found_table])) {
1088 $fromclause
1089 .= "\n" . ' LEFT JOIN '
1090 . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($row[$to . '_table']) . ' ON '
1091 . PMA_backquote($row[$from . '_table']) . '.'
1092 . PMA_backquote($row[$from . '_field']) . ' = '
1093 . PMA_backquote($row[$to . '_table']) . '.'
1094 . PMA_backquote($row[$to . '_field']) . ' ';
1095 $tab_know[$found_table] = $found_table;
1096 unset($tab_left[$found_table]);
1098 } // end while
1100 return true;
1101 } // end of the "PMA_getRelatives()" function
1104 * Rename a field in relation tables
1106 * usually called after a field in a table was renamed in tbl_alter.php
1108 * @uses PMA_getRelationsParam()
1109 * @uses PMA_backquote()
1110 * @uses PMA_sqlAddslashes()
1111 * @uses PMA_query_as_controluser()
1112 * @param string $db
1113 * @param string $table
1114 * @param string $field
1115 * @param string $new_name
1117 function PMA_REL_renameField($db, $table, $field, $new_name)
1119 $cfgRelation = PMA_getRelationsParam();
1121 if ($cfgRelation['displaywork']) {
1122 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
1123 . ' SET display_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1124 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1125 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
1126 . ' AND display_field = \'' . PMA_sqlAddslashes($field) . '\'';
1127 PMA_query_as_controluser($table_query);
1130 if ($cfgRelation['relwork']) {
1131 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1132 . ' SET master_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1133 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
1134 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
1135 . ' AND master_field = \'' . PMA_sqlAddslashes($field) . '\'';
1136 PMA_query_as_controluser($table_query);
1138 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1139 . ' SET foreign_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1140 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
1141 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
1142 . ' AND foreign_field = \'' . PMA_sqlAddslashes($field) . '\'';
1143 PMA_query_as_controluser($table_query);
1144 } // end if relwork
1148 * Create a PDF page
1150 * @uses PMA_backquote()
1151 * @uses $GLOBALS['cfgRelation']['db']
1152 * @uses PMA_sqlAddslashes()
1153 * @uses PMA_query_as_controluser()
1154 * @uses PMA_DBI_insert_id()
1155 * @uses $GLOBALS['controllink']
1156 * @param string $newpage
1157 * @param array $cfgRelation
1158 * @param string $db
1159 * @param string $query_default_option
1160 * @return string $pdf_page_number
1162 function PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option) {
1163 if (! isset($newpage) || $newpage == '') {
1164 $newpage = __('no description');
1166 $ins_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1167 . ' (db_name, page_descr)'
1168 . ' VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($newpage) . '\')';
1169 PMA_query_as_controluser($ins_query, FALSE, $query_default_option);
1170 return PMA_DBI_insert_id(isset($GLOBALS['controllink']) ? $GLOBALS['controllink'] : '');