Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/gallery.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Paroz <github@oparoz.com>2015-04-04 01:59:01 +0300
committerOlivier Paroz <github@oparoz.com>2015-04-04 01:59:01 +0300
commitd7b8a33d5027e30098bbe5fbfb2b040121ff7a9b (patch)
treee2d0cd21afefc5bd657d39f7cbac2915e007205e /service/configservice.php
parent046783374ca01a30494ff5c3e1d906f716311e8f (diff)
Add inheritance support to the album configuration
Diffstat (limited to 'service/configservice.php')
-rw-r--r--service/configservice.php225
1 files changed, 154 insertions, 71 deletions
diff --git a/service/configservice.php b/service/configservice.php
index 34c167e5..fac4aa13 100644
--- a/service/configservice.php
+++ b/service/configservice.php
@@ -20,7 +20,8 @@ use OCP\Files\File;
/**
* Finds configurations files, parses them and returns a configuration array
*
- * Checks the current and parent folders for configuration files
+ * Checks the current and parent folders for configuration files and the privacy flag
+ * Supports explicit inheritance
*
* @package OCA\GalleryPlus\Service
*/
@@ -37,61 +38,77 @@ class ConfigService extends Service {
* @param Folder $folderNode
* @param string $folderPathFromRoot
*
- * @return array<string,string|int>
+ * @return array<null|array,bool>
*/
public function getAlbumInfo($folderNode, $folderPathFromRoot) {
$configName = 'gallery.cnf';
$privacyChecker = '.nomedia';
- $albumInfo = [];
+ $configItems = ['information' => false, 'sorting' => false];
+
list ($albumConfig, $privateAlbum) =
- $this->getAlbumConfig($folderNode, $privacyChecker, $configName);
+ $this->getAlbumConfig($folderNode, $privacyChecker, $configName, $configItems);
if (!$privateAlbum) {
- $albumInfo = [
- 'path' => $folderPathFromRoot,
- 'fileid' => $folderNode->getID(),
- 'permissions' => $folderNode->getPermissions()
- ];
- $albumInfo = array_merge($albumInfo, $albumConfig);
+ $albumConfig =
+ $this->addAlbumPermissions($albumConfig, $folderNode, $folderPathFromRoot);
}
- return [$albumInfo, $privateAlbum];
+ return [$albumConfig, $privateAlbum];
}
/**
* Returns an album configuration array
*
+ * Goes through all the parent folders until either we're told the album is private or we've
+ * gathered all the information we need
+ *
* @param Folder $folder
* @param string $privacyChecker
* @param string $configName
+ * @param array $configItems
* @param int $level
- * @param array $configArray
- * @param bool $configComplete
+ * @param array $config
*
- * @return array <null|string,string>
+ * @return array<null|array,bool>
*/
private function getAlbumConfig(
- $folder, $privacyChecker, $configName, $level = 0, $configArray = [],
- $configComplete = false
+ $folder, $privacyChecker, $configName, $configItems, $level = 0, $config = []
) {
if ($folder->nodeExists($privacyChecker)) {
// Cancel as soon as we find out that the folder is private
return [null, true];
}
- list($configArray, $configComplete) =
- $this->parseConfig($folder, $configName, $configArray, $configComplete, $level);
- $parentFolder = $folder->getParent();
- $path = $parentFolder->getPath();
- if ($path !== '' && $path !== '/') {
- $level++;
-
- return $this->getAlbumConfig(
- $parentFolder, $privacyChecker, $configName, $level, $configArray, $configComplete
+ if ($folder->nodeExists($configName)) {
+ list($config, $configItems) =
+ $this->parseFolderConfig($folder, $configName, $config, $configItems, $level);
+ }
+ if (!$this->isConfigComplete($configItems)) {
+ return $this->getParentConfig(
+ $folder, $privacyChecker, $configName, $configItems, $level, $config
);
}
- // We have reached the root folder
- return [$configArray, false];
+ // We have found a valid config or have reached the root folder
+ return [$config, false];
+ }
+
+ /**
+ * Adds the permission settings to the album config
+ *
+ * @param null|array<string,string|int> $albumConfig
+ * @param Folder $folderNode
+ * @param string $folderPathFromRoot
+ *
+ * @return null|array,bool
+ */
+ private function addAlbumPermissions($albumConfig, $folderNode, $folderPathFromRoot) {
+ $albumInfo = [
+ 'path' => $folderPathFromRoot,
+ 'fileid' => $folderNode->getID(),
+ 'permissions' => $folderNode->getPermissions()
+ ];
+
+ return array_merge($albumConfig, $albumInfo);
}
/**
@@ -99,35 +116,81 @@ class ConfigService extends Service {
*
* @param Folder $folder
* @param string $configName
- * @param array $currentConfigArray
- * @param bool $configComplete
+ * @param array $currentConfig
+ * @param array $configItems
* @param int $level
*
- * @return array<array,bool>
+ * @return array<null|array,array<string,bool>>
*/
- private function parseConfig(
- $folder, $configName, $currentConfigArray, $configComplete, $level
- ) {
- $configArray = $currentConfigArray;
- // Let's try to find the missing information in the configuration located in this folder
- if (!$configComplete && $folder->nodeExists($configName)) {
- /** @type File $configFile */
- $configFile = $folder->get($configName);
- try {
- $rawConfig = $configFile->getContent();
- $saneConfig = $this->bomFixer($rawConfig);
- $parsedConfigArray = Yaml::parse($saneConfig);
- list($configArray, $configComplete) =
- $this->validateConfig($currentConfigArray, $parsedConfigArray, $level);
- } catch (\Exception $exception) {
- $this->logger->error(
- "Problem while parsing the configuration file : {path}",
- ['path' => $folder->getPath() . '/' . $configFile->getPath()]
- );
+ private function parseFolderConfig($folder, $configName, $currentConfig, $configItems, $level) {
+ $config = $currentConfig;
+ /** @type File $configFile */
+ $configFile = $folder->get($configName);
+ try {
+ $rawConfig = $configFile->getContent();
+ $saneConfig = $this->bomFixer($rawConfig);
+ $parsedConfig = Yaml::parse($saneConfig);
+
+ list($config, $configItems) =
+ $this->buildAlbumConfig($currentConfig, $parsedConfig, $configItems, $level);
+ } catch (\Exception $exception) {
+ $this->logger->error(
+ "Problem while parsing the configuration file : {path}",
+ ['path' => $folder->getPath() . '/' . $configFile->getPath()]
+ );
+ }
+
+ return [$config, $configItems];
+ }
+
+ /**
+ * Decides whether we have all the elements we need or not
+ *
+ * @param $configItems
+ *
+ * @return bool
+ */
+ private function isConfigComplete($configItems) {
+ $configComplete = false;
+ $completedItems = 0;
+ foreach ($configItems as $key => $complete) {
+ if ($complete === true) {
+ $completedItems++;
}
}
+ if ($completedItems === sizeof($configItems)) {
+ $configComplete = true;
+ }
+
+ return $configComplete;
+ }
+
+ /**
+ * Looks for an album configuration in the parent folder
+ *
+ * @param Folder $folder
+ * @param string $privacyChecker
+ * @param string $configName
+ * @param array $configItems
+ * @param int $level
+ * @param array <null|string,string> $config
+ *
+ * @return array<null|array,array<string,bool>>
+ */
+ private function getParentConfig(
+ $folder, $privacyChecker, $configName, $configItems, $level, $config
+ ) {
+ $parentFolder = $folder->getParent();
+ $path = $parentFolder->getPath();
+ if ($path !== '' && $path !== '/') {
+ $level++;
+
+ return $this->getAlbumConfig(
+ $parentFolder, $privacyChecker, $configName, $configItems, $level, $config
+ );
+ }
- return [$configArray, $configComplete];
+ return [$config, false];
}
/**
@@ -151,33 +214,53 @@ class ConfigService extends Service {
/**
* Returns either the local config or one merged with a config containing sorting information
*
- * @param array $currentConfigArray
- * @param array $parsedConfigArray
+ * @param array $currentConfig
+ * @param array $parsedConfig
+ * @param array $configItems
* @param int $level
*
- * @return array<array,bool>
+ * @return array<null|array,array<string,bool>>
*/
- private function validateConfig($currentConfigArray, $parsedConfigArray, $level) {
- $configComplete = false;
- $sorting = $parsedConfigArray['sorting'];
- $sortOrder = $parsedConfigArray['sort_order'];
- $configArray = $parsedConfigArray;
- if ($sorting) {
- $configComplete = true;
- if ($level > 0) {
- // We only need the sorting information
- $currentConfigArray['sorting'] = $sorting;
- $currentConfigArray['sort_order'] = $sortOrder;
- $configArray = $currentConfigArray;
- }
- } else {
- if ($level > 0) {
- // Reset the array to what we had earlier since we didn't find any sorting information
- $configArray = $currentConfigArray;
+ private function buildAlbumConfig($currentConfig, $parsedConfig, $configItems, $level) {
+ foreach ($configItems as $key => $complete) {
+ if (!$complete && array_key_exists($key, $parsedConfig)) {
+ $parsedConfigItem = $parsedConfig[$key];
+ list($configItem, $itemComplete) =
+ $this->addConfigItem($key, $parsedConfigItem, $level);
+
+ $currentConfig = array_merge($currentConfig, $configItem);
+ $configItems[$key] = $itemComplete;
}
}
- return [$configArray, $configComplete];
+ return [$currentConfig, $configItems];
+ }
+
+ /**
+ * Adds a config sub-section to the global config
+ *
+ * @param string $key
+ * @param array $parsedConfigItem
+ * @param int $level
+ *
+ * @return array<null|array<string,string>,bool>
+ */
+ private function addConfigItem($key, $parsedConfigItem, $level) {
+ $configItem = [];
+ $itemComplete = false;
+ $inherit = false;
+
+ if (array_key_exists('inherit', $parsedConfigItem)) {
+ $inherit = $parsedConfigItem['inherit'];
+ }
+
+ if ($level === 0 || $inherit === 'yes') {
+ $parsedConfigItem['level'] = $level;
+ $configItem = [$key => $parsedConfigItem];
+ $itemComplete = true;
+ }
+
+ return [$configItem, $itemComplete];
}
}