Xataface 2.0
Xataface Application Framework
Public Member Functions | Data Fields
Dataface_HistoryTool Class Reference

Public Member Functions

 logRecord (&$record, $comments='', $lang=null, $state=null)
 logField (&$record, $fieldname, $history_id)
 logTableName ($tablename)
 updateHistoryTable ($tablename)
 createHistoryTable ($tablename)
 getDiffs ($tablename, $id1, $id2=null, $fieldname=null)
 getDiffsByDate (&$record, $date1, $date2=null, $lang=null, $fieldname=null)
 restore (&$record, $id, $fieldname=null)
 restoreField (&$record, &$old_record, $id, $fieldname)
 restoreToDate (&$record, $date, $lang=null, $fieldname=null)
 getPreviousVersion (&$record, $date, $lang=null, $fieldname=null, $idonly=false)
 getRecordById ($tablename, $id)
 getHistoryLog (&$record, $lang=null, $limit=100)
 findMatchingSnapshots ($record, $query, $idsOnly=true)

Data Fields

 $meta_fields

Detailed Description

Manages the history of records. This class will record the state of each record in the database by copying it to a 'history' table. A separate history table is created for each table in the database.

In addition to storing all of the information in the subject table (the table that is being backed up), the history table stores the language of the record, the username of the user performing the update, some optional state information, a comments field and a modified timestamp to track when the record is from.

This class can be used to restore records to a previous state.

Usage:

$ht = new Dataface_HistoryTool(); $record = df_get_record('Profiles', array('id'=>10)); // get the profile with ID 10. $ht->restoreToDate($record, '2004-5-10'); // restores the record to the way it was on 2004-5-10

