1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* generate MongoDB Fulltext-Search Indexes |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\GeneratorBundle\Command; |
7
|
|
|
|
8
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
9
|
|
|
use Graviton\GeneratorBundle\Definition\JsonDefinition; |
10
|
|
|
use Graviton\GeneratorBundle\Definition\Loader\LoaderInterface; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Here, we generate all MongoDB Fulltext-Search Indexes |
19
|
|
|
* |
20
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
21
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
22
|
|
|
* @link http://swisscom.ch |
23
|
|
|
*/ |
24
|
|
|
class GenerateBuildIndexesCommand extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var LoaderInterface |
28
|
|
|
*/ |
29
|
|
|
private $definitionLoader; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var documentManager |
33
|
|
|
*/ |
34
|
|
|
private $documentManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* GenerateBuildIndexesCommand constructor. |
38
|
|
|
* |
39
|
|
|
* @param LoaderInterface $definitionLoader The definition Loader - loads Definitions from JSON-Files |
40
|
|
|
* @param DocumentManager $documentManager The Doctrine Document Manager |
41
|
|
|
* @param String $name The Name of this Command |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
LoaderInterface $definitionLoader, |
45
|
|
|
DocumentManager $documentManager, |
46
|
|
|
$name = null |
47
|
|
|
) { |
48
|
|
|
parent::__construct($name); |
49
|
|
|
|
50
|
|
|
$this->definitionLoader = $definitionLoader; |
51
|
|
|
$this->documentManager = $documentManager; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
protected function configure() |
60
|
|
|
{ |
61
|
|
|
parent::configure(); |
62
|
|
|
|
63
|
|
|
$this |
64
|
|
|
->setName('graviton:generate:build-indexes') |
65
|
|
|
->setDescription( |
66
|
|
|
'Generates Mongo-Text Indexes (MongoDB >= 2.6) for collections as defined' |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
* |
73
|
|
|
* @param InputInterface $input input |
74
|
|
|
* @param OutputInterface $output output |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
79
|
|
|
{ |
80
|
|
|
// Check mongo db version |
81
|
|
|
$mongoVersion = $this->getMongoDBVersion('Graviton\\CoreBundle\\Document\\App'); |
82
|
|
|
if ((float) $mongoVersion < 2.6) { |
83
|
|
|
$output->writeln("MongoDB Version =< 2.6 installed: " . $mongoVersion); |
84
|
|
|
exit(); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$metadatas = $this->documentManager->getMetadataFactory()->getAllMetadata(); |
88
|
|
|
/** @var ClassMetadata $metadata */ |
89
|
|
|
foreach ($metadatas as $metadata) { |
90
|
|
|
$indexes = $metadata->getIndexes(); |
91
|
|
|
$searchName = 'search'.$metadata->getCollection().'Index'; |
92
|
|
|
foreach ($indexes as $index) { |
93
|
|
|
if (array_key_exists('keys', $index) && array_key_exists($searchName, $index['keys'])) { |
94
|
|
|
if (array_key_exists('options', $index) && !empty($index['options'])) { |
95
|
|
|
$collection = $this->documentManager->getDocumentCollection($metadata->getName()); |
96
|
|
|
if (!$collection) { |
97
|
|
|
continue; |
98
|
|
|
} |
99
|
|
|
$newIndex = []; |
100
|
|
|
foreach ($index['options'] as $optionName => $optionsValue) { |
101
|
|
|
$optionName = str_replace('search.', '', $optionName); |
102
|
|
|
$newIndex[$optionName] = 'text'; |
103
|
|
|
$index['options'][$optionName] = floatval($optionsValue); |
104
|
|
|
} |
105
|
|
|
foreach ($collection->getIndexInfo() as $indexInfo) { |
106
|
|
|
// When using doctrine name may have a _1 |
107
|
|
|
if (strpos($indexInfo['name'], $searchName) !== false) { |
108
|
|
|
$output->writeln("Deleting Custom Text index {$searchName}"); |
109
|
|
|
$this->documentManager->getDocumentDatabase($metadata->getName())->command( |
110
|
|
|
[ |
111
|
|
|
"deleteIndexes" => $collection->getName(), |
112
|
|
|
"index" => $indexInfo['name'] |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
break; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
$output->writeln($metadata->getName().": created custom Text index {$searchName}"); |
119
|
|
|
$collection->ensureIndex( |
120
|
|
|
$newIndex, |
121
|
|
|
[ |
122
|
|
|
'weights' => $index['options'], |
123
|
|
|
'name' => $searchName, |
124
|
|
|
'default_language' => 'de', |
125
|
|
|
'language_override' => 'language' |
126
|
|
|
] |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Gets the installed MongoDB Version |
137
|
|
|
* @param String $className The Classname of the collection, needed to fetch the right DB connection |
138
|
|
|
* @return String getMongoDBVersion The version of the MongoDB as a string |
139
|
|
|
*/ |
140
|
|
|
private function getMongoDBVersion($className) |
141
|
|
|
{ |
142
|
|
|
$buildInfo = $this->documentManager->getDocumentDatabase($className)->command(['buildinfo' => 1]); |
143
|
|
|
if (isset($buildInfo['version'])) { |
144
|
|
|
return $buildInfo['version']; |
145
|
|
|
} else { |
146
|
|
|
return 'unkown'; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.