Console_Application
[ class tree: Console_Application ] [ index: Console_Application ] [ all elements ]

Source for file ScannerApplication.php

Documentation is available at ScannerApplication.php

  1. <?php
  2. /**
  3.  * MediaSearch
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author$
  14.  * $Revision$
  15.  *
  16.  * @category    MediaSearch
  17.  * @package     Console_Application
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * AbstractDbApplication
  23.  *
  24.  * @see AbstractDbApplication
  25.  */
  26. require_once('AbstractDbApplication.php');
  27. /**
  28.  * MediaSearch_File_TreeFactory
  29.  *
  30.  * @see MediaSearch_File_TreeFactory
  31.  */
  32. require_once('MediaSearch/File/TreeFactory.php');
  33. /**
  34.  * MediaSearch_File_Visitor
  35.  *
  36.  * @see MediaSearch_File_Visitor
  37.  */
  38. require_once('MediaSearch/File/Visitor.php');
  39. /**
  40.  * MediaSearch_Filter_KeyWords_PathRemover
  41.  *
  42.  * @see MediaSearch_Filter_KeyWords_PathRemover
  43.  */
  44. require_once('MediaSearch/Filter/KeyWords/PathRemover.php');
  45.  
  46. /**
  47.  * Neequired to react on signals
  48.  * @link http://de.php.net/manual/en/function.pcntl-signal.php
  49.  */
  50. declare(ticks 1);
  51.  
  52. /**
  53.  * Scanns the given directory recursivle and gathers file meta informations.
  54.  *
  55.  * @see AbstractDbApplication
  56.  *
  57.  * @abstract
  58.  * @category   MediaSearch
  59.  * @package    Console_Application
  60.  * @uses       MediaSearch_File_TreeFactory
  61.  * @uses       MediaSearch_File_Visitor
  62.  * @uses       MediaSearch_Model_FileMeta
  63.  * @uses       MediaSearch_Log
  64.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  65.  */
  66.     const TRACE_LELVEL = 7;
  67.  
  68.     /**
  69.      * The directory which will be scanned.
  70.      *
  71.      * @access private
  72.      * @var string 
  73.      */
  74.     private $scanningDir;
  75.     /**
  76.      * Flags identify if the table will be truncated before save.
  77.      *
  78.      * @access private
  79.      * @var bool 
  80.      */
  81.     private $clearFileMetaTable false;
  82.     /**
  83.      * Counts how much files are scanned.
  84.      *
  85.      * @access private
  86.      * @var int 
  87.      */
  88.     private $scannedFilesCnt 0;
  89.  
  90.     /**
  91.      * Generates a file tree and fisits it after that to
  92.      * gather meta information.
  93.      *
  94.      * @see Console_Application_Abstract::run()
  95.      * 
  96.      * @access protected
  97.      */
  98.     protected function run({
  99.         $fileTree    $this->generateTree();
  100.         $scannedList $this->visitTree($fileTree);
  101.         $this->clearFileMetaTable();
  102.         $this->saveData($scannedList);
  103.     }
  104.  
  105.     /**
  106.      * @see AbstractDbApplication::getUsage()
  107.      */
  108.     protected function tearDown({
  109.         parent::tearDown();
  110.         $this->echoMsg($this->scannedFilesCnt ' files scanned.');
  111.         $this->echoMsg('Meta data saved in database.');
  112.     }
  113.  
  114.     /**
  115.      * Listen to option -d with a following string which is the scanned
  116.      * directory. Also listen to -c to clear the file meta table beforre
  117.      * saving.
  118.      *
  119.      * @see Console_Application_Abstract::findConsoleParameters()
  120.      *
  121.      * @access protected
  122.      * @throws Console_Exception_MissingParameter
  123.      * @return bool 
  124.      */
  125.     protected function findConsoleParameters({
  126.         if (!parent::findConsoleParameters()) {
  127.             return false;
  128.         }
  129.  
  130.         $options getopt('d:c');
  131.  
  132.         if (!isset($options['d']|| empty($options['d'])) {
  133.             require_once('Console/Exception/MissingParameter.php');
  134.             $message  'You ommited the parameter -d for the scanning directory!';
  135.             throw new Console_Exception_MissingParameter($message);
  136.         }
  137.  
  138.         $this->scanningDir $options['d'];
  139.  
  140.         if (!is_readable($this->scanningDir)) {
  141.             require_once('Console/Exception/MissingParameter.php');
  142.             $message  'The given directory ' $this->scanningDir;
  143.             $message .= 'is not readable!';
  144.             throw new Console_Exception_MissingParameter($message);
  145.         }
  146.  
  147.         if (!is_dir($this->scanningDir)) {
  148.             require_once('Console/Exception/MissingParameter.php');
  149.             $message  'The given directory ' $this->scanningDir;
  150.             $message .= 'is not a directory!';
  151.             throw new Console_Exception_MissingParameter($message);
  152.         }
  153.  
  154.         MediaSearch_Filter_KeyWords_PathRemover::$basePath $this->scanningDir;
  155.  
  156.         if (isset($options['c'])) {
  157.             $this->clearFileMetaTable true;
  158.         }
  159.  
  160.         return true;
  161.     }
  162.  
  163.     /**
  164.      * Checks if the clear file meta table flag is set true and
  165.      * executes a truncate table.
  166.      *
  167.      * @access private
  168.      */
  169.     private function clearFileMetaTable({
  170.         if ($this->clearFileMetaTable{
  171.             MediaSearch_Log::info('Clearing table FileMeta...');
  172.             $dbAdapter $this->dbResource->getDbAdapter();
  173.             $dbAdapter->getConnection()->exec('TRUNCATE TABLE FileMeta');
  174.         }
  175.     }
  176.  
  177.     /**
  178.      * Utilizes MediaSearch_File_TreeFactory to creates a file tree.
  179.      *
  180.      * @access private
  181.      * @return MediaSearch_File_Folder 
  182.      */
  183.     private function generateTree({
  184.         MediaSearch_Log::info("Reading file tree of {$this->scanningDir} ...");
  185.         $treeFactory = new MediaSearch_File_TreeFactory($this->scanningDir);
  186.     $fileTree $treeFactory->construct();
  187.         MediaSearch_Log::info('Tree generated.');
  188.  
  189.         return $fileTree;
  190.     }
  191.  
  192.     /**
  193.      * Visits the file tree with MediaSearch_File_Visitor to perform
  194.      * data gathering on the files.
  195.      *
  196.      * @access private
  197.      * @param MediaSearch_File_Folder $fileTree
  198.      * @return MediaSearch_Scan_List
  199.      */
  200.     private function visitTree(MediaSearch_File_Folder $fileTree) {
  201.         MediaSearch_Log::info('Start scanning ...');
  202.     $visitor = new MediaSearch_File_Visitor();
  203.     $visitor->visit($fileTree);
  204.         MediaSearch_Log::info('Scan finished.');
  205.  
  206.     return $visitor->getFileList();
  207.     }
  208.  
  209.     /**
  210.      * Saves the gathered file meta data to the database.
  211.      *
  212.      * @access private
  213.      * @param MediaSearch_Scan_List $scannedList
  214.      */
  215.     private function saveData(MediaSearch_Scan_List $scannedList) {
  216.         MediaSearch_Log::info('Saving scanned data...');
  217.         $count = 0;
  218.  
  219.         foreach ($scannedList as $fileMeta) {
  220.             echo '.';
  221.             $fileMetaModel = new MediaSearch_Model_FileMeta();
  222.             $fileMetaModel->setOptions($fileMeta->toArray());
  223.             $fileMetaModel->setLastUpdate(time());
  224.             $fileMetaModel->save();
  225.             $count++;
  226.         }
  227.         $this->echoMsg();
  228.         MediaSearch_Log::info('');
  229.         MediaSearch_Log::info("Data saved ($count).");
  230.         $this->scannedFilesCnt $count;
  231.     }
  232.  
  233.     /**
  234.      * @see Console_Application_Abstract::getUsage()
  235.      */
  236.     public function getUsage($options = null) {
  237.        return parent::getUsage('-d DIRECTORY -c');
  238.     }
  239.  
  240.     /**
  241.      * @see Console_Application_Abstract::getHelpMessage()
  242.      */
  243.     public function getHelpMessage($author = 'Sven Strittmatter', $email = 'ich@weltraumschaf.de') {
  244.         $message  = 'This script scanns a given directory for the containing ';
  245.         $message .= 'media files.' . PHP_EOL;
  246.         $message .= PHP_EOL;
  247.     $message .= $this->getUsage(':' PHP_EOL;
  248.         $message .= "\t{$this->scriptname} -d : The scanned directory" . PHP_EOL;
  249.         $message .= "\t{$this->scriptname} -c : The index table is cleared. ';
  250.         $message .= 'Before indexing starts." . PHP_EOL;
  251.         $message .= PHP_EOL;
  252.         $message .= parent::getHelpMessage($author, $email);
  253.  
  254.         throw new Console_Exception($message);
  255.     }

Documentation generated on Mon, 17 Aug 2009 14:54:01 +0200 by phpDocumentor 1.4.2