2015-09-15 04:52:02 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Assets;
|
|
|
|
|
|
|
|
use SilverStripe\Assets\Storage\AssetStore;
|
|
|
|
use SilverStripe\Core\Object;
|
|
|
|
use SilverStripe\ORM\DataList;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
/**
|
|
|
|
* Service to help migrate File dataobjects to the new APL.
|
|
|
|
*
|
|
|
|
* This service does not alter these records in such a way that prevents downgrading back to 3.x
|
|
|
|
*/
|
|
|
|
class FileMigrationHelper extends Object {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform migration
|
|
|
|
*
|
|
|
|
* @param string $base Absolute base path (parent of assets folder). Will default to BASE_PATH
|
|
|
|
* @return int Number of files successfully migrated
|
|
|
|
*/
|
|
|
|
public function run($base = null) {
|
|
|
|
if(empty($base)) {
|
|
|
|
$base = BASE_PATH;
|
|
|
|
}
|
|
|
|
// Check if the File dataobject has a "Filename" field.
|
|
|
|
// If not, cannot migrate
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2015-09-15 04:52:02 +02:00
|
|
|
if(!DB::get_schema()->hasField('File', 'Filename')) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set max time and memory limit
|
|
|
|
increase_time_limit_to();
|
|
|
|
increase_memory_limit_to();
|
|
|
|
|
|
|
|
// Loop over all files
|
|
|
|
$count = 0;
|
2016-03-02 06:18:10 +01:00
|
|
|
$originalState = Versioned::get_reading_mode();
|
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2015-09-15 04:52:02 +02:00
|
|
|
$filenameMap = $this->getFilenameArray();
|
|
|
|
foreach($this->getFileQuery() as $file) {
|
|
|
|
// Get the name of the file to import
|
|
|
|
$filename = $filenameMap[$file->ID];
|
|
|
|
$success = $this->migrateFile($base, $file, $filename);
|
|
|
|
if($success) {
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
2016-03-02 06:18:10 +01:00
|
|
|
Versioned::set_reading_mode($originalState);
|
2015-09-15 04:52:02 +02:00
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Migrate a single file
|
|
|
|
*
|
|
|
|
* @param string $base Absolute base path (parent of assets folder)
|
|
|
|
* @param File $file
|
2016-03-02 06:18:10 +01:00
|
|
|
* @param string $legacyFilename
|
2015-09-15 04:52:02 +02:00
|
|
|
* @return bool True if this file is imported successfully
|
|
|
|
*/
|
|
|
|
protected function migrateFile($base, File $file, $legacyFilename) {
|
|
|
|
// Make sure this legacy file actually exists
|
|
|
|
$path = $base . '/' . $legacyFilename;
|
|
|
|
if(!file_exists($path)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
// Copy local file into this filesystem
|
2016-03-02 06:18:10 +01:00
|
|
|
$filename = $file->generateFilename();
|
2015-12-09 22:19:23 +01:00
|
|
|
$result = $file->setFromLocalFile(
|
|
|
|
$path, $filename, null, null,
|
|
|
|
array('conflict' => AssetStore::CONFLICT_OVERWRITE)
|
|
|
|
);
|
2015-09-15 04:52:02 +02:00
|
|
|
|
|
|
|
// Move file if the APL changes filename value
|
|
|
|
if($result['Filename'] !== $filename) {
|
2016-03-02 06:18:10 +01:00
|
|
|
$file->setFilename($result['Filename']);
|
2015-09-15 04:52:02 +02:00
|
|
|
}
|
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
// Save and publish
|
2015-09-15 04:52:02 +02:00
|
|
|
$file->write();
|
2016-04-01 05:27:59 +02:00
|
|
|
$file->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE);
|
2015-09-15 04:52:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get list of File dataobjects to import
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
protected function getFileQuery() {
|
|
|
|
// Select all records which have a Filename value, but not FileFilename.
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2015-09-15 04:52:02 +02:00
|
|
|
return File::get()
|
2016-08-19 00:51:35 +02:00
|
|
|
->exclude('ClassName', ['SilverStripe\\Assets\\Folder', 'Folder'])
|
2015-09-15 04:52:02 +02:00
|
|
|
->filter('FileFilename', array('', null))
|
|
|
|
->where('"File"."Filename" IS NOT NULL AND "File"."Filename" != \'\''); // Non-orm field
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get map of File IDs to legacy filenames
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getFilenameArray() {
|
|
|
|
// Convert original query, ensuring the legacy "Filename" is included in the result
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @skipUpgrade */
|
2015-09-15 04:52:02 +02:00
|
|
|
return $this
|
|
|
|
->getFileQuery()
|
|
|
|
->dataQuery()
|
|
|
|
->selectFromTable('File', array('ID', 'Filename'))
|
|
|
|
->execute()
|
|
|
|
->map(); // map ID to Filename
|
|
|
|
}
|
|
|
|
}
|