-
-
Save katzueno/1e5c810387cccfce0a19e3f6cdc4b726 to your computer and use it in GitHub Desktop.
[concrete5][Legacy] Find duplicate file name and change it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ScanDuplicateFileName extends QueueableJob | |
{ | |
public $jNotUninstallable=0; | |
public $jSupportsQueue = true; | |
public function getJobName() | |
{ | |
return t('Scan Duplicate File Name'); | |
} | |
public function getJobDescription() | |
{ | |
return t('Find duplicate file name and change the name'); | |
} | |
public function start(Zend_Queue $q) | |
{ | |
Loader::model('file_list'); | |
$list = new FileList(); | |
$files = $list->get(); | |
/** @var File $file */ | |
foreach ($files as $file) { | |
$q->send($file->getFileID()); | |
} | |
} | |
public function finish(Zend_Queue $q) | |
{ | |
return t('Done.'); | |
} | |
public function processQueueItem(Zend_Queue_Message $msg) | |
{ | |
try { | |
/** @var File|FileVersion $f */ | |
$f = File::getByID($msg->body); | |
$filename = $f->getFileName(); | |
$files = $this->getDuplicatedFiles($f, $filename); | |
foreach ($files as $file) { | |
$this->changeFileName($file); | |
} | |
} catch (Exception $e) { | |
Log::addEntry($e->getMessage()); | |
} | |
} | |
public function getDuplicatedFiles(File $f, $filename) | |
{ | |
$list = new FileList(); | |
$list->filter('fv.fvFilename', $filename); | |
$list->filter('f.fID', $f->getFileID(), '<>'); | |
return $list->get(); | |
} | |
public function changeFileName(File $f, $count = 1) | |
{ | |
$fileName = $f->getFileName(); | |
$fileExt = $f->getExtension(); | |
$newFileName = substr($fileName, 0, strrpos($fileName, '.')) . '_' . $count . '.' . $fileExt; | |
$files = $this->getDuplicatedFiles($f, $newFileName); | |
if (count($files)) { | |
$this->changeFileName($f, ++$count); | |
} else { | |
$oldFilePath = $f->getPath(); | |
$f->updateFile($newFileName, $f->getPrefix()); | |
$f = File::getByID($f->getFileID()); | |
$newFilePath = $f->getPath(); | |
if (copy($oldFilePath, $newFilePath)) { | |
Log::addEntry(sprintf('copy %s to %s: id: %d', $oldFilePath, $newFilePath, $f->getFileID())); | |
} else { | |
$f->updateFile($fileName, $f->getPrefix()); | |
} | |
$f->refreshThumbnails(true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment