Dead code.
[phpmyadmin/last10db.git] / libraries / relation.lib.php
blob9b31b91b95a9495251b3213d666868c7cda96bae
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 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
15 require_once './libraries/Table.class.php';
17 /**
18 * Executes a query as controluser if possible, otherwise as normal user
20 * @param string the query to execute
21 * @param boolean whether to display SQL error messages or not
23 * @return integer the result set, or false if no result set
25 * @access public
27 * @author Mike Beck <mikebeck@users.sourceforge.net>
29 function PMA_query_as_cu($sql, $show_error = true, $options = 0)
31 if ($show_error) {
32 $result = PMA_DBI_query($sql, $GLOBALS['controllink'], $options);
33 } else {
34 $result = @PMA_DBI_try_query($sql, $GLOBALS['controllink'], $options);
35 } // end if... else...
37 if ($result) {
38 return $result;
39 } else {
40 return false;
42 } // end of the "PMA_query_as_cu()" function
44 /**
45 * @uses $_SESSION['relation'] for caching
46 * @uses $GLOBALS['cfgRelation'] to set it
47 * @uses PMA__getRelationsParam()
48 * @uses PMA_printRelationsParamDiagnostic()
49 * @param bool $verbose whether to print diagnostic info
50 * @return array $cfgRelation
52 function PMA_getRelationsParam($verbose = false)
54 if (empty($_SESSION['relation'])) {
55 $_SESSION['relation'] = PMA__getRelationsParam();
58 if ($verbose) {
59 PMA_printRelationsParamDiagnostic($_SESSION['relation']);
62 // just for BC
63 $GLOBALS['cfgRelation'] = $_SESSION['relation'];
65 return $_SESSION['relation'];
68 /**
69 * prints out diagnostic info for pma relation feature
71 * @uses $GLOBALS['server']
72 * @uses $GLOBALS['controllink']
73 * @uses $GLOBALS['strNotOK']
74 * @uses $GLOBALS['strDocu']
75 * @uses $GLOBALS['strGeneralRelationFeat']
76 * @uses $GLOBALS['strDisabled']
77 * @uses $GLOBALS['strEnabled']
78 * @uses $GLOBALS['strDisplayFeat']
79 * @uses $GLOBALS['strCreatePdfFeat']
80 * @uses $GLOBALS['strColComFeat']
81 * @uses $GLOBALS['strBookmarkQuery']
82 * @uses $GLOBALS['strUpdComTab']
83 * @uses $GLOBALS['strQuerySQLHistory']
84 * @uses $GLOBALS['strDesigner']
85 * @uses $cfg['Server']['pmadb']
86 * @uses sprintf()
87 * @uses PMA_printDiagMessageForFeature()
88 * @uses PMA_printDiagMessageForParameter()
89 * @param array $cfgRelation
91 function PMA_printRelationsParamDiagnostic($cfgRelation)
93 $messages['error'] = '<font color="red"><strong>' . $GLOBALS['strNotOK']
94 . '</strong></font> [ <a href="Documentation.html#%s" target="documentation">'
95 . $GLOBALS['strDocu'] . '</a> ]';
97 $messages['ok'] = '<font color="green"><strong>' . $GLOBALS['strOK'] . '</strong></font>';
98 $messages['enabled'] = '<font color="green">' . $GLOBALS['strEnabled'] . '</font>';
99 $messages['disabled'] = '<font color="red">' . $GLOBALS['strDisabled'] . '</font>';
101 if (false === $GLOBALS['cfg']['Server']['pmadb']) {
102 echo 'PMA Database ... '
103 . sprintf($messages['error'], 'pmadb')
104 . '<br />' . "\n"
105 . $GLOBALS['strGeneralRelationFeat']
106 . ' <font color="green">' . $GLOBALS['strDisabled']
107 . '</font>' . "\n";
108 return;
111 echo '<table>' . "\n";
113 PMA_printDiagMessageForParameter('pmadb', $GLOBALS['cfg']['Server']['pmadb'], $messages, 'pmadb');
115 PMA_printDiagMessageForParameter('relation', isset($cfgRelation['relation']), $messages, 'relation');
117 PMA_printDiagMessageForFeature('strGeneralRelationFeat', 'relwork', $messages);
119 PMA_printDiagMessageForParameter('table_info', isset($cfgRelation['displaywork']), $messages, 'table_info');
121 PMA_printDiagMessageForFeature('strDisplayFeat', 'displaywork', $messages);
123 PMA_printDiagMessageForParameter('table_coords', isset($cfgRelation['table_coords']), $messages, 'table_coords');
125 PMA_printDiagMessageForParameter('pdf_pages', isset($cfgRelation['pdf_pages']), $messages, 'table_coords');
127 PMA_printDiagMessageForFeature('strCreatePdfFeat', 'pdfwork', $messages);
129 PMA_printDiagMessageForParameter('column_info', isset($cfgRelation['column_info']), $messages, 'col_com');
131 PMA_printDiagMessageForFeature('strColComFeat', 'commwork', $messages, false);
133 PMA_printDiagMessageForFeature('strBookmarkQuery', 'bookmarkwork', $messages, false);
135 PMA_printDiagMessageForFeature('strMIME_transformation', 'mimework', $messages);
137 if ($cfgRelation['commwork'] && ! $cfgRelation['mimework']) {
138 echo '<tr><td colspan=2 align="left">' . $GLOBALS['strUpdComTab'] . '</td></tr>' . "\n";
141 PMA_printDiagMessageForParameter('history', isset($cfgRelation['history']), $messages, 'history');
143 PMA_printDiagMessageForFeature('strQuerySQLHistory', 'historywork', $messages);
145 PMA_printDiagMessageForParameter('designer_coords', isset($cfgRelation['designer_coords']), $messages, 'designer_coords');
147 PMA_printDiagMessageForFeature('strDesigner', 'designerwork', $messages);
149 echo '</table>' . "\n";
153 * prints out one diagnostic message for a feature
155 * @param string feature name in a message string
156 * @param string the $GLOBALS['cfgRelation'] parameter to check
157 * @param array utility messages
158 * @param boolean whether to skip a line after the message
160 function PMA_printDiagMessageForFeature($feature_name, $relation_parameter, $messages, $skip_line=true)
162 echo ' <tr><td colspan=2 align="right">' . $GLOBALS[$feature_name] . ': '
163 . ($GLOBALS['cfgRelation'][$relation_parameter] ? $messages['enabled'] : $messages['disabled'])
164 . '</td></tr>' . "\n";
165 if ($skip_line) {
166 echo ' <tr><td>&nbsp;</td></tr>' . "\n";
171 * prints out one diagnostic message for a configuration parameter
173 * @param string config parameter name to display
174 * @param boolean whether this parameter is set
175 * @param array utility messages
176 * @param string anchor in Documentation.html
178 function PMA_printDiagMessageForParameter($parameter, $relation_parameter_set, $messages, $doc_anchor)
180 echo ' <tr><th align="left">';
181 echo '$cfg[\'Servers\'][$i][\'' . $parameter . '\'] ... </th><td align="right">';
182 echo ($relation_parameter_set ? $messages['ok'] : sprintf($messages['error'], $doc_anchor)) . '</td></tr>' . "\n";
187 * Defines the relation parameters for the current user
188 * just a copy of the functions used for relations ;-)
189 * but added some stuff to check what will work
191 * @uses $cfg['Server']['user']
192 * @uses $cfg['Server']['pmadb']
193 * @uses $cfg['Server']['verbose_check']
194 * @uses $GLOBALS['server']
195 * @uses $GLOBALS['controllink']
196 * @uses PMA_DBI_QUERY_STORE
197 * @uses PMA_DBI_select_db()
198 * @uses PMA_backquote()
199 * @uses PMA_query_as_cu()
200 * @uses PMA_DBI_fetch_row()
201 * @uses PMA_DBI_free_result()
202 * @access protected
203 * @author Mike Beck <mikebeck@users.sourceforge.net>
204 * @return array the relation parameters for the current user
206 function PMA__getRelationsParam()
208 $cfgRelation = array();
209 $cfgRelation['relwork'] = false;
210 $cfgRelation['displaywork'] = false;
211 $cfgRelation['bookmarkwork']= false;
212 $cfgRelation['pdfwork'] = false;
213 $cfgRelation['commwork'] = false;
214 $cfgRelation['mimework'] = false;
215 $cfgRelation['historywork'] = false;
216 $cfgRelation['designerwork'] = false;
217 $cfgRelation['allworks'] = false;
218 $cfgRelation['user'] = null;
219 $cfgRelation['db'] = null;
221 if ($GLOBALS['server'] == 0 || empty($GLOBALS['cfg']['Server']['pmadb'])
222 || ! PMA_DBI_select_db($GLOBALS['cfg']['Server']['pmadb'], $GLOBALS['controllink'])) {
223 // No server selected -> no bookmark table
224 // we return the array with the falses in it,
225 // to avoid some 'Unitialized string offset' errors later
226 $GLOBALS['cfg']['Server']['pmadb'] = false;
227 return $cfgRelation;
231 $cfgRelation['user'] = $GLOBALS['cfg']['Server']['user'];
232 $cfgRelation['db'] = $GLOBALS['cfg']['Server']['pmadb'];
234 // Now I just check if all tables that i need are present so I can for
235 // example enable relations but not pdf...
236 // I was thinking of checking if they have all required columns but I
237 // fear it might be too slow
239 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($GLOBALS['cfg']['Server']['pmadb']);
240 $tab_rs = PMA_query_as_cu($tab_query, false, PMA_DBI_QUERY_STORE);
242 if (! $tab_rs) {
243 // query failed ... ?
244 //$GLOBALS['cfg']['Server']['pmadb'] = false;
245 return $cfgRelation;
248 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
249 if ($curr_table[0] == $GLOBALS['cfg']['Server']['bookmarktable']) {
250 $cfgRelation['bookmark'] = $curr_table[0];
251 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['relation']) {
252 $cfgRelation['relation'] = $curr_table[0];
253 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_info']) {
254 $cfgRelation['table_info'] = $curr_table[0];
255 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['table_coords']) {
256 $cfgRelation['table_coords'] = $curr_table[0];
257 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['designer_coords']) {
258 $cfgRelation['designer_coords'] = $curr_table[0];
259 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['column_info']) {
260 $cfgRelation['column_info'] = $curr_table[0];
261 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['pdf_pages']) {
262 $cfgRelation['pdf_pages'] = $curr_table[0];
263 } elseif ($curr_table[0] == $GLOBALS['cfg']['Server']['history']) {
264 $cfgRelation['history'] = $curr_table[0];
266 } // end while
267 PMA_DBI_free_result($tab_rs);
269 if (isset($cfgRelation['relation'])) {
270 $cfgRelation['relwork'] = true;
271 if (isset($cfgRelation['table_info'])) {
272 $cfgRelation['displaywork'] = true;
275 if (isset($cfgRelation['table_coords']) && isset($cfgRelation['pdf_pages'])) {
276 $cfgRelation['pdfwork'] = true;
278 if (isset($cfgRelation['column_info'])) {
279 $cfgRelation['commwork'] = true;
281 if ($GLOBALS['cfg']['Server']['verbose_check']) {
282 $mime_query = 'SHOW FIELDS FROM '
283 . PMA_backquote($cfgRelation['db']) . '.'
284 . PMA_backquote($cfgRelation['column_info']);
285 $mime_rs = PMA_query_as_cu($mime_query, false);
287 $mime_field_mimetype = false;
288 $mime_field_transformation = false;
289 $mime_field_transformation_options = false;
290 while ($curr_mime_field = @PMA_DBI_fetch_row($mime_rs)) {
291 if ($curr_mime_field[0] == 'mimetype') {
292 $mime_field_mimetype = true;
293 } elseif ($curr_mime_field[0] == 'transformation') {
294 $mime_field_transformation = true;
295 } elseif ($curr_mime_field[0] == 'transformation_options') {
296 $mime_field_transformation_options = true;
299 PMA_DBI_free_result($mime_rs);
301 if ($mime_field_mimetype
302 && $mime_field_transformation
303 && $mime_field_transformation_options) {
304 $cfgRelation['mimework'] = true;
306 } else {
307 $cfgRelation['mimework'] = true;
311 if (isset($cfgRelation['history'])) {
312 $cfgRelation['historywork'] = true;
315 // we do not absolutely need that the internal relations or the PDF
316 // schema feature be activated
317 if (isset($cfgRelation['designer_coords'])) {
318 $cfgRelation['designerwork'] = true;
321 if (isset($cfgRelation['bookmark'])) {
322 $cfgRelation['bookmarkwork'] = true;
325 if ($cfgRelation['relwork'] && $cfgRelation['displaywork']
326 && $cfgRelation['pdfwork'] && $cfgRelation['commwork']
327 && $cfgRelation['mimework'] && $cfgRelation['historywork']
328 && $cfgRelation['bookmarkwork'] && $cfgRelation['designerwork']) {
329 $cfgRelation['allworks'] = true;
332 return $cfgRelation;
333 } // end of the 'PMA_getRelationsParam()' function
336 * Gets all Relations to foreign tables for a given table or
337 * optionally a given column in a table
339 * @author Mike Beck <mikebeck@users.sourceforge.net>
340 * @author Marc Delisle
341 * @access public
342 * @uses $GLOBALS['controllink']
343 * @uses $GLOBALS['information_schema_relations']
344 * @uses PMA_getRelationsParam()
345 * @uses PMA_backquote()
346 * @uses PMA_sqlAddslashes()
347 * @uses PMA_DBI_fetch_result()
348 * @uses PMA_DBI_fetch_value()
349 * @uses PMA_SQP_analyze()
350 * @uses PMA_SQP_parse()
351 * @uses count()
352 * @uses strlen()
353 * @param string $db the name of the db to check for
354 * @param string $table the name of the table to check for
355 * @param string $column the name of the column to check for
356 * @param string $source the source for foreign key information
357 * @return array db,table,column
359 function PMA_getForeigners($db, $table, $column = '', $source = 'both')
361 $cfgRelation = PMA_getRelationsParam();
362 $foreign = array();
364 if ($cfgRelation['relwork'] && ($source == 'both' || $source == 'internal')) {
365 $rel_query = '
366 SELECT `master_field`,
367 `foreign_db`,
368 `foreign_table`,
369 `foreign_field`
370 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . '
371 WHERE `master_db` = \'' . PMA_sqlAddslashes($db) . '\'
372 AND `master_table` = \'' . PMA_sqlAddslashes($table) . '\' ';
373 if (strlen($column)) {
374 $rel_query .= ' AND `master_field` = \'' . PMA_sqlAddslashes($column) . '\'';
376 $foreign = PMA_DBI_fetch_result($rel_query, 'master_field', null, $GLOBALS['controllink']);
379 if (($source == 'both' || $source == 'foreign') && strlen($table)) {
380 $show_create_table_query = 'SHOW CREATE TABLE '
381 . PMA_backquote($db) . '.' . PMA_backquote($table);
382 $show_create_table = PMA_DBI_fetch_value($show_create_table_query, 0, 1);
383 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
385 foreach ($analyzed_sql[0]['foreign_keys'] as $one_key) {
386 // The analyzer may return more than one column name in the
387 // index list or the ref_index_list; if this happens,
388 // the current logic just discards the whole index; having
389 // more than one index field is currently unsupported (see FAQ 3.6)
390 if (count($one_key['index_list']) == 1) {
391 foreach ($one_key['index_list'] as $i => $field) {
392 // If a foreign key is defined in the 'internal' source (pmadb)
393 // and as a native foreign key, we won't get it twice
394 // if $source='both' because we use $field as key
396 // The parser looks for a CONSTRAINT clause just before
397 // the FOREIGN KEY clause. It finds it (as output from
398 // SHOW CREATE TABLE) in MySQL 4.0.13, but not in older
399 // versions like 3.23.58.
400 // In those cases, the FOREIGN KEY parsing will put numbers
401 // like -1, 0, 1... instead of the constraint number.
403 if (isset($one_key['constraint'])) {
404 $foreign[$field]['constraint'] = $one_key['constraint'];
407 if (isset($one_key['ref_db_name'])) {
408 $foreign[$field]['foreign_db'] = $one_key['ref_db_name'];
409 } else {
410 $foreign[$field]['foreign_db'] = $db;
412 $foreign[$field]['foreign_table'] = $one_key['ref_table_name'];
413 $foreign[$field]['foreign_field'] = $one_key['ref_index_list'][$i];
414 if (isset($one_key['on_delete'])) {
415 $foreign[$field]['on_delete'] = $one_key['on_delete'];
417 if (isset($one_key['on_update'])) {
418 $foreign[$field]['on_update'] = $one_key['on_update'];
426 * Emulating relations for some information_schema tables
428 if ($db == 'information_schema'
429 && ($source == 'internal' || $source == 'both')) {
430 require_once './libraries/information_schema_relations.lib.php';
432 if (isset($GLOBALS['information_schema_relations'][$table])) {
433 foreach ($GLOBALS['information_schema_relations'][$table] as $field => $relations) {
434 if ((! strlen($column) || $column == $field)
435 && (! isset($foreign[$field]) || ! strlen($foreign[$field]))) {
436 $foreign[$field] = $relations;
442 return $foreign;
443 } // end of the 'PMA_getForeigners()' function
446 * Gets the display field of a table
448 * @access public
449 * @author Mike Beck <mikebeck@users.sourceforge.net>
450 * @uses $GLOBALS['controllink']
451 * @uses PMA_getRelationsParam()
452 * @uses PMA_backquote()
453 * @uses PMA_sqlAddslashes()
454 * @uses PMA_DBI_fetch_single_row()
455 * @uses trim()
456 * @param string $db the name of the db to check for
457 * @param string $table the name of the table to check for
458 * @return string field name
460 function PMA_getDisplayField($db, $table)
462 $cfgRelation = PMA_getRelationsParam();
465 * Try to fetch the display field from DB.
467 if ($cfgRelation['displaywork']) {
468 $disp_query = '
469 SELECT `display_field`
470 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info']) . '
471 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
472 AND `table_name` = \'' . PMA_sqlAddslashes($table) . '\'';
474 $row = PMA_DBI_fetch_single_row($disp_query, 'ASSOC', $GLOBALS['controllink']);
475 if (isset($row['display_field'])) {
476 return $row['display_field'];
481 * Emulating the display field for some information_schema tables.
483 if ($db == 'information_schema') {
484 switch ($table) {
485 case 'CHARACTER_SETS': return 'DESCRIPTION';
486 case 'TABLES': return 'TABLE_COMMENT';
491 * No Luck...
493 return false;
495 } // end of the 'PMA_getDisplayField()' function
498 * Gets the comments for all rows of a table or the db itself
500 * @author Mike Beck <mikebeck@users.sourceforge.net>
501 * @author lem9
502 * @access public
503 * @uses PMA_DBI_get_fields()
504 * @uses PMA_getDbComment()
505 * @param string the name of the db to check for
506 * @param string the name of the table to check for
507 * @return array [field_name] = comment
509 function PMA_getComments($db, $table = '')
511 $comments = array();
513 if ($table != '') {
514 // MySQL native column comments
515 $fields = PMA_DBI_get_fields($db, $table);
516 if ($fields) {
517 foreach ($fields as $key => $field) {
518 if (! empty($field['Comment'])) {
519 $comments[$field['Field']] = $field['Comment'];
523 } else {
524 $comments[] = PMA_getDbComment($db);
527 return $comments;
528 } // end of the 'PMA_getComments()' function
531 * Gets the comment for a db
533 * @author Mike Beck <mikebeck@users.sourceforge.net>
534 * @author lem9
535 * @access public
536 * @uses PMA_DBI_QUERY_STORE
537 * @uses PMA_DBI_num_rows()
538 * @uses PMA_DBI_fetch_assoc()
539 * @uses PMA_DBI_free_result()
540 * @uses PMA_getRelationsParam()
541 * @uses PMA_backquote()
542 * @uses PMA_sqlAddslashes()
543 * @uses PMA_query_as_cu()
544 * @uses strlen()
545 * @param string the name of the db to check for
546 * @return string comment
548 function PMA_getDbComment($db)
550 $cfgRelation = PMA_getRelationsParam();
551 $comment = '';
553 if ($cfgRelation['commwork']) {
554 // pmadb internal db comment
555 $com_qry = "
556 SELECT `comment`
557 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
558 WHERE db_name = '" . PMA_sqlAddslashes($db) . "'
559 AND table_name = ''
560 AND column_name = '(db_comment)'";
561 $com_rs = PMA_query_as_cu($com_qry, true, PMA_DBI_QUERY_STORE);
563 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
564 $row = PMA_DBI_fetch_assoc($com_rs);
565 $comment = $row['comment'];
567 PMA_DBI_free_result($com_rs);
570 return $comment;
571 } // end of the 'PMA_getDbComment()' function
574 * Gets the comment for a db
576 * @author Mike Beck <mikebeck@users.sourceforge.net>
577 * @author lem9
578 * @access public
579 * @uses PMA_DBI_QUERY_STORE
580 * @uses PMA_DBI_num_rows()
581 * @uses PMA_DBI_fetch_assoc()
582 * @uses PMA_DBI_free_result()
583 * @uses PMA_getRelationsParam()
584 * @uses PMA_backquote()
585 * @uses PMA_sqlAddslashes()
586 * @uses PMA_query_as_cu()
587 * @uses strlen()
588 * @param string the name of the db to check for
589 * @return string comment
591 function PMA_getDbComments()
593 $cfgRelation = PMA_getRelationsParam();
594 $comments = array();
596 if ($cfgRelation['commwork']) {
597 // pmadb internal db comment
598 $com_qry = "
599 SELECT `db_name`, `comment`
600 FROM " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
601 WHERE `column_name` = '(db_comment)'";
602 $com_rs = PMA_query_as_cu($com_qry, true, PMA_DBI_QUERY_STORE);
604 if ($com_rs && PMA_DBI_num_rows($com_rs) > 0) {
605 while ($row = PMA_DBI_fetch_assoc($com_rs)) {
606 $comments[$row['db_name']] = $row['comment'];
609 PMA_DBI_free_result($com_rs);
612 return $comments;
613 } // end of the 'PMA_getDbComments()' function
616 * Set a database comment to a certain value.
618 * @uses PMA_getRelationsParam()
619 * @uses PMA_backquote()
620 * @uses PMA_sqlAddslashes()
621 * @uses PMA_query_as_cu()
622 * @uses strlen()
623 * @access public
624 * @param string $db the name of the db
625 * @param string $comment the value of the column
626 * @return boolean true, if comment-query was made.
628 function PMA_setDbComment($db, $comment = '')
630 $cfgRelation = PMA_getRelationsParam();
632 if (! $cfgRelation['commwork']) {
633 return false;
636 if (strlen($comment)) {
637 $upd_query = "
638 INSERT INTO
639 " . PMA_backquote($cfgRelation['db']) . "." . PMA_backquote($cfgRelation['column_info']) . "
640 (`db_name`, `table_name`, `column_name`, `comment`)
641 VALUES (
642 '" . PMA_sqlAddslashes($db) . "',
644 '(db_comment)',
645 '" . PMA_sqlAddslashes($comment) . "')
646 ON DUPLICATE KEY UPDATE
647 `comment` = '" . PMA_sqlAddslashes($comment) . "'";
648 } else {
649 $upd_query = '
650 DELETE FROM
651 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['column_info']) . '
652 WHERE `db_name` = \'' . PMA_sqlAddslashes($db) . '\'
653 AND `table_name` = \'\'
654 AND `column_name` = \'(db_comment)\'';
657 if (isset($upd_query)){
658 return PMA_query_as_cu($upd_query);
661 return false;
662 } // end of 'PMA_setDbComment()' function
665 * Set a SQL history entry
667 * @uses $_SESSION['sql_history']
668 * @uses $cfg['QueryHistoryMax']
669 * @uses PMA_getRelationsParam()
670 * @uses PMA_query_as_cu()
671 * @uses PMA_backquote()
672 * @uses PMA_sqlAddslashes()
673 * @uses count()
674 * @uses md5()
675 * @uses array_shift()
676 * @param string $db the name of the db
677 * @param string $table the name of the table
678 * @param string $username the username
679 * @param string $sqlquery the sql query
680 * @access public
682 function PMA_setHistory($db, $table, $username, $sqlquery)
684 if (strlen($sqlquery) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
685 return;
688 $cfgRelation = PMA_getRelationsParam();
690 if (! isset($_SESSION['sql_history'])) {
691 $_SESSION['sql_history'] = array();
694 $key = md5($sqlquery . $db . $table);
696 if (isset($_SESSION['sql_history'][$key])) {
697 unset($_SESSION['sql_history'][$key]);
700 $_SESSION['sql_history'][$key] = array(
701 'db' => $db,
702 'table' => $table,
703 'sqlquery' => $sqlquery,
706 if (count($_SESSION['sql_history']) > $GLOBALS['cfg']['QueryHistoryMax']) {
707 // history should not exceed a maximum count
708 array_shift($_SESSION['sql_history']);
711 if (! $cfgRelation['historywork']) {
712 return;
715 PMA_query_as_cu('
716 INSERT INTO
717 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
718 (`username`,
719 `db`,
720 `table`,
721 `timevalue`,
722 `sqlquery`)
723 VALUES
724 (\'' . PMA_sqlAddslashes($username) . '\',
725 \'' . PMA_sqlAddslashes($db) . '\',
726 \'' . PMA_sqlAddslashes($table) . '\',
727 NOW(),
728 \'' . PMA_sqlAddslashes($sqlquery) . '\')');
729 } // end of 'PMA_setHistory()' function
732 * Gets a SQL history entry
734 * @uses $_SESSION['sql_history']
735 * @uses $GLOBALS['controllink']
736 * @uses PMA_getRelationsParam()
737 * @uses PMA_backquote()
738 * @uses PMA_sqlAddslashes()
739 * @uses PMA_DBI_fetch_result()
740 * @uses array_reverse()
741 * @param string $username the username
742 * @return array list of history items
743 * @access public
745 function PMA_getHistory($username)
747 $cfgRelation = PMA_getRelationsParam();
749 if (isset($_SESSION['sql_history'])) {
750 return array_reverse($_SESSION['sql_history']);
753 if (! $cfgRelation['historywork']) {
754 return false;
757 $hist_query = '
758 SELECT `db`,
759 `table`,
760 `sqlquery`
761 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
762 WHERE `username` = \'' . PMA_sqlAddslashes($username) . '\'
763 ORDER BY `id` DESC';
765 return PMA_DBI_fetch_result($hist_query, null, null, $GLOBALS['controllink']);
766 } // end of 'PMA_getHistory()' function
769 * purges SQL history
771 * deletes entries that exceeds $cfg['QueryHistoryMax'], oldest first, for the
772 * given user
774 * @uses $cfg['QueryHistoryMax']
775 * @uses $cfg['QueryHistoryDB']
776 * @uses $GLOBALS['controllink']
777 * @uses PMA_backquote()
778 * @uses PMA_sqlAddSlashes()
779 * @uses PMA_query_as_cu()
780 * @uses PMA_DBI_fetch_value()
781 * @param string $username the username
782 * @access public
784 function PMA_purgeHistory($username)
786 $cfgRelation = PMA_getRelationsParam();
787 if (! $GLOBALS['cfg']['QueryHistoryDB'] || ! $cfgRelation['historywork']) {
788 return;
791 if (! $cfgRelation['historywork']) {
792 return;
795 $search_query = '
796 SELECT `timevalue`
797 FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
798 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
799 ORDER BY `timevalue` DESC
800 LIMIT ' . $GLOBALS['cfg']['QueryHistoryMax'] . ', 1';
802 if ($max_time = PMA_DBI_fetch_value($search_query, 0, 0, $GLOBALS['controllink'])) {
803 PMA_query_as_cu('
804 DELETE FROM
805 ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . '
806 WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\'
807 AND `timevalue` <= \'' . $max_time . '\'');
809 } // end of 'PMA_purgeHistory()' function
812 * Prepares the dropdown for one mode
814 * @uses $cfg['LimitChars']
815 * @uses $cfg['NaturalOrder']
816 * @uses PMA_strlen()
817 * @uses htmlspecialchars()
818 * @uses substr()
819 * @uses uksort()
820 * @uses ksort()
821 * @uses natcasesort()
822 * @uses asort()
823 * @param array $foreign the keys and values for foreigns
824 * @param string $data the current data of the dropdown
825 * @param string $mode the needed mode
827 * @return array the <option value=""><option>s
829 * @access protected
831 function PMA__foreignDropdownBuild($foreign, $data, $mode)
833 $reloptions = array();
835 if ($mode == 'id-content') {
836 // sort for id-content
837 if ($GLOBALS['cfg']['NaturalOrder']) {
838 uksort($foreign, 'strnatcasecmp');
839 } else {
840 ksort($foreign);
842 } elseif ($mode == 'content-id') {
843 // sort for content-id
844 if ($GLOBALS['cfg']['NaturalOrder']) {
845 natcasesort($foreign);
846 } else {
847 asort($foreign);
851 foreach ($foreign as $key => $value) {
853 if (PMA_strlen($value) <= $GLOBALS['cfg']['LimitChars']) {
854 $vtitle = '';
855 $value = htmlspecialchars($value);
856 } else {
857 $vtitle = htmlspecialchars($value);
858 $value = htmlspecialchars(substr($value, 0, $GLOBALS['cfg']['LimitChars']) . '...');
861 $reloption = ' <option value="' . htmlspecialchars($key) . '"';
862 if ($vtitle != '') {
863 $reloption .= ' title="' . $vtitle . '"';
866 if ((string) $key == (string) $data) {
867 $reloption .= ' selected="selected"';
870 if ($mode == 'content-id') {
871 $reloptions[] = $reloption . '>' . $value . '&nbsp;-&nbsp;' . htmlspecialchars($key) . '</option>' . "\n";
872 } else {
873 $reloptions[] = $reloption . '>' . htmlspecialchars($key) . '&nbsp;-&nbsp;' . $value . '</option>' . "\n";
875 } // end foreach
877 return $reloptions;
878 } // end of 'PMA__foreignDropdownBuild' function
881 * Outputs dropdown with values of foreign fields
883 * @uses $cfg['ForeignKeyMaxLimit']
884 * @uses $cfg['ForeignKeyDropdownOrder']
885 * @uses PMA__foreignDropdownBuild()
886 * @uses PMA_isValid()
887 * @uses implode()
888 * @param array array of the displayed row
889 * @param string the foreign field
890 * @param string the foreign field to display
891 * @param string the current data of the dropdown (field in row)
892 * @return string the <option value=""><option>s
893 * @access public
895 function PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, $data,
896 $max = null)
898 if (null === $max) {
899 $max = $GLOBALS['cfg']['ForeignKeyMaxLimit'];
902 $foreign = array();
904 // collect the data
905 foreach ($disp_row as $relrow) {
906 $key = $relrow[$foreign_field];
908 // if the display field has been defined for this foreign table
909 if ($foreign_display) {
910 $value = $relrow[$foreign_display];
911 } else {
912 $value = '';
913 } // end if ($foreign_display)
915 $foreign[$key] = $value;
916 } // end foreach
918 // put the dropdown sections in correct order
919 $top = array();
920 $bot = array();
921 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'], 'array')) {
922 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][0])) {
923 $top = PMA__foreignDropdownBuild($foreign, $data,
924 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][0]);
926 if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][1])) {
927 $bot = PMA__foreignDropdownBuild($foreign, $data,
928 $GLOBALS['cfg']['ForeignKeyDropdownOrder'][1]);
930 } else {
931 $top = PMA__foreignDropdownBuild($foreign, $data, 'id-content');
932 $bot = PMA__foreignDropdownBuild($foreign, $data, 'content-id');
935 // beginning of dropdown
936 $ret = '<option value="">&nbsp;</option>' . "\n";
938 $top_count = count($top);
939 if ($max == -1 || $top_count < $max) {
940 $ret .= implode('', $top);
941 if ($top_count > 0) {
942 $ret .= ' <option value="">&nbsp;</option>' . "\n";
943 $ret .= ' <option value="">&nbsp;</option>' . "\n";
946 $ret .= implode('', $bot);
948 return $ret;
949 } // end of 'PMA_foreignDropdown()' function
952 * Gets foreign keys in preparation for a drop-down selector
953 * Thanks to <markus@noga.de>
955 * @uses PMA_Table::countRecords()
956 * @uses PMA_backquote()
957 * @uses PMA_getDisplayField()
958 * @uses PMA_sqlAddslashes()
959 * @uses PMA_DBI_fetch_value()
960 * @uses PMA_DBI_free_result()
961 * @uses PMA_DBI_query()
962 * @uses PMA_DBI_num_rows()
963 * @uses PMA_DBI_fetch_assoc()
964 * @param array array of the foreign keys
965 * @param string the foreign field name
966 * @param bool whether to override the total
967 * @param string a possible filter
968 * @param string a possible LIMIT clause
969 * @return array data about the foreign keys
970 * @access public
973 function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filter, $foreign_limit)
975 // we always show the foreign field in the drop-down; if a display
976 // field is defined, we show it besides the foreign field
977 $foreign_link = false;
978 if ($foreigners && isset($foreigners[$field])) {
979 $foreigner = $foreigners[$field];
980 $foreign_db = $foreigner['foreign_db'];
981 $foreign_table = $foreigner['foreign_table'];
982 $foreign_field = $foreigner['foreign_field'];
984 // Count number of rows in the foreign table. Currently we do
985 // not use a drop-down if more than 200 rows in the foreign table,
986 // for speed reasons and because we need a better interface for this.
988 // We could also do the SELECT anyway, with a LIMIT, and ensure that
989 // the current value of the field is one of the choices.
991 $the_total = PMA_Table::countRecords($foreign_db, $foreign_table, TRUE);
993 if ($override_total == true || $the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']) {
994 // foreign_display can be FALSE if no display field defined:
995 $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table);
997 $f_query_main = 'SELECT ' . PMA_backquote($foreign_field)
998 . (($foreign_display == FALSE) ? '' : ', ' . PMA_backquote($foreign_display));
999 $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table);
1000 $f_query_filter = empty($foreign_filter) ? '' : ' WHERE ' . PMA_backquote($foreign_field)
1001 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1002 . (($foreign_display == FALSE) ? '' : ' OR ' . PMA_backquote($foreign_display)
1003 . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"'
1005 $f_query_order = ($foreign_display == FALSE) ? '' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display);
1006 $f_query_limit = isset($foreign_limit) ? $foreign_limit : '';
1008 if (!empty($foreign_filter)) {
1009 $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter);
1010 if ($res) {
1011 $the_total = PMA_DBI_fetch_value($res);
1012 @PMA_DBI_free_result($res);
1013 } else {
1014 $the_total = 0;
1018 $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit);
1019 if ($disp && PMA_DBI_num_rows($disp) > 0) {
1020 // If a resultset has been created, pre-cache it in the $disp_row array
1021 // This helps us from not needing to use mysql_data_seek by accessing a pre-cached
1022 // PHP array. Usually those resultsets are not that big, so a performance hit should
1023 // not be expected.
1024 $disp_row = array();
1025 while ($single_disp_row = @PMA_DBI_fetch_assoc($disp)) {
1026 $disp_row[] = $single_disp_row;
1028 @PMA_DBI_free_result($disp);
1030 } else {
1031 $disp_row = null;
1032 $foreign_link = true;
1034 } // end if $foreigners
1036 $foreignData['foreign_link'] = $foreign_link;
1037 $foreignData['the_total'] = isset($the_total) ? $the_total : null;
1038 $foreignData['foreign_display'] = isset($foreign_display) ? $foreign_display : null;
1039 $foreignData['disp_row'] = isset($disp_row) ? $disp_row : null;
1040 $foreignData['foreign_field'] = isset($foreign_field) ? $foreign_field : null;
1041 return $foreignData;
1042 } // end of 'PMA_getForeignData()' function
1045 * Finds all related tables
1047 * @uses $GLOBALS['controllink']
1048 * @uses $GLOBALS['cfgRelation']
1049 * @uses $GLOBALS['db']
1050 * @param string whether to go from master to foreign or vice versa
1051 * @return boolean always TRUE
1052 * @global array $tab_left the list of tables that we still couldn't connect
1053 * @global array $tab_know the list of allready connected tables
1054 * @global string $fromclause
1056 * @access private
1058 function PMA_getRelatives($from)
1060 global $tab_left, $tab_know, $fromclause;
1062 if ($from == 'master') {
1063 $to = 'foreign';
1064 } else {
1065 $to = 'master';
1067 $in_know = '(\'' . implode('\', \'', $tab_know) . '\')';
1068 $in_left = '(\'' . implode('\', \'', $tab_left) . '\')';
1070 $rel_query = 'SELECT *'
1071 . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db'])
1072 . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation'])
1073 . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1074 . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\''
1075 . ' AND ' . $from . '_table IN ' . $in_know
1076 . ' AND ' . $to . '_table IN ' . $in_left;
1077 $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']);
1078 while ($row = PMA_DBI_fetch_assoc($relations)) {
1079 $found_table = $row[$to . '_table'];
1080 if (isset($tab_left[$found_table])) {
1081 $fromclause
1082 .= "\n" . ' LEFT JOIN '
1083 . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($row[$to . '_table']) . ' ON '
1084 . PMA_backquote($row[$from . '_table']) . '.'
1085 . PMA_backquote($row[$from . '_field']) . ' = '
1086 . PMA_backquote($row[$to . '_table']) . '.'
1087 . PMA_backquote($row[$to . '_field']) . ' ';
1088 $tab_know[$found_table] = $found_table;
1089 unset($tab_left[$found_table]);
1091 } // end while
1093 return true;
1094 } // end of the "PMA_getRelatives()" function
1097 * Rename a field in relation tables
1099 * usually called after a field in a table was renamed in tbl_alter.php
1101 * @uses PMA_getRelationsParam()
1102 * @uses PMA_backquote()
1103 * @uses PMA_sqlAddslashes()
1104 * @uses PMA_query_as_cu()
1105 * @param string $db
1106 * @param string $table
1107 * @param string $field
1108 * @param string $new_name
1110 function PMA_REL_renameField($db, $table, $field, $new_name)
1112 $cfgRelation = PMA_getRelationsParam();
1114 if ($cfgRelation['displaywork']) {
1115 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
1116 . ' SET display_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1117 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
1118 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
1119 . ' AND display_field = \'' . PMA_sqlAddslashes($field) . '\'';
1120 PMA_query_as_cu($table_query);
1123 if ($cfgRelation['relwork']) {
1124 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1125 . ' SET master_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1126 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
1127 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
1128 . ' AND master_field = \'' . PMA_sqlAddslashes($field) . '\'';
1129 PMA_query_as_cu($table_query);
1131 $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation'])
1132 . ' SET foreign_field = \'' . PMA_sqlAddslashes($new_name) . '\''
1133 . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\''
1134 . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
1135 . ' AND foreign_field = \'' . PMA_sqlAddslashes($field) . '\'';
1136 PMA_query_as_cu($table_query);
1137 } // end if relwork
1141 * Create a PDF page
1143 * @uses $GLOBALS['strNoDescription']
1144 * @uses PMA_backquote()
1145 * @uses $GLOBALS['cfgRelation']['db']
1146 * @uses PMA_sqlAddslashes()
1147 * @uses PMA_query_as_cu()
1148 * @uses PMA_DBI_insert_id()
1149 * @uses $GLOBALS['controllink']
1150 * @param string $newpage
1151 * @param array $cfgRelation
1152 * @param string $db
1153 * @param string $query_default_option
1154 * @return string $pdf_page_number
1156 function PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option) {
1157 if (! isset($newpage) || $newpage == '') {
1158 $newpage = $GLOBALS['strNoDescription'];
1160 $ins_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages'])
1161 . ' (db_name, page_descr)'
1162 . ' VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($newpage) . '\')';
1163 PMA_query_as_cu($ins_query, FALSE, $query_default_option);
1164 return PMA_DBI_insert_id(isset($GLOBALS['controllink']) ? $GLOBALS['controllink'] : '');