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