Source of file AllFilesInfo.php
Size: 9,782 Bytes - Last Modified: 2021-12-23T10:38:50+00:00
/var/www/docs.ssmods.com/process/src/src/Files/AllFilesInfo.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 | <?php namespace Sunnysideup\AssetsOverview\Files; use FilesystemIterator; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; use SilverStripe\Assets\File; use SilverStripe\Assets\Folder; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Injector\Injectable; use SilverStripe\ORM\DB; use Sunnysideup\AssetsOverview\Interfaces\FileInfo; use Sunnysideup\AssetsOverview\Traits\Cacher; use Sunnysideup\AssetsOverview\Traits\FilesystemRelatedTraits; use Sunnysideup\Flush\FlushNow; class AllFilesInfo implements FileInfo { use FilesystemRelatedTraits; use Injectable; use Configurable; use Cacher; use FlushNow; /** * @var string */ protected $path = ''; /** * @var array */ protected static $dataStaging = []; /** * @var array */ protected static $dataLive = []; /** * @var array */ protected static $listOfFiles = []; /** * @var array */ protected static $databaseLookupListStaging = []; /** * @var array */ protected static $availableExtensions = []; /** * @var array */ protected static $databaseLookupListLive = []; private static $not_real_file_substrings = [ DIRECTORY_SEPARATOR . '_resampled', DIRECTORY_SEPARATOR . '__', DIRECTORY_SEPARATOR . '.', // '__Fit', // '__Pad', // '__Fill', // '__Focus', // '__Scale', // '__ResizedImage', ]; public function __construct($path) { $this->path = $path; } public static function getTotalFilesCount(): int { return (int) count(self::$listOfFiles); } public static function getAvailableExtensions(): array { return self::$availableExtensions ?? []; } /** * does the file exists in the database on staging? */ public static function existsOnStaging(int $id): bool { return isset(self::$dataStaging[$id]); } /** * does the file exists in the database on live? */ public static function existsOnLive(int $id): bool { return isset(self::$dataLive[$id]); } /** * get data from staging database row. */ public static function getAnyData(string $pathFromAssets, ?int $id = 0): array { $data = self::getStagingData($pathFromAssets, $id); if (empty($data)) { $data = self::getLiveData($pathFromAssets, $id); } return $data; } /** * get data from staging database row. * * @param string $pathFromAssets from the root of assets * @param int $id */ public static function getStagingData(string $pathFromAssets, ?int $id = 0): array { if (! $id) { $id = self::$databaseLookupListStaging[$pathFromAssets] ?? 0; } return self::$dataStaging[$id] ?? []; } /** * get data from live database row. * * @param string $pathFromAssets - full lookup list */ public static function getLiveData(string $pathFromAssets, ?int $id = 0): array { if (! $id) { $id = self::$databaseLookupListLive[$pathFromAssets] ?? 0; } return self::$dataLive[$id] ?? []; } /** * find a value in a field in staging * returns ID of row. * * @param mixed $value */ public static function findInStagingData(string $fieldName, $value): int { return self::findInData(self::$dataStaging, $fieldName, $value); } /** * find a value in a field in live * returns ID of row. * * @param mixed $value */ public static function findInLiveData(string $fieldName, $value): int { return self::findInData(self::$dataLive, $fieldName, $value); } public static function getTotalFileSizesRaw() { $bytestotal = 0; $path = realpath(ASSETS_PATH); if (false !== $path && '' !== $path && file_exists($path)) { foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object) { $bytestotal += $object->getSize(); } } return $bytestotal; } public function toArray(): array { if (0 === count(self::$listOfFiles)) { $cachekey = $this->getCacheKey(); if (! $this->hasCacheKey($cachekey)) { $this->flushNow('<h1>Analysing files</h1>'); //disk $diskArray = $this->getArrayOfFilesOnDisk(); foreach ($diskArray as $path) { $this->registerFile($path, true); } //database $databaseArray = $this->getArrayOfFilesInDatabase(); foreach ($databaseArray as $path) { $this->registerFile($path, false); } asort(self::$listOfFiles); asort(self::$availableExtensions); $this->setCacheValue($cachekey, self::$listOfFiles); $this->setCacheValue($cachekey . 'availableExtensions', self::$availableExtensions); $this->setCacheValue($cachekey . 'dataStaging', self::$dataStaging); $this->setCacheValue($cachekey . 'dataLive', self::$dataLive); $this->setCacheValue($cachekey . 'databaseLookupStaging', self::$databaseLookupListStaging); $this->setCacheValue($cachekey . 'databaseLookupLive', self::$databaseLookupListLive); } else { self::$listOfFiles = $this->getCacheValue($cachekey); self::$availableExtensions = $this->getCacheValue($cachekey . 'availableExtensions'); self::$dataStaging = $this->getCacheValue($cachekey . 'dataStaging'); self::$dataLive = $this->getCacheValue($cachekey . 'dataLive'); self::$databaseLookupListStaging = $this->getCacheValue($cachekey . 'databaseLookupStaging'); self::$databaseLookupListLive = $this->getCacheValue($cachekey . 'databaseLookupLive'); } } return self::$listOfFiles; } protected function registerFile($path, $inFileSystem) { if ($path) { if (! isset(self::$listOfFiles[$path])) { self::$listOfFiles[$path] = $inFileSystem; if ($inFileSystem) { $this->flushNow('. ', '', false); } else { $this->flushNow('x ', '', false); } $extension = strtolower($this->getExtension($path)); self::$availableExtensions[$extension] = $extension; } } } protected static function findIdFromFileName(array $data, string $filename): int { $id = self::findInData($data, 'FileFilename', $filename); if (! $id) { $id = self::findInData($data, 'Filename', $filename); } return $id; } /** * @param mixed $value */ protected static function findInData(array $data, string $fieldName, $value): int { foreach ($data as $id => $row) { if (isset($row[$fieldName])) { if ($row[$fieldName] === $value) { return (int) $id; } } } return 0; } protected function isRealFile(string $path): bool { $listOfItemsToSearchFor = Config::inst()->get(self::class, 'not_real_file_substrings'); foreach ($listOfItemsToSearchFor as $test) { if (strpos($path, $test)) { return false; } } $fileName = basename($path); return ! ('error' === substr($fileName, 0, 5) && '.html' === substr($fileName, -5)); } protected function getArrayOfFilesOnDisk(): array { $finalArray = []; $arrayRaw = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($this->path), RecursiveIteratorIterator::SELF_FIRST ); foreach ($arrayRaw as $src) { $path = $src->getPathName(); if (false === $this->isRealFile($path)) { continue; } $finalArray[$path] = $path; } return $finalArray; } protected function getArrayOfFilesInDatabase(): array { $finalArray = []; foreach (['', '_Live'] as $stage) { $sql = 'SELECT * FROM "File' . $stage . '" WHERE "ClassName" <> \'' . addslashes(Folder::class) . "';"; $rows = DB::query($sql); foreach ($rows as $row) { $file = $row['FileFilename'] ?? $row['Filename']; if (trim($file)) { $absoluteLocation = $this->path . DIRECTORY_SEPARATOR . $file; if ('' === $stage) { self::$dataStaging[$row['ID']] = $row; self::$databaseLookupListStaging[$file] = $row['ID']; } elseif ('_Live' === $stage) { self::$dataLive[$row['ID']] = $row; self::$databaseLookupListLive[$file] = $row['ID']; } else { user_error('Can not find stage'); } $finalArray[$absoluteLocation] = $absoluteLocation; } } } return $finalArray; } //############################################# // CACHE //############################################# protected function getCacheKey(): string { return 'allfiles'; } } |