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