1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
<?php
/**
* PHPPgAdmin v6.0.0-RC6
*/
namespace PHPPgAdmin\Database\Traits;
/**
* Common trait for full text search manipulation.
*/
trait FtsTrait
{
/**
* Creates a new FTS configuration.
*
* @param string $cfgname The name of the FTS configuration to create
* @param string $parser The parser to be used in new FTS configuration
* @param string $template The existing FTS configuration to be used as template for the new one
* @param string $comment If omitted, defaults to nothing
*
* @return bool|int 0 success
*
* @internal param string $locale Locale of the FTS configuration
* @internal param string $withmap Should we copy whole map of existing FTS configuration to the new one
* @internal param string $makeDefault Should this configuration be the default for locale given
*/
public function createFtsConfiguration($cfgname, $parser = '', $template = '', $comment = '')
{
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($cfgname);
$sql = "CREATE TEXT SEARCH CONFIGURATION \"{$f_schema}\".\"{$cfgname}\" (";
if ($parser != '') {
$this->fieldClean($parser['schema']);
$this->fieldClean($parser['parser']);
$parser = "\"{$parser['schema']}\".\"{$parser['parser']}\"";
$sql .= " PARSER = {$parser}";
}
if ($template != '') {
$this->fieldClean($template['schema']);
$this->fieldClean($template['name']);
$sql .= " COPY = \"{$template['schema']}\".\"{$template['name']}\"";
}
$sql .= ')';
if ($comment != '') {
$status = $this->beginTransaction();
if ($status != 0) {
return -1;
}
}
// Create the FTS configuration
$status = $this->execute($sql);
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
// Set the comment
if ($comment != '') {
$status = $this->setComment('TEXT SEARCH CONFIGURATION', $cfgname, '', $comment);
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
return $this->endTransaction();
}
return 0;
}
// Language functions
/**
* Returns available FTS configurations.
*
* @param bool $all if false, returns schema qualified FTS confs
*
* @return \PHPPgAdmin\ADORecordSet A recordset
*/
public function getFtsConfigurations($all = true)
{
$c_schema = $this->_schema;
$this->clean($c_schema);
$sql = "
SELECT
n.nspname as schema,
c.cfgname as name,
pg_catalog.obj_description(c.oid, 'pg_ts_config') as comment
FROM
pg_catalog.pg_ts_config c
JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace
WHERE
pg_catalog.pg_ts_config_is_visible(c.oid)";
if (!$all) {
$sql .= " AND n.nspname='{$c_schema}'\n";
}
$sql .= 'ORDER BY name';
return $this->selectSet($sql);
}
// Aggregate functions
/**
* Returns the map of FTS configuration given
* (list of mappings (tokens) and their processing dictionaries).
*
* @param string $ftscfg Name of the FTS configuration
*
* @return \PHPPgAdmin\ADORecordSet recordset
*/
public function getFtsConfigurationMap($ftscfg)
{
$c_schema = $this->_schema;
$this->clean($c_schema);
$this->fieldClean($ftscfg);
$oidSet = $this->selectSet("SELECT c.oid
FROM pg_catalog.pg_ts_config AS c
LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.cfgnamespace)
WHERE c.cfgname = '{$ftscfg}'
AND n.nspname='{$c_schema}'");
$oid = $oidSet->fields['oid'];
$sql = "
SELECT
(SELECT t.alias FROM pg_catalog.ts_token_type(c.cfgparser) AS t WHERE t.tokid = m.maptokentype) AS name,
(SELECT t.description FROM pg_catalog.ts_token_type(c.cfgparser) AS t WHERE t.tokid = m.maptokentype) AS description,
c.cfgname AS cfgname, n.nspname ||'.'|| d.dictname as dictionaries
FROM
pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m, pg_catalog.pg_ts_dict d,
pg_catalog.pg_namespace n
WHERE
c.oid = {$oid}
AND m.mapcfg = c.oid
AND m.mapdict = d.oid
AND d.dictnamespace = n.oid
ORDER BY name
";
return $this->selectSet($sql);
}
/**
* Returns FTS parsers available.
*
* @param bool $all if false, return only Parsers from the current schema
*
* @return \PHPPgAdmin\ADORecordSet A recordset
*/
public function getFtsParsers($all = true)
{
$c_schema = $this->_schema;
$this->clean($c_schema);
$sql = "
SELECT
n.nspname as schema,
p.prsname as name,
pg_catalog.obj_description(p.oid, 'pg_ts_parser') as comment
FROM pg_catalog.pg_ts_parser p
LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = p.prsnamespace)
WHERE pg_catalog.pg_ts_parser_is_visible(p.oid)";
if (!$all) {
$sql .= " AND n.nspname='{$c_schema}'\n";
}
$sql .= 'ORDER BY name';
return $this->selectSet($sql);
}
/**
* Returns FTS dictionaries available.
*
* @param bool $all if false, return only Dics from the current schema
*
* @return \PHPPgAdmin\ADORecordSet A recordset
*/
public function getFtsDictionaries($all = true)
{
$c_schema = $this->_schema;
$this->clean($c_schema);
$sql = "
SELECT
n.nspname as schema, d.dictname as name,
pg_catalog.obj_description(d.oid, 'pg_ts_dict') as comment
FROM pg_catalog.pg_ts_dict d
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace
WHERE pg_catalog.pg_ts_dict_is_visible(d.oid)";
if (!$all) {
$sql .= " AND n.nspname='{$c_schema}'\n";
}
$sql .= 'ORDER BY name;';
return $this->selectSet($sql);
}
/**
* Returns all FTS dictionary templates available.
*
* @return \PHPPgAdmin\ADORecordSet all FTS dictionary templates available
*/
public function getFtsDictionaryTemplates()
{
$sql = "
SELECT
n.nspname as schema,
t.tmplname as name,
( SELECT COALESCE(np.nspname, '(null)')::pg_catalog.text || '.' || p.proname
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.pronamespace
WHERE t.tmplinit = p.oid ) AS init,
( SELECT COALESCE(np.nspname, '(null)')::pg_catalog.text || '.' || p.proname
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace np ON np.oid = p.pronamespace
WHERE t.tmpllexize = p.oid ) AS lexize,
pg_catalog.obj_description(t.oid, 'pg_ts_template') as comment
FROM pg_catalog.pg_ts_template t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.tmplnamespace
WHERE pg_catalog.pg_ts_template_is_visible(t.oid)
ORDER BY name;";
return $this->selectSet($sql);
}
/**
* Drops FTS coniguration.
*
* @param string $ftscfg The configuration's name
* @param bool $cascade true to Cascade to dependenced objects
*
* @return int 0 if operation was successful
*/
public function dropFtsConfiguration($ftscfg, $cascade)
{
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($ftscfg);
$sql = "DROP TEXT SEARCH CONFIGURATION \"{$f_schema}\".\"{$ftscfg}\"";
if ($cascade) {
$sql .= ' CASCADE';
}
return $this->execute($sql);
}
/**
* Drops FTS dictionary.
*
* @param string $ftsdict The dico's name
* @param bool $cascade Cascade to dependenced objects
*
* @return int 0 if operation was successful
*
* @todo Support of dictionary templates dropping
*/
public function dropFtsDictionary($ftsdict, $cascade)
{
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($ftsdict);
$sql = 'DROP TEXT SEARCH DICTIONARY';
$sql .= " \"{$f_schema}\".\"{$ftsdict}\"";
if ($cascade) {
$sql .= ' CASCADE';
}
return $this->execute($sql);
}
/**
* Alters FTS configuration.
*
* @param string $cfgname The conf's name
* @param string $comment A comment on for the conf
* @param string $name The new conf name
*
* @return bool|int 0 on success
*/
public function updateFtsConfiguration($cfgname, $comment, $name)
{
$status = $this->beginTransaction();
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
$this->fieldClean($cfgname);
$status = $this->setComment('TEXT SEARCH CONFIGURATION', $cfgname, '', $comment);
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
// Only if the name has changed
if ($name != $cfgname) {
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($name);
$sql = "ALTER TEXT SEARCH CONFIGURATION \"{$f_schema}\".\"{$cfgname}\" RENAME TO \"{$name}\"";
$status = $this->execute($sql);
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
}
return $this->endTransaction();
}
/**
* Creates a new FTS dictionary or FTS dictionary template.
*
* @param string $dictname The name of the FTS dictionary to create
* @param bool $isTemplate Flag whether we create usual dictionary or dictionary template
* @param string $template The existing FTS dictionary to be used as template for the new one
* @param string $lexize The name of the function, which does transformation of input word
* @param string $init The name of the function, which initializes dictionary
* @param string $option Usually, it stores various options required for the dictionary
* @param string $comment If omitted, defaults to nothing
*
* @return bool|int 0 success
*/
public function createFtsDictionary(
$dictname,
$isTemplate = false,
$template = '',
$lexize = '',
$init = '',
$option = '',
$comment = ''
) {
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($dictname);
$this->fieldClean($template);
$this->fieldClean($lexize);
$this->fieldClean($init);
$this->fieldClean($option);
$sql = 'CREATE TEXT SEARCH';
if ($isTemplate) {
$sql .= " TEMPLATE \"{$f_schema}\".\"{$dictname}\" (";
if ($lexize != '') {
$sql .= " LEXIZE = {$lexize}";
}
if ($init != '') {
$sql .= ", INIT = {$init}";
}
$sql .= ')';
$whatToComment = 'TEXT SEARCH TEMPLATE';
} else {
$sql .= " DICTIONARY \"{$f_schema}\".\"{$dictname}\" (";
if ($template != '') {
$this->fieldClean($template['schema']);
$this->fieldClean($template['name']);
$template = "\"{$template['schema']}\".\"{$template['name']}\"";
$sql .= " TEMPLATE = {$template}";
}
if ($option != '') {
$sql .= ", {$option}";
}
$sql .= ')';
$whatToComment = 'TEXT SEARCH DICTIONARY';
}
/* if comment, begin a transaction to
* run both commands */
if ($comment != '') {
$status = $this->beginTransaction();
if ($status != 0) {
return -1;
}
}
// Create the FTS dictionary
$status = $this->execute($sql);
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
// Set the comment
if ($comment != '') {
$status = $this->setComment($whatToComment, $dictname, '', $comment);
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
}
return $this->endTransaction();
}
// Role, User/Group functions
/**
* Alters FTS dictionary or dictionary template.
*
* @param string $dictname The dico's name
* @param string $comment The comment
* @param string $name The new dico's name
*
* @return bool|int 0 on success
*/
public function updateFtsDictionary($dictname, $comment, $name)
{
$status = $this->beginTransaction();
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
$this->fieldClean($dictname);
$status = $this->setComment('TEXT SEARCH DICTIONARY', $dictname, '', $comment);
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
// Only if the name has changed
if ($name != $dictname) {
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($name);
$sql = "ALTER TEXT SEARCH DICTIONARY \"{$f_schema}\".\"{$dictname}\" RENAME TO \"{$name}\"";
$status = $this->execute($sql);
if ($status != 0) {
$this->rollbackTransaction();
return -1;
}
}
return $this->endTransaction();
}
/**
* Return all information relating to a FTS dictionary.
*
* @param string $ftsdict The name of the FTS dictionary
*
* @return \PHPPgAdmin\ADORecordSet recordset of FTS dictionary information
*/
public function getFtsDictionaryByName($ftsdict)
{
$c_schema = $this->_schema;
$this->clean($c_schema);
$this->clean($ftsdict);
$sql = "SELECT
n.nspname as schema,
d.dictname as name,
( SELECT COALESCE(nt.nspname, '(null)')::pg_catalog.text || '.' || t.tmplname FROM
pg_catalog.pg_ts_template t
LEFT JOIN pg_catalog.pg_namespace nt ON nt.oid = t.tmplnamespace
WHERE d.dicttemplate = t.oid ) AS template,
d.dictinitoption as init,
pg_catalog.obj_description(d.oid, 'pg_ts_dict') as comment
FROM pg_catalog.pg_ts_dict d
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = d.dictnamespace
WHERE d.dictname = '{$ftsdict}'
AND pg_catalog.pg_ts_dict_is_visible(d.oid)
AND n.nspname='{$c_schema}'
ORDER BY name";
return $this->selectSet($sql);
}
/**
* Creates/updates/deletes FTS mapping.
*
* @param string $ftscfg The name of the FTS dictionary
* @param array $mapping Array of tokens' names
* @param string $action What to do with the mapping: add, alter or drop
* @param string $dictname Dictionary that will process tokens given or null in case of drop action
*
* @return int 0 if operation was successful
*
* @internal param string $cfgname The name of the FTS configuration to alter
*/
public function changeFtsMapping($ftscfg, $mapping, $action, $dictname = null)
{
if (count($mapping) > 0) {
$f_schema = $this->_schema;
$this->fieldClean($f_schema);
$this->fieldClean($ftscfg);
$this->fieldClean($dictname);
$this->arrayClean($mapping);
switch ($action) {
case 'alter':
$whatToDo = 'ALTER';
break;
case 'drop':
$whatToDo = 'DROP';
break;
default:
$whatToDo = 'ADD';
break;
}
$sql = "ALTER TEXT SEARCH CONFIGURATION \"{$f_schema}\".\"{$ftscfg}\" {$whatToDo} MAPPING FOR ";
$sql .= implode(',', $mapping);
if ($action != 'drop' && !empty($dictname)) {
$sql .= " WITH {$dictname}";
}
return $this->execute($sql);
}
return -1;
}
/**
* Return all information related to a given FTS configuration's mapping.
*
* @param string $ftscfg The name of the FTS configuration
* @param string $mapping The name of the mapping
*
* @return \PHPPgAdmin\ADORecordSet FTS configuration information
*/
public function getFtsMappingByName($ftscfg, $mapping)
{
$c_schema = $this->_schema;
$this->clean($c_schema);
$this->clean($ftscfg);
$this->clean($mapping);
$oidSet = $this->selectSet("SELECT c.oid, cfgparser
FROM pg_catalog.pg_ts_config AS c
LEFT JOIN pg_catalog.pg_namespace AS n ON n.oid = c.cfgnamespace
WHERE c.cfgname = '{$ftscfg}'
AND n.nspname='{$c_schema}'");
$oid = $oidSet->fields['oid'];
$cfgparser = $oidSet->fields['cfgparser'];
$tokenIdSet = $this->selectSet("SELECT tokid
FROM pg_catalog.ts_token_type({$cfgparser})
WHERE alias = '{$mapping}'");
$tokid = $tokenIdSet->fields['tokid'];
$sql = "SELECT
(SELECT t.alias FROM pg_catalog.ts_token_type(c.cfgparser) AS t WHERE t.tokid = m.maptokentype) AS name,
d.dictname as dictionaries
FROM pg_catalog.pg_ts_config AS c, pg_catalog.pg_ts_config_map AS m, pg_catalog.pg_ts_dict d
WHERE c.oid = {$oid} AND m.mapcfg = c.oid AND m.maptokentype = {$tokid} AND m.mapdict = d.oid
LIMIT 1;";
return $this->selectSet($sql);
}
/**
* Return list of FTS mappings possible for given parser
* (specified by given configuration since configuration can only have 1 parser).
*
* @param string $ftscfg The config's name that use the parser
*
* @return int 0 if operation was successful
*/
public function getFtsMappings($ftscfg)
{
$cfg = $this->getFtsConfigurationByName($ftscfg);
$sql = "SELECT alias AS name, description
FROM pg_catalog.ts_token_type({$cfg->fields['parser_id']})
ORDER BY name";
return $this->selectSet($sql);
}
/**
* Return all information related to a FTS configuration.
*
* @param string $ftscfg The name of the FTS configuration
*
* @return \PHPPgAdmin\ADORecordSet FTS configuration information
*/
public function getFtsConfigurationByName($ftscfg)
{
$c_schema = $this->_schema;
$this->clean($c_schema);
$this->clean($ftscfg);
$sql = "
SELECT
n.nspname as schema,
c.cfgname as name,
p.prsname as parser,
c.cfgparser as parser_id,
pg_catalog.obj_description(c.oid, 'pg_ts_config') as comment
FROM pg_catalog.pg_ts_config c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.cfgnamespace
LEFT JOIN pg_catalog.pg_ts_parser p ON p.oid = c.cfgparser
WHERE pg_catalog.pg_ts_config_is_visible(c.oid)
AND c.cfgname = '{$ftscfg}'
AND n.nspname='{$c_schema}'";
return $this->selectSet($sql);
}
abstract public function fieldClean(&$str);
abstract public function beginTransaction();
abstract public function rollbackTransaction();
abstract public function endTransaction();
abstract public function execute($sql);
abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null);
abstract public function selectSet($sql);
abstract public function clean(&$str);
abstract public function arrayClean(&$arr);
}
|