| Total Complexity | 132 |
| Total Lines | 1309 |
| Duplicated Lines | 13.67 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FulltextController 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 FulltextController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class FulltextController extends BaseController |
||
| 11 | { |
||
| 12 | public $_name = 'FulltextController'; |
||
| 13 | |||
| 14 | public function render() |
||
| 127 | } |
||
| 128 | |||
| 129 | public function doDefault($msg = '') |
||
| 130 | { |
||
| 131 | $conf = $this->conf; |
||
| 132 | $misc = $this->misc; |
||
| 133 | $lang = $this->lang; |
||
| 134 | $data = $misc->getDatabaseAccessor(); |
||
| 135 | |||
| 136 | $this->printTrail('schema'); |
||
| 137 | $this->printTabs('schema', 'fulltext'); |
||
| 138 | $this->printTabs('fulltext', 'ftsconfigs'); |
||
| 139 | $this->printMsg($msg); |
||
| 140 | |||
| 141 | $cfgs = $data->getFtsConfigurations(false); |
||
| 142 | |||
| 143 | $columns = [ |
||
| 144 | 'configuration' => [ |
||
| 145 | 'title' => $lang['strftsconfig'], |
||
| 146 | 'field' => Decorator::field('name'), |
||
| 147 | 'url' => "fulltext.php?action=viewconfig&{$misc->href}&", |
||
| 148 | 'vars' => ['ftscfg' => 'name'], |
||
| 149 | ], |
||
| 150 | 'schema' => [ |
||
| 151 | 'title' => $lang['strschema'], |
||
| 152 | 'field' => Decorator::field('schema'), |
||
| 153 | ], |
||
| 154 | 'actions' => [ |
||
| 155 | 'title' => $lang['stractions'], |
||
| 156 | ], |
||
| 157 | 'comment' => [ |
||
| 158 | 'title' => $lang['strcomment'], |
||
| 159 | 'field' => Decorator::field('comment'), |
||
| 160 | ], |
||
| 161 | ]; |
||
| 162 | |||
| 163 | $actions = [ |
||
| 164 | 'drop' => [ |
||
| 165 | 'content' => $lang['strdrop'], |
||
| 166 | 'attr' => [ |
||
| 167 | 'href' => [ |
||
| 168 | 'url' => 'fulltext.php', |
||
| 169 | 'urlvars' => [ |
||
| 170 | 'action' => 'dropconfig', |
||
| 171 | 'ftscfg' => Decorator::field('name'), |
||
| 172 | ], |
||
| 173 | ], |
||
| 174 | ], |
||
| 175 | ], |
||
| 176 | 'alter' => [ |
||
| 177 | 'content' => $lang['stralter'], |
||
| 178 | 'attr' => [ |
||
| 179 | 'href' => [ |
||
| 180 | 'url' => 'fulltext.php', |
||
| 181 | 'urlvars' => [ |
||
| 182 | 'action' => 'alterconfig', |
||
| 183 | 'ftscfg' => Decorator::field('name'), |
||
| 184 | ], |
||
| 185 | ], |
||
| 186 | ], |
||
| 187 | ], |
||
| 188 | ]; |
||
| 189 | |||
| 190 | echo $this->printTable($cfgs, $columns, $actions, 'fulltext-fulltext', $lang['strftsnoconfigs']); |
||
| 191 | |||
| 192 | $navlinks = [ |
||
| 193 | 'createconf' => [ |
||
| 194 | 'attr' => [ |
||
| 195 | 'href' => [ |
||
| 196 | 'url' => 'fulltext.php', |
||
| 197 | 'urlvars' => [ |
||
| 198 | 'action' => 'createconfig', |
||
| 199 | 'server' => $_REQUEST['server'], |
||
| 200 | 'database' => $_REQUEST['database'], |
||
| 201 | 'schema' => $_REQUEST['schema'], |
||
| 202 | ], |
||
| 203 | ], |
||
| 204 | ], |
||
| 205 | 'content' => $lang['strftscreateconfig'], |
||
| 206 | ], |
||
| 207 | ]; |
||
| 208 | |||
| 209 | $this->printNavLinks($navlinks, 'fulltext-fulltext', get_defined_vars()); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Generate XML for the browser tree. |
||
| 214 | */ |
||
| 215 | View Code Duplication | public function doTree() |
|
| 216 | { |
||
| 217 | $conf = $this->conf; |
||
| 218 | $misc = $this->misc; |
||
| 219 | $lang = $this->lang; |
||
| 220 | $data = $misc->getDatabaseAccessor(); |
||
| 221 | |||
| 222 | $tabs = $misc->getNavTabs('fulltext'); |
||
| 223 | $items = $this->adjustTabsForTree($tabs); |
||
| 224 | |||
| 225 | $reqvars = $misc->getRequestVars('ftscfg'); |
||
| 226 | |||
| 227 | $attrs = [ |
||
| 228 | 'text' => Decorator::field('title'), |
||
| 229 | 'icon' => Decorator::field('icon'), |
||
| 230 | 'action' => Decorator::actionurl('fulltext.php', |
||
| 231 | $reqvars, |
||
| 232 | field('urlvars') |
||
| 233 | ), |
||
| 234 | 'branch' => Decorator::url('fulltext.php', |
||
| 235 | $reqvars, |
||
| 236 | [ |
||
| 237 | 'action' => 'subtree', |
||
| 238 | 'what' => Decorator::field('icon'), // IZ: yeah, it's ugly, but I do not want to change navigation tabs arrays |
||
| 239 | ] |
||
| 240 | ), |
||
| 241 | ]; |
||
| 242 | |||
| 243 | return $this->printTree($items, $attrs, 'fts'); |
||
| 244 | } |
||
| 245 | |||
| 246 | public function doSubTree($what) |
||
| 247 | { |
||
| 248 | $conf = $this->conf; |
||
| 249 | $misc = $this->misc; |
||
| 250 | $lang = $this->lang; |
||
| 251 | $data = $misc->getDatabaseAccessor(); |
||
| 252 | |||
| 253 | switch ($what) { |
||
| 254 | case 'FtsCfg': |
||
| 255 | $items = $data->getFtsConfigurations(false); |
||
| 256 | $urlvars = ['action' => 'viewconfig', 'ftscfg' => Decorator::field('name')]; |
||
| 257 | break; |
||
| 258 | case 'FtsDict': |
||
| 259 | $items = $data->getFtsDictionaries(false); |
||
| 260 | $urlvars = ['action' => 'viewdicts']; |
||
| 261 | break; |
||
| 262 | case 'FtsParser': |
||
| 263 | $items = $data->getFtsParsers(false); |
||
| 264 | $urlvars = ['action' => 'viewparsers']; |
||
| 265 | break; |
||
| 266 | default: |
||
| 267 | return; |
||
| 268 | } |
||
| 269 | |||
| 270 | $reqvars = $misc->getRequestVars('ftscfg'); |
||
| 271 | |||
| 272 | $attrs = [ |
||
| 273 | 'text' => Decorator::field('name'), |
||
| 274 | 'icon' => $what, |
||
| 275 | 'toolTip' => Decorator::field('comment'), |
||
| 276 | 'action' => Decorator::actionurl('fulltext.php', |
||
| 277 | $reqvars, |
||
| 278 | $urlvars |
||
| 279 | ), |
||
| 280 | 'branch' => Decorator::ifempty(Decorator::field('branch'), |
||
| 281 | '', |
||
| 282 | url('fulltext.php', |
||
| 283 | $reqvars, |
||
| 284 | [ |
||
| 285 | 'action' => 'subtree', |
||
| 286 | 'ftscfg' => Decorator::field('name'), |
||
| 287 | ] |
||
| 288 | ) |
||
| 289 | ), |
||
| 290 | ]; |
||
| 291 | |||
| 292 | return $this->printTree($items, $attrs, strtolower($what)); |
||
| 293 | } |
||
| 294 | |||
| 295 | View Code Duplication | public function doDropConfig($confirm) |
|
| 296 | { |
||
| 297 | $conf = $this->conf; |
||
| 298 | $misc = $this->misc; |
||
| 299 | $lang = $this->lang; |
||
| 300 | $data = $misc->getDatabaseAccessor(); |
||
| 301 | |||
| 302 | if ($confirm) { |
||
| 303 | $this->printTrail('ftscfg'); |
||
| 304 | $this->printTitle($lang['strdrop'], 'pg.ftscfg.drop'); |
||
| 305 | |||
| 306 | echo '<p>', sprintf($lang['strconfdropftsconfig'], $misc->printVal($_REQUEST['ftscfg'])), "</p>\n"; |
||
| 307 | |||
| 308 | echo '<form action="' . SUBFOLDER . "/src/views/fulltext.php\" method=\"post\">\n"; |
||
| 309 | echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n"; |
||
| 310 | echo "<p><input type=\"hidden\" name=\"action\" value=\"dropconfig\" />\n"; |
||
| 311 | echo '<input type="hidden" name="database" value="', htmlspecialchars($_REQUEST['database']), "\" />\n"; |
||
| 312 | echo '<input type="hidden" name="ftscfg" value="', htmlspecialchars($_REQUEST['ftscfg']), "\" />\n"; |
||
| 313 | echo $misc->form; |
||
| 314 | echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n"; |
||
| 315 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 316 | echo "</form>\n"; |
||
| 317 | } else { |
||
| 318 | $status = $data->dropFtsConfiguration($_POST['ftscfg'], isset($_POST['cascade'])); |
||
| 319 | if ($status == 0) { |
||
| 320 | $this->misc->setReloadBrowser(true); |
||
| 321 | $this->doDefault($lang['strftsconfigdropped']); |
||
| 322 | } else { |
||
| 323 | $this->doDefault($lang['strftsconfigdroppedbad']); |
||
| 324 | } |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | public function doDropDict($confirm) |
||
| 329 | { |
||
| 330 | $conf = $this->conf; |
||
| 331 | $misc = $this->misc; |
||
| 332 | $lang = $this->lang; |
||
| 333 | $data = $misc->getDatabaseAccessor(); |
||
| 334 | |||
| 335 | if ($confirm) { |
||
| 336 | $this->printTrail('ftscfg'); // TODO: change to smth related to dictionary |
||
| 337 | $this->printTitle($lang['strdrop'], 'pg.ftsdict.drop'); |
||
| 338 | |||
| 339 | echo '<p>', sprintf($lang['strconfdropftsdict'], $misc->printVal($_REQUEST['ftsdict'])), "</p>\n"; |
||
| 340 | |||
| 341 | echo '<form action="' . SUBFOLDER . "/src/views/fulltext.php\" method=\"post\">\n"; |
||
| 342 | echo "<p><input type=\"checkbox\" id=\"cascade\" name=\"cascade\" /> <label for=\"cascade\">{$lang['strcascade']}</label></p>\n"; |
||
| 343 | echo "<p><input type=\"hidden\" name=\"action\" value=\"dropdict\" />\n"; |
||
| 344 | echo '<input type="hidden" name="database" value="', htmlspecialchars($_REQUEST['database']), "\" />\n"; |
||
| 345 | echo '<input type="hidden" name="ftsdict" value="', htmlspecialchars($_REQUEST['ftsdict']), "\" />\n"; |
||
| 346 | //echo "<input type=\"hidden\" name=\"ftscfg\" value=\"", htmlspecialchars($_REQUEST['ftscfg']), "\" />\n"; |
||
| 347 | echo "<input type=\"hidden\" name=\"prev_action\" value=\"viewdicts\" /></p>\n"; |
||
| 348 | echo $misc->form; |
||
| 349 | echo "<input type=\"submit\" name=\"drop\" value=\"{$lang['strdrop']}\" />\n"; |
||
| 350 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 351 | echo "</form>\n"; |
||
| 352 | } else { |
||
| 353 | $status = $data->dropFtsDictionary($_POST['ftsdict'], isset($_POST['cascade'])); |
||
| 354 | if ($status == 0) { |
||
| 355 | $this->misc->setReloadBrowser(true); |
||
| 356 | $this->doViewDicts($lang['strftsdictdropped']); |
||
| 357 | } else { |
||
| 358 | $this->doViewDicts($lang['strftsdictdroppedbad']); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Displays a screen where one can enter a new FTS configuration |
||
| 365 | */ |
||
| 366 | public function doCreateConfig($msg = '') |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Actually creates the new FTS configuration in the database |
||
| 473 | */ |
||
| 474 | public function doSaveCreateConfig() |
||
| 475 | { |
||
| 476 | $conf = $this->conf; |
||
| 477 | $misc = $this->misc; |
||
| 478 | $lang = $this->lang; |
||
| 479 | $data = $misc->getDatabaseAccessor(); |
||
| 480 | |||
| 481 | $err = ''; |
||
| 482 | // Check that they've given a name |
||
| 483 | if ($_POST['formName'] == '') { |
||
| 484 | $err .= "{$lang['strftsconfigneedsname']}<br />"; |
||
| 485 | } |
||
| 486 | |||
| 487 | if (($_POST['formParser'] != '') && ($_POST['formTemplate'] != '')) { |
||
| 488 | $err .= "{$lang['strftscantparsercopy']}<br />"; |
||
| 489 | } |
||
| 490 | |||
| 491 | if ($err != '') { |
||
| 492 | return doCreateConfig($err); |
||
| 493 | } |
||
| 494 | |||
| 495 | if ($_POST['formParser'] != '') { |
||
| 496 | $formParser = unserialize($_POST['formParser']); |
||
| 497 | } else { |
||
| 498 | $formParser = ''; |
||
| 499 | } |
||
| 500 | |||
| 501 | View Code Duplication | if ($_POST['formTemplate'] != '') { |
|
| 502 | $formTemplate = unserialize($_POST['formTemplate']); |
||
| 503 | } else { |
||
| 504 | $formTemplate = ''; |
||
| 505 | } |
||
| 506 | |||
| 507 | $status = $data->createFtsConfiguration($_POST['formName'], $formParser, $formTemplate, $_POST['formComment']); |
||
| 508 | if ($status == 0) { |
||
| 509 | $this->misc->setReloadBrowser(true); |
||
| 510 | $this->doDefault($lang['strftsconfigcreated']); |
||
| 511 | } else { |
||
| 512 | $this->doCreateConfig($lang['strftsconfigcreatedbad']); |
||
| 513 | } |
||
| 514 | } |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Display a form to permit editing FTS configuration properies. |
||
| 518 | */ |
||
| 519 | public function doAlterConfig($msg = '') |
||
| 520 | { |
||
| 521 | $conf = $this->conf; |
||
| 522 | $misc = $this->misc; |
||
| 523 | $lang = $this->lang; |
||
| 524 | $data = $misc->getDatabaseAccessor(); |
||
| 525 | |||
| 526 | $this->printTrail('ftscfg'); |
||
| 527 | $this->printTitle($lang['stralter'], 'pg.ftscfg.alter'); |
||
| 528 | $this->printMsg($msg); |
||
| 529 | |||
| 530 | $ftscfg = $data->getFtsConfigurationByName($_REQUEST['ftscfg']); |
||
| 531 | if ($ftscfg->recordCount() > 0) { |
||
| 532 | if (!isset($_POST['formComment'])) { |
||
| 533 | $_POST['formComment'] = $ftscfg->fields['comment']; |
||
| 534 | } |
||
| 535 | |||
| 536 | if (!isset($_POST['ftscfg'])) { |
||
| 537 | $_POST['ftscfg'] = $_REQUEST['ftscfg']; |
||
| 538 | } |
||
| 539 | |||
| 540 | if (!isset($_POST['formName'])) { |
||
| 541 | $_POST['formName'] = $_REQUEST['ftscfg']; |
||
| 542 | } |
||
| 543 | |||
| 544 | if (!isset($_POST['formParser'])) { |
||
| 545 | $_POST['formParser'] = ''; |
||
| 546 | } |
||
| 547 | |||
| 548 | // Fetch all FTS parsers from the database |
||
| 549 | $ftsparsers = $data->getFtsParsers(); |
||
|
1 ignored issue
–
show
|
|||
| 550 | |||
| 551 | echo '<form action="' . SUBFOLDER . "/src/views/fulltext.php\" method=\"post\">\n"; |
||
| 552 | echo "<table>\n"; |
||
| 553 | |||
| 554 | echo "\t<tr>\n"; |
||
| 555 | echo "\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
| 556 | echo "\t\t<td class=\"data1\">"; |
||
| 557 | echo "\t\t\t<input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 558 | htmlspecialchars($_POST['formName']), "\" />\n"; |
||
| 559 | echo "\t\t</td>\n"; |
||
| 560 | echo "\t</tr>\n"; |
||
| 561 | |||
| 562 | // Comment |
||
| 563 | echo "\t<tr>\n"; |
||
| 564 | echo "\t\t<th class=\"data\">{$lang['strcomment']}</th>\n"; |
||
| 565 | echo "\t\t<td class=\"data1\"><textarea cols=\"32\" rows=\"3\"name=\"formComment\">", htmlspecialchars($_POST['formComment']), "</textarea></td>\n"; |
||
| 566 | echo "\t</tr>\n"; |
||
| 567 | echo "</table>\n"; |
||
| 568 | echo "<p><input type=\"hidden\" name=\"action\" value=\"alterconfig\" />\n"; |
||
| 569 | echo '<input type="hidden" name="ftscfg" value="', htmlspecialchars($_POST['ftscfg']), "\" />\n"; |
||
| 570 | echo $misc->form; |
||
| 571 | echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n"; |
||
| 572 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 573 | echo "</form>\n"; |
||
| 574 | } else { |
||
| 575 | echo "<p>{$lang['strnodata']}</p>\n"; |
||
| 576 | } |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Save the form submission containing changes to a FTS configuration |
||
| 581 | */ |
||
| 582 | public function doSaveAlterConfig() |
||
| 583 | { |
||
| 584 | $conf = $this->conf; |
||
| 585 | $misc = $this->misc; |
||
| 586 | $lang = $this->lang; |
||
| 587 | $data = $misc->getDatabaseAccessor(); |
||
| 588 | $status = $data->updateFtsConfiguration($_POST['ftscfg'], $_POST['formComment'], $_POST['formName']); |
||
| 589 | if ($status == 0) { |
||
| 590 | $this->doDefault($lang['strftsconfigaltered']); |
||
| 591 | } else { |
||
| 592 | $this->doAlterConfig($lang['strftsconfigalteredbad']); |
||
| 593 | } |
||
| 594 | } |
||
| 595 | |||
| 596 | /** |
||
| 597 | * View list of FTS parsers |
||
| 598 | */ |
||
| 599 | View Code Duplication | public function doViewParsers($msg = '') |
|
| 600 | { |
||
| 601 | $conf = $this->conf; |
||
| 602 | $misc = $this->misc; |
||
| 603 | $lang = $this->lang; |
||
| 604 | $data = $misc->getDatabaseAccessor(); |
||
| 605 | |||
| 606 | $this->printTrail('schema'); |
||
| 607 | $this->printTabs('schema', 'fulltext'); |
||
| 608 | $this->printTabs('fulltext', 'ftsparsers'); |
||
| 609 | $this->printMsg($msg); |
||
| 610 | |||
| 611 | $parsers = $data->getFtsParsers(false); |
||
| 612 | |||
| 613 | $columns = [ |
||
| 614 | 'schema' => [ |
||
| 615 | 'title' => $lang['strschema'], |
||
| 616 | 'field' => Decorator::field('schema'), |
||
| 617 | ], |
||
| 618 | 'name' => [ |
||
| 619 | 'title' => $lang['strname'], |
||
| 620 | 'field' => Decorator::field('name'), |
||
| 621 | ], |
||
| 622 | 'comment' => [ |
||
| 623 | 'title' => $lang['strcomment'], |
||
| 624 | 'field' => Decorator::field('comment'), |
||
| 625 | ], |
||
| 626 | ]; |
||
| 627 | |||
| 628 | $actions = []; |
||
| 629 | |||
| 630 | echo $this->printTable($parsers, $columns, $actions, 'fulltext-viewparsers', $lang['strftsnoparsers']); |
||
| 631 | |||
| 632 | //TODO: navlink to "create parser" |
||
| 633 | } |
||
| 634 | |||
| 635 | /** |
||
| 636 | * View list of FTS dictionaries |
||
| 637 | */ |
||
| 638 | public function doViewDicts($msg = '') |
||
| 639 | { |
||
| 640 | $conf = $this->conf; |
||
| 641 | $misc = $this->misc; |
||
| 642 | $lang = $this->lang; |
||
| 643 | $data = $misc->getDatabaseAccessor(); |
||
| 644 | |||
| 645 | $this->printTrail('schema'); |
||
| 646 | $this->printTabs('schema', 'fulltext'); |
||
| 647 | $this->printTabs('fulltext', 'ftsdicts'); |
||
| 648 | $this->printMsg($msg); |
||
| 649 | |||
| 650 | $dicts = $data->getFtsDictionaries(false); |
||
| 651 | |||
| 652 | $columns = [ |
||
| 653 | 'schema' => [ |
||
| 654 | 'title' => $lang['strschema'], |
||
| 655 | 'field' => Decorator::field('schema'), |
||
| 656 | ], |
||
| 657 | 'name' => [ |
||
| 658 | 'title' => $lang['strname'], |
||
| 659 | 'field' => Decorator::field('name'), |
||
| 660 | ], |
||
| 661 | 'actions' => [ |
||
| 662 | 'title' => $lang['stractions'], |
||
| 663 | ], |
||
| 664 | 'comment' => [ |
||
| 665 | 'title' => $lang['strcomment'], |
||
| 666 | 'field' => Decorator::field('comment'), |
||
| 667 | ], |
||
| 668 | ]; |
||
| 669 | |||
| 670 | $actions = [ |
||
| 671 | 'drop' => [ |
||
| 672 | 'content' => $lang['strdrop'], |
||
| 673 | 'attr' => [ |
||
| 674 | 'href' => [ |
||
| 675 | 'url' => 'fulltext.php', |
||
| 676 | 'urlvars' => [ |
||
| 677 | 'action' => 'dropdict', |
||
| 678 | 'ftsdict' => Decorator::field('name'), |
||
| 679 | ], |
||
| 680 | ], |
||
| 681 | ], |
||
| 682 | ], |
||
| 683 | 'alter' => [ |
||
| 684 | 'content' => $lang['stralter'], |
||
| 685 | 'attr' => [ |
||
| 686 | 'href' => [ |
||
| 687 | 'url' => 'fulltext.php', |
||
| 688 | 'urlvars' => [ |
||
| 689 | 'action' => 'alterdict', |
||
| 690 | 'ftsdict' => Decorator::field('name'), |
||
| 691 | ], |
||
| 692 | ], |
||
| 693 | ], |
||
| 694 | ], |
||
| 695 | ]; |
||
| 696 | |||
| 697 | echo $this->printTable($dicts, $columns, $actions, 'fulltext-viewdicts', $lang['strftsnodicts']); |
||
| 698 | |||
| 699 | $navlinks = [ |
||
| 700 | 'createdict' => [ |
||
| 701 | 'attr' => [ |
||
| 702 | 'href' => [ |
||
| 703 | 'url' => 'fulltext.php', |
||
| 704 | 'urlvars' => [ |
||
| 705 | 'action' => 'createdict', |
||
| 706 | 'server' => $_REQUEST['server'], |
||
| 707 | 'database' => $_REQUEST['database'], |
||
| 708 | 'schema' => $_REQUEST['schema'], |
||
| 709 | ], |
||
| 710 | ], |
||
| 711 | ], |
||
| 712 | 'content' => $lang['strftscreatedict'], |
||
| 713 | ], |
||
| 714 | ]; |
||
| 715 | |||
| 716 | $this->printNavLinks($navlinks, 'fulltext-viewdicts', get_defined_vars()); |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * View details of FTS configuration given |
||
| 721 | */ |
||
| 722 | public function doViewConfig($ftscfg, $msg = '') |
||
| 723 | { |
||
| 724 | $conf = $this->conf; |
||
| 725 | $misc = $this->misc; |
||
| 726 | $lang = $this->lang; |
||
| 727 | $data = $misc->getDatabaseAccessor(); |
||
| 728 | |||
| 729 | $this->printTrail('ftscfg'); |
||
| 730 | $this->printTabs('schema', 'fulltext'); |
||
| 731 | $this->printTabs('fulltext', 'ftsconfigs'); |
||
| 732 | $this->printMsg($msg); |
||
| 733 | |||
| 734 | echo "<h3>{$lang['strftsconfigmap']}</h3>\n"; |
||
| 735 | |||
| 736 | $map = $data->getFtsConfigurationMap($ftscfg); |
||
| 737 | |||
| 738 | $columns = [ |
||
| 739 | 'name' => [ |
||
| 740 | 'title' => $lang['strftsmapping'], |
||
| 741 | 'field' => Decorator::field('name'), |
||
| 742 | ], |
||
| 743 | 'dictionaries' => [ |
||
| 744 | 'title' => $lang['strftsdicts'], |
||
| 745 | 'field' => Decorator::field('dictionaries'), |
||
| 746 | ], |
||
| 747 | 'actions' => [ |
||
| 748 | 'title' => $lang['stractions'], |
||
| 749 | ], |
||
| 750 | 'comment' => [ |
||
| 751 | 'title' => $lang['strcomment'], |
||
| 752 | 'field' => Decorator::field('description'), |
||
| 753 | ], |
||
| 754 | ]; |
||
| 755 | |||
| 756 | $actions = [ |
||
| 757 | 'drop' => [ |
||
| 758 | 'multiaction' => 'dropmapping', |
||
| 759 | 'content' => $lang['strdrop'], |
||
| 760 | 'attr' => [ |
||
| 761 | 'href' => [ |
||
| 762 | 'url' => 'fulltext.php', |
||
| 763 | 'urlvars' => [ |
||
| 764 | 'action' => 'dropmapping', |
||
| 765 | 'mapping' => Decorator::field('name'), |
||
| 766 | 'ftscfg' => Decorator::field('cfgname'), |
||
| 767 | ], |
||
| 768 | ], |
||
| 769 | ], |
||
| 770 | ], |
||
| 771 | 'alter' => [ |
||
| 772 | 'content' => $lang['stralter'], |
||
| 773 | 'attr' => [ |
||
| 774 | 'href' => [ |
||
| 775 | 'url' => 'fulltext.php', |
||
| 776 | 'urlvars' => [ |
||
| 777 | 'action' => 'altermapping', |
||
| 778 | 'mapping' => Decorator::field('name'), |
||
| 779 | 'ftscfg' => Decorator::field('cfgname'), |
||
| 780 | ], |
||
| 781 | ], |
||
| 782 | ], |
||
| 783 | ], |
||
| 784 | 'multiactions' => [ |
||
| 785 | 'keycols' => ['mapping' => 'name'], |
||
| 786 | 'url' => 'fulltext.php', |
||
| 787 | 'default' => null, |
||
| 788 | 'vars' => ['ftscfg' => $ftscfg], |
||
| 789 | ], |
||
| 790 | |||
| 791 | ]; |
||
| 792 | |||
| 793 | echo $this->printTable($map, $columns, $actions, 'fulltext-viewconfig', $lang['strftsemptymap']); |
||
| 794 | |||
| 795 | $navlinks = [ |
||
| 796 | 'addmapping' => [ |
||
| 797 | 'attr' => [ |
||
| 798 | 'href' => [ |
||
| 799 | 'url' => 'fulltext.php', |
||
| 800 | 'urlvars' => [ |
||
| 801 | 'action' => 'addmapping', |
||
| 802 | 'server' => $_REQUEST['server'], |
||
| 803 | 'database' => $_REQUEST['database'], |
||
| 804 | 'schema' => $_REQUEST['schema'], |
||
| 805 | 'ftscfg' => $ftscfg, |
||
| 806 | ], |
||
| 807 | ], |
||
| 808 | ], |
||
| 809 | 'content' => $lang['strftsaddmapping'], |
||
| 810 | ], |
||
| 811 | ]; |
||
| 812 | |||
| 813 | $this->printNavLinks($navlinks, 'fulltext-viewconfig', get_defined_vars()); |
||
| 814 | } |
||
| 815 | |||
| 816 | /** |
||
| 817 | * Displays a screen where one can enter a details of a new FTS dictionary |
||
| 818 | */ |
||
| 819 | public function doCreateDict($msg = '') |
||
| 820 | { |
||
| 821 | $conf = $this->conf; |
||
| 822 | $misc = $this->misc; |
||
| 823 | $lang = $this->lang; |
||
| 824 | $data = $misc->getDatabaseAccessor(); |
||
| 825 | |||
| 826 | $server_info = $misc->getServerInfo(); |
||
| 827 | |||
| 828 | if (!isset($_POST['formName'])) { |
||
| 829 | $_POST['formName'] = ''; |
||
| 830 | } |
||
| 831 | |||
| 832 | if (!isset($_POST['formIsTemplate'])) { |
||
| 833 | $_POST['formIsTemplate'] = false; |
||
| 834 | } |
||
| 835 | |||
| 836 | if (!isset($_POST['formTemplate'])) { |
||
| 837 | $_POST['formTemplate'] = ''; |
||
| 838 | } |
||
| 839 | |||
| 840 | if (!isset($_POST['formLexize'])) { |
||
| 841 | $_POST['formLexize'] = ''; |
||
| 842 | } |
||
| 843 | |||
| 844 | if (!isset($_POST['formInit'])) { |
||
| 845 | $_POST['formInit'] = ''; |
||
| 846 | } |
||
| 847 | |||
| 848 | if (!isset($_POST['formOption'])) { |
||
| 849 | $_POST['formOption'] = ''; |
||
| 850 | } |
||
| 851 | |||
| 852 | if (!isset($_POST['formComment'])) { |
||
| 853 | $_POST['formComment'] = ''; |
||
| 854 | } |
||
| 855 | |||
| 856 | // Fetch all FTS dictionaries from the database |
||
| 857 | $ftstpls = $data->getFtsDictionaryTemplates(); |
||
| 858 | |||
| 859 | $this->printTrail('schema'); |
||
| 860 | // TODO: create doc links |
||
| 861 | $this->printTitle($lang['strftscreatedict'], 'pg.ftsdict.create'); |
||
| 862 | $this->printMsg($msg); |
||
| 863 | |||
| 864 | echo '<form action="' . SUBFOLDER . "/src/views/fulltext.php\" method=\"post\">\n"; |
||
| 865 | echo "<table>\n"; |
||
| 866 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
| 867 | echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 868 | htmlspecialchars($_POST['formName']), '" /> ', |
||
| 869 | '<input type="checkbox" name="formIsTemplate" id="formIsTemplate"', $_POST['formIsTemplate'] ? ' checked="checked" ' : '', " />\n", |
||
| 870 | "<label for=\"formIsTemplate\">{$lang['strftscreatedicttemplate']}</label></td>\n\t</tr>\n"; |
||
| 871 | |||
| 872 | // Template |
||
| 873 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strftstemplate']}</th>\n"; |
||
| 874 | echo "\t\t<td class=\"data1\">"; |
||
| 875 | $tpls = []; |
||
| 876 | $tplsel = ''; |
||
| 877 | View Code Duplication | while (!$ftstpls->EOF) { |
|
| 878 | $data->fieldClean($ftstpls->fields['schema']); |
||
| 879 | $data->fieldClean($ftstpls->fields['name']); |
||
| 880 | $tplname = $ftstpls->fields['schema'] . '.' . $ftstpls->fields['name']; |
||
| 881 | $tpls[$tplname] = serialize([ |
||
| 882 | 'name' => $ftstpls->fields['name'], |
||
| 883 | 'schema' => $ftstpls->fields['schema'], |
||
| 884 | ]); |
||
| 885 | if ($_POST['formTemplate'] == $tpls[$tplname]) { |
||
| 886 | $tplsel = htmlspecialchars($tpls[$tplname]); |
||
| 887 | } |
||
| 888 | $ftstpls->moveNext(); |
||
| 889 | } |
||
| 890 | echo \PHPPgAdmin\XHtml\HTMLController::printCombo($tpls, 'formTemplate', true, $tplsel, false); |
||
| 891 | echo "\n\t\t</td>\n\t</tr>\n"; |
||
| 892 | |||
| 893 | // TODO: what about maxlengths? |
||
| 894 | // Lexize |
||
| 895 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strftslexize']}</th>\n"; |
||
| 896 | echo "\t\t<td class=\"data1\"><input name=\"formLexize\" size=\"32\" maxlength=\"1000\" value=\"", |
||
| 897 | htmlspecialchars($_POST['formLexize']), '" ', isset($_POST['formIsTemplate']) ? '' : ' disabled="disabled" ', |
||
| 898 | "/></td>\n\t</tr>\n"; |
||
| 899 | |||
| 900 | // Init |
||
| 901 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strftsinit']}</th>\n"; |
||
| 902 | echo "\t\t<td class=\"data1\"><input name=\"formInit\" size=\"32\" maxlength=\"1000\" value=\"", |
||
| 903 | htmlspecialchars($_POST['formInit']), '"', @$_POST['formIsTemplate'] ? '' : ' disabled="disabled" ', |
||
| 904 | "/></td>\n\t</tr>\n"; |
||
| 905 | |||
| 906 | // Option |
||
| 907 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strftsoptionsvalues']}</th>\n"; |
||
| 908 | echo "\t\t<td class=\"data1\"><input name=\"formOption\" size=\"32\" maxlength=\"1000\" value=\"", |
||
| 909 | htmlspecialchars($_POST['formOption']), "\" /></td>\n\t</tr>\n"; |
||
| 910 | |||
| 911 | // Comment |
||
| 912 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
| 913 | echo "\t\t<td class=\"data1\"><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
| 914 | htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
| 915 | |||
| 916 | echo "</table>\n"; |
||
| 917 | echo "<p>\n"; |
||
| 918 | echo "<input type=\"hidden\" name=\"action\" value=\"createdict\" />\n"; |
||
| 919 | echo '<input type="hidden" name="database" value="', htmlspecialchars($_REQUEST['database']), "\" />\n"; |
||
| 920 | echo $misc->form; |
||
| 921 | echo "<input type=\"submit\" name=\"create\" value=\"{$lang['strcreate']}\" />\n"; |
||
| 922 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" />\n"; |
||
| 923 | echo "</p>\n"; |
||
| 924 | echo "</form>\n", |
||
| 925 | "<script type=\"text/javascript\"> |
||
| 926 | function templateOpts() { |
||
| 927 | isTpl = document.getElementsByName('formIsTemplate')[0].checked; |
||
| 928 | $this->document.getElementsByName('formTemplate')[0].disabled = isTpl; |
||
| 929 | $this->document.getElementsByName('formOption')[0].disabled = isTpl; |
||
| 930 | $this->document.getElementsByName('formLexize')[0].disabled = !isTpl; |
||
| 931 | $this->document.getElementsByName('formInit')[0].disabled = !isTpl; |
||
| 932 | } |
||
| 933 | |||
| 934 | $this->document.getElementsByName('formIsTemplate')[0].onchange = templateOpts; |
||
| 935 | |||
| 936 | templateOpts(); |
||
| 937 | </script>\n"; |
||
| 938 | } |
||
| 939 | |||
| 940 | /** |
||
| 941 | * Actually creates the new FTS dictionary in the database |
||
| 942 | */ |
||
| 943 | public function doSaveCreateDict() |
||
| 944 | { |
||
| 945 | $conf = $this->conf; |
||
| 946 | $misc = $this->misc; |
||
| 947 | $lang = $this->lang; |
||
| 948 | $data = $misc->getDatabaseAccessor(); |
||
| 949 | |||
| 950 | // Check that they've given a name |
||
| 951 | if ($_POST['formName'] == '') { |
||
| 952 | $this->doCreateDict($lang['strftsdictneedsname']); |
||
| 953 | } else { |
||
| 954 | if (!isset($_POST['formIsTemplate'])) { |
||
| 955 | $_POST['formIsTemplate'] = false; |
||
| 956 | } |
||
| 957 | |||
| 958 | View Code Duplication | if (isset($_POST['formTemplate'])) { |
|
| 959 | $formTemplate = unserialize($_POST['formTemplate']); |
||
| 960 | } else { |
||
| 961 | $formTemplate = ''; |
||
| 962 | } |
||
| 963 | |||
| 964 | if (!isset($_POST['formLexize'])) { |
||
| 965 | $_POST['formLexize'] = ''; |
||
| 966 | } |
||
| 967 | |||
| 968 | if (!isset($_POST['formInit'])) { |
||
| 969 | $_POST['formInit'] = ''; |
||
| 970 | } |
||
| 971 | |||
| 972 | if (!isset($_POST['formOption'])) { |
||
| 973 | $_POST['formOption'] = ''; |
||
| 974 | } |
||
| 975 | |||
| 976 | $status = $data->createFtsDictionary($_POST['formName'], $_POST['formIsTemplate'], |
||
| 977 | $formTemplate, $_POST['formLexize'], |
||
| 978 | $_POST['formInit'], $_POST['formOption'], $_POST['formComment'] |
||
| 979 | ); |
||
| 980 | |||
| 981 | if ($status == 0) { |
||
| 982 | $this->misc->setReloadBrowser(true); |
||
| 983 | $this->doViewDicts($lang['strftsdictcreated']); |
||
| 984 | } else { |
||
| 985 | $this->doCreateDict($lang['strftsdictcreatedbad']); |
||
| 986 | } |
||
| 987 | } |
||
| 988 | } |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Display a form to permit editing FTS dictionary properies. |
||
| 992 | */ |
||
| 993 | public function doAlterDict($msg = '') |
||
| 1044 | } |
||
| 1045 | } |
||
| 1046 | |||
| 1047 | /** |
||
| 1048 | * Save the form submission containing changes to a FTS dictionary |
||
| 1049 | */ |
||
| 1050 | public function doSaveAlterDict() |
||
| 1062 | } |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Show confirmation of drop and perform actual drop of FTS mapping |
||
| 1067 | */ |
||
| 1068 | public function doDropMapping($confirm) |
||
| 1069 | { |
||
| 1070 | $conf = $this->conf; |
||
| 1071 | $misc = $this->misc; |
||
| 1125 | } |
||
| 1126 | } |
||
| 1127 | } |
||
| 1128 | } |
||
| 1129 | |||
| 1130 | public function doAlterMapping($msg = '') |
||
| 1131 | { |
||
| 1132 | $conf = $this->conf; |
||
| 1133 | $misc = $this->misc; |
||
| 1134 | $lang = $this->lang; |
||
| 1135 | $data = $misc->getDatabaseAccessor(); |
||
| 1136 | $this->printTrail('ftscfg'); |
||
| 1137 | $this->printTitle($lang['stralter'], 'pg.ftscfg.alter'); |
||
| 1138 | $this->printMsg($msg); |
||
| 1139 | |||
| 1140 | $ftsdicts = $data->getFtsDictionaries(); |
||
| 1141 | if ($ftsdicts->recordCount() > 0) { |
||
| 1142 | if (!isset($_POST['formMapping'])) { |
||
| 1143 | $_POST['formMapping'] = @$_REQUEST['mapping']; |
||
| 1144 | } |
||
| 1145 | |||
| 1146 | if (!isset($_POST['formDictionary'])) { |
||
| 1147 | $_POST['formDictionary'] = ''; |
||
| 1148 | } |
||
| 1149 | |||
| 1150 | if (!isset($_POST['ftscfg'])) { |
||
| 1151 | $_POST['ftscfg'] = $_REQUEST['ftscfg']; |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | echo '<form action="' . SUBFOLDER . "/src/views/fulltext.php\" method=\"post\">\n"; |
||
| 1155 | |||
| 1156 | echo "<table>\n"; |
||
| 1157 | echo "\t<tr>\n"; |
||
| 1158 | echo "\t\t<th class=\"data left required\">{$lang['strftsmapping']}</th>\n"; |
||
| 1159 | echo "\t\t<td class=\"data1\">"; |
||
| 1160 | |||
| 1161 | // Case of multiaction drop |
||
| 1162 | if (isset($_REQUEST['ma'])) { |
||
| 1163 | $ma_mappings = []; |
||
| 1164 | $ma_mappings_names = []; |
||
| 1165 | foreach ($_REQUEST['ma'] as $v) { |
||
| 1166 | $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES)); |
||
| 1167 | printf('<input type="hidden" name="formMapping[]" value="%s" />', htmlspecialchars($a['mapping'])); |
||
| 1168 | $ma_mappings[] = $data->getFtsMappingByName($_POST['ftscfg'], $a['mapping']); |
||
| 1169 | $ma_mappings_names[] = $a['mapping']; |
||
| 1170 | } |
||
| 1171 | echo implode(', ', $ma_mappings_names); |
||
| 1172 | } else { |
||
| 1173 | $mapping = $data->getFtsMappingByName($_POST['ftscfg'], $_POST['formMapping']); |
||
| 1174 | echo $mapping->fields['name']; |
||
| 1175 | echo '<input type="hidden" name="formMapping" value="', htmlspecialchars($_POST['formMapping']), "\" />\n"; |
||
| 1176 | } |
||
| 1177 | |||
| 1178 | echo "\t\t</td>\n"; |
||
| 1179 | echo "\t</tr>\n"; |
||
| 1180 | |||
| 1181 | // Dictionary |
||
| 1182 | echo "\t<tr>\n"; |
||
| 1183 | echo "\t\t<th class=\"data left required\">{$lang['strftsdict']}</th>\n"; |
||
| 1184 | echo "\t\t<td class=\"data1\">"; |
||
| 1185 | echo "\t\t\t<select name=\"formDictionary\">\n"; |
||
| 1186 | while (!$ftsdicts->EOF) { |
||
| 1187 | $ftsdict = htmlspecialchars($ftsdicts->fields['name']); |
||
| 1188 | echo "\t\t\t\t<option value=\"{$ftsdict}\"", |
||
| 1189 | ($ftsdict == $_POST['formDictionary'] || $ftsdict == @$mapping->fields['dictionaries'] || $ftsdict == @$ma_mappings[0]->fields['dictionaries']) ? ' selected="selected"' : '', ">{$ftsdict}</option>\n"; |
||
| 1190 | $ftsdicts->moveNext(); |
||
| 1191 | } |
||
| 1192 | |||
| 1193 | echo "\t\t</td>\n"; |
||
| 1194 | echo "\t</tr>\n"; |
||
| 1195 | |||
| 1196 | echo "</table>\n"; |
||
| 1197 | echo "<p><input type=\"hidden\" name=\"action\" value=\"altermapping\" />\n"; |
||
| 1198 | echo '<input type="hidden" name="ftscfg" value="', htmlspecialchars($_POST['ftscfg']), "\" />\n"; |
||
| 1199 | echo "<input type=\"hidden\" name=\"prev_action\" value=\"viewconfig\" /></p>\n"; |
||
| 1200 | |||
| 1201 | echo $misc->form; |
||
| 1202 | echo "<input type=\"submit\" name=\"alter\" value=\"{$lang['stralter']}\" />\n"; |
||
| 1203 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 1204 | echo "</form>\n"; |
||
| 1205 | } else { |
||
| 1206 | echo "<p>{$lang['strftsnodictionaries']}</p>\n"; |
||
| 1207 | } |
||
| 1208 | } |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Save the form submission containing changes to a FTS mapping |
||
| 1212 | */ |
||
| 1213 | View Code Duplication | public function doSaveAlterMapping() |
|
| 1214 | { |
||
| 1215 | $conf = $this->conf; |
||
| 1216 | $misc = $this->misc; |
||
| 1217 | $lang = $this->lang; |
||
| 1218 | $data = $misc->getDatabaseAccessor(); |
||
| 1219 | |||
| 1220 | $mappingArray = (is_array($_POST['formMapping']) ? $_POST['formMapping'] : [$_POST['formMapping']]); |
||
| 1221 | $status = $data->changeFtsMapping($_POST['ftscfg'], $mappingArray, 'alter', $_POST['formDictionary']); |
||
| 1222 | if ($status == 0) { |
||
| 1223 | $this->doViewConfig($_POST['ftscfg'], $lang['strftsmappingaltered']); |
||
| 1224 | } else { |
||
| 1225 | $this->doAlterMapping($lang['strftsmappingalteredbad']); |
||
| 1226 | } |
||
| 1227 | } |
||
| 1228 | |||
| 1229 | /** |
||
| 1230 | * Show the form to enter parameters of a new FTS mapping |
||
| 1231 | */ |
||
| 1232 | public function doAddMapping($msg = '') |
||
| 1300 | } |
||
| 1301 | } |
||
| 1302 | |||
| 1303 | /** |
||
| 1304 | * Save the form submission containing parameters of a new FTS mapping |
||
| 1305 | */ |
||
| 1306 | View Code Duplication | public function doSaveAddMapping() |
|
| 1319 | } |
||
| 1320 | } |
||
| 1321 | } |
||
| 1322 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: