Krang::Media - Media and media metadata storage and access methods
# create new media object
my $media = pkg('Media')->new( title => 'test media',
caption => 'test caption',
copyright => 'AP 1999',
media_type_id => $media_type_id,
category_id => $category_id );
# Find permissions for this media (for this user)
$media->may_see();
$media->may_edit();
# add actual media file to media object
$media->upload_file( filehandle => $filehandle, filename => 'media.jpg' );
# get MIME type of uploaded file
$mime_type = $media->mime_type();
# get path to thumbnail - if image (thumbnail will be created if
# does not exist)
$thumbnail_path = $media->thumbnail_path();
# assign 2 contributors to media object, specifying thier contributor type
$media->contribs({contrib_id => 1, contrib_type_id => 3},
{contrib_id => 44, contrib_type_id => 4});
# get contrib objects attached to this media
@contribs = $media->contribs();
# change assignment to include just the first contributor
$media->contribs($contribs[0]);
# get media element
my $element = $media->element();
$element_id = $media->element_id; # undef until after save()
# save the object to the database
$media->save();
# mark as checked out by you (your user_id)
$media->checkout();
# update caption for this media object
$media->caption('new caption');
# again, save the object to the database (upping version)
$media->save();
# get current version number, in this example 2
$version = $media->version();
# revert to version 1 (creating version 3, identical to 1)
$media->revert(1);
# preview media object
$media->preview
# publish media object
$media->publish
# get id for this object
my $media_id = $media->media_id();
# return object by id
($media_obj) = pkg('Media')->find( media_id => $media_id );
This class handles the storage and retrieval of media objects on the filesystem, as well as media object metadata in the database. Contributors (Krang::Contrib objects) can also be attached to stories.
Versioning in this system functions perhaps in a non-traditional way. A quick walk-thru of a media edit and revert may help understanding. We'll assume the existing media object starts with version = 1.
First, the media object is marked as 'checked out' by the current user. After this call, only someone logged in with the same user_id can edit this media object:
$media->checkout();
Now that the media object cannot be edited by anyone else, let's make a change to the title of this media object:
$media->title('new title');
Finally, we save the media object:
$media->save();
After save(), the in-memory object $media will be saved into the 'media' table as version = 2, and also saved in media_version for later use.
To begin the explaination of 'revert', the most important thing to
understand is that revert() simply takes a copy of an older
version and uses it to make a new version.
To revert to the contents of version 1, we call the revert() method:
$media->revert(1)
So now what do we have? We now have version 1, 2 and 3 in the versioning table, and 3 is a copy of 1.
Thus, revert() does not give you access to the actual original
version, but instead gives you a copy of it.
The reason for doing things this way is so you can always get back to a previous version no matter how many times you've saved and reverted. This is also how CVS works.
new()$media->new() supports the following name-value arguments:
media_id()save() is called the first time.
media_uuid()title()category_id()category()filename()caption()copyright()alt_tag()mime_type()upload_file() has been called.
notes()media_type_id()checked_out()checked_out_by()published()published_version()preview_version()version()creation_date()selected_contrib_type set according to their use with this media
object.
May be set two ways. First, a contributor may specified as a two-key hash containing the contrib_id and the contrib_type_id for the contributor. A single contributor can be present in the list multiple times with different contrib_type_ids.
Second, a list of contributor objects with selected_contrib_type() set
may be passed in.
clear_contribs()$all_version_numbers = $media->all_versions();$media->prune_versions(number_to_keep => 10);prune_versions() keeps
the number of versions specified by SavedVersionsPerMedia in krang.conf;
this can be overridden as above. In either case, it returns the number of
versions actually deleted.
filename() also.
Of if you already have the file in a temporary location in KrangRoot then you can
simply pass the filepath argument instead.
$media->upload_file(filepath => $path);
file_path()version
option, returns the filesystem path for this version or undef if this
version does not exist.
If the relative option is set to 1 then the path returned is relative
to KrangRoot. Returns undef before upload_file() on new objects.
Both options may be combined.
file_size()width()height()element (readonly)element_id (readonly)save()$media->save(keep_version => 1)If this media object has the same URL as an existing object then
save() will throw a Krang::Media::DuplicateURL exception with a
media_id field indicating the conflicting object.
Users may only save media to categories to which they have edit access.
If the user does not have access to the specified category, save()
will throw a 'Krang::Media::NoCategoryEditAccess' exception.
This method will throw a ``Krang::Media::NoEditAccess'' exception if a user does not otherwise have access to edit the media.
find($param)object(s) with parameters specified. Supported paramter keys:
media_id (and only media_id),
loads a specific version of a media object. Unlike revert(), this
object has version set to the actual version number of the loaded
object.
title
title_like - case insensitive match on title. Must include '%' on
either end for substring match.
alt_tag
alt_tag_like - case insensitive match on alt_tag. Must include '%' on
either end for substring match.
category_id
below_category_id - will return media in category and in categories
below as well.
site_id - returns all media objects associated with a given site.
media_type_id
contrib_id
filename
mime_type
published - only returns items that have been published previously.
filename_like - case insensitive match on filename. Must include '%' on either end for substring match.
simple_search - Performs a per-word LIKE substring match against title, filename, and url, and an exact match against media_id if value passed in is a number.
exclude_media_ids - excludes (an array ref of) IDs from the result set
no_attributes - returns objects where the fields caption, copyright, notes, and alt_tag are empty if this is set.
order_by - field to order search by, defaults to media_id
order_desc - results will be in ascending order unless this is set to 1 (making them descending).
limit - limits result to number passed in here, else no limit.
offset - offset results by this number, else no offset.
url
url_like - case insensitive match on url. Must include '%' on either end for substring match.
undef returns all media.
undef, specifying no limit in that direction.
find() should not return live media. The
default is 1.
NOTE:When searching for media_id, these three include_* flags are not taken into account!
@media = pkg('Media')->find(element_index_like => [location => '%boston%']);
revert($version)If the new version is successfully written to disk (no duplicate URL errors, etc.), the object itself is returned; if not, an error is returned.
relative is set to 1, returns a path relative
to KrangRoot.
If medium is set to true, then it will return the path to the medium
sized thumbnail.
Returns undef if a thumbnail cannot be created.
checkout() || Krang::Media->checkout($media_id)Will throw a Krang::Media::NoEditAccess exception if user is not allowed to edit this media.
Will throw a Krang::Media::CheckedOut exception if the media is already checked out.
checkin() || Krang::Media->checkin($media_id)$media->mark_as_published()publish_date and
published_version attributes, and will also check the media object back
in.
This will also make an entry in the log that the media object has been published.
$media->mark_as_previewed(unsaved => 1)preview_version attribute, setting it equal to version. This is
used as a sanity check by Krang::Publisher to prevent re-generation
of content.
The argument unsaved defaults to 0. If true, it indicates that the
media being previewed is in the process of being edited, in which case
any previews made cannot be trusted for future use. In that case,
preview_version is set to -1.
publish_path()preview_path()duplicate_check()delete() || Krang::Media->delete($media_id)Attempts to checkout the media object, will croak if checked out by another user.
Will throw ``Krang::Media::NoEditAccess'' exception if user is not allowed to edit this media.
@linked_stories = $media->linked_stories@linked_media = $media->linked_media$media->serialize_xml(writer => $writer, set => $set)$media = Krang::Media->deserialize_xml(xml => $xml, set => $set, no_update => 0)If an incoming media has the same URL as an existing media then an update will occur, unless no_update is set.
Note that the creation_date, version, published_version and publish_date fields are ignored when importing media.
$data = Storable::freeze($media)STORABLE_freeze() to
ensure this works correctly.
$media = Storable::thaw($data)STORABLE_thaw() to ensure this works correctly.
$media->retire()Krang::Media->retire(media_id => $media_id)$media->unretire()Krang::Media->unretire(media_id => $media_id)$media->trash()Krang::Media->trash(media_id => $media_id)$media->untrash()Krang::Media->untrash(media_id => $media_id)$media->wont_publish()$media->clone(category_id => $category_id)$media->is_text()$media->is_image()