Skip to content

Commit d70644e

Browse files
committed
Extract results nav buttons into table template
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent dfe47a1 commit d70644e

6 files changed

Lines changed: 110 additions & 259 deletions

File tree

‎libraries/classes/Display/Results.php‎

Lines changed: 25 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use PhpMyAdmin\Utils\Gis;
3434

3535
use function __;
36-
use function _pgettext;
3736
use function array_filter;
3837
use function array_keys;
3938
use function array_merge;
@@ -659,64 +658,6 @@ private function isSelect(array $analyzedSqlResults): bool
659658
&& ! empty($analyzedSqlResults['statement']->from[0]->table);
660659
}
661660

662-
/**
663-
* Get a navigation button
664-
*
665-
* @see getMoveBackwardButtonsForTableNavigation(),
666-
* getMoveForwardButtonsForTableNavigation()
667-
*
668-
* @param string $caption iconic caption for button
669-
* @param string $title text for button
670-
* @param int $pos position for next query
671-
* @param string $htmlSqlQuery query ready for display
672-
* @param bool $back whether 'begin' or 'previous'
673-
* @param string $onsubmit optional onsubmit clause
674-
* @param string $inputForRealEnd optional hidden field for special treatment
675-
*
676-
* @return string html content
677-
*/
678-
private function getTableNavigationButton(
679-
$caption,
680-
$title,
681-
$pos,
682-
$htmlSqlQuery,
683-
$back,
684-
$onsubmit = '',
685-
$inputForRealEnd = ''
686-
): string {
687-
$captionOutput = '';
688-
if ($back) {
689-
if (Util::showIcons('TableNavigationLinksMode')) {
690-
$captionOutput .= $caption;
691-
}
692-
693-
if (Util::showText('TableNavigationLinksMode')) {
694-
$captionOutput .= '&nbsp;' . $title;
695-
}
696-
} else {
697-
if (Util::showText('TableNavigationLinksMode')) {
698-
$captionOutput .= $title;
699-
}
700-
701-
if (Util::showIcons('TableNavigationLinksMode')) {
702-
$captionOutput .= '&nbsp;' . $caption;
703-
}
704-
}
705-
706-
return $this->template->render('display/results/table_navigation_button', [
707-
'db' => $this->properties['db'],
708-
'table' => $this->properties['table'],
709-
'sql_query' => $htmlSqlQuery,
710-
'pos' => $pos,
711-
'is_browse_distinct' => $this->properties['is_browse_distinct'],
712-
'goto' => $this->properties['goto'],
713-
'input_for_real_end' => $inputForRealEnd,
714-
'caption_output' => $captionOutput,
715-
'title' => $title,
716-
'onsubmit' => $onsubmit,
717-
]);
718-
}
719-
720661
/**
721662
* Possibly return a page selector for table navigation
722663
*
@@ -775,15 +716,6 @@ private function getTableNavigation(
775716
): array {
776717
$isShowingAll = $_SESSION['tmpval']['max_rows'] === self::ALL_ROWS;
777718

778-
// Move to the beginning or to the previous page
779-
$moveBackwardButtons = '';
780-
if ($_SESSION['tmpval']['pos'] && ! $isShowingAll) {
781-
$moveBackwardButtons = $this->getMoveBackwardButtonsForTableNavigation(
782-
htmlspecialchars($this->properties['sql_query']),
783-
$posPrevious
784-
);
785-
}
786-
787719
$pageSelector = '';
788720
$numberTotalPage = 1;
789721
if (! $isShowingAll) {
@@ -793,22 +725,24 @@ private function getTableNavigation(
793725
] = $this->getHtmlPageSelector();
794726
}
795727

796-
// Move to the next page or to the last one
797-
$moveForwardButtons = '';
798-
if (
799-
// view with unknown number of rows
800-
($this->properties['unlim_num_rows'] === -1 || $this->properties['unlim_num_rows'] === false)
801-
|| (! $isShowingAll
802-
&& intval($_SESSION['tmpval']['pos']) + intval($_SESSION['tmpval']['max_rows'])
803-
< $this->properties['unlim_num_rows']
804-
&& $this->properties['num_rows'] >= $_SESSION['tmpval']['max_rows'])
805-
) {
806-
$moveForwardButtons = $this->getMoveForwardButtonsForTableNavigation(
807-
htmlspecialchars($this->properties['sql_query']),
808-
$posNext,
809-
$isInnodb
810-
);
811-
}
728+
$isLastPage = $this->properties['unlim_num_rows'] !== -1 && $this->properties['unlim_num_rows'] !== false
729+
&& ($isShowingAll
730+
|| intval($_SESSION['tmpval']['pos']) + intval($_SESSION['tmpval']['max_rows'])
731+
>= $this->properties['unlim_num_rows']
732+
|| $this->properties['num_rows'] < $_SESSION['tmpval']['max_rows']);
733+
734+
$onsubmit = ' onsubmit="return '
735+
. (intval($_SESSION['tmpval']['pos'])
736+
+ intval($_SESSION['tmpval']['max_rows'])
737+
< $this->properties['unlim_num_rows']
738+
&& $this->properties['num_rows'] >= intval($_SESSION['tmpval']['max_rows'])
739+
? 'true'
740+
: 'false') . ';"';
741+
742+
$hasRealEndInput = $isInnodb && $this->properties['unlim_num_rows'] > $GLOBALS['cfg']['MaxExactCount'];
743+
$posLast = @((int) ceil(
744+
(int) $this->properties['unlim_num_rows'] / $_SESSION['tmpval']['max_rows']
745+
) - 1) * intval($_SESSION['tmpval']['max_rows']);
812746

813747
$hiddenFields = [
814748
'db' => $this->properties['db'],
@@ -820,9 +754,7 @@ private function getTableNavigation(
820754
];
821755

822756
return [
823-
'move_backward_buttons' => $moveBackwardButtons,
824757
'page_selector' => $pageSelector,
825-
'move_forward_buttons' => $moveForwardButtons,
826758
'number_total_page' => $numberTotalPage,
827759
'has_show_all' => $GLOBALS['cfg']['ShowAll'] || ($this->properties['unlim_num_rows'] <= 500),
828760
'hidden_fields' => $hiddenFields,
@@ -831,95 +763,15 @@ private function getTableNavigation(
831763
'max_rows' => $_SESSION['tmpval']['max_rows'],
832764
'pos' => $_SESSION['tmpval']['pos'],
833765
'sort_by_key' => $sortByKeyData,
766+
'pos_previous' => $posPrevious,
767+
'pos_next' => $posNext,
768+
'pos_last' => $posLast,
769+
'is_last_page' => $isLastPage,
770+
'has_real_end_input' => $hasRealEndInput,
771+
'onsubmit' => $onsubmit,
834772
];
835773
}
836774

837-
/**
838-
* Prepare move backward buttons - previous and first
839-
*
840-
* @see getTableNavigation()
841-
*
842-
* @param string $htmlSqlQuery the sql encoded by html special characters
843-
* @param int $posPrev the offset for the "previous" page
844-
*
845-
* @return string html content
846-
*/
847-
private function getMoveBackwardButtonsForTableNavigation(
848-
string $htmlSqlQuery,
849-
int $posPrev
850-
): string {
851-
return $this->getTableNavigationButton(
852-
'&lt;&lt;',
853-
_pgettext('First page', 'Begin'),
854-
0,
855-
$htmlSqlQuery,
856-
true
857-
)
858-
. $this->getTableNavigationButton(
859-
'&lt;',
860-
_pgettext('Previous page', 'Previous'),
861-
$posPrev,
862-
$htmlSqlQuery,
863-
true
864-
);
865-
}
866-
867-
/**
868-
* Prepare move forward buttons - next and last
869-
*
870-
* @see getTableNavigation()
871-
*
872-
* @param string $htmlSqlQuery the sql encoded by htmlspecialchars()
873-
* @param int $posNext the offset for the "next" page
874-
* @param bool $isInnodb whether it's InnoDB or not
875-
*
876-
* @return string html content
877-
*/
878-
private function getMoveForwardButtonsForTableNavigation(
879-
string $htmlSqlQuery,
880-
int $posNext,
881-
bool $isInnodb
882-
): string {
883-
// display the Next button
884-
$buttonsHtml = $this->getTableNavigationButton(
885-
'&gt;',
886-
_pgettext('Next page', 'Next'),
887-
$posNext,
888-
$htmlSqlQuery,
889-
false
890-
);
891-
892-
$inputForRealEnd = '';
893-
// prepare some options for the End button
894-
if ($isInnodb && $this->properties['unlim_num_rows'] > $GLOBALS['cfg']['MaxExactCount']) {
895-
$inputForRealEnd = '<input id="real_end_input" type="hidden" name="find_real_end" value="1">';
896-
// no backquote around this message
897-
}
898-
899-
$maxRows = (int) $_SESSION['tmpval']['max_rows'];
900-
$onsubmit = 'onsubmit="return '
901-
. (intval($_SESSION['tmpval']['pos'])
902-
+ $maxRows
903-
< $this->properties['unlim_num_rows']
904-
&& $this->properties['num_rows'] >= $maxRows
905-
? 'true'
906-
: 'false') . '"';
907-
908-
// display the End button
909-
return $buttonsHtml . $this->getTableNavigationButton(
910-
'&gt;&gt;',
911-
_pgettext('Last page', 'End'),
912-
@((int) ceil(
913-
(int) $this->properties['unlim_num_rows']
914-
/ $_SESSION['tmpval']['max_rows']
915-
) - 1) * $maxRows,
916-
$htmlSqlQuery,
917-
false,
918-
$onsubmit,
919-
$inputForRealEnd
920-
);
921-
}
922-
923775
/**
924776
* Get the headers of the results table, for all of the columns
925777
*
@@ -3728,6 +3580,7 @@ public function getTable(
37283580
'save_cells_at_once' => $GLOBALS['cfg']['SaveCellsAtOnce'],
37293581
'default_sliders_state' => $GLOBALS['cfg']['InitialSlidersState'],
37303582
'text_dir' => $this->properties['text_dir'],
3583+
'is_browse_distinct' => $this->properties['is_browse_distinct'],
37313584
]);
37323585
}
37333586

‎phpstan-baseline.neon‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9450,11 +9450,6 @@ parameters:
94509450
count: 1
94519451
path: test/classes/Display/ResultsTest.php
94529452

9453-
-
9454-
message: "#^Method PhpMyAdmin\\\\Tests\\\\Display\\\\ResultsTest\\:\\:providerForTestGetTableNavigationButton\\(\\) return type has no value type specified in iterable type array\\.$#"
9455-
count: 1
9456-
path: test/classes/Display/ResultsTest.php
9457-
94589453
-
94599454
message: "#^Method PhpMyAdmin\\\\Tests\\\\Display\\\\ResultsTest\\:\\:providerSetConfigParamsForDisplayTable\\(\\) return type has no value type specified in iterable type array\\.$#"
94609455
count: 1
@@ -9562,7 +9557,7 @@ parameters:
95629557

95639558
-
95649559
message: "#^Parameter \\#2 \\$haystack of method PHPUnit\\\\Framework\\\\Assert\\:\\:assertStringContainsString\\(\\) expects string, mixed given\\.$#"
9565-
count: 9
9560+
count: 4
95669561
path: test/classes/Display/ResultsTest.php
95679562

95689563
-

‎psalm-baseline.xml‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6119,6 +6119,7 @@
61196119
<code>$whereClause</code>
61206120
<code>$whereClauseMap[$rowNumber][$meta-&gt;orgtable]</code>
61216121
<code>(int) $this-&gt;properties['unlim_num_rows'] / $_SESSION['tmpval']['max_rows']</code>
6122+
<code>(int) $this-&gt;properties['unlim_num_rows'] / $_SESSION['tmpval']['max_rows']</code>
61226123
<code>empty($field-&gt;database) ? $this-&gt;properties['db'] : $field-&gt;database</code>
61236124
</MixedArgument>
61246125
<MixedArgumentTypeCoercion occurrences="6">
@@ -6129,7 +6130,7 @@
61296130
<code>$sortExpression</code>
61306131
<code>$urlParams</code>
61316132
</MixedArgumentTypeCoercion>
6132-
<MixedArrayAccess occurrences="64">
6133+
<MixedArrayAccess occurrences="65">
61336134
<code>$_SESSION['tmpval']['display_binary']</code>
61346135
<code>$_SESSION['tmpval']['display_binary']</code>
61356136
<code>$_SESSION['tmpval']['display_binary']</code>
@@ -6152,6 +6153,8 @@
61526153
<code>$_SESSION['tmpval']['max_rows']</code>
61536154
<code>$_SESSION['tmpval']['max_rows']</code>
61546155
<code>$_SESSION['tmpval']['max_rows']</code>
6156+
<code>$_SESSION['tmpval']['max_rows']</code>
6157+
<code>$_SESSION['tmpval']['max_rows']</code>
61556158
<code>$_SESSION['tmpval']['pftext']</code>
61566159
<code>$_SESSION['tmpval']['pftext']</code>
61576160
<code>$_SESSION['tmpval']['pftext']</code>
@@ -6163,7 +6166,6 @@
61636166
<code>$_SESSION['tmpval']['pos']</code>
61646167
<code>$_SESSION['tmpval']['pos']</code>
61656168
<code>$_SESSION['tmpval']['pos']</code>
6166-
<code>$_SESSION['tmpval']['pos']</code>
61676169
<code>$_SESSION['tmpval']['possible_as_geometry']</code>
61686170
<code>$_SESSION['tmpval']['query']</code>
61696171
<code>$_SESSION['tmpval']['query']</code>
@@ -15446,8 +15448,7 @@
1544615448
</MixedInferredReturnType>
1544715449
</file>
1544815450
<file src="test/classes/Display/ResultsTest.php">
15449-
<MixedArgument occurrences="21">
15450-
<code>$actual</code>
15451+
<MixedArgument occurrences="20">
1545115452
<code>$analyzedSqlResults</code>
1545215453
<code>$analyzedSqlResults</code>
1545315454
<code>$analyzedSqlResults['is_analyse']</code>
@@ -15495,15 +15496,13 @@
1549515496
<code>$_SESSION['tmpval']['relational_display']</code>
1549615497
<code>$_SESSION['tmpval']['relational_display']</code>
1549715498
</MixedArrayAssignment>
15498-
<MixedAssignment occurrences="5">
15499-
<code>$actual</code>
15499+
<MixedAssignment occurrences="4">
1550015500
<code>$output</code>
1550115501
<code>$output</code>
1550215502
<code>$output</code>
1550315503
<code>$output</code>
1550415504
</MixedAssignment>
15505-
<MixedInferredReturnType occurrences="7">
15506-
<code>array</code>
15505+
<MixedInferredReturnType occurrences="6">
1550715506
<code>array</code>
1550815507
<code>array</code>
1550915508
<code>array</code>

0 commit comments

Comments
 (0)