Source for file FileSystemFileRecord.class.php

Documentation is available at FileSystemFileRecord.class.php

  1. <?php
  2.  
  3. require_once(dirname(__FILE__)."/Fields/FileSystemFileDataPart.class.php");
  4. require_once(dirname(__FILE__)."/Fields/FileNamePart.class.php");
  5. require_once(dirname(__FILE__)."/Fields/FileSizePart.class.php");
  6. require_once(dirname(__FILE__)."/Fields/MimeTypePart.class.php");
  7. require_once(dirname(__FILE__)."/Fields/ThumbnailDataPart.class.php");
  8. require_once(dirname(__FILE__)."/Fields/ThumbnailMimeTypePart.class.php");
  9. require_once(HARMONI."/oki2/repository/HarmoniPartIterator.class.php");
  10.  
  11. /**
  12. * This Record will use a FileSystemFileDataPart to store the file data in the
  13. * filesystem.
  14. *
  15. * Important configuration options:
  16. * - 'use_filesystem_for_files'
  17. * - 'file_data_path'
  18. *
  19. * <p>
  20. * OSID Version: 2.0
  21. * </p>
  22. *
  23. * @package harmoni.osid_v2.repository
  24. *
  25. * @copyright Copyright &copy;2005, Middlebury College
  26. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  27. *
  28. * @version $Id: FileSystemFileRecord.class.php,v 1.9 2007/09/11 17:40:57 adamfranco Exp $
  29. */
  30. class FileSystemFileRecord
  31. extends FileRecord
  32. {
  33. function FileSystemFileRecord( $recordStructure, $id, $configuration, $asset ) {
  34. $this->_id =$id;
  35. $this->_recordStructure =$recordStructure;
  36. $this->_configuration = $configuration;
  37. $this->_asset =$asset;
  38. $idManager = Services::getService("Id");
  39. $this->_parts = array();
  40. $this->_parts['FILE_DATA'] = new FileSystemFileDataPart(
  41. $recordStructure->getPartStructure($idManager->getId('FILE_DATA')),
  42. $this->_id,
  43. $this->_configuration);
  44. $this->_parts['FILE_NAME'] = new FileNamePart(
  45. $recordStructure->getPartStructure($idManager->getId('FILE_NAME')),
  46. $this->_id,
  47. $this->_configuration);
  48. $this->_parts['FILE_SIZE'] = new FileSizePart(
  49. $recordStructure->getPartStructure($idManager->getId('FILE_SIZE')),
  50. $this->_id,
  51. $this->_configuration);
  52. $this->_parts['MIME_TYPE'] = new MimeTypePart(
  53. $recordStructure->getPartStructure($idManager->getId('MIME_TYPE')),
  54. $this->_id,
  55. $this->_configuration);
  56. $this->_parts['THUMBNAIL_DATA'] = new ThumbnailDataPart(
  57. $recordStructure->getPartStructure($idManager->getId('THUMBNAIL_DATA')),
  58. $this->_id,
  59. $this->_configuration);
  60. $this->_parts['THUMBNAIL_MIME_TYPE'] = new ThumbnailMimeTypePart(
  61. $recordStructure->getPartStructure($idManager->getId('THUMBNAIL_MIME_TYPE')),
  62. $this->_id,
  63. $this->_configuration);
  64. }
  65.  
  66.  
  67. /**
  68. * Delete a Part and all its Parts.
  69. *
  70. * @param object Id $partId
  71. *
  72. * @throws object RepositoryException An exception with one of
  73. * the following messages defined in
  74. * org.osid.repository.RepositoryException may be thrown: {@link }
  75. * org.osid.repository.RepositoryException#OPERATION_FAILED
  76. * OPERATION_FAILED}, {@link }
  77. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  78. * PERMISSION_DENIED}, {@link }
  79. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  80. * CONFIGURATION_ERROR}, {@link }
  81. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  82. * UNIMPLEMENTED}, {@link }
  83. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  84. * NULL_ARGUMENT}, {@link }
  85. * org.osid.repository.RepositoryException#UNKNOWN_ID UNKNOWN_ID}
  86. *
  87. * @access public
  88. */
  89. function deletePart($partId) {
  90. $string = $partId->getIdString();
  91. if (ereg("(.*)-(FILE_SIZE|FILE_NAME|FILE_DATA|MIME_TYPE|THUMBNAIL_DATA|THUMBNAIL_MIME_TYPE)",$string,$r)) {
  92. $recordId = $r[1];
  93. $field = $r[2];
  94. if ($this->_isLastPart($field)) {
  95. $dbHandler = Services::getService("DatabaseManager");
  96. // Delete the data
  97. $file = $this->_parts['FILE_DATA']->_getFilePath();
  98. if (!unlink($file))
  99. throwError(new Error(RepositoryException::OPERATION_FAILED()
  100. .": '$file' could not be deleted.", "FileSystemFileRecord", true));
  101. // Delete the thumbnail
  102. $query = new DeleteQuery();
  103. $query->setTable("dr_thumbnail");
  104. $query->setWhere("fk_file = '".$this->_id->getIdString()."'");
  105. $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  106. // Delete the data row in case we were switching from another type
  107. // that used it.
  108. $query = new DeleteQuery();
  109. $query->setTable("dr_file_data");
  110. $query->setWhere("fk_file = '".$this->_id->getIdString()."'");
  111. $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  112. // delete the file row.
  113. $query = new DeleteQuery();
  114. $query->setTable("dr_file");
  115. $query->setWhere("id = '".$this->_id->getIdString()."'");
  116. $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  117. } else if ($field != "FILE_SIZE") {
  118. $this->_parts[$field]->updateValue("NULL");
  119. }
  120. } else {
  121. throwError(new Error(RepositoryException::UNKNOWN_ID().": $string", "FileSystemFileRecord", true));
  122. }
  123. }
  124. /**
  125. * Return TRUE if the Part of the passed Id is the last one, and the whole schebang should be deleted.
  126. *
  127. * WARNING: NOT IN OSID - Use at your own risk
  128. *
  129. * @param string $idString
  130. * @return boolean
  131. * @access private
  132. * @since 10/25/04
  133. */
  134. function _isLastPart ($idString) {
  135. $dbHandler = Services::getService("DatabaseManager");
  136. // Check to see if the data is in the database
  137. $query = new SelectQuery;
  138. $query->addTable("dr_file");
  139. // $query->addTable("dr_file_data", LEFT_JOIN, "dr_file.id = dr_file_data.fk_file");
  140. $query->addTable("dr_thumbnail", LEFT_JOIN, "dr_file.id = dr_thumbnail.fk_file");
  141. $query->addTable("dr_mime_type", LEFT_JOIN, "dr_file.fk_mime_type = file_mime_type.id", "file_mime_type");
  142. $query->addTable("dr_mime_type", LEFT_JOIN, "dr_thumbnail.fk_mime_type = thumbnail_mime_type.id", "thumbnail_mime_type");
  143. $query->addColumn("filename");
  144. $query->addColumn("size");
  145. $query->addColumn("file_mime_type.type", "file_type");
  146. // $query->addColumn("dr_file_data.data", "file_data");
  147. $query->addColumn("thumbnail_mime_type.type", "thumbnail_type");
  148. $query->addColumn("dr_thumbnail.data", "thumbnail_data");
  149. $query->addWhere("dr_file.id = '".$this->_id->getIdString()."'");
  150. $result =$dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  151. $file = $this->_parts['FILE_DATA']->_getFilePath();
  152. if (!$result->getNumberOfRows() && !file_exists($file)) {
  153. $result->free();
  154. return TRUE;
  155. }
  156. $fields = array('filename', 'size', 'file_type', 'thumbnail_type', 'thumbnail_data');
  157. $countValues = 0;
  158. foreach ($fields as $field) {
  159. if ($result->field($field))
  160. $countValues++;
  161. }
  162. $result->free();
  163. if (file_exists($file))
  164. $countValues++;
  165. if ($countValues <= 1)
  166. return TRUE;
  167. else
  168. return FALSE;
  169. }
  170. }

Documentation generated on Wed, 19 Sep 2007 10:23:15 -0400 by phpDocumentor 1.3.0RC3