History tables are created to follow the following naming convention. The history table for the table named 'Foo' would be called 'Foo__history'. If Foo contains a container field whose files are saved in the folder /path/to/foo then their history files (the backups for the history table) will be stored in /path/to/foo/.dataface_history/ with the file name the same as the history id (The value of the history__id column for that entry.

History is automatically stored if the [history] section is present in the conf.ini file. If it is not, you can manually record the history for a record using the logRecord method as follows: $ht = new Dataface_HistoryTool(); $hid = $ht->logRecord($record); // $hid is the id of the history record.

$historyRecord = $ht->getRecordById($record->_table->tablename, $hid); // obtains the Dataface_Record for the corresponding record of the history // table. // $historyRecord and $record should contain nearly identical values, // except that $historyRecord contains extra history information // like timestamps etc.

You can obtain a previous version of a record without actually restoring the database record to that version using the getPreviousVersion() method: $historyTool = new Dataface_HistoryTool(); $record = df_get_record('Profiles', array('id'=>10)); $old_record = $ht->getPreviousVersion($record, '2004-10-10'); // Now $old_record is a Dataface_Record object for the "Profiles" table //representing the status of $record on 2004-10-10.

Author:
Steve Hannah (shannah@sfu.ca) September 2006

Definition at line 64 of file HistoryTool.php.


Member Function Documentation

createHistoryTable ( tablename)

Creates a history table corresponding to the given table.

Parameters:
string$tablenameThe name of the table whose history table is to be created.

Definition at line 288 of file HistoryTool.php.

findMatchingSnapshots ( record,
query,
idsOnly = true 
)

Returns an array of history ids of history records that match the given query.

Definition at line 676 of file HistoryTool.php.

getDiffs ( tablename,
id1,
id2 = null,
fieldname = null 
)

Gets an HTML diff output between the records at $id1 and $id2 respectively, where $id1 and $id2 are history ids from the history__id column of the history table.

Parameters:
string$tablenameThe name of the base table.
integer$id1The id number of the first record (from the history__id column)
integer$id2The id of the second record (from the history__id column)
string$fieldnameOptional name of a field to return.
Returns:
mixed Either the value of the specified field name if $fieldname is specified, or a Dataface_Record object whose field values are formatted diffs.

Definition at line 335 of file HistoryTool.php.

getDiffsByDate ( &$  record,
date1,
date2 = null,
lang = null,
fieldname = null 
)

Gets the changes for a given record between dates.

Parameters:
Dataface_Record&$recordThe record we are checking for changes on. This is a record of the base table, and not its history table.
string$date1The start date as an SQL date string (e.g. YYYY-MM-DD)
string$date2The end date as an SQL date string (e.g. YYYY-MM-DD)
string$langThe 2-digit language code of the language we are using.
string$fieldnameOptional fieldname
Returns:
mixed Either a Dataface_Record object from the history table, whose values are the formatted diffs - or the formatted diff for the specified $fieldname.

Definition at line 392 of file HistoryTool.php.

getHistoryLog ( &$  record,
lang = null,
limit = 100 
)

Returns an array of the meta fields from the history table in descending order of modified date.

Parameters:
Dataface_Record&$recordThe record we wish to obtain history for.
string$langThe 2-digit language code.
integer$limitThe maximum number of records to return. null for unlimited.
Returns:
Array of Associative arrays.

Definition at line 643 of file HistoryTool.php.

getPreviousVersion ( &$  record,
date,
lang = null,
fieldname = null,
idonly = false 
)

Returns a previous version of the given record.

Parameters:
Dataface_Record&$recordThe record whose previous version we are interested in.
string$dateThe MySQL date to obtain. Returns the most recent record on or before this date. (e.g. YYYY-MM-DD HH:MM:SS)
string$fieldnameIf we only want the previous version of a single field we can include it here.
boolean$idonlyIf this is set to true then only the history id of the appropriate record will be returned.
Returns:
mixed If $idonly is true then an integer is returned corresponding to the value of the history__id field for the matching record. If The $fieldname field was specified then the value of that field will be returned. Otherwise a Dataface_Record will be returned.

Definition at line 576 of file HistoryTool.php.

getRecordById ( tablename,
id 
)

Obtains a record from the history table given the value in the history__id column.

Parameters:
string$tablenameThe name of the base table.
integer$idThe id (history__id column value).
Returns:
Dataface_Record from the history table.

Definition at line 620 of file HistoryTool.php.

logField ( &$  record,
fieldname,
history_id 
)

Performs extra logging steps for fields. This method is called by logRecord(). In particular for container fields, this will copy the container's files into the hidden .dataface_history subdirectory of the container field's save path.

Parameters:
Dataface_Record&$recordThe record whose field is being logged.
string$fieldnameThe name of the field to perform extra steps for.
integer$history_idThe id of the history record for the record that is being logged.

Definition at line 166 of file HistoryTool.php.

logRecord ( &$  record,
comments = '',
lang = null,
state = null 
)

Backs up a record to the history table. This will automatically happen when using Dataface_IO::save() if the [history] section exists in the conf.ini file.

Parameters:
Dataface_Record&$recordThe record that is being backed up.
string$commentsComments about this version to be stored.
string$langThe 2-digit language code of which language to use to back up this record. If none is specified then the current language of the system will be used.
integer$stateUnused as yet. Was intended to store state/workflow information.. but ..
Returns:
integer The history id of the resulting history record.

Definition at line 94 of file HistoryTool.php.

logTableName ( tablename)

Given a table name - this returns the name of the table's history table.

Parameters:
string$tablenameThe name of the table whose history we want.
Returns:
string

Definition at line 226 of file HistoryTool.php.

restore ( &$  record,
id,
fieldname = null 
)

Restores an old version of the given record, from the history table.

Definition at line 461 of file HistoryTool.php.

restoreField ( &$  record,
&$  old_record,
id,
fieldname 
)

Performs extra steps to restore particular fields. Any steps outside of copying the field values form the history table back to the main table. In particular container fields need to have their files copied back from the history subdirectory into the container field's save path.

Parameters:
Dataface_Record&$recordThe record whose field we are restoring.
Dataface_Record&$old_recordThe record as is was before the restore.
integer$idThe History id (The value in the history__id column)
string$fieldnameThe name of the field to restore.

Definition at line 504 of file HistoryTool.php.

restoreToDate ( &$  record,
date,
lang = null,
fieldname = null 
)

Restores a record to a particular date.

Parameters:
Dataface_Record&$recordThe record to be restored.
string$dateThe date to restore to as a MySQL date string (e.g. YYYY-MM-DD HH:MM:SS)
string$langThe 2-digit language code of the version to restore.
string$fieldnameIf we are only restoring a single field we include the name here.

Definition at line 539 of file HistoryTool.php.

updateHistoryTable ( tablename)

Updates the history table for a particular table so that it exists and contains all of the correct columns.

Definition at line 234 of file HistoryTool.php.


Field Documentation

$meta_fields
Initial value:
 array(
                'history__id'=>array('Type'=>'int(11)', 'Extra'=>'auto_increment'),
                        'history__language'=> array('Type'=>'varchar(2)'),
                        'history__comments'=> array('Type'=>'text'),
                        'history__user'=>array('Type'=>'varchar(32)'),
                        'history__state'=>array('Type'=>'int(5)'),
                        'history__modified'=>array('Type'=>'datetime')
                        )

The fields that should appear in every history table.

Definition at line 69 of file HistoryTool.php.


The documentation for this class was generated from the following file:
 All Data Structures Namespaces Files Functions Variables Enumerations