Translation update done using Pootle.
[phpmyadmin/ammar_yasir.git] / libraries / relation.lib.php
blob960e8905a437ebfb1b33512e999ed3c9b114951c
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 // id-only is a special mode used when no foreign display column
844 // is available
845 if ($mode == 'id-content' || $mode == 'id-only') {
846 // sort for id-content
847 if ($GLOBALS['cfg']['NaturalOrder']) {
848 uksort($foreign, 'strnatcasecmp');
849 } else {
850 ksort($foreign);
852 } elseif ($mode == 'content-id') {
853 // sort for content-id
854 if ($GLOBALS['cfg']['NaturalOrder']) {
855 natcasesort($foreign);
856 } else {
857 asort($foreign);
861 foreach ($foreign as $key => $value) {
862 if (PMA_strlen($value) <= $GLOBALS['cfg']['LimitChars']) {
863 $vtitle = '';
864 $value = htmlspecialchars($value);
865 } else {
866 $vtitle = htmlspecialchars($value);
867 $value = htmlspecialchars(substr($value, 0, $GLOBALS['cfg']['LimitChars']) . '...');
870 $reloption = '<option value="' . htmlspecialchars($key) . '"';
871 if ($vtitle != '') {
872 $reloption .= ' title="' . $vtitle . '"';
875 if ((string) $key == (string) $data) {
876 $reloption .= ' selected="selected"';
879 if ($mode == 'content-id') {
880 $reloptions[] = $reloption . '>' . $value . '&nbsp;-&nbsp;' . htmlspecialchars($key) . '</option>';
881 } elseif ($mode == 'id-content') {
882 $reloptions[] = $reloption . '>' . htmlspecialchars($key) . '&nbsp;-&nbsp;' . $value . '</option>';
883 } elseif ($mode == 'id-only') {
884 $reloptions[] = $reloption . '>' . htmlspecialchars($key) . '</option>';
886 } // end foreach
888 return $reloptions;
889 } // end of 'PMA__foreignDropdownBuild' function
892 * Outputs dropdown with values of foreign fields
894 * @uses $cfg['ForeignKeyMaxLimit']
895 * @uses $cfg['ForeignKeyDropdownOrder']
896 * @uses PMA__foreignDropdownBuild()
897 * @uses PMA_isValid()
898 * @uses implode()
899 * @param array array of the displayed row
900 * @param string the foreign field
901 * @param string the foreign field to display
902 * @param string the current data of the dropdown (field in row)
903 * @return string the <option value=""><option>s
904 * @access public
906 function PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, $data,
907 $max = null)
909 if (null === $max) {
910 $max = $GLOBALS['cfg']['ForeignKeyMaxLimit'];
913 $foreign = array();
915 // collect the data
916 foreach ($disp_row as $relrow) {
917 $key = $relrow[$foreign_field];
919 // if the display field has been defined for this foreign table
920 if ($foreign_display) {
921 $value = $relrow[$foreign_display];
922 } else {
923 $value = '';
924 } // end if ($foreign_display)
926 $foreign[$key] = $value;
927 } // end foreach
929 // put the dropdown sections in correct order
930 $top = array();
931 $bottom = array();
932 if ($foreign_display) {
933 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'], 'array')) {
934 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][0])) {
935 $top = PMA__foreignDropdownBuild($foreign, $data,
936 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][0]);
938 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][1])) {
939 $bottom = PMA__foreignDropdownBuild($foreign, $data,
940 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][1]);
942 } else {
943 $top = PMA__foreignDropdownBuild($foreign, $data, 'id-content');
944 $bottom = PMA__foreignDropdownBuild($foreign, $data, 'content-id');
946 } else {
947 $top = PMA__foreignDropdownBuild($foreign, $data, 'id-only');
950 // beginning of dropdown
951 $ret = '<option value="">&nbsp;</option>';
952 $top_count = count($top);
953 if ($max == -1 || $top_count < $max) {
954 $ret .= implode('', $top);
955 if ($foreign_display && $top_count > 0) {
956 // this empty option is to visually mark the beginning of the
957 // second series of values (bottom)
958 $ret .= '<option value="">&nbsp;</option>';
961 if ($foreign_display) {
962 $ret .= implode('', $bottom);
965 return $ret;
966 } // end of 'PMA_foreignDropdown()' function
969 * Gets foreign keys in preparation for a drop-down selector
971 * @uses PMA_Table::countRecords()
972 * @uses PMA_backquote()
973 * @uses PMA_getDisplayField()
974 * @uses PMA_sqlAddslashes()
975 * @uses PMA_DBI_fetch_value()
976 * @uses PMA_DBI_free_result()
977 * @uses PMA_DBI_query()
978 * @uses PMA_DBI_num_rows()
979 * @uses PMA_DBI_fetch_assoc()
980 * @param array array of the foreign keys
981 * @param string the foreign field name
982 * @param bool whether to override the total
983 * @param string a possible filter
984 * @param string a possible LIMIT clause
985 * @return array data about the foreign keys
986 * @access public
989 function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filter, $foreign_limit)
991 // we always show the foreign field in the drop-down; if a display
992 // field is defined, we show it besides the foreign field
993 $foreign_link = false;
994 if ($foreigners && isset($foreigners[$field])) {
995 $foreigner = $foreigners[$field];
996 $foreign_db = $foreigner['foreign_db'];
997 $foreign_table = $foreigner['foreign_table'];
998 $foreign_field = $foreigner['foreign_field'];
1000 // Count number of rows in the foreign table. Currently we do
1001 // not use a drop-down if more than 200 rows in the foreign table,
1002 // for speed reasons and because we need a better interface for this.
1004 // We could also do the SELECT anyway, with a LIMIT, and ensure that
1005 // the current value of the field is one of the choices.
1007 $the_total = PMA_Table::countRecords($foreign_db, $foreign_table);
1009 if ($override_total == true || $the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']) {
1010 // foreign_display can be FALSE if no display field defined:
1011 $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
1013 $f_query_main = 'SELECT ' . PMA_backquote($foreign_field)
1014 . (($foreign_display == FALSE) ? '' : ', ' . PMA_backquote($foreign_display));
1015 $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table);
1016 $f_query_filter = empty($foreign_filter) ? '' : ' WHERE ' . PMA_backquote($foreign_field)
1017 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1018 . (($foreign_display == FALSE) ? '' : ' OR ' . PMA_backquote($foreign_display)
1019 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1021 $f_query_order = ($foreign_display == FALSE) ? '' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display);
1022 $f_query_limit = isset($foreign_limit) ? $foreign_limit : '';
1024 if (!empty($foreign_filter)) {
1025 $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter);
1026 if ($res) {
1027 $the_total = PMA_DBI_fetch_value($res);
1028 @PMA_DBI_free_result($res);
1029 } else {
1030 $the_total = 0;
1034 $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit);
1035 if ($disp && PMA_DBI_num_rows($disp) > 0) {
1036 // If a resultset has been created, pre-cache it in the $disp_row array
1037 // This helps us from not needing to use mysql_data_seek by accessing a pre-cached
1038 // PHP array. Usually those resultsets are not that big, so a performance hit should
1039 // not be expected.
1040 $disp_row = array();
1041 while ($single_disp_row = @PMA_DBI_fetch_assoc($disp)) {
1042 $disp_row[] = $single_disp_row;
1044 @PMA_DBI_free_result($disp);
1046 } else {
1047 $disp_row = null;
1048 $foreign_link = true;
1050 } // end if $foreigners
1052 $foreignData['foreign_link'] = $foreign_link;
1053 $foreignData['the_total'] = isset($the_total) ? $the_total : null;
1054 $foreignData['foreign_display'] = isset($foreign_display) ? $foreign_display : null;
1055 $foreignData['disp_row'] = isset($disp_row) ? $disp_row : null;
1056 $foreignData['foreign_field'] = isset($foreign_field) ? $foreign_field : null;
1057 return $foreignData;
1058 } // end of 'PMA_getForeignData()' function
1061 * Finds all related tables
1063 * @uses $GLOBALS['controllink']
1064 * @uses $GLOBALS['cfgRelation']
1065 * @uses $GLOBALS['db']
1066 * @param string whether to go from master to foreign or vice versa
1067 * @return boolean always TRUE
1068 * @global array $tab_left the list of tables that we still couldn't connect
1069 * @global array $tab_know the list of allready connected tables
1070 * @global string $fromclause
1072 * @access private
1074 function PMA_getRelatives($from)
1076 global $tab_left, $tab_know, $fromclause;
1078 if ($from == 'master') {
1079 $to = 'foreign';
1080 } else {
1081 $to = 'master';
1083 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
1084 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
1086 $rel_query = 'SELECT *'
1087 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db'])
1088 . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
1089 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1090 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1091 . ' AND ' . $from . '_table IN ' . $in_know
1092 . ' AND ' . $to . '_table IN ' . $in_left;
1093 $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']);
1094 while ($row = PMA_DBI_fetch_assoc($relations)) {
1095 $found_table = $row[$to . '_table'];
1096 if (isset($tab_left[$found_table])) {
1097 $fromclause
1098 .= "\n" . ' LEFT JOIN '
1099 . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($row[$to . '_table']) . ' ON '
1100 . PMA_backquote($row[$from . '_table']) . '.'
1101 . PMA_backquote($row[$from . '_field']) . ' = '
1102 . PMA_backquote($row[$to . '_table']) . '.'
1103 . PMA_backquote($row[$to . '_field']) . ' ';
1104 $tab_know[$found_table] = $found_table;
1105 unset($tab_left[$found_table]);
1107 } // end while
1109 return true;
1110 } // end of the "PMA_getRelatives()" function
1113 * Rename a field in relation tables
1115 * usually called after a field in a table was renamed in tbl_alter.php
1117 * @uses PMA_getRelationsParam()
1118 * @uses PMA_backquote()
1119 * @uses PMA_sqlAddslashes()
1120 * @uses PMA_query_as_controluser()
1121 * @param string $db
1122 * @param string $table
1123 * @param string $field
1124 * @param string $new_name
1126 function PMA_REL_renameField($db, $table, $field, $new_name)
1128 $cfgRelation = PMA_getRelationsParam();
1130 if ($cfgRelation['displaywork']) {
1131 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
1132 . ' SET display_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1133 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1134 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
1135 . ' AND display_field = \'' . PMA_sqlAddslashes($field) . '\'';
1136 PMA_query_as_controluser($table_query);
1139 if ($cfgRelation['relwork']) {
1140 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1141 . ' SET master_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1142 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
1143 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
1144 . ' AND master_field = \'' . PMA_sqlAddslashes($field) . '\'';
1145 PMA_query_as_controluser($table_query);
1147 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1148 . ' SET foreign_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1149 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
1150 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
1151 . ' AND foreign_field = \'' . PMA_sqlAddslashes($field) . '\'';
1152 PMA_query_as_controluser($table_query);
1153 } // end if relwork
1157 * Create a PDF page
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'] : '');