/** * Get file list (retrieve and sort) * * @param array $params * * @return array */ public function filelist($params = array()) { // Accept params $filter = isset($params['filter']) ? $params['filter'] : NULL; $dirPath = isset($params['subdir']) ? $params['subdir'] : NULL; $showUntracked = isset($params['showUntracked']) ? $params['showUntracked'] : false; $remotes = isset($params['remoteConnections']) ? $params['remoteConnections'] : array(); $sortdir = isset($params['sortdir']) && $params['sortdir'] == 'DESC' ? SORT_DESC : SORT_ASC; $sortby = isset($params['sortby']) ? $params['sortby'] : 'name'; $getParents = isset($params['getParents']) ? $params['getParents'] : false; $getChildren = isset($params['getChildren']) ? $params['getChildren'] : false; $files = isset($params['files']) && is_array($params['files']) ? $params['files'] : array(); // Get a list of files Git repo $files = empty($files) ? $this->_git->getFiles($dirPath, false) : $files; // Add untracked? $untracked = $showUntracked ? $this->_git->getFiles($dirPath, true) : array(); if (!empty($untracked)) { $files = array_merge($files, $untracked); } // Include remote connections? if (!empty($remotes)) { foreach ($remotes as $name => $item) { $files[] = $name; } } $remote = $this->get('remote'); // Output containers $items = array(); $sorting = array(); $folders = array(); if ($dirPath) { $folders[] = $dirPath; } // Go through items and get what we need foreach ($files as $item) { $item = urldecode($item); if (trim($item) == '') { continue; } // Load basic file metadata $file = new Models\File($item, $this->_path); // Search filter applied if ($filter && strpos(trim($file->get('localPath')), trim($filter)) === false && strpos(trim($file->get('localPath')), trim($filter)) === false) { continue; } elseif ($filter) { $getParents = false; } // Untracked? if (in_array($file->get('localPath'), $untracked)) { $file->set('untracked', true); } // Do we have a parent? Get folder information if ($file->get('dirname') && !in_array($file->get('dirname'), $folders)) { // Folder metadata $file->setFolder(); // Add to list if (empty($items[$file->get('name')])) { // Recursive check if ($getChildren || !$getChildren && $file->getDirLevel($dirPath) > $file->getDirLevel($file->get('dirname'))) { $folders[] = $file->get('localPath'); if (!$getParents) { continue; } $items[$file->get('name')] = $file; // Collect info for sorting switch ($sortby) { case 'size': $sorting[] = $file->getSize(); break; case 'modified': // Need to get extra metadata (slower) $sorting[] = ''; break; case 'localpath': $sorting[] = strtolower($file->get('localPath')); break; case 'name': default: $sorting[] = strtolower($file->get('name')); break; } } } } // Getting parent information? if ($getParents) { $file->setParents(); } // Do not recurse if (!$getChildren && $file->get('dirname')) { if (!$dirPath || $dirPath != $file->get('dirname')) { continue; } } // Skip this if ($file->get('name') == '.gitignore') { continue; } // Check for remote connections $syncRecord = NULL; if (isset($remotes[$file->get('localPath')])) { // Pick up data from sync record $syncRecord = $remotes[$file->get('localPath')]; $file->set('remote', $syncRecord->service); $file->set('author', $syncRecord->remote_author); $file->set('date', date('c', strtotime($syncRecord->remote_modified . ' UTC'))); $file->set('mimeType', $syncRecord->remote_format); } // Add to list if (empty($items[$file->get('name')])) { $items[$file->get('name')] = $file; // Collect info for sorting switch ($sortby) { case 'size': $sorting[] = $file->getSize(); break; case 'modified': // Need to get extra metadata (slower) $sorting[] = $this->_file($file, $dirPath, 'date'); break; case 'localpath': $sorting[] = strtolower($file->get('localPath')); break; case 'name': default: $sorting[] = strtolower($file->get('name')); break; } } } // Sort array_multisort($sorting, $sortdir, $items); // Apply start and limit, get complete metadata and return return $this->_list($items, $params); }
/** * Get file list (retrieve and sort) * * @param array $params * @return array */ public function filelist($params = []) { // Parse incoming params and establish defaults $filter = isset($params['filter']) ? $params['filter'] : NULL; $dirPath = isset($params['subdir']) ? $params['subdir'] : NULL; $showUntracked = isset($params['showUntracked']) ? $params['showUntracked'] : false; $remotes = isset($params['remoteConnections']) ? $params['remoteConnections'] : []; $sortdir = isset($params['sortdir']) && $params['sortdir'] == 'DESC' ? SORT_DESC : SORT_ASC; $sortby = isset($params['sortby']) ? $params['sortby'] : 'name'; $files = isset($params['files']) && is_array($params['files']) ? $params['files'] : []; $dirsOnly = isset($params['dirsOnly']) ? $params['dirsOnly'] : false; if (!$dirsOnly) { // Get a list of files from the git repository $files = empty($files) ? $this->_git->getFiles($dirPath) : $files; // Add untracked? $untracked = $showUntracked ? $this->_git->getUntrackedFiles($dirPath) : []; if (!empty($untracked)) { $files = array_merge($files, $untracked); } // Include remote connections? if (!empty($remotes)) { foreach ($remotes as $name => $item) { $files[] = $name; } } } else { // This is recursive by default $files = empty($files) ? $this->_git->getDirectories($dirPath) : $files; // Add untracked? $untracked = $showUntracked ? $this->_git->getUntrackedDirectories($dirPath) : []; if (!empty($untracked)) { $files = array_merge($files, $untracked); } } // Duplicates can be introduced by the remotes $files = array_unique($files); // Output containers $items = []; $sorting = []; // Apply the filter early, reduces iterations through foreach() if (isset($filter) && $filter != '') { $files = preg_grep("(" . $filter . ")", $files); } // Go through items and get what we need foreach ($files as $item) { $item = urldecode($item); if (trim($item) == '') { continue; } // Load basic file metadata $file = new Models\File($item, $this->_path); // Untracked? if (in_array($file->get('localPath'), $untracked)) { $file->set('untracked', true); } // Skip this if ($file->get('name') == '.gitignore') { continue; } // Check for remote connections $syncRecord = NULL; if (isset($remotes[$file->get('localPath')])) { // Pick up data from sync record $syncRecord = $remotes[$file->get('localPath')]; $file->set('remote', $syncRecord->service); $file->set('author', $syncRecord->remote_author); $file->set('date', date('c', strtotime($syncRecord->remote_modified . ' UTC'))); $file->set('mimeType', $syncRecord->remote_format); $file->set('converted', $syncRecord->remote_editing); } // Add to list if (empty($items[$file->get('name')])) { $items[$file->get('localPath')] = $file; // Collect info for sorting switch ($sortby) { case 'size': $sorting[] = $file->getSize(); break; case 'modified': // Need to get extra metadata (slower) $sorting[] = $this->_file($file, $dirPath, 'date'); break; case 'localpath': $sorting[] = strtolower($file->get('localPath')); break; case 'name': default: $sorting[] = strtolower($file->get('name')); break; } } } // Sort array_multisort($sorting, $sortdir, $items); // Apply start and limit, get complete metadata and return return $this->_list($items, $params); }
/** * Build file metadata object * * @return object */ public function getMetadata($item = NULL, $type = 'file', $params = array()) { if ($item === NULL) { return false; } $path = isset($params['path']) ? $params['path'] : $this->get('path'); $dirPath = isset($params['subdir']) ? $params['subdir'] : NULL; $remotes = isset($params['remoteConnections']) ? $params['remoteConnections'] : array(); $localPath = $dirPath ? $dirPath . DS . $item : $item; $file = new Models\File($localPath, $path); $file->set('type', $type); if ($type == 'folder') { $file->clear('ext'); } // Synced file? if (!empty($remotes) && isset($remotes[$file->get('localPath')])) { // Pick up data from sync record $syncRecord = $remotes[$file->get('localPath')]; $file->set('remote', $syncRecord->service); $file->set('remoteId', $syncRecord->remote_id); $file->set('remoteTitle', $syncRecord->remote_title); $file->set('remoteParent', $syncRecord->remote_parent); $file->set('author', $syncRecord->remote_author); $file->set('modified', $syncRecord->remote_modified); $file->set('mimeType', $syncRecord->remote_format); $file->set('converted', $syncRecord->remote_editing); $file->set('originalId', $syncRecord->original_id); $file->set('originalPath', $syncRecord->original_path); $file->set('originalFormat', $syncRecord->original_format); $file->set('recordId', $syncRecord->id); } return $file; }