| Conditions | 68 |
| Total Lines | 453 |
| Code Lines | 265 |
| Lines | 47 |
| Ratio | 10.38 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 100 | public function doBrowse($msg = '') |
||
| 101 | { |
||
| 102 | $conf = $this->conf; |
||
| 103 | $misc = $this->misc; |
||
| 104 | $lang = $this->lang; |
||
| 105 | $plugin_manager = $this->plugin_manager; |
||
| 106 | $data = $misc->getDatabaseAccessor(); |
||
| 107 | |||
| 108 | $save_history = false; |
||
| 109 | // If current page is not set, default to first page |
||
| 110 | if (!isset($_REQUEST['page'])) { |
||
| 111 | $_REQUEST['page'] = 1; |
||
| 112 | } |
||
| 113 | |||
| 114 | if (!isset($_REQUEST['nohistory'])) { |
||
| 115 | $save_history = true; |
||
| 116 | } |
||
| 117 | |||
| 118 | if (isset($_REQUEST['subject'])) { |
||
| 119 | $subject = $_REQUEST['subject']; |
||
| 120 | if (isset($_REQUEST[$subject])) { |
||
| 121 | $object = $_REQUEST[$subject]; |
||
| 122 | } |
||
| 123 | } else { |
||
| 124 | $subject = ''; |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->printTrail(isset($subject) ? $subject : 'database'); |
||
| 128 | $this->printTabs($subject, 'browse'); |
||
| 129 | |||
| 130 | /* This code is used when browsing FK in pure-xHTML (without js) */ |
||
| 131 | if (isset($_REQUEST['fkey'])) { |
||
| 132 | $ops = []; |
||
| 133 | foreach ($_REQUEST['fkey'] as $x => $y) { |
||
| 134 | $ops[$x] = '='; |
||
| 135 | } |
||
| 136 | $query = $data->getSelectSQL($_REQUEST['table'], [], $_REQUEST['fkey'], $ops); |
||
| 137 | $_REQUEST['query'] = $query; |
||
| 138 | } |
||
| 139 | |||
| 140 | if (isset($object)) { |
||
| 141 | if (isset($_REQUEST['query'])) { |
||
| 142 | $_SESSION['sqlquery'] = $_REQUEST['query']; |
||
| 143 | $this->printTitle($lang['strselect']); |
||
| 144 | $type = 'SELECT'; |
||
| 145 | } else { |
||
| 146 | $type = 'TABLE'; |
||
| 147 | } |
||
| 148 | } else { |
||
| 149 | $this->printTitle($lang['strqueryresults']); |
||
| 150 | /*we comes from sql.php, $_SESSION['sqlquery'] has been set there */ |
||
| 151 | $type = 'QUERY'; |
||
| 152 | } |
||
| 153 | |||
| 154 | $this->printMsg($msg); |
||
| 155 | |||
| 156 | // If 'sortkey' is not set, default to '' |
||
| 157 | if (!isset($_REQUEST['sortkey'])) { |
||
| 158 | $_REQUEST['sortkey'] = ''; |
||
| 159 | } |
||
| 160 | |||
| 161 | // If 'sortdir' is not set, default to '' |
||
| 162 | if (!isset($_REQUEST['sortdir'])) { |
||
| 163 | $_REQUEST['sortdir'] = ''; |
||
| 164 | } |
||
| 165 | |||
| 166 | // If 'strings' is not set, default to collapsed |
||
| 167 | if (!isset($_REQUEST['strings'])) { |
||
| 168 | $_REQUEST['strings'] = 'collapsed'; |
||
| 169 | } |
||
| 170 | |||
| 171 | // Fetch unique row identifier, if this is a table browse request. |
||
| 172 | if (isset($object)) { |
||
| 173 | $key = $data->getRowIdentifier($object); |
||
| 174 | } else { |
||
| 175 | $key = []; |
||
| 176 | } |
||
| 177 | |||
| 178 | // Set the schema search path |
||
| 179 | View Code Duplication | if (isset($_REQUEST['search_path'])) { |
|
| 180 | if ($data->setSearchPath(array_map('trim', explode(',', $_REQUEST['search_path']))) != 0) { |
||
| 181 | return; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | try { |
||
| 186 | // Retrieve page from query. $max_pages is returned by reference. |
||
| 187 | $rs = $data->browseQuery($type, |
||
| 188 | isset($object) ? $object : null, |
||
| 189 | isset($_SESSION['sqlquery']) ? $_SESSION['sqlquery'] : null, |
||
| 190 | $_REQUEST['sortkey'], $_REQUEST['sortdir'], $_REQUEST['page'], |
||
| 191 | $conf['max_rows'], $max_pages); |
||
| 192 | } catch (\PHPPgAdmin\ADOdbException $e) { |
||
| 193 | return; |
||
| 194 | } |
||
| 195 | |||
| 196 | $fkey_information = $this->getFKInfo(); |
||
| 197 | |||
| 198 | // Build strings for GETs in array |
||
| 199 | $_gets = [ |
||
| 200 | 'server' => $_REQUEST['server'], |
||
| 201 | 'database' => $_REQUEST['database'], |
||
| 202 | ]; |
||
| 203 | |||
| 204 | if (isset($_REQUEST['schema'])) { |
||
| 205 | $_gets['schema'] = $_REQUEST['schema']; |
||
| 206 | } |
||
| 207 | |||
| 208 | if (isset($object)) { |
||
| 209 | $_gets[$subject] = $object; |
||
| 210 | } |
||
| 211 | |||
| 212 | if (isset($subject)) { |
||
| 213 | $_gets['subject'] = $subject; |
||
| 214 | } |
||
| 215 | |||
| 216 | if (isset($_REQUEST['query'])) { |
||
| 217 | $_gets['query'] = $_REQUEST['query']; |
||
| 218 | } |
||
| 219 | |||
| 220 | if (isset($_REQUEST['count'])) { |
||
| 221 | $_gets['count'] = $_REQUEST['count']; |
||
| 222 | } |
||
| 223 | |||
| 224 | if (isset($_REQUEST['return'])) { |
||
| 225 | $_gets['return'] = $_REQUEST['return']; |
||
| 226 | } |
||
| 227 | |||
| 228 | if (isset($_REQUEST['search_path'])) { |
||
| 229 | $_gets['search_path'] = $_REQUEST['search_path']; |
||
| 230 | } |
||
| 231 | |||
| 232 | if (isset($_REQUEST['table'])) { |
||
| 233 | $_gets['table'] = $_REQUEST['table']; |
||
| 234 | } |
||
| 235 | |||
| 236 | if (isset($_REQUEST['sortkey'])) { |
||
| 237 | $_gets['sortkey'] = $_REQUEST['sortkey']; |
||
| 238 | } |
||
| 239 | |||
| 240 | if (isset($_REQUEST['sortdir'])) { |
||
| 241 | $_gets['sortdir'] = $_REQUEST['sortdir']; |
||
| 242 | } |
||
| 243 | |||
| 244 | if (isset($_REQUEST['nohistory'])) { |
||
| 245 | $_gets['nohistory'] = $_REQUEST['nohistory']; |
||
| 246 | } |
||
| 247 | |||
| 248 | $_gets['strings'] = $_REQUEST['strings']; |
||
| 249 | |||
| 250 | if ($save_history && is_object($rs) && ($type == 'QUERY')) { //{ |
||
| 251 | $misc->saveScriptHistory($_REQUEST['query']); |
||
| 252 | } |
||
| 253 | |||
| 254 | if (isset($_REQUEST['query'])) { |
||
| 255 | $query = $_REQUEST['query']; |
||
| 256 | } else { |
||
| 257 | $query = "SELECT * FROM {$_REQUEST['schema']}"; |
||
| 258 | if ($_REQUEST['subject'] == 'matview') { |
||
| 259 | $query = "{$query}.{$_REQUEST['matview']};"; |
||
| 260 | } elseif ($_REQUEST['subject'] == 'view') { |
||
| 261 | $query = "{$query}.{$_REQUEST['view']};"; |
||
| 262 | } else { |
||
| 263 | $query = "{$query}.{$_REQUEST['table']};"; |
||
| 264 | } |
||
| 265 | } |
||
| 266 | //$query = isset($_REQUEST['query'])? $_REQUEST['query'] : "select * from {$_REQUEST['schema']}.{$_REQUEST['table']};"; |
||
| 267 | $this->prtrace($query); |
||
| 268 | //die(htmlspecialchars($query)); |
||
| 269 | |||
| 270 | echo '<form method="post" id="sqlform" action="' . $_SERVER['REQUEST_URI'] . '">'; |
||
| 271 | echo '<textarea width="90%" name="query" id="query" rows="5" cols="100" resizable="true">'; |
||
| 272 | |||
| 273 | echo htmlspecialchars($query); |
||
| 274 | echo '</textarea><br><input type="submit"/></form>'; |
||
| 275 | |||
| 276 | if (is_object($rs) && $rs->recordCount() > 0) { |
||
| 277 | // Show page navigation |
||
| 278 | $misc->printPages($_REQUEST['page'], $max_pages, $_gets); |
||
| 279 | |||
| 280 | echo "<table id=\"data\">\n<tr>"; |
||
| 281 | |||
| 282 | // Check that the key is actually in the result set. This can occur for select |
||
| 283 | // operations where the key fields aren't part of the select. XXX: We should |
||
| 284 | // be able to support this, somehow. |
||
| 285 | foreach ($key as $v) { |
||
| 286 | // If a key column is not found in the record set, then we |
||
| 287 | // can't use the key. |
||
| 288 | if (!in_array($v, array_keys($rs->fields))) { |
||
| 289 | $key = []; |
||
| 290 | break; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | $buttons = [ |
||
| 295 | 'edit' => [ |
||
| 296 | 'content' => $lang['stredit'], |
||
| 297 | 'attr' => [ |
||
| 298 | 'href' => [ |
||
| 299 | 'url' => 'display.php', |
||
| 300 | 'urlvars' => array_merge([ |
||
| 301 | 'action' => 'confeditrow', |
||
| 302 | 'strings' => $_REQUEST['strings'], |
||
| 303 | 'page' => $_REQUEST['page'], |
||
| 304 | ], $_gets), |
||
| 305 | ], |
||
| 306 | ], |
||
| 307 | ], |
||
| 308 | 'delete' => [ |
||
| 309 | 'content' => $lang['strdelete'], |
||
| 310 | 'attr' => [ |
||
| 311 | 'href' => [ |
||
| 312 | 'url' => 'display.php', |
||
| 313 | 'urlvars' => array_merge([ |
||
| 314 | 'action' => 'confdelrow', |
||
| 315 | 'strings' => $_REQUEST['strings'], |
||
| 316 | 'page' => $_REQUEST['page'], |
||
| 317 | ], $_gets), |
||
| 318 | ], |
||
| 319 | ], |
||
| 320 | ], |
||
| 321 | ]; |
||
| 322 | $actions = [ |
||
| 323 | 'actionbuttons' => &$buttons, |
||
| 324 | 'place' => 'display-browse', |
||
| 325 | ]; |
||
| 326 | $plugin_manager->do_hook('actionbuttons', $actions); |
||
| 327 | |||
| 328 | foreach (array_keys($actions['actionbuttons']) as $action) { |
||
| 329 | $actions['actionbuttons'][$action]['attr']['href']['urlvars'] = array_merge( |
||
| 330 | $actions['actionbuttons'][$action]['attr']['href']['urlvars'], |
||
| 331 | $_gets |
||
| 332 | ); |
||
| 333 | } |
||
| 334 | |||
| 335 | $edit_params = isset($actions['actionbuttons']['edit']) ? |
||
| 336 | $actions['actionbuttons']['edit'] : []; |
||
| 337 | $delete_params = isset($actions['actionbuttons']['delete']) ? |
||
| 338 | $actions['actionbuttons']['delete'] : []; |
||
| 339 | |||
| 340 | // Display edit and delete actions if we have a key |
||
| 341 | $colspan = count($buttons); |
||
| 342 | if ($colspan > 0 and count($key) > 0) { |
||
| 343 | echo "<th colspan=\"{$colspan}\" class=\"data\">{$lang['stractions']}</th>" . "\n"; |
||
| 344 | } |
||
| 345 | |||
| 346 | /* we show OIDs only if we are in TABLE or SELECT type browsing */ |
||
| 347 | $this->printTableHeaderCells($rs, $_gets, isset($object)); |
||
| 348 | |||
| 349 | echo '</tr>' . "\n"; |
||
| 350 | |||
| 351 | $i = 0; |
||
| 352 | reset($rs->fields); |
||
| 353 | while (!$rs->EOF) { |
||
| 354 | $id = (($i % 2) == 0 ? '1' : '2'); |
||
| 355 | echo "<tr class=\"data{$id}\">" . "\n"; |
||
| 356 | // Display edit and delete links if we have a key |
||
| 357 | if ($colspan > 0 and count($key) > 0) { |
||
| 358 | $keys_array = []; |
||
| 359 | $has_nulls = false; |
||
| 360 | foreach ($key as $v) { |
||
| 361 | if ($rs->fields[$v] === null) { |
||
| 362 | $has_nulls = true; |
||
| 363 | break; |
||
| 364 | } |
||
| 365 | $keys_array["key[{$v}]"] = $rs->fields[$v]; |
||
| 366 | } |
||
| 367 | if ($has_nulls) { |
||
| 368 | echo "<td colspan=\"{$colspan}\"> </td>" . "\n"; |
||
| 369 | } else { |
||
| 370 | View Code Duplication | if (isset($actions['actionbuttons']['edit'])) { |
|
| 371 | $actions['actionbuttons']['edit'] = $edit_params; |
||
| 372 | $actions['actionbuttons']['edit']['attr']['href']['urlvars'] = array_merge( |
||
| 373 | $actions['actionbuttons']['edit']['attr']['href']['urlvars'], |
||
| 374 | $keys_array |
||
| 375 | ); |
||
| 376 | } |
||
| 377 | |||
| 378 | View Code Duplication | if (isset($actions['actionbuttons']['delete'])) { |
|
| 379 | $actions['actionbuttons']['delete'] = $delete_params; |
||
| 380 | $actions['actionbuttons']['delete']['attr']['href']['urlvars'] = array_merge( |
||
| 381 | $actions['actionbuttons']['delete']['attr']['href']['urlvars'], |
||
| 382 | $keys_array |
||
| 383 | ); |
||
| 384 | } |
||
| 385 | |||
| 386 | foreach ($actions['actionbuttons'] as $action) { |
||
| 387 | echo "<td class=\"opbutton{$id}\">"; |
||
| 388 | $this->printLink($action, true, __METHOD__); |
||
| 389 | echo '</td>' . "\n"; |
||
| 390 | } |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | $this->printTableRowCells($rs, $fkey_information, isset($object)); |
||
| 395 | |||
| 396 | echo '</tr>' . "\n"; |
||
| 397 | $rs->moveNext(); |
||
| 398 | $i++; |
||
| 399 | } |
||
| 400 | echo '</table>' . "\n"; |
||
| 401 | |||
| 402 | echo '<p>', $rs->recordCount(), " {$lang['strrows']}</p>" . "\n"; |
||
| 403 | // Show page navigation |
||
| 404 | $misc->printPages($_REQUEST['page'], $max_pages, $_gets); |
||
| 405 | } else { |
||
| 406 | echo "<p>{$lang['strnodata']}</p>" . "\n"; |
||
| 407 | } |
||
| 408 | |||
| 409 | // Navigation links |
||
| 410 | $navlinks = []; |
||
| 411 | |||
| 412 | $fields = [ |
||
| 413 | 'server' => $_REQUEST['server'], |
||
| 414 | 'database' => $_REQUEST['database'], |
||
| 415 | ]; |
||
| 416 | |||
| 417 | if (isset($_REQUEST['schema'])) { |
||
| 418 | $fields['schema'] = $_REQUEST['schema']; |
||
| 419 | } |
||
| 420 | |||
| 421 | // Return |
||
| 422 | View Code Duplication | if (isset($_REQUEST['return'])) { |
|
| 423 | $urlvars = $misc->getSubjectParams($_REQUEST['return']); |
||
| 424 | |||
| 425 | $navlinks['back'] = [ |
||
| 426 | 'attr' => [ |
||
| 427 | 'href' => [ |
||
| 428 | 'url' => $urlvars['url'], |
||
| 429 | 'urlvars' => $urlvars['params'], |
||
| 430 | ], |
||
| 431 | ], |
||
| 432 | 'content' => $lang['strback'], |
||
| 433 | ]; |
||
| 434 | } |
||
| 435 | |||
| 436 | // Edit SQL link |
||
| 437 | if ($type == 'QUERY') { |
||
| 438 | $navlinks['edit'] = [ |
||
| 439 | 'attr' => [ |
||
| 440 | 'href' => [ |
||
| 441 | 'url' => 'database.php', |
||
| 442 | 'urlvars' => array_merge($fields, [ |
||
| 443 | 'action' => 'sql', |
||
| 444 | 'paginate' => 'on', |
||
| 445 | ]), |
||
| 446 | ], |
||
| 447 | ], |
||
| 448 | 'content' => $lang['streditsql'], |
||
| 449 | ]; |
||
| 450 | } |
||
| 451 | |||
| 452 | // Expand/Collapse |
||
| 453 | if ($_REQUEST['strings'] == 'expanded') { |
||
| 454 | $navlinks['collapse'] = [ |
||
| 455 | 'attr' => [ |
||
| 456 | 'href' => [ |
||
| 457 | 'url' => 'display.php', |
||
| 458 | 'urlvars' => array_merge( |
||
| 459 | $_gets, |
||
| 460 | [ |
||
| 461 | 'strings' => 'collapsed', |
||
| 462 | 'page' => $_REQUEST['page'], |
||
| 463 | ]), |
||
| 464 | ], |
||
| 465 | ], |
||
| 466 | 'content' => $lang['strcollapse'], |
||
| 467 | ]; |
||
| 468 | } else { |
||
| 469 | $navlinks['collapse'] = [ |
||
| 470 | 'attr' => [ |
||
| 471 | 'href' => [ |
||
| 472 | 'url' => 'display.php', |
||
| 473 | 'urlvars' => array_merge( |
||
| 474 | $_gets, |
||
| 475 | [ |
||
| 476 | 'strings' => 'expanded', |
||
| 477 | 'page' => $_REQUEST['page'], |
||
| 478 | ]), |
||
| 479 | ], |
||
| 480 | ], |
||
| 481 | 'content' => $lang['strexpand'], |
||
| 482 | ]; |
||
| 483 | } |
||
| 484 | |||
| 485 | // Create view and download |
||
| 486 | if (isset($_REQUEST['query']) && isset($rs) && is_object($rs) && $rs->recordCount() > 0) { |
||
| 487 | |||
| 488 | // Report views don't set a schema, so we need to disable create view in that case |
||
| 489 | View Code Duplication | if (isset($_REQUEST['schema'])) { |
|
| 490 | $navlinks['createview'] = [ |
||
| 491 | 'attr' => [ |
||
| 492 | 'href' => [ |
||
| 493 | 'url' => 'views.php', |
||
| 494 | 'urlvars' => array_merge($fields, [ |
||
| 495 | 'action' => 'create', |
||
| 496 | 'formDefinition' => $_REQUEST['query'], |
||
| 497 | ]), |
||
| 498 | ], |
||
| 499 | ], |
||
| 500 | 'content' => $lang['strcreateview'], |
||
| 501 | ]; |
||
| 502 | } |
||
| 503 | |||
| 504 | $urlvars = []; |
||
| 505 | if (isset($_REQUEST['search_path'])) { |
||
| 506 | $urlvars['search_path'] = $_REQUEST['search_path']; |
||
| 507 | } |
||
| 508 | |||
| 509 | $navlinks['download'] = [ |
||
| 510 | 'attr' => [ |
||
| 511 | 'href' => [ |
||
| 512 | 'url' => 'dataexport.php', |
||
| 513 | 'urlvars' => array_merge($fields, $urlvars), |
||
| 514 | ], |
||
| 515 | ], |
||
| 516 | 'content' => $lang['strdownload'], |
||
| 517 | ]; |
||
| 518 | } |
||
| 519 | |||
| 520 | // Insert |
||
| 521 | if (isset($object) && (isset($subject) && $subject == 'table')) { |
||
| 522 | $navlinks['insert'] = [ |
||
| 523 | 'attr' => [ |
||
| 524 | 'href' => [ |
||
| 525 | 'url' => 'tables.php', |
||
| 526 | 'urlvars' => array_merge($fields, [ |
||
| 527 | 'action' => 'confinsertrow', |
||
| 528 | 'table' => $object, |
||
| 529 | ]), |
||
| 530 | ], |
||
| 531 | ], |
||
| 532 | 'content' => $lang['strinsert'], |
||
| 533 | ]; |
||
| 534 | } |
||
| 535 | |||
| 536 | // Refresh |
||
| 537 | $navlinks['refresh'] = [ |
||
| 538 | 'attr' => [ |
||
| 539 | 'href' => [ |
||
| 540 | 'url' => 'display.php', |
||
| 541 | 'urlvars' => array_merge( |
||
| 542 | $_gets, |
||
| 543 | [ |
||
| 544 | 'strings' => $_REQUEST['strings'], |
||
| 545 | 'page' => $_REQUEST['page'], |
||
| 546 | ]), |
||
| 547 | ], |
||
| 548 | ], |
||
| 549 | 'content' => $lang['strrefresh'], |
||
| 550 | ]; |
||
| 551 | |||
| 552 | $this->printNavLinks($navlinks, 'display-browse', get_defined_vars()); |
||
| 553 | } |
||
| 1021 |
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: