Source for file FileSystemFileDataPart.class.php

Documentation is available at FileSystemFileDataPart.class.php

  1. <?php
  2.  
  3. /**
  4. * This Part stores the File data on the filesytem, in a file whose name is the id
  5. * of our record.
  6. *
  7. * Important configuration options:
  8. * - 'use_filesystem_for_files'
  9. * - 'file_data_path'
  10. *
  11. * <p>
  12. * OSID Version: 2.0
  13. * </p>
  14. *
  15. * @package harmoni.osid_v2.repository
  16. *
  17. * @copyright Copyright &copy;2005, Middlebury College
  18. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
  19. *
  20. * @version $Id: FileSystemFileDataPart.class.php,v 1.7 2007/09/04 20:25:46 adamfranco Exp $
  21. */
  22. class FileSystemFileDataPart
  23. extends FileDataPart
  24. {
  25.  
  26. /**
  27. * Get the value for this Part.
  28. *
  29. * @return object mixed (original type: java.io.Serializable)
  30. *
  31. * @throws object RepositoryException An exception with one of
  32. * the following messages defined in
  33. * org.osid.repository.RepositoryException may be thrown: {@link }
  34. * org.osid.repository.RepositoryException#OPERATION_FAILED
  35. * OPERATION_FAILED}, {@link }
  36. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  37. * PERMISSION_DENIED}, {@link }
  38. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  39. * CONFIGURATION_ERROR}, {@link }
  40. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  41. * UNIMPLEMENTED}
  42. *
  43. * @access public
  44. */
  45. function getValue() {
  46. $file = $this->_getFilePath();
  47. if (!file_exists($file))
  48. return "";
  49. if (!is_readable($file))
  50. throwError(new Error(RepositoryException::OPERATION_FAILED()
  51. .": '$file' could not read.", "FileSystemFileDataPart", true));
  52. return file_get_contents($file);
  53. }
  54. /**
  55. * Update the value for this Part.
  56. *
  57. * @param object mixed $value (original type: java.io.Serializable)
  58. *
  59. * @throws object RepositoryException An exception with one of
  60. * the following messages defined in
  61. * org.osid.repository.RepositoryException may be thrown: {@link }
  62. * org.osid.repository.RepositoryException#OPERATION_FAILED
  63. * OPERATION_FAILED}, {@link }
  64. * org.osid.repository.RepositoryException#PERMISSION_DENIED
  65. * PERMISSION_DENIED}, {@link }
  66. * org.osid.repository.RepositoryException#CONFIGURATION_ERROR
  67. * CONFIGURATION_ERROR}, {@link }
  68. * org.osid.repository.RepositoryException#UNIMPLEMENTED
  69. * UNIMPLEMENTED}, {@link }
  70. * org.osid.repository.RepositoryException#NULL_ARGUMENT
  71. * NULL_ARGUMENT}
  72. *
  73. * @access public
  74. */
  75. function updateValue($value) {
  76. $file = $this->_getFilePath();
  77. if (!$handle = fopen($file, 'w')) {
  78. throwError(new Error(RepositoryException::OPERATION_FAILED()
  79. .": '$file' could not be opened for writing.", "FileSystemFileDataPart", true));
  80. }
  81. if (fwrite($handle, $value) === FALSE) {
  82. fclose($handle);
  83. throwError(new Error(RepositoryException::OPERATION_FAILED()
  84. .": '$file' could not be written to.", "FileSystemFileDataPart", true));
  85. }
  86. fclose($handle);
  87. // Check to see if the size is in the database
  88. $dbHandler = Services::getService("DatabaseManager");
  89. $query = new SelectQuery;
  90. $query->addTable("dr_file");
  91. $query->addColumn("COUNT(*) as count");
  92. $query->addWhere("id = '".$this->_recordId->getIdString()."'");
  93. $result =$dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  94. // If it already exists, use an update query.
  95. if ($result->field("count") > 0) {
  96. $query = new UpdateQuery;
  97. $query->setTable("dr_file");
  98. $query->setColumns(array("size"));
  99. $query->setValues(array("'".strlen($value)."'"));
  100. $query->addWhere("id = '".$this->_recordId->getIdString()."'");
  101. }
  102. // If it doesn't exist, use an insert query.
  103. else {
  104. $query = new InsertQuery;
  105. $query->setTable("dr_file");
  106. $query->setColumns(array("id","size"));
  107. $query->setValues(array("'".$this->_recordId->getIdString()."'",
  108. "'".strlen($value)."'"));
  109. }
  110. $result->free();
  111. // run the query
  112. $dbHandler->query($query, $this->_configuration->getProperty("database_index"));
  113. $this->_asset->updateModificationDate();
  114. }
  115. /**
  116. * Build the file path where we will store our data
  117. *
  118. * @return string
  119. * @access public
  120. * @since 2/16/05
  121. */
  122. function _getFilePath () {
  123. if (!$this->_configuration->getProperty('file_data_path'))
  124. throwError(new Error(RepositoryException::CONFIGURATION_ERROR()
  125. .": 'file_data_path' was not specified.", "FileSystemFileDataPart", true));
  126. $path = $this->_configuration->getProperty('file_data_path');
  127. if (!file_exists($path))
  128. throwError(new Error(RepositoryException::CONFIGURATION_ERROR()
  129. .": The 'file_data_path' specified, '$path', was does not exist.", "FileSystemFileDataPart", true));
  130.  
  131. if (!is_readable($path))
  132. throwError(new Error(RepositoryException::CONFIGURATION_ERROR()
  133. .": The 'file_data_path' specified, '$path', is not readable.", "FileSystemFileDataPart", true));
  134. if (!is_writable($path))
  135. throwError(new Error(RepositoryException::CONFIGURATION_ERROR()
  136. .": The 'file_data_path' specified, '$path', is not writable.", "FileSystemFileDataPart", true));
  137. return $this->_configuration->getProperty('file_data_path')."/".$this->_recordId->getIdString();
  138. }
  139. }

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