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