class MigrateFileUri in Migrate 7.2
Handle cases where we're handed a URI, or local filespec, representing a file to be imported to Drupal.
Hierarchy
- class \MigrateFileBase implements MigrateFileInterface
- class \MigrateFile
- class \MigrateFileUri
- class \MigrateFile
Expanded class hierarchy of MigrateFileUri
3 string references to 'MigrateFileUri'
- BeerNodeMigration::__construct in migrate_example/
beer.inc - General initialization of a Migration object.
- MigrateFileFieldBaseHandler::fields in plugins/
destinations/ fields.inc - Implementation of MigrateFieldHandler::fields().
- MigrateFileFieldBaseHandler::prepare in plugins/
destinations/ fields.inc - Implementation of MigrateFieldHandler::prepare().
File
- plugins/
destinations/ file.inc, line 341 - Support for file entity as destination. Note that File Fields have their own destination in fields.inc
View source
class MigrateFileUri extends MigrateFile {
/**
* The source directory for the file, relative to which the value (source
* file) will be taken.
*
* @var string
*/
protected $sourceDir = '';
/**
* The full path to the source file.
*
* @var string
*/
protected $sourcePath = '';
/**
* Whether to apply rawurlencode to the components of an incoming file path.
*/
protected $urlEncode = TRUE;
public function __construct($arguments = array(), $default_file = NULL) {
parent::__construct($arguments, $default_file);
if (isset($arguments['source_dir'])) {
$this->sourceDir = rtrim($arguments['source_dir'], "/\\");
}
if (isset($arguments['urlencode'])) {
$this->urlEncode = $arguments['urlencode'];
}
}
/**
* Implementation of MigrateFileInterface::fields().
*
* @return array
*/
public static function fields() {
return parent::fields() + array(
'source_dir' => t('Subfield: <a href="@doc">Path to source file.</a>', array(
'@doc' => 'http://drupal.org/node/1540106#source_dir',
)),
'urlencode' => t('Option: <a href="@doc">Encode all segments of the incoming path (defaults to TRUE).</a>', array(
'@doc' => 'http://drupal.org/node/1540106#urlencode',
)),
);
}
/**
* Implementation of MigrateFile::copyFile().
*
* @param $destination
* Destination within Drupal.
*
* @return bool
* TRUE if the copy succeeded, FALSE otherwise.
*/
protected function copyFile($destination) {
if ($this->urlEncode) {
// Perform the copy operation, with a cleaned-up path.
$this->sourcePath = self::urlencode($this->sourcePath);
}
try {
$copied = copy($this->sourcePath, $destination);
if ($copied == FALSE) {
$migration = Migration::currentMigration();
$migration
->saveMessage(t('The specified file %file could not be copied to %destination', array(
'%file' => $this->sourcePath,
'%destination' => $destination,
)));
}
return $copied;
} catch (Exception $e) {
$migration = Migration::currentMigration();
$migration
->saveMessage(t('The specified file %file could not be copied to %destination: "%exception_msg"', array(
'%file' => $this->sourcePath,
'%destination' => $destination,
'%exception_msg' => $e
->getMessage(),
)));
return FALSE;
}
}
/**
* Urlencode all the components of a remote filename.
*
* @param $filename
*
* @return string
*/
public static function urlencode($filename) {
// Only apply to a full URL
if (strpos($filename, '://')) {
$components = explode('/', $filename);
foreach ($components as $key => $component) {
$components[$key] = rawurlencode($component);
}
$filename = implode('/', $components);
// Actually, we don't want certain characters encoded
$filename = str_replace('%3A', ':', $filename);
$filename = str_replace('%3F', '?', $filename);
$filename = str_replace('%26', '&', $filename);
$filename = str_replace('%40', '@', $filename);
}
return $filename;
}
/**
* Implementation of MigrateFileInterface::processFiles().
*
* @param $value
* The URI or local filespec of a file to be imported.
* @param $owner
* User ID (uid) to be the owner of the file.
*
* @return object
* The file entity being created or referenced.
*/
public function processFile($value, $owner) {
// Identify the full path to the source file
if (!empty($this->sourceDir)) {
$this->sourcePath = rtrim($this->sourceDir, "/\\") . '/' . ltrim($value, "/\\");
}
else {
$this->sourcePath = $value;
}
if (empty($this->destinationFile)) {
$path = explode('?', $this->sourcePath);
$this->destinationFile = basename($path[0]);
}
// MigrateFile has most of the smarts - the key is that it will call back
// to our copyFile() implementation.
$file = parent::processFile($value, $owner);
return $file;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MigrateFile:: |
protected | property | The destination directory within Drupal. | |
MigrateFile:: |
protected | property | The filename relative to destinationDir to which to save the current file. | |
MigrateFileBase:: |
protected | property | An optional file object to use as a default starting point for building the file entity. | |
MigrateFileBase:: |
protected | property | How to handle destination filename collisions. | |
MigrateFileBase:: |
protected | property | Set to TRUE to prevent file deletion on rollback. | |
MigrateFileBase:: |
protected | function | Setup a file entity object suitable for saving. | |
MigrateFileBase:: |
constant | Extension of the core FILE_EXISTS_* constants, offering an alternative to reuse the existing file if present as-is (core only offers the options of replacing it or renaming to avoid collision). | ||
MigrateFileBase:: |
protected | function | If asked to preserve files from deletion on rollback, add a file_usage entry. | |
MigrateFileUri:: |
protected | property | The source directory for the file, relative to which the value (source file) will be taken. | |
MigrateFileUri:: |
protected | property | The full path to the source file. | |
MigrateFileUri:: |
protected | property | Whether to apply rawurlencode to the components of an incoming file path. | |
MigrateFileUri:: |
protected | function |
Implementation of MigrateFile::copyFile(). Overrides MigrateFile:: |
|
MigrateFileUri:: |
public static | function |
Implementation of MigrateFileInterface::fields(). Overrides MigrateFile:: |
|
MigrateFileUri:: |
public | function |
Implementation of MigrateFileInterface::processFiles(). Overrides MigrateFile:: |
|
MigrateFileUri:: |
public static | function | Urlencode all the components of a remote filename. | |
MigrateFileUri:: |
public | function |
Overrides MigrateFile:: |