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