\seekquarry\yioop\libraryVersionManager

VersionManager can be used to create and manage versions of files in a folder so that a user can revert the files to any version desired back to the time the folder under manager was first managed. It is used by Yioop's Wiki system to handle versions of image and other media resources for a Wiki page.

Summary

Methods
Properties
Constants
__construct()
createVersion()
headGetContents()
headPutContents()
headCopy()
headDelete()
headMakeDirectory()
headRename()
headInfo()
versionGetContents()
restoreVersion()
getActiveVersion()
getVersionsInRange()
$archive_name
$archive_path
$lock_file
$versions_path
$hash_algorithm
$managed_folder
$permissions
RENAME_FAILED
PUT_CONTENTS_FAILED
MAKE_DIR_FAILED
DELETE_FAILED
COPY_FAILED
UNMANAGED_FILE_LOOK_UP
TARGET_LOCATION_ERROR
INVALID_DIR_ENTRY
HASH_FILE_NOT_FOUND
VERSION_LOOK_UP_FAIL
HASH_LOOKUP_FAIL
LOCK_FAIL
SUCCESS
getArchivePathHashName()
getArchiveFileAndPath()
getVersionPath()
getHashNamePath()
unlinkHead()
copyFromVersionOrHashName()
traverseUnlinkHead()
deleteVersionFileOrFolder()
No protected properties found
N/A
No private methods found
No private properties found
N/A

Constants

RENAME_FAILED

RENAME_FAILED

Return code constants for public VersionManager methods

PUT_CONTENTS_FAILED

PUT_CONTENTS_FAILED

MAKE_DIR_FAILED

MAKE_DIR_FAILED

DELETE_FAILED

DELETE_FAILED

COPY_FAILED

COPY_FAILED

UNMANAGED_FILE_LOOK_UP

UNMANAGED_FILE_LOOK_UP

TARGET_LOCATION_ERROR

TARGET_LOCATION_ERROR

INVALID_DIR_ENTRY

INVALID_DIR_ENTRY

HASH_FILE_NOT_FOUND

HASH_FILE_NOT_FOUND

VERSION_LOOK_UP_FAIL

VERSION_LOOK_UP_FAIL

HASH_LOOKUP_FAIL

HASH_LOOKUP_FAIL

LOCK_FAIL

LOCK_FAIL

SUCCESS

SUCCESS

Properties

$archive_name

$archive_name : string

Name of subfolder in which to store files when a version of the managed folder is created.

Type

string

$archive_path

$archive_path : string

Filesystem path to the archive folder

Type

string

$lock_file

$lock_file : string

Filesystem path to file that is used for locking whether a new VersionManager public method is allowed to manipulate files in the archive.

Type

string

$versions_path

$versions_path : string

Path to folder in archive in which a list of all versions is maintained

Type

string

$hash_algorithm

$hash_algorithm : string

Hash algorithm to be applied to folder's files inorder to come up with a name to store in a version in the archive.

Type

string

$managed_folder

$managed_folder : string

Folder that is being managed (prior versions of files in it being maintained) by this VersionManager instance

Type

string

$permissions

$permissions : string

File system permissions to use when storing version files into the version archive. If <=0 then use default file permissions

Type

string

Methods

__construct()

__construct(string  $managed_folder = '.', string  $archive_name = '.archive', string  $hash_algorithm = 'sha256', integer  $permissions = 511) 

Creates an object which can be used to manage multiple versions of files in a managed folder, storing prior version in an archive folder using a hash_algorithm to determine how to name these archived files and saving these archived files according to some file system permissions.

Parameters

string $managed_folder

what folder should be managed with this versioning system

string $archive_name

file_name in the folder to use for the subfolder containing the archived versions of files

string $hash_algorithm

what hash algorithm should be used to generate archive filenames. Defaults to sha256

integer $permissions

what to set the file permissions to for the archive file. To keep things simple this defaults to world read write. In practice you probably want to tailor this to the situation for security. If you set the value to <= 0 the permissions will be whatever your OS would use by default

createVersion()

createVersion(string  $file_changed = "", string  $folder = "", integer  $now, boolean  $lock = true, array  $force_update_list = array()) : mixed

