Created
July 26, 2009 06:42
-
-
Save pcdinh/155451 to your computer and use it in GitHub Desktop.
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 to control the indexing operation. | |
* | |
* @author pcdinh | |
* @since July 25, 2009 | |
* @version $Id$ | |
*/ | |
class IndexerController | |
{ | |
/** | |
* Indexer database service. | |
* | |
* @var IndexerDatabaseService | |
*/ | |
public $dbs; | |
/** | |
* Indexer file service. | |
* | |
* @var IndexerFileService | |
*/ | |
public $fs; | |
/** | |
* File format detector service. | |
* | |
* @var FileNameFormatDetector | |
*/ | |
public $parser; | |
/** | |
* Office name. | |
* | |
* @var string | |
*/ | |
public $office; | |
/** | |
* Working directory. | |
* | |
* @var string | |
*/ | |
public $dir; | |
/** | |
* Constructs an object of <code>IndexerController</code>. | |
* | |
* @param IndexerDatabaseService $dbs | |
* @param FileNameParser $parser | |
* @param IndexerFileService $fs | |
*/ | |
public function __construct($dbs, $parser, $fs) | |
{ | |
$this->fs = $fs; | |
$this->dbs = $dbs; | |
$this->parser = $parser; | |
} | |
/** | |
* Sets office name which current files are uploaded from. | |
* | |
* @param string $name | |
*/ | |
public function setOfficeName($name) | |
{ | |
$this->office = $name; | |
} | |
/** | |
* Processes files in a path. | |
* | |
* @param string $path | |
*/ | |
public function processDir($path) | |
{ | |
$this->dir = $path; | |
$this->fs->setWorkingDirectory($path); | |
$itor = new DirectoryIterator($path); | |
// DEBUG | |
echo 'Working on path: '.$path."\n"; | |
$this->processFiles($itor); | |
} | |
/** | |
* Processes file in a directory. | |
* | |
* @param DirectoryIterator $itor File iterator | |
*/ | |
public function processFiles($itor) | |
{ | |
foreach ($itor as $file) | |
{ | |
// Ignores . directory | |
if (true === $file->isDot()) | |
{ | |
continue; | |
} | |
// DEBUG | |
echo 'Working on '.$file->getFileName()."\n"; | |
$data = array(); | |
$data['name'] = $file->getFilename(); | |
$data['full_path'] = $this->dir.'/'.$file->getFilename(); | |
$data['size'] = $file->getSize(); | |
$data['office'] = $this->office; | |
$this->_processData($data); | |
} | |
} | |
/** | |
* Processes data on a file. | |
* | |
* @param array $data Information on a file | |
*/ | |
protected function _processData($data) | |
{ | |
try | |
{ | |
$part = $this->parser->parse($data['name']); | |
if (FileNameFormat::NEW_FORMAT_2 !== $part->format) | |
{ | |
throw new FileNameParsingException('Invalid format <'.$data['name'].'>'); | |
} | |
$ids = $this->dbs->getIds($data['name']); | |
$this->fs->moveToFileServer($data['full_path']); | |
if (false !== empty($ids)) | |
{ | |
$data['id'] = $ids; | |
$this->dbs->updateIndex($data); | |
} | |
else | |
{ | |
$this->dbs->createIndex($data); | |
} | |
$this->fs->moveToBackupDir($data['full_path']); | |
} | |
catch (FileNameParsingException $e) | |
{ | |
$this->fs->moveToInvalidDirectory($data['full_path']); | |
error_log($e->getMessage()); | |
} | |
catch (Exception $e) | |
{ | |
error_log($e->getMessage()); | |
} | |
} | |
} | |
?> |
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 | |
/** | |
* Indexes files that were uploaded and move them to file server | |
* and update path to the database | |
* | |
* @author pcdinh | |
* @since July 24, 2009 | |
* @version $Id: main.php 436 2009-07-25 19:43:48Z pcdinh $ | |
*/ | |
ini_set('error_log', dirname(__FILE__).'/error/log_'.date('Ymd').'.log'); | |
include_once './config.php'; | |
include_once './FileNameParser.php'; | |
include_once './FileNameFormat.php'; | |
include_once './IndexerDatabaseService.php'; | |
include_once './IndexerFileService.php'; | |
include_once './IndexerController.php'; | |
// List of base directories | |
$dirs = $config['ftpdirs']; | |
$db = new IndexerDatabaseService($config['dbdoc']); | |
$parser = new FileNameParser(); | |
$fileService = new IndexerFileService(); | |
$controller = new IndexerController($db, $parser, $fileService); | |
foreach ($dirs as $name => $baseDir) | |
{ | |
$controller->setOfficeName($name); | |
// Directory for public companies' report files | |
$path = $baseDir.'/public'; | |
$fileService->setGroup('public'); | |
try | |
{ | |
$controller->processDir($path); | |
} | |
catch (Exception $e) | |
{ | |
error_log($e->getMessage()); | |
} | |
// Directory for private companies' report files | |
$path = $baseDir.'/private'; | |
$fileService->setGroup('private'); | |
try | |
{ | |
$controller->processDir($path); | |
} | |
catch (Exception $e) | |
{ | |
error_log($e->getMessage()); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment