Lithron.LMediaManager
[ class tree: Lithron.LMediaManager ] [ index: Lithron.LMediaManager ] [ all elements ]

Source for file LMediaManagerCore.php

Documentation is available at LMediaManagerCore.php

  1. <?php
  2.  
  3.  
  4. /**
  5. * Class file.
  6. *
  7. * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
  8. * @copyright 2005, diemeisterei GmbH. All rights reserved.
  9. * @author $Author: schmunk $
  10. * @version $Revision: 268 $ $Date: 2006-06-11 17:25:11 +0200 (So, 11 Jun 2006) $
  11. * @package Lithron.LMediaManager
  12. * @subpackage Core
  13. */
  14.  
  15. /**
  16. * Provides database access and administration functions
  17. *
  18. * @package Lithron.LMediaManager
  19. * @subpackage Core
  20. */
  21. Prado :: setPathOfAlias("LMediaManager", dirname(__FILE__));
  22. Prado :: using("LMediaManager.db.propel.*");
  23. Prado :: using("LMediaManager.db.propel.l_media_manager.*");
  24.  
  25. class LMediaManagerCore extends TControl
  26. {
  27.  
  28. /**
  29. * @return media files directory, defined in application.xml
  30. */
  31. function getMediaDir()
  32. {
  33. $param= $this->Application->getParameters();
  34. return $param->itemAt("MediaDir");
  35. }
  36.  
  37. /**
  38. * scans media directory and updates database
  39. */
  40. public function updateDatabase($deleteMissing= false)
  41. {
  42. if ($cache= $this->Application->Cache)
  43. {
  44. $cache->delete("LMediaManager:sets");
  45. }
  46. $files= $this->scanDirectory($this->getMediaDir());
  47. $this->updateMediaFiles($files, $deleteMissing);
  48. }
  49.  
  50. /**
  51. * returns records from database
  52. *
  53. * Either a Criteria object or a Integer id.
  54. * @param Criteria|integer
  55. */
  56. public function queryRecords($c= null)
  57. {
  58. if (is_numeric($c))
  59. {
  60. Prado :: trace("Querying database by id($c)", "Lithron.LMediaManager");
  61. return LMediaManagerFilesPeer :: retrieveByPK($c, $this->Application->getModule("database")->getConnection("l_media_manager", "propel"));
  62. }
  63. if ($c === null)
  64. $c= new Criteria;
  65. Prado :: trace("Querying database by criteria", "Lithron.LMediaManager");
  66. $result= LMediaManagerFilesPeer :: doSelect($c, $this->Application->getModule("database")->getConnection("l_media_manager", "propel"));
  67. return $result;
  68. }
  69.  
  70. /**
  71. * scans media directory
  72. */
  73. private function scanDirectory($dir, $files= array ())
  74. {
  75. if (!is_dir($dir))
  76. {
  77. Prado :: log("Invalid media files directory", 4, "Lithron.LMediaManager");
  78. return;
  79. }
  80. $_scan= scandir($dir);
  81. foreach ($_scan AS $item)
  82. {
  83. $location= $dir . "/" . $item;
  84. if (substr($item, 0, 1) == ".")
  85. continue;
  86. if (is_dir($location))
  87. {
  88. $files= $this->scanDirectory($location, $files);
  89. }
  90. if (is_file($location))
  91. {
  92. // may need rework due to performance :( TODO
  93. $_data['name']= $item;
  94. $_data['directory']= $dir;
  95. $_data['md5']= md5_file($location);
  96. $_data['size']= filesize($location);
  97. #var_dump($_data);
  98. $files[]= $_data;
  99. }
  100. }
  101. #var_dump($files);
  102. return $files;
  103. }
  104. /**
  105. * compares files and updates database
  106. */
  107. private function updateMediaFiles($files, $deleteMissing= false)
  108. {
  109. $dbList= $this->queryRecords();
  110. $processedList= array ();
  111. $conn= $this->Application->getModule("database")->getConnection("l_media_manager", "propel");
  112. $magick= new LImage;
  113.  
  114. if (is_array($files))
  115. // walk throgh files from filesystem
  116. foreach ($files AS $file)
  117. {
  118. $_found= false;
  119. if (is_array($dbList))
  120. // compare with database
  121. foreach ($dbList AS $key => $record)
  122. {
  123. if ($record->getMd5() == $file['md5'])
  124. {
  125. unset ($dbList[$key]);
  126. $_found= true;
  127. break;
  128. }
  129. }
  130. foreach ($processedList AS $key => $record)
  131. {
  132. # if ($record['directory'].$record['name'] == $file['directory'].$file['name'])
  133. # continue;
  134. if ($record['md5'] == $file['md5'])
  135. {
  136. Prado :: log("Duplicate media file '{$file['name']}' - FILE WAS NOT ADDED", 5, "Lithron.LMediaManager");
  137. continue 2;
  138. }
  139. }
  140.  
  141.  
  142. if ($_found != true)
  143. {
  144. Prado :: trace("New media file '{$file['name']}'", "Lithron.LMediaManager");
  145. // create new file if md5 not found
  146. $new= new LMediaManagerFiles;
  147. $_data= $magick->doIdentify($file['directory'] . "/" . $file['name']);
  148. if (is_array($_data))
  149. foreach ($_data AS $key => $d)
  150. {
  151. $file[$key]= $d;
  152. // TODO ?
  153. @ $new->setType($file['type']);
  154. $new->setSize($file['size']);
  155. @ $new->setSizeShort($file['size_short']);
  156. @ $new->setWidth($file['width']);
  157. @ $new->setHeight($file['height']);
  158. @ $new->setColorspace($file['colorspace']);
  159. @ $new->setTransparent($file['transparent']);
  160. @ $new->setResolutionX($file['resolution_x']);
  161. @ $new->setResolutionY($file['resolution_y']);
  162. @ $new->setColors($file['colors']);
  163. @ $new->setImageDepth($file['image_depth']);
  164. }
  165. $new->setDescription($file['name']);
  166. }
  167. else
  168. {
  169. Prado :: trace("Existing media file '{$file['name']}' found", "Lithron.LMediaManager");
  170. // open existing file and update info
  171. $c= new Criteria;
  172. $c->add(LMediaManagerFilesPeer :: MD5, $file['md5']);
  173. $result= LMediaManagerFilesPeer :: doSelect($c, $conn);
  174. $new= $result[0];
  175. }
  176.  
  177. $new->setFileName($file['name']);
  178. $new->setMd5($file['md5']);
  179. $new->setDirectory($file['directory']);
  180. $new->setStatus(true);
  181. $new->save();
  182.  
  183. $processedList[]= $file;
  184. }
  185.  
  186. if (is_array($dbList))
  187. // set missing files in db to status false
  188. foreach ($dbList AS $file)
  189. {
  190. Prado :: trace("Non-existing file in database '{$file->getFileName()}'", "Lithron.LMediaManager");
  191. $c= new Criteria;
  192. $c->add(LMediaManagerFilesPeer :: MD5, $file->getMd5());
  193. $result= LMediaManagerFilesPeer :: doSelect($c, $conn);
  194. foreach ($result AS $record)
  195. {
  196. if ($deleteMissing)
  197. {
  198. Prado :: trace("Deleting media file ", "Lithron.LMediaManager");
  199. $record->delete();
  200. }
  201. else
  202. {
  203. $record->setStatus(false);
  204. $record->save();
  205. }
  206. }
  207. }
  208.  
  209. }
  210.  
  211. }
  212. ?>

Documentation generated on Tue, 20 Jun 2006 05:15:16 +0200 by phpDocumentor 1.3.0RC4