Total Complexity | 51 |
Total Lines | 638 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like FtsTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FtsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait FtsTrait |
||
13 | { |
||
14 | /** |
||
15 | * Creates a new FTS configuration. |
||
16 | * |
||
17 | * @param string $cfgname The name of the FTS configuration to create |
||
18 | * @param string $parser The parser to be used in new FTS configuration |
||
19 | * @param string $template The existing FTS configuration to be used as template for the new one |
||
20 | * @param string $comment If omitted, defaults to nothing |
||
21 | * |
||
22 | * @return bool|int 0 success |
||
23 | * |
||
24 | * @internal param string $locale Locale of the FTS configuration |
||
25 | * @internal param string $withmap Should we copy whole map of existing FTS configuration to the new one |
||
26 | * @internal param string $makeDefault Should this configuration be the default for locale given |
||
27 | */ |
||
28 | public function createFtsConfiguration($cfgname, $parser = '', $template = '', $comment = '') |
||
29 | { |
||
30 | $f_schema = $this->_schema; |
||
31 | $this->fieldClean($f_schema); |
||
32 | $this->fieldClean($cfgname); |
||
33 | |||
34 | $sql = "CREATE TEXT SEARCH CONFIGURATION \"{$f_schema}\".\"{$cfgname}\" ("; |
||
35 | if ($parser != '') { |
||
36 | $this->fieldClean($parser['schema']); |
||
37 | $this->fieldClean($parser['parser']); |
||
38 | $parser = "\"{$parser['schema']}\".\"{$parser['parser']}\""; |
||
39 | $sql .= " PARSER = {$parser}"; |
||
40 | } |
||
41 | if ($template != '') { |
||
42 | $this->fieldClean($template['schema']); |
||
43 | $this->fieldClean($template['name']); |
||
44 | $sql .= " COPY = \"{$template['schema']}\".\"{$template['name']}\""; |
||
45 | } |
||
46 | $sql .= ')'; |
||
47 | |||
48 | if ($comment != '') { |
||
49 | $status = $this->beginTransaction(); |
||
50 | if ($status != 0) { |
||
51 | return -1; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | // Create the FTS configuration |
||
56 | $status = $this->execute($sql); |
||
57 | if ($status != 0) { |
||
58 | $this->rollbackTransaction(); |
||
59 | |||
60 | return -1; |
||
61 | } |
||
62 | |||
63 | // Set the comment |
||
64 | if ($comment != '') { |
||
65 | $status = $this->setComment('TEXT SEARCH CONFIGURATION', $cfgname, '', $comment); |
||
66 | if ($status != 0) { |
||
67 | $this->rollbackTransaction(); |
||
68 | |||
69 | return -1; |
||
70 | } |
||
71 | |||
72 | return $this->endTransaction(); |
||
73 | } |
||
74 | |||
75 | return 0; |
||
76 | } |
||
77 | |||
78 | // Language functions |
||
79 | |||
80 | /** |
||
81 | * Returns available FTS configurations. |
||
82 | * |
||
83 | * @param bool $all if false, returns schema qualified FTS confs |
||
84 | * |
||
85 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
86 | */ |
||
87 | public function getFtsConfigurations($all = true) |
||
88 | { |
||
89 | $c_schema = $this->_schema; |
||
90 | $this->clean($c_schema); |
||
91 | $sql = " |
||
92 | SELECT |
||
93 | n.nspname as schema, |
||
94 | c.cfgname as name, |
||
95 | pg_catalog.obj_description(c.oid, 'pg_ts_config') as comment |
||
96 | FROM |
||
97 | pg_catalog.pg_ts_config c |
||
98 | JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace |
||
99 | WHERE |
||
100 | pg_catalog.pg_ts_config_is_visible(c.oid)"; |
||
101 | |||
102 | if (!$all) { |
||
103 | $sql .= " AND n.nspname='{$c_schema}'\n"; |
||
104 | } |
||
105 | |||
106 | $sql .= 'ORDER BY name'; |
||
107 | |||
108 | return $this->selectSet($sql); |
||
109 | } |
||
110 | |||
111 | // Aggregate functions |
||
112 | |||
113 | /** |
||
114 | * Returns the map of FTS configuration given |
||
115 | * (list of mappings (tokens) and their processing dictionaries). |
||
116 | * |
||
117 | * @param string $ftscfg Name of the FTS configuration |
||
118 | * |
||
119 | * @return \PHPPgAdmin\ADORecordSet recordset |
||
120 | */ |
||
121 | public function getFtsConfigurationMap($ftscfg) |
||
122 | { |
||
123 | $c_schema = $this->_schema; |
||
124 | $this->clean($c_schema); |
||
125 | $this->fieldClean($ftscfg); |
||
126 | |||
127 | $oidSet = $this->selectSet("SELECT c.oid |
||
1 ignored issue
–
show
|
|||
128 | FROM pg_catalog.pg_ts_config AS c |
||
129 | LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.cfgnamespace) |
||
130 | WHERE c.cfgname = '{$ftscfg}' |
||
131 | AND n.nspname='{$c_schema}'"); |
||
1 ignored issue
–
show
|
|||
132 | |||
133 | $oid = $oidSet->fields['oid']; |
||
134 | |||
135 | $sql = " |
||
136 | SELECT |
||
137 | (SELECT t.alias FROM pg_catalog.ts_token_type(c.cfgparser) AS t WHERE t.tokid = m.maptokentype) AS name, |
||
138 | (SELECT t.description FROM pg_catalog.ts_token_type(c.cfgparser) AS t WHERE t.tokid = m.maptokentype) AS description, |
||
139 | c.cfgname AS cfgname, n.nspname ||'.'|| d.dictname as dictionaries |
||
140 | FROM |
||
141 | pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m, pg_catalog.pg_ts_dict d, |
||
142 | pg_catalog.pg_namespace n |
||
143 | WHERE |
||
144 | c.oid = {$oid} |
||
145 | AND m.mapcfg = c.oid |
||
146 | AND m.mapdict = d.oid |
||
147 | AND d.dictnamespace = n.oid |
||
148 | ORDER BY name |
||
149 | "; |
||
150 | |||
151 | return $this->selectSet($sql); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Returns FTS parsers available. |
||
156 | * |
||
157 | * @param bool $all if false, return only Parsers from the current schema |
||
158 | * |
||
159 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
160 | */ |
||
161 | public function getFtsParsers($all = true) |
||
162 | { |
||
163 | $c_schema = $this->_schema; |
||
164 | $this->clean($c_schema); |
||
165 | $sql = " |
||
166 | SELECT |
||
167 | n.nspname as schema, |
||
168 | p.prsname as name, |
||
169 | pg_catalog.obj_description(p.oid, 'pg_ts_parser') as comment |
||
170 | FROM pg_catalog.pg_ts_parser p |
||
171 | LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = p.prsnamespace) |
||
172 | WHERE pg_catalog.pg_ts_parser_is_visible(p.oid)"; |
||
173 | |||
174 | if (!$all) { |
||
175 | $sql .= " AND n.nspname='{$c_schema}'\n"; |
||
176 | } |
||
177 | |||
178 | $sql .= 'ORDER BY name'; |
||
179 | |||
180 | return $this->selectSet($sql); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Returns FTS dictionaries available. |
||
185 | * |
||
186 | * @param bool $all if false, return only Dics from the current schema |
||
187 | * |
||
188 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
189 | */ |
||
190 | public function getFtsDictionaries($all = true) |
||
191 | { |
||
192 | $c_schema = $this->_schema; |
||
193 | $this->clean($c_schema); |
||
194 | $sql = " |
||
195 | SELECT |
||
196 | n.nspname as schema, d.dictname as name, |
||
197 | pg_catalog.obj_description(d.oid, 'pg_ts_dict') as comment |
||
198 | FROM pg_catalog.pg_ts_dict d |
||
199 | LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace |
||
200 | WHERE pg_catalog.pg_ts_dict_is_visible(d.oid)"; |
||
201 | |||
202 | if (!$all) { |
||
203 | $sql .= " AND n.nspname='{$c_schema}'\n"; |
||
204 | } |
||
205 | |||
206 | $sql .= 'ORDER BY name;'; |
||
207 | |||
208 | return $this->selectSet($sql); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Returns all FTS dictionary templates available. |
||
213 | * |
||
214 | * @return \PHPPgAdmin\ADORecordSet all FTS dictionary templates available |
||
215 | */ |
||
216 | public function getFtsDictionaryTemplates() |
||
217 | { |
||
218 | $sql = " |
||
219 | SELECT |
||
220 | n.nspname as schema, |
||
221 | t.tmplname as name, |
||
222 | ( SELECT COALESCE(np.nspname, '(null)')::pg_catalog.text || '.' || p.proname |
||
223 | FROM pg_catalog.pg_proc p |
||
224 | LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.pronamespace |
||
225 | WHERE t.tmplinit = p.oid ) AS init, |
||
226 | ( SELECT COALESCE(np.nspname, '(null)')::pg_catalog.text || '.' || p.proname |
||
227 | FROM pg_catalog.pg_proc p |
||
228 | LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.pronamespace |
||
229 | WHERE t.tmpllexize = p.oid ) AS lexize, |
||
230 | pg_catalog.obj_description(t.oid, 'pg_ts_template') as comment |
||
231 | FROM pg_catalog.pg_ts_template t |
||
232 | LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace |
||
233 | WHERE pg_catalog.pg_ts_template_is_visible(t.oid) |
||
234 | ORDER BY name;"; |
||
235 | |||
236 | return $this->selectSet($sql); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Drops FTS coniguration. |
||
241 | * |
||
242 | * @param string $ftscfg The configuration's name |
||
243 | * @param bool $cascade true to Cascade to dependenced objects |
||
244 | * |
||
245 | * @return int 0 if operation was successful |
||
246 | */ |
||
247 | public function dropFtsConfiguration($ftscfg, $cascade) |
||
248 | { |
||
249 | $f_schema = $this->_schema; |
||
250 | $this->fieldClean($f_schema); |
||
251 | $this->fieldClean($ftscfg); |
||
252 | |||
253 | $sql = "DROP TEXT SEARCH CONFIGURATION \"{$f_schema}\".\"{$ftscfg}\""; |
||
254 | if ($cascade) { |
||
255 | $sql .= ' CASCADE'; |
||
256 | } |
||
257 | |||
258 | return $this->execute($sql); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Drops FTS dictionary. |
||
263 | * |
||
264 | * @param string $ftsdict The dico's name |
||
265 | * @param bool $cascade Cascade to dependenced objects |
||
266 | * |
||
267 | * @return int 0 if operation was successful |
||
268 | * |
||
269 | * @todo Support of dictionary templates dropping |
||
270 | */ |
||
271 | public function dropFtsDictionary($ftsdict, $cascade) |
||
272 | { |
||
273 | $f_schema = $this->_schema; |
||
274 | $this->fieldClean($f_schema); |
||
275 | $this->fieldClean($ftsdict); |
||
276 | |||
277 | $sql = 'DROP TEXT SEARCH DICTIONARY'; |
||
278 | $sql .= " \"{$f_schema}\".\"{$ftsdict}\""; |
||
279 | if ($cascade) { |
||
280 | $sql .= ' CASCADE'; |
||
281 | } |
||
282 | |||
283 | return $this->execute($sql); |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Alters FTS configuration. |
||
288 | * |
||
289 | * @param string $cfgname The conf's name |
||
290 | * @param string $comment A comment on for the conf |
||
291 | * @param string $name The new conf name |
||
292 | * |
||
293 | * @return bool|int 0 on success |
||
294 | */ |
||
295 | public function updateFtsConfiguration($cfgname, $comment, $name) |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Creates a new FTS dictionary or FTS dictionary template. |
||
333 | * |
||
334 | * @param string $dictname The name of the FTS dictionary to create |
||
335 | * @param bool $isTemplate Flag whether we create usual dictionary or dictionary template |
||
336 | * @param string $template The existing FTS dictionary to be used as template for the new one |
||
337 | * @param string $lexize The name of the function, which does transformation of input word |
||
338 | * @param string $init The name of the function, which initializes dictionary |
||
339 | * @param string $option Usually, it stores various options required for the dictionary |
||
340 | * @param string $comment If omitted, defaults to nothing |
||
341 | * |
||
342 | * @return bool|int 0 success |
||
343 | */ |
||
344 | public function createFtsDictionary( |
||
345 | $dictname, |
||
346 | $isTemplate = false, |
||
347 | $template = '', |
||
348 | $lexize = '', |
||
349 | $init = '', |
||
350 | $option = '', |
||
351 | $comment = '' |
||
352 | ) { |
||
353 | $f_schema = $this->_schema; |
||
354 | $this->fieldClean($f_schema); |
||
355 | $this->fieldClean($dictname); |
||
356 | $this->fieldClean($template); |
||
357 | $this->fieldClean($lexize); |
||
358 | $this->fieldClean($init); |
||
359 | $this->fieldClean($option); |
||
360 | |||
361 | $sql = 'CREATE TEXT SEARCH'; |
||
362 | if ($isTemplate) { |
||
363 | $sql .= " TEMPLATE \"{$f_schema}\".\"{$dictname}\" ("; |
||
364 | if ($lexize != '') { |
||
365 | $sql .= " LEXIZE = {$lexize}"; |
||
366 | } |
||
367 | |||
368 | if ($init != '') { |
||
369 | $sql .= ", INIT = {$init}"; |
||
370 | } |
||
371 | |||
372 | $sql .= ')'; |
||
373 | $whatToComment = 'TEXT SEARCH TEMPLATE'; |
||
374 | } else { |
||
375 | $sql .= " DICTIONARY \"{$f_schema}\".\"{$dictname}\" ("; |
||
376 | if ($template != '') { |
||
377 | $this->fieldClean($template['schema']); |
||
378 | $this->fieldClean($template['name']); |
||
379 | $template = "\"{$template['schema']}\".\"{$template['name']}\""; |
||
380 | |||
381 | $sql .= " TEMPLATE = {$template}"; |
||
382 | } |
||
383 | if ($option != '') { |
||
384 | $sql .= ", {$option}"; |
||
385 | } |
||
386 | |||
387 | $sql .= ')'; |
||
388 | $whatToComment = 'TEXT SEARCH DICTIONARY'; |
||
389 | } |
||
390 | |||
391 | /* if comment, begin a transaction to |
||
392 | * run both commands */ |
||
393 | if ($comment != '') { |
||
394 | $status = $this->beginTransaction(); |
||
395 | if ($status != 0) { |
||
396 | return -1; |
||
397 | } |
||
398 | } |
||
399 | |||
400 | // Create the FTS dictionary |
||
401 | $status = $this->execute($sql); |
||
402 | if ($status != 0) { |
||
403 | $this->rollbackTransaction(); |
||
404 | |||
405 | return -1; |
||
406 | } |
||
407 | |||
408 | // Set the comment |
||
409 | if ($comment != '') { |
||
410 | $status = $this->setComment($whatToComment, $dictname, '', $comment); |
||
411 | if ($status != 0) { |
||
412 | $this->rollbackTransaction(); |
||
413 | |||
414 | return -1; |
||
415 | } |
||
416 | } |
||
417 | |||
418 | return $this->endTransaction(); |
||
419 | } |
||
420 | |||
421 | // Role, User/Group functions |
||
422 | |||
423 | /** |
||
424 | * Alters FTS dictionary or dictionary template. |
||
425 | * |
||
426 | * @param string $dictname The dico's name |
||
427 | * @param string $comment The comment |
||
428 | * @param string $name The new dico's name |
||
429 | * |
||
430 | * @return bool|int 0 on success |
||
431 | */ |
||
432 | public function updateFtsDictionary($dictname, $comment, $name) |
||
433 | { |
||
434 | $status = $this->beginTransaction(); |
||
435 | if ($status != 0) { |
||
436 | $this->rollbackTransaction(); |
||
437 | |||
438 | return -1; |
||
439 | } |
||
440 | |||
441 | $this->fieldClean($dictname); |
||
442 | $status = $this->setComment('TEXT SEARCH DICTIONARY', $dictname, '', $comment); |
||
443 | if ($status != 0) { |
||
444 | $this->rollbackTransaction(); |
||
445 | |||
446 | return -1; |
||
447 | } |
||
448 | |||
449 | // Only if the name has changed |
||
450 | if ($name != $dictname) { |
||
451 | $f_schema = $this->_schema; |
||
452 | $this->fieldClean($f_schema); |
||
453 | $this->fieldClean($name); |
||
454 | |||
455 | $sql = "ALTER TEXT SEARCH DICTIONARY \"{$f_schema}\".\"{$dictname}\" RENAME TO \"{$name}\""; |
||
456 | $status = $this->execute($sql); |
||
457 | if ($status != 0) { |
||
458 | $this->rollbackTransaction(); |
||
459 | |||
460 | return -1; |
||
461 | } |
||
462 | } |
||
463 | |||
464 | return $this->endTransaction(); |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * Return all information relating to a FTS dictionary. |
||
469 | * |
||
470 | * @param string $ftsdict The name of the FTS dictionary |
||
471 | * |
||
472 | * @return \PHPPgAdmin\ADORecordSet recordset of FTS dictionary information |
||
473 | */ |
||
474 | public function getFtsDictionaryByName($ftsdict) |
||
475 | { |
||
476 | $c_schema = $this->_schema; |
||
477 | $this->clean($c_schema); |
||
478 | $this->clean($ftsdict); |
||
479 | |||
480 | $sql = "SELECT |
||
481 | n.nspname as schema, |
||
482 | d.dictname as name, |
||
483 | ( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM |
||
484 | pg_catalog.pg_ts_template t |
||
485 | LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace |
||
486 | WHERE d.dicttemplate = t.oid ) AS template, |
||
487 | d.dictinitoption as init, |
||
488 | pg_catalog.obj_description(d.oid, 'pg_ts_dict') as comment |
||
489 | FROM pg_catalog.pg_ts_dict d |
||
490 | LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace |
||
491 | WHERE d.dictname = '{$ftsdict}' |
||
492 | AND pg_catalog.pg_ts_dict_is_visible(d.oid) |
||
493 | AND n.nspname='{$c_schema}' |
||
494 | ORDER BY name"; |
||
495 | |||
496 | return $this->selectSet($sql); |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * Creates/updates/deletes FTS mapping. |
||
501 | * |
||
502 | * @param string $ftscfg The name of the FTS dictionary |
||
503 | * @param array $mapping Array of tokens' names |
||
504 | * @param string $action What to do with the mapping: add, alter or drop |
||
505 | * @param string $dictname Dictionary that will process tokens given or null in case of drop action |
||
506 | * |
||
507 | * @return int 0 if operation was successful |
||
508 | * |
||
509 | * @internal param string $cfgname The name of the FTS configuration to alter |
||
510 | */ |
||
511 | public function changeFtsMapping($ftscfg, $mapping, $action, $dictname = null) |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * Return all information related to a given FTS configuration's mapping. |
||
549 | * |
||
550 | * @param string $ftscfg The name of the FTS configuration |
||
551 | * @param string $mapping The name of the mapping |
||
552 | * |
||
553 | * @return \PHPPgAdmin\ADORecordSet FTS configuration information |
||
554 | */ |
||
555 | public function getFtsMappingByName($ftscfg, $mapping) |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * Return list of FTS mappings possible for given parser |
||
589 | * (specified by given configuration since configuration can only have 1 parser). |
||
590 | * |
||
591 | * @param string $ftscfg The config's name that use the parser |
||
592 | * |
||
593 | * @return int 0 if operation was successful |
||
594 | */ |
||
595 | public function getFtsMappings($ftscfg) |
||
604 | } |
||
605 | |||
606 | /** |
||
607 | * Return all information related to a FTS configuration. |
||
608 | * |
||
609 | * @param string $ftscfg The name of the FTS configuration |
||
610 | * |
||
611 | * @return \PHPPgAdmin\ADORecordSet FTS configuration information |
||
612 | */ |
||
613 | public function getFtsConfigurationByName($ftscfg) |
||
633 | } |
||
634 | |||
635 | abstract public function fieldClean(&$str); |
||
636 | |||
637 | abstract public function beginTransaction(); |
||
638 | |||
639 | abstract public function rollbackTransaction(); |
||
640 | |||
641 | abstract public function endTransaction(); |
||
642 | |||
643 | abstract public function execute($sql); |
||
644 | |||
645 | abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
||
646 | |||
647 | abstract public function selectSet($sql); |
||
648 | |||
649 | abstract public function clean(&$str); |
||
650 | } |
||
651 |