Skip to content

Commit

Permalink
Merge branch '1.3' into 1
Browse files Browse the repository at this point in the history
  • Loading branch information
robbieaverill committed Oct 19, 2018
2 parents 9663248 + 75464a4 commit 6b599be
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,11 @@ public function onAfterRevertToLive()
{
// Force query of draft object and update (as source record is bound to live stage)
/** @var File $draftRecord */
$draftRecord = Versioned::get_by_stage(self::class, Versioned::DRAFT)->byID($this->ID);
$draftRecord->updateDependantObjects();
if (class_exists(Versioned::class) &&
$draftRecord = Versioned::get_by_stage(self::class, Versioned::DRAFT)->byID($this->ID)
) {
$draftRecord->updateDependantObjects();
}
}

/**
Expand All @@ -663,7 +666,7 @@ public function onAfterRevertToLive()
protected function updateDependantObjects()
{
// Skip live stage
if (Versioned::get_stage() === Versioned::LIVE) {
if (class_exists(Versioned::class) && Versioned::get_stage() === Versioned::LIVE) {
return;
}

Expand Down
31 changes: 31 additions & 0 deletions src/FileMigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\Versioned\Versioned;

Expand Down Expand Up @@ -102,6 +103,23 @@ protected function migrateFile($base, File $file, $legacyFilename)
return false;
}

// Fix file classname if it has a classname that's incompatible with it's extention
if (!$this->validateFileClassname($file, $extension)) {
// We disable validation (if it is enabled) so that we are able to write a corrected
// classname, once that is changed we re-enable it for subsequent writes
$validationEnabled = DataObject::Config()->get('validation_enabled');
if ($validationEnabled) {
DataObject::Config()->set('validation_enabled', false);
}
$destinationClass = $file->get_class_for_file_extension($extension);
$file = $file->newClassInstance($destinationClass);
$fileID = $file->write();
if ($validationEnabled) {
DataObject::Config()->set('validation_enabled', true);
}
$file = File::get_by_id($fileID);
}

// Copy local file into this filesystem
$filename = $file->generateFilename();
$result = $file->setFromLocalFile(
Expand Down Expand Up @@ -130,6 +148,19 @@ protected function migrateFile($base, File $file, $legacyFilename)
return true;
}

/**
* Check if a file's classname is compatible with it's extension
*
* @param File $file
* @param string $extension
* @return bool
*/
protected function validateFileClassname($file, $extension)
{
$destinationClass = $file->get_class_for_file_extension($extension);
return $file->ClassName === $destinationClass;
}

/**
* Get list of File dataobjects to import
*
Expand Down
10 changes: 8 additions & 2 deletions src/Shortcodes/FileLinkTracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ public function syncLinkTracking()
public function augmentSyncLinkTracking()
{
// If owner is versioned, skip tracking on live
if (Versioned::get_stage() == Versioned::LIVE && $this->owner->hasExtension(Versioned::class)) {
if (class_exists(Versioned::class) &&
Versioned::get_stage() == Versioned::LIVE &&
$this->owner->hasExtension(Versioned::class)
) {
return;
}

Expand Down Expand Up @@ -149,7 +152,10 @@ public function augmentSyncLinkTracking()
public function onAfterDelete()
{
// If owner is versioned, skip tracking on live
if (Versioned::get_stage() == Versioned::LIVE && $this->owner->hasExtension(Versioned::class)) {
if (class_exists(Versioned::class) &&
Versioned::get_stage() == Versioned::LIVE &&
$this->owner->hasExtension(Versioned::class)
) {
return;
}

Expand Down

0 comments on commit 6b599be

Please sign in to comment.