|
25
|
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\Utils; |
|
|
12 |
|
|
|
13 |
use Mandango\Mondator\Definition\Definition; |
|
|
14 |
use Mandango\Mondator\Definition\Property; |
|
|
15 |
use Mandango\Mondator\Definition\Method; |
|
|
16 |
use Mandango\Mondator\Dumper; |
|
|
17 |
|
|
|
18 |
class SchemaUtils |
|
|
19 |
{ |
|
|
20 |
/** |
|
|
21 |
* The container for the service |
|
|
22 |
* @var unknown_type |
|
|
23 |
*/ |
|
|
24 |
protected $container; |
|
|
25 |
|
|
|
26 |
/** |
|
|
27 |
* Accessor for the container property. |
|
|
28 |
*/ |
|
|
29 |
public function getContainer() |
|
|
30 |
{ |
|
|
31 |
return $this->container; |
|
|
32 |
} |
|
|
33 |
|
|
|
34 |
/** |
|
|
35 |
* |
|
|
36 |
* construct the shema utils service injects the container |
|
|
37 |
* @param unknown_type $container |
|
|
38 |
*/ |
|
|
39 |
public function __construct($container) |
|
|
40 |
{ |
|
|
41 |
$this->container = $container; |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
/** |
|
|
47 |
* Return the sql to create the document table full text indexes |
|
|
48 |
* @return array |
|
|
49 |
*/ |
|
|
50 |
public function createFullTextIndexes() |
|
|
51 |
{ |
|
|
52 |
$sql_code = array(); |
|
|
53 |
$fields = $this->getContainer()->getParameter('wiki_tag.fields'); |
|
|
54 |
$def_columns = array(); |
|
|
55 |
foreach ( $fields as $name => $field_def) |
|
|
56 |
{ |
|
|
57 |
if(isset($field_def['type'])) |
|
|
58 |
{ |
|
|
59 |
$type = $field_def['type']; |
|
|
60 |
} |
|
|
61 |
if(!isset($type) || is_null($type) || strlen($type) == 0) |
|
|
62 |
{ |
|
|
63 |
$type = "text"; |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
if($type === 'text') |
|
|
67 |
{ |
|
|
68 |
$def_column = "$name(4096)"; |
|
|
69 |
} |
|
|
70 |
else |
|
|
71 |
{ |
|
|
72 |
$def_column = $name; |
|
|
73 |
} |
|
|
74 |
$def_columns[] = $def_column; |
|
|
75 |
|
|
|
76 |
$sql_code[] = "ALTER IGNORE TABLE wikitag_document DROP INDEX ${name}_document_fulltext_idx"; |
|
|
77 |
$sql_code[] = "ALTER TABLE wikitag_document ADD FULLTEXT INDEX ${name}_document_fulltext_idx ($def_column)"; |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
$sql_code[] = "ALTER IGNORE TABLE wikitag_document DROP INDEX tags_str_document_fulltext_idx"; |
|
|
81 |
$sql_code[] = "ALTER TABLE wikitag_document ADD FULLTEXT INDEX tags_str_document_fulltext_idx (tags_str)"; |
|
|
82 |
|
|
|
83 |
$sql_code[] = "ALTER IGNORE TABLE wikitag_document DROP INDEX all_document_fulltext_idx"; |
|
|
84 |
$sql_code[] = "ALTER TABLE wikitag_document ADD FULLTEXT INDEX all_document_fulltext_idx (".join(",", $def_columns).")"; |
|
|
85 |
|
|
|
86 |
return $sql_code; |
|
|
87 |
|
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
public function filter_foreign_key(array $sqls) |
|
|
91 |
{ |
|
|
92 |
$res_sqls = array(); |
|
|
93 |
//TODO : take the column and table names from the schema |
|
|
94 |
foreach ($sqls as $sql) { |
|
|
95 |
if(!preg_match("/ADD CONSTRAINT .+ FOREIGN KEY \(.*\) REFERENCES wikitag_document\(id\)/i", $sql)) |
|
|
96 |
{ |
|
|
97 |
$res_sqls[] = $sql; |
|
|
98 |
} |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
return $res_sqls; |
|
|
102 |
|
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
public function filter_index_creation(array $sqls) |
|
|
106 |
{ |
|
|
107 |
$res_sqls = array(); |
|
|
108 |
|
|
|
109 |
$replace_regexps = array(); |
|
|
110 |
|
|
|
111 |
$fields = $this->getContainer()->getParameter('wiki_tag.fields'); |
|
|
112 |
$field_names = array(); |
|
|
113 |
foreach ( $fields as $name => $field_def) |
|
|
114 |
{ |
|
|
115 |
// create regular expression |
|
|
116 |
$replace_regexps[] = "/INDEX (${name}_document_fulltext_idx (?:ON wikitag_document ){0,1}\(${name}\))/"; |
|
|
117 |
$field_names[] = " ?${name},?"; |
|
|
118 |
} |
|
|
119 |
$field_names[] = " ?tags_str,?"; |
|
|
120 |
$replace_regexps[] = "/INDEX (tags_str_document_fulltext_idx (?:ON wikitag_document ){0,1}\(tags_str\))/"; |
|
|
121 |
$replace_regexps[] = "/INDEX (all_document_fulltext_idx (?:ON wikitag_document ){0,1}\((?:".implode("|",$field_names)."){".count($field_names)."}\))/"; |
|
|
122 |
|
|
|
123 |
foreach($sqls as $sql) |
|
|
124 |
{ |
|
|
125 |
if(strrpos($sql,"wikitag_document")) |
|
|
126 |
{ |
|
|
127 |
$sql = preg_replace($replace_regexps, "FULLTEXT INDEX $1", $sql); |
|
|
128 |
} |
|
|
129 |
$res_sqls[] = $sql; |
|
|
130 |
} |
|
|
131 |
|
|
|
132 |
return $res_sqls; |
|
|
133 |
} |
|
|
134 |
|
|
|
135 |
public function generateDocumentClass() |
|
|
136 |
{ |
|
|
137 |
$definition = new Definition('IRI\Bundle\WikiTagBundle\Entity\Document'); |
|
|
138 |
|
|
|
139 |
$definition->setParentClass('\IRI\Bundle\WikiTagBundle\Model\Document'); |
|
|
140 |
|
|
|
141 |
$fields = $this->getContainer()->getParameter('wiki_tag.fields'); |
|
|
142 |
|
|
|
143 |
foreach ( $fields as $name => $field_def) |
|
|
144 |
{ |
|
|
145 |
$property = new Property("private", $name, NULL); |
|
|
146 |
$definition->addProperty($property); |
|
|
147 |
|
|
|
148 |
$get_method = new Method("public", "get".ucfirst($name), NULL, <<<EOF |
|
|
149 |
return \$this->$name; |
|
|
150 |
EOF |
|
|
151 |
); |
|
|
152 |
$definition->addMethod($get_method); |
|
|
153 |
|
|
|
154 |
$set_method = new Method("public", "set".ucfirst($name), "\$$name", <<<EOF |
|
|
155 |
\$this->$name = \$$name; |
|
|
156 |
EOF |
|
|
157 |
); |
|
|
158 |
$definition->addMethod($set_method); |
|
|
159 |
|
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
$dumper = new Dumper($definition); |
|
|
163 |
$classCode = $dumper->dump(); |
|
|
164 |
|
|
|
165 |
return $classCode; |
|
|
166 |
} |
|
|
167 |
} |