Skip to content

Commit

Permalink
Move usortComparisonCallback to Query\Utilities and remove 2 globals
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed May 31, 2020
1 parent 7e9d2ae commit aadb6ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
39 changes: 2 additions & 37 deletions libraries/classes/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,15 +792,12 @@ public function getDatabasesFull(
* (caused by older MySQL < 5 or $GLOBALS['cfg']['NaturalOrder'])
*/
if ($apply_limit_and_order_manual) {
$GLOBALS['callback_sort_order'] = $sort_order;
$GLOBALS['callback_sort_by'] = $sort_by;
usort(
$databases,
static function ($a, $b) {
return self::usortComparisonCallback($a, $b);
static function ($a, $b) use ($sort_by, $sort_order) {
return Utilities::usortComparisonCallback($a, $b, $sort_by, $sort_order);
}
);
unset($GLOBALS['callback_sort_order'], $GLOBALS['callback_sort_by']);

/**
* now apply limit
Expand All @@ -813,38 +810,6 @@ static function ($a, $b) {
return $databases;
}

/**
* usort comparison callback
*
* @param array $a first argument to sort
* @param array $b second argument to sort
*
* @return int a value representing whether $a should be before $b in the
* sorted array or not
*
* @access private
*/
private static function usortComparisonCallback($a, $b): int
{
if ($GLOBALS['cfg']['NaturalOrder']) {
$sorter = 'strnatcasecmp';
} else {
$sorter = 'strcasecmp';
}
/* No sorting when key is not present */
if (! isset($a[$GLOBALS['callback_sort_by']], $b[$GLOBALS['callback_sort_by']])
) {
return 0;
}

// produces f.e.:
// return -1 * strnatcasecmp($a["SCHEMA_TABLES"], $b["SCHEMA_TABLES"])
return ($GLOBALS['callback_sort_order'] == 'ASC' ? 1 : -1) * $sorter(
$a[$GLOBALS['callback_sort_by']],
$b[$GLOBALS['callback_sort_by']]
);
}

/**
* returns detailed array with all columns for sql
*
Expand Down
36 changes: 36 additions & 0 deletions libraries/classes/Query/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use PhpMyAdmin\Url;
use function htmlspecialchars;
use function strcasecmp;
use function strnatcasecmp;
use function strpos;
use function strtolower;

Expand Down Expand Up @@ -115,4 +117,38 @@ public static function formatError(int $error_number, string $error_message): st

return $error;
}

/**
* usort comparison callback
*
* @param array $a first argument to sort
* @param array $b second argument to sort
* @param string $sortBy Key to sort by
* @param string $sortOrder The order (ASC/DESC)
*
* @return int a value representing whether $a should be before $b in the
* sorted array or not
*/
public static function usortComparisonCallback(array $a, array $b, string $sortBy, string $sortOrder): int
{
global $cfg;

/* No sorting when key is not present */
if (! isset($a[$sortBy], $b[$sortBy])
) {
return 0;
}

// produces f.e.:
// return -1 * strnatcasecmp($a['SCHEMA_TABLES'], $b['SCHEMA_TABLES'])
$compare = $cfg['NaturalOrder'] ? strnatcasecmp(
$a[$sortBy],
$b[$sortBy]
) : strcasecmp(
$a[$sortBy],
$b[$sortBy]
);

return ($sortOrder === 'ASC' ? 1 : -1) * $compare;
}
}

0 comments on commit aadb6ab

Please sign in to comment.