📄 relation.lib.php
字号:
* @uses PMA_DBI_fetch_value() * @param string $username the username * @access public */function PMA_purgeHistory($username){ $cfgRelation = PMA_getRelationsParam(); if (! $GLOBALS['cfg']['QueryHistoryDB'] || ! $cfgRelation['historywork']) { return; } if (! $cfgRelation['historywork']) { return; } $search_query = ' SELECT `timevalue` FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . ' WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\' ORDER BY `timevalue` DESC LIMIT ' . $GLOBALS['cfg']['QueryHistoryMax'] . ', 1'; if ($max_time = PMA_DBI_fetch_value($search_query, 0, 0, $GLOBALS['controllink'])) { PMA_query_as_cu(' DELETE FROM ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['history']) . ' WHERE `username` = \'' . PMA_sqlAddSlashes($username) . '\' AND `timevalue` <= \'' . $max_time . '\''); }} // end of 'PMA_purgeHistory()' function/** * Prepares the dropdown for one mode * * @uses $cfg['LimitChars'] * @uses $cfg['NaturalOrder'] * @uses PMA_strlen() * @uses htmlspecialchars() * @uses substr() * @uses uksort() * @uses ksort() * @uses natcasesort() * @uses asort() * @param array $foreign the keys and values for foreigns * @param string $data the current data of the dropdown * @param string $mode the needed mode * * @return array the <option value=""><option>s * * @access protected */function PMA__foreignDropdownBuild($foreign, $data, $mode){ $reloptions = array(); if ($mode == 'id-content') { // sort for id-content if ($GLOBALS['cfg']['NaturalOrder']) { uksort($foreign, 'strnatcasecmp'); } else { ksort($foreign); } } elseif ($mode == 'content-id') { // sort for content-id if ($GLOBALS['cfg']['NaturalOrder']) { natcasesort($foreign); } else { asort($foreign); } } foreach ($foreign as $key => $value) { if (PMA_strlen($value) <= $GLOBALS['cfg']['LimitChars']) { $vtitle = ''; $value = htmlspecialchars($value); } else { $vtitle = htmlspecialchars($value); $value = htmlspecialchars(substr($value, 0, $GLOBALS['cfg']['LimitChars']) . '...'); } $reloption = ' <option value="' . htmlspecialchars($key) . '"'; if ($vtitle != '') { $reloption .= ' title="' . $vtitle . '"'; } if ((string) $key == (string) $data) { $reloption .= ' selected="selected"'; } if ($mode == 'content-id') { $reloptions[] = $reloption . '>' . $value . ' - ' . htmlspecialchars($key) . '</option>' . "\n"; } else { $reloptions[] = $reloption . '>' . htmlspecialchars($key) . ' - ' . $value . '</option>' . "\n"; } } // end foreach return $reloptions;} // end of 'PMA__foreignDropdownBuild' function/** * Outputs dropdown with values of foreign fields * * @uses $cfg['ForeignKeyMaxLimit'] * @uses $cfg['ForeignKeyDropdownOrder'] * @uses PMA__foreignDropdownBuild() * @uses PMA_isValid() * @uses implode() * @param array array of the displayed row * @param string the foreign field * @param string the foreign field to display * @param string the current data of the dropdown (field in row) * @return string the <option value=""><option>s * @access public */function PMA_foreignDropdown($disp_row, $foreign_field, $foreign_display, $data, $max = null){ if (null === $max) { $max = $GLOBALS['cfg']['ForeignKeyMaxLimit']; } $foreign = array(); // collect the data foreach ($disp_row as $relrow) { $key = $relrow[$foreign_field]; // if the display field has been defined for this foreign table if ($foreign_display) { $value = $relrow[$foreign_display]; } else { $value = ''; } // end if ($foreign_display) $foreign[$key] = $value; } // end foreach // put the dropdown sections in correct order $top = array(); $bot = array(); if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'], 'array')) { if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][0])) { $top = PMA__foreignDropdownBuild($foreign, $data, $GLOBALS['cfg']['ForeignKeyDropdownOrder'][0]); } if (PMA_isValid($GLOBALS['cfg']['ForeignKeyDropdownOrder'][1])) { $bot = PMA__foreignDropdownBuild($foreign, $data, $GLOBALS['cfg']['ForeignKeyDropdownOrder'][1]); } } else { $top = PMA__foreignDropdownBuild($foreign, $data, 'id-content'); $bot = PMA__foreignDropdownBuild($foreign, $data, 'content-id'); } // beginning of dropdown $ret = '<option value=""> </option>' . "\n"; $top_count = count($top); if ($max == -1 || $top_count < $max) { $ret .= implode('', $top); if ($top_count > 0) { $ret .= ' <option value=""> </option>' . "\n"; $ret .= ' <option value=""> </option>' . "\n"; } } $ret .= implode('', $bot); return $ret;} // end of 'PMA_foreignDropdown()' function/** * Gets foreign keys in preparation for a drop-down selector * Thanks to <markus@noga.de> * * @uses PMA_Table::countRecords() * @uses PMA_backquote() * @uses PMA_getDisplayField() * @uses PMA_sqlAddslashes() * @uses PMA_DBI_fetch_value() * @uses PMA_DBI_free_result() * @uses PMA_DBI_query() * @uses PMA_DBI_num_rows() * @uses PMA_DBI_fetch_assoc() * @param array array of the foreign keys * @param string the foreign field name * @param bool whether to override the total * @param string a possible filter * @param string a possible LIMIT clause * @return array data about the foreign keys * @access public */function PMA_getForeignData($foreigners, $field, $override_total, $foreign_filter, $foreign_limit){ // we always show the foreign field in the drop-down; if a display // field is defined, we show it besides the foreign field $foreign_link = false; if ($foreigners && isset($foreigners[$field])) { $foreigner = $foreigners[$field]; $foreign_db = $foreigner['foreign_db']; $foreign_table = $foreigner['foreign_table']; $foreign_field = $foreigner['foreign_field']; // Count number of rows in the foreign table. Currently we do // not use a drop-down if more than 200 rows in the foreign table, // for speed reasons and because we need a better interface for this. // // We could also do the SELECT anyway, with a LIMIT, and ensure that // the current value of the field is one of the choices. $the_total = PMA_Table::countRecords($foreign_db, $foreign_table, TRUE); if ($override_total == true || $the_total < $GLOBALS['cfg']['ForeignKeyMaxLimit']) { // foreign_display can be FALSE if no display field defined: $foreign_display = PMA_getDisplayField($foreign_db, $foreign_table); $f_query_main = 'SELECT ' . PMA_backquote($foreign_field) . (($foreign_display == FALSE) ? '' : ', ' . PMA_backquote($foreign_display)); $f_query_from = ' FROM ' . PMA_backquote($foreign_db) . '.' . PMA_backquote($foreign_table); $f_query_filter = empty($foreign_filter) ? '' : ' WHERE ' . PMA_backquote($foreign_field) . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"' . (($foreign_display == FALSE) ? '' : ' OR ' . PMA_backquote($foreign_display) . ' LIKE "%' . PMA_sqlAddslashes($foreign_filter, TRUE) . '%"' ); $f_query_order = ($foreign_display == FALSE) ? '' :' ORDER BY ' . PMA_backquote($foreign_table) . '.' . PMA_backquote($foreign_display); $f_query_limit = isset($foreign_limit) ? $foreign_limit : ''; if (!empty($foreign_filter)) { $res = PMA_DBI_query('SELECT COUNT(*)' . $f_query_from . $f_query_filter); if ($res) { $the_total = PMA_DBI_fetch_value($res); @PMA_DBI_free_result($res); } else { $the_total = 0; } } $disp = PMA_DBI_query($f_query_main . $f_query_from . $f_query_filter . $f_query_order . $f_query_limit); if ($disp && PMA_DBI_num_rows($disp) > 0) { // If a resultset has been created, pre-cache it in the $disp_row array // This helps us from not needing to use mysql_data_seek by accessing a pre-cached // PHP array. Usually those resultsets are not that big, so a performance hit should // not be expected. $disp_row = array(); while ($single_disp_row = @PMA_DBI_fetch_assoc($disp)) { $disp_row[] = $single_disp_row; } @PMA_DBI_free_result($disp); } } else { $disp_row = null; $foreign_link = true; } } // end if $foreigners $foreignData['foreign_link'] = $foreign_link; $foreignData['the_total'] = isset($the_total) ? $the_total : null; $foreignData['foreign_display'] = isset($foreign_display) ? $foreign_display : null; $foreignData['disp_row'] = isset($disp_row) ? $disp_row : null; $foreignData['foreign_field'] = isset($foreign_field) ? $foreign_field : null; return $foreignData;} // end of 'PMA_getForeignData()' function/** * Finds all related tables * * @uses $GLOBALS['controllink'] * @uses $GLOBALS['cfgRelation'] * @uses $GLOBALS['db'] * @param string whether to go from master to foreign or vice versa * @return boolean always TRUE * @global array $tab_left the list of tables that we still couldn't connect * @global array $tab_know the list of allready connected tables * @global string $fromclause * * @access private */function PMA_getRelatives($from){ global $tab_left, $tab_know, $fromclause; if ($from == 'master') { $to = 'foreign'; } else { $to = 'master'; } $in_know = '(\'' . implode('\', \'', $tab_know) . '\')'; $in_left = '(\'' . implode('\', \'', $tab_left) . '\')'; $rel_query = 'SELECT *' . ' FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . ' WHERE ' . $from . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\'' . ' AND ' . $to . '_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\'' . ' AND ' . $from . '_table IN ' . $in_know . ' AND ' . $to . '_table IN ' . $in_left; $relations = @PMA_DBI_query($rel_query, $GLOBALS['controllink']); while ($row = PMA_DBI_fetch_assoc($relations)) { $found_table = $row[$to . '_table']; if (isset($tab_left[$found_table])) { $fromclause .= "\n" . ' LEFT JOIN ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($row[$to . '_table']) . ' ON ' . PMA_backquote($row[$from . '_table']) . '.' . PMA_backquote($row[$from . '_field']) . ' = ' . PMA_backquote($row[$to . '_table']) . '.' . PMA_backquote($row[$to . '_field']) . ' '; $tab_know[$found_table] = $found_table; unset($tab_left[$found_table]); } } // end while return true;} // end of the "PMA_getRelatives()" function/** * Rename a field in relation tables * * usually called after a field in a table was renamed in tbl_alter.php * * @uses PMA_getRelationsParam() * @uses PMA_backquote() * @uses PMA_sqlAddslashes() * @uses PMA_query_as_cu() * @param string $db * @param string $table * @param string $field * @param string $new_name */function PMA_REL_renameField($db, $table, $field, $new_name){ $cfgRelation = PMA_getRelationsParam(); if ($cfgRelation['displaywork']) { $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['table_info']) . ' SET display_field = \'' . PMA_sqlAddslashes($new_name) . '\'' . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'' . ' AND display_field = \'' . PMA_sqlAddslashes($field) . '\''; PMA_query_as_cu($table_query); } if ($cfgRelation['relwork']) { $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' SET master_field = \'' . PMA_sqlAddslashes($new_name) . '\'' . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\'' . ' AND master_field = \'' . PMA_sqlAddslashes($field) . '\''; PMA_query_as_cu($table_query); $table_query = 'UPDATE ' . PMA_backquote($cfgRelation['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' SET foreign_field = \'' . PMA_sqlAddslashes($new_name) . '\'' . ' WHERE foreign_db = \'' . PMA_sqlAddslashes($db) . '\'' . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\'' . ' AND foreign_field = \'' . PMA_sqlAddslashes($field) . '\''; PMA_query_as_cu($table_query); } // end if relwork}/** * Create a PDF page * * @uses $GLOBALS['strNoDescription'] * @uses PMA_backquote() * @uses $GLOBALS['cfgRelation']['db'] * @uses PMA_sqlAddslashes() * @uses PMA_query_as_cu() * @uses PMA_DBI_insert_id() * @uses $GLOBALS['controllink'] * @param string $newpage * @param array $cfgRelation * @param string $db * @param string $query_default_option * @return string $pdf_page_number */function PMA_REL_create_page($newpage, $cfgRelation, $db, $query_default_option) { if (! isset($newpage) || $newpage == '') { $newpage = $GLOBALS['strNoDescription']; } $ins_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['pdf_pages']) . ' (db_name, page_descr)' . ' VALUES (\'' . PMA_sqlAddslashes($db) . '\', \'' . PMA_sqlAddslashes($newpage) . '\')'; PMA_query_as_cu($ins_query, FALSE, $query_default_option); return PMA_DBI_insert_id(isset($GLOBALS['controllink']) ? $GLOBALS['controllink'] : '');}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -