Translated using Weblate (Hungarian)
[phpmyadmin.git] / libraries / tracking.lib.php
blobba8e7c7c3bccecf232793d53cb618943d80e4887
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Functions used for database and table tracking
6 * @package PhpMyAdmin
7 */
9 /**
10 * Filters tracking entries
12 * @param array $data the entries to filter
13 * @param string $filter_ts_from "from" date
14 * @param string $filter_ts_to "to" date
15 * @param array $filter_users users
17 * @return array filtered entries
19 function PMA_filterTracking(
20 $data, $filter_ts_from, $filter_ts_to, $filter_users
21 ) {
22 $tmp_entries = array();
23 $id = 0;
24 foreach ($data as $entry) {
25 $timestamp = strtotime($entry['date']);
26 $filtered_user = in_array($entry['username'], $filter_users);
27 if ($timestamp >= $filter_ts_from
28 && $timestamp <= $filter_ts_to
29 && (in_array('*', $filter_users) || $filtered_user)
30 ) {
31 $tmp_entries[] = array(
32 'id' => $id,
33 'timestamp' => $timestamp,
34 'username' => $entry['username'],
35 'statement' => $entry['statement']
38 $id++;
40 return($tmp_entries);
43 /**
44 * Function to get html for data definition and data manipulation statements
46 * @param string $url_query url query
47 * @param int $last_version last version
49 * @return string
51 function PMA_getHtmlForDataDefinitionAndManipulationStatements($url_query,
52 $last_version
53 ) {
54 $html = '<div id="div_create_version">';
55 $html .= '<form method="post" action="tbl_tracking.php' . $url_query . '">';
56 $html .= PMA_URL_getHiddenInputs($GLOBALS['db'], $GLOBALS['table']);
57 $html .= '<fieldset>';
58 $html .= '<legend>';
59 $html .= sprintf(
60 __('Create version %1$s of %2$s'),
61 ($last_version + 1),
62 htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table'])
64 $html .= '</legend>';
65 $html .= '<input type="hidden" name="version" value="' . ($last_version + 1)
66 . '" />';
67 $html .= '<p>' . __('Track these data definition statements:')
68 . '</p>';
69 $html .= '<input type="checkbox" name="alter_table" value="true"'
70 . (/*overload*/mb_stripos(
71 $GLOBALS['cfg']['Server']['tracking_default_statements'],
72 'ALTER TABLE'
73 ) !== false ? ' checked="checked"' : '')
74 . ' /> ALTER TABLE<br/>';
75 $html .= '<input type="checkbox" name="rename_table" value="true"'
76 . (/*overload*/mb_stripos(
77 $GLOBALS['cfg']['Server']['tracking_default_statements'],
78 'RENAME TABLE'
79 ) !== false ? ' checked="checked"' : '')
80 . ' /> RENAME TABLE<br/>';
81 $html .= '<input type="checkbox" name="create_table" value="true"'
82 . (/*overload*/mb_stripos(
83 $GLOBALS['cfg']['Server']['tracking_default_statements'],
84 'CREATE TABLE'
85 ) !== false ? ' checked="checked"' : '')
86 . ' /> CREATE TABLE<br/>';
87 $html .= '<input type="checkbox" name="drop_table" value="true"'
88 . (/*overload*/mb_stripos(
89 $GLOBALS['cfg']['Server']['tracking_default_statements'],
90 'DROP TABLE'
91 ) !== false ? ' checked="checked"' : '')
92 . ' /> DROP TABLE<br/>';
93 $html .= '<br/>';
94 $html .= '<input type="checkbox" name="create_index" value="true"'
95 . (/*overload*/mb_stripos(
96 $GLOBALS['cfg']['Server']['tracking_default_statements'],
97 'CREATE INDEX'
98 ) !== false ? ' checked="checked"' : '')
99 . ' /> CREATE INDEX<br/>';
100 $html .= '<input type="checkbox" name="drop_index" value="true"'
101 . (/*overload*/mb_stripos(
102 $GLOBALS['cfg']['Server']['tracking_default_statements'],
103 'DROP INDEX'
104 ) !== false ? ' checked="checked"' : '')
105 . ' /> DROP INDEX<br/>';
106 $html .= '<p>' . __('Track these data manipulation statements:') . '</p>';
107 $html .= '<input type="checkbox" name="insert" value="true"'
108 . (/*overload*/mb_stripos(
109 $GLOBALS['cfg']['Server']['tracking_default_statements'],
110 'INSERT'
111 ) !== false ? ' checked="checked"' : '')
112 . ' /> INSERT<br/>';
113 $html .= '<input type="checkbox" name="update" value="true"'
114 . (/*overload*/mb_stripos(
115 $GLOBALS['cfg']['Server']['tracking_default_statements'],
116 'UPDATE'
117 ) !== false ? ' checked="checked"' : '')
118 . ' /> UPDATE<br/>';
119 $html .= '<input type="checkbox" name="delete" value="true"'
120 . (/*overload*/mb_stripos(
121 $GLOBALS['cfg']['Server']['tracking_default_statements'],
122 'DELETE'
123 ) !== false ? ' checked="checked"' : '')
124 . ' /> DELETE<br/>';
125 $html .= '<input type="checkbox" name="truncate" value="true"'
126 . (/*overload*/mb_stripos(
127 $GLOBALS['cfg']['Server']['tracking_default_statements'],
128 'TRUNCATE'
129 ) !== false ? ' checked="checked"' : '')
130 . ' /> TRUNCATE<br/>';
131 $html .= '</fieldset>';
133 $html .= '<fieldset class="tblFooters">';
134 $html .= '<input type="hidden" name="submit_create_version" value="1" />';
135 $html .= '<input type="submit" value="' . __('Create version') . '" />';
136 $html .= '</fieldset>';
138 $html .= '</form>';
139 $html .= '</div>';
141 return $html;
145 * Function to get html for activate/deactivate tracking
147 * @param string $action activate|deactivate
148 * @param string $url_query url query
149 * @param int $last_version last version
151 * @return string
153 function PMA_getHtmlForActivateDeactivateTracking(
154 $action, $url_query, $last_version
156 $html = '<div>';
157 $html .= '<form method="post" action="tbl_tracking.php' . $url_query . '">';
158 $html .= '<fieldset>';
159 $html .= '<legend>';
161 switch($action) {
162 case 'activate':
163 $legend = __('Activate tracking for %s');
164 $hidden = "submit_activate_now";
165 $button = __('Activate now');
166 break;
167 case 'deactivate':
168 $legend = __('Deactivate tracking for %s');
169 $hidden = "submit_deactivate_now";
170 $button = __('Deactivate now');
171 break;
172 default:
173 $legend = '';
174 $hidden = '';
175 $button = '';
178 $html .= sprintf(
179 $legend,
180 htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table'])
182 $html .= '</legend>';
183 $html .= '<input type="hidden" name="version" value="' . $last_version . '" />';
184 $html .= '<input type="hidden" name="' . $hidden . '" value="1" />';
185 $html .= '<input type="submit" value="' . $button . '" />';
186 $html .= '</fieldset>';
187 $html .= '</form>';
188 $html .= '</div>';
190 return $html;
194 * Function to get the list versions of the table
196 * @return array
198 function PMA_getListOfVersionsOfTable()
200 $sql_query = " SELECT * FROM " .
201 PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "." .
202 PMA_Util::backquote($GLOBALS['cfg']['Server']['tracking']) .
203 " WHERE db_name = '" . PMA_Util::sqlAddSlashes($_REQUEST['db']) . "' " .
204 " AND table_name = '" . PMA_Util::sqlAddSlashes($_REQUEST['table']) . "' " .
205 " ORDER BY version DESC ";
207 return PMA_queryAsControlUser($sql_query);
211 * Function to get html for displaying last version number
213 * @param array $sql_result sql result
214 * @param int $last_version last version
215 * @param array $url_params url parameters
216 * @param string $url_query url query
218 * @return string
220 function PMA_getHtmlForTableVersionDetails($sql_result, $last_version, $url_params,
221 $url_query
223 $tracking_active = false;
225 $html = '<table id="versions" class="data">';
226 $html .= '<thead>';
227 $html .= '<tr>';
228 $html .= '<th>' . __('Database') . '</th>';
229 $html .= '<th>' . __('Table') . '</th>';
230 $html .= '<th>' . __('Version') . '</th>';
231 $html .= '<th>' . __('Created') . '</th>';
232 $html .= '<th>' . __('Updated') . '</th>';
233 $html .= '<th>' . __('Status') . '</th>';
234 $html .= '<th>' . __('Show') . '</th>';
235 $html .= '</tr>';
236 $html .= '</thead>';
237 $html .= '<tbody>';
239 $style = 'odd';
240 $GLOBALS['dbi']->dataSeek($sql_result, 0);
241 while ($version = $GLOBALS['dbi']->fetchArray($sql_result)) {
242 if ($version['version'] == $last_version) {
243 if ($version['tracking_active'] == 1) {
244 $tracking_active = true;
245 } else {
246 $tracking_active = false;
249 $html .= '<tr class="noclick ' . $style . '">';
250 $html .= '<td>' . htmlspecialchars($version['db_name']) . '</td>';
251 $html .= '<td>' . htmlspecialchars($version['table_name']) . '</td>';
252 $html .= '<td>' . htmlspecialchars($version['version']) . '</td>';
253 $html .= '<td>' . htmlspecialchars($version['date_created']) . '</td>';
254 $html .= '<td>' . htmlspecialchars($version['date_updated']) . '</td>';
255 $html .= '<td>' . PMA_getVersionStatus($version) . '</td>';
256 $html .= '<td><a href="tbl_tracking.php';
257 $html .= PMA_URL_getCommon(
258 $url_params + array(
259 'report' => 'true', 'version' => $version['version']
262 $html .= '">' . __('Tracking report') . '</a>';
263 $html .= '&nbsp;|&nbsp;';
264 $html .= '<a href="tbl_tracking.php';
265 $html .= PMA_URL_getCommon(
266 $url_params + array(
267 'snapshot' => 'true', 'version' => $version['version']
270 $html .= '">' . __('Structure snapshot') . '</a>';
271 $html .= '</td>';
272 $html .= '</tr>';
274 if ($style == 'even') {
275 $style = 'odd';
276 } else {
277 $style = 'even';
281 $html .= '</tbody>';
282 $html .= '</table>';
284 if ($tracking_active) {
285 $html .= PMA_getHtmlForActivateDeactivateTracking(
286 'deactivate', $url_query, $last_version
288 } else {
289 $html .= PMA_getHtmlForActivateDeactivateTracking(
290 'activate', $url_query, $last_version
294 return $html;
298 * Function to get the last version number of a table
300 * @param array $sql_result sql result
302 * @return int
304 function PMA_getTableLastVersionNumber($sql_result)
306 $maxversion = $GLOBALS['dbi']->fetchArray($sql_result);
307 $last_version = $maxversion['version'];
309 return $last_version;
313 * Function to get sql results for selectable tables
315 * @return array
317 function PMA_getSQLResultForSelectableTables()
319 include_once 'libraries/relation.lib.php';
321 $sql_query = " SELECT DISTINCT db_name, table_name FROM " .
322 PMA_Util::backquote($GLOBALS['cfg']['Server']['pmadb']) . "." .
323 PMA_Util::backquote($GLOBALS['cfg']['Server']['tracking']) .
324 " WHERE db_name = '" . PMA_Util::sqlAddSlashes($GLOBALS['db']) . "' " .
325 " ORDER BY db_name, table_name";
327 return PMA_queryAsControlUser($sql_query);
331 * Function to get html for selectable table rows
333 * @param array $selectable_tables_sql_result sql results for selectable rows
334 * @param string $url_query url query
336 * @return string
338 function PMA_getHtmlForSelectableTables($selectable_tables_sql_result, $url_query)
340 $html = '<form method="post" action="tbl_tracking.php' . $url_query . '">';
341 $html .= '<select name="table">';
342 while ($entries = $GLOBALS['dbi']->fetchArray($selectable_tables_sql_result)) {
343 if (PMA_Tracker::isTracked($entries['db_name'], $entries['table_name'])) {
344 $status = ' (' . __('active') . ')';
345 } else {
346 $status = ' (' . __('not active') . ')';
348 if ($entries['table_name'] == $_REQUEST['table']) {
349 $s = ' selected="selected"';
350 } else {
351 $s = '';
353 $html .= '<option value="' . htmlspecialchars($entries['table_name'])
354 . '"' . $s . '>' . htmlspecialchars($entries['db_name']) . ' . '
355 . htmlspecialchars($entries['table_name']) . $status . '</option>'
356 . "\n";
358 $html .= '</select>';
359 $html .= '<input type="hidden" name="show_versions_submit" value="1" />';
360 $html .= '<input type="submit" value="' . __('Show versions') . '" />';
361 $html .= '</form>';
363 return $html;
367 * Function to get html for tracking report and tracking report export
369 * @param string $url_query url query
370 * @param array $data data
371 * @param array $url_params url params
372 * @param boolean $selection_schema selection schema
373 * @param boolean $selection_data selection data
374 * @param boolean $selection_both selection both
375 * @param int $filter_ts_to filter time stamp from
376 * @param int $filter_ts_from filter time stamp tp
377 * @param array $filter_users filter users
379 * @return string
381 function PMA_getHtmlForTrackingReport($url_query, $data, $url_params,
382 $selection_schema, $selection_data, $selection_both, $filter_ts_to,
383 $filter_ts_from, $filter_users
385 $html = '<h3>' . __('Tracking report')
386 . ' [<a href="tbl_tracking.php' . $url_query . '">' . __('Close')
387 . '</a>]</h3>';
389 $html .= '<small>' . __('Tracking statements') . ' '
390 . htmlspecialchars($data['tracking']) . '</small><br/>';
391 $html .= '<br/>';
393 list($str1, $str2, $str3, $str4, $str5) = PMA_getHtmlForElementsOfTrackingReport(
394 $selection_schema, $selection_data, $selection_both
397 // Prepare delete link content here
398 $drop_image_or_text = '';
399 if (PMA_Util::showIcons('ActionLinksMode')) {
400 $drop_image_or_text .= PMA_Util::getImage(
401 'b_drop.png', __('Delete tracking data row from report')
404 if (PMA_Util::showText('ActionLinksMode')) {
405 $drop_image_or_text .= __('Delete');
409 * First, list tracked data definition statements
411 if (count($data['ddlog']) == 0 && count($data['dmlog']) == 0) {
412 $msg = PMA_Message::notice(__('No data'));
413 $msg->display();
416 $html .= PMA_getHtmlForTrackingReportExportForm1(
417 $data, $url_params, $selection_schema, $selection_data, $selection_both,
418 $filter_ts_to, $filter_ts_from, $filter_users, $str1, $str2, $str3,
419 $str4, $str5, $drop_image_or_text
422 $html .= PMA_getHtmlForTrackingReportExportForm2(
423 $url_params, $str1, $str2, $str3, $str4, $str5
426 $html .= "<br/><br/><hr/><br/>\n";
428 return $html;
432 * Generate HTML element for report form
434 * @param boolean $selection_schema selection schema
435 * @param boolean $selection_data selection data
436 * @param boolean $selection_both selection both
438 * @return array
440 function PMA_getHtmlForElementsOfTrackingReport(
441 $selection_schema, $selection_data, $selection_both
443 $str1 = '<select name="logtype">'
444 . '<option value="schema"'
445 . ($selection_schema ? ' selected="selected"' : '') . '>'
446 . __('Structure only') . '</option>'
447 . '<option value="data"'
448 . ($selection_data ? ' selected="selected"' : '') . '>'
449 . __('Data only') . '</option>'
450 . '<option value="schema_and_data"'
451 . ($selection_both ? ' selected="selected"' : '') . '>'
452 . __('Structure and data') . '</option>'
453 . '</select>';
454 $str2 = '<input type="text" name="date_from" value="'
455 . htmlspecialchars($_REQUEST['date_from']) . '" size="19" />';
456 $str3 = '<input type="text" name="date_to" value="'
457 . htmlspecialchars($_REQUEST['date_to']) . '" size="19" />';
458 $str4 = '<input type="text" name="users" value="'
459 . htmlspecialchars($_REQUEST['users']) . '" />';
460 $str5 = '<input type="hidden" name="list_report" value="1" />'
461 . '<input type="submit" value="' . __('Go') . '" />';
462 return array($str1, $str2, $str3, $str4, $str5);
466 * Generate HTML for export form
468 * @param array $data data
469 * @param array $url_params url params
470 * @param boolean $selection_schema selection schema
471 * @param boolean $selection_data selection data
472 * @param boolean $selection_both selection both
473 * @param int $filter_ts_to filter time stamp from
474 * @param int $filter_ts_from filter time stamp tp
475 * @param array $filter_users filter users
476 * @param string $str1 HTML for logtype select
477 * @param string $str2 HTML for "from date"
478 * @param string $str3 HTML for "to date"
479 * @param string $str4 HTML for user
480 * @param string $str5 HTML for "list report"
481 * @param string $drop_image_or_text HTML for image or text
483 * @return string HTML for form
485 function PMA_getHtmlForTrackingReportExportForm1(
486 $data, $url_params, $selection_schema, $selection_data, $selection_both,
487 $filter_ts_to, $filter_ts_from, $filter_users, $str1, $str2, $str3,
488 $str4, $str5, $drop_image_or_text
490 $ddlog_count = 0;
492 $html = '<form method="post" action="tbl_tracking.php'
493 . PMA_URL_getCommon(
494 $url_params + array(
495 'report' => 'true', 'version' => $_REQUEST['version']
498 . '">';
500 $html .= sprintf(
501 __('Show %1$s with dates from %2$s to %3$s by user %4$s %5$s'),
502 $str1, $str2, $str3, $str4, $str5
505 if ($selection_schema || $selection_both && count($data['ddlog']) > 0) {
506 list($temp, $ddlog_count) = PMA_getHtmlForDataDefinitionStatements(
507 $data, $filter_users, $filter_ts_from, $filter_ts_to, $url_params,
508 $drop_image_or_text
510 $html .= $temp;
511 unset($temp);
512 } //endif
515 * Secondly, list tracked data manipulation statements
517 if (($selection_data || $selection_both) && count($data['dmlog']) > 0) {
518 $html .= PMA_getHtmlForDataManipulationStatements(
519 $data, $filter_users, $filter_ts_from, $filter_ts_to, $url_params,
520 $ddlog_count, $drop_image_or_text
523 $html .= '</form>';
524 return $html;
528 * Generate HTML for export form
530 * @param array $url_params Parameters
531 * @param string $str1 HTML for logtype select
532 * @param string $str2 HTML for "from date"
533 * @param string $str3 HTML for "to date"
534 * @param string $str4 HTML for user
535 * @param string $str5 HTML for "list report"
537 * @return string HTML for form
539 function PMA_getHtmlForTrackingReportExportForm2(
540 $url_params, $str1, $str2, $str3, $str4, $str5
542 $html = '<form method="post" action="tbl_tracking.php'
543 . PMA_URL_getCommon(
544 $url_params + array(
545 'report' => 'true', 'version' => $_REQUEST['version']
548 . '">';
549 $html .= sprintf(
550 __('Show %1$s with dates from %2$s to %3$s by user %4$s %5$s'),
551 $str1, $str2, $str3, $str4, $str5
553 $html .= '</form>';
555 $html .= '<form class="disableAjax" method="post" action="tbl_tracking.php'
556 . PMA_URL_getCommon(
557 $url_params
558 + array('report' => 'true', 'version' => $_REQUEST['version'])
560 . '">';
561 $html .= '<input type="hidden" name="logtype" value="'
562 . htmlspecialchars($_REQUEST['logtype']) . '" />';
563 $html .= '<input type="hidden" name="date_from" value="'
564 . htmlspecialchars($_REQUEST['date_from']) . '" />';
565 $html .= '<input type="hidden" name="date_to" value="'
566 . htmlspecialchars($_REQUEST['date_to']) . '" />';
567 $html .= '<input type="hidden" name="users" value="'
568 . htmlspecialchars($_REQUEST['users']) . '" />';
570 $str_export1 = '<select name="export_type">'
571 . '<option value="sqldumpfile">' . __('SQL dump (file download)')
572 . '</option>'
573 . '<option value="sqldump">' . __('SQL dump') . '</option>'
574 . '<option value="execution" onclick="alert(\''
575 . PMA_escapeJsString(
576 __('This option will replace your table and contained data.')
578 . '\')">' . __('SQL execution') . '</option>' . '</select>';
580 $str_export2 = '<input type="hidden" name="report_export" value="1" />'
581 . '<input type="submit" value="' . __('Go') . '" />';
583 $html .= "<br/>" . sprintf(__('Export as %s'), $str_export1)
584 . $str_export2 . "<br/>";
585 $html .= '</form>';
586 return $html;
590 * Function to get html for data manipulation statements
592 * @param array $data data
593 * @param array $filter_users filter users
594 * @param int $filter_ts_from filter time staml from
595 * @param int $filter_ts_to filter time stamp to
596 * @param array $url_params url parameters
597 * @param int $ddlog_count data definition log count
598 * @param string $drop_image_or_text drop image or text
600 * @return string
602 function PMA_getHtmlForDataManipulationStatements($data, $filter_users,
603 $filter_ts_from, $filter_ts_to, $url_params, $ddlog_count,
604 $drop_image_or_text
606 // no need for the secondth returned parameter
607 list($html,) = PMA_getHtmlForDataStatements(
608 $data, $filter_users, $filter_ts_from, $filter_ts_to, $url_params,
609 $drop_image_or_text, 'dmlog', __('Data manipulation statement'),
610 $ddlog_count, 'dml_versions'
613 return $html;
617 * Function to get html for one data manipulation statement
619 * @param array $entry entry
620 * @param array $filter_users filter users
621 * @param int $filter_ts_from filter time stamp from
622 * @param int $filter_ts_to filter time stamp to
623 * @param string $style style
624 * @param int $line_number line number
625 * @param array $url_params url parameters
626 * @param int $offset line number offset
627 * @param string $drop_image_or_text drop image or text
628 * @param string $delete_param parameter for delete
630 * @return string
632 function PMA_getHtmlForOneStatement($entry, $filter_users,
633 $filter_ts_from, $filter_ts_to, $style, $line_number, $url_params, $offset,
634 $drop_image_or_text, $delete_param
636 $statement = PMA_Util::formatSql($entry['statement'], true);
637 $timestamp = strtotime($entry['date']);
638 $filtered_user = in_array($entry['username'], $filter_users);
639 $html = null;
641 if ($timestamp >= $filter_ts_from
642 && $timestamp <= $filter_ts_to
643 && (in_array('*', $filter_users) || $filtered_user)
645 $html = '<tr class="noclick ' . $style . '">';
646 $html .= '<td><small>' . $line_number . '</small></td>';
647 $html .= '<td><small>'
648 . htmlspecialchars($entry['date']) . '</small></td>';
649 $html .= '<td><small>'
650 . htmlspecialchars($entry['username']) . '</small></td>';
651 $html .= '<td>' . $statement . '</td>';
652 $html .= '<td class="nowrap"><a href="tbl_tracking.php'
653 . PMA_URL_getCommon(
654 $url_params + array(
655 'report' => 'true',
656 'version' => $_REQUEST['version'],
657 $delete_param => ($line_number - $offset),
660 . '">'
661 . $drop_image_or_text
662 . '</a></td>';
663 $html .= '</tr>';
666 return $html;
669 * Function to get html for data definition statements in schema snapshot
671 * @param array $data data
672 * @param array $filter_users filter users
673 * @param int $filter_ts_from filter time stamp from
674 * @param int $filter_ts_to filter time stamp to
675 * @param array $url_params url parameters
676 * @param string $drop_image_or_text drop image or text
678 * @return array
680 function PMA_getHtmlForDataDefinitionStatements($data, $filter_users,
681 $filter_ts_from, $filter_ts_to, $url_params, $drop_image_or_text
683 list($html, $line_number) = PMA_getHtmlForDataStatements(
684 $data, $filter_users, $filter_ts_from, $filter_ts_to, $url_params,
685 $drop_image_or_text, 'ddlog', __('Data definition statement'),
686 1, 'ddl_versions'
689 return array($html, $line_number);
693 * Function to get html for data statements in schema snapshot
695 * @param array $data data
696 * @param array $filter_users filter users
697 * @param int $filter_ts_from filter time stamp from
698 * @param int $filter_ts_to filter time stamp to
699 * @param array $url_params url parameters
700 * @param string $drop_image_or_text drop image or text
701 * @param string $which_log dmlog|ddlog
702 * @param string $header_message message for this section
703 * @param int $line_number line number
704 * @param string $table_id id for the table element
706 * @return array
708 function PMA_getHtmlForDataStatements($data, $filter_users,
709 $filter_ts_from, $filter_ts_to, $url_params, $drop_image_or_text,
710 $which_log, $header_message, $line_number, $table_id
712 $offset = $line_number;
713 $html = '<table id="' . $table_id . '" class="data" width="100%">';
714 $html .= '<thead>';
715 $html .= '<tr>';
716 $html .= '<th width="18">#</th>';
717 $html .= '<th width="100">' . __('Date') . '</th>';
718 $html .= '<th width="60">' . __('Username') . '</th>';
719 $html .= '<th>' . $header_message . '</th>';
720 $html .= '<th>' . __('Delete') . '</th>';
721 $html .= '</tr>';
722 $html .= '</thead>';
723 $html .= '<tbody>';
725 $style = 'odd';
726 foreach ($data[$which_log] as $entry) {
727 $html .= PMA_getHtmlForOneStatement(
728 $entry, $filter_users, $filter_ts_from, $filter_ts_to, $style,
729 $line_number, $url_params, $offset, $drop_image_or_text,
730 'delete_' . $which_log
732 if ($style == 'even') {
733 $style = 'odd';
734 } else {
735 $style = 'even';
737 $line_number++;
739 $html .= '</tbody>';
740 $html .= '</table>';
742 return array($html, $line_number);
746 * Function to get html for schema snapshot
748 * @param string $url_query url query
750 * @return string
752 function PMA_getHtmlForSchemaSnapshot($url_query)
754 $html = '<h3>' . __('Structure snapshot')
755 . ' [<a href="tbl_tracking.php' . $url_query . '">' . __('Close')
756 . '</a>]</h3>';
757 $data = PMA_Tracker::getTrackedData(
758 $_REQUEST['db'], $_REQUEST['table'], $_REQUEST['version']
761 // Get first DROP TABLE/VIEW and CREATE TABLE/VIEW statements
762 $drop_create_statements = $data['ddlog'][0]['statement'];
764 if (/*overload*/mb_strstr($data['ddlog'][0]['statement'], 'DROP TABLE')
765 || /*overload*/mb_strstr($data['ddlog'][0]['statement'], 'DROP VIEW')
767 $drop_create_statements .= $data['ddlog'][1]['statement'];
769 // Print SQL code
770 $html .= PMA_Util::getMessage(
771 sprintf(
772 __('Version %s snapshot (SQL code)'),
773 htmlspecialchars($_REQUEST['version'])
775 $drop_create_statements
778 // Unserialize snapshot
779 $temp = unserialize($data['schema_snapshot']);
780 $columns = $temp['COLUMNS'];
781 $indexes = $temp['INDEXES'];
782 $html .= PMA_getHtmlForColumns($columns);
784 if (count($indexes) > 0) {
785 $html .= PMA_getHtmlForIndexes($indexes);
786 } // endif
787 $html .= '<br /><hr /><br />';
789 return $html;
793 * Function to get html for displaying columns in the schema snapshot
795 * @param array $columns columns
797 * @return string
799 function PMA_getHtmlForColumns($columns)
801 $html = '<h3>' . __('Structure') . '</h3>';
802 $html .= '<table id="tablestructure" class="data">';
803 $html .= '<thead>';
804 $html .= '<tr>';
805 $html .= '<th>' . __('Column') . '</th>';
806 $html .= '<th>' . __('Type') . '</th>';
807 $html .= '<th>' . __('Collation') . '</th>';
808 $html .= '<th>' . __('Null') . '</th>';
809 $html .= '<th>' . __('Default') . '</th>';
810 $html .= '<th>' . __('Extra') . '</th>';
811 $html .= '<th>' . __('Comment') . '</th>';
812 $html .= '</tr>';
813 $html .= '</thead>';
814 $html .= '<tbody>';
815 $style = 'odd';
816 foreach ($columns as $field) {
817 $html .= PMA_getHtmlForField($field, $style);
818 if ($style == 'even') {
819 $style = 'odd';
820 } else {
821 $style = 'even';
825 $html .= '</tbody>';
826 $html .= '</table>';
828 return $html;
832 * Function to get html for field
834 * @param array $field field
835 * @param string $style style
837 * @return string
839 function PMA_getHtmlForField($field, $style)
841 $html = '<tr class="noclick ' . $style . '">';
842 $html .= '<td><b>' . htmlspecialchars($field['Field']);
843 if ($field['Key'] == 'PRI') {
844 $html .= ' ' . PMA_Util::getImage(
845 'b_primary.png', __('Primary')
848 $html .= '</b></td>';
849 $html .= "\n";
850 $html .= '<td>' . htmlspecialchars($field['Type']) . '</td>';
851 $html .= '<td>' . htmlspecialchars($field['Collation']) . '</td>';
852 $html .= '<td>' . (($field['Null'] == 'YES') ? __('Yes') : __('No')) . '</td>';
853 $html .= '<td>';
854 if (isset($field['Default'])) {
855 $extracted_columnspec = PMA_Util::extractColumnSpec($field['Type']);
856 if ($extracted_columnspec['type'] == 'bit') {
857 // here, $field['Default'] contains something like b'010'
858 $html .= PMA_Util::convertBitDefaultValue($field['Default']);
859 } else {
860 $html .= htmlspecialchars($field['Default']);
862 } else {
863 if ($field['Null'] == 'YES') {
864 $html .= '<i>NULL</i>';
865 } else {
866 $html .= '<i>' . _pgettext('None for default', 'None') . '</i>';
869 $html .= '</td>';
870 $html .= '<td>' . htmlspecialchars($field['Extra']) . '</td>';
871 $html .= '<td>' . htmlspecialchars($field['Comment']) . '</td>';
872 $html .= '</tr>';
874 return $html;
878 * Function to get html for the indexes in schema snapshot
880 * @param array $indexes indexes
882 * @return string
884 function PMA_getHtmlForIndexes($indexes)
886 $html = '<h3>' . __('Indexes') . '</h3>';
887 $html .= '<table id="tablestructure_indexes" class="data">';
888 $html .= '<thead>';
889 $html .= '<tr>';
890 $html .= '<th>' . __('Keyname') . '</th>';
891 $html .= '<th>' . __('Type') . '</th>';
892 $html .= '<th>' . __('Unique') . '</th>';
893 $html .= '<th>' . __('Packed') . '</th>';
894 $html .= '<th>' . __('Column') . '</th>';
895 $html .= '<th>' . __('Cardinality') . '</th>';
896 $html .= '<th>' . __('Collation') . '</th>';
897 $html .= '<th>' . __('Null') . '</th>';
898 $html .= '<th>' . __('Comment') . '</th>';
899 $html .= '</tr>';
900 $html .= '<tbody>';
902 $style = 'odd';
903 foreach ($indexes as $index) {
904 $html .= PMA_getHtmlForIndex($index, $style);
905 if ($style == 'even') {
906 $style = 'odd';
907 } else {
908 $style = 'even';
911 $html .= '</tbody>';
912 $html .= '</table>';
913 return $html;
917 * Function to get html for an index in schema snapshot
919 * @param array $index index
920 * @param string $style style
922 * @return string
924 function PMA_getHtmlForIndex($index, $style)
926 if ($index['Non_unique'] == 0) {
927 $str_unique = __('Yes');
928 } else {
929 $str_unique = __('No');
931 if ($index['Packed'] != '') {
932 $str_packed = __('Yes');
933 } else {
934 $str_packed = __('No');
937 $html = '<tr class="noclick ' . $style . '">';
938 $html .= '<td><b>' . htmlspecialchars($index['Key_name']) . '</b></td>';
939 $html .= '<td>' . htmlspecialchars($index['Index_type']) . '</td>';
940 $html .= '<td>' . $str_unique . '</td>';
941 $html .= '<td>' . $str_packed . '</td>';
942 $html .= '<td>' . htmlspecialchars($index['Column_name']) . '</td>';
943 $html .= '<td>' . htmlspecialchars($index['Cardinality']) . '</td>';
944 $html .= '<td>' . htmlspecialchars($index['Collation']) . '</td>';
945 $html .= '<td>' . htmlspecialchars($index['Null']) . '</td>';
946 $html .= '<td>' . htmlspecialchars($index['Comment']) . '</td>';
947 $html .= '</tr>';
949 return $html;
953 * Function to handle the tracking report
955 * @param array &$data tracked data
957 * @return string HTML for the message
959 function PMA_deleteTrackingReportRows(&$data)
961 $html = '';
962 if (isset($_REQUEST['delete_ddlog'])) {
963 // Delete ddlog row data
964 $html .= PMA_handleDeleteDataDefinitionsLog($data);
967 if (isset($_REQUEST['delete_dmlog'])) {
968 // Delete dmlog row data
969 $html .= PMA_handleDeleteDataManipulationLog($data);
971 return $html;
975 * Function to handle the delete ddlog row data
977 * @param array &$data tracked data
979 * @return string HTML for the message
981 function PMA_handleDeleteDataDefinitionsLog(&$data)
983 $html = '';
984 $delete_id = $_REQUEST['delete_ddlog'];
986 // Only in case of valable id
987 if ($delete_id == (int)$delete_id) {
988 unset($data['ddlog'][$delete_id]);
990 $successfullyDeleted = PMA_Tracker::changeTrackingData(
991 $_REQUEST['db'], $_REQUEST['table'],
992 $_REQUEST['version'], 'DDL', $data['ddlog']
994 if ($successfullyDeleted) {
995 $msg = PMA_Message::success(
996 __('Tracking data definition successfully deleted')
998 } else {
999 $msg = PMA_Message::rawError(__('Query error'));
1001 $html .= $msg->getDisplay();
1003 return $html;
1007 * Function to handle the delete of fmlog rows
1009 * @param array &$data tracked data
1011 * @return string HTML for the message
1013 function PMA_handleDeleteDataManipulationLog(&$data)
1015 $html = '';
1016 $delete_id = $_REQUEST['delete_dmlog'];
1018 // Only in case of valable id
1019 if ($delete_id == (int)$delete_id) {
1020 unset($data['dmlog'][$delete_id]);
1022 $successfullyDeleted = PMA_Tracker::changeTrackingData(
1023 $_REQUEST['db'], $_REQUEST['table'],
1024 $_REQUEST['version'], 'DML', $data['dmlog']
1026 if ($successfullyDeleted) {
1027 $msg = PMA_Message::success(
1028 __('Tracking data manipulation successfully deleted')
1030 } else {
1031 $msg = PMA_Message::rawError(__('Query error'));
1033 $html .= $msg->getDisplay();
1035 return $html;
1039 * Function to export as sql dump
1041 * @param array $entries entries
1043 * @return string HTML SQL query form
1045 function PMA_exportAsSQLDump($entries)
1047 $html = '';
1048 $new_query = "# "
1049 . __(
1050 'You can execute the dump by creating and using a temporary database. '
1051 . 'Please ensure that you have the privileges to do so.'
1053 . "\n"
1054 . "# " . __('Comment out these two lines if you do not need them.') . "\n"
1055 . "\n"
1056 . "CREATE database IF NOT EXISTS pma_temp_db; \n"
1057 . "USE pma_temp_db; \n"
1058 . "\n";
1060 foreach ($entries as $entry) {
1061 $new_query .= $entry['statement'];
1063 $msg = PMA_Message::success(
1064 __('SQL statements exported. Please copy the dump or execute it.')
1066 $html .= $msg->getDisplay();
1067 $db_temp = $GLOBALS['db'];
1068 $table_temp = $GLOBALS['table'];
1070 $GLOBALS['db'] = $GLOBALS['table'] = '';
1071 include_once './libraries/sql_query_form.lib.php';
1073 $html .= PMA_getHtmlForSqlQueryForm($new_query, 'sql');
1075 $GLOBALS['db'] = $db_temp;
1076 $GLOBALS['table'] = $table_temp;
1078 return $html;
1082 * Function to export as sql execution
1084 * @param array $entries entries
1086 * @return array
1088 function PMA_exportAsSQLExecution($entries)
1090 $sql_result = array();
1091 foreach ($entries as $entry) {
1092 $sql_result = $GLOBALS['dbi']->query("/*NOTRACK*/\n" . $entry['statement']);
1095 return $sql_result;
1099 * Function to export as entries
1101 * @param array $entries entries
1103 * @return void
1105 function PMA_exportAsFileDownload($entries)
1107 @ini_set('url_rewriter.tags', '');
1109 $dump = "# " . sprintf(
1110 __('Tracking report for table `%s`'), htmlspecialchars($_REQUEST['table'])
1112 . "\n" . "# " . date('Y-m-d H:i:s') . "\n";
1113 foreach ($entries as $entry) {
1114 $dump .= $entry['statement'];
1116 $filename = 'log_' . htmlspecialchars($_REQUEST['table']) . '.sql';
1117 PMA_Response::getInstance()->disable();
1118 PMA_downloadHeader(
1119 $filename,
1120 'text/x-sql',
1121 /*overload*/mb_strlen($dump)
1123 echo $dump;
1125 exit();
1129 * Function to activate tracking
1131 * @return string HTML for the success message
1133 function PMA_activateTracking()
1135 $html = '';
1136 $activated = PMA_Tracker::activateTracking(
1137 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version']
1139 if ($activated) {
1140 $msg = PMA_Message::success(
1141 sprintf(
1142 __('Tracking for %1$s was activated at version %2$s.'),
1143 htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table']),
1144 htmlspecialchars($_REQUEST['version'])
1147 $html .= $msg->getDisplay();
1150 return $html;
1154 * Function to deactivate tracking
1156 * @return string HTML of the success message
1158 function PMA_deactivateTracking()
1160 $html = '';
1161 $deactivated = PMA_Tracker::deactivateTracking(
1162 $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version']
1164 if ($deactivated) {
1165 $msg = PMA_Message::success(
1166 sprintf(
1167 __('Tracking for %1$s was deactivated at version %2$s.'),
1168 htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table']),
1169 htmlspecialchars($_REQUEST['version'])
1172 $html .= $msg->getDisplay();
1175 return $html;
1179 * Function to get tracking set
1181 * @return string
1183 function PMA_getTrackingSet()
1185 $tracking_set = '';
1187 // a key is absent from the request if it has been removed from
1188 // tracking_default_statements in the config
1189 if (isset($_REQUEST['alter_table']) && $_REQUEST['alter_table'] == true) {
1190 $tracking_set .= 'ALTER TABLE,';
1192 if (isset($_REQUEST['rename_table']) && $_REQUEST['rename_table'] == true) {
1193 $tracking_set .= 'RENAME TABLE,';
1195 if (isset($_REQUEST['create_table']) && $_REQUEST['create_table'] == true) {
1196 $tracking_set .= 'CREATE TABLE,';
1198 if (isset($_REQUEST['drop_table']) && $_REQUEST['drop_table'] == true) {
1199 $tracking_set .= 'DROP TABLE,';
1201 if (isset($_REQUEST['create_index']) && $_REQUEST['create_index'] == true) {
1202 $tracking_set .= 'CREATE INDEX,';
1204 if (isset($_REQUEST['drop_index']) && $_REQUEST['drop_index'] == true) {
1205 $tracking_set .= 'DROP INDEX,';
1207 if (isset($_REQUEST['insert']) && $_REQUEST['insert'] == true) {
1208 $tracking_set .= 'INSERT,';
1210 if (isset($_REQUEST['update']) && $_REQUEST['update'] == true) {
1211 $tracking_set .= 'UPDATE,';
1213 if (isset($_REQUEST['delete']) && $_REQUEST['delete'] == true) {
1214 $tracking_set .= 'DELETE,';
1216 if (isset($_REQUEST['truncate']) && $_REQUEST['truncate'] == true) {
1217 $tracking_set .= 'TRUNCATE,';
1219 $tracking_set = rtrim($tracking_set, ',');
1221 return $tracking_set;
1225 * Function to create the tracking version
1227 * @return string HTML of the success message
1229 function PMA_createTrackingVersion()
1231 $html = '';
1232 $tracking_set = PMA_getTrackingSet();
1234 $versionCreated = PMA_Tracker::createVersion(
1235 $GLOBALS['db'],
1236 $GLOBALS['table'],
1237 $_REQUEST['version'],
1238 $tracking_set,
1239 PMA_Table::isView($GLOBALS['db'], $GLOBALS['table'])
1241 if ($versionCreated) {
1242 $msg = PMA_Message::success(
1243 sprintf(
1244 __('Version %1$s was created, tracking for %2$s is active.'),
1245 htmlspecialchars($_REQUEST['version']),
1246 htmlspecialchars($GLOBALS['db'] . '.' . $GLOBALS['table'])
1249 $html .= $msg->getDisplay();
1252 return $html;
1256 * Function to get the entries
1258 * @param array $data data
1259 * @param int $filter_ts_from filter time stamp from
1260 * @param int $filter_ts_to filter time stamp to
1261 * @param array $filter_users filter users
1263 * @return array
1265 function PMA_getEntries($data, $filter_ts_from, $filter_ts_to, $filter_users)
1267 $entries = array();
1268 // Filtering data definition statements
1269 if ($_REQUEST['logtype'] == 'schema'
1270 || $_REQUEST['logtype'] == 'schema_and_data'
1272 $entries = array_merge(
1273 $entries,
1274 PMA_filterTracking(
1275 $data['ddlog'], $filter_ts_from, $filter_ts_to, $filter_users
1280 // Filtering data manipulation statements
1281 if ($_REQUEST['logtype'] == 'data'
1282 || $_REQUEST['logtype'] == 'schema_and_data'
1284 $entries = array_merge(
1285 $entries,
1286 PMA_filterTracking(
1287 $data['dmlog'], $filter_ts_from, $filter_ts_to, $filter_users
1292 // Sort it
1293 $ids = $timestamps = $usernames = $statements = array();
1294 foreach ($entries as $key => $row) {
1295 $ids[$key] = $row['id'];
1296 $timestamps[$key] = $row['timestamp'];
1297 $usernames[$key] = $row['username'];
1298 $statements[$key] = $row['statement'];
1301 array_multisort(
1302 $timestamps, SORT_ASC, $ids, SORT_ASC, $usernames,
1303 SORT_ASC, $statements, SORT_ASC, $entries
1306 return $entries;
1310 * Function to get version status
1312 * @param array $version version info
1314 * @return string $version_status The status message
1316 function PMA_getVersionStatus($version)
1318 if ($version['tracking_active'] == 1) {
1319 return __('active');
1320 } else {
1321 return __('not active');