Commit b450cd65 authored by Jon Stacey's avatar Jon Stacey

Removing resource module. This will become stream wrappers in core or will...

Removing resource module. This will become stream wrappers in core or will need a complete refresh in worst case scenario (stream wrappers doesn't make it).
parent c53a0eed
<?php
// $Id$
/**
* private:// stream wrapper class.
*/
class ResourcePrivateStreamWrapper extends ResourcePublicStreamWrapper {
private $pathKey = 'stream_private_path';
private $pathDefault = 'sites/default/files-private';
/**
* Return the HTML Url of a private file.
*/
function htmlUrl($url) {
$basepath = variable_get($this->pathKey, $this->pathDefault);
return url('system/files/' . parse_url($url, PHP_URL_PATH), array('absolute' => TRUE));
}
}
<?php
// $Id$
/**
* public:// stream wrapper class.
*
* This class provides support for storing publicly
* accessible files with the Drupal resource api.
*/
class ResourcePublicStreamWrapper extends ResourceStreamWrapper {
// A handle to the file opened by stream_open().
private $pathKey = 'stream_public_path';
private $pathDefault = 'sites/default/files';
/**
* Return the HTML Url of a public file.
*/
function htmlUrl($url) {
$basepath = variable_get($this->pathKey, $this->pathDefault);
$path = parse_url($url, PHP_URL_PATH);
return $GLOBALS['base_url'] . '/' . $basepath . '/' . str_replace('\\', '/', $path);
}
/**
* Interpolate the url path, adding the public files path.
*/
function interpolateUrl($url) {
$basepath = variable_get($this->pathKey, $this->pathDefault);
// just in case stream_public_path is s3://, ftp://, etc. Don't call PHP's
// realpath().
if (parse_url($basepath, PHP_URL_SCHEME)) {
$path = $basepath . parse_url($url, PHP_URL_PATH);;
}
else {
// interpolate relative paths for basepath, and strip relative paths from
// url path.
$path = realpath($basepath) . str_replace('/..','', parse_url($url, PHP_URL_PATH));
}
return $path;
}
/**
* Return the mime type of a file.
*/
function mime($url) {
return file_get_mimetype(basename($url));
}
}
<?php
// $Id$
/**
* Generic PHP stream wrapper interface.
* @see: http://php.net/manual/en/function.stream-wrapper-register.php
*/
interface StreamWrapperInterface {
public function stream_open($url, $mode, $options, &$opened_url);
public function stream_close();
// Undocumented @see: http://us.php.net/flock
public function stream_lock($operation);
public function stream_read($count);
public function stream_write($data);
public function stream_eof();
public function stream_seek($offset, $whence);
public function stream_flush();
public function stream_tell();
public function stream_stat();
public function unlink($url) ;
public function rename($fromUrl, $toUrl);
public function mkdir($url, $mode, $options);
public function rmdir($url, $options);
public function url_stat($url, $flags);
public function dir_opendir($url, $options);
public function dir_readdir() ;
public function dir_rewinddir();
public function dir_closedir() ;
}
/**
* Definition for a Resource stream wrapper in Drupal.
* Extends the StreamWrapperInterface with methods expected by the
* Drupal Resource Classes.
*/
interface ResourceStreamWrapperInterface extends StreamWrapperInterface {
/**
* Return the html accessible URL for a resource.
* @param $url
* Stream wrapper resource url.
* @return string
*/
function htmlUrl($url);
/**
* Return the mime type of a resource.
* @param $url
* Stream wrapper resource url.
* @return string
*/
function mime($url);
/**
* Return an absolute stream resource URL.
* @param $url
* Stream wrapper resource url.
* @return string
*/
function interpolateUrl($url);
}
/**
* A base class for Resource Stream Wrappers.
*
* This class provides a complete stream wrapper implementation. It passes
* incoming URL's through an interpolation method then recursively calls
* the invoking PHP filesystem function.
*
* DrupalStreamWrapper implementations need to override at least the interpolateUrl
* method to rewrite the URL before is it passed back into the calling function.
*
*/
abstract class ResourceStreamWrapper implements ResourceStreamWrapperInterface {
private $handle = NULL;
function interpolateUrl($url) {
return $url;
}
function htmlUrl($url) {
return $url;
}
function mime($url) {
return 'application/octet-stream';
}
/**
* Support for fopen(), file_get_contents(), file_put_contents() etc.
*
* @param $path
* A string containing the path to the file to open.
* @param $mode
* The file mode ("r", "wb" etc.).
* @param $options
* A bit mask of STREAM_USE_PATH and STREAM_REPORT_ERRORS.
* @param &$opened_path
* A string containing the path actually opened.
* @return
* TRUE if file was opened successfully.
*/
public function stream_open($url, $mode, $options, &$opened_url) {
resource_debug("stream open: $url");
$url = $this->interpolateUrl($url);
$this->handle = ($options & STREAM_REPORT_ERRORS) ? fopen($url, $mode) : @fopen($url, $mode);
if ((bool)$this->handle && $options & STREAM_USE_PATH) $opened_url = $url;
resource_debug("stream opened: $this->handle");
return (bool)$this->handle;
}
// Undocumented PHP stream wrapper method.
function stream_lock($operation) {
if (in_array($operation, array(LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB))) {
return flock($this->handle, $operation);
}
return TRUE;
}
/**
* Support for fread(), file_get_contents() etc.
*
* @param $count
* Maximum number of bytes to be read.
* @return
* The string that was read, or FALSE in case of an error.
*/
public function stream_read($count) {
resource_debug("stream_read: $this->handle");
return fread($this->handle, $count);
}
/**
* Support for fwrite(), file_put_contents() etc.
*
* @param $data
* The string to be written.
* @return
* The number of bytes written.
*/
public function stream_write($data) {
resource_debug("stream_write: $this->handle");
return fwrite($this->handle, $data);
}
/**
* Support for feof().
*
* @return
* TRUE if end-of-file has been reached.
*/
public function stream_eof() {
return feof($this->handle);
}
/**
* Support for fseek().
*
* @param $offset
* The byte offset to got to.
* @param $whence
* SEEK_SET, SEEK_CUR, or SEEK_END.
* @return
* TRUE on success
*/
public function stream_seek($offset, $whence) {
return fseek($this->handle, $offset, $whence);
}
/**
* Support for fflush().
*
* @return
* TRUE if data was successfully stored (or there was no data to store).
*/
public function stream_flush() {
return fflush($this->handle);
}
/**
* Support for ftell().
*
* @return
* The current offset in bytes from the beginning of file.
*/
public function stream_tell() {
return ftell($this->handle);
}
/**
* Support for fstat().
*
* @return
* An array with file status, or FALSE in case of an error - see fstat()
* for a description of this array.
*/
public function stream_stat() {
return fstat($this->handle);
}
/**
* Support for fclose().
*
* @return
* TRUE if stream was successfully closed.
*/
public function stream_close() {
resource_debug("streamclose: $this->handle");
return fclose($this->handle);
}
/**
* Support for unlink().
*
* @param $url
* A string containing the url to the resource to delete.
* @return
* TRUE if resource was successfully deleted.
*/
public function unlink($url) {
return unlink($this->interpolateUrl($url));
}
/**
* Support for rename().
*
* @param $fromUrl,
* The url to the file to rename.
* @param $toUrl
* The new url for file.
*
* @return
* TRUE if file was successfully renamed.
*/
public function rename($fromUrl, $toUrl) {
return rename($this->interpolateUrl($fromUrl), $this->interpolateUrl($toUrl));
}
/**
* Support for mkdir().
*
* @param $url
* A string containing the url to the directory to create.
* @param $mode
* Permission flags - see mkdir().
* @param $options
* A bit mask of STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE.
* @return
* TRUE if directory was successfully created.
*/
public function mkdir($url, $mode, $options) {
$recursive = (bool)($options & STREAM_MKDIR_RECURSIVE);
if ($options & STREAM_REPORT_ERRORS) {
return mkdir($this->interpolateUrl($url), $mode, $recursive);
}
else {
return @mkdir($this->interpolateUrl($url), $mode, $recursive);
}
}
/**
* Support for rmdir().
*
* @param $url
* A string containing the url to the directory to delete.
* @param $options
* A bit mask of STREAM_REPORT_ERRORS.
* @return
* TRUE if directory was successfully removed.
*/
public function rmdir($url, $options) {
if ($options & STREAM_REPORT_ERRORS) {
return rmdir($this->interpolateUrl($url));
}
else {
return @rmdir($this->interpolateUrl($url));
}
}
/**
* Support for stat().
*
* @param $url
* A string containing the url to get information about.
* @param $flags
* A bit mask of STREAM_URL_STAT_LINK and STREAM_URL_STAT_QUIET.
* @return
* An array with file status, or FALSE in case of an error - see fstat()
* for a description of this array.
*/
public function url_stat($url, $flags) {
return ($flags & STREAM_URL_STAT_QUIET) ? @stat($this->interpolateUrl($url)) : stat($this->interpolateUrl($url));
}
/**
* Support for opendir().
*
* @param $url
* A string containing the url to the directory to open.
* @param $options
* Unknown (parameter is not documented in PHP Manual).
* @return
* TRUE on success.
*/
public function dir_opendir($url, $options) {
$this->handle = opendir($this->interpolateUrl($url));
return (bool)$this->handle;
}
/**
* Support for readdir().
*
* @return
* The next filename, or FALSE if there are no more files in the directory.
*/
public function dir_readdir() {
return readdir($this->handle);
}
/**
* Support for rewinddir().
*
* @return
* TRUE on success.
*/
public function dir_rewinddir() {
return rewinddir($this->handle);
}
/**
* Support for closedir().
*
* @return
* TRUE on success.
*/
public function dir_closedir() {
return closedir($this->handle);
}
}
<?php
// $Id$
/**
* The StreamWrapperManager provides a class for managing and querying
* user defined stream wrappers in PHP. PHP's internal stream_get_wrappers
* doesn't return the class registered to handle a stream. We need to be
* able to find the handler class for instantiation.
*/
class ResourceStreamWrapperManager {
// stream wrapper registry
private $wrappers = array();
// private constructor to enforce singleton.
private function __construct() { }
/**
* Load the singleton instance of the StreamWrapperManager.
* @return object:StreamWrapperManager
*/
public static function singleton() {
static $instance = NULL;
if (is_null($instance)) {
$instance = new ResourceStreamWrapperManager();
}
return $instance;
}
/**
* Register a class to handle a scheme.
* @param string $scheme URI scheme.
* @param string $class classname for the stream wrapper.
* @return bool result of stream_wrapper_register
* @see: http://us3.php.net/manual/en/function.stream-wrapper-register.php
*/
function register($scheme, $classname) {
$this->wrappers[$scheme] = $classname;
return stream_wrapper_register($scheme, $classname);
}
function unregister($scheme) {
if (stream_wrapper_unregister($scheme)) {
unset(self::$this->wrappers[$scheme]);
return TRUE;
}
return FALSE;
}
/**
* Return the streamwrapper classname for a given scheme.
* @param string $scheme stream scheme.
* @return mixed string is a scheme has a registered handler or FALSE.
*/
function classname($scheme) {
if (empty(self::$this->wrappers[$scheme])) {
return FALSE;
}
return self::$this->wrappers[$scheme];
}
/**
* Return the stream class name for a given scheme.
* @param string $scheme stream scheme.
* @return mixed string is a scheme has a registered handler or FALSE.
*/
function scheme($class) {
return array_search(self::$this->wrappers, $class);
}
/**
* Return the DrupalStreamWrapperManager's wrapper registry.
*/
function wrappers() {
return self::$this->wrappers;
}
}
// $Id$
Changelog for the Resource module.
January 2009
------------
* Initial development (dopry).
; $Id$
name = Resource
description = A stream wrapper enabled replacement for core filehandling.
package = Media
core = 7.x
files[] = resource.module
files[] = ResourceStreamWrapperManager.inc
files[] = ResourceStreamWrapper.inc
files[] = ResourcePublicStreamWrapper.inc
files[] = ResourcePrivateStreamWrapper.inc
<?php
// $Id$
function resource_install() {
drupal_install_schema('resource');
}
function resource_uninstall() {
drupal_uninstall_schema('resource');
}
function resource_enable() {
$path = file_directory_path();
if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) {
$scheme = 'public:';
}
else {
$scheme = 'private:';
}
//db_query('INSERT INTO {file_backup} SELECT * FROM {files}');
//db_query('DELETE FROM {files}');
db_query("
INSERT INTO {resource} (rid, uid, name, url, mimetype, size, status, timestamp)
SELECT
fid, uid, filename, REPLACE(filepath, '%s', '%s'), filemime, filesize, status, timestamp
FROM {files}
", $path, $scheme);
}
function resource_disable() {
db_query('DELETE FROM {resource}');
//db_query('INSERT INTO {files} SELECT * FROM {file_backup}');
}
function resource_schema() {
$schema['resource'] = array(
'description' => t('Stores information for Resources.'),
'fields' => array(
'rid' => array(
'description' => t('Primary Key: Unique Resource ID.'),
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'uid' => array(
'description' => t('The {users}.uid of the user who is associated with the resource.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'name' => array(
'description' => t('Name of the resource.'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'url' => array(
'description' => t('URL of the resource.'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'mimetype' => array(
'description' => t('The resource MIME type.'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'size' => array(
'description' => t('The size of the resource in bytes.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'status' => array(
'description' => t('A flag indicating whether file is temporary (0) or permanent (1).'),
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'timestamp' => array(
'description' => t('UNIX timestamp for when the file was added.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'uid' => array('uid'),
'status' => array('status'),
'timestamp' => array('timestamp'),
),
'primary key' => array('rid'),
);
return $schema;
}
This diff is collapsed.
<?php
// $Id$
/**
* @file
* This provides SimpleTests for the core stream wrapper functionality.
*/
/**
* Base class for file tests that adds some additional file specific
* assertions and helper functions.
*/
class ResourceStreamTestCase extends DrupalWebTestCase {
public $scheme = 'public://localhost/';
public $pathkey = 'stream_public_path';
public $pathdefault = '/sites/default/files';
public $filename = '';
public $dirname = '';
public $path = '';
public $handle = '';
public $streampath = '';
public $url = '';
function getInfo() {
return array(
'name' => t('ResourceStream Filesystem functions'),
'description' => t('Tests basic php stream functions.'),
'group' => t('Resource'),
);
}
function setUp() {
// create a known file and directory for testing.
resource_debug($this->scheme);
$this->realpath = realpath(variable_get($this->pathkey, $this->pathdefault));
$this->dirname = 'ResourceStreamTestCaseDir' . uniqid();
$this->filename = 'ResourceStreamTestCaseFile' . uniqid() . '.txt';
$this->path = '/'. $this->dirname . '/' . $this->filename;
$this->url = $this->scheme . $this->path;
resource_debug("realpath: $this->realpath");
$data = '1234567890abcdef';
mkdir($this->realpath .'/'. $this->dirname);
file_put_contents($this->realpath . $this->path, $data, LOCK_EX);
$this->assertTrue(file_exists($this->realpath . $this->path), 'Source file created.');
}
function tearDown() {
unlink($this->realpath . $this->path);
rmdir($this->realpath .'/'. $this->dirname);
}
/**
* fopen calls the stream_open method of a streamwrapper.
*/
function testFopen() {
$this->handle = fopen($this->url, 'r+');
$this->assertTrue($this->handle, 'fopen: '. $this->url);
}
// fread - stream_read
function testFread() {
$data = fread($this->handle, 4);
$this->assertTrue($data == '1234', 'fread 4 bytes, start pos 0');
$pos = ftell($this->handle);
$this->assertTrue($pos == 4, "ftell at pos 4($pos)");
$data = fread($this->handle, 4);
$this->assertTrue($data == '5678', 'fread 4 bytes, start pos 4');
// There is apparently a bug with either ftell or fseek working here.
// ftell should return the currently position. Seek is obviously
// moving the file pointer or the reads wouldn't match. Either fseek
// isn't updating the pos read by ftell, or ftell is actually returning
// bytes read.
$this->assertTrue(fseek($this->handle, 8), 'fseek to pos 0');
$pos = ftell($this->handle);
$this->assertTrue($pos == 8, "ftell at pos 8($pos)");
$data = fread($this->handle, 4);
$this->assertTrue($data == '90ab', "fread 4 bytes, start pos 4($data)");
$this->assertFalse(feof($this->handle), 'feof at pos 4');
$data = fgets($this->handle);
$this->assertTrue($data == 'cdef', 'fgets pos 12 - 15');
$this->assertTrue(feof($this->handle), 'feof at end of file');
$this->assertTrue(fseek($this->handle, 4), 'fseek to pos 4');
$this->assertTrue(fclose($this->handle), "Close file.");
}
// fwrite - stream_write
function testFwrite() {
$this->handle = fopen($this->scheme . $this->path, 'a+');
$this->assertTrue($this->handle, "Open $this->scheme$this->path in mode a+");
$data = '1234';
$this->assertTrue(fwrite($this->handle, $data), "fwrite ($data) to handle");
$this->assertTrue(fseek($this->handle, -4, SEEK_END), "Seek to pos before fwrite.");
$read = fread($this->handle, 4);
$this->assertTrue($data == $read, "fread ($read) == fwritten ($data) bytes");
$this->assertTrue(fclose($this->handle), "Close file.");
}
function testFilePutGetContents() {
$data = 'fedcba0987654321';