2014-02-18 15:29:05 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-22 10:45:36 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author AW-UC <git@a-wesemann.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-02-18 15:29:05 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-02-18 15:29:05 +04:00
|
|
|
namespace OC;
|
|
|
|
|
|
|
|
class NaturalSort {
|
|
|
|
private static $instance;
|
|
|
|
private $collator;
|
2015-01-20 15:40:18 +03:00
|
|
|
private $cache = array();
|
2014-02-18 15:29:05 +04:00
|
|
|
|
2015-02-20 16:03:34 +03:00
|
|
|
/**
|
|
|
|
* Instantiate a new \OC\NaturalSort instance.
|
|
|
|
* @param object $injectedCollator
|
|
|
|
*/
|
|
|
|
public function __construct($injectedCollator = null) {
|
|
|
|
// inject an instance of \Collator('en_US') to force using the php5-intl Collator
|
|
|
|
// or inject an instance of \OC\NaturalSort_DefaultCollator to force using Owncloud's default collator
|
|
|
|
if (isset($injectedCollator)) {
|
|
|
|
$this->collator = $injectedCollator;
|
2015-07-03 15:06:40 +03:00
|
|
|
\OCP\Util::writeLog('core', 'forced use of '.get_class($injectedCollator), \OCP\Util::DEBUG);
|
2015-02-20 16:03:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 15:29:05 +04:00
|
|
|
/**
|
|
|
|
* Split the given string in chunks of numbers and strings
|
|
|
|
* @param string $t string
|
|
|
|
* @return array of strings and number chunks
|
|
|
|
*/
|
|
|
|
private function naturalSortChunkify($t) {
|
|
|
|
// Adapted and ported to PHP from
|
|
|
|
// http://my.opera.com/GreyWyvern/blog/show.dml/1671288
|
2015-01-20 15:40:18 +03:00
|
|
|
if (isset($this->cache[$t])) {
|
|
|
|
return $this->cache[$t];
|
|
|
|
}
|
2014-02-18 15:29:05 +04:00
|
|
|
$tz = array();
|
|
|
|
$x = 0;
|
|
|
|
$y = -1;
|
|
|
|
$n = null;
|
|
|
|
|
2015-01-20 15:40:18 +03:00
|
|
|
while (isset($t[$x])) {
|
2014-02-18 15:29:05 +04:00
|
|
|
$c = $t[$x];
|
2014-07-23 14:52:13 +04:00
|
|
|
// only include the dot in strings
|
|
|
|
$m = ((!$n && $c === '.') || ($c >= '0' && $c <= '9'));
|
2014-02-18 15:29:05 +04:00
|
|
|
if ($m !== $n) {
|
|
|
|
// next chunk
|
|
|
|
$y++;
|
|
|
|
$tz[$y] = '';
|
|
|
|
$n = $m;
|
|
|
|
}
|
|
|
|
$tz[$y] .= $c;
|
|
|
|
$x++;
|
|
|
|
}
|
2015-01-20 15:40:18 +03:00
|
|
|
$this->cache[$t] = $tz;
|
2014-02-18 15:29:05 +04:00
|
|
|
return $tz;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the string collator
|
2014-09-30 15:34:50 +04:00
|
|
|
* @return \Collator string collator
|
2014-02-18 15:29:05 +04:00
|
|
|
*/
|
|
|
|
private function getCollator() {
|
|
|
|
if (!isset($this->collator)) {
|
|
|
|
// looks like the default is en_US_POSIX which yields wrong sorting with
|
|
|
|
// German umlauts, so using en_US instead
|
|
|
|
if (class_exists('Collator')) {
|
|
|
|
$this->collator = new \Collator('en_US');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->collator = new \OC\NaturalSort_DefaultCollator();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->collator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two strings to provide a natural sort
|
2014-09-30 15:34:50 +04:00
|
|
|
* @param string $a first string to compare
|
|
|
|
* @param string $b second string to compare
|
|
|
|
* @return int -1 if $b comes before $a, 1 if $a comes before $b
|
2014-02-18 15:29:05 +04:00
|
|
|
* or 0 if the strings are identical
|
|
|
|
*/
|
|
|
|
public function compare($a, $b) {
|
|
|
|
// Needed because PHP doesn't sort correctly when numbers are enclosed in
|
|
|
|
// parenthesis, even with NUMERIC_COLLATION enabled.
|
|
|
|
// For example it gave ["test (2).txt", "test.txt"]
|
|
|
|
// instead of ["test.txt", "test (2).txt"]
|
|
|
|
$aa = self::naturalSortChunkify($a);
|
|
|
|
$bb = self::naturalSortChunkify($b);
|
|
|
|
|
2015-01-20 15:40:18 +03:00
|
|
|
for ($x = 0; isset($aa[$x]) && isset($bb[$x]); $x++) {
|
2014-02-18 15:29:05 +04:00
|
|
|
$aChunk = $aa[$x];
|
|
|
|
$bChunk = $bb[$x];
|
|
|
|
if ($aChunk !== $bChunk) {
|
2015-01-20 15:40:18 +03:00
|
|
|
// test first character (character comparison, not number comparison)
|
|
|
|
if ($aChunk[0] >= '0' && $aChunk[0] <= '9' && $bChunk[0] >= '0' && $bChunk[0] <= '9') {
|
2014-02-18 15:29:05 +04:00
|
|
|
$aNum = (int)$aChunk;
|
|
|
|
$bNum = (int)$bChunk;
|
|
|
|
return $aNum - $bNum;
|
|
|
|
}
|
|
|
|
return self::getCollator()->compare($aChunk, $bChunk);
|
|
|
|
}
|
|
|
|
}
|
2015-01-20 15:40:18 +03:00
|
|
|
return count($aa) - count($bb);
|
2014-02-18 15:29:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a singleton
|
|
|
|
* @return \OC\NaturalSort instance
|
|
|
|
*/
|
|
|
|
public static function getInstance() {
|
|
|
|
if (!isset(self::$instance)) {
|
|
|
|
self::$instance = new \OC\NaturalSort();
|
|
|
|
}
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
}
|