/[drupal]/contributions/modules/mmedia/API/media.inc
ViewVC logotype

Diff of /contributions/modules/mmedia/API/media.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.4, Tue Dec 16 15:11:46 2008 UTC revision 1.5, Thu Mar 19 13:31:53 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id$  // $Id: media.inc,v 1.4 2008/12/16 15:11:46 rhys Exp $
3    
4  /**  /**
5   * Loads a media item.   * Load a media item.
6   *   *
7   * @param $mid   * @param $mid
8   *   The media identifier from the database.   *   The media identifier from the database.
# Line 11  Line 11 
11   * @return   * @return
12   *   Returns false on failure, media object on success.   *   Returns false on failure, media object on success.
13   */   */
14  function media_load($mid, $refresh = false) {  function media_load($mid, $refresh = FALSE) {
15    static $media;    static $media;
16    
17    // refresh the cache    // refresh the cache
# Line 20  function media_load($mid, $refresh = fal Line 20  function media_load($mid, $refresh = fal
20    }    }
21    
22    if (!is_numeric($mid)) {    if (!is_numeric($mid)) {
23      return false;      return FALSE;
24    }    }
25    
26    // if media cached    // if media cached
# Line 29  function media_load($mid, $refresh = fal Line 29  function media_load($mid, $refresh = fal
29    }    }
30    
31    // load from database    // load from database
32    $m = db_fetch_object(db_query("SELECT * FROM {media} WHERE mid=%d", $mid));    $m = db_fetch_object(db_query("SELECT * FROM {media} WHERE mid = %d", $mid));
33    
34    // not a valid item    // not a valid item
35    if (!$m) {    if (!$m) {
# Line 50  function media_load($mid, $refresh = fal Line 50  function media_load($mid, $refresh = fal
50  }  }
51    
52  /**  /**
53   * Inserts a media item.   * Insert a media item.
54   *   *
55   * @param $media   * @param $media
56   *   The media upload object   *   The media upload object
# Line 65  function media_insert($media, $fid = 0, Line 65  function media_insert($media, $fid = 0,
65    
66    // check that some basics are actually set    // check that some basics are actually set
67    if (!is_object($media) || !(@$media->title) || (!mapi_file_exists(@$media->path) && !mapi_file_exists(@$media->path . @$media->name .'.'. @$media->ext))) {    if (!is_object($media) || !(@$media->title) || (!mapi_file_exists(@$media->path) && !mapi_file_exists(@$media->path . @$media->name .'.'. @$media->ext))) {
68      return false;      return FALSE;
69    }    }
70    
71    // load parent to check    // load parent to check
72    if (!($parent = mmedia_folder_load($fid))) {    if (!($parent = mmedia_folder_load($fid))) {
73      return false;      return FALSE;
74    }    }
75    
76    // find a unique hash for the media.    // find a unique hash for the media.
# Line 94  function media_insert($media, $fid = 0, Line 94  function media_insert($media, $fid = 0,
94    }    }
95    
96    // make sure it exists, within workspace    // make sure it exists, within workspace
97    if (!mapi_directory_check($path, array('create' => true, 'workspace' => $workspace))) {    if (!mapi_directory_check($path, array('create' => TRUE, 'workspace' => $workspace))) {
98      mapi_error('Unable to create directory !path', array('!path' => $path), 'media');      mapi_error('Unable to create directory !path', array('!path' => $path), 'media');
99      return false;      return FALSE;
100    }    }
101    
102    // get the file details    // get the file details
# Line 116  function media_insert($media, $fid = 0, Line 116  function media_insert($media, $fid = 0,
116    if (!$external) {    if (!$external) {
117      if (!($file = mapi_file_copy($media->path . $fname, $path . $fname, array('workspace' => $workspace)))) {      if (!($file = mapi_file_copy($media->path . $fname, $path . $fname, array('workspace' => $workspace)))) {
118        mapi_error('Unable to copy !filename to !path', array('!filename' => $media->path . $fname, '!path' => $path . $fname));        mapi_error('Unable to copy !filename to !path', array('!filename' => $media->path . $fname, '!path' => $path . $fname));
119        return false;        return FALSE;
120      }      }
121    }    }
122    // it's an external file    // it's an external file
# Line 136  function media_insert($media, $fid = 0, Line 136  function media_insert($media, $fid = 0,
136    $media->changed     = time();    $media->changed     = time();
137    $media->expire      = (!empty($media->expire) ? $media->expire : 0);    $media->expire      = (!empty($media->expire) ? $media->expire : 0);
138    $media->local       = $external;    $media->local       = $external;
139    $media->public      = (!empty($media->public) ? $media->public : true);    $media->public      = (!empty($media->public) ? $media->public : TRUE);
140    $media->summary     = (!empty($media->summary) ? substr($media->summary, 0, 255) : '');    $media->summary     = (!empty($media->summary) ? drupal_substr($media->summary, 0, 255) : '');
141    $media->source      = (!empty($media->source) ? substr($media->source, 0, 255) : '');    $media->source      = (!empty($media->source) ? drupal_substr($media->source, 0, 255) : '');
142    $media->reference   = (!empty($media->reference) ? substr($media->reference, 0, 255): '');    $media->reference   = (!empty($media->reference) ? drupal_substr($media->reference, 0, 255): '');
143    
144    // define the media and it's values to insert into the database    // define the media and it's values to insert into the database
145    $mid = @$media->mid;    $mid = @$media->mid;
# Line 166  function media_insert($media, $fid = 0, Line 166  function media_insert($media, $fid = 0,
166    }    }
167    // otherwise return false with error    // otherwise return false with error
168    else {    else {
169      mapi_error('Unable to insert media into database:' . $file);      mapi_error('Unable to insert media into database:'. $file);
170      return false;      return FALSE;
171    }    }
172    
173    // notify others that the media as been created    // notify others that the media as been created
# Line 181  function media_insert($media, $fid = 0, Line 181  function media_insert($media, $fid = 0,
181  }  }
182    
183  /**  /**
184   * Creates a media item from a filename, or a URL.   * Create a media item from a filename, or a URL.
185   *   *
186   * @param $filename   * @param $filename
187   *   The filename to copy.   *   The filename to copy.
# Line 193  function media_insert($media, $fid = 0, Line 193  function media_insert($media, $fid = 0,
193  function media_create($uri, $title, $parent = 0, $workspace = 'mmedia') {  function media_create($uri, $title, $parent = 0, $workspace = 'mmedia') {
194    // must have title and a filename that exists    // must have title and a filename that exists
195    if (!$title || !mapi_file_exists($uri)) {    if (!$title || !mapi_file_exists($uri)) {
196      return false;      return FALSE;
197    }    }
198    
199    // create an object which can be processed correctly    // create an object which can be processed correctly
# Line 205  function media_create($uri, $title, $par Line 205  function media_create($uri, $title, $par
205  }  }
206    
207  /**  /**
208   * Deletes an item from the database.   * Delete an item from the database.
209   *   *
210   * @param $mid   * @param $mid
211   *   The identifier for the media item.   *   The identifier for the media item.
212   */   */
213  function media_delete($mid, $workspace = null) {  function media_delete($mid, $workspace = NULL) {
214    // load item from database    // load item from database
215    if (!($media = media_load($mid))) {    if (!($media = media_load($mid))) {
216      return false;      return FALSE;
217    }    }
218    
219    $path = $media->path . $media->name .'.'. $media->ext;    $path = $media->path . $media->name .'.'. $media->ext;
# Line 243  function media_delete($mid, $workspace = Line 243  function media_delete($mid, $workspace =
243  }  }
244    
245  /**  /**
246   * Renames an item, including original and derivatives.   * Rename an item, including original and derivatives.
247   *   *
248   * @param $mid   * @param $mid
249   *   The identifier for the media item.   *   The identifier for the media item.
250   * @param $name   * @param $name
251   *   The new name, invalid characters filtered.   *   The new name, invalid characters filtered.
252   * @return   * @return
253   *   Return false on failure, true on success   *   Return FALSE on failure, TRUE on success
254   */   */
255  function media_rename($mid, $name, $workspace = 'mmedia') {  function media_rename($mid, $name, $workspace = 'mmedia') {
256    // load derivative    // load derivative
257    if (!($media = media_load($mid))) {    if (!($media = media_load($mid))) {
258      return false;      return FALSE;
259    }    }
260    
261    // cannot rename a non-local file    // cannot rename a non-local file
262    if (!$media->local) {    if (!$media->local) {
263      return true;      return TRUE;
264    }    }
265    
266    // replace potential invalid characters with '-'    // replace potential invalid characters with '-'
# Line 284  function media_rename($mid, $name, $work Line 284  function media_rename($mid, $name, $work
284    
285    db_query("UPDATE {media} SET name='%s' WHERE mid=%d", $media->name, $media->mid);    db_query("UPDATE {media} SET name='%s' WHERE mid=%d", $media->name, $media->mid);
286    
287    media_load($media->mid, true);    media_load($media->mid, TRUE);
288    
289    // notify others that the media has been renamed    // notify others that the media has been renamed
290    $media->old_name = $old_media->name;    $media->old_name = $old_media->name;
291    media_invoke_item('rename', $media);    media_invoke_item('rename', $media);
292    
293    return true;    return TRUE;
294  }  }
295    
296  /**  /**
297   * Moves an item from one folder to another. This moves the derivatives if the media system is   * Move an item from one folder to another. This moves the derivatives if the media system is
298   * set to a hierarchical structure.   * set to a hierarchical structure.
299   *   *
300   * @param $mid   * @param $mid
# Line 302  function media_rename($mid, $name, $work Line 302  function media_rename($mid, $name, $work
302   * @param $fid   * @param $fid
303   *   The new folder to move media item to.   *   The new folder to move media item to.
304   * @return   * @return
305   *   Return false on failure, true on success   *   Return FALSE on failure, TRUE on success
306   */   */
307  function media_move($mid, $fid, $workspace = 'mmedia') {  function media_move($mid, $fid, $workspace = 'mmedia') {
308    // load the media and folder    // load the media and folder
309    if (!($media = media_load($mid)) || !($folder = mmedia_folder_load($fid))) {    if (!($media = media_load($mid)) || !($folder = mmedia_folder_load($fid))) {
310      return false;      return FALSE;
311    }    }
312    
313    // no real need to shift it (if folder is the same, or non-local)    // no real need to shift it (if folder is the same, or non-local)
314    if ($media->fid == $folder->fid || !$media->local) {    if ($media->fid == $folder->fid || !$media->local) {
315      return true;      return TRUE;
316    }    }
317    
318    // update the media settings    // update the media settings
# Line 323  function media_move($mid, $fid, $workspa Line 323  function media_move($mid, $fid, $workspa
323    $source = $media->path . $media->name .'.'. $media->ext;    $source = $media->path . $media->name .'.'. $media->ext;
324    
325    // change the folder path    // change the folder path
326    $media->path = media_folder($media, $workspace, true);    $media->path = media_folder($media, $workspace, TRUE);
327    $dest = media_filename($media);    $dest = media_filename($media);
328    
329    if ($file = mapi_file_move($source, $dest, FILE_EXISTS_RENAME, $workspace)) {    if ($file = mapi_file_move($source, $dest, FILE_EXISTS_RENAME, $workspace)) {
# Line 338  function media_move($mid, $fid, $workspa Line 338  function media_move($mid, $fid, $workspa
338    // change the media folder    // change the media folder
339    db_query("UPDATE {media} SET fid=%d, path='%s' WHERE mid=%d", $fid, $media->path, $media->mid);    db_query("UPDATE {media} SET fid=%d, path='%s' WHERE mid=%d", $fid, $media->path, $media->mid);
340    
341    media_load($media->mid, true);    media_load($media->mid, TRUE);
342    
343    $media->old_ext = $old_media->ext;    $media->old_ext = $old_media->ext;
344    $media->old_name = $old_media->name;    $media->old_name = $old_media->name;
# Line 349  function media_move($mid, $fid, $workspa Line 349  function media_move($mid, $fid, $workspa
349    // notify others that the media has been moved    // notify others that the media has been moved
350    media_invoke_item('move', $media);    media_invoke_item('move', $media);
351    
352    return true;    return TRUE;
353  }  }
354    
355  /**  /**
356   * Loads the metadata for the item.   * Load the metadata for the item.
357   *   *
358   * @param $mid   * @param $mid
359   *   The media identifier.   *   The media identifier.
360   * @return   * @return
361   *   Returns an associative key array for the item.   *   Returns an associative key array for the item.
362   */   */
363  function media_metadata_load($mid, $refresh = false) {  function media_metadata_load($mid, $refresh = FALSE) {
364    static $media;    static $media;
365    
366    $metadata = array();    $metadata = array();
# Line 382  function media_metadata_load($mid, $refr Line 382  function media_metadata_load($mid, $refr
382    $results = db_query("SELECT * FROM {media_metadata} WHERE mid=%d", $mid);    $results = db_query("SELECT * FROM {media_metadata} WHERE mid=%d", $mid);
383    while ($obj = db_fetch_object($results)) {    while ($obj = db_fetch_object($results)) {
384      $value = unserialize($obj->value);      $value = unserialize($obj->value);
385      $metadata[$obj->type] = ($value !== false ? $value : $obj->value);      $metadata[$obj->type] = ($value !== FALSE ? $value : $obj->value);
386    }    }
387    
388    // load commonly used metadata    // load commonly used metadata
# Line 403  function media_metadata_load($mid, $refr Line 403  function media_metadata_load($mid, $refr
403  }  }
404    
405  /**  /**
406   * Saves the metadata for the item.   * Save the metadata for the item.
407   *   *
408   * @param $mid   * @param $mid
409   *   The media identifier.   *   The media identifier.
# Line 419  function media_metadata_save($mid, $meta Line 419  function media_metadata_save($mid, $meta
419    
420      // the inbuilt metadata      // the inbuilt metadata
421      $inbuilt = array(      $inbuilt = array(
422        'title' => !empty($metadata['title']) ? substr($metadata['title'], 0, 255) : '',        'title' => !empty($metadata['title']) ? drupal_substr($metadata['title'], 0, 255) : '',
423        'summary' => !empty($metadata['summary']) ? substr($metadata['summary'], 0, 255) : '',        'summary' => !empty($metadata['summary']) ? drupal_substr($metadata['summary'], 0, 255) : '',
424        'source' => !empty($metadata['source']) ? substr($metadata['source'], 0, 255) : '',        'source' => !empty($metadata['source']) ? drupal_substr($metadata['source'], 0, 255) : '',
425        'reference' => !empty($metadata['reference']) ? substr($metadata['reference'], 0, 255) : '',        'reference' => !empty($metadata['reference']) ? drupal_substr($metadata['reference'], 0, 255) : '',
426        'created' => !empty($metadata['created']) && is_numeric($metadata['created']) ? $metadata['created'] : time(),        'created' => !empty($metadata['created']) && is_numeric($metadata['created']) ? $metadata['created'] : time(),
427        'expire' => !empty($metadata['expire']) && is_numeric($metadata['expire']) ? $metadata['expire'] : 0,        'expire' => !empty($metadata['expire']) && is_numeric($metadata['expire']) ? $metadata['expire'] : 0,
428      );      );
# Line 446  function media_metadata_save($mid, $meta Line 446  function media_metadata_save($mid, $meta
446      // serialize the standard metadata      // serialize the standard metadata
447      $standard = array(      $standard = array(
448        'mid' => $mid,        'mid' => $mid,
449        'licence' => (isset($metadata['licence']) ? $metadata['licence'] : null),        'licence' => (isset($metadata['licence']) ? $metadata['licence'] : NULL),
450        'description' => (isset($metadata['description']) ? $metadata['description'] : null),        'description' => (isset($metadata['description']) ? $metadata['description'] : NULL),
451        'bitrate' => (isset($metadata['bitrate']) ? substr($metadata['bitrate'], 0, 255) : null),        'bitrate' => (isset($metadata['bitrate']) ? drupal_substr($metadata['bitrate'], 0, 255) : NULL),
452        'quality' => (isset($metadata['quality']) ? substr($metadata['quality'], 0, 255) : null),        'quality' => (isset($metadata['quality']) ? drupal_substr($metadata['quality'], 0, 255) : NULL),
453        'length' => (isset($metadata['length']) ? substr($metadata['length'], 0, 255) : null),        'length' => (isset($metadata['length']) ? drupal_substr($metadata['length'], 0, 255) : NULL),
454        'info' => serialize(isset($metadata['info']) ? $metadata['info'] : array()),        'info' => serialize(isset($metadata['info']) ? $metadata['info'] : array()),
455      );      );
456    
# Line 496  function media_metadata_save($mid, $meta Line 496  function media_metadata_save($mid, $meta
496        db_query("INSERT INTO {media_metadata} (mid, type, value) VALUES ". implode(', ', $sql), $values);        db_query("INSERT INTO {media_metadata} (mid, type, value) VALUES ". implode(', ', $sql), $values);
497      }      }
498    
499      media_metadata_load($mid, true);      media_metadata_load($mid, TRUE);
500    }    }
501  }  }
502    
503  /**  /**
504   * Gets the list of metadata keys used within the database   * Retrieve the list of metadata keys used within the database.
505   * This does not include the inbuilt metadata within the media table.   * This does not include the inbuilt metadata within the media table.
506   *   *
507   * @return   * @return
508   *   Returns array of keys used for metadata within the database   *   Returns array of keys used for metadata within the database
509   */   */
510  function media_metadata_used($refresh = false) {  function media_metadata_used($refresh = FALSE) {
511    static $list = array();    static $list = array();
512    
513    if (!count($list) || $refresh) {    if (!count($list) || $refresh) {
# Line 525  function media_metadata_used($refresh = Line 525  function media_metadata_used($refresh =
525  }  }
526    
527  /**  /**
528   * Checks for existance of a media file by the same as another in a folder.   * Check for existance of a media file by the same as another in a folder.
529   *   *
530   * @param $name   * @param $name
531   *   Name for media file.   *   Name for media file.
# Line 534  function media_metadata_used($refresh = Line 534  function media_metadata_used($refresh =
534   * @param $ignore   * @param $ignore
535   *   Gives us the ability to ignore an edited item   *   Gives us the ability to ignore an edited item
536   * @return   * @return
537   *   Returns true on success, false on failure.   *   Returns TRUE on success, FALSE on failure.
538   */   */
539  function media_exists($name, $fid, $ignore = false) {  function media_exists($name, $fid, $ignore = FALSE) {
540    return (db_result(db_query("SELECT mid FROM {media} WHERE name='%s' AND fid=%d AND mid<>%d", $name, $fid, $ignore)) !== false);    return (db_result(db_query("SELECT mid FROM {media} WHERE name='%s' AND fid=%d AND mid<>%d", $name, $fid, $ignore)) !== FALSE);
541  }  }
542    
543  /**  /**
544   * Returns the public filename for the item   * Retrieve the public filename for the item.
545   *   *
546   * @param $media   * @param $media
547   *   The media object.   *   The media object.
548   * @return   * @return
549   *   Returns generated filename.   *   Returns generated filename.
550   */   */
551  function media_filename($media, $workspace = 'mmedia') {  function media_filename($media, $options = array()) {
552      $workspace = !empty($options['workspace']) ? $options['workspace'] : 'mmedia';
553      $public = !empty($options['public']) ? $options['public'] : FALSE;
554    
555    if (!mapi_directory_check_location($media->path, $workspace) || (!$media->public && !$public)) {    if (!mapi_directory_check_location($media->path, $workspace) || (!$media->public && !$public)) {
556      return 'media/'. $media->mid .'/transfer';      return 'media/'. $media->mid .'/transfer';
557    }    }
# Line 562  function media_filename($media, $workspa Line 565  function media_filename($media, $workspa
565  }  }
566    
567  /**  /**
568   * Provides the folder path for the media   * Provide the folder path for the media.
569   *   *
570   * @param $media   * @param $media
571   *   A media object   *   A media object
572   * @param $refresh   * @param $refresh
573   *   Forces refresh of directory buffering.   *   Forces refresh of directory buffering.
574   */   */
575  function media_folder($media, $workspace = 'mmedia', $refresh = false) {  function media_folder($media, $workspace = 'mmedia', $refresh = FALSE) {
576    static $paths;    static $paths;
577    
578    // free memory if necessary    // free memory if necessary
# Line 579  function media_folder($media, $workspace Line 582  function media_folder($media, $workspace
582    
583    // only works if the media is installed    // only works if the media is installed
584    if (!is_object($media)) {    if (!is_object($media)) {
585      return false;      return FALSE;
586    }    }
587    
588    // only call the database if it is not already loaded    // only call the database if it is not already loaded
# Line 599  function media_default_profile($context, Line 602  function media_default_profile($context,
602    }    }
603    switch ($context['context']) {    switch ($context['context']) {
604      case 'teaser':      case 'teaser':
605        $profile = variable_get('mmedia_teaser_profile', null);        $profile = variable_get('mmedia_teaser_profile', NULL);
606        break;        break;
607      case 'body':      case 'body':
608        $profile = variable_get('mmedia_node_profile', null);        $profile = variable_get('mmedia_node_profile', NULL);
609        break;        break;
610    }    }
611    
612    if (!$profile) {    if (!$profile) {
613      $profile = variable_get('mapi_profile_default', null);      $profile = variable_get('mapi_profile_default', NULL);
614    }    }
615    
616    return $profile;    return $profile;
# Line 618  function media_default_profile($context, Line 621  function media_default_profile($context,
621   */   */
622  function media_compare_files($file1, $file2, $attributes = array()) {  function media_compare_files($file1, $file2, $attributes = array()) {
623    if (empty($attributes)) {    if (empty($attributes)) {
624      return false;      return FALSE;
625    }    }
626    
627    // must match extension    // must match extension
628    if (mapi_file_ext($file1) != mapi_file_ext($file2)) {    if (mapi_file_ext($file1) != mapi_file_ext($file2)) {
629      return false;      return FALSE;
630    }    }
631    
632    // now check general attributes within the system    // now check general attributes within the system
# Line 631  function media_compare_files($file1, $fi Line 634  function media_compare_files($file1, $fi
634    $dim2 = mapi_extension_dimensions($file2);    $dim2 = mapi_extension_dimensions($file2);
635    foreach ($attributes as $key) {    foreach ($attributes as $key) {
636      if ($dim1[$key] != $dim2[$key]) {      if ($dim1[$key] != $dim2[$key]) {
637        return false;        return FALSE;
638      }      }
639    }    }
640    
641    return true;    return TRUE;
642  }  }

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.2