diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-10-15 17:55:55 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-10-15 17:55:55 +0000 |
commit | 2fb451c87241b5dfb44b45129978e1bb9b971a26 (patch) | |
tree | faca7f0132377c12a8a9f4397e5d4d91fdc0fd99 /includes | |
parent | 977d635bb1705ecebae8783a5e629214986eaddf (diff) | |
download | brdo-2fb451c87241b5dfb44b45129978e1bb9b971a26.tar.gz brdo-2fb451c87241b5dfb44b45129978e1bb9b971a26.tar.bz2 |
- Patch #604618 by Crell, JacobSingh: create a common interface for Archive operations so we can handle .zip, .tar.gz.
Diffstat (limited to 'includes')
-rw-r--r-- | includes/archiver.inc | 67 | ||||
-rw-r--r-- | includes/common.inc | 50 |
2 files changed, 117 insertions, 0 deletions
diff --git a/includes/archiver.inc b/includes/archiver.inc new file mode 100644 index 000000000..8d514794a --- /dev/null +++ b/includes/archiver.inc @@ -0,0 +1,67 @@ +<?php + +/** + * @file + * Shared classes and interfaces for the archiver system. + */ + +/** + * Common interface for all Archiver classes. + */ +interface ArchiverInterface { + + /** + * Contructor for a new archiver instance. + * + * @param $file_path + * The full system path of the archive to manipulate. Only local files + * are supported. If the file does not yet exist, it will be created if + * appropriate. + */ + public function __construct($file_path); + + /** + * Add the specified file or directory to the archive. + * + * @param $file_path + * The full system path of the file or directory to add. Only local files + * and directories are supported. + * @return + * The called object. + */ + public function add($file_path); + + /** + * Remove the specified file from the archive. + * + * @param $path + * The file name relative to the root of the archive to remove. + * @return + * The called object. + */ + public function remove($path); + + /** + * Extract multiple files in the archive to the specified path. + * + * @param $path + * A full system path of the directory to which to extract files. + * @param $files + * Optionally specify a list of files to be extracted. Files are + * relative to the root of the archive. If not specified, all files + * in the archive will be extracted + * @return + * The called object. + */ + public function extract($path, Array $files = array()); + + /** + * List all files in the archive. + * + * @return + * An array of file names relative to the root of the archive, or + * an iteratable object that resolves to such a list. + */ + public function listContents(); +} + diff --git a/includes/common.inc b/includes/common.inc index c3939b9cb..a72039566 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -5939,3 +5939,53 @@ function xmlrpc($url) { return call_user_func_array('_xmlrpc', $args); } +/** + * Retrieve a list of all available archivers. + */ +function archiver_get_info() { + $archiver_info = &drupal_static(__FUNCTION__, array()); + + if (empty($archiver_info)) { + $cache = cache_get('archiver_info'); + if ($cache === FALSE) { + // Rebuild the cache and save it. + $archiver_info = module_invoke_all('archiver_info'); + drupal_alter('archiver_info', $archiver_info); + uasort($archiver_info, 'drupal_sort_weight'); + cache_set('archiver_info', $archiver_info); + } + else { + $archiver_info = $cache->data; + } + } + + return $archiver_info; +} + +/** + * Create the appropriate archiver for the specified file. + * + * @param $file + * The full path of the archive file. Note that stream wrapper + * paths are supported. + * @return + * A newly created instance of the archiver class appropriate + * for the specified file, already bound to that file. + */ +function archiver_get_archiver($file) { + $archiver_info = archiver_get_info(); + + foreach ($archiver_info as $implementation) { + foreach ($implementation['extensions'] as $extension) { + // Because extensions may be multi-part, such as .tar.gz, + // we cannot use simpler approaches like substr() or pathinfo(). + // This method isn't quite as clean but gets the job done. + // Also note that the file may not yet exist, so we cannot rely + // on fileinfo() or other disk-level utilities. + if (strrpos($file, '.' . $extension) === strlen($file) - strlen('.' . $extension)) { + return new $implementation['class']($file); + } + } + } +} + |