[Tiki-devel] Unicode-aware sort plugin

Volker Wysk <post-hhF2Jplw28UoZk/[email protected]>
Newsgroups gmane.comp.cms.tiki.devel
Message-ID <[email protected]>
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 suspect that this state of affairs occurs in a few other places as well,
which use "natural ordering".

Cheers,
Volker

_______________________________________________
TikiWiki-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-devel
wikiplugin_sort.php (application/x-php, 2.4 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)
{
    global $tikilib;

    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;

    # XXX Need to insert correct locale here!
    $coll = collator_create("de_DE");

    collator_set_attribute($coll, Collator::NUMERIC_COLLATION, Collator::ON);
    collator_set_attribute($coll, Collator::CASE_FIRST, Collator::LOWER_FIRST);

    if ($sort == "asc") {
        collator_asort($coll, $lines);
    } elseif ($sort == "desc") {
        collator_asort($coll, $lines);
        $lines = array_reverse($lines);
    } elseif ($sort == "reverse") {
        $lines = array_reverse($lines);
    } elseif ($sort == "shuffle") {
        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+K4ydFOsHoUFAmSduf4ACgkQ+K4ydFOs
HoU7mA//QWmhUkSnnaSWaa7FkpB1Kftb5AaeQMx5jBCA3Lycq6pSm5r37FXMi0Tx
8HdFMnfO/BsEOh8bJMYEHhPy62aYzJRawJTTP+ZEXPOBcoHC0m9gkYYzNs5Ez63S
arf8bv53aMnpsyjNLFJMKJfJ6c+YMpUqUy52rBuF3cZl5knzbqDQfac9Es2oCISA
l0uYc0aQ4u3Zy+a8+Dg8SZfLvms1FqbfhbaTP+mfgufYhnnzb+hajXPGLPs6mKth
wojlLH2A50HeINxgVQXo/+yg7Hqk+cJr+u/t7Q/IbGBiCaGjsuEF1GLEp8bnvOsy
6ky7xFpTL8JdNN0xhBMKAqXeFzFSvtXOipZMWXu4+oEI101nVPYbnB259mQUxd9z
4PGX5XsonMkrI6prcgbvDDfMNAxnZD7nh+KTIdYL4ZkrUTykj9Wo+scgfXb6USv3
Y3d3muNnvsoiNw44ZCnhWAbQ23yXZP4NN/Pf5v7GGnd6h4VZjENM+/dA3TI9p4Y9
qqjJvNo1ZeLR1e+n6I3nzh6dTjOC/5r/0MwalSImK59xCKi7b3oaTvzcj/Avf3QY
NQtsYoSTiFcRhgbXzMIsXX7RDkye6CSpmH7UuLfr6V6WuolnAwyGeLYw13Azrq3Y
Fe3LLVau9ch+5IzSPvUyCIdjT2FIfh1Euv66cs5HxufNBaO868o=
=G4N4
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.