Command/CreateFullTextIndexesCommand.php
changeset 19 7051e55a3131
child 23 b435f8055cb4
equal deleted inserted replaced
18:6f16b9fd6a17 19:7051e55a3131
       
     1 <?php
       
     2 /*
       
     3  * This file is part of the WikiTagBundle package.
       
     4  *
       
     5  * (c) IRI <http://www.iri.centrepompidou.fr/>
       
     6  *
       
     7  * For the full copyright and license information, please view the LICENSE
       
     8  * file that was distributed with this source code.
       
     9  */
       
    10 
       
    11 namespace IRI\Bundle\WikiTagBundle\Command;
       
    12 
       
    13 
       
    14 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
       
    15 use Symfony\Component\Console\Input\InputArgument;
       
    16 use Symfony\Component\Console\Input\InputInterface;
       
    17 use Symfony\Component\Console\Input\InputOption;
       
    18 use Symfony\Component\Console\Output\OutputInterface;
       
    19 use Mandango\Mondator\Definition\Definition;
       
    20 use Mandango\Mondator\Definition\Property;
       
    21 use Mandango\Mondator\Definition\Method;
       
    22 use Mandango\Mondator\Dumper;
       
    23 
       
    24 class CreateFullTextIndexesCommand extends ContainerAwareCommand
       
    25 {
       
    26     protected function configure()
       
    27     {
       
    28         parent::configure();
       
    29         
       
    30         $this
       
    31             ->setName('wikitag:create-fulltext-indexes')
       
    32             ->setDescription('Generate the full text indexes for the document table')
       
    33             ->addArgument('path', InputArgument::OPTIONAL, 'The generation path')
       
    34             ->addOption("simulate","S",InputOption::VALUE_NONE, "Simulate generation");
       
    35     }
       
    36 
       
    37     protected function execute(InputInterface $input, OutputInterface $output)
       
    38     {
       
    39         $file = $input->getArgument('path');
       
    40         $simulate = $input->getOption('simulate');
       
    41         if(is_null($file) || strlen($file) == 0)
       
    42         {
       
    43             $simulate = true;
       
    44         }
       
    45         
       
    46         $sql_code = "";
       
    47         $fields = $this->getContainer()->getParameter('wiki_tag.fields');
       
    48         $def_columns = array();
       
    49         foreach ( $fields as $name => $field_def)
       
    50         {
       
    51             if(isset($field_def['type']))
       
    52             {
       
    53                 $type = $field_def['type'];
       
    54             }
       
    55             if(!isset($type) || is_null($type) || strlen($type) == 0)
       
    56             {
       
    57                 $type = "text";
       
    58             }
       
    59             
       
    60             if($type === 'text')
       
    61             {
       
    62                 $def_column = "$name(4096)";
       
    63             }
       
    64             else
       
    65             {
       
    66                 $def_column = $name;
       
    67             }
       
    68             $def_columns[] = $def_column;
       
    69             
       
    70             $sql_code .= "ALTER IGNORE TABLE wikitag_document DROP INDEX ${name}_document_fulltext_idx;\n";
       
    71             $sql_code .= "ALTER TABLE wikitag_document ADD FULLTEXT INDEX ${name}_document_fulltext_idx ($def_column);\n";
       
    72         }
       
    73         
       
    74         $sql_code .= "ALTER IGNORE TABLE wikitag_document DROP INDEX all_document_fulltext_idx;\n";
       
    75         $sql_code .= "ALTER TABLE wikitag_document ADD FULLTEXT INDEX all_document_fulltext_idx (".join(",", $def_columns).");\n";
       
    76         
       
    77         
       
    78         if($simulate)
       
    79         {
       
    80             $output->writeln($sql_code);
       
    81         }
       
    82         else
       
    83         {
       
    84             $output->writeln("Creating Indexes in $file");
       
    85             
       
    86             if(!file_exists(dirname($file)) && !mkdir(dirname($file),0777,true))
       
    87             {
       
    88                 $output->writeln("Impossible to create folder exitiing.");
       
    89                 die;
       
    90             }
       
    91             file_put_contents($file, $sql_code);
       
    92         }
       
    93 
       
    94     }
       
    95 }