* @license PHP License * @package WB * @subpackage base */ WBClass::load( 'WBCli' ); /** * Command Line Interface class: VFS * * Maintain files * * @version 0.2.1 * @package WB * @subpackage base */ class WBCli_VFS extends WBCli { /** * Command line arguments * @var array */ protected $arguments = array( 'des' => array( 'short' => 'd', 'min' => 1, 'max' => 1, 'default' => '', 'desc' => 'Destination folder', ), 'older' => array( // 'short' => '', 'min' => 1, 'max' => 1, 'default' => '', 'desc' => 'Date, when files where deleted. Something like "1year" or "6month"', ), CONSOLE_GETARGS_PARAMS => array( 'min' => 1, 'max' => -1, 'default' => '', 'desc' => 'action and additional arguments', ) ); /** * VFS File * @var WBVFS_File */ private $file; /** * VFS File Explorer * @var WBVFS_Explorer */ private $expl; /** * 2nd constructor * * @see include/WB/WBCli#init() */ protected function init() { } /** * Execute Programme * * Use observer and execute event queue */ protected function execute() { if (empty($this->commands)) { $this->pl('No command given.'); return 1; } $cmd = array_shift($this->commands); if (!method_exists($this, 'execute' . $cmd)) { $this->pl('Command ' . $cmd . ' not known.'); return 1; } return call_user_func(array($this, 'execute' . $cmd)); } /** * Execute Delete Programme * * Delete old files in folder. This is usefull to clean up temporary folders */ private function executeDelete() { $older = $this->args->getValue('older'); if (empty($older)) { $this->pl('Cannot delete files. Older parameter is required.'); return 1; } $des = $this->args->getValue('des'); if (empty($des)) { $this->pl('Cannot delete files. Destination parameter is required.'); return 1; } $this->expl = WBClass::create('WBVFS_Explorer'); $this->expl->switchTranslation(false); try { $base = $this->expl->getDir($des); } catch (WBException $e) { $this->pl('Invalid destination ' . $des . ' to delete old files!', STDERR); return 1; } $this->p('Delete '); $this->pvl('old files'); $this->pvl(' Destination: ' . $des); $older = '-' . $older; $older = strtotime($older); $this->pvl(sprintf(' Older: %s', date('Y-m-d', $older))); $this->startVFS(); $count = $this->deleteOldFile4Dir($des, $older); $this->pvl(sprintf(' Deleted: %d files', $count)); return 0; } /** * Mark Files as Deleted * * Recursive function to delete files * * @param string dir id * @param int timestamp */ private function deleteOldFile4Dir($dirId, $older) { $files = $this->file->listDir($dirId); $count = 0; foreach ($files as $f) { $this->file = $this->file->loadByData($f); if (!$this->file->isOK()) { continue; } $created = $this->file->getCreated(); $created = strtotime($created); if ($older > $created) { ++$count; $this->file->delete(); } } $dirs = $this->expl->lsDir($dirId); foreach ($dirs as $d) { $count += $this->deleteOldFile4Dir($d['id'], $older); } return $count; } /** * Execute Flush Programme * * Remove deleted files */ private function executeFlush() { $older = $this->args->getValue('older'); if (empty($older)) { $this->pl('Cannot flush files. Older parameter is required.'); return 1; } $this->p('Flush '); $this->pvl('deleted files'); $older = '-' . $older; $this->pvl(sprintf(' Older: %s', date('Y-m-d', strtotime($older)))); $this->startVFS(); $count = $this->file->flushDeleted($older); $this->pv(' Count: '); $this->pl($count); return 0; } /** * Execute Import Programme * * Import files to destination folder */ private function executeImport() { $des = $this->args->getValue('des'); $this->pvl('Destination: ' . $des); $this->startVFS(); $this->file->setCurrentDir($des, true); foreach ($this->commands as $file) { $name = basename($file); $this->file = $this->file->import($file, $name, true); $this->pl($this->file->getObscureId() . ' ' . $name); } return 0; } /** * Start File Instance * * Auxilliary method to init object */ private function startVFS() { $this->file = WBClass::create('WBVFS_File'); $this->file->switchTranslation(false); } /** * Get Help Text - Head * * Show available commands * * @return string */ protected function getHelpHead() { $head = array(); $head[] = 'Newsletter Help'; $head[] = ''; $head[] = 'Usage: ' . basename($_SERVER['_']) . ' [OPTION]... COMMAND [ARGS...] '; $head[] = ''; $head[] = 'Commands: '; $head[] = ' - delete: deleted in folder that are files older than ...'; $head[] = ' - flush: remove deleted files older than ...'; $head[] = ' - import: Import files in directory'; $head[] = 'Options:'; $head[] = ''; return implode("\n", $head); } }