If $file_changed is not a subpath of $folder then only the $folder file is involved in new version. I.e., a single repository dir file for folder will be made. If $file_changed is a nonexistent file in $folder then the dir's in path to $file_changed will be updated.

Parameters

string $file_changed
string $folder
integer $now
boolean $lock

whether or not a lock should be obtained before carrying out the operation

array $force_update_list

Returns

mixed —

either an error code or the name of the hash_file in the repository for the version just created

headGetContents()

headGetContents(string  $file, boolean  $lock = true) : integer

Read from the head version of the repository the contents of $file.

Parameters

string $file

name of file to get contents of

boolean $lock

whether or not a lock should be obtained before carrying out the operation

Returns

integer —

success code

headPutContents()

headPutContents(string  $file, string  $data, boolean  $lock = true) : integer

Write $data into the file $file in the head version of the repository

Parameters

string $file

name of file to store data for

string $data

what to store in the file $file

boolean $lock

whether or not a lock should be obtained before carrying out the operation

Returns

integer —

success code

headCopy()

headCopy(string  $from_name, string  $to_name, boolean  $lock = true) : integer

Copies the data in a file $from_name in the head version of the repository to a different file named $to_name

Parameters

string $from_name

name of file or dir to copy

string $to_name

name of file or dir to save copy to

boolean $lock

whether or not a lock should be obtained before carrying out the operation

Returns

integer —

success code

headDelete()

headDelete(string  $file, boolean  $lock = true) : integer

Deletes a file $file from the version of the repository

Parameters

string $file

name of file to delete

boolean $lock

whether or not a lock should be obtained before carrying out the operation

Returns

integer —

success code

headMakeDirectory()

headMakeDirectory(string  $dir, boolean  $lock = true) : integer

Makes a directory named $dir in the current folder in the head version of the repository being managed

Parameters

string $dir

name of directory folder to make

boolean $lock

whether or not a lock should be obtained before carrying out the operation

Returns

integer —

success code

headRename()

headRename(string  $old_name, string  $new_name, boolean  $lock = true) : integer

Renames the file $old_name in the head version of the repository to a different file named $new_name

Parameters

string $old_name

original name of file

string $new_name

what to change it to

boolean $lock

whether or not a lock should be obtained before carrying out the operation

Returns

integer —

success code

headInfo()

headInfo(boolean  $lock = true) : mixed

Returns the files in the root directory in the most recent version of the repository together with a TIMESTAMP of the date when the most recent version was made.

Parameters

boolean $lock

whether or not a lock should be obtained before carrying out the operation

Returns

mixed —

