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()
Initialize new media object. Passing in filehandle and filename upon new is eqivalent of calling new(), then upload_file().
$media->new() supports the following name-value arguments:
Fields for storing arbitrary metadata -- title is required.
ID for media_type, the media_type this media object is associated with.
ID for category, the category this media object is associated with.
User id of person who has media object checked out, undef if not checked out.
Last published version
Returns true if the media has been published under its current URL.
Last preview version
The filename of the uploaded media.
Filehandle for uploaded media.
Date the media object was created. Defaults to current time unless set.
media_id()
Returns the unique id assigned the media object. Will not be populated until $media->save() is called the first time.
media_uuid()
Unique ID for media, valid across different machines when the object is moved via krang_export and krang_import.
title()
category_id()
category()
Returns to category object matching category_id.
filename()
caption()
copyright()
alt_tag()
mime_type()
Returns the MIME type of the media file. This is readonly and undef
until after upload_file() has been called.
notes()
media_type_id()
Gets/sets the value.
checked_out()
Returns 1 if checked out by a user (checked_out_by is set), otherwise returns 0.
checked_out_by()
Returns id of user who has object checked out, if checked out.
published()
Returns true if the media has been published under its current URL.
published_version()
Returns version number of published version of this object (if has been published).
preview_version()
Returns version number of the last version previewed.
version()
Returns the current version number.
creation_date()
Returns the initial creation date of the media object. Not settable here.
Called with no arguments, returns a list of contributor
(Krang::Contrib) objects. These objects will have
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()
Removes all contributor associatisons.
$all_version_numbers = $media->all_versions();
Returns an arrayref containing all the existing version numbers for this media object.
$media->prune_versions(number_to_keep => 10);
Deletes old versions of this media object. By default 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.
Stores media file to temporary location on filesystem. Sets $media->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);
Stores media file to temporary location on filesystem, and sets $media->filename(), just like upload_file(). But in this case, the filename and its scalar text content are passed, instead of an actual file.
file_path()
Return filesystem path of uploaded media file. Given the 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()
Return filesize in bytes.
width()
Return width of image in pixels.
height()
Return height of image in pixels.
element (readonly)
The element for this media object.
element_id (readonly)
The element_id for this media object.
save()
$media->save(keep_version => 1)
Commits media object to the database. Will set media_id to unique id if not already defined (first save). Increments the version number unless called with 'keep_version' set to 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)
Find and return media object(s) with parameters specified. Supported paramter keys:
media_id (can optionally take a list of ids)
media_uuid
version - combined with a single 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.
Set to 0 to find only non-checked-out media. Set to 1 to find only
checked out media. The default, undef returns all media.
Set to a user_id to find media checked-out by a user.
ids_only - return only media_ids, not objects if this is set true.
count - return only a count if this is set to true. Cannot be used with ids_only.
creation_date - May be either a single date (a Time::Piece object) or an
array of 2 dates specifying a range. In ranges either member may be
undef, specifying no limit in that direction.
Include live media in the search result. Live media are media
that are neither retired nor have been moved to the trashbin. Set
this option to 0, if find() should not return live media. The
default is 1.
Set this option to 1 if you want to include retired media in the search result. The default is 0.
Set this option to 1 if you want to include trashed media in the search result. Trashed media live in the trashbin. The default is 0.
NOTE:When searching for media_id, these three include_* flags are not taken into account!
This find option allows you to search against indexed element data. For details on element indexing, see Krang::ElementClass. This option should be set with an array containing the element name and the value to match against. For example, to search for media objects containing 'boston' in their location, assuming location is an indexed element:
@media = pkg('Media')->find(element_index_like => [location => '%boston%']);
revert($version)
Changes media object to a copy of the version specified. Does not actually edit the original version, but creates a new version identical to the original.
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.
Returns the path to the thumbnail (if media is an image). Valid image
types are stored in IMAGE_TYPES constant. Will create thumbnail if
first time called. If 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)
Marks media object as checked out by user_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)
Marks media object as checked in.
$media->mark_as_published()
Mark the media object as published. This will update the 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)
Mark the media object as previewed. This will update the
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.
Returns calculated url of media object based on category_id and filename
The preview URL for this media object
publish_path()
Returns the publish path for the media object, using the site's publish_path and the media's URL. This is the filesystem path where the media object will be published.
preview_path()
Returns the preview path for the media object, using the site's preview_path and the media's URL. This is the filesystem path where the media object will be previewed.
duplicate_check()
This method checks whether the url of a media object is unique.
Convenience method to Krang::Publisher, previews media object.
Convenience method to Krang::Publisher, publishes media object.
delete() || Krang::Media->delete($media_id)
Permenantly delete media object or media object with given 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
Returns a list of stories linked to from this media object. These will be Krang::Story objects. If no stories are linked, returns an empty list. This list will not contain any duplicate stories, even if a story is linked more than once.
@linked_media = $media->linked_media
Returns a list of media linked to from this media. These will be Krang::Media objects. If no media are linked, returns an empty list. This list will not contain any duplicate media, even if a media object is linked more than once.
$media->serialize_xml(writer => $writer, set => $set)
Serialize as XML. See Krang::DataSet for details.
$media = Krang::Media->deserialize_xml(xml => $xml, set => $set, no_update => 0)
Deserialize XML. See Krang::DataSet for details.
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)
Serialize media. Krang::Media implements STORABLE_freeze() to
ensure this works correctly.
$media = Storable::thaw($data)
Deserialize frozen media. Krang::Media implements STORABLE_thaw() to ensure this works correctly.
$media->retire()
Krang::Media->retire(media_id => $media_id)
Retire the media, i.e. remove it from its publish/preview location and don't show it on the Find Media screen. Throws a Krang::Media::NoEditAccess exception if user may not retire this media. Croaks if the media is checked out by another user.
$media->unretire()
Krang::Media->unretire(media_id => $media_id)
Unretire the media, i.e. show it again on the Find Media screen, but don't republish it. Throws a Krang::Media::NoEditAccess exception if user may not unretire this media. Throws a Krang::Media::DuplicateURL exception if a media with the same URL has been created in Live. Croaks if the media is checked out by another user.
$media->trash()
Krang::Media->trash(media_id => $media_id)
Move the media to the trashbin, i.e. remove it from its publish/preview location and don't show it on the Find Media screen. Throws a Krang::Media::NoEditAccess exception if user may not edit this media. Croaks if the media is checked out by another user.
$media->untrash()
Krang::Media->untrash(media_id => $media_id)
Restore the media from the trashbin, i.e. show it again on the Find Media screen or Retired Media screens (depending on the location from where it was deleted). Throws a Krang::Media::NoRestoreAccess exception if user may not edit this media. Croaks if the media is checked out by another user. This method is called by Krang::Trash->restore().
$media->wont_publish()
Convenience method returning true if media has been retired or trashed.
$media->clone(category_id => $category_id)
Copy $media to the category having the specified category_id. Returns an unsaved and checked_out copy with the media file already uploaded.
$media->is_text()
Returns true if this media object appears to be text (HTML, JS, CSS, etc);
$media->is_image()
Returns true if this media object appears to be an image.
Krang::Media->guess_media_type($filename)
Returns a media_type_id based on a good guess from the filename.
Krang::Media->guess_mime_type($filename)
Returns a mime_type based on a good guess from the filename.
Krang::Media->clean_filename($filename)
Cleans up the filename just like Krang::Media does internally before saving filename changes. This method let's perform those same cleanups in case you need to work with the file prior to creating a media object.