2 /* vim: set expandtab sw=4 ts=4 sts=4: */
9 require_once './libraries/common.inc.php';
11 define('TABLE_MAY_BE_ABSENT', true);
12 require './libraries/tbl_common.php';
13 $url_query .= '&goto=tbl_tracking.php&back=tbl_tracking.php';
14 $url_params['goto'] = 'tbl_tracking.php';;
15 $url_params['back'] = 'tbl_tracking.php';
17 // Init vars for tracking report
18 if (isset($_REQUEST['report']) ||
isset($_REQUEST['report_export'])) {
19 $data = PMA_Tracker
::getTrackedData($_REQUEST['db'], $_REQUEST['table'], $_REQUEST['version']);
21 $selection_schema = false;
22 $selection_data = false;
23 $selection_both = false;
25 if (! isset($_REQUEST['logtype'])) {
26 $_REQUEST['logtype'] = 'schema_and_data';
28 if ($_REQUEST['logtype'] == 'schema') {
29 $selection_schema = true;
30 } elseif($_REQUEST['logtype'] == 'data') {
31 $selection_data = true;
33 $selection_both = true;
35 if (! isset($_REQUEST['date_from'])) {
36 $_REQUEST['date_from'] = $data['date_from'];
38 if (! isset($_REQUEST['date_to'])) {
39 $_REQUEST['date_to'] = $data['date_to'];
41 if (! isset($_REQUEST['users'])) {
42 $_REQUEST['users'] = '*';
44 $filter_ts_from = strtotime($_REQUEST['date_from']);
45 $filter_ts_to = strtotime($_REQUEST['date_to']);
46 $filter_users = array_map('trim', explode(',', $_REQUEST['users']));
50 if (isset($_REQUEST['report_export'])) {
53 * Filters tracking entries
55 * @param array the entries to filter
56 * @param string "from" date
57 * @param string "to" date
60 * @return array filtered entries
63 function PMA_filter_tracking($data, $filter_ts_from, $filter_ts_to, $filter_users) {
64 $tmp_entries = array();
66 foreach( $data as $entry ) {
67 $timestamp = strtotime($entry['date']);
69 if ($timestamp >= $filter_ts_from && $timestamp <= $filter_ts_to &&
70 ( in_array('*', $filter_users) ||
in_array($entry['username'], $filter_users) ) ) {
71 $tmp_entries[] = array( 'id' => $id,
72 'timestamp' => $timestamp,
73 'username' => $entry['username'],
74 'statement' => $entry['statement']
83 // Filtering data definition statements
84 if ($_REQUEST['logtype'] == 'schema' ||
$_REQUEST['logtype'] == 'schema_and_data') {
85 $entries = array_merge($entries, PMA_filter_tracking($data['ddlog'], $filter_ts_from, $filter_ts_to, $filter_users));
88 // Filtering data manipulation statements
89 if ($_REQUEST['logtype'] == 'data' ||
$_REQUEST['logtype'] == 'schema_and_data') {
90 $entries = array_merge($entries, PMA_filter_tracking($data['dmlog'], $filter_ts_from, $filter_ts_to, $filter_users));
94 foreach ($entries as $key => $row) {
95 $ids[$key] = $row['id'];
96 $timestamps[$key] = $row['timestamp'];
97 $usernames[$key] = $row['username'];
98 $statements[$key] = $row['statement'];
101 array_multisort($timestamps, SORT_ASC
, $ids, SORT_ASC
, $usernames, SORT_ASC
, $statements, SORT_ASC
, $entries);
105 // Export as file download
106 if (isset($_REQUEST['report_export']) && $_REQUEST['export_type'] == 'sqldumpfile') {
107 @ini_set
('url_rewriter.tags','');
109 $dump = "# " . sprintf(__('Tracking report for table `%s`'), htmlspecialchars($_REQUEST['table'])) . "\n" .
110 "# " . date('Y-m-d H:i:s') . "\n";
111 foreach($entries as $entry) {
112 $dump .= $entry['statement'];
114 $filename = 'log_' . htmlspecialchars($_REQUEST['table']) . '.sql';
115 header('Content-Type: text/x-sql');
116 header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
117 header('Content-Disposition: attachment; filename="' . $filename . '"');
118 if (PMA_USR_BROWSER_AGENT
== 'IE') {
119 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
120 header('Pragma: public');
122 header('Pragma: no-cache');
123 header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
132 * Gets tables informations
136 * Displays top menu links
138 require_once './libraries/tbl_links.inc.php';
145 // Create tracking version
146 if (isset($_REQUEST['submit_create_version'])) {
149 if ($_REQUEST['alter_table'] == true) {
150 $tracking_set .= 'ALTER TABLE,';
152 if ($_REQUEST['rename_table'] == true) {
153 $tracking_set .= 'RENAME TABLE,';
155 if ($_REQUEST['create_table'] == true) {
156 $tracking_set .= 'CREATE TABLE,';
158 if ($_REQUEST['drop_table'] == true) {
159 $tracking_set .= 'DROP TABLE,';
161 if ($_REQUEST['create_index'] == true) {
162 $tracking_set .= 'CREATE INDEX,';
164 if ($_REQUEST['drop_index'] == true) {
165 $tracking_set .= 'DROP INDEX,';
167 if ($_REQUEST['insert'] == true) {
168 $tracking_set .= 'INSERT,';
170 if ($_REQUEST['update'] == true) {
171 $tracking_set .= 'UPDATE,';
173 if ($_REQUEST['delete'] == true) {
174 $tracking_set .= 'DELETE,';
176 if ($_REQUEST['truncate'] == true) {
177 $tracking_set .= 'TRUNCATE,';
179 $tracking_set = rtrim($tracking_set, ',');
181 if (PMA_Tracker
::createVersion($GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version'], $tracking_set )) {
182 $msg = PMA_Message
::success(sprintf(__('Version %s is created, tracking for %s.%s is activated.'), $_REQUEST['version'], $GLOBALS['db'], $GLOBALS['table']));
187 // Deactivate tracking
188 if (isset($_REQUEST['submit_deactivate_now'])) {
189 if (PMA_Tracker
::deactivateTracking($GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version'])) {
190 $msg = PMA_Message
::success(sprintf(__('Tracking for %s.%s , version %s is deactivated.'), $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version']));
196 if (isset($_REQUEST['submit_activate_now'])) {
197 if (PMA_Tracker
::activateTracking($GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version'])) {
198 $msg = PMA_Message
::success(sprintf(__('Tracking for %s.%s , version %s is activated.'), $GLOBALS['db'], $GLOBALS['table'], $_REQUEST['version']));
203 // Export as SQL execution
204 if (isset($_REQUEST['report_export']) && $_REQUEST['export_type'] == 'execution') {
205 foreach($entries as $entry) {
206 $sql_result = PMA_DBI_query( "/*NOTRACK*/\n" . $entry['statement'] );
208 $msg = PMA_Message
::success(__('SQL statements executed.'));
212 // Export as SQL dump
213 if (isset($_REQUEST['report_export']) && $_REQUEST['export_type'] == 'sqldump')
215 $new_query = "# " . __('You can execute the dump by creating and using a temporary database. Please ensure that you have the privileges to do so.') . "\n" .
216 "# " . __('Comment out these two lines if you do not need them.') . "\n" .
218 "CREATE database IF NOT EXISTS pma_temp_db; \n" .
219 "USE pma_temp_db; \n" .
222 foreach($entries as $entry) {
223 $new_query .= $entry['statement'];
225 $msg = PMA_Message
::success(__('SQL statements exported. Please copy the dump or execute it.'));
229 $table_temp = $table;
232 require_once './libraries/sql_query_form.lib.php';
234 PMA_sqlQueryForm($new_query, 'sql');
237 $table = $table_temp;
243 if (isset($_REQUEST['snapshot'])) {
245 <h3
><?php
echo __('Structure snapshot');?
> [<a href
="tbl_tracking.php?<?php echo $url_query;?>"><?php
echo __('Close');?
></a
>]</h3
>
247 $data = PMA_Tracker
::getTrackedData($_REQUEST['db'], $_REQUEST['table'], $_REQUEST['version']);
249 // Get first DROP TABLE and CREATE TABLE statements
250 $drop_create_statements = $data['ddlog'][0]['statement'];
252 if (strstr($data['ddlog'][0]['statement'], 'DROP TABLE')) {
253 $drop_create_statements .= $data['ddlog'][1]['statement'];
256 PMA_showMessage(sprintf(__('Version %s snapshot (SQL code)'), $_REQUEST['version']), $drop_create_statements);
258 // Unserialize snapshot
259 $temp = unserialize($data['schema_snapshot']);
260 $columns = $temp['COLUMNS'];
261 $indexes = $temp['INDEXES'];
263 <h3
><?php
echo __('Structure');?
></h3
>
264 <table id
="tablestructure" class="data">
267 <th
><?php
echo __('Column'); ?
></th
>
268 <th
><?php
echo __('Type'); ?
></th
>
269 <th
><?php
echo __('Collation'); ?
></th
>
270 <th
><?php
echo __('Null'); ?
></th
>
271 <th
><?php
echo __('Default'); ?
></th
>
272 <th
><?php
echo __('Extra'); ?
></th
>
273 <th
><?php
echo __('Comment'); ?
></th
>
279 foreach($columns as $field_index => $field) {
281 <tr
class="<?php echo $style; ?>">
283 if ($field['Key'] == 'PRI') {
284 echo '<td><b><u>' . $field['Field'] . '</u></b></td>' . "\n";
286 echo '<td><b>' . $field['Field'] . '</b></td>' . "\n";
289 <td
><?php
echo $field['Type'];?
></td
>
290 <td
><?php
echo $field['Collation'];?
></td
>
291 <td
><?php
echo $field['Null'];?
></td
>
292 <td
><?php
echo $field['Default'];?
></td
>
293 <td
><?php
echo $field['Extra'];?
></td
>
294 <td
><?php
echo $field['Comment'];?
></td
>
297 if ($style == 'even') {
308 if (count($indexes) > 0) {
310 <h3
><?php
echo __('Indexes');?
></h3
>
311 <table id
="tablestructure_indexes" class="data">
314 <th
><?php
echo __('Keyname');?
></th
>
315 <th
><?php
echo __('Type');?
></th
>
316 <th
><?php
echo __('Unique');?
></th
>
317 <th
><?php
echo __('Packed');?
></th
>
318 <th
><?php
echo __('Column');?
></th
>
319 <th
><?php
echo __('Cardinality');?
></th
>
320 <th
><?php
echo __('Collation');?
></th
>
321 <th
><?php
echo __('Null');?
></th
>
322 <th
><?php
echo __('Comment');?
></th
>
327 foreach ($indexes as $indexes_index => $index) {
328 if ($index['Non_unique'] == 0) {
329 $str_unique = __('Yes');
331 $str_unique = __('No');
333 if ($index['Packed'] != '') {
334 $str_packed = __('Yes');
336 $str_packed = __('No');
339 <tr
class="<?php echo $style; ?>">
340 <td
><b
><?php
echo $index['Key_name'];?
></b
></td
>
341 <td
><?php
echo $index['Index_type'];?
></td
>
342 <td
><?php
echo $str_unique;?
></td
>
343 <td
><?php
echo $str_packed;?
></td
>
344 <td
><?php
echo $index['Column_name'];?
></td
>
345 <td
><?php
echo $index['Cardinality'];?
></td
>
346 <td
><?php
echo $index['Collation'];?
></td
>
347 <td
><?php
echo $index['Null'];?
></td
>
348 <td
><?php
echo $index['Comment'];?
></td
>
351 if ($style == 'even') {
366 // end of snapshot report
371 if (isset($_REQUEST['report']) ||
isset($_REQUEST['report_export'])) {
373 <h3
><?php
echo __('Tracking report');?
> [<a href
="tbl_tracking.php?<?php echo $url_query;?>"><?php
echo __('Close');?
></a
>]</h3
>
375 <small
><?php
echo __('Tracking statements') . ' ' . $data['tracking']; ?
></small
><br
/>
378 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>&report=true&version=<?php echo $_REQUEST['version'];?>">
381 $str1 = '<select name="logtype">' .
382 '<option value="schema"' . ($selection_schema ?
' selected="selected"' : '') . '>' . __('Structure only') . '</option>' .
383 '<option value="data"' . ($selection_data ?
' selected="selected"' : ''). '>' . __('Data only') . '</option>' .
384 '<option value="schema_and_data"' . ($selection_both ?
' selected="selected"' : '') . '>' . __('Structure and data') . '</option>' .
386 $str2 = '<input type="text" name="date_from" value="' . $_REQUEST['date_from'] . '" size="19" />';
387 $str3 = '<input type="text" name="date_to" value="' . $_REQUEST['date_to'] . '" size="19" />';
388 $str4 = '<input type="text" name="users" value="' . $_REQUEST['users'] . '" />';
389 $str5 = '<input type="submit" name="list_report" value="' . __('Go') . '" />';
391 printf(__('Show %s with dates from %s to %s by user %s %s'), $str1, $str2, $str3, $str4, $str5);
395 * First, list tracked data definition statements
398 if ($selection_schema ||
$selection_both ) {
400 <table id
="ddl_versions" class="data" width
="100%">
403 <th width
="18">#</th>
404 <th width
="100"><?php
echo __('Date');?
></th
>
405 <th width
="60"><?php
echo __('Username');?
></th
>
406 <th
><?php
echo __('Data definition statement');?
></th
>
412 foreach ($data['ddlog'] as $entry) {
413 if (strlen($entry['statement']) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
414 $statement = substr($entry['statement'], 0, $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) . '[...]';
416 $statement = PMA_formatSql(PMA_SQP_parse($entry['statement']));
418 $timestamp = strtotime($entry['date']);
420 if ($timestamp >= $filter_ts_from && $timestamp <= $filter_ts_to &&
421 ( in_array('*', $filter_users) ||
in_array($entry['username'], $filter_users) ) ) {
423 <tr
class="<?php echo $style; ?>">
424 <td
><small
><?php
echo $i;?
></small
></td
>
425 <td
><small
><?php
echo $entry['date'];?
></small
></td
>
426 <td
><small
><?php
echo $entry['username']; ?
></small
></td
>
427 <td
><?php
echo $statement; ?
></td
>
430 if ($style == 'even') {
446 * Secondly, list tracked data manipulation statements
449 if (($selection_data ||
$selection_both) && count($data['dmlog']) > 0) {
451 <table id
="dml_versions" class="data" width
="100%">
454 <th width
="18">#</th>
455 <th width
="100"><?php
echo __('Date');?
></th
>
456 <th width
="60"><?php
echo __('Username');?
></th
>
457 <th
><?php
echo __('Data manipulation statement');?
></th
>
463 foreach ($data['dmlog'] as $entry) {
464 if (strlen($entry['statement']) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
465 $statement = substr($entry['statement'], 0, $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) . '[...]';
467 $statement = PMA_formatSql(PMA_SQP_parse($entry['statement']));
469 $timestamp = strtotime($entry['date']);
471 if ($timestamp >= $filter_ts_from && $timestamp <= $filter_ts_to &&
472 ( in_array('*', $filter_users) ||
in_array($entry['username'], $filter_users) ) ) {
474 <tr
class="<?php echo $style; ?>">
475 <td
><small
><?php
echo $i; ?
></small
></td
>
476 <td
><small
><?php
echo $entry['date']; ?
></small
></td
>
477 <td
><small
><?php
echo $entry['username']; ?
></small
></td
>
478 <td
><?php
echo $statement; ?
></td
>
481 if ($style == 'even') {
496 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>&report=true&version=<?php echo $_REQUEST['version'];?>">
498 printf(__('Show %s with dates from %s to %s by user %s %s'), $str1, $str2, $str3, $str4, $str5);
500 $str_export1 = '<select name="export_type">' .
501 '<option value="sqldumpfile">' . __('SQL dump (file download)') . '</option>' .
502 '<option value="sqldump">' . __('SQL dump') . '</option>' .
503 '<option value="execution" onclick="alert(\'' . PMA_escapeJsString(__('This option will replace your table and contained data.')) .'\')">' . __('SQL execution') . '</option>' .
506 $str_export2 = '<input type="submit" name="report_export" value="' . __('Go') .'" />';
509 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>&report=true&version=<?php echo $_REQUEST['version'];?>">
510 <input type
="hidden" name
="logtype" value
="<?php echo $_REQUEST['logtype'];?>" />
511 <input type
="hidden" name
="date_from" value
="<?php echo $_REQUEST['date_from'];?>" />
512 <input type
="hidden" name
="date_to" value
="<?php echo $_REQUEST['date_to'];?>" />
513 <input type
="hidden" name
="users" value
="<?php echo $_REQUEST['users'];?>" />
515 echo "<br/>" . sprintf(__('Export as %s'), $str_export1) . $str_export2 . "<br/>";
519 echo "<br/><br/><hr/><br/>\n";
524 * List selectable tables
527 $sql_query = " SELECT DISTINCT db_name, table_name FROM " .
528 PMA_backquote($GLOBALS['cfg']['Server']['pmadb']) . "." .
529 PMA_backquote($GLOBALS['cfg']['Server']['tracking']) .
530 " WHERE " . PMA_backquote('db_name') . " = '" . PMA_sqlAddslashes($GLOBALS['db']) . "' " .
531 " ORDER BY ". PMA_backquote('db_name') . ", " . PMA_backquote('table_name');
533 $sql_result = PMA_query_as_controluser($sql_query);
535 if (PMA_DBI_num_rows($sql_result) > 0) {
537 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query;?>">
538 <select name
="table">
540 while ($entries = PMA_DBI_fetch_array($sql_result)) {
541 if (PMA_Tracker
::isTracked($entries['db_name'], $entries['table_name'])) {
542 $status = ' (' . __('active') . ')';
544 $status = ' (' . __('not active') . ')';
546 if ($entries['table_name'] == $_REQUEST['table']) {
547 $s = ' selected="selected"';
551 echo '<option value="' . htmlspecialchars($entries['table_name']) . '"' . $s . '>' . htmlspecialchars($entries['db_name']) . ' . ' . htmlspecialchars($entries['table_name']) . $status . '</option>' . "\n";
555 <input type
="submit" name
="show_versions_submit" value
="<?php echo __('Show versions');?>" />
564 * List versions of current table
567 $sql_query = " SELECT * FROM " .
568 PMA_backquote($GLOBALS['cfg']['Server']['pmadb']) . "." .
569 PMA_backquote($GLOBALS['cfg']['Server']['tracking']) .
570 " WHERE " . PMA_backquote('db_name') . " = '" . PMA_sqlAddslashes($_REQUEST['db']) . "' ".
571 " AND " . PMA_backquote('table_name') . " = '" . PMA_sqlAddslashes($_REQUEST['table']) ."' ".
572 " ORDER BY ". PMA_backquote('version') . " DESC ";
574 $sql_result = PMA_query_as_controluser($sql_query);
577 $maxversion = PMA_DBI_fetch_array($sql_result);
578 $last_version = $maxversion['version'];
580 if ($last_version > 0) {
582 <table id
="versions" class="data">
585 <th
><?php
echo __('Database');?
></th
>
586 <th
><?php
echo __('Table');?
></th
>
587 <th
><?php
echo __('Version');?
></th
>
588 <th
><?php
echo __('Created');?
></th
>
589 <th
><?php
echo __('Updated');?
></th
>
590 <th
><?php
echo __('Status');?
></th
>
591 <th
><?php
echo __('Show');?
></th
>
597 PMA_DBI_data_seek($sql_result, 0);
598 while($version = PMA_DBI_fetch_array($sql_result)) {
599 if ($version['tracking_active'] == 1) {
600 $version_status = __('active');
602 $version_status = __('not active');
604 if (($version['version'] == $last_version) && ($version_status == __('not active'))) {
605 $tracking_active = false;
607 if (($version['version'] == $last_version) && ($version_status == __('active'))) {
608 $tracking_active = true;
611 <tr
class="<?php echo $style;?>">
612 <td
><?php
echo htmlspecialchars($version['db_name']);?
></td
>
613 <td
><?php
echo htmlspecialchars($version['table_name']);?
></td
>
614 <td
><?php
echo $version['version'];?
></td
>
615 <td
><?php
echo $version['date_created'];?
></td
>
616 <td
><?php
echo $version['date_updated'];?
></td
>
617 <td
><?php
echo $version_status;?
></td
>
618 <td
> <a href
="tbl_tracking.php?<?php echo $url_query;?>&report=true&version=<?php echo $version['version'];?>"><?php
echo __('Tracking report');?
></a
> |
<a href
="tbl_tracking.php?<?php echo $url_query;?>&snapshot=true&version=<?php echo $version['version'];?>"><?php
echo __('Structure snapshot');?
></a
></td
>
621 if ($style == 'even') {
630 <?php
if ($tracking_active == true) {?
>
631 <div id
="div_deactivate_tracking">
632 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>">
634 <legend
><?php
printf(__('Deactivate tracking for %s.%s'), $GLOBALS['db'], $GLOBALS['table']); ?
></legend
>
635 <input type
="hidden" name
="version" value
="<?php echo $last_version; ?>" />
636 <input type
="submit" name
="submit_deactivate_now" value
="<?php echo __('Deactivate now'); ?>" />
643 <?php
if ($tracking_active == false) {?
>
644 <div id
="div_activate_tracking">
645 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>">
647 <legend
><?php
printf(__('Activate tracking for %s.%s'), $GLOBALS['db'], $GLOBALS['table']); ?
></legend
>
648 <input type
="hidden" name
="version" value
="<?php echo $last_version; ?>" />
649 <input type
="submit" name
="submit_activate_now" value
="<?php echo __('Activate now'); ?>" />
658 <div id
="div_create_version">
659 <form method
="post" action
="tbl_tracking.php?<?php echo $url_query; ?>">
660 <?php
echo PMA_generate_common_hidden_inputs($GLOBALS['db'], $GLOBALS['table']); ?
>
662 <legend
><?php
printf(__('Create version %s of %s.%s'), ($last_version +
1), $GLOBALS['db'], $GLOBALS['table']); ?
></legend
>
664 <input type
="hidden" name
="version" value
="<?php echo ($last_version + 1); ?>" />
666 <p
><?php
echo __('Track these data definition statements:');?
></p
>
667 <input type
="checkbox" name
="alter_table" value
="true" checked
="checked" /> ALTER TABLE
<br
/>
668 <input type
="checkbox" name
="rename_table" value
="true" checked
="checked" /> RENAME TABLE
<br
/>
669 <input type
="checkbox" name
="create_table" value
="true" checked
="checked" /> CREATE TABLE
<br
/>
670 <input type
="checkbox" name
="drop_table" value
="true" checked
="checked" /> DROP TABLE
<br
/>
672 <input type
="checkbox" name
="create_index" value
="true" checked
="checked" /> CREATE INDEX
<br
/>
673 <input type
="checkbox" name
="drop_index" value
="true" checked
="checked" /> DROP INDEX
<br
/>
674 <p
><?php
echo __('Track these data manipulation statements:');?
></p
>
675 <input type
="checkbox" name
="insert" value
="true" checked
="checked" /> INSERT
<br
/>
676 <input type
="checkbox" name
="update" value
="true" checked
="checked" /> UPDATE
<br
/>
677 <input type
="checkbox" name
="delete" value
="true" checked
="checked" /> DELETE
<br
/>
678 <input type
="checkbox" name
="truncate" value
="true" checked
="checked" /> TRUNCATE
<br
/>
681 <fieldset
class="tblFooters">
682 <input type
="submit" name
="submit_create_version" value
="<?php echo __('Create version'); ?>" />
687 <br
class="clearfloat"/>
691 * Displays the footer
693 require './libraries/footer.inc.php';