1 | <?php |
---|
2 | |
---|
3 | class reindexSolrObjectTask extends sfBaseTask { |
---|
4 | |
---|
5 | protected function configure() { |
---|
6 | $this->namespace = 'reindex'; |
---|
7 | $this->name = 'SolrObject'; |
---|
8 | $this->briefDescription = 'Réindexe un objet ou supprime son index si non-existant ; usage : php symfony reindex:SolrObject object_class object_id'; |
---|
9 | $this->addArgument('class', sfCommandArgument::REQUIRED, 'Classe de l\'objet'); |
---|
10 | $this->addArgument('id', sfCommandArgument::REQUIRED, 'ID de l\'objet'); |
---|
11 | $this->addOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'Changes the environment this task is run in', 'test'); |
---|
12 | $this->addOption('app', null, sfCommandOption::PARAMETER_OPTIONAL, 'Changes the environment this task is run in', 'frontend'); |
---|
13 | } |
---|
14 | |
---|
15 | protected function execute($arguments = array(), $options = array()) { |
---|
16 | $manager = new sfDatabaseManager($this->configuration); |
---|
17 | $class = $arguments['class']; |
---|
18 | if (!preg_match('/^(Commentaire|Intervention|Amendement|QuestionEcrite|Section|Organisme|Texteloi|Parlementaire|Seance)$/', $class)) { |
---|
19 | echo "ERREUR : $class n'est pas une classe d'objet indexé dans Solr\n"; |
---|
20 | return; |
---|
21 | } |
---|
22 | $id = $arguments['id']; |
---|
23 | if (!($id >= 0)) { |
---|
24 | echo "ERREUR : $id n'a pas l'air d'une id correcte"; |
---|
25 | return; |
---|
26 | } |
---|
27 | if ($class === "Seance") { |
---|
28 | $inters = Doctrine::getTable('Intervention')->createQuery('i')->where('seance_id = ?', $id)->execute(); |
---|
29 | foreach ($inters as $i) |
---|
30 | $this->index($i); |
---|
31 | } else { |
---|
32 | $obj = Doctrine::getTable($class)->find($id); |
---|
33 | $this->index($obj); |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | protected static function index($obj) { |
---|
38 | if (!$obj) { |
---|
39 | $json = new stdClass(); |
---|
40 | $json->id = $class.'/'.$id; |
---|
41 | SolrCommands::getInstance()->addCommand('DELETE', $json); |
---|
42 | } else { |
---|
43 | $obj->save(); |
---|
44 | } |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|