Re: [Tiki-devel] Unicode-aware sort plugin
Volker Wysk <post-hhF2Jplw28UoZk/[email protected]>
| Newsgroups | gmane.comp.cms.tiki.devel |
|---|---|
| Message-ID | <[email protected]> |
Am Donnerstag, dem 29.06.2023 um 19:06 +0200 schrieb Volker Wysk: > Hi! > > The Sort Plugin is kinda broken, because it isn't Unicode aware. That's not > that easy to fix, because the PHP natcasesort() function isn't Unicode > aware. And there is no mb_natcasesort(). It needs to be done differently. > > I've found on the web, how it is to be done, here: > > https://stackoverflow.com/questions/58577558/how-to-use-multiple-sort-flags-in-php-array-sorting-using-sort-locale-string > > I've applied it to the wikiplugin_sort and it works. However, this plugin > needs to use the correct locale for the user. I don't know how to get this > locale. This bit is still missing. > > I've attached the new wikiplugin_sort.php. > > Someone with more Tiki knowledge should improve this version, by querying > the locale to be used and pass it to the collator_create function. I've figured it out (hopefully). The new version sorts naturally and in accordance to the user's locale. I've attached it, again. I'm about to do a merge request next. Cheers, Volker _______________________________________________ TikiWiki-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/tikiwiki-devel
wikiplugin_sort.php
(application/x-php, 2.7 KB)
<?php
// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
//
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
// $Id$
function wikiplugin_sort_info()
{
return [
'name' => tra('Sort'),
'documentation' => 'PluginSort',
'description' => tra('Sort lines of text'),
'prefs' => [ 'wikiplugin_sort' ],
'body' => tra('Data to sort, one entry per line.'),
'filter' => 'text',
'iconname' => 'sort-desc',
'introduced' => 1,
'tags' => [ 'basic' ],
'params' => [
'sort' => [
'required' => false,
'name' => tra('Order'),
'description' => tra('Set the sort order of lines of content (default is ascending)'),
'since' => '1',
'filter' => 'alpha',
'default' => 'asc',
'options' => [
['text' => '', 'value' => ''],
['text' => tra('Ascending'), 'value' => 'asc'],
['text' => tra('Descending'), 'value' => 'desc'],
['text' => tra('Reverse'), 'value' => 'reverse'],
['text' => tra('Shuffle'), 'value' => 'shuffle']
]
]
]
];
}
function wikiplugin_sort($data, $params)
{
$tikilib = TikiLib::lib('tiki');
$loginlib = TikiLib::lib('login');
extract($params, EXTR_SKIP);
$sort = (isset($sort)) ? $sort : "asc";
$lines = preg_split("/\n+/", $data, -1, PREG_SPLIT_NO_EMPTY); // separate lines into array
// $lines = array_filter( $lines, "chop" ); // remove \n
srand((float) microtime() * 1000000); // needed for shuffle;
// Determine the language of the user
$user = $loginlib->getUser();
$lang = $tikilib->get_language($user);
// Set up the collator for the natural ordering and the language of the user.
$coll = collator_create($lang);
collator_set_attribute($coll, Collator::NUMERIC_COLLATION, Collator::ON);
collator_set_attribute($coll, Collator::CASE_FIRST, Collator::LOWER_FIRST);
if ($sort == "asc") {
// Sort ascending
collator_asort($coll, $lines);
} elseif ($sort == "desc") {
// Sort descending
collator_asort($coll, $lines);
$lines = array_reverse($lines);
} elseif ($sort == "reverse") {
// Reverse the lines
$lines = array_reverse($lines);
} elseif ($sort == "shuffle") {
// Shuffle the lines
shuffle($lines);
}
reset($lines);
if (is_array($lines)) {
$data = implode("\n", $lines);
}
$data = trim($data);
return $data;
}
signature.asc
(application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6QXGh82Ov3+2nrxp+K4ydFOsHoUFAmSf9qIACgkQ+K4ydFOs HoV2bw//SLGEDRh6vbDd2nmjSWxr5DwweuJ3iVINpNNk8sqr4azcQmTHrKzS+bIE Rk7F5wzUiX6fiQlLpQ0M0fxQ3DpUEglpf9pkKi2ssdPVeCua3EnB4cecm88SWaVf 1CiTMW7rGFhB6XiebEL2n5hQSSk6+V2wxEk44qsHomkkm2feaaoK1ykvUgjvNTIi jgaawond40GowfaP8QEHXoHoeZiYTuUjbYtvq2+JJjyQfx06wWNbyevOvsY1SUNB vtqV90rZQllXux8xxycRFgmG793z+08dl80MVAb6A56cBxyOFDZQJEXegW2hctqQ fPkZRf+xXn8fEUscSmUvUlfaYn3eHi7iUA2snGPqNlKrMZ5VyX0UJ0y1QhjkR6gx C4CSN/uYQe0QJqU6vYNoO6HcLomdu7XiY6QX9Zhk0dQmpENk5I0YGtNlET0gzmua qXkspY1j/VXmpvBJJT7s71nqFcW77d5uCvv26jZepUj8/knOzxpF0I2i1hRj+zkI ORzjgv+kr9xwTEkZokTbZeKmUgbLQHPUiQCYFXdeAxe4vXudMcecKYyf9Os1Ikpx RZQ68Bl0tyKIxrMuEBcPTAdC8NqLhAVIAhZMb21PP2ywC22j7RDPatkQFBH/Shpp lMVLIdb0ZpJa4mrcwRwGnZMrZcgF6Z1Ej81NVxfeNp0ufL+7Tc0= =MohO -----END PGP SIGNATURE-----