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

Source for file IndexerApplication.php

Documentation is available at IndexerApplication.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_Index_KeyWords
  29.  *
  30.  * @see MediaSearch_Index_KeyWords
  31.  */
  32. require_once('MediaSearch/Index/KeyWords.php');
  33. /**
  34.  * MediaSearch_Log
  35.  *
  36.  * @see MediaSearch_Log
  37.  */
  38. require_once('MediaSearch/Log.php');
  39.  
  40. /**
  41.  * Neequired to react on signals
  42.  * @link http://de.php.net/manual/en/function.pcntl-signal.php
  43.  */
  44. declare(ticks 1);
  45.  
  46. /**
  47.  * This application generates the keyword index by the information
  48.  * found in the FileMeta table.
  49.  *
  50.  * @see AbstractDbApplication
  51.  *
  52.  * @abstract
  53.  * @category   MediaSearch
  54.  * @package    Console_Application
  55.  * @uses       MediaSearch_Model_FileMeta
  56.  * @uses       MediaSearch_Index_KeyWords
  57.  * @uses       MediaSearch_Log
  58.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  59.  */
  60.     const TRACE_LELVEL = 6;
  61.  
  62.     /**
  63.      * Flag identifies if the existing index should
  64.      * be dropped.
  65.      *
  66.      * @access private
  67.      * @var bool 
  68.      */
  69.     private $clearIndexTable false;
  70.  
  71.     /**
  72.      * Checks for the option -c. If set the existing
  73.      * index will be deleted: @see IndexerApplication::$clearIndexTable
  74.      *
  75.      * @see Console_Application_Abstract::findConsoleParameters()
  76.      *
  77.      * @access protected
  78.      * @return bool 
  79.      */
  80.     protected function findConsoleParameters({
  81.         if (!parent::findConsoleParameters()) {
  82.             return false;
  83.         }
  84.  
  85.         $options getopt('c');
  86.  
  87.         if (isset($options['c'])) {
  88.             $this->clearIndexTable true;
  89.         }
  90.  
  91.         return true;
  92.     }
  93.  
  94.     /**
  95.      * Reads the FileMeta table information in chunks by 100 data rows
  96.      * and processes them. After that the generated index is saved to
  97.      * the KeyWord table.
  98.      *
  99.      * @access protected
  100.      */
  101.     protected function run({
  102.         $model         new MediaSearch_Model_FileMeta();
  103.         $chunkSize     100;
  104.         $fileMetaCount $model->count();
  105.         $chunkCount    ceil($fileMetaCount $chunkSize);
  106.         $keyWordIndex  new MediaSearch_Index_KeyWords();
  107.         $this->echoMsg('Indexing ' $fileMetaCount ' file...');
  108.         
  109.         for ($i 0$i $chunkCount$i++{
  110.             MediaSearch_Log::info('Selecting chunk ' ($i 1));
  111.             $this->echoMsg('Selecting chunk ' ($i 1));
  112.             $offset $i $chunkSize;
  113.             $chunk  $model->fetchAll($chunkSize$offset);
  114.             $this->processChunk($chunk$keyWordIndex);
  115.         }
  116.  
  117.         $this->clearIndexTable();
  118.         $this->saveKeyWordIndex($keyWordIndex);
  119.     }
  120.  
  121.     /**
  122.      * Adds the file meta model of a chunk to the key word index.
  123.      *
  124.      * @access private
  125.      * @param array $chunk 
  126.      * @param MediaSearch_Index_KeyWords $keyWordIndex 
  127.      */
  128.     private function processChunk(array $chunkMediaSearch_Index_KeyWords $keyWordIndex{
  129.         MediaSearch_Log::info('Processing chunk...');
  130.         $this->echoMsg('Processing chunk...');
  131.  
  132.         foreach ($chunk as $fileMetaModel{
  133.             MediaSearch_Log::trace('Indexing file with id ' $fileMetaModel->getId());
  134.             $this->echoMsg('Indexing file with id ' $fileMetaModel->getId());
  135.             $keyWordIndex->addFileCriteria($fileMetaModel);
  136.         }
  137.     }
  138.  
  139.     /**
  140.      * Converts the given key word index to key word model objects and saves them.
  141.      *
  142.      * @access private
  143.      * @param MediaSearch_Index_KeyWords $keyWordIndex 
  144.      */
  145.     private function saveKeyWordIndex(MediaSearch_Index_KeyWords $keyWordIndex{
  146.         MediaSearch_Log::info('Saving keyword index...');
  147.         $this->echoMsg('Saving keyword index...');
  148.         $models $keyWordIndex->convertToModels();
  149.         $count  0;
  150.  
  151.         foreach ($models as $model{
  152.             echo '.';
  153.             MediaSearch_Log::trace($model->getKeyWord());
  154.             $model->save();
  155.             $count++;
  156.         }
  157.  
  158.         MediaSearch_Log::info('');
  159.         MediaSearch_Log::info("Data saved ($count).");
  160.         $this->echoMsg();
  161.         $this->echoMsg("Data saved ($count).");
  162.     }
  163.  
  164.     /**
  165.      * Checks if the clear index table flag is set true and
  166.      * executes a truncate table.
  167.      *
  168.      * @access private
  169.      */
  170.     private function clearIndexTable({
  171.         if ($this->clearIndexTable{
  172.             MediaSearch_Log::info('Clearing table KeyWord...');
  173.             $this->echoMsg('Clearing table KeyWord...');
  174.             $dbAdapter $this->dbResource->getDbAdapter();
  175.             $dbAdapter->getConnection()->exec('TRUNCATE TABLE KeyWord');
  176.         }
  177.     }
  178.  
  179.     /**
  180.      * @see Console_Application_Abstract::getUsage()
  181.      */
  182.     public function getUsage($options null{
  183.        return parent::getUsage('-c');
  184.     }
  185.  
  186.     /**
  187.      * @see Console_Application_Abstract::getHelpMessage()
  188.      */
  189.     public function getHelpMessage($author 'Sven Strittmatter'$email 'ich@weltraumschaf.de'{
  190.         $message  'This script generates the keyword index frm the data in ';
  191.         $message .= 'the FileMeta table.' PHP_EOL;
  192.         $message .= PHP_EOL;
  193.     $message .= $this->getUsage(':' PHP_EOL;
  194.         $message .= "\t{$this->scriptname} -c : The index table is cleared. ";
  195.         $message .= 'Before indexing starts.' PHP_EOL;
  196.         $message .= PHP_EOL;
  197.         $message .= parent::getHelpMessage($author$email);
  198.  
  199.         throw new Console_Exception($message);
  200.     }
  201. }

Documentation generated on Mon, 17 Aug 2009 14:53:12 +0200 by phpDocumentor 1.4.2