either any array [TIMESTAMP => time of last version, FILES => files in last version's folder] or LOCK_FAIL error code

versionGetContents()

versionGetContents(string  $file, integer  $timestamp, boolean  $get_nearest_version = false, boolean  $lock = true) : mixed

Retrieves the contents of a file from a particular version of the repository

Parameters

string $file

name of file to get data about

integer $timestamp

which version want to get file out of

boolean $get_nearest_version

if true then if $timestamp doesn't exist as a version get the nearest version after $timestamp

boolean $lock

whether or not a lock should be obtained before carrying out the operation

Returns

mixed —

either a string with the file's data or an error code

restoreVersion()

restoreVersion(integer  $timestamp, boolean  $lock = true, boolean  $force_lock = false) : integer

Restores the version of the repository that existed at a timestamp to the managed folder. Files currently in the managed folder before the restored but which exist in the HEAD version of the repository are removed from the managed folder (kept in repository).

Parameters

integer $timestamp

of version what to restore to

boolean $lock

whether or not a lock should be obtained before carrying out the operation

boolean $force_lock

whether or not any existing lock should be ignored

Returns

integer —

success code

getActiveVersion()

getActiveVersion(integer  $search_timestamp, boolean  $lock = true) 

Gets the most recent version timestamp of a version in the repository that is less than or equal to the searched for timestamp.

Parameters

integer $search_timestamp

want to find the version in the repository closest to, but not exceeding this value.

boolean $lock

whether or not a lock should be obtained before carrying out the operation

getVersionsInRange()

getVersionsInRange(integer  $start_time, integer  $end_time, boolean  $lock = true) 

Gets all the versions times that exist in the repository and which are between in time two values.

Parameters

integer $start_time

look for timestamps in repository above or equal this value

integer $end_time

look for timestamps in repository below or equal this value

boolean $lock

whether or not a lock should be obtained before carrying out the operation

getArchivePathHashName()

getArchivePathHashName(string  $hash_name) : string

Get the path in the repository archive that corresponds to the given hash name of an object that might be in the repository.

Currently, the archive consists of two nested folders based on prefixes of objects stored in the repository, so this method calculates those prefixes and tacks them on to the archive path.

Parameters

string $hash_name

to make a archive path for

Returns

string —

path to $hash_name object in repository archive

getArchiveFileAndPath()

getArchiveFileAndPath(string  $file, integer  $timestamp, boolean  $make_path = false) : array

Gets the hash file name and path within the archive repository for a file name from the managed folder that existed at timestamp

Parameters

string $file

name of file want to get the archive name and archive path for

integer $timestamp

of version want to compute archive filename and path for

boolean $make_path

whether to make folders (if they don't exists already) in the archive repository for the path calculated

Returns

array —

[hash_name, archive_path] to use for file in the repository for object given that version timestamp

getVersionPath()

getVersionPath(integer  $timestamp, boolean  $make_path = false) : string

Versions are stored in the version subfolder of the archive repository within a year folder within a day folder. Given a timestamp this function returns the path of the version folder it would correspond to

Parameters

integer $timestamp

to find version folder for

boolean $make_path

whether to make folders (if they don't exists already) in the archive repository for the path calculated

Returns

string —

path to version folder

getHashNamePath()

getHashNamePath(string  $file, integer  $timestamp, boolean  $get_nearest_version = false, string  $path_so_far = "", string  $hash_path_so_far = "") : string

Given a file or directory and a timestamp finds the path to that file in the repository by tranversing the repository and looking the hash names of folders subfolders in the repository.

The timestamp that is lookedup might not be the timestamp of the file because when created a version that file might not have changed so its old info is copied into the new version. This is why a traversal might be needed.

Parameters

string $file

name of file to get path for

integer $timestamp

which version in repository want to get file for

boolean $get_nearest_version

if true then if $timestamp doesn't exist as a version get the nearest version after $timestamp

string $path_so_far

path in managed folder that this recursive procedure has already traversed

string $hash_path_so_far

corresponding path to path_so_far but in the archive repository

Returns

string —

path to file in the archive repository

unlinkHead()

unlinkHead(boolean  $lock = true) 

Delete all the files from the managed folder which exist in the HEAD version in the archive repository

Parameters

boolean $lock

whether or not a lock should be obtained before carrying out the operation

copyFromVersionOrHashName()

copyFromVersionOrHashName(string  $file, string  $target, integer  $timestamp, string  $hash_name_type = "", boolean  $lock = true) 

Copies to the target folder in the managed folder a file or directory that existed at a given timestamp in the archive repository

Parameters

string $file

file name of file or direcotry to copy to managed folder

string $target

folder to copy to

integer $timestamp

which archive version of the file to copy

string $hash_name_type

either f, l, or d depending on whether the object in the repository is known to be a file, link, or folder. If left empty then this is looked up in the repository

boolean $lock

whether or not a lock should be obtained before carrying out the operation

traverseUnlinkHead()

traverseUnlinkHead(string  $dir, integer  $timestamp) 

Recursively traverse a directory structure and call a callback function

Parameters

string $dir

name of folder to delete

integer $timestamp

only deletes if the file existed in the version given by the timestamp in the repository (by default this is the timestamp asscoaited with the HEAD version)

deleteVersionFileOrFolder()

deleteVersionFileOrFolder(string  $file_or_dir, integer  $timestamp) 

This function is used in the process of recursively deleting a directory

Parameters

string $file_or_dir

the filename or directory name to be deleted

integer $timestamp

only deletes if the file existed in the version given by the timestamp in the repository